| 1 | // Part of dump1090, a Mode S message decoder for RTLSDR devices. |
| 2 | // |
| 3 | // track.h: aircraft state tracking prototypes |
| 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 | #ifndef DUMP1090_TRACK_H |
| 51 | #define DUMP1090_TRACK_H |
| 52 | |
| 53 | /* Maximum age of tracked aircraft in milliseconds */ |
| 54 | #define TRACK_AIRCRAFT_TTL 300000 |
| 55 | |
| 56 | /* Maximum age of a tracked aircraft with only 1 message received, in milliseconds */ |
| 57 | #define TRACK_AIRCRAFT_ONEHIT_TTL 60000 |
| 58 | |
| 59 | /* Maximum validity of an aircraft position */ |
| 60 | #define TRACK_AIRCRAFT_POSITION_TTL 60000 |
| 61 | |
| 62 | typedef struct { |
| 63 | datasource_t source; /* where the data came from */ |
| 64 | uint64_t updated; /* when it arrived */ |
| 65 | uint64_t stale; /* when it will become stale */ |
| 66 | uint64_t expires; /* when it will expire */ |
| 67 | } data_validity; |
| 68 | |
| 69 | /* Structure used to describe the state of one tracked aircraft */ |
| 70 | struct aircraft { |
| 71 | uint32_t addr; // ICAO address |
| 72 | addrtype_t addrtype; // highest priority address type seen for this aircraft |
| 73 | |
| 74 | uint64_t seen; // Time (millis) at which the last packet was received |
| 75 | long messages; // Number of Mode S messages received |
| 76 | |
| 77 | double signalLevel[8]; // Last 8 Signal Amplitudes |
| 78 | int signalNext; // next index of signalLevel to use |
| 79 | |
| 80 | data_validity callsign_valid; |
| 81 | char callsign[9]; // Flight number |
| 82 | |
| 83 | data_validity altitude_valid; |
| 84 | int altitude; // Altitude (Baro) |
| 85 | unsigned altitude_modeC; // (as a Mode C value) |
| 86 | |
| 87 | data_validity altitude_gnss_valid; |
| 88 | int altitude_gnss; // Altitude (GNSS) |
| 89 | |
| 90 | data_validity gnss_delta_valid; |
| 91 | int gnss_delta; // Difference between GNSS and Baro altitudes |
| 92 | |
| 93 | data_validity speed_valid; |
| 94 | unsigned speed; |
| 95 | |
| 96 | data_validity speed_ias_valid; |
| 97 | unsigned speed_ias; |
| 98 | |
| 99 | data_validity speed_tas_valid; |
| 100 | unsigned speed_tas; |
| 101 | |
| 102 | data_validity heading_valid; |
| 103 | unsigned heading; // Heading (OK it's really the track) |
| 104 | |
| 105 | data_validity heading_magnetic_valid; |
| 106 | unsigned heading_magnetic; // Heading |
| 107 | |
| 108 | data_validity vert_rate_valid; |
| 109 | int vert_rate; // Vertical rate |
| 110 | altitude_source_t vert_rate_source; |
| 111 | |
| 112 | data_validity squawk_valid; |
| 113 | unsigned squawk; // Squawk |
| 114 | |
| 115 | data_validity category_valid; |
| 116 | unsigned category; // Aircraft category A0 - D7 encoded as a single hex byte |
| 117 | |
| 118 | data_validity airground_valid; |
| 119 | airground_t airground; // air/ground status |
| 120 | |
| 121 | data_validity cpr_odd_valid; // Last seen even CPR message |
| 122 | cpr_type_t cpr_odd_type; |
| 123 | unsigned cpr_odd_lat; |
| 124 | unsigned cpr_odd_lon; |
| 125 | unsigned cpr_odd_nuc; |
| 126 | |
| 127 | data_validity cpr_even_valid; // Last seen odd CPR message |
| 128 | cpr_type_t cpr_even_type; |
| 129 | unsigned cpr_even_lat; |
| 130 | unsigned cpr_even_lon; |
| 131 | unsigned cpr_even_nuc; |
| 132 | |
| 133 | data_validity position_valid; |
| 134 | double lat, lon; // Coordinated obtained from CPR encoded data |
| 135 | unsigned pos_nuc; // NUCp of last computed position |
| 136 | |
| 137 | long modeAcount; // Mode A Squawk hit Count |
| 138 | long modeCcount; // Mode C Altitude hit Count |
| 139 | int modeACflags; // Flags for mode A/C recognition |
| 140 | |
| 141 | int fatsv_emitted_altitude; // last FA emitted altitude |
| 142 | int fatsv_emitted_altitude_gnss; // -"- GNSS altitude |
| 143 | int fatsv_emitted_heading; // -"- true track |
| 144 | int fatsv_emitted_heading_magnetic; // -"- magnetic heading |
| 145 | int fatsv_emitted_speed; // -"- groundspeed |
| 146 | int fatsv_emitted_speed_ias; // -"- IAS |
| 147 | int fatsv_emitted_speed_tas; // -"- TAS |
| 148 | airground_t fatsv_emitted_airground; // -"- air/ground state |
| 149 | unsigned char fatsv_emitted_bds_10[7]; // -"- BDS 1,0 message |
| 150 | unsigned char fatsv_emitted_bds_30[7]; // -"- BDS 3,0 message |
| 151 | unsigned char fatsv_emitted_es_status[7]; // -"- ES operational status message |
| 152 | unsigned char fatsv_emitted_es_target[7]; // -"- ES target status message |
| 153 | unsigned char fatsv_emitted_es_acas_ra[7]; // -"- ES ACAS RA report message |
| 154 | |
| 155 | uint64_t fatsv_last_emitted; // time (millis) aircraft was last FA emitted |
| 156 | |
| 157 | struct aircraft *next; // Next aircraft in our linked list |
| 158 | |
| 159 | struct modesMessage first_message; // A copy of the first message we received for this aircraft. |
| 160 | }; |
| 161 | |
| 162 | /* is this bit of data valid? */ |
| 163 | static inline int trackDataValid(const data_validity *v) |
| 164 | { |
| 165 | return (v->source != SOURCE_INVALID); |
| 166 | } |
| 167 | |
| 168 | /* .. with these constraints? */ |
| 169 | static inline int trackDataValidEx(const data_validity *v, |
| 170 | uint64_t now, |
| 171 | uint64_t maxAge, |
| 172 | datasource_t minSource) |
| 173 | { |
| 174 | if (v->source == SOURCE_INVALID) |
| 175 | return 0; |
| 176 | if (v->source < minSource) |
| 177 | return 0; |
| 178 | if (v->updated < now && (now - v->updated) > maxAge) |
| 179 | return 0; |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | /* what's the age of this data? */ |
| 184 | static inline uint64_t trackDataAge(const data_validity *v, |
| 185 | uint64_t now) |
| 186 | { |
| 187 | if (v->source == SOURCE_INVALID) |
| 188 | return ~(uint64_t)0; |
| 189 | if (v->updated >= now) |
| 190 | return 0; |
| 191 | return (now - v->updated); |
| 192 | } |
| 193 | |
| 194 | /* Update aircraft state from data in the provided mesage. |
| 195 | * Return the tracked aircraft. |
| 196 | */ |
| 197 | struct modesMessage; |
| 198 | struct aircraft *trackUpdateFromMessage(struct modesMessage *mm); |
| 199 | |
| 200 | /* Call periodically */ |
| 201 | void trackPeriodicUpdate(); |
| 202 | |
| 203 | #endif |