forked from peterkvt80/vbit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.cpp
569 lines (500 loc) · 14.4 KB
/
packet.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
#include "packet.h"
using namespace vbit;
Packet::Packet() :
_isHeader(false),
_mag(1),
_coding(CODING_7BIT_TEXT)
{
//ctor
SetMRAG(8,25);
_packet[45] = '\0'; // ensure termination
}
Packet::Packet(char *val) : _isHeader(false), _mag(1), _page(999), _row(99)
{
//ctor
strncpy(_packet,val,45);
_packet[45] = '\0'; // ensure termination
}
Packet::Packet(std::string val) : _isHeader(false), _mag(1), _page(999), _row(99)
{
//ctor
strncpy(_packet,val.c_str(),45);
_packet[45] = '\0'; // ensure termination
}
Packet::Packet(int mag, int row, std::string val) : _isHeader(false), _mag(mag), _page(999), _row(row)
{
SetMRAG(_mag, _row);
SetPacketText(val);
assert(_row!=0); // Use Header for row 0
_packet[45] = '\0'; // ensure termination
}
void Packet::SetRow(int mag, int row, std::string val, PageCoding coding)
{
int triplet;
int designationcode;
SetMRAG(mag, row);
SetPacketText(val);
_coding = coding;
// The following block of code is for page enhancement
// packets read from special OL rows in tti page files.
// If OL,28, packet is used the page file should not contain a non zero RE,
if (coding == CODING_13_TRIPLETS)
{
// Special handler to allow stuffing enhancement packets in as OL rows
// Each 18 bits of data for a triplet is coded in the input line as
// three bytes least significant first where each byte contains 6 data
// bits in b0-b5.
designationcode = _packet[5] & 0x0F;
_packet[5] = HamTab[designationcode]; // designation code is 8/4 hamming coded
/* 0x0a and 0x00 in the hammed output is causing a problem so disable this until they are fixed (output will be gibberish) */
for (int i = 1; i<=13; i++){
//std::cerr << "[Packet::SetRow] enhancement " << std::hex << (_packet[i*3+3] & 0x3F) << " " << ((_packet[i*3+4]) & 0x3F) << " " << ((_packet[i*3+5]) & 0x3F) << std::endl;
triplet = _packet[i*3+3] & 0x3F;
triplet |= ((_packet[i*3+4]) & 0x3F) << 6;
triplet |= ((_packet[i*3+5]) & 0x3F) << 12;
SetTriplet(i, triplet);
}
}
else if (coding == CODING_HAMMING_8_4)
{
for (int i = 0; i<40; i++)
{
_packet[5+i] = HamTab[_packet[5+i] & 0x0F];
}
}
}
Packet::~Packet()
{
//dtor
}
void Packet::Set_packet(char *val)
{
//std::cerr << "[Packet::Set_packet] todo. Implement copy" << std::endl;
strncpy(&_packet[5],val,40);
}
void Packet::SetPacketRaw(char *val)
{
// this is a raw copy of 40 bytes into our packet. use with caution.
memcpy(&_packet[5],val,40);
_coding = CODING_8BIT_DATA; // don't allow this to be re-processed with parity etc
}
void Packet::SetPacketText(std::string val)
{
_isHeader=false; // Because it can't be a header
strncpy(&_packet[5],val.c_str(),40);
}
// Clear entire packet to 0
void Packet::PacketQuiet()
{
uint8_t i;
for (i=0;i<PACKETSIZE;i++)
_packet[i]=0;
}
// Set CRI and MRAG. Leave the rest of the packet alone
void Packet::SetMRAG(uint8_t mag, uint8_t row)
{
char *p=_packet;
*p++=0x55; // clock run in
*p++=0x55; // clock run in
*p++=0x27; // framing code
// add MRAG
*p++=HamTab[mag%8+((row%2)<<3)]; // mag + bit 3 is the lowest bit of row
*p++=HamTab[((row>>1)&0x0f)];
_isHeader=row==0;
_row=row;
_mag=mag;
} // SetMRAG
/** get_offset_time. @todo Convert this to c++
* Given a parameter of say %t+02
* where str[2] is + or -
* str[4:3] is a two digit of half hour offsets from UTC
* @return local time at offset from UTC
*/
bool Packet::get_offset_time(char* str)
{
char strTime[6];
time_t rawtime;
struct tm tmGMT;
time( &rawtime );
// What is our offset in seconds?
int offset=((str[3]-'0')*10+str[4]-'0')*30*60; // @todo We really ought to validate this
// Is it negative (west of us?)
if (str[2]=='-')
offset=-offset;
else
if (str[2]!='+') return false; // Must be + or -
// Add the offset to the time value
rawtime+=offset;
gmtime_r(&rawtime, &tmGMT);
strftime(strTime, 21, "%H:%M", &tmGMT);
strncpy(str,strTime,5);
return true; // @todo
}
/* Ideally we would set _packet[0] for other hardware, or _packet[3] for Alistair Buxton raspi-teletext/
* but hard code this for now
* Most of this should be rewritten for c++
*/
char* Packet::tx(bool reverse)
{
// Get local time
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
// @TODO: parity
if (_isHeader) // We can do header substitutions
{
// mpp page number. Use %%#
char* ptr2=strstr(_packet,"%%#");
if (ptr2)
{
if (_mag==0)
ptr2[0]='8';
else
ptr2[0]=_mag+'0';
//std::cerr << "[Packet::tx]page=" << _page << std::endl;
ptr2[1]=_page/0x10+'0';
if (ptr2[1]>'9')
ptr2[1]=ptr2[1]-'0'-10+'A'; // Particularly poor hex conversion algorithm
//std::cerr << "[Packet::tx]ptr[1]=" << ptr2[1] << std::endl;
ptr2[2]=_page%0x10+'0';
if (ptr2[2]>'9')
ptr2[2]=ptr2[2]-'0'-10+'A'; // Particularly poor hex conversion algorithm
//std::cerr << "[Packet::tx]ptr[2]=" << ptr2[2] << std::endl;
}
char tmpstr[11];
ptr2=strstr(_packet,"%%a"); // Tue
if (ptr2)
{
strftime(tmpstr,10,"%a",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
ptr2[2]=tmpstr[2];
}
ptr2=strstr(_packet,"%%b"); // Jan
if (ptr2)
{
strftime(tmpstr,10,"%b",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
ptr2[2]=tmpstr[2];
}
ptr2=strstr(_packet,"%d"); // day of month with leading zero
if (ptr2)
{
strftime(tmpstr,10,"%d",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%e"); // day of month with no leading zero
if (ptr2)
{
#ifndef WIN32
strftime(tmpstr,10,"%e",timeinfo);
ptr2[0]=tmpstr[0];
#else
strftime(tmpstr,10,"%d",timeinfo);
if (tmpstr[0] == '0')
ptr2[0]=' ';
else
ptr2[0]=tmpstr[0];
#endif
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%m"); // month number with leading 0
if (ptr2)
{
strftime(tmpstr,10,"%m",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%y"); // year. 2 digits
if (ptr2)
{
strftime(tmpstr,10,"%y",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%H"); // hour.
if (ptr2)
{
strftime(tmpstr,10,"%H",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%M"); // minutes.
if (ptr2)
{
strftime(tmpstr,10,"%M",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
ptr2=strstr(_packet,"%S"); // seconds.
if (ptr2)
{
strftime(tmpstr,10,"%S",timeinfo);
ptr2[0]=tmpstr[0];
ptr2[1]=tmpstr[1];
}
Parity(13); // apply parity to the text of the header
}
else if (_coding == CODING_7BIT_TEXT) // Other text rows
{
char *tmpptr;
for (int i=5;i<45;i++) _packet[i]=_packet[i] & 0x7f;
// ======= TEMPERATURE ========
char strtemp[]=" ";
#ifdef RASPBIAN
tmpptr=strstr((char*)_packet,"%%%T");
if (tmpptr) {
get_temp(strtemp);
strncpy(tmpptr,strtemp,4);
}
#endif
// ======= WORLD TIME ========
// Special case for world time. Put %t<+|-><hh> to get local time HH:MM offset by +/- half hours
for (;;)
{
tmpptr=strstr((char*) _packet,"%t+");
if (!tmpptr) {
tmpptr=strstr((char*) _packet,"%t-");
}
if (tmpptr) {
//std::cout << "[test 1]" << _packet << std::endl;
get_offset_time(tmpptr);
//exit(4);
}
else
break;
}
// ======= NETWORK ========
#ifndef WIN32
// Special case for network address. Put %%%%%%%%%%%%%%n to get network address in form xxx.yyy.zzz.aaa with trailing spaces (15 characters total)
tmpptr=strstr((char*)_packet,"%%%%%%%%%%%%%%n");
if (tmpptr) {
// strncpy(tmpptr,"not yet working",15);
get_net(strtemp);
strncpy(tmpptr,strtemp,15);
}
#endif
// ======= TIME AND DATE ========
// Special case for system time. Put %%%%%%%%%%%%timedate to get time and date
tmpptr=strstr((char*) _packet,"%%%%%%%%%%%%timedate");
if (tmpptr) {
get_time(strtemp);
strncpy(tmpptr,strtemp,20);
}
// ======= VERSION ========
// %%%V version number eg. 2.00
tmpptr=strstr((char*) _packet,"%%%V");
if (tmpptr) {
strncpy(tmpptr,"2.01",4); // @todo: Move this value to somewhere more obvious
}
Parity(5); // redo the parity because substitutions will need processing
}
if (reverse)
{
for (int i=0;i<PACKETSIZE;i++)
{
_packet[i]=_vbi_bit_reverse[(uint8_t)(_packet[i])];
}
}
return &_packet[3]; // For raspi-pi we skip clock run in and framing code
}
/** A header has mag, row=0, page, flags, caption and time
*/
void Packet::Header(unsigned char mag, unsigned char page, unsigned int subcode, unsigned int control)
{
uint8_t cbit;
SetMRAG(mag,0);
_packet[5]=HamTab[page%0x10];
_packet[6]=HamTab[page/0x10];
_packet[7]=HamTab[(subcode&0x0f)]; // S1 four bits
subcode>>=4;
// Map the page settings control bits from MiniTED to actual teletext packet.
// To find the MiniTED settings look at the tti format document.
// To find the target bit position these are in reverse order to tx and not hammed.
// So for each bit in ETSI document, just divide the bit number by 2 to find the target location.
// Where ETSI says bit 8,6,4,2 this maps to 4,3,2,1 (where the bits are numbered 1 to 8)
cbit=0;
if (control & 0x4000) cbit=0x08; // C4 Erase page
_packet[8]=HamTab[(subcode&0x07) | cbit]; // S2 (3 bits) add C4
subcode>>=4;
_packet[9]=HamTab[(subcode&0x0f)]; // S3 four bits
subcode>>=4;
cbit=0;
// Not sure if these bits are reversed. C5 and C6 are indistinguishable
if (control & 0x0002) cbit=0x08; // C6 Subtitle
if (control & 0x0001) cbit|=0x04; // C5 Newsflash
// cbit|=0x08; // TEMPORARY!
_packet[10]=HamTab[(subcode&0x03) | cbit]; // S4 C6, C5
cbit=0;
if (control & 0x0004) cbit=0x01; // C7 Suppress Header TODO: Check if these should be reverse order
if (control & 0x0008) cbit|=0x02; // C8 Update
if (control & 0x0010) cbit|=0x04; // C9 Interrupted sequence
if (control & 0x0020) cbit|=0x08; // C10 Inhibit display
_packet[11]=HamTab[cbit]; // C7 to C10
cbit=(control & 0x0380) >> 6; // Shift the language bits C12,C13,C14. TODO: Check if C12/C14 need swapping. CHECKED OK.
// if (control & 0x0040) cbit|=0x01; // C11 serial/parallel *** We only work in parallel mode, Serial would mean a different packet ordering.
_packet[12]=HamTab[cbit]; // C11 to C14 (C11=0 is parallel, C12,C13,C14 language)
_page=page;
} // Header
void Packet::HeaderText(std::string val)
{
_isHeader=true; // Because it must be a header
strncpy(&_packet[13],val.c_str(),32);
}
/**
* @brief Check parity.
* \param Offset is normally 5 for rows, 13 for header
*/
void Packet::Parity(uint8_t offset)
{
int i;
//uint8_t c;
for (i=offset;i<PACKETSIZE;i++)
{
_packet[i]=ParTab[(uint8_t)(_packet[i]&0x7f)];
}
} // Parity
void Packet::Fastext(int* links, int mag)
{
unsigned long nLink;
// add the designation code
char *p;
uint8_t i;
p=_packet+5;
*p++=HamTab[0]; // Designation code 0
mag&=0x07; // Mask the mag just in case. Keep it valid
// add the link control byte. This will allow row 24 to show.
_packet[42]=HamTab[0x0f];
// and the page CRC
_packet[43]=HamTab[0]; // Don't know how to calculate this.
_packet[44]=HamTab[0];
// for each of the six links
for (i=0; i<6; i++)
{
nLink=links[i];
if (nLink == 0) nLink = 0x8ff; // turn zero into 8FF to be ignored
// calculate the relative magazine
char cRelMag=(nLink/0x100 ^ mag);
*p++=HamTab[nLink & 0xF]; // page units
*p++=HamTab[(nLink & 0xF0) >> 4]; // page tens
*p++=HamTab[0xF]; // subcode S1
*p++=HamTab[((cRelMag & 1) << 3) | 7];
*p++=HamTab[0xF];
*p++=HamTab[((cRelMag & 6) << 1) | 3];
}
}
#ifdef RASPBIAN
/** get_temp
* Pinched from raspi-teletext demo.c
* @return Four character temperature in degrees C eg. "45.7"
*/
bool Packet::get_temp(char* str)
{
FILE *fp;
char *pch;
char tmp[100];
fp = popen("/usr/bin/vcgencmd measure_temp", "r");
fgets(tmp, 99, fp);
pclose(fp);
pch = strtok (tmp,"=\n");
pch = strtok (NULL,"=\n");
strncpy(str,pch,5);
return true; // @todo
}
#endif
/** get_time
* Pinched from raspi-teletext demo.c
* @return Time as 20 characters
*/
bool Packet::get_time(char* str)
{
time_t rawtime;
struct tm *info;
time( &rawtime );
info = localtime( &rawtime );
strftime(str, 21, "\x02%a %d %b\x03%H:%M/%S", info);
return false; // @todo
}
#ifndef WIN32
/** get_net
* Pinched from raspi-teletext demo.c
* @return network address as 20 characters
* Sample response
* 3: wlan0 inet 192.168.1.14/24 brd 192.168.1.255 scope global wlan0\ valid_lft forever preferred_lft forever
*/
bool Packet::get_net(char* str)
{
FILE *fp;
char *pch;
int n;
char temp[100];
fp = popen("/sbin/ip -o -f inet addr show scope global", "r");
fgets(temp, 99, fp);
pclose(fp);
pch = strtok (temp," \n/");
for (n=1; n<4; n++)
{
pch = strtok (NULL, " \n/");
}
// If we don't have a connection established, try not to crash
if (pch==NULL)
{
strcpy(str,"IP address????");
return false;
}
strncpy(str,pch,15);
return true; // @todo
}
#endif
void Packet::SetTriplet(int ix, int triplet)
{
//std::cerr << "[Packet::SetTriplet] enters " << std::hex << "ix=" << ix << " triplet=" << triplet << std::endl;
uint8_t t[4];
if (ix<1) return;
vbi_ham24p(t,triplet);
// Now stuff the result in the packet
_packet[ix*3+3]=t[0];
_packet[ix*3+4]=t[1];
_packet[ix*3+5]=t[2];
}
void Packet::vbi_ham24p(uint8_t * p, unsigned int c)
{
unsigned int D5_D11;
unsigned int D12_D18;
unsigned int P5, P6;
unsigned int Byte_0;
Byte_0 = (_vbi_hamm24_fwd_0 [(c >> 0) & 0xFF]
^ _vbi_hamm24_fwd_1 [(c >> 8) & 0xFF]
^ _vbi_hamm24_fwd_2 [(c >> 16) & 0x03]);
p[0] = Byte_0;
D5_D11 = (c >> 4) & 0x7F;
D12_D18 = (c >> 11) & 0x7F;
P5 = 0x80 & ~(_vbi_hamm24_inv_par[0][D12_D18] << 2);
p[1] = D5_D11 | P5;
P6 = 0x80 & ((_vbi_hamm24_inv_par[0][Byte_0]
^ _vbi_hamm24_inv_par[0][D5_D11]) << 2);
p[2] = D12_D18 | P6;
//std::cerr << "[Packet::vbi_ham24p] leaves " << std::hex << (p[0] | (p[1] << 8) | (p[2] << 16)) << std::endl;
}
void Packet::Dump()
{
std::cerr << "[Packet::Dump] Enters" << std::endl;
for (int i=0; i<45; i++)
{
int ch=_packet[i];
std::cerr << std::setfill('0') << std::setw(2) << std::hex << ch << " ";
}
std::cerr << std::endl;
for (int i=0; i<45; i++)
{
char ch=_packet[i];
if (ch<' ') ch='*';
if (ch>'~') ch='*';
std::cerr << " " << ch << " ";
}
std::cerr << std::endl;
std::cerr << std::dec;
}