-
Notifications
You must be signed in to change notification settings - Fork 16
/
configure.cpp
620 lines (589 loc) · 24.2 KB
/
configure.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/** Configure
*/
#include "configure.h"
using namespace ttx;
int Configure::DirExists(std::string *path)
{
struct stat info;
if(stat(path->c_str(), &info ) != 0)
return 0;
else if(info.st_mode & S_IFDIR)
return 1;
else
return 0;
}
Configure::Configure(vbit::Debug *debug, int argc, char** argv) :
_debug(debug),
// settings for generation of packet 8/30
_initialMag(1),
_initialPage(0x00),
_initialSubcode(0x3F7F),
_NetworkIdentificationCode(0x0000),
_CountryNetworkIdentificationCode(0x0000),
_reservedBytes{0x15, 0x15, 0x15, 0x15}, // initialise reserved bytes to hamming 8/4 encoded 0
_serviceStatusString(20, ' '),
_subtitleRepeats(1)
{
_configFile = CONFIGFILE;
#ifdef RASPBIAN
_pageDir = "/home/pi/teletext";
#else
_pageDir = "./pages"; // a relative path as a sensible default
#endif
// This is where the default header template is defined.
_headerTemplate = "VBIT2 %%# %%a %d %%b" "\x03" "%H:%M:%S";
// the default command interface port
_commandPort = 5570;
_commandPortEnabled = false;
_reverseBits = false;
_rowAdaptive = false;
_linesPerField = 16; // default to 16 lines per field
_datacastLines = 0; // no dedicated datacast lines
_multiplexedSignalFlag = false; // using this would require changing all the line counting and a way to send full field through raspi-teletext - something for the distant future when everything else is done...
_OutputFormat = T42; // t42 output is the default behaviour
_PID = 0x20; // default PID is 0x20
_packetServerPort = 0; // port 0 disables packet server
_datacastServerPort = 0;
uint8_t priority[8]={9,3,3,6,3,3,5,6}; // 1=High priority,9=low. Note: priority[0] is mag 8
for (int i=0; i<8; i++)
_magazinePriority[i] = priority[i];
//Scan the command line for overriding the pages file.
if (argc>1)
{
for (int i=1;i<argc;++i)
{
std::string arg = argv[i];
if (arg == "--dir")
{
if (i + 1 < argc)
_pageDir = argv[++i];
else
{
std::cerr << "--dir requires an argument\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--format")
{
if (i + 1 < argc)
{
arg = argv[++i];
if (arg == "none")
{
_OutputFormat = None;
}
else if (arg == "t42")
{
_OutputFormat = T42;
}
else if (arg == "raw")
{
_OutputFormat = Raw;
}
else if (arg == "ts")
{
_OutputFormat = TS;
}
else if (arg == "tsnpts")
{
_OutputFormat = TSNPTS;
}
else
{
std::cerr << "invalid --format type\n";
exit(EXIT_FAILURE);
}
if (_reverseBits && _OutputFormat != T42)
{
std::cerr << "--reverse requires t42 format\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--format requires an argument\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--reverse")
{
_reverseBits = true;
if (_OutputFormat != T42)
{
std::cerr << "--reverse requires t42 format\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--pid")
{
if (i + 1 < argc)
{
std::istringstream ss(argv[++i]);
ss >> _PID;
if (_PID < 0x20 || _PID >= 0x1FFF || !ss.eof())
{
std::cerr << "invalid PID\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--pid requires an argument\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--reserved")
{
if (i + 1 < argc)
{
// Take a 32 bit hexadecimal value to set the four reserved bytes in the BSDP
// Store bytes big endian so that the order digits appear on the command line is the same as they appear in packet
errno = 0;
char *end_ptr;
unsigned long l = std::strtoul(argv[++i], &end_ptr, 16);
if (errno == 0 && *end_ptr == '\0')
{
_reservedBytes[0] = (l >> 24) & 0xff;
_reservedBytes[1] = (l >> 16) & 0xff;
_reservedBytes[2] = (l >> 8) & 0xff;
_reservedBytes[3] = (l >> 0) & 0xff;
}
else
{
std::cerr << "invalid reserved bytes argument\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--reserved requires an argument\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--debug")
{
if (i + 1 < argc)
{
errno = 0;
char *end_ptr;
long l = std::strtol(argv[++i], &end_ptr, 10);
if (errno == 0 && *end_ptr == '\0' && l > -1)
{
switch(l){
case 0:
_debug->SetDebugLevel(vbit::Debug::LogLevels::logNONE);
break;
case 1:
_debug->SetDebugLevel(vbit::Debug::LogLevels::logERROR);
break;
case 2:
_debug->SetDebugLevel(vbit::Debug::LogLevels::logWARN);
break;
case 3:
_debug->SetDebugLevel(vbit::Debug::LogLevels::logINFO);
break;
case 4:
default:
_debug->SetDebugLevel(vbit::Debug::LogLevels::logDEBUG);
break;
}
}
else
{
std::cerr << "invalid debug level argument\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--debug requires an argument\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--packetserver")
{
if (i + 1 < argc)
{
errno = 0;
char *end_ptr;
long l = std::strtol(argv[++i], &end_ptr, 10);
if (errno == 0 && *end_ptr == '\0' && l > 0 && l < 65536)
{
_packetServerPort = (int)l;
}
else
{
std::cerr << "invalid server port number\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--packetserver requires a port number\n";
exit(EXIT_FAILURE);
}
}
else if (arg == "--datacast")
{
if (i + 1 < argc)
{
errno = 0;
char *end_ptr;
long l = std::strtol(argv[++i], &end_ptr, 10);
if (errno == 0 && *end_ptr == '\0' && l > 0 && l < 65536)
{
_datacastServerPort = (int)l;
}
else
{
std::cerr << "invalid server port number\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "--datacast requires a port number\n";
exit(EXIT_FAILURE);
}
}
else
{
std::cerr << "unrecognised argument: " << arg << std::endl;
exit(EXIT_FAILURE);
}
}
}
if (!DirExists(&_pageDir))
{
std::stringstream ss;
ss << _pageDir << " does not exist or is not a directory\n";
std::cerr << ss.str();
exit(EXIT_FAILURE);
}
// TODO: allow overriding config file from command line
_debug->Log(vbit::Debug::LogLevels::logINFO,"[Configure::Configure] Pages directory is " + _pageDir);
_debug->Log(vbit::Debug::LogLevels::logINFO,"[Configure::Configure] Config file is " + _configFile);
std::string path;
path = _pageDir;
path += "/";
path += _configFile;
LoadConfigFile(path); // load main config file (vbit.conf)
LoadConfigFile(path+".override"); // allow overriding main config file for local configuration where main config is in version control
if (_datacastLines > _linesPerField)
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"[Configure] datacast lines cannot be greater than lines per field");
_datacastLines = _linesPerField; // clamp
}
}
Configure::~Configure()
{
}
int Configure::LoadConfigFile(std::string filename)
{
std::ifstream filein(filename.c_str()); // Open the file
std::vector<std::string>::iterator iter;
// these are all the valid strings for config lines
std::vector<std::string> nameStrings{ "header_template", "initial_teletext_page", "row_adaptive_mode", "network_identification_code", "country_network_identification", "full_field", "status_display", "subtitle_repeats","enable_command_port","command_port","lines_per_field","datacast_lines","magazine_priority"};
if (filein.is_open())
{
_debug->Log(vbit::Debug::LogLevels::logINFO,"[Configure::LoadConfigFile] opened " + filename);
std::string line;
std::string name;
std::string value;
TTXLine* header = new TTXLine();
while (std::getline(filein >> std::ws, line))
{
if (line.front() != ';') // ignore comments
{
std::size_t delim = line.find("=", 0);
int error = 0;
if (delim != std::string::npos)
{
name = line.substr(0, delim);
value = line.substr(delim + 1);
iter = find(nameStrings.begin(), nameStrings.end(), name);
if(iter != nameStrings.end())
{
// matched string
switch(iter - nameStrings.begin())
{
case 0: // header_template
{
header->Setm_textline(value,true);
value = header->GetLine();
value.resize(32,' ');
_headerTemplate.assign(value);
break;
}
case 1: // initial_teletext_page
{
if (value.size() >= 3)
{
size_t idx;
int magpage;
int subcode;
try
{
magpage = stoi(std::string(value, 0, 3), &idx, 16);
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
if (magpage < 0x100 || magpage > 0x8FF || (magpage & 0xFF) == 0xFF)
{
error = 1;
break;
}
if (value.size() > 3)
{
if ((value.size() != 8) || (value.at(idx) != ':'))
{
error = 1;
break;
}
try
{
subcode = stoi(std::string(value, 4, 4), &idx, 16);
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
if (subcode < 0x0000 || subcode > 0x3F7F || subcode & 0xC080)
{
error = 1;
break;
}
_initialSubcode = subcode;
}
_initialMag = magpage / 0x100;
_initialPage = magpage % 0x100;
break;
}
error = 1;
break;
}
case 2: // row_adaptive_mode
{
if (!value.compare("true"))
{
_rowAdaptive = true;
}
else if (!value.compare("false"))
{
_rowAdaptive = false;
}
else
{
error = 1;
}
break;
}
case 3: // "network_identification_code" - four character hex. eg. FA6F
{
if (value.size() == 4)
{
size_t idx;
try
{
_NetworkIdentificationCode = stoi(std::string(value, 0, 4), &idx, 16);
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 4: // "country_network_identification" - four character hex. eg. 2C2F
{
if (value.size() == 4)
{
size_t idx;
try
{
_CountryNetworkIdentificationCode = stoi(std::string(value, 0, 4), &idx, 16);
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 5: // "full_field"
{
break;
}
case 6: // "status_display"
{
value.resize(20,' '); // string must be 20 characters
_serviceStatusString.assign(value);
break;
}
case 7: // "subtitle_repeats" - The number of times a subtitle transmission is repeated 0..9
{
if (value.size() == 1)
{
try
{
_subtitleRepeats = stoi(std::string(value, 0, 1));
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 8: // "enable_command_port"
{
if (!value.compare("true"))
{
_commandPortEnabled = true;
}
else if (!value.compare("false"))
{
_commandPortEnabled = false;
}
else
{
error = 1;
}
break;
}
case 9: // "command_port"
{
if (value.size() > 0 && value.size() < 6)
{
try
{
_commandPort = stoi(std::string(value, 0, 5));
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 10: // "lines_per_field"
{
if (value.size() > 0 && value.size() < 4)
{
try
{
_linesPerField = stoi(std::string(value, 0, 3));
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 11: // datacast_lines
{
if (value.size() > 0 && value.size() < 4)
{
try
{
_datacastLines = stoi(std::string(value, 0, 3));
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
}
else
{
error = 1;
}
break;
}
case 12: // "magazine_priority"
{
std::stringstream ss(value);
std::string temps;
int tmp[8];
int i;
for (i=0; i<8; i++)
{
if (std::getline(ss, temps, ','))
{
try
{
tmp[i] = stoi(temps);
}
catch (const std::invalid_argument& ia)
{
error = 1;
break;
}
if (!(tmp[i] > 0 && tmp[i] < 10)) // must be 1-9
{
error = 1;
break;
}
}
else
{
error = 1;
break;
}
}
for (i=0; i<8; i++)
_magazinePriority[i] = tmp[i];
break;
}
}
}
else
{
error = 1; // unrecognised config line
}
}
if (error)
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"[Configure::LoadConfigFile] invalid config line: " + line);
}
}
}
filein.close();
return 0;
}
else
{
_debug->Log(vbit::Debug::LogLevels::logWARN,"[Configure::LoadConfigFile] open failed");
return -1;
}
}