Debian Codegraph @

mode_s.c

raw symbols SCIP
1 // Part of dump1090, a Mode S message decoder for RTLSDR devices.
2 //
3 // mode_s.c: Mode S message decoding.
4 //
5 // Copyright (c) 2014-2016 Oliver Jowett <[email protected]>
6 //
7 // This file is free software: you may copy, redistribute and/or modify it
8 // under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, either version 2 of the License, or (at your
10 // option) any later version.
11 //
12 // This file is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 // This file incorporates work covered by the following copyright and
21 // permission notice:
22 //
23 // Copyright (C) 2012 by Salvatore Sanfilippo <[email protected]>
24 //
25 // All rights reserved.
26 //
27 // Redistribution and use in source and binary forms, with or without
28 // modification, are permitted provided that the following conditions are
29 // met:
30 //
31 // * Redistributions of source code must retain the above copyright
32 // notice, this list of conditions and the following disclaimer.
33 //
34 // * Redistributions in binary form must reproduce the above copyright
35 // notice, this list of conditions and the following disclaimer in the
36 // documentation and/or other materials provided with the distribution.
37 //
38 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
50
51 #include "dump1090.h"
52
53 /* for PRIX64 */
54 #include <inttypes.h>
55
56 #include <assert.h>
57
58 //
59 // ===================== Mode S detection and decoding ===================
60 //
61 //
62 //
63
64 /* A timestamp that indicates the data is synthetic, created from a
65 * multilateration result
66 */
67 #define MAGIC_MLAT_TIMESTAMP 0xFF004D4C4154ULL
68
69 //=========================================================================
70 //
71 // Given the Downlink Format (DF) of the message, return the message length in bits.
72 //
73 // All known DF's 16 or greater are long. All known DF's 15 or less are short.
74 // There are lots of unused codes in both category, so we can assume ICAO will stick to
75 // these rules, meaning that the most significant bit of the DF indicates the length.
76 //
77 int modesMessageLenByType(int type) {
78 return (type & 0x10) ? MODES_LONG_MSG_BITS : MODES_SHORT_MSG_BITS ;
79 }
80
81 //
82 //=========================================================================
83 //
84 // In the squawk (identity) field bits are interleaved as follows in
85 // (message bit 20 to bit 32):
86 //
87 // C1-A1-C2-A2-C4-A4-ZERO-B1-D1-B2-D2-B4-D4
88 //
89 // So every group of three bits A, B, C, D represent an integer from 0 to 7.
90 //
91 // The actual meaning is just 4 octal numbers, but we convert it into a hex
92 // number tha happens to represent the four octal numbers.
93 //
94 // For more info: http://en.wikipedia.org/wiki/Gillham_code
95 //
96 static int decodeID13Field(int ID13Field) {
97 int hexGillham = 0;
98
99 if (ID13Field & 0x1000) {hexGillham |= 0x0010;} // Bit 12 = C1
100 if (ID13Field & 0x0800) {hexGillham |= 0x1000;} // Bit 11 = A1
101 if (ID13Field & 0x0400) {hexGillham |= 0x0020;} // Bit 10 = C2
102 if (ID13Field & 0x0200) {hexGillham |= 0x2000;} // Bit 9 = A2
103 if (ID13Field & 0x0100) {hexGillham |= 0x0040;} // Bit 8 = C4
104 if (ID13Field & 0x0080) {hexGillham |= 0x4000;} // Bit 7 = A4
105 //if (ID13Field & 0x0040) {hexGillham |= 0x0800;} // Bit 6 = X or M
106 if (ID13Field & 0x0020) {hexGillham |= 0x0100;} // Bit 5 = B1
107 if (ID13Field & 0x0010) {hexGillham |= 0x0001;} // Bit 4 = D1 or Q
108 if (ID13Field & 0x0008) {hexGillham |= 0x0200;} // Bit 3 = B2
109 if (ID13Field & 0x0004) {hexGillham |= 0x0002;} // Bit 2 = D2
110 if (ID13Field & 0x0002) {hexGillham |= 0x0400;} // Bit 1 = B4
111 if (ID13Field & 0x0001) {hexGillham |= 0x0004;} // Bit 0 = D4
112
113 return (hexGillham);
114 }
115
116 //
117 //=========================================================================
118 //
119 // Decode the 13 bit AC altitude field (in DF 20 and others).
120 // Returns the altitude, and set 'unit' to either UNIT_METERS or UNIT_FEET.
121 //
122 static int decodeAC13Field(int AC13Field, altitude_unit_t *unit) {
123 int m_bit = AC13Field & 0x0040; // set = meters, clear = feet
124 int q_bit = AC13Field & 0x0010; // set = 25 ft encoding, clear = Gillham Mode C encoding
125
126 if (!m_bit) {
127 *unit = UNIT_FEET;
128 if (q_bit) {
129 // N is the 11 bit integer resulting from the removal of bit Q and M
130 int n = ((AC13Field & 0x1F80) >> 2) |
131 ((AC13Field & 0x0020) >> 1) |
132 (AC13Field & 0x000F);
133 // The final altitude is resulting number multiplied by 25, minus 1000.
134 return ((n * 25) - 1000);
135 } else {
136 // N is an 11 bit Gillham coded altitude
137 int n = ModeAToModeC(decodeID13Field(AC13Field));
138 if (n < -12) {
139 return INVALID_ALTITUDE;
140 }
141
142 return (100 * n);
143 }
144 } else {
145 *unit = UNIT_METERS;
146 // TODO: Implement altitude when meter unit is selected
147 return INVALID_ALTITUDE;
148 }
149 }
150
151 //
152 //=========================================================================
153 //
154 // Decode the 12 bit AC altitude field (in DF 17 and others).
155 //
156 static int decodeAC12Field(int AC12Field, altitude_unit_t *unit) {
157 int q_bit = AC12Field & 0x10; // Bit 48 = Q
158
159 *unit = UNIT_FEET;
160 if (q_bit) {
161 /// N is the 11 bit integer resulting from the removal of bit Q at bit 4
162 int n = ((AC12Field & 0x0FE0) >> 1) |
163 (AC12Field & 0x000F);
164 // The final altitude is the resulting number multiplied by 25, minus 1000.
165 return ((n * 25) - 1000);
166 } else {
167 // Make N a 13 bit Gillham coded altitude by inserting M=0 at bit 6
168 int n = ((AC12Field & 0x0FC0) << 1) |
169 (AC12Field & 0x003F);
170 n = ModeAToModeC(decodeID13Field(n));
171 if (n < -12) {
172 return INVALID_ALTITUDE;
173 }
174
175 return (100 * n);
176 }
177 }
178
179 //
180 //=========================================================================
181 //
182 // Decode the 7 bit ground movement field PWL exponential style scale
183 //
184 static unsigned decodeMovementField(unsigned movement) {
185 int gspeed;
186
187 // Note : movement codes 0,125,126,127 are all invalid, but they are
188 // trapped for before this function is called.
189
190 if (movement > 123) gspeed = 199; // > 175kt
191 else if (movement > 108) gspeed = ((movement - 108) * 5) + 100;
192 else if (movement > 93) gspeed = ((movement - 93) * 2) + 70;
193 else if (movement > 38) gspeed = ((movement - 38) ) + 15;
194 else if (movement > 12) gspeed = ((movement - 11) >> 1) + 2;
195 else if (movement > 8) gspeed = ((movement - 6) >> 2) + 1;
196 else gspeed = 0;
197
198 return (gspeed);
199 }
200
201 // Correct a decoded native-endian Address Announced field
202 // (from bits 8-31) if it is affected by the given error
203 // syndrome. Updates *addr and returns >0 if changed, 0 if
204 // it was unaffected.
205 static int correct_aa_field(uint32_t *addr, struct errorinfo *ei)
206 {
207 int i;
208 int addr_errors = 0;
209
210 if (!ei)
211 return 0;
212
213 for (i = 0; i < ei->errors; ++i) {
214 if (ei->bit[i] >= 8 && ei->bit[i] <= 31) {
215 *addr ^= 1 << (31 - ei->bit[i]);
216 ++addr_errors;
217 }
218 }
219
220 return addr_errors;
221 }
222
223 // The first bit (MSB of the first byte) is numbered 1, for consistency
224 // with how the specs number them.
225
226 // Extract one bit from a message.
227 static inline __attribute__((always_inline)) unsigned getbit(unsigned char *data, unsigned bitnum)
228 {
229 unsigned bi = bitnum - 1;
230 unsigned by = bi >> 3;
231 unsigned mask = 1 << (7 - (bi & 7));
232
233 return (data[by] & mask) != 0;
234 }
235
236 // Extract some bits (firstbit .. lastbit inclusive) from a message.
237 static inline __attribute__((always_inline)) unsigned getbits(unsigned char *data, unsigned firstbit, unsigned lastbit)
238 {
239 unsigned fbi = firstbit - 1;
240 unsigned lbi = lastbit - 1;
241 unsigned nbi = (lastbit - firstbit + 1);
242
243 unsigned fby = fbi >> 3;
244 unsigned lby = lbi >> 3;
245 unsigned nby = (lby - fby) + 1;
246
247 unsigned shift = 7 - (lbi & 7);
248 unsigned topmask = 0xFF >> (fbi & 7);
249
250 assert (fbi <= lbi);
251 assert (nbi <= 32);
252 assert (nby <= 5);
253
254 if (nby == 5) {
255 return
256 ((data[fby] & topmask) << (32 - shift)) |
257 (data[fby + 1] << (24 - shift)) |
258 (data[fby + 2] << (16 - shift)) |
259 (data[fby + 3] << (8 - shift)) |
260 (data[fby + 4] >> shift);
261 } else if (nby == 4) {
262 return
263 ((data[fby] & topmask) << (24 - shift)) |
264 (data[fby + 1] << (16 - shift)) |
265 (data[fby + 2] << (8 - shift)) |
266 (data[fby + 3] >> shift);
267 } else if (nby == 3) {
268 return
269 ((data[fby] & topmask) << (16 - shift)) |
270 (data[fby + 1] << (8 - shift)) |
271 (data[fby + 2] >> shift);
272 } else if (nby == 2) {
273 return
274 ((data[fby] & topmask) << (8 - shift)) |
275 (data[fby + 1] >> shift);
276 } else if (nby == 1) {
277 return
278 (data[fby] & topmask) >> shift;
279 } else {
280 return 0;
281 }
282 }
283
284 // Score how plausible this ModeS message looks.
285 // The more positive, the more reliable the message is
286
287 // 1000: DF 0/4/5/16/24 with a CRC-derived address matching a known aircraft
288
289 // 1800: DF17/18 with good CRC and an address matching a known aircraft
290 // 1400: DF17/18 with good CRC and an address not matching a known aircraft
291 // 900: DF17/18 with 1-bit error and an address matching a known aircraft
292 // 700: DF17/18 with 1-bit error and an address not matching a known aircraft
293 // 450: DF17/18 with 2-bit error and an address matching a known aircraft
294 // 350: DF17/18 with 2-bit error and an address not matching a known aircraft
295
296 // 1600: DF11 with IID==0, good CRC and an address matching a known aircraft
297 // 800: DF11 with IID==0, 1-bit error and an address matching a known aircraft
298 // 750: DF11 with IID==0, good CRC and an address not matching a known aircraft
299 // 375: DF11 with IID==0, 1-bit error and an address not matching a known aircraft
300
301 // 1000: DF11 with IID!=0, good CRC and an address matching a known aircraft
302 // 500: DF11 with IID!=0, 1-bit error and an address matching a known aircraft
303
304 // 1000: DF20/21 with a CRC-derived address matching a known aircraft
305 // 500: DF20/21 with a CRC-derived address matching a known aircraft (bottom 16 bits only - overlay control in use)
306
307 // -1: message might be valid, but we couldn't validate the CRC against a known ICAO
308 // -2: bad message or unrepairable CRC error
309
310 static unsigned char all_zeros[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
311 int scoreModesMessage(unsigned char *msg, int validbits)
312 {
313 int msgtype, msgbits, crc, iid;
314 uint32_t addr;
315 struct errorinfo *ei;
316
317 if (validbits < 56)
318 return -2;
319
320 msgtype = getbits(msg, 1, 5); // Downlink Format
321 msgbits = modesMessageLenByType(msgtype);
322
323 if (validbits < msgbits)
324 return -2;
325
326 if (!memcmp(all_zeros, msg, msgbits/8))
327 return -2;
328
329 crc = modesChecksum(msg, msgbits);
330
331 switch (msgtype) {
332 case 0: // short air-air surveillance
333 case 4: // surveillance, altitude reply
334 case 5: // surveillance, altitude reply
335 case 16: // long air-air surveillance
336 case 24: // Comm-D (ELM)
337 case 25: // Comm-D (ELM)
338 case 26: // Comm-D (ELM)
339 case 27: // Comm-D (ELM)
340 case 28: // Comm-D (ELM)
341 case 29: // Comm-D (ELM)
342 case 30: // Comm-D (ELM)
343 case 31: // Comm-D (ELM)
344 return icaoFilterTest(crc) ? 1000 : -1;
345
346 case 11: // All-call reply
347 iid = crc & 0x7f;
348 crc = crc & 0xffff80;
349 addr = getbits(msg, 9, 32);
350
351 ei = modesChecksumDiagnose(crc, msgbits);
352 if (!ei)
353 return -2; // can't correct errors
354
355 // see crc.c comments: we do not attempt to fix
356 // more than single-bit errors, as two-bit
357 // errors are ambiguous in DF11.
358 if (ei->errors > 1)
359 return -2; // can't correct errors
360
361 // fix any errors in the address field
362 correct_aa_field(&addr, ei);
363
364 // validate address
365 if (iid == 0) {
366 if (icaoFilterTest(addr))
367 return 1600 / (ei->errors + 1);
368 else
369 return 750 / (ei->errors + 1);
370 } else {
371 if (icaoFilterTest(addr))
372 return 1000 / (ei->errors + 1);
373 else
374 return -1;
375 }
376
377 case 17: // Extended squitter
378 case 18: // Extended squitter/non-transponder
379 ei = modesChecksumDiagnose(crc, msgbits);
380 if (!ei)
381 return -2; // can't correct errors
382
383 // fix any errors in the address field
384 addr = getbits(msg, 9, 32);
385 correct_aa_field(&addr, ei);
386
387 if (icaoFilterTest(addr))
388 return 1800 / (ei->errors+1);
389 else
390 return 1400 / (ei->errors+1);
391
392 case 20: // Comm-B, altitude reply
393 case 21: // Comm-B, identity reply
394 if (icaoFilterTest(crc))
395 return 1000; // Address/Parity
396
397 #if 0
398 // This doesn't seem useful, as we mistake a lot of CRC errors
399 // for overlay control
400 if (icaoFilterTestFuzzy(crc))
401 return 500; // Data/Parity
402 #endif
403
404 return -2;
405
406 default:
407 // unknown message type
408 return -2;
409 }
410 }
411
412 //
413 //=========================================================================
414 //
415 // Decode a raw Mode S message demodulated as a stream of bytes by detectModeS(),
416 // and split it into fields populating a modesMessage structure.
417 //
418
419 static void decodeExtendedSquitter(struct modesMessage *mm);
420 static void decodeCommB(struct modesMessage *mm);
421 static char *ais_charset = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ !\"#$%&'()*+,-./0123456789:;<=>?";
422
423 // return 0 if all OK
424 // -1: message might be valid, but we couldn't validate the CRC against a known ICAO
425 // -2: bad message or unrepairable CRC error
426
427 int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
428 {
429 // Work on our local copy.
430 memcpy(mm->msg, msg, MODES_LONG_MSG_BYTES);
431 if (Modes.net_verbatim) {
432 // Preserve the original uncorrected copy for later forwarding
433 memcpy(mm->verbatim, msg, MODES_LONG_MSG_BYTES);
434 }
435 msg = mm->msg;
436
437 // don't accept all-zeros messages
438 if (!memcmp(all_zeros, msg, 7))
439 return -2;
440
441 // Get the message type ASAP as other operations depend on this
442 mm->msgtype = getbits(msg, 1, 5); // Downlink Format
443 mm->msgbits = modesMessageLenByType(mm->msgtype);
444 mm->crc = modesChecksum(msg, mm->msgbits);
445 mm->correctedbits = 0;
446 mm->addr = 0;
447
448 // Do checksum work and set fields that depend on the CRC
449 switch (mm->msgtype) {
450 case 0: // short air-air surveillance
451 case 4: // surveillance, altitude reply
452 case 5: // surveillance, altitude reply
453 case 16: // long air-air surveillance
454 case 24: // Comm-D (ELM)
455 case 25: // Comm-D (ELM)
456 case 26: // Comm-D (ELM)
457 case 27: // Comm-D (ELM)
458 case 28: // Comm-D (ELM)
459 case 29: // Comm-D (ELM)
460 case 30: // Comm-D (ELM)
461 case 31: // Comm-D (ELM)
462 // These message types use Address/Parity, i.e. our CRC syndrome is the sender's ICAO address.
463 // We can't tell if the CRC is correct or not as we don't know the correct address.
464 // Accept the message if it appears to be from a previously-seen aircraft
465 if (!icaoFilterTest(mm->crc)) {
466 return -1;
467 }
468 mm->source = SOURCE_MODE_S;
469 mm->addr = mm->crc;
470 break;
471
472 case 11: // All-call reply
473 // This message type uses Parity/Interrogator, i.e. our CRC syndrome is CL + IC from the uplink message
474 // which we can't see. So we don't know if the CRC is correct or not.
475 //
476 // however! CL + IC only occupy the lower 7 bits of the CRC. So if we ignore those bits when testing
477 // the CRC we can still try to detect/correct errors.
478
479 mm->IID = mm->crc & 0x7f;
480 if (mm->crc & 0xffff80) {
481 int addr;
482 struct errorinfo *ei = modesChecksumDiagnose(mm->crc & 0xffff80, mm->msgbits);
483 if (!ei) {
484 return -2; // couldn't fix it
485 }
486
487 // see crc.c comments: we do not attempt to fix
488 // more than single-bit errors, as two-bit
489 // errors are ambiguous in DF11.
490 if (ei->errors > 1)
491 return -2; // can't correct errors
492
493 mm->correctedbits = ei->errors;
494 modesChecksumFix(msg, ei);
495
496 // check whether the corrected message looks sensible
497 // we are conservative here: only accept corrected messages that
498 // match an existing aircraft.
499 addr = getbits(msg, 9, 32);
500 if (!icaoFilterTest(addr)) {
501 return -1;
502 }
503 }
504 mm->source = SOURCE_MODE_S_CHECKED;
505 break;
506
507 case 17: // Extended squitter
508 case 18: { // Extended squitter/non-transponder
509 struct errorinfo *ei;
510 int addr1, addr2;
511
512 // These message types use Parity/Interrogator, but are specified to set II=0
513
514 if (mm->crc != 0) {
515 ei = modesChecksumDiagnose(mm->crc, mm->msgbits);
516 if (!ei) {
517 return -2; // couldn't fix it
518 }
519
520 addr1 = getbits(msg, 9, 32);
521 mm->correctedbits = ei->errors;
522 modesChecksumFix(msg, ei);
523 addr2 = getbits(msg, 9, 32);
524
525 // we are conservative here: only accept corrected messages that
526 // match an existing aircraft.
527 if (addr1 != addr2 && !icaoFilterTest(addr2)) {
528 return -1;
529 }
530 }
531
532 mm->source = SOURCE_ADSB; // TIS-B decoding will override this if needed
533 break;
534 }
535
536 case 20: // Comm-B, altitude reply
537 case 21: // Comm-B, identity reply
538 // These message types either use Address/Parity (see DF0 etc)
539 // or Data Parity where the requested BDS is also xored into the top byte.
540 // So not only do we not know whether the CRC is right, we also don't know if
541 // the ICAO is right! Ow.
542
543 // Try an exact match
544 if (icaoFilterTest(mm->crc)) {
545 // OK.
546 mm->source = SOURCE_MODE_S;
547 mm->addr = mm->crc;
548 break;
549 }
550
551 // BDS / overlay control just doesn't work out.
552
553 return -1; // no good
554
555 default:
556 // All other message types, we don't know how to handle their CRCs, give up
557 return -2;
558 }
559
560 // decode the bulk of the message
561
562 // AA (Address announced)
563 if (mm->msgtype == 11 || mm->msgtype == 17 || mm->msgtype == 18) {
564 mm->AA = mm->addr = getbits(msg, 9, 32);
565 }
566
567 // AC (Altitude Code)
568 if (mm->msgtype == 0 || mm->msgtype == 4 || mm->msgtype == 16 || mm->msgtype == 20) {
569 mm->AC = getbits(msg, 20, 32);
570 if (mm->AC) { // Only attempt to decode if a valid (non zero) altitude is present
571 mm->altitude = decodeAC13Field(mm->AC, &mm->altitude_unit);
572 if (mm->altitude != INVALID_ALTITUDE)
573 mm->altitude_valid = 1;
574 mm->altitude_source = ALTITUDE_BARO;
575 }
576 }
577
578 // AF (DF19 Application Field) not decoded
579
580 // CA (Capability)
581 if (mm->msgtype == 11 || mm->msgtype == 17) {
582 mm->CA = getbits(msg, 6, 8);
583
584 switch (mm->CA) {
585 case 0:
586 mm->airground = AG_UNCERTAIN;
587 break;
588 case 4:
589 mm->airground = AG_GROUND;
590 break;
591 case 5:
592 mm->airground = AG_AIRBORNE;
593 break;
594 case 6:
595 mm->airground = AG_UNCERTAIN;
596 break;
597 case 7:
598 mm->airground = AG_UNCERTAIN;
599 break;
600 }
601 }
602
603 // CC (Cross-link capability)
604 if (mm->msgtype == 0) {
605 mm->CC = getbit(msg, 7);
606 }
607
608 // CF (Control field)
609 if (mm->msgtype == 18) {
610 mm->CF = getbits(msg, 5, 8);
611 }
612
613 // DR (Downlink Request)
614 if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
615 mm->DR = getbits(msg, 9, 13);
616 }
617
618 // FS (Flight Status)
619 if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
620 mm->FS = getbits(msg, 6, 8);
621 mm->alert_valid = 1;
622 mm->spi_valid = 1;
623
624 switch (mm->FS) {
625 case 0:
626 mm->airground = AG_UNCERTAIN;
627 break;
628 case 1:
629 mm->airground = AG_GROUND;
630 break;
631 case 2:
632 mm->airground = AG_UNCERTAIN;
633 mm->alert = 1;
634 break;
635 case 3:
636 mm->airground = AG_GROUND;
637 mm->alert = 1;
638 break;
639 case 4:
640 mm->airground = AG_UNCERTAIN;
641 mm->alert = 1;
642 mm->spi = 1;
643 break;
644 case 5:
645 mm->airground = AG_UNCERTAIN;
646 mm->spi = 1;
647 break;
648 default:
649 mm->spi_valid = 0;
650 mm->alert_valid = 0;
651 break;
652 }
653 }
654
655 // ID (Identity)
656 if (mm->msgtype == 5 || mm->msgtype == 21) {
657 // Gillham encoded Squawk
658 mm->ID = getbits(msg, 20, 32);
659 if (mm->ID) {
660 mm->squawk = decodeID13Field(mm->ID);
661 mm->squawk_valid = 1;
662 }
663 }
664
665 // KE (Control, ELM)
666 if (mm->msgtype >= 24 && mm->msgtype <= 31) {
667 mm->KE = getbit(msg, 4);
668 }
669
670 // MB (messsage, Comm-B)
671 if (mm->msgtype == 20 || mm->msgtype == 21) {
672 memcpy(mm->MB, &msg[4], 7);
673 decodeCommB(mm);
674 }
675
676 // MD (message, Comm-D)
677 if (mm->msgtype >= 24 && mm->msgtype <= 31) {
678 memcpy(mm->MD, &msg[1], 10);
679 }
680
681 // ME (message, extended squitter)
682 if (mm->msgtype == 17 || mm->msgtype == 18) {
683 memcpy(mm->ME, &msg[4], 7);
684 decodeExtendedSquitter(mm);
685 }
686
687 // MV (message, ACAS)
688 if (mm->msgtype == 16) {
689 memcpy(mm->MV, &msg[4], 7);
690 }
691
692 // ND (number of D-segment, Comm-D)
693 if (mm->msgtype >= 24 && mm->msgtype <= 31) {
694 mm->ND = getbits(msg, 5, 8);
695 }
696
697 // RI (Reply information, ACAS)
698 if (mm->msgtype == 0 || mm->msgtype == 16) {
699 mm->RI = getbits(msg, 14, 17);
700 }
701
702 // SL (Sensitivity level, ACAS)
703 if (mm->msgtype == 0 || mm->msgtype == 16) {
704 mm->SL = getbits(msg, 9, 11);
705 }
706
707 // UM (Utility Message)
708 if (mm->msgtype == 4 || mm->msgtype == 5 || mm->msgtype == 20 || mm->msgtype == 21) {
709 mm->UM = getbits(msg, 14, 19);
710 }
711
712 // VS (Vertical Status)
713 if (mm->msgtype == 0 || mm->msgtype == 16) {
714 mm->VS = getbit(msg, 6);
715 if (mm->VS)
716 mm->airground = AG_GROUND;
717 else
718 mm->airground = AG_UNCERTAIN;
719 }
720
721 if (!mm->correctedbits && (mm->msgtype == 17 || mm->msgtype == 18 || (mm->msgtype == 11 && mm->IID == 0))) {
722 // No CRC errors seen, and either it was an DF17/18 extended squitter
723 // or a DF11 acquisition squitter with II = 0. We probably have the right address.
724
725 // We wait until here to do this as we may have needed to decode an ES to note
726 // the type of address in DF18 messages.
727
728 // NB this is the only place that adds addresses!
729 icaoFilterAdd(mm->addr);
730 }
731
732 // MLAT overrides all other sources
733 if (mm->remote && mm->timestampMsg == MAGIC_MLAT_TIMESTAMP)
734 mm->source = SOURCE_MLAT;
735
736 // all done
737 return 0;
738 }
739
740 // Decode BDS2,0 carried in Comm-B or ES
741 static void decodeBDS20(struct modesMessage *mm)
742 {
743 unsigned char *msg = mm->msg;
744
745 mm->callsign[0] = ais_charset[getbits(msg, 41, 46)];
746 mm->callsign[1] = ais_charset[getbits(msg, 47, 52)];
747 mm->callsign[2] = ais_charset[getbits(msg, 53, 58)];
748 mm->callsign[3] = ais_charset[getbits(msg, 59, 64)];
749 mm->callsign[4] = ais_charset[getbits(msg, 65, 70)];
750 mm->callsign[5] = ais_charset[getbits(msg, 71, 76)];
751 mm->callsign[6] = ais_charset[getbits(msg, 77, 82)];
752 mm->callsign[7] = ais_charset[getbits(msg, 83, 88)];
753 mm->callsign[8] = 0;
754
755 // Catch possible bad decodings since BDS2,0 is not
756 // 100% reliable: accept only alphanumeric data
757 mm->callsign_valid = 1;
758 for (int i = 0; i < 8; ++i) {
759 if (! ((mm->callsign[i] >= 'A' && mm->callsign[i] <= 'Z') ||
760 (mm->callsign[i] >= '0' && mm->callsign[i] <= '9') ||
761 mm->callsign[i] == ' ') ) {
762 mm->callsign_valid = 0;
763 break;
764 }
765 }
766 }
767
768 static void decodeESIdentAndCategory(struct modesMessage *mm)
769 {
770 // Aircraft Identification and Category
771 unsigned char *me = mm->ME;
772
773 mm->mesub = getbits(me, 6, 8);
774
775 mm->callsign[0] = ais_charset[getbits(me, 9, 14)];
776 mm->callsign[1] = ais_charset[getbits(me, 15, 20)];
777 mm->callsign[2] = ais_charset[getbits(me, 21, 26)];
778 mm->callsign[3] = ais_charset[getbits(me, 27, 32)];
779 mm->callsign[4] = ais_charset[getbits(me, 33, 38)];
780 mm->callsign[5] = ais_charset[getbits(me, 39, 44)];
781 mm->callsign[6] = ais_charset[getbits(me, 45, 50)];
782 mm->callsign[7] = ais_charset[getbits(me, 51, 56)];
783
784 // A common failure mode seems to be to intermittently send
785 // all zeros. Catch that here.
786 mm->callsign_valid = (strcmp(mm->callsign, "@@@@@@@@") != 0);
787
788 mm->category = ((0x0E - mm->metype) << 4) | mm->mesub;
789 mm->category_valid = 1;
790 }
791
792 // Handle setting a non-ICAO address
793 static void setIMF(struct modesMessage *mm)
794 {
795 mm->addr |= MODES_NON_ICAO_ADDRESS;
796 switch (mm->addrtype) {
797 case ADDR_ADSB_ICAO:
798 case ADDR_ADSB_ICAO_NT:
799 // Shouldn't happen, but let's try to handle it
800 mm->addrtype = ADDR_ADSB_OTHER;
801 break;
802
803 case ADDR_TISB_ICAO:
804 mm->addrtype = ADDR_TISB_TRACKFILE;
805 break;
806
807 case ADDR_ADSR_ICAO:
808 mm->addrtype = ADDR_ADSR_OTHER;
809 break;
810
811 default:
812 // Nothing.
813 break;
814 }
815 }
816
817 static void decodeESAirborneVelocity(struct modesMessage *mm, int check_imf)
818 {
819 // Airborne Velocity Message
820 unsigned char *me = mm->ME;
821
822 mm->mesub = getbits(me, 6, 8);
823
824 if (check_imf && getbit(me, 9))
825 setIMF(mm);
826
827 if (mm->mesub < 1 || mm->mesub > 4)
828 return;
829
830 unsigned vert_rate = getbits(me, 38, 46);
831 if (vert_rate) {
832 mm->vert_rate = (vert_rate - 1) * (getbit(me, 37) ? -64 : 64);
833 mm->vert_rate_valid = 1;
834 }
835
836 mm->vert_rate_source = (getbit(me, 36) ? ALTITUDE_GNSS : ALTITUDE_BARO);
837
838 switch (mm->mesub) {
839 case 1: case 2:
840 {
841 unsigned ew_raw = getbits(me, 15, 24);
842 unsigned ns_raw = getbits(me, 26, 35);
843
844 if (ew_raw && ns_raw) {
845 int ew_vel = (ew_raw - 1) * (getbit(me, 14) ? -1 : 1) * ((mm->mesub == 2) ? 4 : 1);
846 int ns_vel = (ns_raw - 1) * (getbit(me, 25) ? -1 : 1) * ((mm->mesub == 2) ? 4 : 1);
847
848 // Compute velocity and angle from the two speed components
849 mm->speed = (unsigned) sqrt((ns_vel * ns_vel) + (ew_vel * ew_vel) + 0.5);
850 mm->speed_valid = 1;
851
852 if (mm->speed) {
853 int heading = (int) (atan2(ew_vel, ns_vel) * 180.0 / M_PI + 0.5);
854 // We don't want negative values but a 0-360 scale
855 if (heading < 0)
856 heading += 360;
857 mm->heading = (unsigned) heading;
858 mm->heading_source = HEADING_TRUE;
859 mm->heading_valid = 1;
860 }
861
862 mm->speed_source = SPEED_GROUNDSPEED;
863 }
864 break;
865 }
866
867 case 3: case 4:
868 {
869 unsigned airspeed = getbits(me, 26, 35);
870 if (airspeed) {
871 mm->speed = (airspeed - 1) * (mm->mesub == 4 ? 4 : 1);
872 mm->speed_source = getbit(me, 25) ? SPEED_TAS : SPEED_IAS;
873 mm->speed_valid = 1;
874 }
875
876 if (getbit(me, 14)) {
877 mm->heading = getbits(me, 15, 24);
878 mm->heading_source = HEADING_MAGNETIC;
879 mm->heading_valid = 1;
880 }
881 break;
882 }
883 }
884
885 unsigned raw_delta = getbits(me, 50, 56);
886 if (raw_delta) {
887 mm->gnss_delta_valid = 1;
888 mm->gnss_delta = (raw_delta - 1) * (getbit(me, 49) ? -25 : 25);
889 }
890 }
891
892 static void decodeESSurfacePosition(struct modesMessage *mm, int check_imf)
893 {
894 // Surface position and movement
895 unsigned char *me = mm->ME;
896
897 if (check_imf && getbit(me, 21))
898 setIMF(mm);
899
900 mm->airground = AG_GROUND; // definitely.
901 mm->cpr_lat = getbits(me, 23, 39);
902 mm->cpr_lon = getbits(me, 40, 56);
903 mm->cpr_odd = getbit(me, 22);
904 mm->cpr_nucp = (14 - mm->metype);
905 mm->cpr_valid = 1;
906 mm->cpr_type = CPR_SURFACE;
907
908 unsigned movement = getbits(me, 6, 12);
909 if (movement > 0 && movement < 125) {
910 mm->speed_valid = 1;
911 mm->speed = decodeMovementField(movement);
912 mm->speed_source = SPEED_GROUNDSPEED;
913 }
914
915 if (getbit(me, 13)) {
916 mm->heading_valid = 1;
917 mm->heading_source = HEADING_TRUE;
918 mm->heading = getbits(me, 14, 20) * 360 / 128;
919 }
920 }
921
922 static void decodeESAirbornePosition(struct modesMessage *mm, int check_imf)
923 {
924 // Airborne position and altitude
925 unsigned char *me = mm->ME;
926
927 if (check_imf && getbit(me, 8))
928 setIMF(mm);
929
930 unsigned AC12Field = getbits(me, 9, 20);
931
932 if (mm->metype == 0) {
933 mm->cpr_nucp = 0;
934 } else {
935 // Catch some common failure modes and don't mark them as valid
936 // (so they won't be used for positioning)
937
938 mm->cpr_lat = getbits(me, 23, 39);
939 mm->cpr_lon = getbits(me, 40, 56);
940
941 if (AC12Field == 0 && mm->cpr_lon == 0 && (mm->cpr_lat & 0x0fff) == 0 && mm->metype == 15) {
942 // Seen from at least:
943 // 400F3F (Eurocopter ECC155 B1) - Bristow Helicopters
944 // 4008F3 (BAE ATP) - Atlantic Airlines
945 // 400648 (BAE ATP) - Atlantic Airlines
946 // altitude == 0, longitude == 0, type == 15 and zeros in latitude LSB.
947 // Can alternate with valid reports having type == 14
948 Modes.stats_current.cpr_filtered++;
949 } else {
950 // Otherwise, assume it's valid.
951 mm->cpr_valid = 1;
952 mm->cpr_type = CPR_AIRBORNE;
953 mm->cpr_odd = getbit(me, 22);
954
955 if (mm->metype == 18 || mm->metype == 22)
956 mm->cpr_nucp = 0;
957 else if (mm->metype < 18)
958 mm->cpr_nucp = (18 - mm->metype);
959 else
960 mm->cpr_nucp = (29 - mm->metype);
961 }
962 }
963
964 if (AC12Field) {// Only attempt to decode if a valid (non zero) altitude is present
965 mm->altitude = decodeAC12Field(AC12Field, &mm->altitude_unit);
966 if (mm->altitude != INVALID_ALTITUDE) {
967 mm->altitude_valid = 1;
968 }
969
970 mm->altitude_source = (mm->metype == 20 || mm->metype == 21 || mm->metype == 22) ? ALTITUDE_GNSS : ALTITUDE_BARO;
971 }
972 }
973
974 static void decodeESTestMessage(struct modesMessage *mm)
975 {
976 unsigned char *me = mm->ME;
977
978 mm->mesub = getbits(me, 6, 8);
979
980 if (mm->mesub == 7) { // (see 1090-WP-15-20)
981 int ID13Field = getbits(me, 9, 21);
982 if (ID13Field) {
983 mm->squawk_valid = 1;
984 mm->squawk = decodeID13Field(ID13Field);
985 }
986 }
987 }
988
989 static void decodeESAircraftStatus(struct modesMessage *mm, int check_imf)
990 {
991 // Extended Squitter Aircraft Status
992 unsigned char *me = mm->ME;
993
994 mm->mesub = getbits(me, 6, 8);
995
996 if (mm->mesub == 1) { // Emergency status squawk field
997 int ID13Field = getbits(me, 12, 24);
998 if (ID13Field) {
999 mm->squawk_valid = 1;
1000 mm->squawk = decodeID13Field(ID13Field);
1001 }
1002
1003 if (check_imf && getbit(me, 56))
1004 setIMF(mm);
1005 }
1006 }
1007
1008 static void decodeESTargetStatus(struct modesMessage *mm, int check_imf)
1009 {
1010 unsigned char *me = mm->ME;
1011
1012 mm->mesub = getbits(me, 6, 7); // an unusual message: only 2 bits of subtype
1013
1014 if (check_imf && getbit(me, 51))
1015 setIMF(mm);
1016
1017 if (mm->mesub == 0) { // Target state and status, V1
1018 // TODO: need RTCA/DO-260A
1019 } else if (mm->mesub == 1) { // Target state and status, V2
1020 mm->tss.valid = 1;
1021 mm->tss.sil_type = getbit(me, 8) ? SIL_PER_SAMPLE : SIL_PER_HOUR;
1022 mm->tss.altitude_type = getbit(me, 9) ? TSS_ALTITUDE_FMS : TSS_ALTITUDE_MCP;
1023
1024 unsigned alt_bits = getbits(me, 10, 20);
1025 if (alt_bits == 0) {
1026 mm->tss.altitude_valid = 0;
1027 } else {
1028 mm->tss.altitude_valid = 1;
1029 mm->tss.altitude = (alt_bits - 1) * 32;
1030 }
1031
1032 unsigned baro_bits = getbits(me, 21, 29);
1033 if (baro_bits == 0) {
1034 mm->tss.baro_valid = 0;
1035 } else {
1036 mm->tss.baro_valid = 1;
1037 mm->tss.baro = 800.0 + (baro_bits - 1) * 0.8;
1038 }
1039
1040 mm->tss.heading_valid = getbit(me, 30);
1041 if (mm->tss.heading_valid) {
1042 // two's complement -180..+180, which is conveniently
1043 // also the same as unsigned 0..360
1044 mm->tss.heading = getbits(me, 31, 39) * 180 / 256;
1045 }
1046
1047 mm->tss.nac_p = getbits(me, 40, 43);
1048 mm->tss.nic_baro = getbit(me, 44);
1049 mm->tss.sil = getbits(me, 45, 46);
1050 mm->tss.mode_valid = getbit(me, 47);
1051 if (mm->tss.mode_valid) {
1052 mm->tss.mode_autopilot = getbit(me, 48);
1053 mm->tss.mode_vnav = getbit(me, 49);
1054 mm->tss.mode_alt_hold = getbit(me, 50);
1055 mm->tss.mode_approach = getbit(me, 52);
1056 }
1057
1058 mm->tss.acas_operational = getbit(me, 53);
1059 }
1060 }
1061
1062 static void decodeESOperationalStatus(struct modesMessage *mm, int check_imf)
1063 {
1064 unsigned char *me = mm->ME;
1065
1066 mm->mesub = getbits(me, 6, 8);
1067
1068 // Aircraft Operational Status
1069 if (check_imf && getbit(me, 56))
1070 setIMF(mm);
1071
1072 if (mm->mesub == 0 || mm->mesub == 1) {
1073 mm->opstatus.valid = 1;
1074 mm->opstatus.version = getbits(me, 41, 43);
1075
1076 switch (mm->opstatus.version) {
1077 case 0:
1078 break;
1079
1080 case 1:
1081 if (getbits(me, 25, 26) == 0) {
1082 mm->opstatus.om_acas_ra = getbit(me, 27);
1083 mm->opstatus.om_ident = getbit(me, 28);
1084 mm->opstatus.om_atc = getbit(me, 29);
1085 }
1086
1087 if (mm->mesub == 0 && getbits(me, 9, 10) == 0 && getbits(me, 13, 14) == 0) {
1088 // airborne
1089 mm->opstatus.cc_acas = !getbit(me, 11);
1090 mm->opstatus.cc_cdti = getbit(me, 12);
1091 mm->opstatus.cc_arv = getbit(me, 15);
1092 mm->opstatus.cc_ts = getbit(me, 16);
1093 mm->opstatus.cc_tc = getbits(me, 17, 18);
1094 } else if (mm->mesub == 1 && getbits(me, 9, 10) == 0 && getbits(me, 13, 14) == 0) {
1095 // surface
1096 mm->opstatus.cc_poa = getbit(me, 11);
1097 mm->opstatus.cc_cdti = getbit(me, 12);
1098 mm->opstatus.cc_b2_low = getbit(me, 15);
1099 mm->opstatus.cc_lw_valid = 1;
1100 mm->opstatus.cc_lw = getbits(me, 21, 24);
1101 }
1102
1103 mm->opstatus.nic_supp_a = getbit(me, 44);
1104 mm->opstatus.nac_p = getbits(me, 45, 48);
1105 mm->opstatus.sil = getbits(me, 51, 52);
1106 if (mm->mesub == 0) {
1107 mm->opstatus.nic_baro = getbit(me, 53);
1108 } else {
1109 mm->opstatus.track_angle = getbit(me, 53) ? ANGLE_TRACK : ANGLE_HEADING;
1110 }
1111 mm->opstatus.hrd = getbit(me, 54) ? HEADING_MAGNETIC : HEADING_TRUE;
1112 break;
1113
1114 case 2:
1115 default:
1116 if (getbits(me, 25, 26) == 0) {
1117 mm->opstatus.om_acas_ra = getbit(me, 27);
1118 mm->opstatus.om_ident = getbit(me, 28);
1119 mm->opstatus.om_atc = getbit(me, 29);
1120 mm->opstatus.om_saf = getbit(me, 30);
1121 mm->opstatus.om_sda = getbits(me, 31, 32);
1122 }
1123
1124 if (mm->mesub == 0 && getbits(me, 9, 10) == 0 && getbits(me, 13, 14) == 0) {
1125 // airborne
1126 mm->opstatus.cc_acas = getbit(me, 11);
1127 mm->opstatus.cc_1090_in = getbit(me, 12);
1128 mm->opstatus.cc_arv = getbit(me, 15);
1129 mm->opstatus.cc_ts = getbit(me, 16);
1130 mm->opstatus.cc_tc = getbits(me, 17, 18);
1131 mm->opstatus.cc_uat_in = getbit(me, 19);
1132 } else if (mm->mesub == 1 && getbits(me, 9, 10) == 0 && getbits(me, 13, 14) == 0) {
1133 // surface
1134 mm->opstatus.cc_poa = getbit(me, 11);
1135 mm->opstatus.cc_1090_in = getbit(me, 12);
1136 mm->opstatus.cc_b2_low = getbit(me, 15);
1137 mm->opstatus.cc_uat_in = getbit(me, 16);
1138 mm->opstatus.cc_nac_v = getbits(me, 17, 19);
1139 mm->opstatus.cc_nic_supp_c = getbit(me, 20);
1140 mm->opstatus.cc_lw_valid = 1;
1141 mm->opstatus.cc_lw = getbits(me, 21, 24);
1142 mm->opstatus.cc_antenna_offset = getbits(me, 33, 40);
1143 }
1144
1145 mm->opstatus.nic_supp_a = getbit(me, 44);
1146 mm->opstatus.nac_p = getbits(me, 45, 48);
1147 mm->opstatus.sil = getbits(me, 51, 52);
1148 if (mm->mesub == 0) {
1149 mm->opstatus.gva = getbits(me, 49, 50);
1150 mm->opstatus.nic_baro = getbit(me, 53);
1151 } else {
1152 mm->opstatus.track_angle = getbit(me, 53) ? ANGLE_TRACK : ANGLE_HEADING;
1153 }
1154 mm->opstatus.hrd = getbit(me, 54) ? HEADING_MAGNETIC : HEADING_TRUE;
1155 mm->opstatus.sil_type = getbit(me, 55) ? SIL_PER_SAMPLE : SIL_PER_HOUR;
1156 break;
1157 }
1158 }
1159 }
1160
1161 static void decodeExtendedSquitter(struct modesMessage *mm)
1162 {
1163 unsigned char *me = mm->ME;
1164 unsigned metype = mm->metype = getbits(me, 1, 5);
1165 unsigned check_imf = 0;
1166
1167 // Check CF on DF18 to work out the format of the ES and whether we need to look for an IMF bit
1168 if (mm->msgtype == 18) {
1169 switch (mm->CF) {
1170 case 0: // ADS-B Message from a non-transponder device, AA field holds 24-bit ICAO aircraft address
1171 mm->addrtype = ADDR_ADSB_ICAO_NT;
1172 break;
1173
1174 case 1: // Reserved for ADS-B Message in which the AA field holds anonymous address or ground vehicle address or fixed obstruction address
1175 mm->addrtype = ADDR_ADSB_OTHER;
1176 mm->addr |= MODES_NON_ICAO_ADDRESS;
1177 break;
1178
1179 case 2: // Fine TIS-B Message
1180 // IMF=0: AA field contains the 24-bit ICAO aircraft address
1181 // IMF=1: AA field contains the 12-bit Mode A code followed by a 12-bit track file number
1182 mm->source = SOURCE_TISB;
1183 mm->addrtype = ADDR_TISB_ICAO;
1184 check_imf = 1;
1185 break;
1186
1187 case 3: // Coarse TIS-B airborne position and velocity.
1188 // IMF=0: AA field contains the 24-bit ICAO aircraft address
1189 // IMF=1: AA field contains the 12-bit Mode A code followed by a 12-bit track file number
1190
1191 // For now we only look at the IMF bit.
1192 mm->source = SOURCE_TISB;
1193 mm->addrtype = ADDR_TISB_ICAO;
1194 if (getbit(me, 1))
1195 setIMF(mm);
1196 return;
1197
1198 case 5: // Fine TIS-B Message, AA field contains a non-ICAO 24-bit address
1199 mm->addrtype = ADDR_TISB_OTHER;
1200 mm->source = SOURCE_TISB;
1201 mm->addr |= MODES_NON_ICAO_ADDRESS;
1202 break;
1203
1204 case 6: // Rebroadcast of ADS-B Message from an alternate data link
1205 // IMF=0: AA field holds 24-bit ICAO aircraft address
1206 // IMF=1: AA field holds anonymous address or ground vehicle address or fixed obstruction address
1207 mm->addrtype = ADDR_ADSR_ICAO;
1208 check_imf = 1;
1209 break;
1210
1211 default: // All others, we don't know the format.
1212 mm->addrtype = ADDR_UNKNOWN;
1213 mm->addr |= MODES_NON_ICAO_ADDRESS; // assume non-ICAO
1214 return;
1215 }
1216 }
1217
1218 switch (metype) {
1219 case 1: case 2: case 3: case 4:
1220 decodeESIdentAndCategory(mm);
1221 break;
1222
1223 case 19:
1224 decodeESAirborneVelocity(mm, check_imf);
1225 break;
1226
1227 case 5: case 6: case 7: case 8:
1228 decodeESSurfacePosition(mm, check_imf);
1229 break;
1230
1231 case 0: // Airborne position, baro altitude only
1232 case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: // Airborne position, baro
1233 case 20: case 21: case 22: // Airborne position, GNSS altitude (HAE or MSL)
1234 decodeESAirbornePosition(mm, check_imf);
1235 break;
1236
1237 case 23:
1238 decodeESTestMessage(mm);
1239 break;
1240
1241 case 24: // Reserved for Surface System Status
1242 break;
1243
1244 case 28:
1245 decodeESAircraftStatus(mm, check_imf);
1246 break;
1247
1248 case 29:
1249 decodeESTargetStatus(mm, check_imf);
1250 break;
1251
1252 case 30: // Aircraft Operational Coordination
1253 break;
1254
1255 case 31:
1256 decodeESOperationalStatus(mm, check_imf);
1257 break;
1258
1259 default:
1260 break;
1261 }
1262 }
1263
1264 static void decodeCommB(struct modesMessage *mm)
1265 {
1266 unsigned char *msg = mm->msg;
1267
1268 // This is a bit hairy as we don't know what the requested register was
1269 if (getbits(msg, 33, 40) == 0x20) { // BDS 2,0 Aircraft Identification
1270 decodeBDS20(mm);
1271 }
1272 }
1273
1274 static const char *df_names[33] = {
1275 /* 0 */ "Short Air-Air Surveillance",
1276 /* 1 */ NULL,
1277 /* 2 */ NULL,
1278 /* 3 */ NULL,
1279 /* 4 */ "Survelliance, Altitude Reply",
1280 /* 5 */ "Survelliance, Identity Reply",
1281 /* 6 */ NULL,
1282 /* 7 */ NULL,
1283 /* 8 */ NULL,
1284 /* 9 */ NULL,
1285 /* 10 */ NULL,
1286 /* 11 */ "All Call Reply",
1287 /* 12 */ NULL,
1288 /* 13 */ NULL,
1289 /* 14 */ NULL,
1290 /* 15 */ NULL,
1291 /* 16 */ "Long Air-Air ACAS",
1292 /* 17 */ "Extended Squitter",
1293 /* 18 */ "Extended Squitter (Non-Transponder)",
1294 /* 19 */ "Extended Squitter (Military)",
1295 /* 20 */ "Comm-B, Altitude Reply",
1296 /* 21 */ "Comm-B, Identity Reply",
1297 /* 22 */ "Military Use",
1298 /* 23 */ NULL,
1299 /* 24 */ "Comm-D Extended Length Message",
1300 /* 25 */ "Comm-D Extended Length Message",
1301 /* 26 */ "Comm-D Extended Length Message",
1302 /* 27 */ "Comm-D Extended Length Message",
1303 /* 28 */ "Comm-D Extended Length Message",
1304 /* 29 */ "Comm-D Extended Length Message",
1305 /* 30 */ "Comm-D Extended Length Message",
1306 /* 31 */ "Comm-D Extended Length Message",
1307 /* 32 */ "Mode A/C Reply",
1308 };
1309
1310 static const char *df_to_string(unsigned df) {
1311 if (df > 32)
1312 return "out of range";
1313 if (!df_names[df])
1314 return "reserved";
1315 return df_names[df];
1316 }
1317
1318 static const char *altitude_unit_to_string(altitude_unit_t unit) {
1319 switch (unit) {
1320 case UNIT_FEET:
1321 return "ft";
1322 case UNIT_METERS:
1323 return "m";
1324 default:
1325 return "(unknown altitude unit)";
1326 }
1327 }
1328
1329 static const char *altitude_source_to_string(altitude_source_t source) {
1330 switch (source) {
1331 case ALTITUDE_BARO:
1332 return "barometric";
1333 case ALTITUDE_GNSS:
1334 return "GNSS";
1335 default:
1336 return "(unknown altitude source)";
1337 }
1338 }
1339
1340 static const char *airground_to_string(airground_t airground) {
1341 switch (airground) {
1342 case AG_GROUND:
1343 return "ground";
1344 case AG_AIRBORNE:
1345 return "airborne";
1346 case AG_INVALID:
1347 return "invalid";
1348 case AG_UNCERTAIN:
1349 return "airborne?";
1350 default:
1351 return "(unknown airground state)";
1352 }
1353 }
1354
1355 static const char *speed_source_to_string(speed_source_t speed) {
1356 switch (speed) {
1357 case SPEED_GROUNDSPEED:
1358 return "groundspeed";
1359 case SPEED_IAS:
1360 return "IAS";
1361 case SPEED_TAS:
1362 return "TAS";
1363 default:
1364 return "(unknown speed type)";
1365 }
1366 }
1367
1368 static const char *addrtype_to_string(addrtype_t type) {
1369 switch (type) {
1370 case ADDR_ADSB_ICAO:
1371 return "Mode S / ADS-B";
1372 case ADDR_ADSB_ICAO_NT:
1373 return "ADS-B, non-transponder";
1374 case ADDR_ADSB_OTHER:
1375 return "ADS-B, other addressing scheme";
1376 case ADDR_TISB_ICAO:
1377 return "TIS-B";
1378 case ADDR_TISB_OTHER:
1379 return "TIS-B, other addressing scheme";
1380 case ADDR_TISB_TRACKFILE:
1381 return "TIS-B, Mode A code and track file number";
1382 case ADDR_ADSR_ICAO:
1383 return "ADS-R";
1384 case ADDR_ADSR_OTHER:
1385 return "ADS-R, other addressing scheme";
1386 default:
1387 return "unknown addressing scheme";
1388 }
1389 }
1390
1391 static const char *cpr_type_to_string(cpr_type_t type) {
1392 switch (type) {
1393 case CPR_SURFACE:
1394 return "Surface";
1395 case CPR_AIRBORNE:
1396 return "Airborne";
1397 case CPR_COARSE:
1398 return "TIS-B Coarse";
1399 default:
1400 return "unknown CPR type";
1401 }
1402 }
1403
1404 static void print_hex_bytes(unsigned char *data, size_t len) {
1405 size_t i;
1406 for (i = 0; i < len; ++i) {
1407 printf("%02X", (unsigned)data[i]);
1408 }
1409 }
1410
1411 static int esTypeHasSubtype(unsigned metype)
1412 {
1413 if (metype <= 18) {
1414 return 0;
1415 }
1416
1417 if (metype >= 20 && metype <= 22) {
1418 return 0;
1419 }
1420
1421 return 1;
1422 }
1423
1424 static const char *esTypeName(unsigned metype, unsigned mesub)
1425 {
1426 switch (metype) {
1427 case 0:
1428 return "No position information (airborne or surface)";
1429
1430 case 1: case 2: case 3: case 4:
1431 return "Aircraft identification and category";
1432
1433 case 5: case 6: case 7: case 8:
1434 return "Surface position";
1435
1436 case 9: case 10: case 11: case 12:
1437 case 13: case 14: case 15: case 16:
1438 case 17: case 18:
1439 return "Airborne position (barometric altitude)";
1440
1441 case 19:
1442 switch (mesub) {
1443 case 1:
1444 return "Airborne velocity over ground, subsonic";
1445 case 2:
1446 return "Airborne velocity over ground, supersonic";
1447 case 3:
1448 return "Airspeed and heading, subsonic";
1449 case 4:
1450 return "Airspeed and heading, supersonic";
1451 default:
1452 return "Unknown";
1453 }
1454
1455 case 20: case 21: case 22:
1456 return "Airborne position (GNSS altitude)";
1457
1458 case 23:
1459 switch (mesub) {
1460 case 0:
1461 return "Test message";
1462 case 7:
1463 return "National use / 1090-WP-15-20 Mode A squawk";
1464 default:
1465 return "Unknown";
1466 }
1467
1468 case 24:
1469 return "Reserved for surface system status";
1470
1471 case 27:
1472 return "Reserved for trajectory change";
1473
1474 case 28:
1475 switch (mesub) {
1476 case 1:
1477 return "Emergency/priority status";
1478 case 2:
1479 return "ACAS RA broadcast";
1480 default:
1481 return "Unknown";
1482 }
1483
1484 case 29:
1485 switch (mesub) {
1486 case 0:
1487 return "Target state and status (V1)";
1488 case 1:
1489 return "Target state and status (V2)";
1490 default:
1491 return "Unknown";
1492 }
1493
1494 case 30:
1495 return "Aircraft Operational Coordination";
1496
1497 case 31: // Aircraft Operational Status
1498 switch (mesub) {
1499 case 0:
1500 return "Aircraft operational status (airborne)";
1501 case 1:
1502 return "Aircraft operational status (surface)";
1503 default:
1504 return "Unknown";
1505 }
1506
1507 default:
1508 return "Unknown";
1509 }
1510 }
1511
1512 void displayModesMessage(struct modesMessage *mm) {
1513 int j;
1514
1515 // Handle only addresses mode first.
1516 if (Modes.onlyaddr) {
1517 printf("%06x\n", mm->addr);
1518 return; // Enough for --onlyaddr mode
1519 }
1520
1521 // Show the raw message.
1522 if (Modes.mlat && mm->timestampMsg) {
1523 printf("@%012" PRIX64, mm->timestampMsg);
1524 } else
1525 printf("*");
1526
1527 for (j = 0; j < mm->msgbits/8; j++) printf("%02x", mm->msg[j]);
1528 printf(";\n");
1529
1530 if (Modes.raw) {
1531 fflush(stdout); // Provide data to the reader ASAP
1532 return; // Enough for --raw mode
1533 }
1534
1535 if (mm->msgtype < 32)
1536 printf("CRC: %06x\n", mm->crc);
1537
1538 if (mm->correctedbits != 0)
1539 printf("No. of bit errors fixed: %d\n", mm->correctedbits);
1540
1541 if (mm->signalLevel > 0)
1542 printf("RSSI: %.1f dBFS\n", 10 * log10(mm->signalLevel));
1543
1544 if (mm->score)
1545 printf("Score: %d\n", mm->score);
1546
1547 if (mm->timestampMsg) {
1548 if (mm->timestampMsg == MAGIC_MLAT_TIMESTAMP)
1549 printf("This is a synthetic MLAT message.\n");
1550 else
1551 printf("Time: %.2fus\n", mm->timestampMsg / 12.0);
1552 }
1553
1554 switch (mm->msgtype) {
1555 case 0:
1556 printf("DF:0 addr:%06X VS:%u CC:%u SL:%u RI:%u AC:%u\n",
1557 mm->addr, mm->VS, mm->CC, mm->SL, mm->RI, mm->AC);
1558 break;
1559
1560 case 4:
1561 printf("DF:4 addr:%06X FS:%u DR:%u UM:%u AC:%u\n",
1562 mm->addr, mm->FS, mm->DR, mm->UM, mm->AC);
1563 break;
1564
1565 case 5:
1566 printf("DF:5 addr:%06X FS:%u DR:%u UM:%u ID:%u\n",
1567 mm->addr, mm->FS, mm->DR, mm->UM, mm->ID);
1568 break;
1569
1570 case 11:
1571 printf("DF:11 AA:%06X IID:%u CA:%u\n",
1572 mm->AA, mm->IID, mm->CA);
1573 break;
1574
1575 case 16:
1576 printf("DF:16 addr:%06x VS:%u SL:%u RI:%u AC:%u MV:",
1577 mm->addr, mm->VS, mm->SL, mm->RI, mm->AC);
1578 print_hex_bytes(mm->MV, sizeof(mm->MV));
1579 printf("\n");
1580 break;
1581
1582 case 17:
1583 printf("DF:17 AA:%06X CA:%u ME:",
1584 mm->AA, mm->CA);
1585 print_hex_bytes(mm->ME, sizeof(mm->ME));
1586 printf("\n");
1587 break;
1588
1589 case 18:
1590 printf("DF:18 AA:%06X CF:%u ME:",
1591 mm->AA, mm->CF);
1592 print_hex_bytes(mm->ME, sizeof(mm->ME));
1593 printf("\n");
1594 break;
1595
1596 case 20:
1597 printf("DF:20 addr:%06X FS:%u DR:%u UM:%u AC:%u MB:",
1598 mm->addr, mm->FS, mm->DR, mm->UM, mm->AC);
1599 print_hex_bytes(mm->MB, sizeof(mm->MB));
1600 printf("\n");
1601 break;
1602
1603 case 21:
1604 printf("DF:21 addr:%06x FS:%u DR:%u UM:%u ID:%u MB:",
1605 mm->addr, mm->FS, mm->DR, mm->UM, mm->ID);
1606 print_hex_bytes(mm->MB, sizeof(mm->MB));
1607 printf("\n");
1608 break;
1609
1610 case 24:
1611 case 25:
1612 case 26:
1613 case 27:
1614 case 28:
1615 case 29:
1616 case 30:
1617 case 31:
1618 printf("DF:24 addr:%06x KE:%u ND:%u MD:",
1619 mm->addr, mm->KE, mm->ND);
1620 print_hex_bytes(mm->MD, sizeof(mm->MD));
1621 printf("\n");
1622 break;
1623 }
1624
1625 printf(" %s", df_to_string(mm->msgtype));
1626 if (mm->msgtype == 17 || mm->msgtype == 18) {
1627 if (esTypeHasSubtype(mm->metype)) {
1628 printf(" %s (%u/%u)",
1629 esTypeName(mm->metype, mm->mesub),
1630 mm->metype,
1631 mm->mesub);
1632 } else {
1633 printf(" %s (%u)",
1634 esTypeName(mm->metype, mm->mesub),
1635 mm->metype);
1636 }
1637 }
1638 printf("\n");
1639
1640 if (mm->addr & MODES_NON_ICAO_ADDRESS) {
1641 printf(" Other Address: %06X (%s)\n", mm->addr & 0xFFFFFF, addrtype_to_string(mm->addrtype));
1642 } else {
1643 printf(" ICAO Address: %06X (%s)\n", mm->addr, addrtype_to_string(mm->addrtype));
1644 }
1645
1646 if (mm->airground != AG_INVALID) {
1647 printf(" Air/Ground: %s\n",
1648 airground_to_string(mm->airground));
1649 }
1650
1651 if (mm->altitude_valid) {
1652 printf(" Altitude: %d %s %s\n",
1653 mm->altitude,
1654 altitude_unit_to_string(mm->altitude_unit),
1655 altitude_source_to_string(mm->altitude_source));
1656 }
1657
1658 if (mm->gnss_delta_valid) {
1659 printf(" GNSS delta: %d ft\n",
1660 mm->gnss_delta);
1661 }
1662
1663 if (mm->heading_valid) {
1664 printf(" Heading: %u\n", mm->heading);
1665 }
1666
1667 if (mm->speed_valid) {
1668 printf(" Speed: %u kt %s\n",
1669 mm->speed,
1670 speed_source_to_string(mm->speed_source));
1671 }
1672
1673 if (mm->vert_rate_valid) {
1674 printf(" Vertical rate: %d ft/min %s\n",
1675 mm->vert_rate,
1676 altitude_source_to_string(mm->vert_rate_source));
1677 }
1678
1679 if (mm->squawk_valid) {
1680 printf(" Squawk: %04x\n",
1681 mm->squawk);
1682 }
1683
1684 if (mm->callsign_valid) {
1685 printf(" Ident: %s\n",
1686 mm->callsign);
1687 }
1688
1689 if (mm->category_valid) {
1690 printf(" Category: %02X\n",
1691 mm->category);
1692 }
1693
1694 if (mm->cpr_valid) {
1695 printf(" CPR type: %s\n"
1696 " CPR odd flag: %s\n"
1697 " CPR NUCp/NIC: %u\n",
1698 cpr_type_to_string(mm->cpr_type),
1699 mm->cpr_odd ? "odd" : "even",
1700 mm->cpr_nucp);
1701
1702 if (mm->cpr_decoded) {
1703 printf(" CPR latitude: %.5f (%u)\n"
1704 " CPR longitude: %.5f (%u)\n"
1705 " CPR decoding: %s\n",
1706 mm->decoded_lat,
1707 mm->cpr_lat,
1708 mm->decoded_lon,
1709 mm->cpr_lon,
1710 mm->cpr_relative ? "local" : "global");
1711 } else {
1712 printf(" CPR latitude: (%u)\n"
1713 " CPR longitude: (%u)\n"
1714 " CPR decoding: none\n",
1715 mm->cpr_lat,
1716 mm->cpr_lon);
1717 }
1718 }
1719
1720 if (mm->opstatus.valid) {
1721 printf(" Aircraft Operational Status:\n");
1722 printf(" Version: %d\n", mm->opstatus.version);
1723
1724 printf(" Capability classes: ");
1725 if (mm->opstatus.cc_acas) printf("ACAS ");
1726 if (mm->opstatus.cc_cdti) printf("CDTI ");
1727 if (mm->opstatus.cc_1090_in) printf("1090IN ");
1728 if (mm->opstatus.cc_arv) printf("ARV ");
1729 if (mm->opstatus.cc_ts) printf("TS ");
1730 if (mm->opstatus.cc_tc) printf("TC=%d ", mm->opstatus.cc_tc);
1731 if (mm->opstatus.cc_uat_in) printf("UATIN ");
1732 if (mm->opstatus.cc_poa) printf("POA ");
1733 if (mm->opstatus.cc_b2_low) printf("B2-LOW ");
1734 if (mm->opstatus.cc_nac_v) printf("NACv=%d ", mm->opstatus.cc_nac_v);
1735 if (mm->opstatus.cc_nic_supp_c) printf("NIC-C=1 ");
1736 if (mm->opstatus.cc_lw_valid) printf("L/W=%d ", mm->opstatus.cc_lw);
1737 if (mm->opstatus.cc_antenna_offset) printf("GPS-OFFSET=%d ", mm->opstatus.cc_antenna_offset);
1738 printf("\n");
1739
1740 printf(" Operational modes: ");
1741 if (mm->opstatus.om_acas_ra) printf("ACASRA ");
1742 if (mm->opstatus.om_ident) printf("IDENT ");
1743 if (mm->opstatus.om_atc) printf("ATC ");
1744 if (mm->opstatus.om_saf) printf("SAF ");
1745 if (mm->opstatus.om_sda) printf("SDA=%d ", mm->opstatus.om_sda);
1746 printf("\n");
1747
1748 if (mm->opstatus.nic_supp_a) printf(" NIC-A: %d\n", mm->opstatus.nic_supp_a);
1749 if (mm->opstatus.nac_p) printf(" NACp: %d\n", mm->opstatus.nac_p);
1750 if (mm->opstatus.gva) printf(" GVA: %d\n", mm->opstatus.gva);
1751 if (mm->opstatus.sil) printf(" SIL: %d (%s)\n", mm->opstatus.sil, (mm->opstatus.sil_type == SIL_PER_HOUR ? "per hour" : "per sample"));
1752 if (mm->opstatus.nic_baro) printf(" NICbaro: %d\n", mm->opstatus.nic_baro);
1753
1754 if (mm->mesub == 1)
1755 printf(" Heading type: %s\n", (mm->opstatus.track_angle == ANGLE_HEADING ? "heading" : "track angle"));
1756 printf(" Heading reference: %s\n", (mm->opstatus.hrd == HEADING_TRUE ? "true north" : "magnetic north"));
1757 }
1758
1759 if (mm->tss.valid) {
1760 printf(" Target State and Status:\n");
1761 if (mm->tss.altitude_valid)
1762 printf(" Target altitude: %s, %d ft\n", (mm->tss.altitude_type == TSS_ALTITUDE_MCP ? "MCP" : "FMS"), mm->tss.altitude);
1763 if (mm->tss.baro_valid)
1764 printf(" Altimeter setting: %.1f millibars\n", mm->tss.baro);
1765 if (mm->tss.heading_valid)
1766 printf(" Target heading: %d\n", mm->tss.heading);
1767 if (mm->tss.mode_valid) {
1768 printf(" Active modes: ");
1769 if (mm->tss.mode_autopilot) printf("autopilot ");
1770 if (mm->tss.mode_vnav) printf("VNAV ");
1771 if (mm->tss.mode_alt_hold) printf("altitude-hold ");
1772 if (mm->tss.mode_approach) printf("approach ");
1773 printf("\n");
1774 }
1775 printf(" ACAS: %s\n", mm->tss.acas_operational ? "operational" : "NOT operational");
1776 printf(" NACp: %d\n", mm->tss.nac_p);
1777 printf(" NICbaro: %d\n", mm->tss.nic_baro);
1778 printf(" SIL: %d (%s)\n", mm->tss.sil, (mm->opstatus.sil_type == SIL_PER_HOUR ? "per hour" : "per sample"));
1779 }
1780
1781 printf("\n");
1782 fflush(stdout);
1783 }
1784
1785 //
1786 //=========================================================================
1787 //
1788 // When a new message is available, because it was decoded from the RTL device,
1789 // file, or received in the TCP input port, or any other way we can receive a
1790 // decoded message, we call this function in order to use the message.
1791 //
1792 // Basically this function passes a raw message to the upper layers for further
1793 // processing and visualization
1794 //
1795 void useModesMessage(struct modesMessage *mm) {
1796 struct aircraft *a;
1797
1798 ++Modes.stats_current.messages_total;
1799
1800 // Track aircraft state
1801 a = trackUpdateFromMessage(mm);
1802
1803 // In non-interactive non-quiet mode, display messages on standard output
1804 if (!Modes.interactive && !Modes.quiet && (!Modes.show_only || mm->addr == Modes.show_only)) {
1805 displayModesMessage(mm);
1806 }
1807
1808 // Feed output clients.
1809 // If in --net-verbatim mode, do this for all messages.
1810 // Otherwise, apply a sanity-check filter and only
1811 // forward messages when we have seen two of them.
1812
1813 if (Modes.net) {
1814 if (Modes.net_verbatim || mm->msgtype == 32) {
1815 // Unconditionally send
1816 modesQueueOutput(mm, a);
1817 } else if (a->messages > 1) {
1818 // If this is the second message, and we
1819 // squelched the first message, then re-emit the
1820 // first message now.
1821 if (!Modes.net_verbatim && a->messages == 2) {
1822 modesQueueOutput(&a->first_message, a);
1823 }
1824 modesQueueOutput(mm, a);
1825 }
1826 }
1827 }
1828
1829 //
1830 // ===================== Mode S detection and decoding ===================
1831 //