-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombineChunks.cpp
686 lines (580 loc) · 19.3 KB
/
CombineChunks.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#include "pch.h"
#include <vector>
#include <bitset>
#include <exception>
#include "CombineChunks.h"
#include "BinPoly.h"
#define ITOA_BUFFER_SIZE 10
// Deklaration der static class members
char* CombineChunks::rest;
int CombineChunks::counter;
char* CombineChunks::lastRawMessage;
int CombineChunks::OKcounter; // Statistik Parityzähler
int CombineChunks::NOTOKcounter;
ADSBData* CombineChunks::rawQueue;
ICAOData* CombineChunks::icaoData;
BitMask CombineChunks::maskDF; // Downlink-Format (ADS-B: 17 oder 18)
BitMask CombineChunks::maskCA; // Capability
BitMask CombineChunks::maskICAO; // FlugzeugID
BitMask CombineChunks::maskDATA; // Daten
BitMask CombineChunks::maskTC; // Type Code
BitMask CombineChunks::maskPI; // Parität
BitMask CombineChunks::maskAP; // AP/DP für DF20,DF21
BitMask CombineChunks::maskMB; // MB für DF20,DF21
BitMask CombineChunks::maskBDS2; // BDS2 für DF20,DF21
CombineChunks::CombineChunks(ADSBData* rawQueue, ICAOData* icaoData)
{
rest = NULL;
counter = 0;
lastRawMessage = "";
OKcounter = 0;
NOTOKcounter = 0;
this->rawQueue = rawQueue;
this->icaoData = icaoData;
// Initialisierung der Bitmasken für Decodierung
maskDF = BitMask(5, 107); // Downlink Format für DF17 oder DF18
maskCA = BitMask(3, 104); // Capability für DF17 oder DF18
maskICAO = BitMask(24, 80); // FlugzeugID für DF17 oder DF18
maskDATA = BitMask(56, 24); // Daten für DF17 oder DF18
maskTC = BitMask(5, 75); // Type Code für DF17 oder DF18
maskPI = BitMask(24, 0); // Parität für DF17 oder DF18
maskAP = BitMask(24, 0); // AP/DP für DF20,DF21
maskMB = BitMask(56, 24); // MB für DF20,DF21
maskBDS2 = BitMask(8, 48); // BDS2 für DF20,DF21
}
uint32_t CombineChunks::parity(ADSBLongBitset rawMessageBits)
{
// Die folgende Annahme ist bewiesen (ADS-B Viewer Doc.pdf):
// ---------------------------------------------------------
// 1. Die Menge der boolschen Zahlen {0,1} erhält mit der Addition (logisches OR) und der
// Multiplikation (logisches AND) die Struktur eines Körpers.
//
// Damit kann man die Ergebnisse aus "Polynomial GCDs by Linear Algebra; Barry Dayton;
// Northeastern Illinois University; March 2004" für den Paritäts-Check benutzen.
//
BYTE* buffer = bitsToBin(rawMessageBits);
delete[] buffer;
//BinPoly poly = BinPoly(rawMessageBits);
//ADSBLongBitset generator("1111111111111010000001001");
//ADSBLongBitset res = poly.Divide(generator);
// TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST
ADSBLongBitset dataExample("10110"); // x^4+x^2+x
ADSBLongBitset generatorExample("110"); // x^2+x
BinPoly example = BinPoly(dataExample);
ADSBLongBitset res = example.Divide(generatorExample);
// TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST
return 0;
}
uint32_t CombineChunks::checkParity(BYTE* msg, int bits)
{
uint32_t mode_s_checksum_table[] =
{
0x3935ea, 0x1c9af5, 0xf1b77e, 0x78dbbf, 0xc397db, 0x9e31e9, 0xb0e2f0, 0x587178,
0x2c38bc, 0x161c5e, 0x0b0e2f, 0xfa7d13, 0x82c48d, 0xbe9842, 0x5f4c21, 0xd05c14,
0x682e0a, 0x341705, 0xe5f186, 0x72f8c3, 0xc68665, 0x9cb936, 0x4e5c9b, 0xd8d449,
0x939020, 0x49c810, 0x24e408, 0x127204, 0x093902, 0x049c81, 0xfdb444, 0x7eda22,
0x3f6d11, 0xe04c8c, 0x702646, 0x381323, 0xe3f395, 0x8e03ce, 0x4701e7, 0xdc7af7,
0x91c77f, 0xb719bb, 0xa476d9, 0xadc168, 0x56e0b4, 0x2b705a, 0x15b82d, 0xf52612,
0x7a9309, 0xc2b380, 0x6159c0, 0x30ace0, 0x185670, 0x0c2b38, 0x06159c, 0x030ace,
0x018567, 0xff38b7, 0x80665f, 0xbfc92b, 0xa01e91, 0xaff54c, 0x57faa6, 0x2bfd53,
0xea04ad, 0x8af852, 0x457c29, 0xdd4410, 0x6ea208, 0x375104, 0x1ba882, 0x0dd441,
0xf91024, 0x7c8812, 0x3e4409, 0xe0d800, 0x706c00, 0x383600, 0x1c1b00, 0x0e0d80,
0x0706c0, 0x038360, 0x01c1b0, 0x00e0d8, 0x00706c, 0x003836, 0x001c1b, 0xfff409,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
};
uint32_t crc = 0;
int offset = (bits == 112) ? 0 : (112 - 56);
for (int j = 0; j < bits; j++) {
int byte = j / 8;
int bit = j % 8;
int bitmask = 1 << (7 - bit);
// If bit is set, xor with corresponding table entry.
if (msg[byte] & bitmask)
crc ^= mode_s_checksum_table[j + offset];
}
return crc; // 24 bit checksum.
}
string CombineChunks::binToCharacter(BYTE* buffer, int size)
{
char itoabuffer[ITOA_BUFFER_SIZE];
string s = "";
for (int i = size - 1; i >= 0; --i)
{
BYTE low = buffer[i] & 0x0f;
BYTE high = (buffer[i] & 0xf0) >> 4;
memset(itoabuffer, 0, ITOA_BUFFER_SIZE);
_itoa_s((int)high, itoabuffer, 16); // 16: hexadezimal
s += itoabuffer;
memset(itoabuffer, 0, ITOA_BUFFER_SIZE);
_itoa_s((int)low, itoabuffer, 16); // 16: hexadezimal
s += itoabuffer;
}
return s;
}
string CombineChunks::reveseStringBytes(string in)
{
int len = in.size();
string out = in;
// Byte-Reihenfolge des "raw"-Strings invertieren
for (int i = 0; i < len; i += 2)
{
out[i] = in[len - 2 - i];
out[i + 1] = in[len - 1 - i];
}
return out;
}
string CombineChunks::reveseStringNibbles(string in)
{
int len = in.size();
string out = in;
// Nibble-Reihenfolge des "raw"-Strings invertieren
for (int i = 0; i < len; i += 2)
{
out[i] = in[i + 1];
out[i + 1] = in[i];
}
return out;
}
ADSBLongBitset CombineChunks::characterToBits(string raw)
{
// hexadezimalen ASCII-String in Bitformat konvertieren
ADSBLongBitset nibble;
ADSBLongBitset rawMessageBits;
int len = raw.size();
for (int i = 0; i < len; ++i)
{
switch (raw[i])
{
case '0': nibble = ADSBLongBitset("0000"); break;
case '1': nibble = ADSBLongBitset("0001"); break;
case '2': nibble = ADSBLongBitset("0010"); break;
case '3': nibble = ADSBLongBitset("0011"); break;
case '4': nibble = ADSBLongBitset("0100"); break;
case '5': nibble = ADSBLongBitset("0101"); break;
case '6': nibble = ADSBLongBitset("0110"); break;
case '7': nibble = ADSBLongBitset("0111"); break;
case '8': nibble = ADSBLongBitset("1000"); break;
case '9': nibble = ADSBLongBitset("1001"); break;
case 'a':
case 'A': nibble = ADSBLongBitset("1010"); break;
case 'b':
case 'B': nibble = ADSBLongBitset("1011"); break;
case 'c':
case 'C': nibble = ADSBLongBitset("1100"); break;
case 'd':
case 'D': nibble = ADSBLongBitset("1101"); break;
case 'e':
case 'E': nibble = ADSBLongBitset("1110"); break;
case 'f':
case 'F': nibble = ADSBLongBitset("1111"); break;
default:
throw(1);
}
rawMessageBits |= nibble;
if (i != len - 1)
rawMessageBits <<= 4; // Nibbel nach links verschieben (aber nicht das letzte)
}
return rawMessageBits;
}
BYTE* CombineChunks::bitsToBin(ADSBLongBitset bits)
{
// Bitformat in binäres Format konvertieren, der gelieferte Pointer muss wieder freigegeben werden
int sizeBits = bits.size();
int sizeBytes = sizeBits / 8;
BYTE* buffer = new BYTE[sizeBytes];
#define REVERS
#ifdef REVERS
int byteNdx = sizeBytes - 1;
#else
int byteNdx = 0;
#endif
for (int j = 0; j < sizeBytes; ++j)
{
BYTE byte = 0;
for (int i = 0; i < sizeBits; ++i)
{
int bitNdx = 8 * j + i % 8;
if (bits[bitNdx]) byte |= (1 << i);
}
buffer[byteNdx] = byte;
#ifdef REVERS
--byteNdx;
#else
++byteNdx;
#endif
}
return buffer;
}
uint32_t CombineChunks::checkParity(ADSBLongBitset rawMessageBits, ADSBMessageState& state)
{
// zum Test:
uint32_t ret = parity(rawMessageBits);
ADSBLongBitset Parity = maskPI.Apply(rawMessageBits);
long int parityMessage = Parity.to_ulong();
BYTE* buffer = bitsToBin(rawMessageBits);
uint32_t parityCalculated = checkParity(buffer, rawMessageBits.size());
delete[] buffer;
if (parityCalculated == parityMessage)
state = ADSBMessageState::OK;
else
state = ADSBMessageState::Parity;
return parityCalculated;
}
ADSBMessageTypeCode CombineChunks::typeCode(ADSBLongBitset TC)
{
int tc = TC.to_ulong();
ADSBMessageTypeCode typeCode {};
if (tc <= 4) typeCode = ADSBMessageTypeCode::AircraftID;
else if (tc <= 8) typeCode = ADSBMessageTypeCode::SurfacePos;
else if (tc <= 18) typeCode = ADSBMessageTypeCode::AirbornePosBaroAlti;
else if (tc <= 19) typeCode = ADSBMessageTypeCode::AirborneVelo;
else if (tc <= 22) typeCode = ADSBMessageTypeCode::AirbornePosGNSSHeight;
else if (tc <= 27) typeCode = ADSBMessageTypeCode::Reserved;
else if (tc <= 28) typeCode = ADSBMessageTypeCode::AircraftStatus;
else if (tc <= 29) typeCode = ADSBMessageTypeCode::TargetState;
else if (tc <= 31) typeCode = ADSBMessageTypeCode::AircraftOperationStatus;
return typeCode;
}
string CombineChunks::decodeAircraftIDs(ADSBLongBitset DATA)
{
// Jeweils 6 Bits codieren ein Zeichen der ID (DF17/18) oder Identification (DF20/21), Lookup-Tabelle
const char* lookup = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ##### ###############0123456789######";
int len = 8; // fix: 8 Zeichen
char* buffer = new char[len + 1];
memset(buffer, 0, len + 1);
int offset = 48;
for (int i = 0; i < len; ++i)
{
int c = 0;
c += DATA[offset + 0] * 1; // pow(2, 0);
c += DATA[offset + 1] * 2; // pow(2, 1);
c += DATA[offset + 2] * 4; // pow(2, 2);
c += DATA[offset + 3] * 8; // pow(2, 3);
c += DATA[offset + 4] * 16; // pow(2, 4);
c += DATA[offset + 5] * 32; // pow(2, 5);
buffer[i] = lookup[c];
offset -= 6;
}
string ret = buffer;
delete[] buffer;
return ret;
}
void CombineChunks::decodeDF17Message(ADSBLongBitset rawMessageBits, ADSBMessageCode code,
ADSBMessageState& state, unsigned long& icao, unsigned char& ca, string& airCraftID)
{
checkParity(rawMessageBits, state);
ADSBLongBitset CA = maskCA.Apply(rawMessageBits);
ADSBLongBitset ICAO = maskICAO.Apply(rawMessageBits);
ADSBLongBitset DATA = maskDATA.Apply(rawMessageBits);
ADSBLongBitset TC = maskTC.Apply(rawMessageBits);
// Auswertung Type Code
ADSBMessageTypeCode tc = typeCode(TC);
switch (tc)
{
case ADSBMessageTypeCode::AircraftID:
airCraftID = decodeAircraftIDs(DATA);
break;
case ADSBMessageTypeCode::SurfacePos:
break;
case ADSBMessageTypeCode::AirbornePosBaroAlti:
break;
case ADSBMessageTypeCode::AirborneVelo:
break;
case ADSBMessageTypeCode::AirbornePosGNSSHeight:
break;
case ADSBMessageTypeCode::Reserved:
break;
case ADSBMessageTypeCode::AircraftStatus:
break;
case ADSBMessageTypeCode::TargetState:
break;
case ADSBMessageTypeCode::AircraftOperationStatus:
break;
}
ca = (unsigned char)CA.to_ulong();
icao = ICAO.to_ulong();
}
void CombineChunks::decodeDF20Message(ADSBLongBitset rawMessageBits, ADSBMessageCode code, ADSBMessageState& state,
string& airCraftIdentification)
{
// ICAO bestimmen
ADSBLongBitset parity = maskPI.Apply(rawMessageBits);
long int parityMessage = parity.to_ulong();
ADSBMessageState stateLocal;
long int parityCalculated = checkParity(rawMessageBits, stateLocal);
// Aircraft-Identification bestimmen
ADSBLongBitset MB = maskMB.Apply(rawMessageBits);
ADSBLongBitset BDS2 = maskBDS2.Apply(MB);
switch (BDS2.to_ulong())
{
case 0x20:
airCraftIdentification = decodeAircraftIDs(MB);
break;
}
}
void CombineChunks::decodeMessage(string raw, ADSBMessageState& state, unsigned long& df,
ADSBMessageCode& code, unsigned long& icao, unsigned char& ca, string& airCraftID, string& airCraftIdentification)
{
state = ADSBMessageState::Unknown;
// Nibble-String in binäre Bits wandeln
ADSBLongBitset rawMessageBits = characterToBits(raw);
ADSBLongBitset DF = maskDF.Apply(rawMessageBits);
df = DF.to_ulong();
switch (df)
{
case 0: // ACAS
code = ADSBMessageCode::DF0;
break;
case 11: // All-Call Reply
code = ADSBMessageCode::DF11;
break;
case 16: // ACAS
code = ADSBMessageCode::DF16;
break;
case 17: // Extended Squitter
code = ADSBMessageCode::DF17;
decodeDF17Message(rawMessageBits, code, state, icao, ca, airCraftID);
break;
case 18: // Extended Squitter
code = ADSBMessageCode::DF18;
decodeDF17Message(rawMessageBits, code, state, icao, ca, airCraftID);
break;
case 20: // Comm-B, Mode-S EHS, Altitude
code = ADSBMessageCode::DF20;
decodeDF20Message(rawMessageBits, code, state, airCraftIdentification);
break;
case 21: // Comm-B, Mode-S EHS, Ident
code = ADSBMessageCode::DF21;
decodeDF20Message(rawMessageBits, code, state, airCraftIdentification);
break;
case 24: // Comm-D Extended Length Message (ELM)
code = ADSBMessageCode::DF24;
break;
default: // Unknown
code = ADSBMessageCode::DFUnknown;
break;
}
}
string* CombineChunks::decodedMessage(ADSBMessageState state, unsigned long df, ADSBMessageCode code,
unsigned long icao, unsigned char ca, string airCraftID, string airCraftIdentification)
{
string ret;
char itoabuffer[ITOA_BUFFER_SIZE];
switch (code)
{
case ADSBMessageCode::DF0: // ACAS
ret = "DF00 ";
break;
case ADSBMessageCode::DF11: // All-Call Reply
ret = "DF11 ";
break;
case ADSBMessageCode::DF16: // ACAS
ret = "DF16 ";
break;
case ADSBMessageCode::DF17: // Extended Squitter
ret = "DF17 ";
break;
case ADSBMessageCode::DF18: // Extended Squitter
ret = "DF18 ";
break;
case ADSBMessageCode::DF20: // Comm-B, Mode-S EHS, Altitude
ret = "DF20 ";
break;
case ADSBMessageCode::DF21: // Comm-B, Mode-S EHS, Ident
ret = "DF21 ";
break;
case ADSBMessageCode::DF24: // Comm-D Extended Length Message (ELM)
ret = "DF24 ";
break;
case ADSBMessageCode::DFUnknown: // Unknown
ret = "DF";
memset(itoabuffer, 0, ITOA_BUFFER_SIZE);
_itoa_s((int)df, itoabuffer, 10); // 10: dezimal
ret += itoabuffer;
ret += "?";
break;
}
switch (state)
{
case ADSBMessageState::Unknown:
ret += " UK";
break;
case ADSBMessageState::OK:
ret += " OK";
break;
case ADSBMessageState::Parity:
ret += " PA";
break;
}
// Auswertung Capability
if (ca != 0)
{
ret += " CA:";
memset(itoabuffer, 0, ITOA_BUFFER_SIZE);
_itoa_s((int)ca, itoabuffer, 10); // 10: dezimal
ret += itoabuffer;
}
// Auswertung ICA0
if (icao != 0)
{
ret += " ICAO:";
memset(itoabuffer, 0, ITOA_BUFFER_SIZE);
_itoa_s((int)icao, itoabuffer, 16); // 16: hexadezimal
ret += itoabuffer;
}
if (airCraftID.size() != 0)
{
ret += " ID: ";
ret += airCraftID;
}
if (airCraftIdentification.size() != 0)
{
ret += " ID: ";
ret += airCraftIdentification;
}
return new string(ret);
}
int CombineChunks::processMessage(char* s1, char* s2)
{
// Eine komplette Rawmessage Syntax-checken, parsen und in Message-Liste eintragen.
// Returnwert: 0: alles OK, -1: Syntaxfehler
// s1: Pointer auf erstes Zeichen der Rawmessage
// s2: Pointer auf letztes Zeichen der Rawmessage
// Struktur der Daten: !Zeitstempel*ADS-B-Message\r\n
// ^ ^ ^
// | | |
// s1 str1 s2
// Syntax-Check
if (*s1 != '!') return -1;
char* str1 = strpbrk(s1, "*");
if (str1 == NULL) return -1;
if (*s2 != '\n') return -1;
if (*(s2 - 1) != '\r') return -1;
// Parse
++s1; // "!" überspringen
int lenTimeStamp = str1 - s1;
char* timeStamp = new char[lenTimeStamp + 1];
strncpy_s(timeStamp, lenTimeStamp + 1, s1, lenTimeStamp);
*(timeStamp + lenTimeStamp) = 0;
++str1; // "*" überspringen
s2 = s2 - 2; // "\r\n" überspringen
int lenRawMsg = s2 - str1; // ";" nicht mitkopieren
char* rawMsg = new char[lenRawMsg + 1];
strncpy_s(rawMsg, lenRawMsg + 1, str1, lenRawMsg);
*(rawMsg + lenRawMsg) = 0;
// Doubletten im Eingangsdatenstrom ignorieren (unklar warum die auftreten)
if (strcmp(lastRawMessage, rawMsg) != 0)
{
lastRawMessage = rawMsg;
// Eintrag in Message-Liste
unsigned char ca = 0;
unsigned long icao = 0;
unsigned long df = 0;
string airCraftID;
string airCraftIdentification;
ADSBMessageCode code;
ADSBMessageState state;
// Raw-Message decodieren
decodeMessage(rawMsg, state, df, code, icao, ca, airCraftID, airCraftIdentification);
// Statistik Parityfehler
if (state == ADSBMessageState::OK) ++OKcounter;
if (state == ADSBMessageState::Parity) ++NOTOKcounter;
if (code != ADSBMessageCode::DF0) // keine DF0 Messages eintragen
{
ADSBMessage row;
row.timeMark = new string(timeStamp);
row.rawMessage = new string(rawMsg);
row.decodedMessage = decodedMessage(state, df, code, icao, ca, airCraftID, airCraftIdentification);
row.code = new ADSBMessageCode(code);
row.icao = new unsigned long(icao);
row.state = new ADSBMessageState(state);
rawQueue->push_back(row);
if (icao != 0) icaoData->insert(pair<unsigned long,char*>(icao, rawMsg)); // TODO: unter der icao die Daten aller Messages sammeln
}
}
delete[] timeStamp;
delete[] rawMsg;
return 0;
}
char* CombineChunks::duplicateBuffer(char* buffer)
{
int len = strlen(buffer);
char* cpy = new char[len + 1];
*(cpy + len) = 0; // 0-Terminierung
memcpy_s(cpy, len + 1, buffer, len);
return cpy;
}
char* CombineChunks::detectRawMessages(char* buffer)
{
// Erkennung und Listeneintrag aller Rawmessages in buffer.
// Rekursiv, aber keine Heap-Nutzung ausser in der Message-Liste.
// Es wird ein evtl. verbleibender Reststring in der Variable rest gespeichert.
if (buffer == NULL) return buffer;
if (strlen(buffer) == 0) return buffer;
char* str1 = strpbrk(buffer, "!");
if (str1 != NULL)
{
// Ende 1. ADS-B Message suchen
char* str2 = strpbrk(str1 + 1, "\n");
if (str2 != NULL)
{
// vollständige ADS-B Message gefunden -> in Message-Liste eintragen
if (processMessage(str1, str2) == -1)
throw(exception("CombineChunks SyntaxError Exception!!!"));
++str2; // "\n" überspringen
char* buff = NULL;
if (*str2 != NULL) buff = detectRawMessages(str2);
return buff;
}
return buffer;
}
return buffer;
}
void CombineChunks::nextChunk(char* buffer)
{
// Struktur der Daten in buffer: .....!Zeitstempel*ADS-B-Message\r\n!Zeitstempel*ADS-B-Message\r\n.....
if (buffer == NULL) return; // nichts zu tun!
int lenBuffer = strlen(buffer);
if (lenBuffer == 0) return; // nichts zu tun!
++counter; // Aufrufzähler
if (counter == 1)
{
// 1. Aufruf
char* str1 = strpbrk(buffer, "!");
if (str1 != NULL)
{
char* r = detectRawMessages(buffer); // "!" gefunden, Chunk vorher weglassen
if (r != NULL && strlen(r) != 0) rest = duplicateBuffer(r);
}
}
else
{
// Folgeaufruf
if (rest != NULL)
{
// Reststring vorhanden
int len1 = strlen(rest);
if (len1 != 0)
{
// Reststring vorne an buffer anhängen
int len2 = strlen(buffer); // Länge buffer
char* buff = new char[len1 + len2 + 1];
strcpy_s(buff, len1 + len2 + 1, rest);
strncpy_s(buff + len1, len2 + 1, buffer, len2);
*(buff + len1 + len2) = 0; // 0-Terminierung
delete rest;
rest = NULL;
char* r = detectRawMessages(buff);
if (r != NULL && strlen(r) != 0) rest = duplicateBuffer(r);
delete[] buff;
}
}
else
{
char* r = detectRawMessages(buffer); // kein Reststring vorhanden
if (r != NULL && strlen(r) != 0) rest = duplicateBuffer(r);
}
}
}