-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpage.cpp
More file actions
555 lines (499 loc) · 14.1 KB
/
page.cpp
File metadata and controls
555 lines (499 loc) · 14.1 KB
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
#include "page.h"
using namespace vbit;
Page::Page() :
_carouselPage(nullptr)
{
ClearPage(); // initialises variables
}
Page::~Page()
{
//std::cerr << "Page dtor\n";
}
void Page::AppendSubpage(std::shared_ptr<Subpage> s)
{
s->SetMagazine(_pageNumber >> 8); // tell subpage what magazine it is in for fastext
_subpages.push_back(s);
if (_carouselPage == nullptr)
StepFirstSubpage();
}
void Page::InsertSubpage(std::shared_ptr<Subpage> s)
{
s->SetMagazine(_pageNumber >> 8); // tell subpage what magazine it is in for fastext
for (std::list<std::shared_ptr<Subpage>>::iterator it=_subpages.begin();it!=_subpages.end();++it)
{
// find first subpage with a higher subcode
std::shared_ptr<Subpage> ptr = *it;
if (ptr->GetSubCode() > s->GetSubCode())
{
_subpages.insert(it,s);
return;
}
}
// if we are here we ran to the end of the list without a match
_subpages.push_back(s);
if (_carouselPage == nullptr)
StepFirstSubpage();
}
void Page::RemoveSubpage(std::shared_ptr<Subpage> s)
{
_subpages.remove(s);
if (_subpages.empty())
_carouselPage = nullptr;
else
{
_iter=_subpages.begin();
_carouselPage = *_iter;
}
}
void Page::ClearPage()
{
_pageNumber = 0; // an invalid page number
_pageCoding=CODING_7BIT_TEXT;
_pageFunction=LOP;
_carouselPage=nullptr;
_subpages.clear(); // empty subpage list
_iter=_subpages.begin(); // reset iterator
}
void Page::RenumberSubpages()
{
int count=0;
unsigned int subcode;
int code[4];
if (_subpages.size() == 1)
{
// A single page
// Annex A.1 states that pages with no sub-pages should be coded Mxx-0000. This is the default when no subcode is specified in tti file.
// Annex E.2 states that the subcode may be used to transmit a BCD time code, e.g for an alarm clock. Where a non zero subcode is specified in the tti file keep it.
if (Special())
{
// "Special" pages (e.g. MOT, POP, GPOP, DRCS, GDRCS, MIP) should be coded sequentially in hexadecimal 0000-000F
_subpages.front()->SetSubCode(0);
}
}
else if (_subpages.size() > 1)
{
// Page has subpages. Renumber according to Annex A.1.
for (int i=0;i<4;i++) code[i]=0;
std::list<std::shared_ptr<Subpage>>::iterator it;
for (it = _subpages.begin(); it != _subpages.end(); ++it)
{
if (Special())
{
// "Special" pages (e.g. MOT, POP, GPOP, DRCS, GDRCS, MIP) should be coded sequentially in hexadecimal 0000-000F
subcode = count;
}
else
{
// Pages intended for display with sub-pages should have sub-pages coded sequentially from Mxx-0001 to
// Mxx-0009 and then Mxx-0010 to Mxx-0019 and similarly using the decimal values of sub-code nibbles, from subcode 0001 up to 0079 as per ETS-300-706 Annex A.1.
// Pages intended for display shouldn't have more than 79 subpages, however we should handle it sensibly if they do, therefore above 0079 the numbers jump to 01nn to 09nn, then 1nnn to 3nnn.
// Increment the subcode is a baroque way
code[3]++; // increment units
if (code[3]>9) // if units > 9
{
code[3]=0; // units = 0
code[2]++; // increment tens
if (code[2]>7) // if tens > 7
{
code[2]=0; // tens = 0
code[1]++; // increment 'hundreds'
if (code[1]>9) // if 'hundreds' > 9
{
code[1]=0; // 'hundreds' = 0
code[0]++; // increment 'thousands'
if (code[0]>3) // if 'thousands' > 3
{
code[0]=0; // overflow subcode
code[1]=0;
code[2]=0;
code[3]=0;
}
}
}
}
subcode=(code[0]<<12) + (code[1]<<8) + (code[2]<<4) + code[3];
}
(*it)->SetSubCode(subcode); // modify the subcode
count++;
}
}
}
void Page::SetPageNumber(int page)
{
if ((page<0x100) || (page>0x8ff))
{
page = 0x8FF;
}
_pageNumber=page;
}
void Page::SetPageFunctionInt(int pageFunction)
{
switch (pageFunction)
{
default: // treat page functions we don't know as level one pages
case 0:
{
_pageFunction = LOP;
break;
}
case 2:
{
_pageFunction = GPOP;
break;
}
case 3:
{
_pageFunction = POP;
break;
}
case 4:
{
_pageFunction = GDRCS;
break;
}
case 5:
{
_pageFunction = DRCS;
break;
}
case 6:
{
_pageFunction = MOT;
break;
}
case 7:
{
_pageFunction = MIP;
break;
}
case 8:
{
_pageFunction = BTT;
break;
}
case 9:
{
_pageFunction = AIT;
break;
}
case 10:
{
_pageFunction = MPT;
break;
}
case 11:
{
_pageFunction = MPT_EX;
break;
}
}
}
void Page::SetPageCodingInt(int pageCoding)
{
if (pageCoding != _pageCoding)
{
_pageCoding = ReturnPageCoding(pageCoding);
for (auto it = _subpages.begin(); it != _subpages.end(); ++it)
{
(*it)->SetSubpageChanged(); // page coding changed so CRC needs recalculating for each subpage
}
}
}
PageCoding Page::ReturnPageCoding(int pageCoding)
{
switch (pageCoding)
{
default: // treat codings we don't know yet as normal text.
case 0:
return CODING_7BIT_TEXT;
case 1:
return CODING_8BIT_DATA;
case 2:
return CODING_13_TRIPLETS;
case 3:
return CODING_HAMMING_8_4;
case 4:
return CODING_HAMMING_7BIT_GROUPS;
case 5:
return CODING_PER_PACKET;
}
}
bool Page::IsCarousel()
{
if (_subpages.size() > 1) // has multiple subpages
{
return true;
}
if (std::shared_ptr<Subpage> s = GetSubpage())
{
if (s->GetTimedMode() && s->GetSubpageStatus() & PAGESTATUS_C9_INTERRUPTED)
{
// interrupted sequence flag is set, and page is in timed mode, so treat as a 1 page carousel
return true;
}
}
return false;
}
void Page::StepFirstSubpage()
{
if (_subpages.empty())
{
_carouselPage = nullptr;
}
else
{
_iter=_subpages.begin();
_carouselPage = *_iter;
}
}
void Page::StepLastSubpage()
{
if (_subpages.empty())
{
_carouselPage = nullptr;
}
else
{
_iter=_subpages.end();
_carouselPage = *--_iter;
}
}
void Page::StepNextSubpageNoLoop()
{
if (_subpages.empty())
{
_carouselPage = nullptr;
}
else
{
if (_carouselPage==nullptr)
{
_iter=_subpages.begin();
}
else
{
++_iter;
}
if (_iter == _subpages.end())
{
_carouselPage = nullptr;
}
else
{
_carouselPage = *_iter;
}
if (_carouselPage != nullptr)
{
if (!(_carouselPage->GetSubpageStatus() & PAGESTATUS_TRANSMITPAGE))
StepNextSubpageNoLoop(); // skip over subpages if transmit flag not set
}
}
}
void Page::StepNextSubpage()
{
if (_subpages.empty())
{
_carouselPage = nullptr;
}
else
{
if (_carouselPage==nullptr)
{
_iter=_subpages.begin();
_carouselPage = *_iter;
}
else
{
if (_iter == _subpages.end())
_iter = _subpages.begin();
if (++_iter == _subpages.end())
_iter = _subpages.begin();
_carouselPage = *_iter;
}
if (!(_carouselPage->GetSubpageStatus() & PAGESTATUS_TRANSMITPAGE))
{
StepNextSubpageNoLoop(); // skip over subpages if transmit flag not set
}
}
}
// Find a subpage by subcode - Warning: this will only find the first match so don't let multiples into the list!
std::shared_ptr<Subpage> Page::LocateSubpage(uint16_t SubpageNumber)
{
for (std::list<std::shared_ptr<Subpage>>::iterator s=_subpages.begin();s!=_subpages.end();++s)
{
std::shared_ptr<Subpage> ptr = *s;
if (SubpageNumber==ptr->GetSubCode())
return ptr;
}
return nullptr;
}
// attempt to set current subpage by number
void Page::SetSubpage(uint16_t SubpageNumber)
{
if (std::shared_ptr<Subpage> s = LocateSubpage(SubpageNumber))
_carouselPage = s;
// no warning on failure
}
std::shared_ptr<TTXLine> Page::GetTxRow(uint8_t row)
{
// Return a line or nullptr if the row does not exist
std::shared_ptr<TTXLine> line=nullptr;
if (_carouselPage)
line=_carouselPage->GetRow(row);
if (line!=nullptr) // Found a line
{
return line;
}
// No more lines? return NULL.
return nullptr;
}
Subpage::Subpage() :
_subcode(0),
_status(0),
_cycleTime(1),
_timedMode(false),
_region(0),
_mag(0),
_lastPacket(0),
_subpageChanged(true),
_headerCRC(0),
_subpageCRC(0)
{
for (int i=0;i<=MAXROW;i++)
{
_lines[i]=nullptr; // delete rows
}
}
Subpage::~Subpage()
{
//std::cerr << "Subpage dtor\n";
}
std::shared_ptr<TTXLine> Subpage::GetRow(unsigned int row)
{
if (row>MAXROW)
{
return nullptr;
}
if (_lines[row]==nullptr && row>0 && row<26)
{
_lines[row].reset(new TTXLine()); // return a blank row for X/1-X/25
}
return _lines[row];
}
void Subpage::SetRow(unsigned int rownumber, std::shared_ptr<TTXLine> line)
{
unsigned int dc;
// assert(rownumber<=MAXROW);
if (rownumber>MAXROW) return;
if (rownumber == 26)
{
dc = line->GetCharAt(0) & 0x0F;
if ((dc + 26) > _lastPacket)
_lastPacket = dc + 26;
}
else if (rownumber < 26)
{
_subpageChanged = true; // page content within scope of CRC was changed
if (rownumber > _lastPacket)
_lastPacket = rownumber;
}
if (_lines[rownumber]==nullptr)
{
_lines[rownumber] = line; // Didn't exist before
}
else
{
if (rownumber<26) // Ordinary line
{
if (line->IsBlank())
{
// don't store blank lines - they will be generated on the fly if necessary
_lines[rownumber] = nullptr;
}
else
{
_lines[rownumber] = line;
}
}
else // Enhanced packet
{
// If the line already exists we want to add to linked list of different designation codes
_lines[rownumber]->AppendLine(line);
}
}
}
void Subpage::DeleteRow(unsigned int rownumber, int designationCode)
{
if (rownumber < 26)
_subpageChanged = true; // page content within scope of CRC was changed
if (rownumber < 26 || designationCode < 0 || designationCode > 15)
{
_lines[rownumber] = nullptr; // delete entire row
}
else
{
if (_lines[rownumber])
_lines[rownumber] = _lines[rownumber]->RemoveLine(designationCode); // delete specific dc
}
}
void Subpage::SetFastext(std::array<FastextLink, 6> links)
{
std::array<uint8_t, 40> line; // 40 bytes of packet data in CODING_HAMMING_8_4 form
uint16_t lp, ls;
uint8_t p=0;
line[p++] = 0x0; // designation code 0
line[37] = 0xf; // link control set
line[38] = 0;
line[39] = 0; // last two bytes get overwritten with page CRC by packetmag
for (uint8_t i=0; i<6; i++)
{
lp=links[i].page;
if (lp == 0) lp = 0x8ff; // turn zero into 8FF to be ignored
ls=links[i].subpage;
uint8_t m=(lp/0x100 ^ _mag); // calculate the relative magazine
line[p++]=lp & 0xF; // page units
line[p++]=(lp & 0xF0) >> 4; // page tens
line[p++]=ls & 0xF; // S1
line[p++]=((m & 1) << 3) | ((ls >> 4) & 0xF); // S2 + M1
line[p++]=((ls >> 8) & 0xF); // S3
line[p++]=((m & 6) << 1) | ((ls >> 4) & 0x3); // S4 + M2, M3
}
std::shared_ptr<TTXLine> ttxline(new TTXLine(line));
SetRow(27,ttxline);
}
bool Subpage::GetFastext(std::array<FastextLink, 6> *links)
{
std::shared_ptr<TTXLine> line = _lines[27];
if (line == nullptr)
return false; // no X/27
line = line->LocateLine(0);
if (line == nullptr)
return false; // no X/27/0
uint8_t p=1;
for (uint8_t i=0; i<6; i++)
{
uint8_t m;
uint16_t lp, ls;
lp = line->GetCharAt(p++) & 0xf; // page units
lp |= (line->GetCharAt(p++) & 0xf) << 4; // page tens
ls = line->GetCharAt(p++) & 0xf; // S1
m = (line->GetCharAt(p) & 0x8) >> 3; // M1
ls |= (line->GetCharAt(p++) & 0x7) << 4; // S2
ls |= (line->GetCharAt(p++) & 0xf) << 8; // S3
m |= (line->GetCharAt(p) & 0xc) >> 1; // M2 + M3
ls |= (line->GetCharAt(p++) & 0x3) << 12; // S4
links->at(i).page = ((m ^ _mag) << 8) | lp;
links->at(i).subpage = ls;
}
return true;
}
bool Subpage::HasHeaderChanged(uint16_t crc)
{
if (_headerCRC != crc)
{
// update stored CRC and signal change
_headerCRC = crc;
return true;
}
return false; // no change
}