| 1 | // dump1090, a Mode S messages decoder for RTLSDR devices. |
| 2 | // |
| 3 | // Copyright (C) 2012 by Salvatore Sanfilippo <[email protected]> |
| 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 | // |
| 33 | //========================================================================= |
| 34 | // |
| 35 | // Input format is : 00:A4:A2:A1:00:B4:B2:B1:00:C4:C2:C1:00:D4:D2:D1 |
| 36 | // |
| 37 | int ModeAToModeC(unsigned int ModeA) |
| 38 | { |
| 39 | unsigned int FiveHundreds = 0; |
| 40 | unsigned int OneHundreds = 0; |
| 41 | |
| 42 | if ((ModeA & 0xFFFF8889) != 0 || // check zero bits are zero, D1 set is illegal |
| 43 | (ModeA & 0x000000F0) == 0) { // C1,,C4 cannot be Zero |
| 44 | return INVALID_ALTITUDE; |
| 45 | } |
| 46 | |
| 47 | if (ModeA & 0x0010) {OneHundreds ^= 0x007;} // C1 |
| 48 | if (ModeA & 0x0020) {OneHundreds ^= 0x003;} // C2 |
| 49 | if (ModeA & 0x0040) {OneHundreds ^= 0x001;} // C4 |
| 50 | |
| 51 | // Remove 7s from OneHundreds (Make 7->5, snd 5->7). |
| 52 | if ((OneHundreds & 5) == 5) {OneHundreds ^= 2;} |
| 53 | |
| 54 | // Check for invalid codes, only 1 to 5 are valid |
| 55 | if (OneHundreds > 5) { |
| 56 | return INVALID_ALTITUDE; |
| 57 | } |
| 58 | |
| 59 | //if (ModeA & 0x0001) {FiveHundreds ^= 0x1FF;} // D1 never used for altitude |
| 60 | if (ModeA & 0x0002) {FiveHundreds ^= 0x0FF;} // D2 |
| 61 | if (ModeA & 0x0004) {FiveHundreds ^= 0x07F;} // D4 |
| 62 | |
| 63 | if (ModeA & 0x1000) {FiveHundreds ^= 0x03F;} // A1 |
| 64 | if (ModeA & 0x2000) {FiveHundreds ^= 0x01F;} // A2 |
| 65 | if (ModeA & 0x4000) {FiveHundreds ^= 0x00F;} // A4 |
| 66 | |
| 67 | if (ModeA & 0x0100) {FiveHundreds ^= 0x007;} // B1 |
| 68 | if (ModeA & 0x0200) {FiveHundreds ^= 0x003;} // B2 |
| 69 | if (ModeA & 0x0400) {FiveHundreds ^= 0x001;} // B4 |
| 70 | |
| 71 | // Correct order of OneHundreds. |
| 72 | if (FiveHundreds & 1) {OneHundreds = 6 - OneHundreds;} |
| 73 | |
| 74 | return ((FiveHundreds * 5) + OneHundreds - 13); |
| 75 | } |
| 76 | // |
| 77 | //========================================================================= |
| 78 | // |
| 79 | void decodeModeAMessage(struct modesMessage *mm, int ModeA) |
| 80 | { |
| 81 | mm->msgtype = 32; // Valid Mode S DF's are DF-00 to DF-31. |
| 82 | // so use 32 to indicate Mode A/C |
| 83 | |
| 84 | mm->msgbits = 16; // Fudge up a Mode S style data stream |
| 85 | mm->msg[0] = mm->verbatim[0] = (ModeA >> 8); |
| 86 | mm->msg[1] = mm->verbatim[1] = (ModeA); |
| 87 | |
| 88 | // Fudge an address based on Mode A (remove the Ident bit) |
| 89 | mm->addr = (ModeA & 0x0000FF7F) | MODES_NON_ICAO_ADDRESS; |
| 90 | |
| 91 | // Set the Identity field to ModeA |
| 92 | mm->squawk = ModeA & 0x7777; |
| 93 | mm->squawk_valid = 1; |
| 94 | |
| 95 | // Flag ident in flight status |
| 96 | mm->spi = (ModeA & 0x0080) ? 1 : 0; |
| 97 | mm->spi_valid = 1; |
| 98 | |
| 99 | // Decode an altitude if this looks like a possible mode C |
| 100 | if (!mm->spi) { |
| 101 | int modeC = ModeAToModeC(ModeA); |
| 102 | if (modeC != INVALID_ALTITUDE) { |
| 103 | mm->altitude = modeC * 100; |
| 104 | mm->altitude_unit = UNIT_FEET; |
| 105 | mm->altitude_source = ALTITUDE_BARO; |
| 106 | mm->altitude_valid = 1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Not much else we can tell from a Mode A/C reply. |
| 111 | // Just fudge up a few bits to keep other code happy |
| 112 | mm->correctedbits = 0; |
| 113 | } |
| 114 | // |
| 115 | // ===================== Mode A/C detection and decoding =================== |
| 116 | // |