forked from mutability/dump1090
-
Notifications
You must be signed in to change notification settings - Fork 236
/
mode_s.c
2298 lines (2002 loc) · 74.9 KB
/
mode_s.c
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
//
// mode_s.c: Mode S message decoding.
//
// Copyright (c) 2014-2016 Oliver Jowett <[email protected]>
// Copyright (c) 2021 FlightAware LLC
//
// This file is free software: you may copy, redistribute and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 2 of the License, or (at your
// option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright (C) 2012 by Salvatore Sanfilippo <[email protected]>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "dump1090.h"
#include "ais_charset.h"
/* for PRIX64 */
#include <inttypes.h>
//
// ===================== Mode S detection and decoding ===================
//
//
//
/* A timestamp that indicates the data is synthetic, created from a
* multilateration result
*/
#define MAGIC_MLAT_TIMESTAMP 0xFF004D4C4154ULL
//=========================================================================
//
// Given the Downlink Format (DF) of the message, return the message length in bits.
//
// All known DF's 16 or greater are long. All known DF's 15 or less are short.
// There are lots of unused codes in both category, so we can assume ICAO will stick to
// these rules, meaning that the most significant bit of the DF indicates the length.
//
int modesMessageLenByType(int type) {
return (type & 0x10) ? MODES_LONG_MSG_BITS : MODES_SHORT_MSG_BITS ;
}
//
//=========================================================================
//
// In the squawk (identity) field bits are interleaved as follows in
// (message bit 20 to bit 32):
//
// C1-A1-C2-A2-C4-A4-ZERO-B1-D1-B2-D2-B4-D4
//
// So every group of three bits A, B, C, D represent an integer from 0 to 7.
//
// The actual meaning is just 4 octal numbers, but we convert it into a hex
// number tha happens to represent the four octal numbers.
//
// For more info: http://en.wikipedia.org/wiki/Gillham_code
//
static int decodeID13Field(int ID13Field) {
int hexGillham = 0;
if (ID13Field & 0x1000) {hexGillham |= 0x0010;} // Bit 12 = C1
if (ID13Field & 0x0800) {hexGillham |= 0x1000;} // Bit 11 = A1
if (ID13Field & 0x0400) {hexGillham |= 0x0020;} // Bit 10 = C2
if (ID13Field & 0x0200) {hexGillham |= 0x2000;} // Bit 9 = A2
if (ID13Field & 0x0100) {hexGillham |= 0x0040;} // Bit 8 = C4
if (ID13Field & 0x0080) {hexGillham |= 0x4000;} // Bit 7 = A4
//if (ID13Field & 0x0040) {hexGillham |= 0x0800;} // Bit 6 = X or M
if (ID13Field & 0x0020) {hexGillham |= 0x0100;} // Bit 5 = B1
if (ID13Field & 0x0010) {hexGillham |= 0x0001;} // Bit 4 = D1 or Q
if (ID13Field & 0x0008) {hexGillham |= 0x0200;} // Bit 3 = B2
if (ID13Field & 0x0004) {hexGillham |= 0x0002;} // Bit 2 = D2
if (ID13Field & 0x0002) {hexGillham |= 0x0400;} // Bit 1 = B4
if (ID13Field & 0x0001) {hexGillham |= 0x0004;} // Bit 0 = D4
return (hexGillham);
}
//
//=========================================================================
//
// Decode the 13 bit AC altitude field (in DF 20 and others).
// Returns the altitude, and set 'unit' to either UNIT_METERS or UNIT_FEET.
//
static int decodeAC13Field(int AC13Field, altitude_unit_t *unit) {
int m_bit = AC13Field & 0x0040; // set = meters, clear = feet
int q_bit = AC13Field & 0x0010; // set = 25 ft encoding, clear = Gillham Mode C encoding
if (!m_bit) {
*unit = UNIT_FEET;
if (q_bit) {
// N is the 11 bit integer resulting from the removal of bit Q and M
int n = ((AC13Field & 0x1F80) >> 2) |
((AC13Field & 0x0020) >> 1) |
(AC13Field & 0x000F);
// The final altitude is resulting number multiplied by 25, minus 1000.
return ((n * 25) - 1000);
} else {
// N is an 11 bit Gillham coded altitude
int n = modeAToModeC(decodeID13Field(AC13Field));
if (n < -12) {
return INVALID_ALTITUDE;
}
return (100 * n);
}
} else {
*unit = UNIT_METERS;
// TODO: Implement altitude when meter unit is selected
return INVALID_ALTITUDE;
}
}
//
//=========================================================================
//
// Decode the 12 bit AC altitude field (in DF 17 and others).
//
static int decodeAC12Field(int AC12Field, altitude_unit_t *unit) {
int q_bit = AC12Field & 0x10; // Bit 48 = Q
*unit = UNIT_FEET;
if (q_bit) {
/// N is the 11 bit integer resulting from the removal of bit Q at bit 4
int n = ((AC12Field & 0x0FE0) >> 1) |
(AC12Field & 0x000F);
// The final altitude is the resulting number multiplied by 25, minus 1000.
return ((n * 25) - 1000);
} else {
// Make N a 13 bit Gillham coded altitude by inserting M=0 at bit 6
int n = ((AC12Field & 0x0FC0) << 1) |
(AC12Field & 0x003F);
n = modeAToModeC(decodeID13Field(n));
if (n < -12) {
return INVALID_ALTITUDE;
}
return (100 * n);
}
}
//
//=========================================================================
//
// Decode the 7 bit ground movement field PWL exponential style scale (ADS-B v2)
//
static float decodeMovementFieldV2(unsigned movement) {
// Note : movement codes 0,125,126,127 are all invalid, but they are
// trapped for before this function is called.
// Each movement value is a range of speeds;
// we return the midpoint of the range (rounded to the nearest integer)
if (movement >= 125) return 0; // invalid
else if (movement == 124) return 180; // gs > 175kt, pick a value..
else if (movement >= 109) return 100 + (movement - 109 + 0.5) * 5; // 100 < gs <= 175 in 5kt steps
else if (movement >= 94) return 70 + (movement - 94 + 0.5) * 2; // 70 < gs <= 100 in 2kt steps
else if (movement >= 39) return 15 + (movement - 39 + 0.5) * 1; // 15 < gs <= 70 in 1kt steps
else if (movement >= 13) return 2 + (movement - 13 + 0.5) * 0.50; // 2 < gs <= 15 in 0.5kt steps
else if (movement >= 9) return 1 + (movement - 9 + 0.5) * 0.25; // 1 < gs <= 2 in 0.25kt steps
else if (movement >= 3) return 0.125 + (movement - 3 + 0.5) * 0.875 / 6; // 0.125 < gs <= 1 in 0.875/6 kt step
else if (movement >= 2) return 0.125 / 2; // 0 < gs <= 0.125
// 1: stopped, gs = 0
// 0: no data
else return 0;
}
//
//=========================================================================
//
// Decode the 7 bit ground movement field PWL exponential style scale (ADS-B v0)
//
static float decodeMovementFieldV0(unsigned movement) {
// Note : movement codes 0,125,126,127 are all invalid, but they are
// trapped for before this function is called.
// Each movement value is a range of speeds;
// we return the midpoint of the range
if (movement >= 125) return 0; // invalid
else if (movement == 124) return 180; // gs >= 175kt, pick a value..
else if (movement >= 109) return 100 + (movement - 109 + 0.5) * 5; // 100 < gs <= 175 in 5kt steps
else if (movement >= 94) return 70 + (movement - 94 + 0.5) * 2; // 70 < gs <= 100 in 2kt steps
else if (movement >= 39) return 15 + (movement - 39 + 0.5) * 1; // 15 < gs <= 70 in 1kt steps
else if (movement >= 13) return 2 + (movement - 13 + 0.5) * 0.50; // 2 < gs <= 15 in 0.5kt steps
else if (movement >= 9) return 1 + (movement - 9 + 0.5) * 0.25; // 1 < gs <= 2 in 0.25kt steps
else if (movement >= 2) return 0.125 + (movement - 2 + 0.5) * 0.125; // 0.125 < gs <= 1 in 0.125kt step
// 1: stopped, gs < 0.125kt
// 0: no data
else return 0;
}
// Apply possible corrections to the 14-byte message in "in", storing the result in "out"
//
// If the message has a correct CRC, copies in to out unchanged and returns 0
// If the message has correctable errors, applies the corrections to out and returns the number of corrected errors
// If the message is uncorrectable (this may mean the message type does not have CRC coverage), returns -1
// is this message a long-form message with a DF that uses Parity/Interrogator?
static bool isLongPIMessage(const unsigned char *msg)
{
const unsigned df = getbits(msg, 1, 5);
if (df == 17 || df == 18)
return true;
return false;
}
// is this message a short-form message with a DF that uses Parity/Interrogator?
static bool isShortPIMessage(const unsigned char *msg)
{
const unsigned df = getbits(msg, 1, 5);
return (df == 11); // assume IID==0
}
#define UNCHECKED_SYNDROME 0xFFFFFFFFU
static int correctMessage(const unsigned char *in, unsigned char *out, uint32_t *short_syndrome, uint32_t *long_syndrome)
{
// Possible DF values of the first byte of a message that could be a valid DF11/17/18
// message after correction. See tools/df-correction-arrays.py for generator code.
// This is used to shortcut message correction so that we don't bother computing a CRC over
// messages that couldn't possibly become one of those message types.
// These are bitsets, where the bit with value 1<<N represents a match for DF N
static const uint32_t df_correctable_short[MODES_MAX_BITERRORS + 1] = {
0x00000800, 0x08008e08, 0x08008e08
};
static const uint32_t df_correctable_long[MODES_MAX_BITERRORS + 1] = {
0x00060000, 0x066f0006, 0x6fff066f
};
*short_syndrome = UNCHECKED_SYNDROME;
*long_syndrome = UNCHECKED_SYNDROME;
// Try to correct, including corrections to the initial 5 bit DF field
// that determines message format
const unsigned uncorrected_df = getbits(in, 1, 5);
const uint32_t df_bit = 1 << uncorrected_df;
// Select the right bitset based on the maximum number of bit errors in the DF field that we could correct.
// nb: strictly speaking, --no-fix-df doesn't _entirely_ disable correction of the DF field when nfix_crc == 2
// (DF17 could be corrected to DF18 or vice versa), but it does disable the CPU hungry part of it.
const unsigned fix_df_bits = (Modes.fix_df ? Modes.nfix_crc : 0);
struct errorinfo *long_ei = NULL;
if (df_correctable_long[fix_df_bits] & df_bit) {
*long_syndrome = modesChecksum(in, MODES_LONG_MSG_BITS);
if (isLongPIMessage(in) && *long_syndrome == 0) {
// DF17/18 message with correct checksum
memcpy(out, in, MODES_LONG_MSG_BYTES);
return 0;
}
long_ei = modesChecksumDiagnose(*long_syndrome, MODES_LONG_MSG_BITS);
}
struct errorinfo *short_ei = NULL;
if (df_correctable_short[fix_df_bits] & df_bit) {
*short_syndrome = modesChecksum(in, MODES_SHORT_MSG_BITS);
if (isShortPIMessage(in) && (*short_syndrome & 0xFFFF80) == 0) {
// DF11 message with correct checksum
// (low 7 bits may be IID)
memcpy(out, in, MODES_SHORT_MSG_BYTES);
return 0;
}
short_ei = modesChecksumDiagnose(*short_syndrome, MODES_SHORT_MSG_BITS); // assume IID == 0
}
// Might be a damaged DF11/17/18, or might be another message type that doesn't have a full CRC
unsigned long_errors = (long_ei ? long_ei->errors : 999);
unsigned short_errors = (short_ei ? short_ei->errors : 999);
// If both 56-bit and 112-bit corrections are possible:
// try the correction with fewer error bits first
// if there's a tie, try the 112-bit version first
if (long_ei && long_errors <= short_errors) {
memcpy(out, in, MODES_LONG_MSG_BYTES);
modesChecksumFix(out, long_ei);
if (isLongPIMessage(out)) {
// valid DF17/18 message after corrections
return long_errors;
}
}
// Don't try to correct >1 error in DF11 (see crc.c)
if (short_ei && short_errors == 1) {
memcpy(out, in, MODES_SHORT_MSG_BYTES);
modesChecksumFix(out, short_ei);
if (isShortPIMessage(out)) {
// valid DF11 message after corrections
return short_errors;
}
}
if (long_ei && long_errors > short_errors) {
memcpy(out, in, MODES_LONG_MSG_BYTES);
modesChecksumFix(out, long_ei);
if (isLongPIMessage(out)) {
// valid DF17/18 message after corrections
return long_errors;
}
}
// Nothing more to try, we can't correct this one further
memcpy(out, in, MODES_LONG_MSG_BYTES);
return -1;
}
// Score how plausible this ModeS message looks.
// The more positive, the more reliable the message is.
score_rank scoreModesMessage(const unsigned char *uncorrected)
{
// This is a "valid" DF0 message, but it's not useful; we discard these messages
static const unsigned char all_zeros[MODES_SHORT_MSG_BYTES] = { 0, 0, 0, 0, 0, 0, 0 };
if (!memcmp(all_zeros, uncorrected, sizeof(all_zeros)))
return SR_ALL_ZEROS;
// try to produce a corrected DF11/17/18, including correcting the DF bits
unsigned char corrected[14];
uint32_t short_syndrome, long_syndrome;
int corrections = correctMessage(uncorrected, corrected, &short_syndrome, &long_syndrome);
unsigned df = getbits(corrected, 1, 5); // Downlink Format
switch (df) {
case 0: // short air-air surveillance
case 4: // surveillance, altitude reply
case 5: // surveillance, altitude reply
{
if (short_syndrome == UNCHECKED_SYNDROME)
short_syndrome = modesChecksum(corrected, MODES_SHORT_MSG_BITS);
bool recent = icaoFilterTest(short_syndrome);
return recent ? SR_UNRELIABLE_KNOWN : SR_UNRELIABLE_UNKNOWN;
}
case 16: // long air-air surveillance
case 20: // Comm-B, altitude reply
case 21: // Comm-B, identity reply
{
if (long_syndrome == UNCHECKED_SYNDROME)
long_syndrome = modesChecksum(corrected, MODES_LONG_MSG_BITS);
bool recent = icaoFilterTest(long_syndrome);
return recent ? SR_UNRELIABLE_KNOWN : SR_UNRELIABLE_UNKNOWN;
}
case 24: // Comm-D (ELM)
case 25: // Comm-D (ELM)
case 26: // Comm-D (ELM)
case 27: // Comm-D (ELM)
case 28: // Comm-D (ELM)
case 29: // Comm-D (ELM)
case 30: // Comm-D (ELM)
case 31: // Comm-D (ELM)
{
if (!Modes.enable_df24)
return SR_UNCORRECTABLE;
if (long_syndrome == UNCHECKED_SYNDROME)
long_syndrome = modesChecksum(corrected, MODES_LONG_MSG_BITS);
bool recent = icaoFilterTest(long_syndrome);
return recent ? SR_UNRELIABLE_KNOWN : SR_UNRELIABLE_UNKNOWN;
}
case 11:
{
// DF11 All-call reply
uint32_t addr = getbits(corrected, 9, 32);
if (short_syndrome == UNCHECKED_SYNDROME)
short_syndrome = modesChecksum(corrected, MODES_SHORT_MSG_BITS);
uint32_t iid = short_syndrome & 0x7F;
bool recent = icaoFilterTest(addr);
switch (corrections) {
case 0:
if (iid == 0)
return recent ? SR_DF11_ACQ_KNOWN : SR_DF11_ACQ_UNKNOWN;
else
return recent ? SR_DF11_IID_KNOWN : SR_DF11_IID_UNKNOWN;
case 1:
if (iid == 0)
return recent ? SR_DF11_ACQ_1ERROR_KNOWN : SR_DF11_ACQ_1ERROR_UNKNOWN;
else
return recent ? SR_DF11_IID_1ERROR_KNOWN : SR_DF11_IID_1ERROR_UNKNOWN;
default:
return SR_UNCORRECTABLE;
}
}
case 17: // Extended squitter
{
uint32_t addr = getbits(corrected, 9, 32);
bool recent = icaoFilterTest(addr);
switch (corrections) {
case 0:
return recent ? SR_DF17_KNOWN : SR_DF17_UNKNOWN;
case 1:
return recent ? SR_DF17_1ERROR_KNOWN : SR_DF17_1ERROR_UNKNOWN;
case 2:
return recent ? SR_DF17_2ERROR_KNOWN : SR_DF17_2ERROR_UNKNOWN;
default:
return SR_UNCORRECTABLE;
}
}
case 18: // Extended squitter/non-transponder
{
uint32_t addr = getbits(corrected, 9, 32);
bool recent = icaoFilterTest(addr | ICAO_FILTER_ADSB_NT); // only look for previous DF18 activity
switch (corrections) {
case 0:
return recent ? SR_DF18_KNOWN : SR_DF18_UNKNOWN;
case 1:
return recent ? SR_DF18_1ERROR_KNOWN : SR_DF18_1ERROR_UNKNOWN;
case 2:
return recent ? SR_DF18_2ERROR_KNOWN : SR_DF18_2ERROR_UNKNOWN;
default:
return SR_UNCORRECTABLE;
}
}
default:
// unknown message type
return SR_UNKNOWN_DF;
}
}
static const char *score_to_string(score_rank score)
{
switch (score) {
case SR_NOT_SET: return "NOT_SET";
case SR_UNKNOWN_THRESHOLD: return "UNKNOWN_THRESHOLD";
case SR_ACCEPT_THRESHOLD: return "ACCEPT_THRESHOLD";
case SR_ALL_ZEROS: return "ALL_ZEROS";
case SR_UNKNOWN_DF: return "UNKNOWN_DF";
case SR_UNCORRECTABLE: return "UNCORRECTABLE";
case SR_UNRELIABLE_UNKNOWN: return "UNRELIABLE_UNKNOWN";
case SR_UNRELIABLE_KNOWN: return "UNRELIABLE_KNOWN";
case SR_DF11_IID_1ERROR_UNKNOWN: return "DF11_IID_1ERROR_UNKNOWN";
case SR_DF11_ACQ_1ERROR_UNKNOWN: return "DF11_ACQ_1ERROR_UNKNOWN";
case SR_DF11_IID_UNKNOWN: return "DF11_IID_UNKNOWN";
case SR_DF11_ACQ_UNKNOWN: return "DF11_ACQ_UNKNOWN";
case SR_DF11_IID_1ERROR_KNOWN: return "DF11_IID_1ERROR_KNOWN";
case SR_DF11_ACQ_1ERROR_KNOWN: return "DF11_ACQ_1ERROR_KNOWN";
case SR_DF11_IID_KNOWN: return "DF11_IID_KNOWN";
case SR_DF11_ACQ_KNOWN: return "DF11_ACQ_KNOWN";
case SR_DF17_2ERROR_UNKNOWN: return "DF17_2ERROR_UNKNOWN";
case SR_DF17_2ERROR_KNOWN: return "DF17_2ERROR_KNOWN";
case SR_DF17_1ERROR_UNKNOWN: return "DF17_1ERROR_UNKNOWN";
case SR_DF17_1ERROR_KNOWN: return "DF17_1ERROR_KNOWN";
case SR_DF17_UNKNOWN: return "DF17_UNKNOWN";
case SR_DF17_KNOWN: return "DF17_KNOWN";
case SR_DF18_2ERROR_UNKNOWN: return "DF18_2ERROR_UNKNOWN";
case SR_DF18_2ERROR_KNOWN: return "DF18_2ERROR_KNOWN";
case SR_DF18_1ERROR_UNKNOWN: return "DF18_1ERROR_UNKNOWN";
case SR_DF18_1ERROR_KNOWN: return "DF18_1ERROR_KNOWN";
case SR_DF18_UNKNOWN: return "DF18_UNKNOWN";
case SR_DF18_KNOWN: return "DF18_KNOWN";
}
return "<bad value>";
}
static void decodeExtendedSquitter(struct modesMessage *mm);
//
//=========================================================================
//
// Decode a raw Mode S message demodulated as a stream of bytes by detectModeS(),
// and split it into fields populating a modesMessage structure.
//
// return 0 if all OK
// <0 if it's a bad message
//
int decodeModesMessage(struct modesMessage *mm, const unsigned char *in)
{
// score the message if needed (it might be coming off the network)
if (mm->score == SR_NOT_SET)
mm->score = scoreModesMessage(in);
if (mm->score < SR_UNKNOWN_THRESHOLD)
return -1;
if (mm->score < SR_ACCEPT_THRESHOLD)
return -2;
// Preserve the original uncorrected copy for later forwarding
memcpy(mm->verbatim, in, MODES_LONG_MSG_BYTES);
// Apply corrections to our local copy
uint32_t short_syndrome, long_syndrome;
int corrections = correctMessage(in, mm->msg, &short_syndrome, &long_syndrome);
const unsigned char *msg = mm->msg;
// Get the message type ASAP as other operations depend on this
mm->msgtype = getbits(msg, 1, 5); // Downlink Format
mm->msgbits = modesMessageLenByType(mm->msgtype);
if (mm->msgtype & 16) {
if (long_syndrome == UNCHECKED_SYNDROME)
long_syndrome = modesChecksum(mm->msg, MODES_LONG_MSG_BITS);
mm->crc = long_syndrome;
} else {
if (short_syndrome == UNCHECKED_SYNDROME)
short_syndrome = modesChecksum(mm->msg, MODES_SHORT_MSG_BITS);
mm->crc = short_syndrome;
}
mm->correctedbits = corrections > 0 ? corrections : 0;
mm->addr = 0;
// Do checksum work and set fields that depend on the CRC
switch (mm->msgtype) {
case 0: // short air-air surveillance
case 4: // surveillance, altitude reply
case 5: // surveillance, identity reply
case 16: // long air-air surveillance
// These message types use Address/Parity
// so we can't check the CRC and must infer the transmitter's address
mm->source = SOURCE_MODE_S;
mm->addr = mm->crc;
mm->reliable = 0;
break;
case 11: // All-call reply
// This message type uses Parity/Interrogator, i.e. our CRC syndrome is CL + IC from the uplink message
// which we can't see. So we don't know if the CRC is correct or not.
//
// however! CL + IC only occupy the lower 7 bits of the CRC. So if we ignore those bits when testing
// the CRC we can still try to detect/correct errors.
mm->IID = mm->crc & 0x7f;
mm->source = SOURCE_MODE_S_CHECKED;
mm->reliable = (mm->IID == 0 && mm->correctedbits == 0);
break;
case 17: // Extended squitter
case 18: { // Extended squitter/non-transponder
// These message types use Parity/Interrogator, but are specified to set II=0
mm->source = SOURCE_ADSB; // TIS-B decoding will override this if needed
mm->reliable = (mm->correctedbits == 0);
break;
}
case 20: // Comm-B, altitude reply
case 21: // Comm-B, identity reply
// These message types either use Address/Parity
// or Data Parity where the requested BDS is also xored into the top byte.
// So not only do we not know whether the CRC is right, we also don't know if
// the ICAO is right! Ow.
mm->source = SOURCE_MODE_S;
mm->addr = mm->crc;
mm->reliable = 0;
break;
case 24: // Comm-D (ELM)
case 25: // Comm-D (ELM)
case 26: // Comm-D (ELM)
case 27: // Comm-D (ELM)
case 28: // Comm-D (ELM)
case 29: // Comm-D (ELM)
case 30: // Comm-D (ELM)
case 31: // Comm-D (ELM)
// These messages use Address/Parity,
// and also use some of the DF bits to carry data. Remap them all to a single
// DF for simplicity.
mm->msgtype = 24;
mm->source = SOURCE_MODE_S;
mm->addr = mm->crc;
mm->reliable = 0;
break;
default:
// All other message types, we don't know how to handle their CRCs, give up
return -2;
}
// decode the bulk of the message
// AA (Address announced)
if (mm->msgtype == 11 || mm->msgtype == 17 || mm->msgtype == 18) {
mm->AA = mm->addr = getbits(msg, 9, 32);
}
// AC (Altitude Code)
if (mm->msgtype == 0 || mm->msgtype == 4 || mm->msgtype == 16 || mm->msgtype == 20) {
mm->AC = getbits(msg, 20, 32);
if (mm->AC) { // Only attempt to decode if a valid (non zero) altitude is present
mm->altitude_baro = decodeAC13Field(mm->AC, &mm->altitude_baro_unit);
if (mm->altitude_baro != INVALID_ALTITUDE)
mm->altitude_baro_valid = 1;
}
}
// AF (DF19 Application Field) not decoded
// CA (Capability)
if (mm->msgtype == 11 || mm->msgtype == 17) {
mm->CA = getbits(msg, 6, 8);
switch (mm->CA) {
case 0:
mm->airground = AG_UNCERTAIN;
break;
case 4:
mm->airground = AG_GROUND;
break;
case 5:
mm->airground = AG_AIRBORNE;
break;
case 6:
mm->airground = AG_UNCERTAIN;
break;
case 7:
mm->airground = AG_UNCERTAIN;
break;
}
}
// CC (Cross-link capability)
if (mm->msgtype == 0) {
mm->CC = getbit(msg, 7);
}
// CF (Control field, see Figure 2-2 ADS-B Message BaselineFormat Structure)
if (mm->msgtype == 18) {
mm->CF = getbits(msg, 6, 8);
}
// DR (Downlink Request)
if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
mm->DR = getbits(msg, 9, 13);
}
// FS (Flight Status)
if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
mm->FS = getbits(msg, 6, 8);
mm->alert_valid = 1;
mm->spi_valid = 1;
switch (mm->FS) {
case 0:
mm->airground = AG_UNCERTAIN;
break;
case 1:
mm->airground = AG_GROUND;
break;
case 2:
mm->airground = AG_UNCERTAIN;
mm->alert = 1;
break;
case 3:
mm->airground = AG_GROUND;
mm->alert = 1;
break;
case 4:
mm->airground = AG_UNCERTAIN;
mm->alert = 1;
mm->spi = 1;
break;
case 5:
mm->airground = AG_UNCERTAIN;
mm->spi = 1;
break;
default:
mm->spi_valid = 0;
mm->alert_valid = 0;
break;
}
}
// ID (Identity)
if (mm->msgtype == 5 || mm->msgtype == 21) {
// Gillham encoded Squawk
mm->ID = getbits(msg, 20, 32);
if (mm->ID) {
mm->squawk = decodeID13Field(mm->ID);
mm->squawk_valid = 1;
}
}
// KE (Control, ELM)
if (mm->msgtype == 24) {
mm->KE = getbit(msg, 4);
}
// MB (messsage, Comm-B)
if (mm->msgtype == 20 || mm->msgtype == 21) {
memcpy(mm->MB, &msg[4], 7);
decodeCommB(mm);
}
// MD (message, Comm-D)
if (mm->msgtype == 24) {
memcpy(mm->MD, &msg[1], 10);
}
// ME (message, extended squitter)
if (mm->msgtype == 17 || mm->msgtype == 18) {
memcpy(mm->ME, &msg[4], 7);
decodeExtendedSquitter(mm);
}
// MV (message, ACAS)
if (mm->msgtype == 16) {
memcpy(mm->MV, &msg[4], 7);
}
// ND (number of D-segment, Comm-D)
if (mm->msgtype == 24) {
mm->ND = getbits(msg, 5, 8);
}
// RI (Reply information, ACAS)
if (mm->msgtype == 0 || mm->msgtype == 16) {
mm->RI = getbits(msg, 14, 17);
}
// SL (Sensitivity level, ACAS)
if (mm->msgtype == 0 || mm->msgtype == 16) {
mm->SL = getbits(msg, 9, 11);
}
// UM (Utility Message)
if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
mm->UM = getbits(msg, 14, 19);
}
// VS (Vertical Status)
if (mm->msgtype == 0 || mm->msgtype == 16) {
mm->VS = getbit(msg, 6);
if (mm->VS)
mm->airground = AG_GROUND;
else
mm->airground = AG_UNCERTAIN;
}
if (!mm->correctedbits && (mm->msgtype == 17 || (mm->msgtype == 11 && mm->IID == 0))) {
// DF17 ADS-B or DF11 acquisition squitter. Mark as known Mode-S source
icaoFilterAdd(mm->addr);
}
if (!mm->correctedbits && mm->msgtype == 18) {
// Mark as known ADS-B (NT) source
icaoFilterAdd(mm->addr | ICAO_FILTER_ADSB_NT);
}
// MLAT overrides all other sources
if (mm->remote && mm->timestampMsg == MAGIC_MLAT_TIMESTAMP)
mm->source = SOURCE_MLAT;
// all done
return 0;
}
static void decodeESIdentAndCategory(struct modesMessage *mm)
{
// Aircraft Identification and Category
unsigned char *me = mm->ME;
mm->mesub = getbits(me, 6, 8);
mm->callsign[0] = ais_charset[getbits(me, 9, 14)];
mm->callsign[1] = ais_charset[getbits(me, 15, 20)];
mm->callsign[2] = ais_charset[getbits(me, 21, 26)];
mm->callsign[3] = ais_charset[getbits(me, 27, 32)];
mm->callsign[4] = ais_charset[getbits(me, 33, 38)];
mm->callsign[5] = ais_charset[getbits(me, 39, 44)];
mm->callsign[6] = ais_charset[getbits(me, 45, 50)];
mm->callsign[7] = ais_charset[getbits(me, 51, 56)];
mm->callsign[8] = 0;
mm->callsign_valid = 1;
// actually valid?
for (unsigned i = 0; i < 8; ++i) {
if (!(mm->callsign[i] >= 'A' && mm->callsign[i] <= 'Z') &&
!(mm->callsign[i] >= '0' && mm->callsign[i] <= '9') &&
mm->callsign[i] != ' ') {
// Bad callsign, ignore it
mm->callsign_valid = 0;
break;
}
}
mm->category = ((0x0E - mm->metype) << 4) | mm->mesub;
mm->category_valid = 1;
}
// Handle setting a non-ICAO address
static void setIMF(struct modesMessage *mm)
{
mm->addr |= MODES_NON_ICAO_ADDRESS;
switch (mm->addrtype) {
case ADDR_ADSB_ICAO:
case ADDR_ADSB_ICAO_NT:
// Shouldn't happen, but let's try to handle it
mm->addrtype = ADDR_ADSB_OTHER;
break;
case ADDR_TISB_ICAO:
mm->addrtype = ADDR_TISB_TRACKFILE;
break;
case ADDR_ADSR_ICAO:
mm->addrtype = ADDR_ADSR_OTHER;
break;
default:
// Nothing.
break;
}
}
static void decodeESAirborneVelocity(struct modesMessage *mm, int check_imf)
{
// Airborne Velocity Message
unsigned char *me = mm->ME;
// 1-5: ME type
// 6-8: ME subtype
mm->mesub = getbits(me, 6, 8);
if (mm->mesub < 1 || mm->mesub > 4)
return;
// 9: IMF or Intent Change
if (check_imf && getbit(me, 9))
setIMF(mm);
// 10: reserved
// 11-13: NACv (NUCr in v0, maps directly to NACv in v2)
mm->accuracy.nac_v_valid = 1;
mm->accuracy.nac_v = getbits(me, 11, 13);
// 14-35: speed/velocity depending on subtype
switch (mm->mesub) {
case 1: case 2:
{
// 14: E/W direction
// 15-24: E/W speed
// 25: N/S direction
// 26-35: N/S speed
unsigned ew_raw = getbits(me, 15, 24);
unsigned ns_raw = getbits(me, 26, 35);
if (ew_raw && ns_raw) {
int ew_vel = (ew_raw - 1) * (getbit(me, 14) ? -1 : 1) * ((mm->mesub == 2) ? 4 : 1);
int ns_vel = (ns_raw - 1) * (getbit(me, 25) ? -1 : 1) * ((mm->mesub == 2) ? 4 : 1);
// Compute velocity and angle from the two speed components
mm->gs.v0 = mm->gs.v2 = mm->gs.selected = sqrtf((ns_vel * ns_vel) + (ew_vel * ew_vel) + 0.5);
mm->gs_valid = 1;
if (mm->gs.selected > 0) {
float ground_track = atan2(ew_vel, ns_vel) * 180.0 / M_PI;
// We don't want negative values but a 0-360 scale
if (ground_track < 0)
ground_track += 360;
mm->heading = ground_track;
mm->heading_type = HEADING_GROUND_TRACK;
mm->heading_valid = 1;
}
}
break;
}
case 3: case 4:
{
// 14: heading status
// 15-24: heading
if (getbit(me, 14)) {
mm->heading_valid = 1;
mm->heading = getbits(me, 15, 24) * 360.0 / 1024.0;
mm->heading_type = HEADING_MAGNETIC_OR_TRUE;
}
// 25: airspeed type
// 26-35: airspeed
unsigned airspeed = getbits(me, 26, 35);
if (airspeed) {
unsigned speed = (airspeed - 1) * (mm->mesub == 4 ? 4 : 1);
if (getbit(me, 25)) {
mm->tas_valid = 1;
mm->tas = speed;
} else {
mm->ias_valid = 1;
mm->ias = speed;
}
}
break;
}
}
// 36: vert rate source
// 37: vert rate sign
// 38-46: vert rate magnitude
unsigned vert_rate = getbits(me, 38, 46);
unsigned vert_rate_is_baro = getbit(me, 36);
if (vert_rate) {
int rate = (vert_rate - 1) * (getbit(me, 37) ? -64 : 64);
if (vert_rate_is_baro) {
mm->baro_rate = rate;
mm->baro_rate_valid = 1;
} else {
mm->geom_rate = rate;
mm->geom_rate_valid = 1;
}
}
// 47-48: reserved
// 49: baro/geom delta sign
// 50-56: baro/geom delta magnitude
unsigned raw_delta = getbits(me, 50, 56);
if (raw_delta) {
mm->geom_delta_valid = 1;
mm->geom_delta = (raw_delta - 1) * (getbit(me, 49) ? -25 : 25);
}
}
static void decodeESSurfacePosition(struct modesMessage *mm, int check_imf)
{
// Surface position and movement
unsigned char *me = mm->ME;
mm->airground = AG_GROUND; // definitely.
mm->cpr_valid = 1;
mm->cpr_type = CPR_SURFACE;
// 6-12: Movement
unsigned movement = getbits(me, 6, 12);
if (movement > 0 && movement < 125) {
mm->gs_valid = 1;
mm->gs.selected = mm->gs.v0 = decodeMovementFieldV0(movement); // assumed v0 until told otherwise
mm->gs.v2 = decodeMovementFieldV2(movement);
}
// 13: Heading/track status
// 14-20: Heading/track
if (getbit(me, 13)) {
mm->heading_valid = 1;
mm->heading = getbits(me, 14, 20) * 360.0 / 128.0;
mm->heading_type = HEADING_TRACK_OR_HEADING;
}
// 21: IMF or T flag
if (check_imf && getbit(me, 21))
setIMF(mm);
// 22: F flag (odd/even)
mm->cpr_odd = getbit(me, 22);
// 23-39: CPR encoded latitude
mm->cpr_lat = getbits(me, 23, 39);
// 40-56: CPR encoded longitude
mm->cpr_lon = getbits(me, 40, 56);