This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
uat2json.c
378 lines (309 loc) · 9.3 KB
/
uat2json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// Copyright 2015, Oliver Jowett <oliver@mutability.co.uk>
//
// This file is free software: you may copy, redistribute and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 2 of the License, or (at your
// option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <sys/select.h>
#include <errno.h>
#include "uat.h"
#include "uat_decode.h"
#include "reader.h"
#define NON_ICAO_ADDRESS 0x1000000U
struct aircraft {
struct aircraft *next;
uint32_t address;
uint32_t messages;
time_t last_seen;
time_t last_seen_pos;
int position_valid : 1;
int altitude_valid : 1;
int track_valid : 1;
int speed_valid : 1;
int vert_rate_valid : 1;
airground_state_t airground_state;
char callsign[9];
char squawk[9];
// if position_valid:
double lat;
double lon;
// if altitude_valid:
int32_t altitude; // in feet
// if track_valid:
uint16_t track;
// if speed_valid:
uint16_t speed; // in kts
// if vert_rate_valid:
int16_t vert_rate; // in ft/min
};
static struct aircraft *aircraft_list;
static time_t NOW;
static const char *json_dir;
static struct aircraft *find_aircraft(uint32_t address)
{
struct aircraft *a;
for (a = aircraft_list; a; a = a->next)
if (a->address == address)
return a;
return NULL;
}
static struct aircraft *find_or_create_aircraft(uint32_t address)
{
struct aircraft *a = find_aircraft(address);
if (a)
return a;
a = calloc(1, sizeof(*a));
a->address = address;
a->airground_state = AG_RESERVED;
a->next = aircraft_list;
aircraft_list = a;
return a;
}
static void expire_old_aircraft()
{
struct aircraft *a, **last;
for (last = &aircraft_list, a = *last; a; a = *last) {
if ((NOW - a->last_seen) > 300) {
*last = a->next;
free(a);
} else {
last = &a->next;
}
}
}
static uint32_t message_count;
static void process_mdb(struct uat_adsb_mdb *mdb)
{
struct aircraft *a;
uint32_t addr;
++message_count;
switch (mdb->address_qualifier) {
case AQ_ADSB_ICAO:
case AQ_TISB_ICAO:
addr = mdb->address;
break;
default:
addr = mdb->address | NON_ICAO_ADDRESS;
break;
}
a = find_or_create_aircraft(addr);
a->last_seen = NOW;
++a->messages;
// copy state into aircraft
if (mdb->airground_state != AG_RESERVED)
a->airground_state = mdb->airground_state;
if (mdb->position_valid) {
a->position_valid = 1;
a->lat = mdb->lat;
a->lon = mdb->lon;
a->last_seen_pos = NOW;
}
if (mdb->altitude_type != ALT_INVALID) {
a->altitude_valid = 1;
a->altitude = mdb->altitude;
}
if (mdb->track_type != TT_INVALID) {
a->track_valid = 1;
a->track = mdb->track;
}
if (mdb->speed_valid) {
a->speed_valid = 1;
a->speed = mdb->speed;
}
if (mdb->vert_rate_source != ALT_INVALID) {
a->vert_rate_valid = 1;
a->vert_rate = mdb->vert_rate;
}
if (mdb->callsign_type == CS_CALLSIGN)
strcpy(a->callsign, mdb->callsign);
else if (mdb->callsign_type == CS_SQUAWK)
strcpy(a->squawk, mdb->callsign);
if (mdb->sec_altitude_type != ALT_INVALID) {
// only use secondary if no primary is available
if (!a->altitude_valid || mdb->altitude_type == ALT_INVALID) {
a->altitude_valid = 1;
a->altitude = mdb->sec_altitude;
}
}
}
static int write_receiver_json(const char *dir)
{
char path[PATH_MAX];
char path_new[PATH_MAX];
FILE *f;
if (snprintf(path, PATH_MAX, "%s/receiver.json", dir) >= PATH_MAX || snprintf(path_new, PATH_MAX, "%s/receiver.json.new", dir) >= PATH_MAX) {
fprintf(stderr, "write_receiver_json: path too long\n");
return 0;
}
if (!(f = fopen(path_new, "w"))) {
fprintf(stderr, "fopen(%s): %m\n", path_new);
return 0;
}
fprintf(f,
"{\n"
" \"version\" : \"dump978-uat2json\",\n"
" \"refresh\" : 1000,\n"
" \"history\" : 0\n"
"}\n");
fclose(f);
if (rename(path_new, path) < 0) {
fprintf(stderr, "rename(%s,%s): %m\n", path_new, path);
return 0;
}
return 1;
}
static int write_aircraft_json(const char *dir)
{
char path[PATH_MAX];
char path_new[PATH_MAX];
FILE *f;
struct aircraft *a;
if (snprintf(path, PATH_MAX, "%s/aircraft.json", dir) >= PATH_MAX || snprintf(path_new, PATH_MAX, "%s/aircraft.json.new", dir) >= PATH_MAX) {
fprintf(stderr, "write_aircraft_json: path too long\n");
return 0;
}
if (!(f = fopen(path_new, "w"))) {
fprintf(stderr, "fopen(%s): %m\n", path_new);
return 0;
}
fprintf(f,
"{\n"
" \"now\" : %u,\n"
" \"messages\" : %u,\n"
" \"aircraft\" : [\n",
(unsigned)NOW,
message_count);
for (a = aircraft_list; a; a = a->next) {
if (a != aircraft_list)
fprintf(f, ",\n");
fprintf(f,
" {\"hex\":\"%s%06x\"",
(a->address & NON_ICAO_ADDRESS) ? "~" : "",
a->address & 0xFFFFFF);
if (a->squawk[0])
fprintf(f, ",\"squawk\":\"%s\"", a->squawk);
if (a->callsign[0])
fprintf(f, ",\"flight\":\"%s\"", a->callsign);
if (a->position_valid)
fprintf(f, ",\"lat\":%.6f,\"lon\":%.6f,\"seen_pos\":%u", a->lat, a->lon, (unsigned) (NOW - a->last_seen_pos));
if (a->altitude_valid)
fprintf(f, ",\"altitude\":%d", a->altitude);
if (a->vert_rate_valid)
fprintf(f, ",\"vert_rate\":%d", a->vert_rate);
if (a->track_valid)
fprintf(f, ",\"track\":%u", a->track);
if (a->speed_valid)
fprintf(f, ",\"speed\":%u", a->speed);
fprintf(f, ",\"messages\":%u,\"seen\":%u,\"rssi\":0}",
a->messages, (unsigned) (NOW - a->last_seen));
}
fprintf(f,
"\n ]\n"
"}\n");
fclose(f);
if (rename(path_new, path) < 0) {
fprintf(stderr, "rename(%s,%s): %m\n", path_new, path);
return 0;
}
return 1;
}
static void periodic_work()
{
static time_t next_write;
if (NOW >= next_write) {
expire_old_aircraft();
write_aircraft_json(json_dir);
next_write = NOW + 1;
}
}
static void handle_frame(frame_type_t type, uint8_t *frame, int len, void *extra)
{
struct uat_adsb_mdb mdb;
if (type != UAT_DOWNLINK)
return;
if (len == SHORT_FRAME_DATA_BYTES) {
if ((frame[0] >> 3) != 0) {
fprintf(stderr, "short frame with non-zero type\n");
return;
}
} else if (len == LONG_FRAME_DATA_BYTES) {
if ((frame[0] >> 3) == 0) {
fprintf(stderr, "long frame with zero type\n");
return;
}
} else {
fprintf(stderr, "odd frame size: %d\n", len);
return;
}
uat_decode_adsb_mdb(frame, &mdb);
//uat_display_adsb_mdb(&mdb, stdout);
process_mdb(&mdb);
}
static void read_loop()
{
struct dump978_reader *reader;
reader = dump978_reader_new(0, 1);
if (!reader) {
perror("dump978_reader_new");
return;
}
for (;;) {
fd_set readset, writeset, excset;
struct timeval timeout;
int framecount;
FD_ZERO(&readset);
FD_ZERO(&writeset);
FD_ZERO(&excset);
FD_SET(0, &readset);
FD_SET(0, &excset);
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
select(1, &readset, &writeset, &excset, &timeout);
NOW = time(NULL);
framecount = dump978_read_frames(reader, handle_frame, NULL);
if (framecount == 0)
break;
if (framecount < 0 && errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
perror("dump978_read_frames");
break;
}
periodic_work();
}
dump978_reader_free(reader);
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr,
"Syntax: %s <dir>\n"
"\n"
"Reads UAT messages on stdin.\n"
"Periodically writes aircraft state to <dir>/aircraft.json\n"
"Also writes <dir>/receiver.json once on startup\n",
argv[0]);
return 1;
}
json_dir = argv[1];
if (!write_receiver_json(json_dir)) {
fprintf(stderr, "Failed to write receiver.json - check permissions?\n");
return 1;
}
read_loop();
write_aircraft_json(json_dir);
return 0;
}