adsblib 1.0
Portable C99 library for encoding ADS-B Out DF17 messages
 
Loading...
Searching...
No Matches
adsblib.h
1#ifndef ADSB_ENCODER_H
2#define ADSB_ENCODER_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11/* ============================================================
12 * Library Version
13 * ============================================================ */
14
15#define ADSBLIB_VERSION_MAJOR 0
16#define ADSBLIB_VERSION_MINOR 2
17#define ADSBLIB_VERSION_PATCH 0
18
19/**
20 * Get the library version as a "MAJOR.MINOR.PATCH" string.
21 */
22const char *adsb_version_string(void);
23
24/* ============================================================
25 * General Definitions
26 * ============================================================ */
27
28#define ADSB_FRAME_BYTES 14U
29#define ADSB_FRAME_BITS 112U
30#define ADSB_ICAO_MAX 0xFFFFFFU
31
32#define ADSB_CALLSIGN_LEN 8U
33
34/* ============================================================
35 * Status Codes
36 * ============================================================ */
37
38typedef enum
39{
40 ENC_OK = 0,
41
42 ENC_INVALID_ARGUMENT,
43 ENC_INVALID_ICAO,
44 ENC_INVALID_LATITUDE,
45 ENC_INVALID_LONGITUDE,
46 ENC_INVALID_ALTITUDE,
47 ENC_INVALID_CALLSIGN,
48 ENC_INVALID_SPEED,
49 ENC_INVALID_TRACK,
50 ENC_INVALID_VERTICAL_RATE,
51 ENC_INVALID_EMERGENCY_STATE,
52 ENC_INVALID_MODE_A_CODE,
53 ENC_INVALID_SELECTED_ALTITUDE,
54 ENC_INVALID_BARO_PRESSURE,
55 ENC_INVALID_NAC_P,
56 ENC_INVALID_SIL,
57 ENC_INVALID_HEADING,
58 ENC_INVALID_OPERATIONAL_SUBTYPE,
59 ENC_INVALID_ADSB_VERSION,
60
61 ENC_CPR_ERROR,
62 ENC_INTERNAL_ERROR
63
64} enc_status_t;
65
66/* ============================================================
67 * CPR Frame Type
68 * ============================================================ */
69
70typedef enum
71{
72 CPR_EVEN = 0,
73 CPR_ODD = 1
74
75} cpr_format_t;
76
77/* ============================================================
78 * Aircraft Identification
79 * ============================================================ */
80
81typedef struct
82{
83 uint32_t icao;
84 char callsign[ADSB_CALLSIGN_LEN + 1];
85
87
88/* ============================================================
89 * Airborne Position
90 * ============================================================ */
91
92typedef struct
93{
94 uint32_t icao;
95
96 double latitude_deg;
97 double longitude_deg;
98
99 int32_t altitude_ft;
100
101 cpr_format_t cpr_format;
102
104
105/* ============================================================
106 * Surface Position
107 * ============================================================ */
108
109typedef struct
110{
111 uint32_t icao;
112
113 double latitude_deg;
114 double longitude_deg;
115
116 /* Ground speed in knots (>= 0). The surface position message encodes
117 * speed with a non-linear resolution (as fine as 0.125 kt at low
118 * speed, as coarse as 5 kt approaching 175 kt); values >= 175 kt are
119 * encoded as the message's open-ended "175 kt or more" code. */
120 double ground_speed_kt;
121
122 /* Ground track in degrees [0, 360), quantized to steps of 360/128
123 * degrees (~2.8125 deg) by the message format. */
124 double ground_track_deg;
125
126 cpr_format_t cpr_format;
127
129
130/* ============================================================
131 * Airborne Velocity
132 * ============================================================ */
133
134typedef struct
135{
136 uint32_t icao;
137
138 /* Ground speed in knots (>= 0). Encoded as subtype 1 (subsonic,
139 * 1 kt/LSB) when both velocity components fit within +/-1022 kt;
140 * subtype 2 (supersonic, 4 kt/LSB, up to +/-4088 kt per component)
141 * is selected automatically otherwise. */
142 double ground_speed_kt;
143
144 double track_deg;
145
146 int32_t vertical_rate_fpm;
147
149
150/* ============================================================
151 * Emergency / Priority Status
152 * ============================================================ */
153
154typedef enum
155{
156 ADSB_EMERGENCY_NONE = 0,
157 ADSB_EMERGENCY_GENERAL,
158 ADSB_EMERGENCY_LIFEGUARD,
159 ADSB_EMERGENCY_MINIMUM_FUEL,
160 ADSB_EMERGENCY_NO_COMMUNICATIONS,
161 ADSB_EMERGENCY_UNLAWFUL_INTERFERENCE,
162 ADSB_EMERGENCY_DOWNED_AIRCRAFT
163
164} adsb_emergency_state_t;
165
166typedef struct
167{
168 uint32_t icao;
169
170 adsb_emergency_state_t emergency_state;
171
172 /* Four-digit Mode A code, represented as an octal value (0000..7777). */
173 uint16_t mode_a_code;
174
176
177/* ============================================================
178 * Target State and Status
179 * ============================================================ */
180
181typedef enum
182{
183 ADSB_ALTITUDE_SOURCE_MCP = 0,
184 ADSB_ALTITUDE_SOURCE_FMS = 1
185
186} adsb_altitude_source_t;
187
188typedef struct
189{
190 uint32_t icao;
191
192 /* -1 means not available; otherwise encoded at 32 ft resolution. */
193 int32_t selected_altitude_ft;
194 adsb_altitude_source_t selected_altitude_source;
195
196 /* -1.0 means not available; otherwise encoded at 0.8 mbar resolution. */
197 double barometric_pressure_mbar;
198
199 bool selected_heading_valid;
200 double selected_heading_deg;
201
202 uint8_t nac_p;
203 bool nic_baro;
204 uint8_t sil;
205
206 bool mode_status;
207 bool autopilot_engaged;
208 bool vnav_mode;
209 bool altitude_hold_mode;
210 bool approach_mode;
211 bool tcas_operational;
212 bool lnav_mode;
213
215
216/* ============================================================
217 * Aircraft Operational Status
218 * ============================================================ */
219
220typedef struct
221{
222 uint32_t icao;
223
224 /* Subtype 0 is airborne; subtype 1 is surface for ADS-B v1/v2. */
225 uint8_t subtype;
226
227 /* Version- and subtype-specific BDS 6,5 bit regions, kept raw. */
228 uint16_t capability_class;
229 uint16_t operational_mode;
230
231 uint8_t adsb_version;
232 bool nic_supplement_a;
233 uint8_t nac_p;
234 uint8_t sil;
235 bool nic_baro;
236 bool heading_reference_magnetic;
237 bool sil_supplement;
238
240
241/* ============================================================
242 * Public Encoding Functions
243 * ============================================================ */
244
245/**
246 * Encode DF17 Aircraft Identification message.
247 */
248enc_status_t adsb_encode_identification(
249 const adsb_identification_t *msg,
250 uint8_t frame[ADSB_FRAME_BYTES]
251);
252
253/**
254 * Encode DF17 Airborne Position message.
255 */
256enc_status_t adsb_encode_position(
257 const adsb_position_t *msg,
258 uint8_t frame[ADSB_FRAME_BYTES]
259);
260
261/**
262 * Encode DF17 Surface Position message (Type Code 8).
263 */
264enc_status_t adsb_encode_surface_position(
265 const adsb_surface_position_t *msg,
266 uint8_t frame[ADSB_FRAME_BYTES]
267);
268
269/**
270 * Encode DF17 Airborne Velocity message.
271 *
272 * Automatically selects subtype 1 (subsonic) or subtype 2 (supersonic,
273 * 4 kt/LSB) based on the encoded ground speed. See adsb_velocity_t.
274 */
275enc_status_t adsb_encode_velocity(
276 const adsb_velocity_t *msg,
277 uint8_t frame[ADSB_FRAME_BYTES]
278);
279
280/**
281 * Encode DF17 Aircraft Status / Emergency and Priority Status message
282 * (Type Code 28, subtype 1).
283 */
284enc_status_t adsb_encode_emergency(
285 const adsb_emergency_t *msg,
286 uint8_t frame[ADSB_FRAME_BYTES]
287);
288
289/**
290 * Encode DF17 Target State and Status message (Type Code 29, subtype 1).
291 */
292enc_status_t adsb_encode_target_state(
293 const adsb_target_state_t *msg,
294 uint8_t frame[ADSB_FRAME_BYTES]
295);
296
297/**
298 * Encode DF17 Aircraft Operational Status message
299 * (Type Code 31, BDS 6,5).
300 */
301enc_status_t adsb_encode_operational_status(
302 const adsb_operational_status_t *msg,
303 uint8_t frame[ADSB_FRAME_BYTES]
304);
305
306/* ============================================================
307 * Utility Functions
308 * ============================================================ */
309
310/**
311 * Calculate Mode-S CRC24.
312 *
313 * Input:
314 * First 11 bytes (88 bits) of DF17 frame.
315 *
316 * Output:
317 * 24-bit CRC value.
318 */
319uint32_t adsb_crc24(
320 const uint8_t *data,
321 uint32_t num_bits
322);
323
324/**
325 * Insert CRC parity into frame.
326 */
327void adsb_apply_crc(
328 uint8_t frame[ADSB_FRAME_BYTES]
329);
330
331/**
332 * Verify CRC validity.
333 */
334bool adsb_verify_crc(
335 const uint8_t frame[ADSB_FRAME_BYTES]
336);
337
338/* ============================================================
339 * CPR Functions
340 * ============================================================ */
341
342/**
343 * Encode CPR latitude.
344 */
345uint32_t adsb_cpr_encode_latitude(
346 double latitude_deg,
347 cpr_format_t format
348);
349
350/**
351 * Encode CPR longitude.
352 */
353uint32_t adsb_cpr_encode_longitude(
354 double latitude_deg,
355 double longitude_deg,
356 cpr_format_t format
357);
358
359/**
360 * NL(latitude) function.
361 */
362int adsb_cpr_nl(
363 double latitude_deg
364);
365
366/* ============================================================
367 * Frame Utilities
368 * ============================================================ */
369
370/**
371 * Convert frame to hexadecimal string.
372 *
373 * Output buffer size must be >= 29 bytes.
374 */
375void adsb_frame_to_hex(
376 const uint8_t frame[ADSB_FRAME_BYTES],
377 char hex_string[29]
378);
379
380/**
381 * Clear frame contents.
382 */
383void adsb_frame_clear(
384 uint8_t frame[ADSB_FRAME_BYTES]
385);
386
387#ifdef __cplusplus
388}
389#endif
390
391#endif /* ADSB_ENCODER_H */