mode_s.c
Go to the documentation of this file.
1 // dump1090, a Mode S messages decoder for RTLSDR devices.
2 //
3 // Copyright (C) 2012 by Salvatore Sanfilippo <antirez@gmail.com>
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 
31 #include "dump1090.h"
32 #include "aera.h"
33 //
34 // ===================== Mode S detection and decoding ===================
35 //
36 // Parity table for MODE S Messages.
37 // The table contains 112 elements, every element corresponds to a bit set
38 // in the message, starting from the first bit of actual data after the
39 // preamble.
40 //
41 // For messages of 112 bit, the whole table is used.
42 // For messages of 56 bits only the last 56 elements are used.
43 //
44 // The algorithm is as simple as xoring all the elements in this table
45 // for which the corresponding bit on the message is set to 1.
46 //
47 // The latest 24 elements in this table are set to 0 as the checksum at the
48 // end of the message should not affect the computation.
49 //
50 // Note: this function can be used with DF11 and DF17, other modes have
51 // the CRC xored with the sender address as they are reply to interrogations,
52 // but a casual listener can't split the address from the checksum.
53 //
54 uint32_t modes_checksum_table[112] = {
55 0x3935ea, 0x1c9af5, 0xf1b77e, 0x78dbbf, 0xc397db, 0x9e31e9, 0xb0e2f0, 0x587178,
56 0x2c38bc, 0x161c5e, 0x0b0e2f, 0xfa7d13, 0x82c48d, 0xbe9842, 0x5f4c21, 0xd05c14,
57 0x682e0a, 0x341705, 0xe5f186, 0x72f8c3, 0xc68665, 0x9cb936, 0x4e5c9b, 0xd8d449,
58 0x939020, 0x49c810, 0x24e408, 0x127204, 0x093902, 0x049c81, 0xfdb444, 0x7eda22,
59 0x3f6d11, 0xe04c8c, 0x702646, 0x381323, 0xe3f395, 0x8e03ce, 0x4701e7, 0xdc7af7,
60 0x91c77f, 0xb719bb, 0xa476d9, 0xadc168, 0x56e0b4, 0x2b705a, 0x15b82d, 0xf52612,
61 0x7a9309, 0xc2b380, 0x6159c0, 0x30ace0, 0x185670, 0x0c2b38, 0x06159c, 0x030ace,
62 0x018567, 0xff38b7, 0x80665f, 0xbfc92b, 0xa01e91, 0xaff54c, 0x57faa6, 0x2bfd53,
63 0xea04ad, 0x8af852, 0x457c29, 0xdd4410, 0x6ea208, 0x375104, 0x1ba882, 0x0dd441,
64 0xf91024, 0x7c8812, 0x3e4409, 0xe0d800, 0x706c00, 0x383600, 0x1c1b00, 0x0e0d80,
65 0x0706c0, 0x038360, 0x01c1b0, 0x00e0d8, 0x00706c, 0x003836, 0x001c1b, 0xfff409,
66 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
67 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
68 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
69 };
70 
71 uint32_t modesChecksum(unsigned char *msg, int bits) {
72  uint32_t crc = 0;
73  uint32_t rem = 0;
74  int offset = (bits == 112) ? 0 : (112-56);
75  uint8_t theByte = *msg;
76  uint32_t * pCRCTable = &modes_checksum_table[offset];
77  int j;
78 
79  // We don't really need to include the checksum itself
80  bits -= 24;
81  for(j = 0; j < bits; j++) {
82  if ((j & 7) == 0)
83  theByte = *msg++;
84 
85  // If bit is set, xor with corresponding table entry.
86  if (theByte & 0x80) {crc ^= *pCRCTable;}
87  pCRCTable++;
88  theByte = theByte << 1;
89  }
90 
91  rem = (msg[0] << 16) | (msg[1] << 8) | msg[2]; // message checksum
92  return ((crc ^ rem) & 0x00FFFFFF); // 24 bit checksum syndrome.
93 }
94 //
95 //=========================================================================
96 //
97 // Given the Downlink Format (DF) of the message, return the message length in bits.
98 //
99 // All known DF's 16 or greater are long. All known DF's 15 or less are short.
100 // There are lots of unused codes in both category, so we can assume ICAO will stick to
101 // these rules, meaning that the most significant bit of the DF indicates the length.
102 //
103 int modesMessageLenByType(int type) {
104  return (type & 0x10) ? MODES_LONG_MSG_BITS : MODES_SHORT_MSG_BITS ;
105 }
106 //
107 //=========================================================================
108 //
109 // Try to fix single bit errors using the checksum. On success modifies
110 // the original buffer with the fixed version, and returns the position
111 // of the error bit. Otherwise if fixing failed -1 is returned.
112 /*
113 int fixSingleBitErrors(unsigned char *msg, int bits) {
114  int j;
115  unsigned char aux[MODES_LONG_MSG_BYTES];
116 
117  memcpy(aux, msg, bits/8);
118 
119  // Do not attempt to error correct Bits 0-4. These contain the DF, and must
120  // be correct because we can only error correct DF17
121  for (j = 5; j < bits; j++) {
122  int byte = j/8;
123  int bitmask = 1 << (7 - (j & 7));
124 
125  aux[byte] ^= bitmask; // Flip j-th bit
126 
127  if (0 == modesChecksum(aux, bits)) {
128  // The error is fixed. Overwrite the original buffer with the
129  // corrected sequence, and returns the error bit position
130  msg[byte] = aux[byte];
131  return (j);
132  }
133 
134  aux[byte] ^= bitmask; // Flip j-th bit back again
135  }
136  return (-1);
137 }
138 */
139 //=========================================================================
140 //
141 // Similar to fixSingleBitErrors() but try every possible two bit combination.
142 // This is very slow and should be tried only against DF17 messages that
143 // don't pass the checksum, and only in Aggressive Mode.
144 /*
145 int fixTwoBitsErrors(unsigned char *msg, int bits) {
146  int j, i;
147  unsigned char aux[MODES_LONG_MSG_BYTES];
148 
149  memcpy(aux, msg, bits/8);
150 
151  // Do not attempt to error correct Bits 0-4. These contain the DF, and must
152  // be correct because we can only error correct DF17
153  for (j = 5; j < bits; j++) {
154  int byte1 = j/8;
155  int bitmask1 = 1 << (7 - (j & 7));
156  aux[byte1] ^= bitmask1; // Flip j-th bit
157 
158  // Don't check the same pairs multiple times, so i starts from j+1
159  for (i = j+1; i < bits; i++) {
160  int byte2 = i/8;
161  int bitmask2 = 1 << (7 - (i & 7));
162 
163  aux[byte2] ^= bitmask2; // Flip i-th bit
164 
165  if (0 == modesChecksum(aux, bits)) {
166  // The error is fixed. Overwrite the original buffer with
167  // the corrected sequence, and returns the error bit position
168  msg[byte1] = aux[byte1];
169  msg[byte2] = aux[byte2];
170 
171  // We return the two bits as a 16 bit integer by shifting
172  // 'i' on the left. This is possible since 'i' will always
173  // be non-zero because i starts from j+1
174  return (j | (i << 8));
175 
176  aux[byte2] ^= bitmask2; // Flip i-th bit back
177  }
178 
179  aux[byte1] ^= bitmask1; // Flip j-th bit back
180  }
181  }
182  return (-1);
183 }
184 */
185 //
186 //=========================================================================
187 //
188 // Code for introducing a less CPU-intensive method of correcting
189 // single bit errors.
190 //
191 // Makes use of the fact that the crc checksum is linear with respect to
192 // the bitwise xor operation, i.e.
193 // crc(m^e) = (crc(m)^crc(e)
194 // where m and e are the message resp. error bit vectors.
195 //
196 // Call crc(e) the syndrome.
197 //
198 // The code below works by precomputing a table of (crc(e), e) for all
199 // possible error vectors e (here only single bit and double bit errors),
200 // search for the syndrome in the table, and correct the then known error.
201 // The error vector e is represented by one or two bit positions that are
202 // changed. If a second bit position is not used, it is -1.
203 //
204 // Run-time is binary search in a sorted table, plus some constant overhead,
205 // instead of running through all possible bit positions (resp. pairs of
206 // bit positions).
207 //
208 struct errorinfo {
209  uint32_t syndrome; // CRC syndrome
210  int bits; // Number of bit positions to fix
211  int pos[MODES_MAX_BITERRORS]; // Bit positions corrected by this syndrome
212 };
213 
214 #define NERRORINFO \
215  (MODES_LONG_MSG_BITS+MODES_LONG_MSG_BITS*(MODES_LONG_MSG_BITS-1)/2)
217 
218 // Compare function as needed for stdlib's qsort and bsearch functions
219 int cmpErrorInfo(const void *p0, const void *p1) {
220  struct errorinfo *e0 = (struct errorinfo*)p0;
221  struct errorinfo *e1 = (struct errorinfo*)p1;
222  if (e0->syndrome == e1->syndrome) {
223  return 0;
224  } else if (e0->syndrome < e1->syndrome) {
225  return -1;
226  } else {
227  return 1;
228  }
229 }
230 //
231 //=========================================================================
232 //
233 // Compute the table of all syndromes for 1-bit and 2-bit error vectors
235  unsigned char msg[MODES_LONG_MSG_BYTES];
236  int i, j, n;
237  uint32_t crc;
238  n = 0;
239  memset(bitErrorTable, 0, sizeof(bitErrorTable));
240  memset(msg, 0, MODES_LONG_MSG_BYTES);
241  // Add all possible single and double bit errors
242  // don't include errors in first 5 bits (DF type)
243  for (i = 5; i < MODES_LONG_MSG_BITS; i++) {
244  int bytepos0 = (i >> 3);
245  int mask0 = 1 << (7 - (i & 7));
246  msg[bytepos0] ^= mask0; // create error0
247  crc = modesChecksum(msg, MODES_LONG_MSG_BITS);
248  bitErrorTable[n].syndrome = crc; // single bit error case
249  bitErrorTable[n].bits = 1;
250  bitErrorTable[n].pos[0] = i;
251  bitErrorTable[n].pos[1] = -1;
252  n += 1;
253 
254  if (Modes.nfix_crc > 1) {
255  for (j = i+1; j < MODES_LONG_MSG_BITS; j++) {
256  int bytepos1 = (j >> 3);
257  int mask1 = 1 << (7 - (j & 7));
258  msg[bytepos1] ^= mask1; // create error1
259  crc = modesChecksum(msg, MODES_LONG_MSG_BITS);
260  if (n >= NERRORINFO) {
261  //fprintf(stderr, "Internal error, too many entries, fix NERRORINFO\n");
262  break;
263  }
264  bitErrorTable[n].syndrome = crc; // two bit error case
265  bitErrorTable[n].bits = 2;
266  bitErrorTable[n].pos[0] = i;
267  bitErrorTable[n].pos[1] = j;
268  n += 1;
269  msg[bytepos1] ^= mask1; // revert error1
270  }
271  }
272  msg[bytepos0] ^= mask0; // revert error0
273  }
274  qsort(bitErrorTable, NERRORINFO, sizeof(struct errorinfo), cmpErrorInfo);
275 
276  // Test code: report if any syndrome appears at least twice. In this
277  // case the correction cannot be done without ambiguity.
278  // Tried it, does not happen for 1- and 2-bit errors.
279  /*
280  for (i = 1; i < NERRORINFO; i++) {
281  if (bitErrorTable[i-1].syndrome == bitErrorTable[i].syndrome) {
282  fprintf(stderr, "modesInitErrorInfo: Collision for syndrome %06x\n",
283  (int)bitErrorTable[i].syndrome);
284  }
285  }
286 
287  for (i = 0; i < NERRORINFO; i++) {
288  printf("syndrome %06x bit0 %3d bit1 %3d\n",
289  bitErrorTable[i].syndrome,
290  bitErrorTable[i].pos0, bitErrorTable[i].pos1);
291  }
292  */
293 }
294 //
295 //=========================================================================
296 //
297 // Search for syndrome in table and if an entry is found, flip the necessary
298 // bits. Make sure the indices fit into the array
299 // Additional parameter: fix only less than maxcorrected bits, and record
300 // fixed bit positions in corrected[]. This array can be NULL, otherwise
301 // must be of length at least maxcorrected.
302 // Return number of fixed bits.
303 //
304 int fixBitErrors(unsigned char *msg, int bits, int maxfix, char *fixedbits) {
305  struct errorinfo *pei;
306  struct errorinfo ei;
307  int bitpos, offset, res, i;
308  memset(&ei, 0, sizeof(struct errorinfo));
309  ei.syndrome = modesChecksum(msg, bits);
310  pei = bsearch(&ei, bitErrorTable, NERRORINFO,
311  sizeof(struct errorinfo), cmpErrorInfo);
312  if (pei == NULL) {
313  return 0; // No syndrome found
314  }
315 
316  // Check if the syndrome fixes more bits than we allow
317  if (maxfix < pei->bits) {
318  return 0;
319  }
320 
321  // Check that all bit positions lie inside the message length
322  offset = MODES_LONG_MSG_BITS-bits;
323  for (i = 0; i < pei->bits; i++) {
324  bitpos = pei->pos[i] - offset;
325  if ((bitpos < 0) || (bitpos >= bits)) {
326  return 0;
327  }
328  }
329 
330  // Fix the bits
331  for (i = res = 0; i < pei->bits; i++) {
332  bitpos = pei->pos[i] - offset;
333  msg[bitpos >> 3] ^= (1 << (7 - (bitpos & 7)));
334  if (fixedbits) {
335  fixedbits[res++] = bitpos;
336  }
337  }
338  return res;
339 }
340 //
341 // ============================== Debugging =================================
342 //
343 // Helper function for dumpMagnitudeVector().
344 // It prints a single bar used to display raw signals.
345 //
346 // Since every magnitude sample is between 0-255, the function uses
347 // up to 63 characters for every bar. Every character represents
348 // a length of 4, 3, 2, 1, specifically:
349 //
350 // "O" is 4
351 // "o" is 3
352 // "-" is 2
353 // "." is 1
354 //
355 void dumpMagnitudeBar(int index, int magnitude) {
356  char *set = " .-o";
357  char buf[256];
358  int div = magnitude / 256 / 4;
359  int rem = magnitude / 256 % 4;
360 
361  memset(buf,'O',div);
362  buf[div] = set[rem];
363  buf[div+1] = '\0';
364 
365  if (index >= 0)
366  printf("[%.3d] |%-66s 0x%04X\n", index, buf, magnitude);
367  else
368  printf("[%.2d] |%-66s 0x%04X\n", index, buf, magnitude);
369 }
370 //
371 //=========================================================================
372 //
373 // Display an ASCII-art alike graphical representation of the undecoded
374 // message as a magnitude signal.
375 //
376 // The message starts at the specified offset in the "m" buffer.
377 // The function will display enough data to cover a short 56 bit message.
378 //
379 // If possible a few samples before the start of the messsage are included
380 // for context.
381 //
382 void dumpMagnitudeVector(uint16_t *m, uint32_t offset) {
383  uint32_t padding = 5; // Show a few samples before the actual start.
384  uint32_t start = (offset < padding) ? 0 : offset-padding;
385  uint32_t end = offset + (MODES_PREAMBLE_SAMPLES)+(MODES_SHORT_MSG_SAMPLES) - 1;
386  uint32_t j;
387 
388  for (j = start; j <= end; j++) {
389  dumpMagnitudeBar(j-offset, m[j]);
390  }
391 }
392 //
393 //=========================================================================
394 //
395 // Produce a raw representation of the message as a Javascript file
396 // loadable by debug.html.
397 //
398 void dumpRawMessageJS(char *descr, unsigned char *msg,
399  uint16_t *m, uint32_t offset, int fixable, char *bitpos)
400 {
401  int padding = 5; // Show a few samples before the actual start.
402  int start = offset - padding;
403  int end = offset + (MODES_PREAMBLE_SAMPLES)+(MODES_LONG_MSG_SAMPLES) - 1;
404  FILE *fp;
405  int j;
406 
407  MODES_NOTUSED(fixable);
408  if ((fp = fopen("frames.js","a")) == NULL) {
409  fprintf(stderr, "Error opening frames.js: %s\n", strerror(errno));
410  exit(1);
411  }
412 
413  fprintf(fp,"frames.push({\"descr\": \"%s\", \"mag\": [", descr);
414  for (j = start; j <= end; j++) {
415  fprintf(fp,"%d", j < 0 ? 0 : m[j]);
416  if (j != end) fprintf(fp,",");
417  }
418  fprintf(fp,"], \"fix1\": %d, \"fix2\": %d, \"bits\": %d, \"hex\": \"",
419  bitpos[0], bitpos[1] , modesMessageLenByType(msg[0]>>3));
420  for (j = 0; j < MODES_LONG_MSG_BYTES; j++)
421  fprintf(fp,"\\x%02x",msg[j]);
422  fprintf(fp,"\"});\n");
423  fclose(fp);
424 }
425 //
426 //=========================================================================
427 //
428 // This is a wrapper for dumpMagnitudeVector() that also show the message
429 // in hex format with an additional description.
430 //
431 // descr is the additional message to show to describe the dump.
432 // msg points to the decoded message
433 // m is the original magnitude vector
434 // offset is the offset where the message starts
435 //
436 // The function also produces the Javascript file used by debug.html to
437 // display packets in a graphical format if the Javascript output was
438 // enabled.
439 //
440 void dumpRawMessage(char *descr, unsigned char *msg, uint16_t *m, uint32_t offset) {
441  int j;
442  int msgtype = msg[0] >> 3;
443  int fixable = 0;
444  char bitpos[MODES_MAX_BITERRORS];
445 
446  for (j = 0; j < MODES_MAX_BITERRORS; j++) {
447  bitpos[j] = -1;
448  }
449  if (msgtype == 17) {
450  fixable = fixBitErrors(msg, MODES_LONG_MSG_BITS, MODES_MAX_BITERRORS, bitpos);
451  }
452 
453  if (Modes.debug & MODES_DEBUG_JS) {
454  dumpRawMessageJS(descr, msg, m, offset, fixable, bitpos);
455  return;
456  }
457 
458  printf("\n--- %s\n ", descr);
459  for (j = 0; j < MODES_LONG_MSG_BYTES; j++) {
460  printf("%02x",msg[j]);
461  if (j == MODES_SHORT_MSG_BYTES-1) printf(" ... ");
462  }
463  printf(" (DF %d, Fixable: %d)\n", msgtype, fixable);
464  dumpMagnitudeVector(m,offset);
465  printf("---\n\n");
466 }
467 //
468 //=========================================================================
469 //
470 // Code for testing the timing: run all possible 1- and 2-bit error
471 // the test message by all 1-bit errors. Run the old code against
472 // all of them, and new the code.
473 //
474 // Example measurements:
475 // Timing old vs. new crc correction code:
476 // Old code: 1-bit errors on 112 msgs: 3934 usecs
477 // New code: 1-bit errors on 112 msgs: 104 usecs
478 // Old code: 2-bit errors on 6216 msgs: 407743 usecs
479 // New code: 2-bit errors on 6216 msgs: 5176 usecs
480 // indicating a 37-fold resp. 78-fold improvement in speed for 1-bit resp.
481 // 2-bit error.
482 /*
483 unsigned char tmsg0[MODES_LONG_MSG_BYTES] = {
484  // Test data: first ADS-B message from testfiles/modes1.bin
485  0x8f, 0x4d, 0x20, 0x23, 0x58, 0x7f, 0x34, 0x5e,
486  0x35, 0x83, 0x7e, 0x22, 0x18, 0xb2
487 };
488 #define NTWOBITS (MODES_LONG_MSG_BITS*(MODES_LONG_MSG_BITS-1)/2)
489 unsigned char tmsg1[MODES_LONG_MSG_BITS][MODES_LONG_MSG_BYTES];
490 unsigned char tmsg2[NTWOBITS][MODES_LONG_MSG_BYTES];
491 // Init an array of cloned messages with all possible 1-bit errors present,
492 // applied to each message at the respective position
493 //
494 void inittmsg1() {
495  int i, bytepos, mask;
496  for (i = 0; i < MODES_LONG_MSG_BITS; i++) {
497  bytepos = i >> 3;
498  mask = 1 << (7 - (i & 7));
499  memcpy(&tmsg1[i][0], tmsg0, MODES_LONG_MSG_BYTES);
500  tmsg1[i][bytepos] ^= mask;
501  }
502 }
503 
504 // Run sanity check on all but first 5 messages / bits, as those bits
505 // are not corrected.
506 //
507 void checktmsg1(FILE *out) {
508  int i, k;
509  uint32_t crc;
510  for (i = 5; i < MODES_LONG_MSG_BITS; i++) {
511  crc = modesChecksum(&tmsg1[i][0], MODES_LONG_MSG_BITS);
512  if (crc != 0) {
513  fprintf(out, "CRC not fixed for "
514  "positon %d\n", i);
515  fprintf(out, " MSG ");
516  for (k = 0; k < MODES_LONG_MSG_BYTES; k++) {
517  fprintf(out, "%02x", tmsg1[i][k]);
518  }
519  fprintf(out, "\n");
520  }
521  }
522 }
523 
524 void inittmsg2() {
525  int i, j, n, bytepos0, bytepos1, mask0, mask1;
526  n = 0;
527  for (i = 0; i < MODES_LONG_MSG_BITS; i++) {
528  bytepos0 = i >> 3;
529  mask0 = 1 << (7 - (i & 7));
530  for (j = i+1; j < MODES_LONG_MSG_BITS; j++) {
531  bytepos1 = j >> 3;
532  mask1 = 1 << (7 - (j & 7));
533  memcpy(&tmsg2[n][0], tmsg0, MODES_LONG_MSG_BYTES);
534  tmsg2[n][bytepos0] ^= mask0;
535  tmsg2[n][bytepos1] ^= mask1;
536  n += 1;
537  }
538  }
539 }
540 
541 long difftvusec(struct timeval *t0, struct timeval *t1) {
542  long res = 0;
543  res = t1->tv_usec-t0->tv_usec;
544  res += (t1->tv_sec-t0->tv_sec)*1000000L;
545  return res;
546 }
547 
548 // the actual test code
549 void testAndTimeBitCorrection() {
550  struct timeval starttv, endtv;
551  int i;
552  // Run timing on 1-bit errors
553  printf("Timing old vs. new crc correction code:\n");
554  inittmsg1();
555  gettimeofday(&starttv, NULL);
556  for (i = 0; i < MODES_LONG_MSG_BITS; i++) {
557  fixSingleBitErrors(&tmsg1[i][0], MODES_LONG_MSG_BITS);
558  }
559  gettimeofday(&endtv, NULL);
560  printf(" Old code: 1-bit errors on %d msgs: %ld usecs\n",
561  MODES_LONG_MSG_BITS, difftvusec(&starttv, &endtv));
562  checktmsg1(stdout);
563  // Re-init
564  inittmsg1();
565  gettimeofday(&starttv, NULL);
566  for (i = 0; i < MODES_LONG_MSG_BITS; i++) {
567  fixBitErrors(&tmsg1[i][0], MODES_LONG_MSG_BITS, MODES_MAX_BITERRORS, NULL);
568  }
569  gettimeofday(&endtv, NULL);
570  printf(" New code: 1-bit errors on %d msgs: %ld usecs\n",
571  MODES_LONG_MSG_BITS, difftvusec(&starttv, &endtv));
572  checktmsg1(stdout);
573  // Run timing on 2-bit errors
574  inittmsg2();
575  gettimeofday(&starttv, NULL);
576  for (i = 0; i < NTWOBITS; i++) {
577  fixSingleBitErrors(&tmsg2[i][0], MODES_LONG_MSG_BITS);
578  }
579  gettimeofday(&endtv, NULL);
580  printf(" Old code: 2-bit errors on %d msgs: %ld usecs\n",
581  NTWOBITS, difftvusec(&starttv, &endtv));
582  // Re-init
583  inittmsg2();
584  gettimeofday(&starttv, NULL);
585  for (i = 0; i < NTWOBITS; i++) {
586  fixBitErrors(&tmsg2[i][0], MODES_LONG_MSG_BITS, MODES_MAX_BITERRORS, NULL);
587  }
588  gettimeofday(&endtv, NULL);
589  printf(" New code: 2-bit errors on %d msgs: %ld usecs\n",
590  NTWOBITS, difftvusec(&starttv, &endtv));
591 }
592 */
593 //=========================================================================
594 //
595 // Hash the ICAO address to index our cache of MODES_ICAO_CACHE_LEN
596 // elements, that is assumed to be a power of two
597 //
598 uint32_t ICAOCacheHashAddress(uint32_t a) {
599  // The following three rounds wil make sure that every bit affects
600  // every output bit with ~ 50% of probability.
601  a = ((a >> 16) ^ a) * 0x45d9f3b;
602  a = ((a >> 16) ^ a) * 0x45d9f3b;
603  a = ((a >> 16) ^ a);
604  return a & (MODES_ICAO_CACHE_LEN-1);
605 }
606 //
607 //=========================================================================
608 //
609 // Add the specified entry to the cache of recently seen ICAO addresses.
610 // Note that we also add a timestamp so that we can make sure that the
611 // entry is only valid for MODES_ICAO_CACHE_TTL seconds.
612 //
613 void addRecentlySeenICAOAddr(uint32_t addr) {
614  uint32_t h = ICAOCacheHashAddress(addr);
615  Modes.icao_cache[h*2] = addr;
616  Modes.icao_cache[h*2+1] = (uint32_t) time(NULL);
617 }
618 //
619 //=========================================================================
620 //
621 // Returns 1 if the specified ICAO address was seen in a DF format with
622 // proper checksum (not xored with address) no more than * MODES_ICAO_CACHE_TTL
623 // seconds ago. Otherwise returns 0.
624 //
625 int ICAOAddressWasRecentlySeen(uint32_t addr) {
626  uint32_t h = ICAOCacheHashAddress(addr);
627  uint32_t a = Modes.icao_cache[h*2];
628  uint32_t t = Modes.icao_cache[h*2+1];
629  uint64_t tn = time(NULL);
630 
631  return ( (a) && (a == addr) && ( (tn - t) <= MODES_ICAO_CACHE_TTL) );
632 }
633 //
634 //=========================================================================
635 //
636 // In the squawk (identity) field bits are interleaved as follows in
637 // (message bit 20 to bit 32):
638 //
639 // C1-A1-C2-A2-C4-A4-ZERO-B1-D1-B2-D2-B4-D4
640 //
641 // So every group of three bits A, B, C, D represent an integer from 0 to 7.
642 //
643 // The actual meaning is just 4 octal numbers, but we convert it into a hex
644 // number tha happens to represent the four octal numbers.
645 //
646 // For more info: http://en.wikipedia.org/wiki/Gillham_code
647 //
648 int decodeID13Field(int ID13Field) {
649  int hexGillham = 0;
650 
651  if (ID13Field & 0x1000) {hexGillham |= 0x0010;} // Bit 12 = C1
652  if (ID13Field & 0x0800) {hexGillham |= 0x1000;} // Bit 11 = A1
653  if (ID13Field & 0x0400) {hexGillham |= 0x0020;} // Bit 10 = C2
654  if (ID13Field & 0x0200) {hexGillham |= 0x2000;} // Bit 9 = A2
655  if (ID13Field & 0x0100) {hexGillham |= 0x0040;} // Bit 8 = C4
656  if (ID13Field & 0x0080) {hexGillham |= 0x4000;} // Bit 7 = A4
657  //if (ID13Field & 0x0040) {hexGillham |= 0x0800;} // Bit 6 = X or M
658  if (ID13Field & 0x0020) {hexGillham |= 0x0100;} // Bit 5 = B1
659  if (ID13Field & 0x0010) {hexGillham |= 0x0001;} // Bit 4 = D1 or Q
660  if (ID13Field & 0x0008) {hexGillham |= 0x0200;} // Bit 3 = B2
661  if (ID13Field & 0x0004) {hexGillham |= 0x0002;} // Bit 2 = D2
662  if (ID13Field & 0x0002) {hexGillham |= 0x0400;} // Bit 1 = B4
663  if (ID13Field & 0x0001) {hexGillham |= 0x0004;} // Bit 0 = D4
664 
665  return (hexGillham);
666  }
667 //
668 //=========================================================================
669 //
670 // Decode the 13 bit AC altitude field (in DF 20 and others).
671 // Returns the altitude, and set 'unit' to either MODES_UNIT_METERS or MDOES_UNIT_FEETS.
672 //
673 int decodeAC13Field(int AC13Field, int *unit) {
674  int m_bit = AC13Field & 0x0040; // set = meters, clear = feet
675  int q_bit = AC13Field & 0x0010; // set = 25 ft encoding, clear = Gillham Mode C encoding
676 
677  if (!m_bit) {
678  *unit = MODES_UNIT_FEET;
679  if (q_bit) {
680  // N is the 11 bit integer resulting from the removal of bit Q and M
681  int n = ((AC13Field & 0x1F80) >> 2) |
682  ((AC13Field & 0x0020) >> 1) |
683  (AC13Field & 0x000F);
684  // The final altitude is resulting number multiplied by 25, minus 1000.
685  return ((n * 25) - 1000);
686  } else {
687  // N is an 11 bit Gillham coded altitude
688  int n = ModeAToModeC(decodeID13Field(AC13Field));
689  if (n < -12) {n = 0;}
690 
691  return (100 * n);
692  }
693  } else {
694  *unit = MODES_UNIT_METERS;
695  // TODO: Implement altitude when meter unit is selected
696  }
697  return 0;
698 }
699 //
700 //=========================================================================
701 //
702 // Decode the 12 bit AC altitude field (in DF 17 and others).
703 //
704 int decodeAC12Field(int AC12Field, int *unit) {
705  int q_bit = AC12Field & 0x10; // Bit 48 = Q
706 
707  *unit = MODES_UNIT_FEET;
708  if (q_bit) {
710  int n = ((AC12Field & 0x0FE0) >> 1) |
711  (AC12Field & 0x000F);
712  // The final altitude is the resulting number multiplied by 25, minus 1000.
713  return ((n * 25) - 1000);
714  } else {
715  // Make N a 13 bit Gillham coded altitude by inserting M=0 at bit 6
716  int n = ((AC12Field & 0x0FC0) << 1) |
717  (AC12Field & 0x003F);
719  if (n < -12) {n = 0;}
720 
721  return (100 * n);
722  }
723 }
724 //
725 //=========================================================================
726 //
727 // Decode the 7 bit ground movement field PWL exponential style scale
728 //
729 int decodeMovementField(int movement) {
730  int gspeed;
731 
732  // Note : movement codes 0,125,126,127 are all invalid, but they are
733  // trapped for before this function is called.
734 
735  if (movement > 123) gspeed = 199; // > 175kt
736  else if (movement > 108) gspeed = ((movement - 108) * 5) + 100;
737  else if (movement > 93) gspeed = ((movement - 93) * 2) + 70;
738  else if (movement > 38) gspeed = ((movement - 38) ) + 15;
739  else if (movement > 12) gspeed = ((movement - 11) >> 1) + 2;
740  else if (movement > 8) gspeed = ((movement - 6) >> 2) + 1;
741  else gspeed = 0;
742 
743  return (gspeed);
744 }
745 //
746 //=========================================================================
747 //
748 // Capability table
749 char *ca_str[8] = {
750  /* 0 */ "Level 1 (Survillance Only)",
751  /* 1 */ "Level 2 (DF0,4,5,11)",
752  /* 2 */ "Level 3 (DF0,4,5,11,20,21)",
753  /* 3 */ "Level 4 (DF0,4,5,11,20,21,24)",
754  /* 4 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7 - is on ground)",
755  /* 5 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7 - is airborne)",
756  /* 6 */ "Level 2+3+4 (DF0,4,5,11,20,21,24,code7)",
757  /* 7 */ "Level 7 ???"
758 };
759 
760 // DF 18 Control field table.
761 char *cf_str[8] = {
762  /* 0 */ "ADS-B ES/NT device with ICAO 24-bit address",
763  /* 1 */ "ADS-B ES/NT device with other address",
764  /* 2 */ "Fine format TIS-B",
765  /* 3 */ "Coarse format TIS-B",
766  /* 4 */ "TIS-B managment message",
767  /* 5 */ "TIS-B relay of ADS-B message with other address",
768  /* 6 */ "ADS-B rebroadcast using DF-17 message format",
769  /* 7 */ "Reserved"
770 };
771 
772 // Flight status table
773 char *fs_str[8] = {
774  /* 0 */ "Normal, Airborne",
775  /* 1 */ "Normal, On the ground",
776  /* 2 */ "ALERT, Airborne",
777  /* 3 */ "ALERT, On the ground",
778  /* 4 */ "ALERT & Special Position Identification. Airborne or Ground",
779  /* 5 */ "Special Position Identification. Airborne or Ground",
780  /* 6 */ "Value 6 is not assigned",
781  /* 7 */ "Value 7 is not assigned"
782 };
783 //
784 //=========================================================================
785 //
786 char *getMEDescription(int metype, int mesub) {
787  char *mename = "Unknown";
788 
789  if (metype >= 1 && metype <= 4)
790  mename = "Aircraft Identification and Category";
791  else if (metype >= 5 && metype <= 8)
792  mename = "Surface Position";
793  else if (metype >= 9 && metype <= 18)
794  mename = "Airborne Position (Baro Altitude)";
795  else if (metype == 19 && mesub >=1 && mesub <= 4)
796  mename = "Airborne Velocity";
797  else if (metype >= 20 && metype <= 22)
798  mename = "Airborne Position (GNSS Height)";
799  else if (metype == 23 && mesub == 0)
800  mename = "Test Message";
801  else if (metype == 24 && mesub == 1)
802  mename = "Surface System Status";
803  else if (metype == 28 && mesub == 1)
804  mename = "Extended Squitter Aircraft Status (Emergency)";
805  else if (metype == 28 && mesub == 2)
806  mename = "Extended Squitter Aircraft Status (1090ES TCAS RA)";
807  else if (metype == 29 && (mesub == 0 || mesub == 1))
808  mename = "Target State and Status Message";
809  else if (metype == 31 && (mesub == 0 || mesub == 1))
810  mename = "Aircraft Operational Status Message";
811  return mename;
812 }
813 //
814 //=========================================================================
815 //
816 // Decode a raw Mode S message demodulated as a stream of bytes by detectModeS(),
817 // and split it into fields populating a modesMessage structure.
818 //
819 void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
820  char *ais_charset = "?ABCDEFGHIJKLMNOPQRSTUVWXYZ????? ???????????????0123456789??????";
821 
822  // Work on our local copy
823  memcpy(mm->msg, msg, MODES_LONG_MSG_BYTES);
824  msg = mm->msg;
825 
826  // Get the message type ASAP as other operations depend on this
827  mm->msgtype = msg[0] >> 3; // Downlink Format
829  mm->crc = modesChecksum(msg, mm->msgbits);
830 
831  if ((mm->crc) && (Modes.nfix_crc) && ((mm->msgtype == 17) || (mm->msgtype == 18))) {
832 // if ((mm->crc) && (Modes.nfix_crc) && ((mm->msgtype == 11) || (mm->msgtype == 17))) {
833  //
834  // Fixing single bit errors in DF-11 is a bit dodgy because we have no way to
835  // know for sure if the crc is supposed to be 0 or not - it could be any value
836  // less than 80. Therefore, attempting to fix DF-11 errors can result in a
837  // multitude of possible crc solutions, only one of which is correct.
838  //
839  // We should probably perform some sanity checks on corrected DF-11's before
840  // using the results. Perhaps check the ICAO against known aircraft, and check
841  // IID against known good IID's. That's a TODO.
842  //
843  mm->correctedbits = fixBitErrors(msg, mm->msgbits, Modes.nfix_crc, mm->corrected);
844 
845  // If we correct, validate ICAO addr to help filter birthday paradox solutions.
846  if (mm->correctedbits) {
847  uint32_t ulAddr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
848  if (!ICAOAddressWasRecentlySeen(ulAddr))
849  mm->correctedbits = 0;
850  }
851  }
852  //
853  // Note that most of the other computation happens *after* we fix the
854  // single/two bit errors, otherwise we would need to recompute the fields again.
855  //
856  if (mm->msgtype == 11) { // DF 11
857  mm->iid = mm->crc;
858  mm->addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
859  mm->ca = (msg[0] & 0x07); // Responder capabilities
860 
861  if ((mm->crcok = (0 == mm->crc))) {
862  // DF 11 : if crc == 0 try to populate our ICAO addresses whitelist.
864  } else if (mm->crc < 80) {
866  if (mm->crcok) {
868  }
869  }
870 
871  } else if (mm->msgtype == 17) { // DF 17
872  mm->addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
873  mm->ca = (msg[0] & 0x07); // Responder capabilities
874 
875  if ((mm->crcok = (0 == mm->crc))) {
876  // DF 17 : if crc == 0 try to populate our ICAO addresses whitelist.
878  }
879 
880  } else if (mm->msgtype == 18) { // DF 18
881  mm->addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
882  mm->ca = (msg[0] & 0x07); // Control Field
883 
884  if ((mm->crcok = (0 == mm->crc))) {
885  // DF 18 : if crc == 0 try to populate our ICAO addresses whitelist.
887  }
888 
889  } else { // All other DF's
890  // Compare the checksum with the whitelist of recently seen ICAO
891  // addresses. If it matches one, then declare the message as valid
892  mm->crcok = ICAOAddressWasRecentlySeen(mm->addr = mm->crc);
893  }
894 
895  // Fields for DF0, DF16
896  if (mm->msgtype == 0 || mm->msgtype == 16) {
897  if (msg[0] & 0x04) { // VS Bit
899  } else {
901  }
902  }
903 
904  // Fields for DF11, DF17
905  if (mm->msgtype == 11 || mm->msgtype == 17) {
906  if (mm->ca == 4) {
908  } else if (mm->ca == 5) {
910  }
911  }
912 
913  // Fields for DF5, DF21 = Gillham encoded Squawk
914  if (mm->msgtype == 5 || mm->msgtype == 21) {
915  int ID13Field = ((msg[2] << 8) | msg[3]) & 0x1FFF;
916  if (ID13Field) {
918  mm->modeA = decodeID13Field(ID13Field);
919  }
920  }
921 
922  // Fields for DF0, DF4, DF16, DF20 13 bit altitude
923  if (mm->msgtype == 0 || mm->msgtype == 4 ||
924  mm->msgtype == 16 || mm->msgtype == 20) {
925  int AC13Field = ((msg[2] << 8) | msg[3]) & 0x1FFF;
926  if (AC13Field) { // Only attempt to decode if a valid (non zero) altitude is present
928  mm->altitude = decodeAC13Field(AC13Field, &mm->unit);
929  }
930  }
931 
932  // Fields for DF4, DF5, DF20, DF21
933  if ((mm->msgtype == 4) || (mm->msgtype == 20) ||
934  (mm->msgtype == 5) || (mm->msgtype == 21)) {
936  mm->fs = msg[0] & 7; // Flight status for DF4,5,20,21
937  if (mm->fs <= 3) {
939  if (mm->fs & 1)
940  {mm->bFlags |= MODES_ACFLAGS_AOG;}
941  }
942  }
943 
944  // Fields for DF17, DF18_CF0, DF18_CF1, DF18_CF6 squitters
945  if ( (mm->msgtype == 17)
946  || ((mm->msgtype == 18) && ((mm->ca == 0) || (mm->ca == 1) || (mm->ca == 6)) )) {
947  int metype = mm->metype = msg[4] >> 3; // Extended squitter message type
948  int mesub = mm->mesub = msg[4] & 7; // Extended squitter message subtype
949 
950  // Decode the extended squitter message
951 
952  if (metype >= 1 && metype <= 4) { // Aircraft Identification and Category
953  uint32_t chars;
955 
956  chars = (msg[5] << 16) | (msg[6] << 8) | (msg[7]);
957  mm->flight[3] = ais_charset[chars & 0x3F]; chars = chars >> 6;
958  mm->flight[2] = ais_charset[chars & 0x3F]; chars = chars >> 6;
959  mm->flight[1] = ais_charset[chars & 0x3F]; chars = chars >> 6;
960  mm->flight[0] = ais_charset[chars & 0x3F];
961 
962  chars = (msg[8] << 16) | (msg[9] << 8) | (msg[10]);
963  mm->flight[7] = ais_charset[chars & 0x3F]; chars = chars >> 6;
964  mm->flight[6] = ais_charset[chars & 0x3F]; chars = chars >> 6;
965  mm->flight[5] = ais_charset[chars & 0x3F]; chars = chars >> 6;
966  mm->flight[4] = ais_charset[chars & 0x3F];
967 
968  mm->flight[8] = '\0';
969 
970  } else if (metype >= 5 && metype <= 18) { // Position Message
971  mm->raw_latitude = ((msg[6] & 3) << 15) | (msg[7] << 7) | (msg[8] >> 1);
972  mm->raw_longitude = ((msg[8] & 1) << 16) | (msg[9] << 8) | (msg[10]);
973  mm->bFlags |= (mm->msg[6] & 0x04) ? MODES_ACFLAGS_LLODD_VALID
975  if (metype >= 9) { // Airborne
976  int AC12Field = ((msg[5] << 4) | (msg[6] >> 4)) & 0x0FFF;
978  if (AC12Field) {// Only attempt to decode if a valid (non zero) altitude is present
980  mm->altitude = decodeAC12Field(AC12Field, &mm->unit);
981  }
982  } else { // Ground
983  int movement = ((msg[4] << 4) | (msg[5] >> 4)) & 0x007F;
985  if ((movement) && (movement < 125)) {
987  mm->velocity = decodeMovementField(movement);
988  }
989 
990  if (msg[5] & 0x08) {
992  mm->heading = ((((msg[5] << 4) | (msg[6] >> 4)) & 0x007F) * 45) >> 4;
993  }
994  }
995 
996  } else if (metype == 19) { // Airborne Velocity Message
997 
998  // Presumably airborne if we get an Airborne Velocity Message
1000 
1001  if ( (mesub >= 1) && (mesub <= 4) ) {
1002  int vert_rate = ((msg[8] & 0x07) << 6) | (msg[9] >> 2);
1003  if (vert_rate) {
1004  --vert_rate;
1005  if (msg[8] & 0x08)
1006  {vert_rate = 0 - vert_rate;}
1007  mm->vert_rate = vert_rate * 64;
1009  }
1010  }
1011 
1012  if ((mesub == 1) || (mesub == 2)) {
1013  int ew_raw = ((msg[5] & 0x03) << 8) | msg[6];
1014  int ew_vel = ew_raw - 1;
1015  int ns_raw = ((msg[7] & 0x7F) << 3) | (msg[8] >> 5);
1016  int ns_vel = ns_raw - 1;
1017 
1018  if (mesub == 2) { // If (supersonic) unit is 4 kts
1019  ns_vel = ns_vel << 2;
1020  ew_vel = ew_vel << 2;
1021  }
1022 
1023  if (ew_raw) { // Do East/West
1025  if (msg[5] & 0x04)
1026  {ew_vel = 0 - ew_vel;}
1027  mm->ew_velocity = ew_vel;
1028  }
1029 
1030  if (ns_raw) { // Do North/South
1032  if (msg[7] & 0x80)
1033  {ns_vel = 0 - ns_vel;}
1034  mm->ns_velocity = ns_vel;
1035  }
1036 
1037  if (ew_raw && ns_raw) {
1038  // Compute velocity and angle from the two speed components
1040  mm->velocity = (int) sqrt((ns_vel * ns_vel) + (ew_vel * ew_vel));
1041 
1042  if (mm->velocity) {
1043  mm->heading = (int) (atan2(ew_vel, ns_vel) * 180.0 / M_PI);
1044  // We don't want negative values but a 0-360 scale
1045  if (mm->heading < 0) mm->heading += 360;
1046  }
1047  }
1048 
1049  } else if (mesub == 3 || mesub == 4) {
1050  int airspeed = ((msg[7] & 0x7f) << 3) | (msg[8] >> 5);
1051  if (airspeed) {
1053  --airspeed;
1054  if (mesub == 4) // If (supersonic) unit is 4 kts
1055  {airspeed = airspeed << 2;}
1056  mm->velocity = airspeed;
1057  }
1058 
1059  if (msg[5] & 0x04) {
1061  mm->heading = ((((msg[5] & 0x03) << 8) | msg[6]) * 45) >> 7;
1062  }
1063  }
1064  }
1065  }
1066 
1067  // Fields for DF20, DF21 Comm-B
1068  if ((mm->msgtype == 20) || (mm->msgtype == 21)){
1069 
1070  if (msg[4] == 0x20) { // Aircraft Identification
1071  uint32_t chars;
1073 
1074  chars = (msg[5] << 16) | (msg[6] << 8) | (msg[7]);
1075  mm->flight[3] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1076  mm->flight[2] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1077  mm->flight[1] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1078  mm->flight[0] = ais_charset[chars & 0x3F];
1079 
1080  chars = (msg[8] << 16) | (msg[9] << 8) | (msg[10]);
1081  mm->flight[7] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1082  mm->flight[6] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1083  mm->flight[5] = ais_charset[chars & 0x3F]; chars = chars >> 6;
1084  mm->flight[4] = ais_charset[chars & 0x3F];
1085 
1086  mm->flight[8] = '\0';
1087  } else {
1088  }
1089  }
1090 }
1091 //
1092 //=========================================================================
1093 //
1094 // This function gets a decoded Mode S Message and prints it on the screen
1095 // in a human readable format.
1096 //
1098  int j;
1099  unsigned char * pTimeStamp;
1100 
1101  // Handle only addresses mode first.
1102  if (Modes.onlyaddr) {
1103  printf("%06x\n", mm->addr);
1104  return; // Enough for --onlyaddr mode
1105  }
1106 
1107  // output data for AERA
1108  if (Modes.aera) {
1109  aera(mm);
1110 // return; // Logfile of --aera and --aera-net modes contains not all information. Still write out raw data.
1111  }
1112 
1113  if (Modes.print_timestamp) {
1114  printf("%llu ", mstime()); // Print the current timestamp
1115  }
1116 
1117  // Show the raw message.
1118  if (Modes.mlat && mm->timestampMsg) {
1119  printf("@");
1120  pTimeStamp = (unsigned char *) &mm->timestampMsg;
1121  for (j=5; j>=0;j--) {
1122  printf("%02X",pTimeStamp[j]);
1123  }
1124  } else
1125  printf("*");
1126 
1127  for (j = 0; j < mm->msgbits/8; j++) printf("%02x", mm->msg[j]);
1128  printf(";\n");
1129 
1130  if (Modes.raw) {
1131  fflush(stdout); // Provide data to the reader ASAP
1132  return; // Enough for --raw and --raw-with-timestamp modes
1133  }
1134 
1135  if (mm->msgtype < 32)
1136  printf("CRC: %06x (%s)\n", (int)mm->crc, mm->crcok ? "ok" : "wrong");
1137 
1138  if (mm->correctedbits != 0)
1139  printf("No. of bit errors fixed: %d\n", mm->correctedbits);
1140 
1141  if (mm->msgtype == 0) { // DF 0
1142  printf("DF 0: Short Air-Air Surveillance.\n");
1143  printf(" VS : %s\n", (mm->msg[0] & 0x04) ? "Ground" : "Airborne");
1144  printf(" CC : %d\n", ((mm->msg[0] & 0x02) >> 1));
1145  printf(" SL : %d\n", ((mm->msg[1] & 0xE0) >> 5));
1146  printf(" Altitude : %d %s\n", mm->altitude,
1147  (mm->unit == MODES_UNIT_METERS) ? "meters" : "feet");
1148  printf(" ICAO Address : %06x\n", mm->addr);
1149 
1150  } else if (mm->msgtype == 4 || mm->msgtype == 20) {
1151  printf("DF %d: %s, Altitude Reply.\n", mm->msgtype,
1152  (mm->msgtype == 4) ? "Surveillance" : "Comm-B");
1153  printf(" Flight Status : %s\n", fs_str[mm->fs]);
1154  printf(" DR : %d\n", ((mm->msg[1] >> 3) & 0x1F));
1155  printf(" UM : %d\n", (((mm->msg[1] & 7) << 3) | (mm->msg[2] >> 5)));
1156  printf(" Altitude : %d %s\n", mm->altitude,
1157  (mm->unit == MODES_UNIT_METERS) ? "meters" : "feet");
1158  printf(" ICAO Address : %06x\n", mm->addr);
1159 
1160  if (mm->msgtype == 20) {
1161  printf(" Comm-B BDS : %x\n", mm->msg[4]);
1162 
1163  // Decode the extended squitter message
1164  if ( mm->msg[4] == 0x20) { // BDS 2,0 Aircraft identification
1165  printf(" BDS 2,0 Aircraft Identification : %s\n", mm->flight);
1166 /*
1167  } else if ( mm->msg[4] == 0x10) { // BDS 1,0 Datalink Capability report
1168  printf(" BDS 1,0 Datalink Capability report\n");
1169 
1170  } else if ( mm->msg[4] == 0x30) { // BDS 3,0 ACAS Active Resolution Advisory
1171  printf(" BDS 3,0 ACAS Active Resolution Advisory\n");
1172 
1173  } else if ((mm->msg[4] >> 3) == 28) { // BDS 6,1 Extended Squitter Emergecy/Priority Status
1174  printf(" BDS 6,1 Emergecy/Priority Status\n");
1175 
1176  } else if ((mm->msg[4] >> 3) == 29) { // BDS 6,2 Target State and Status
1177  printf(" BDS 6,2 Target State and Status\n");
1178 
1179  } else if ((mm->msg[4] >> 3) == 31) { // BDS 6,5 Extended Squitter Aircraft Operational Status
1180  printf(" BDS 6,5 Aircraft Operational Status\n");
1181 */
1182  }
1183  }
1184 
1185  } else if (mm->msgtype == 5 || mm->msgtype == 21) {
1186  printf("DF %d: %s, Identity Reply.\n", mm->msgtype,
1187  (mm->msgtype == 5) ? "Surveillance" : "Comm-B");
1188  printf(" Flight Status : %s\n", fs_str[mm->fs]);
1189  printf(" DR : %d\n", ((mm->msg[1] >> 3) & 0x1F));
1190  printf(" UM : %d\n", (((mm->msg[1] & 7) << 3) | (mm->msg[2] >> 5)));
1191  printf(" Squawk : %x\n", mm->modeA);
1192  printf(" ICAO Address : %06x\n", mm->addr);
1193 
1194  if (mm->msgtype == 21) {
1195  printf(" Comm-B BDS : %x\n", mm->msg[4]);
1196 
1197  // Decode the extended squitter message
1198  if ( mm->msg[4] == 0x20) { // BDS 2,0 Aircraft identification
1199  printf(" BDS 2,0 Aircraft Identification : %s\n", mm->flight);
1200 /*
1201  } else if ( mm->msg[4] == 0x10) { // BDS 1,0 Datalink Capability report
1202  printf(" BDS 1,0 Datalink Capability report\n");
1203 
1204  } else if ( mm->msg[4] == 0x30) { // BDS 3,0 ACAS Active Resolution Advisory
1205  printf(" BDS 3,0 ACAS Active Resolution Advisory\n");
1206 
1207  } else if ((mm->msg[4] >> 3) == 28) { // BDS 6,1 Extended Squitter Emergecy/Priority Status
1208  printf(" BDS 6,1 Emergecy/Priority Status\n");
1209 
1210  } else if ((mm->msg[4] >> 3) == 29) { // BDS 6,2 Target State and Status
1211  printf(" BDS 6,2 Target State and Status\n");
1212 
1213  } else if ((mm->msg[4] >> 3) == 31) { // BDS 6,5 Extended Squitter Aircraft Operational Status
1214  printf(" BDS 6,5 Aircraft Operational Status\n");
1215 */
1216  }
1217  }
1218 
1219  } else if (mm->msgtype == 11) { // DF 11
1220  printf("DF 11: All Call Reply.\n");
1221  printf(" Capability : %d (%s)\n", mm->ca, ca_str[mm->ca]);
1222  printf(" ICAO Address: %06x\n", mm->addr);
1223  if (mm->iid > 16)
1224  {printf(" IID : SI-%02d\n", mm->iid-16);}
1225  else
1226  {printf(" IID : II-%02d\n", mm->iid);}
1227 
1228  } else if (mm->msgtype == 16) { // DF 16
1229  printf("DF 16: Long Air to Air ACAS\n");
1230  printf(" VS : %s\n", (mm->msg[0] & 0x04) ? "Ground" : "Airborne");
1231  printf(" CC : %d\n", ((mm->msg[0] & 0x02) >> 1));
1232  printf(" SL : %d\n", ((mm->msg[1] & 0xE0) >> 5));
1233  printf(" Altitude : %d %s\n", mm->altitude,
1234  (mm->unit == MODES_UNIT_METERS) ? "meters" : "feet");
1235  printf(" ICAO Address : %06x\n", mm->addr);
1236 
1237  } else if (mm->msgtype == 17) { // DF 17
1238  printf("DF 17: ADS-B message.\n");
1239  printf(" Capability : %d (%s)\n", mm->ca, ca_str[mm->ca]);
1240  printf(" ICAO Address : %06x\n", mm->addr);
1241  printf(" Extended Squitter Type: %d\n", mm->metype);
1242  printf(" Extended Squitter Sub : %d\n", mm->mesub);
1243  printf(" Extended Squitter Name: %s\n", getMEDescription(mm->metype, mm->mesub));
1244 
1245  // Decode the extended squitter message
1246  if (mm->metype >= 1 && mm->metype <= 4) { // Aircraft identification
1247  printf(" Aircraft Type : %c%d\n", ('A' + 4 - mm->metype), mm->mesub);
1248  printf(" Identification : %s\n", mm->flight);
1249 
1250  //} else if (mm->metype >= 5 && mm->metype <= 8) { // Surface position
1251 
1252  } else if (mm->metype >= 9 && mm->metype <= 18) { // Airborne position Baro
1253  printf(" F flag : %s\n", (mm->msg[6] & 0x04) ? "odd" : "even");
1254  printf(" T flag : %s\n", (mm->msg[6] & 0x08) ? "UTC" : "non-UTC");
1255  printf(" Altitude : %d feet\n", mm->altitude);
1256  if (mm->bFlags & MODES_ACFLAGS_LATLON_VALID) {
1257  printf(" Latitude : %f\n", mm->fLat);
1258  printf(" Longitude: %f\n", mm->fLon);
1259  } else {
1260  printf(" Latitude : %d (not decoded)\n", mm->raw_latitude);
1261  printf(" Longitude: %d (not decoded)\n", mm->raw_longitude);
1262  }
1263 
1264  } else if (mm->metype == 19) { // Airborne Velocity
1265  if (mm->mesub == 1 || mm->mesub == 2) {
1266  printf(" EW status : %s\n", (mm->bFlags & MODES_ACFLAGS_EWSPEED_VALID) ? "Valid" : "Unavailable");
1267  printf(" EW velocity : %d\n", mm->ew_velocity);
1268  printf(" NS status : %s\n", (mm->bFlags & MODES_ACFLAGS_NSSPEED_VALID) ? "Valid" : "Unavailable");
1269  printf(" NS velocity : %d\n", mm->ns_velocity);
1270  printf(" Vertical status : %s\n", (mm->bFlags & MODES_ACFLAGS_VERTRATE_VALID) ? "Valid" : "Unavailable");
1271  printf(" Vertical rate src : %d\n", ((mm->msg[8] >> 4) & 1));
1272  printf(" Vertical rate : %d\n", mm->vert_rate);
1273 
1274  } else if (mm->mesub == 3 || mm->mesub == 4) {
1275  printf(" Heading status : %s\n", (mm->bFlags & MODES_ACFLAGS_HEADING_VALID) ? "Valid" : "Unavailable");
1276  printf(" Heading : %d\n", mm->heading);
1277  printf(" Airspeed status : %s\n", (mm->bFlags & MODES_ACFLAGS_SPEED_VALID) ? "Valid" : "Unavailable");
1278  printf(" Airspeed : %d\n", mm->velocity);
1279  printf(" Vertical status : %s\n", (mm->bFlags & MODES_ACFLAGS_VERTRATE_VALID) ? "Valid" : "Unavailable");
1280  printf(" Vertical rate src : %d\n", ((mm->msg[8] >> 4) & 1));
1281  printf(" Vertical rate : %d\n", mm->vert_rate);
1282 
1283  } else {
1284  printf(" Unrecognized ME subtype: %d subtype: %d\n", mm->metype, mm->mesub);
1285  }
1286 
1287  //} else if (mm->metype >= 20 && mm->metype <= 22) { // Airborne position GNSS
1288 
1289  } else {
1290  printf(" Unrecognized ME type: %d subtype: %d\n", mm->metype, mm->mesub);
1291  }
1292 
1293  } else if (mm->msgtype == 18) { // DF 18
1294  printf("DF 18: Extended Squitter.\n");
1295  printf(" Control Field : %d (%s)\n", mm->ca, cf_str[mm->ca]);
1296  if ((mm->ca == 0) || (mm->ca == 1) || (mm->ca == 6)) {
1297  if (mm->ca == 1) {
1298  printf(" Other Address : %06x\n", mm->addr);
1299  } else {
1300  printf(" ICAO Address : %06x\n", mm->addr);
1301  }
1302  printf(" Extended Squitter Type: %d\n", mm->metype);
1303  printf(" Extended Squitter Sub : %d\n", mm->mesub);
1304  printf(" Extended Squitter Name: %s\n", getMEDescription(mm->metype, mm->mesub));
1305 
1306  // Decode the extended squitter message
1307  if (mm->metype >= 1 && mm->metype <= 4) { // Aircraft identification
1308  printf(" Aircraft Type : %c%d\n", ('A' + 4 - mm->metype), mm->mesub);
1309  printf(" Identification : %s\n", mm->flight);
1310 
1311  //} else if (mm->metype >= 5 && mm->metype <= 8) { // Surface position
1312 
1313  } else if (mm->metype >= 9 && mm->metype <= 18) { // Airborne position Baro
1314  printf(" F flag : %s\n", (mm->msg[6] & 0x04) ? "odd" : "even");
1315  printf(" T flag : %s\n", (mm->msg[6] & 0x08) ? "UTC" : "non-UTC");
1316  printf(" Altitude : %d feet\n", mm->altitude);
1317  if (mm->bFlags & MODES_ACFLAGS_LATLON_VALID) {
1318  printf(" Latitude : %f\n", mm->fLat);
1319  printf(" Longitude: %f\n", mm->fLon);
1320  } else {
1321  printf(" Latitude : %d (not decoded)\n", mm->raw_latitude);
1322  printf(" Longitude: %d (not decoded)\n", mm->raw_longitude);
1323  }
1324 
1325  } else if (mm->metype == 19) { // Airborne Velocity
1326  if (mm->mesub == 1 || mm->mesub == 2) {
1327  printf(" EW status : %s\n", (mm->bFlags & MODES_ACFLAGS_EWSPEED_VALID) ? "Valid" : "Unavailable");
1328  printf(" EW velocity : %d\n", mm->ew_velocity);
1329  printf(" NS status : %s\n", (mm->bFlags & MODES_ACFLAGS_NSSPEED_VALID) ? "Valid" : "Unavailable");
1330  printf(" NS velocity : %d\n", mm->ns_velocity);
1331  printf(" Vertical status : %s\n", (mm->bFlags & MODES_ACFLAGS_VERTRATE_VALID) ? "Valid" : "Unavailable");
1332  printf(" Vertical rate src : %d\n", ((mm->msg[8] >> 4) & 1));
1333  printf(" Vertical rate : %d\n", mm->vert_rate);
1334 
1335  } else if (mm->mesub == 3 || mm->mesub == 4) {
1336  printf(" Heading status : %s\n", (mm->bFlags & MODES_ACFLAGS_HEADING_VALID) ? "Valid" : "Unavailable");
1337  printf(" Heading : %d\n", mm->heading);
1338  printf(" Airspeed status : %s\n", (mm->bFlags & MODES_ACFLAGS_SPEED_VALID) ? "Valid" : "Unavailable");
1339  printf(" Airspeed : %d\n", mm->velocity);
1340  printf(" Vertical status : %s\n", (mm->bFlags & MODES_ACFLAGS_VERTRATE_VALID) ? "Valid" : "Unavailable");
1341  printf(" Vertical rate src : %d\n", ((mm->msg[8] >> 4) & 1));
1342  printf(" Vertical rate : %d\n", mm->vert_rate);
1343 
1344  } else {
1345  printf(" Unrecognized ME subtype: %d subtype: %d\n", mm->metype, mm->mesub);
1346  }
1347 
1348  //} else if (mm->metype >= 20 && mm->metype <= 22) { // Airborne position GNSS
1349 
1350  } else {
1351  printf(" Unrecognized ME type: %d subtype: %d\n", mm->metype, mm->mesub);
1352  }
1353  }
1354 
1355  } else if (mm->msgtype == 19) { // DF 19
1356  printf("DF 19: Military Extended Squitter.\n");
1357 
1358  } else if (mm->msgtype == 22) { // DF 22
1359  printf("DF 22: Military Use.\n");
1360 
1361  } else if (mm->msgtype == 24) { // DF 24
1362  printf("DF 24: Comm D Extended Length Message.\n");
1363 
1364  } else if (mm->msgtype == 32) { // DF 32 is special code we use for Mode A/C
1365  printf("SSR : Mode A/C Reply.\n");
1366  if (mm->fs & 0x0080) {
1367  printf(" Mode A : %04x IDENT\n", mm->modeA);
1368  } else {
1369  printf(" Mode A : %04x\n", mm->modeA);
1371  {printf(" Mode C : %d feet\n", mm->altitude);}
1372  }
1373 
1374  } else {
1375  printf("DF %d: Unknown DF Format.\n", mm->msgtype);
1376  }
1377 
1378  printf("\n");
1379 }
1380 //
1381 //=========================================================================
1382 //
1383 // Turn I/Q samples pointed by Modes.data into the magnitude vector
1384 // pointed by Modes.magnitude.
1385 //
1387  uint16_t *m = &Modes.magnitude[MODES_PREAMBLE_SAMPLES+MODES_LONG_MSG_SAMPLES];
1388  uint16_t *p = Modes.data;
1389  uint32_t j;
1390 
1392 
1393  // Compute the magnitudo vector. It's just SQRT(I^2 + Q^2), but
1394  // we rescale to the 0-255 range to exploit the full resolution.
1395  for (j = 0; j < MODES_ASYNC_BUF_SAMPLES; j ++) {
1396  *m++ = Modes.maglut[*p++];
1397  }
1398 }
1399 //
1400 //=========================================================================
1401 //
1402 // Return -1 if the message is out of fase left-side
1403 // Return 1 if the message is out of fase right-size
1404 // Return 0 if the message is not particularly out of phase.
1405 //
1406 // Note: this function will access pPreamble[-1], so the caller should make sure to
1407 // call it only if we are not at the start of the current buffer
1408 //
1409 int detectOutOfPhase(uint16_t *pPreamble) {
1410  if (pPreamble[ 3] > pPreamble[2]/3) return 1;
1411  if (pPreamble[10] > pPreamble[9]/3) return 1;
1412  if (pPreamble[ 6] > pPreamble[7]/3) return -1;
1413  if (pPreamble[-1] > pPreamble[1]/3) return -1;
1414  return 0;
1415 }
1416 //
1417 //=========================================================================
1418 //
1419 // This function does not really correct the phase of the message, it just
1420 // applies a transformation to the first sample representing a given bit:
1421 //
1422 // If the previous bit was one, we amplify it a bit.
1423 // If the previous bit was zero, we decrease it a bit.
1424 //
1425 // This simple transformation makes the message a bit more likely to be
1426 // correctly decoded for out of phase messages:
1427 //
1428 // When messages are out of phase there is more uncertainty in
1429 // sequences of the same bit multiple times, since 11111 will be
1430 // transmitted as continuously altering magnitude (high, low, high, low...)
1431 //
1432 // However because the message is out of phase some part of the high
1433 // is mixed in the low part, so that it is hard to distinguish if it is
1434 // a zero or a one.
1435 //
1436 // However when the message is out of phase passing from 0 to 1 or from
1437 // 1 to 0 happens in a very recognizable way, for instance in the 0 -> 1
1438 // transition, magnitude goes low, high, high, low, and one of of the
1439 // two middle samples the high will be *very* high as part of the previous
1440 // or next high signal will be mixed there.
1441 //
1442 // Applying our simple transformation we make more likely if the current
1443 // bit is a zero, to detect another zero. Symmetrically if it is a one
1444 // it will be more likely to detect a one because of the transformation.
1445 // In this way similar levels will be interpreted more likely in the
1446 // correct way.
1447 //
1448 void applyPhaseCorrection(uint16_t *pPayload) {
1449  int j;
1450 
1451  for (j = 0; j < MODES_LONG_MSG_SAMPLES; j += 2, pPayload += 2) {
1452  if (pPayload[0] > pPayload[1]) { // One
1453  pPayload[2] = (pPayload[2] * 5) / 4;
1454  } else { // Zero
1455  pPayload[2] = (pPayload[2] * 4) / 5;
1456  }
1457  }
1458 }
1459 //
1460 //=========================================================================
1461 //
1462 // Detect a Mode S messages inside the magnitude buffer pointed by 'm' and of
1463 // size 'mlen' bytes. Every detected Mode S message is convert it into a
1464 // stream of bits and passed to the function to display it.
1465 //
1466 void detectModeS(uint16_t *m, uint32_t mlen) {
1467  struct modesMessage mm;
1468  unsigned char msg[MODES_LONG_MSG_BYTES], *pMsg;
1469  uint16_t aux[MODES_LONG_MSG_SAMPLES];
1470  uint32_t j;
1471  int use_correction = 0;
1472 
1473  memset(&mm, 0, sizeof(mm));
1474 
1475  // The Mode S preamble is made of impulses of 0.5 microseconds at
1476  // the following time offsets:
1477  //
1478  // 0 - 0.5 usec: first impulse.
1479  // 1.0 - 1.5 usec: second impulse.
1480  // 3.5 - 4 usec: third impulse.
1481  // 4.5 - 5 usec: last impulse.
1482  //
1483  // Since we are sampling at 2 Mhz every sample in our magnitude vector
1484  // is 0.5 usec, so the preamble will look like this, assuming there is
1485  // an impulse at offset 0 in the array:
1486  //
1487  // 0 -----------------
1488  // 1 -
1489  // 2 ------------------
1490  // 3 --
1491  // 4 -
1492  // 5 --
1493  // 6 -
1494  // 7 ------------------
1495  // 8 --
1496  // 9 -------------------
1497  //
1498  for (j = 0; j < mlen; j++) {
1499  int high, i, errors, errors56, errorsTy;
1500  uint16_t *pPreamble, *pPayload, *pPtr;
1501  uint8_t theByte, theErrs;
1502  int msglen, scanlen, sigStrength;
1503 
1504  pPreamble = &m[j];
1505  pPayload = &m[j+MODES_PREAMBLE_SAMPLES];
1506 
1507  // Rather than clear the whole mm structure, just clear the parts which are required. The clear
1508  // is required for every bit of the input stream, and we don't want to be memset-ing the whole
1509  // modesMessage structure two million times per second if we don't have to..
1510  mm.bFlags =
1511  mm.crcok =
1512  mm.correctedbits = 0;
1513 
1514  if (!use_correction) // This is not a re-try with phase correction
1515  { // so try to find a new preamble
1516 
1517  if (Modes.mode_ac)
1518  {
1519  int ModeA = detectModeA(pPreamble, &mm);
1520 
1521  if (ModeA) // We have found a valid ModeA/C in the data
1522  {
1523  mm.timestampMsg = Modes.timestampBlk + ((j+1) * 6);
1524 
1525  // Decode the received message
1526  decodeModeAMessage(&mm, ModeA);
1527 
1528  // Pass data to the next layer
1529  useModesMessage(&mm);
1530 
1531  j += MODEAC_MSG_SAMPLES;
1532  Modes.stat_ModeAC++;
1533  continue;
1534  }
1535  }
1536 
1537  // First check of relations between the first 10 samples
1538  // representing a valid preamble. We don't even investigate further
1539  // if this simple test is not passed
1540  if (!(pPreamble[0] > pPreamble[1] &&
1541  pPreamble[1] < pPreamble[2] &&
1542  pPreamble[2] > pPreamble[3] &&
1543  pPreamble[3] < pPreamble[0] &&
1544  pPreamble[4] < pPreamble[0] &&
1545  pPreamble[5] < pPreamble[0] &&
1546  pPreamble[6] < pPreamble[0] &&
1547  pPreamble[7] > pPreamble[8] &&
1548  pPreamble[8] < pPreamble[9] &&
1549  pPreamble[9] > pPreamble[6]))
1550  {
1551  if (Modes.debug & MODES_DEBUG_NOPREAMBLE &&
1552  *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL)
1553  dumpRawMessage("Unexpected ratio among first 10 samples", msg, m, j);
1554  continue;
1555  }
1556 
1557  // The samples between the two spikes must be < than the average
1558  // of the high spikes level. We don't test bits too near to
1559  // the high levels as signals can be out of phase so part of the
1560  // energy can be in the near samples
1561  high = (pPreamble[0] + pPreamble[2] + pPreamble[7] + pPreamble[9]) / 6;
1562  if (pPreamble[4] >= high ||
1563  pPreamble[5] >= high)
1564  {
1565  if (Modes.debug & MODES_DEBUG_NOPREAMBLE &&
1566  *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL)
1567  dumpRawMessage("Too high level in samples between 3 and 6", msg, m, j);
1568  continue;
1569  }
1570 
1571  // Similarly samples in the range 11-14 must be low, as it is the
1572  // space between the preamble and real data. Again we don't test
1573  // bits too near to high levels, see above
1574  if (pPreamble[11] >= high ||
1575  pPreamble[12] >= high ||
1576  pPreamble[13] >= high ||
1577  pPreamble[14] >= high)
1578  {
1579  if (Modes.debug & MODES_DEBUG_NOPREAMBLE &&
1580  *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL)
1581  dumpRawMessage("Too high level in samples between 10 and 15", msg, m, j);
1582  continue;
1583  }
1584  Modes.stat_valid_preamble++;
1585  }
1586 
1587  else {
1588  // If the previous attempt with this message failed, retry using
1589  // magnitude correction
1590  // Make a copy of the Payload, and phase correct the copy
1591  memcpy(aux, pPayload, sizeof(aux));
1592  applyPhaseCorrection(aux);
1593  Modes.stat_out_of_phase++;
1594  pPayload = aux;
1595  // TODO ... apply other kind of corrections
1596  }
1597 
1598  // Decode all the next 112 bits, regardless of the actual message
1599  // size. We'll check the actual message type later
1600  pMsg = &msg[0];
1601  pPtr = pPayload;
1602  theByte = 0;
1603  theErrs = 0; errorsTy = 0;
1604  errors = 0; errors56 = 0;
1605 
1606  // We should have 4 'bits' of 0/1 and 1/0 samples in the preamble,
1607  // so include these in the signal strength
1608  sigStrength = (pPreamble[0]-pPreamble[1])
1609  + (pPreamble[2]-pPreamble[3])
1610  + (pPreamble[7]-pPreamble[6])
1611  + (pPreamble[9]-pPreamble[8]);
1612 
1613  msglen = scanlen = MODES_LONG_MSG_BITS;
1614  for (i = 0; i < scanlen; i++) {
1615  uint32_t a = *pPtr++;
1616  uint32_t b = *pPtr++;
1617 
1618  if (a > b)
1619  {theByte |= 1; if (i < 56) {sigStrength += (a-b);}}
1620  else if (a < b)
1621  {/*theByte |= 0;*/ if (i < 56) {sigStrength += (b-a);}}
1622  else if (i >= MODES_SHORT_MSG_BITS) //(a == b), and we're in the long part of a frame
1623  {errors++; /*theByte |= 0;*/}
1624  else if (i >= 5) //(a == b), and we're in the short part of a frame
1625  {scanlen = MODES_LONG_MSG_BITS; errors56 = ++errors;/*theByte |= 0;*/}
1626  else if (i) //(a == b), and we're in the message type part of a frame
1627  {errorsTy = errors56 = ++errors; theErrs |= 1; /*theByte |= 0;*/}
1628  else //(a == b), and we're in the first bit of the message type part of a frame
1629  {errorsTy = errors56 = ++errors; theErrs |= 1; theByte |= 1;}
1630 
1631  if ((i & 7) == 7)
1632  {*pMsg++ = theByte;}
1633  else if (i == 4) {
1634  msglen = modesMessageLenByType(theByte);
1635  if (errors == 0)
1636  {scanlen = msglen;}
1637  }
1638 
1639  theByte = theByte << 1;
1640  if (i < 7)
1641  {theErrs = theErrs << 1;}
1642 
1643  // If we've exceeded the permissible number of encoding errors, abandon ship now
1644  if (errors > MODES_MSG_ENCODER_ERRS) {
1645 
1646  if (i < MODES_SHORT_MSG_BITS) {
1647  msglen = 0;
1648 
1649  } else if ((errorsTy == 1) && (theErrs == 0x80)) {
1650  // If we only saw one error in the first bit of the byte of the frame, then it's possible
1651  // we guessed wrongly about the value of the bit. We may be able to correct it by guessing
1652  // the other way.
1653  //
1654  // We guessed a '1' at bit 7, which is the DF length bit == 112 Bits.
1655  // Inverting bit 7 will change the message type from a long to a short.
1656  // Invert the bit, cross your fingers and carry on.
1657  msglen = MODES_SHORT_MSG_BITS;
1658  msg[0] ^= theErrs; errorsTy = 0;
1659  errors = errors56; // revert to the number of errors prior to bit 56
1660  Modes.stat_DF_Len_Corrected++;
1661 
1662  } else if (i < MODES_LONG_MSG_BITS) {
1663  msglen = MODES_SHORT_MSG_BITS;
1664  errors = errors56;
1665 
1666  } else {
1667  msglen = MODES_LONG_MSG_BITS;
1668  }
1669 
1670  break;
1671  }
1672  }
1673 
1674  // Ensure msglen is consistent with the DF type
1675  i = modesMessageLenByType(msg[0] >> 3);
1676  if (msglen > i) {msglen = i;}
1677  else if (msglen < i) {msglen = 0;}
1678 
1679  //
1680  // If we guessed at any of the bits in the DF type field, then look to see if our guess was sensible.
1681  // Do this by looking to see if the original guess results in the DF type being one of the ICAO defined
1682  // message types. If it isn't then toggle the guessed bit and see if this new value is ICAO defined.
1683  // if the new value is ICAO defined, then update it in our message.
1684  if ((msglen) && (errorsTy == 1) && (theErrs & 0x78)) {
1685  // We guessed at one (and only one) of the message type bits. See if our guess is "likely"
1686  // to be correct by comparing the DF against a list of known good DF's
1687  int thisDF = ((theByte = msg[0]) >> 3) & 0x1f;
1688  uint32_t validDFbits = 0x017F0831; // One bit per 32 possible DF's. Set bits 0,4,5,11,16.17.18.19,20,21,22,24
1689  uint32_t thisDFbit = (1 << thisDF);
1690  if (0 == (validDFbits & thisDFbit)) {
1691  // The current DF is not ICAO defined, so is probably an errors.
1692  // Toggle the bit we guessed at and see if the resultant DF is more likely
1693  theByte ^= theErrs;
1694  thisDF = (theByte >> 3) & 0x1f;
1695  thisDFbit = (1 << thisDF);
1696  // if this DF any more likely?
1697  if (validDFbits & thisDFbit) {
1698  // Yep, more likely, so update the main message
1699  msg[0] = theByte;
1700  Modes.stat_DF_Type_Corrected++;
1701  errors--; // decrease the error count so we attempt to use the modified DF.
1702  }
1703  }
1704  }
1705 
1706  // We measured signal strength over the first 56 bits. Don't forget to add 4
1707  // for the preamble samples, so round up and divide by 60.
1708  sigStrength = (sigStrength + 29) / 60;
1709 
1710  // When we reach this point, if error is small, and the signal strength is large enough
1711  // we may have a Mode S message on our hands. It may still be broken and the CRC may not
1712  // be correct, but this can be handled by the next layer.
1713  if ( (msglen)
1714  && (sigStrength > MODES_MSG_SQUELCH_LEVEL)
1715  && (errors <= MODES_MSG_ENCODER_ERRS) ) {
1716 
1717  // Set initial mm structure details
1718  mm.timestampMsg = Modes.timestampBlk + (j*6);
1719  sigStrength = (sigStrength + 0x7F) >> 8;
1720  mm.signalLevel = ((sigStrength < 255) ? sigStrength : 255);
1721  mm.phase_corrected = use_correction;
1722 
1723  // Decode the received message
1724  decodeModesMessage(&mm, msg);
1725 
1726  // Update statistics
1727  if (Modes.stats) {
1728  if (mm.crcok || use_correction || mm.correctedbits) {
1729 
1730  if (use_correction) {
1731  switch (errors) {
1732  case 0: {Modes.stat_ph_demodulated0++; break;}
1733  case 1: {Modes.stat_ph_demodulated1++; break;}
1734  case 2: {Modes.stat_ph_demodulated2++; break;}
1735  default:{Modes.stat_ph_demodulated3++; break;}
1736  }
1737  } else {
1738  switch (errors) {
1739  case 0: {Modes.stat_demodulated0++; break;}
1740  case 1: {Modes.stat_demodulated1++; break;}
1741  case 2: {Modes.stat_demodulated2++; break;}
1742  default:{Modes.stat_demodulated3++; break;}
1743  }
1744  }
1745 
1746  if (mm.correctedbits == 0) {
1747  if (use_correction) {
1748  if (mm.crcok) {Modes.stat_ph_goodcrc++;}
1749  else {Modes.stat_ph_badcrc++;}
1750  } else {
1751  if (mm.crcok) {Modes.stat_goodcrc++;}
1752  else {Modes.stat_badcrc++;}
1753  }
1754 
1755  } else if (use_correction) {
1756  Modes.stat_ph_badcrc++;
1757  Modes.stat_ph_fixed++;
1758  if ( (mm.correctedbits)
1759  && (mm.correctedbits <= MODES_MAX_BITERRORS) ) {
1760  Modes.stat_ph_bit_fix[mm.correctedbits-1] += 1;
1761  }
1762 
1763  } else {
1764  Modes.stat_badcrc++;
1765  Modes.stat_fixed++;
1766  if ( (mm.correctedbits)
1767  && (mm.correctedbits <= MODES_MAX_BITERRORS) ) {
1768  Modes.stat_bit_fix[mm.correctedbits-1] += 1;
1769  }
1770  }
1771  }
1772  }
1773 
1774  // Output debug mode info if needed
1775  if (use_correction) {
1776  if (Modes.debug & MODES_DEBUG_DEMOD)
1777  dumpRawMessage("Demodulated with 0 errors", msg, m, j);
1778  else if (Modes.debug & MODES_DEBUG_BADCRC &&
1779  mm.msgtype == 17 &&
1780  (!mm.crcok || mm.correctedbits != 0))
1781  dumpRawMessage("Decoded with bad CRC", msg, m, j);
1782  else if (Modes.debug & MODES_DEBUG_GOODCRC && mm.crcok &&
1783  mm.correctedbits == 0)
1784  dumpRawMessage("Decoded with good CRC", msg, m, j);
1785  }
1786 
1787  // Skip this message if we are sure it's fine
1788  if (mm.crcok) {
1789  j += (MODES_PREAMBLE_US+msglen)*2;
1790  }
1791 
1792  // Pass data to the next layer
1793  useModesMessage(&mm);
1794 
1795  } else {
1796  if (Modes.debug & MODES_DEBUG_DEMODERR && use_correction) {
1797  printf("The following message has %d demod errors\n", errors);
1798  dumpRawMessage("Demodulated with errors", msg, m, j);
1799  }
1800  }
1801 
1802  // Retry with phase correction if enabled, necessary and possible.
1803  if (Modes.phase_enhance && !mm.crcok && !mm.correctedbits && !use_correction && j && detectOutOfPhase(pPreamble)) {
1804  use_correction = 1; j--;
1805  } else {
1806  use_correction = 0;
1807  }
1808  }
1809 
1810  //Send any remaining partial raw buffers now
1811  if (Modes.rawOutUsed || Modes.beastOutUsed)
1812  {
1813  Modes.net_output_raw_rate_count++;
1814  if (Modes.net_output_raw_rate_count > Modes.net_output_raw_rate)
1815  {
1816  if (Modes.rawOutUsed) {
1817  modesSendAllClients(Modes.ros, Modes.rawOut, Modes.rawOutUsed);
1818  Modes.rawOutUsed = 0;
1819  }
1820  if (Modes.beastOutUsed) {
1821  modesSendAllClients(Modes.bos, Modes.beastOut, Modes.beastOutUsed);
1822  Modes.beastOutUsed = 0;
1823  }
1824  Modes.net_output_raw_rate_count = 0;
1825  }
1826  }
1827 }
1828 //
1829 //=========================================================================
1830 //
1831 // When a new message is available, because it was decoded from the RTL device,
1832 // file, or received in the TCP input port, or any other way we can receive a
1833 // decoded message, we call this function in order to use the message.
1834 //
1835 // Basically this function passes a raw message to the upper layers for further
1836 // processing and visualization
1837 //
1839  if ((Modes.check_crc == 0) || (mm->crcok) || (mm->correctedbits)) { // not checking, ok or fixed
1840 
1841  // Always track aircraft
1843 
1844  // In non-interactive non-quiet mode, display messages on standard output
1845  if (!Modes.interactive && !Modes.quiet) {
1846  displayModesMessage(mm);
1847  }
1848 
1849  // Feed output clients
1850  if (Modes.net) {modesQueueOutput(mm);}
1851  }
1852 }
1853 //
1854 //=========================================================================
1855 //
1856 // Always positive MOD operation, used for CPR decoding.
1857 //
1858 int cprModFunction(int a, int b) {
1859  int res = a % b;
1860  if (res < 0) res += b;
1861  return res;
1862 }
1863 //
1864 //=========================================================================
1865 //
1866 // The NL function uses the precomputed table from 1090-WP-9-14
1867 //
1868 int cprNLFunction(double lat) {
1869  if (lat < 0) lat = -lat; // Table is simmetric about the equator
1870  if (lat < 10.47047130) return 59;
1871  if (lat < 14.82817437) return 58;
1872  if (lat < 18.18626357) return 57;
1873  if (lat < 21.02939493) return 56;
1874  if (lat < 23.54504487) return 55;
1875  if (lat < 25.82924707) return 54;
1876  if (lat < 27.93898710) return 53;
1877  if (lat < 29.91135686) return 52;
1878  if (lat < 31.77209708) return 51;
1879  if (lat < 33.53993436) return 50;
1880  if (lat < 35.22899598) return 49;
1881  if (lat < 36.85025108) return 48;
1882  if (lat < 38.41241892) return 47;
1883  if (lat < 39.92256684) return 46;
1884  if (lat < 41.38651832) return 45;
1885  if (lat < 42.80914012) return 44;
1886  if (lat < 44.19454951) return 43;
1887  if (lat < 45.54626723) return 42;
1888  if (lat < 46.86733252) return 41;
1889  if (lat < 48.16039128) return 40;
1890  if (lat < 49.42776439) return 39;
1891  if (lat < 50.67150166) return 38;
1892  if (lat < 51.89342469) return 37;
1893  if (lat < 53.09516153) return 36;
1894  if (lat < 54.27817472) return 35;
1895  if (lat < 55.44378444) return 34;
1896  if (lat < 56.59318756) return 33;
1897  if (lat < 57.72747354) return 32;
1898  if (lat < 58.84763776) return 31;
1899  if (lat < 59.95459277) return 30;
1900  if (lat < 61.04917774) return 29;
1901  if (lat < 62.13216659) return 28;
1902  if (lat < 63.20427479) return 27;
1903  if (lat < 64.26616523) return 26;
1904  if (lat < 65.31845310) return 25;
1905  if (lat < 66.36171008) return 24;
1906  if (lat < 67.39646774) return 23;
1907  if (lat < 68.42322022) return 22;
1908  if (lat < 69.44242631) return 21;
1909  if (lat < 70.45451075) return 20;
1910  if (lat < 71.45986473) return 19;
1911  if (lat < 72.45884545) return 18;
1912  if (lat < 73.45177442) return 17;
1913  if (lat < 74.43893416) return 16;
1914  if (lat < 75.42056257) return 15;
1915  if (lat < 76.39684391) return 14;
1916  if (lat < 77.36789461) return 13;
1917  if (lat < 78.33374083) return 12;
1918  if (lat < 79.29428225) return 11;
1919  if (lat < 80.24923213) return 10;
1920  if (lat < 81.19801349) return 9;
1921  if (lat < 82.13956981) return 8;
1922  if (lat < 83.07199445) return 7;
1923  if (lat < 83.99173563) return 6;
1924  if (lat < 84.89166191) return 5;
1925  if (lat < 85.75541621) return 4;
1926  if (lat < 86.53536998) return 3;
1927  if (lat < 87.00000000) return 2;
1928  else return 1;
1929 }
1930 //
1931 //=========================================================================
1932 //
1933 int cprNFunction(double lat, int fflag) {
1934  int nl = cprNLFunction(lat) - (fflag ? 1 : 0);
1935  if (nl < 1) nl = 1;
1936  return nl;
1937 }
1938 //
1939 //=========================================================================
1940 //
1941 double cprDlonFunction(double lat, int fflag, int surface) {
1942  return (surface ? 90.0 : 360.0) / cprNFunction(lat, fflag);
1943 }
1944 //
1945 //=========================================================================
1946 //
1947 // This algorithm comes from:
1948 // http://www.lll.lu/~edward/edward/adsb/DecodingADSBposition.html.
1949 //
1950 // A few remarks:
1951 // 1) 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits.
1952 // 2) We assume that we always received the odd packet as last packet for
1953 // simplicity. This may provide a position that is less fresh of a few
1954 // seconds.
1955 //
1956 void decodeCPR(struct aircraft *a, int fflag, int surface) {
1957  double AirDlat0 = (surface ? 90.0 : 360.0) / 60.0;
1958  double AirDlat1 = (surface ? 90.0 : 360.0) / 59.0;
1959  double lat0 = a->even_cprlat;
1960  double lat1 = a->odd_cprlat;
1961  double lon0 = a->even_cprlon;
1962  double lon1 = a->odd_cprlon;
1963 
1964  // Compute the Latitude Index "j"
1965  int j = (int) floor(((59*lat0 - 60*lat1) / 131072) + 0.5);
1966  double rlat0 = AirDlat0 * (cprModFunction(j,60) + lat0 / 131072);
1967  double rlat1 = AirDlat1 * (cprModFunction(j,59) + lat1 / 131072);
1968 
1969  if (surface) {
1970  // If we're on the ground, make sure we have our receiver base station Lat/Lon
1971  if (0 == (Modes.bUserFlags & MODES_USER_LATLON_VALID))
1972  {return;}
1973  rlat0 += floor(Modes.fUserLat / 90.0) * 90.0; // Move from 1st quadrant to our quadrant
1974  rlat1 += floor(Modes.fUserLat / 90.0) * 90.0;
1975  } else {
1976  if (rlat0 >= 270) rlat0 -= 360;
1977  if (rlat1 >= 270) rlat1 -= 360;
1978  }
1979 
1980  // Check that both are in the same latitude zone, or abort.
1981  if (cprNLFunction(rlat0) != cprNLFunction(rlat1)) return;
1982 
1983  // Compute ni and the Longitude Index "m"
1984  if (fflag) { // Use odd packet.
1985  int ni = cprNFunction(rlat1,1);
1986  int m = (int) floor((((lon0 * (cprNLFunction(rlat1)-1)) -
1987  (lon1 * cprNLFunction(rlat1))) / 131072.0) + 0.5);
1988  a->lon = cprDlonFunction(rlat1, 1, surface) * (cprModFunction(m, ni)+lon1/131072);
1989  a->lat = rlat1;
1990  } else { // Use even packet.
1991  int ni = cprNFunction(rlat0,0);
1992  int m = (int) floor((((lon0 * (cprNLFunction(rlat0)-1)) -
1993  (lon1 * cprNLFunction(rlat0))) / 131072) + 0.5);
1994  a->lon = cprDlonFunction(rlat0, 0, surface) * (cprModFunction(m, ni)+lon0/131072);
1995  a->lat = rlat0;
1996  }
1997 
1998  if (surface) {
1999  a->lon += floor(Modes.fUserLon / 90.0) * 90.0; // Move from 1st quadrant to our quadrant
2000  } else if (a->lon > 180) {
2001  a->lon -= 360;
2002  }
2003 
2004  a->seenLatLon = a->seen;
2005  a->timestampLatLon = a->timestamp;
2007 }
2008 //
2009 //=========================================================================
2010 //
2011 // This algorithm comes from:
2012 // 1090-WP29-07-Draft_CPR101 (which also defines decodeCPR() )
2013 //
2014 // There is an error in this document related to CPR relative decode.
2015 // Should use trunc() rather than the floor() function in Eq 38 and related for deltaZI.
2016 // floor() returns integer less than argument
2017 // trunc() returns integer closer to zero than argument.
2018 // Note: text of document describes trunc() functionality for deltaZI calculation
2019 // but the formulae use floor().
2020 //
2021 int decodeCPRrelative(struct aircraft *a, int fflag, int surface) {
2022  double AirDlat;
2023  double AirDlon;
2024  double lat;
2025  double lon;
2026  double lonr, latr;
2027  double rlon, rlat;
2028  int j,m;
2029 
2030  if (a->bFlags & MODES_ACFLAGS_LATLON_REL_OK) { // Ok to try aircraft relative first
2031  latr = a->lat;
2032  lonr = a->lon;
2033  } else if (Modes.bUserFlags & MODES_USER_LATLON_VALID) { // Try ground station relative next
2034  latr = Modes.fUserLat;
2035  lonr = Modes.fUserLon;
2036  } else {
2037  return (-1); // Exit with error - can't do relative if we don't have ref.
2038  }
2039 
2040  if (fflag) { // odd
2041  AirDlat = (surface ? 90.0 : 360.0) / 59.0;
2042  lat = a->odd_cprlat;
2043  lon = a->odd_cprlon;
2044  } else { // even
2045  AirDlat = (surface ? 90.0 : 360.0) / 60.0;
2046  lat = a->even_cprlat;
2047  lon = a->even_cprlon;
2048  }
2049 
2050  // Compute the Latitude Index "j"
2051  j = (int) (floor(latr/AirDlat) +
2052  trunc(0.5 + cprModFunction((int)latr, (int)AirDlat)/AirDlat - lat/131072));
2053  rlat = AirDlat * (j + lat/131072);
2054  if (rlat >= 270) rlat -= 360;
2055 
2056  // Check to see that answer is reasonable - ie no more than 1/2 cell away
2057  if (fabs(rlat - a->lat) > (AirDlat/2)) {
2058  a->bFlags &= ~MODES_ACFLAGS_LATLON_REL_OK; // This will cause a quick exit next time if no global has been done
2059  return (-1); // Time to give up - Latitude error
2060  }
2061 
2062  // Compute the Longitude Index "m"
2063  AirDlon = cprDlonFunction(rlat, fflag, surface);
2064  m = (int) (floor(lonr/AirDlon) +
2065  trunc(0.5 + cprModFunction((int)lonr, (int)AirDlon)/AirDlon - lon/131072));
2066  rlon = AirDlon * (m + lon/131072);
2067  if (rlon > 180) rlon -= 360;
2068 
2069  // Check to see that answer is reasonable - ie no more than 1/2 cell away
2070  if (fabs(rlon - a->lon) > (AirDlon/2)) {
2071  a->bFlags &= ~MODES_ACFLAGS_LATLON_REL_OK; // This will cause a quick exit next time if no global has been done
2072  return (-1); // Time to give up - Longitude error
2073  }
2074 
2075  a->lat = rlat;
2076  a->lon = rlon;
2077 
2078  a->seenLatLon = a->seen;
2079  a->timestampLatLon = a->timestamp;
2081  return (0);
2082 }
2083 //
2084 // ===================== Mode S detection and decoding ===================
2085 //
int bits
Definition: mode_s.c:210
int ModeAToModeC(unsigned int ModeA)
Definition: mode_ac.c:319
#define MODES_ACFLAGS_NSEWSPD_VALID
Definition: dump1090.h:141
#define MODES_ACFLAGS_EWSPEED_VALID
Definition: dump1090.h:134
int detectModeA(uint16_t *m, struct modesMessage *mm)
Definition: mode_ac.c:135
constexpr double mm
Definition: AugerUnits.h:113
int phase_corrected
Definition: dump1090.h:354
void decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
Definition: mode_s.c:819
int decodeMovementField(int movement)
Definition: mode_s.c:729
void detectModeS(uint16_t *m, uint32_t mlen)
Definition: mode_s.c:1466
#define MODES_ACFLAGS_ALTITUDE_VALID
Definition: dump1090.h:128
int correctedbits
Definition: dump1090.h:351
#define MODES_PREAMBLE_US
Definition: dump1090.h:104
int raw_longitude
Definition: dump1090.h:368
uint32_t modes_checksum_table[112]
Definition: mode_s.c:54
void dumpMagnitudeVector(uint16_t *m, uint32_t offset)
Definition: mode_s.c:382
void modesQueueOutput(struct modesMessage *mm)
Definition: net_io.c:407
struct errorinfo bitErrorTable[((14 *8)+(14 *8)*((14 *8)-1)/2)]
Definition: mode_s.c:216
uint64_t mstime(void)
Definition: aera.c:12
void addRecentlySeenICAOAddr(uint32_t addr)
Definition: mode_s.c:613
void dumpMagnitudeBar(int index, int magnitude)
Definition: mode_s.c:355
uint32_t addr
Definition: dump1090.h:353
#define MODES_ICAO_CACHE_LEN
Definition: dump1090.h:120
#define MODES_DEBUG_GOODCRC
Definition: dump1090.h:151
char corrected[2]
Definition: dump1090.h:352
int even_cprlon
Definition: dump1090.h:215
int vert_rate
Definition: dump1090.h:374
#define MODES_ACFLAGS_FS_VALID
Definition: dump1090.h:140
#define MODES_PREAMBLE_SIZE
Definition: dump1090.h:106
#define MODES_MSG_ENCODER_ERRS
Definition: dump1090.h:89
char flight[16]
Definition: dump1090.h:371
void decodeModeAMessage(struct modesMessage *mm, int ModeA)
Definition: mode_ac.c:359
int detectOutOfPhase(uint16_t *pPreamble)
Definition: mode_s.c:1409
uint64_t timestampLatLon
Definition: dump1090.h:203
int exit
Definition: dump1090.h:237
void modesInitErrorInfo()
Definition: mode_s.c:234
#define MODES_DEBUG_JS
Definition: dump1090.h:154
uint64_t timestampMsg
Definition: dump1090.h:355
#define MODES_ACFLAGS_AOG
Definition: dump1090.h:136
const double high[npar]
Definition: UnivRec.h:78
char * getMEDescription(int metype, int mesub)
Definition: mode_s.c:786
void applyPhaseCorrection(uint16_t *pPayload)
Definition: mode_s.c:1448
int odd_cprlon
Definition: dump1090.h:213
#define MODES_MSG_SQUELCH_LEVEL
Definition: dump1090.h:88
unsigned char signalLevel
Definition: dump1090.h:357
#define MODES_PREAMBLE_SAMPLES
Definition: dump1090.h:105
uint32_t modesChecksum(unsigned char *msg, int bits)
Definition: mode_s.c:71
int fixBitErrors(unsigned char *msg, int bits, int maxfix, char *fixedbits)
Definition: mode_s.c:304
int ns_velocity
Definition: dump1090.h:373
const char nl
Definition: SdInspector.cc:36
int odd_cprlat
Definition: dump1090.h:212
#define MODES_LONG_MSG_BYTES
Definition: dump1090.h:107
double fLon
Definition: dump1090.h:370
int pos[2]
Definition: mode_s.c:211
#define MODES_ACFLAGS_SPEED_VALID
Definition: dump1090.h:130
double fLat
Definition: dump1090.h:369
#define MODES_UNIT_METERS
Definition: dump1090.h:123
#define MODES_ACFLAGS_SQUAWK_VALID
Definition: dump1090.h:132
struct @0 Modes
int decodeAC13Field(int AC13Field, int *unit)
Definition: mode_s.c:673
#define MODES_DEBUG_NOPREAMBLE_LEVEL
Definition: dump1090.h:158
void decodeCPR(struct aircraft *a, int fflag, int surface)
Definition: mode_s.c:1956
uint32_t syndrome
Definition: mode_s.c:209
#define MODES_ACFLAGS_HEADING_VALID
Definition: dump1090.h:129
#define MODES_LONG_MSG_SAMPLES
Definition: dump1090.h:111
struct aircraft * interactiveReceiveData(struct modesMessage *mm)
Definition: interactive.c:178
void displayModesMessage(struct modesMessage *mm)
Definition: mode_s.c:1097
char * fs_str[8]
Definition: mode_s.c:773
void useModesMessage(struct modesMessage *mm)
Definition: mode_s.c:1838
void dumpRawMessageJS(char *descr, unsigned char *msg, uint16_t *m, uint32_t offset, int fixable, char *bitpos)
Definition: mode_s.c:398
#define MODES_MAX_BITERRORS
Definition: dump1090.h:92
#define MODES_ACFLAGS_LATLON_VALID
Definition: dump1090.h:127
void modesSendAllClients(int service, void *msg, int len)
Definition: net_io.c:165
void dumpRawMessage(char *descr, unsigned char *msg, uint16_t *m, uint32_t offset)
Definition: mode_s.c:440
uint32_t ICAOCacheHashAddress(uint32_t a)
Definition: mode_s.c:598
#define MODES_DEBUG_DEMOD
Definition: dump1090.h:148
int aera
Definition: dump1090.h:272
uint64_t timestamp
Definition: dump1090.h:202
#define MODES_DEBUG_DEMODERR
Definition: dump1090.h:149
const double unit[npar]
Definition: UnivRec.h:76
int cprModFunction(int a, int b)
Definition: mode_s.c:1858
int bFlags
Definition: dump1090.h:219
#define MODES_DEBUG_NOPREAMBLE
Definition: dump1090.h:152
int cprNLFunction(double lat)
Definition: mode_s.c:1868
uint32_t crc
Definition: dump1090.h:350
int decodeCPRrelative(struct aircraft *a, int fflag, int surface)
Definition: mode_s.c:2021
#define MODEAC_MSG_SAMPLES
Definition: dump1090.h:94
double lon
Definition: dump1090.h:218
int velocity
Definition: dump1090.h:375
unsigned char msg[14]
Definition: dump1090.h:346
#define MODES_ASYNC_BUF_SAMPLES
Definition: dump1090.h:85
uint16_t * magnitude
Definition: dump1090.h:229
#define MODES_ICAO_CACHE_TTL
Definition: dump1090.h:121
#define MODES_ACFLAGS_LLODD_VALID
Definition: dump1090.h:138
double lat
Definition: dump1090.h:218
#define MODES_ACFLAGS_VERTRATE_VALID
Definition: dump1090.h:131
void computeMagnitudeVector()
Definition: mode_s.c:1386
int ew_velocity
Definition: dump1090.h:372
#define MODES_SHORT_MSG_SAMPLES
Definition: dump1090.h:112
time_t seen
Definition: dump1090.h:200
#define MODES_USER_LATLON_VALID
Definition: dump1090.h:125
#define MODES_ACFLAGS_AOG_VALID
Definition: dump1090.h:139
#define MODES_LONG_MSG_SIZE
Definition: dump1090.h:113
int modesMessageLenByType(int type)
Definition: mode_s.c:103
int decodeAC12Field(int AC12Field, int *unit)
Definition: mode_s.c:704
int cprNFunction(double lat, int fflag)
Definition: mode_s.c:1933
#define MODES_LONG_MSG_BITS
Definition: dump1090.h:109
#define MODES_DEBUG_BADCRC
Definition: dump1090.h:150
char * cf_str[8]
Definition: mode_s.c:761
int altitude
Definition: dump1090.h:382
#define NERRORINFO
Definition: mode_s.c:214
time_t seenLatLon
Definition: dump1090.h:201
#define MODES_ACFLAGS_CALLSIGN_VALID
Definition: dump1090.h:133
int decodeID13Field(int ID13Field)
Definition: mode_s.c:648
#define MODES_ACFLAGS_LATLON_REL_OK
Definition: dump1090.h:142
int raw_latitude
Definition: dump1090.h:367
int cmpErrorInfo(const void *p0, const void *p1)
Definition: mode_s.c:219
#define MODES_NOTUSED(V)
Definition: dump1090.h:179
constexpr double m
Definition: AugerUnits.h:121
int ICAOAddressWasRecentlySeen(uint32_t addr)
Definition: mode_s.c:625
#define MODES_UNIT_FEET
Definition: dump1090.h:122
#define MODES_SHORT_MSG_BYTES
Definition: dump1090.h:108
double cprDlonFunction(double lat, int fflag, int surface)
Definition: mode_s.c:1941
char * ca_str[8]
Definition: mode_s.c:749
#define MODES_ACFLAGS_LLEVEN_VALID
Definition: dump1090.h:137
int even_cprlat
Definition: dump1090.h:214
#define MODES_SHORT_MSG_BITS
Definition: dump1090.h:110
#define MODES_ACFLAGS_NSSPEED_VALID
Definition: dump1090.h:135

, generated on Tue Sep 26 2023.