forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acarsdec.c
517 lines (479 loc) · 12 KB
/
acarsdec.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Copyright (c) 2015 Thierry Leconte
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <sched.h>
#include <unistd.h>
#include <limits.h>
#ifdef HAVE_LIBACARS
#include <libacars/version.h>
#endif
#include "acarsdec.h"
extern void build_label_filter(char *arg);
channel_t channel[MAXNBCHANNELS];
unsigned int nbch;
char *idstation = NULL;
int inmode = 0;
int verbose = 0;
int outtype = OUTTYPE_STD;
int netout = NETLOG_NONE;
int airflt = 0;
int emptymsg = 0;
int mdly=600;
int hourly = 0;
int daily = 0;
int signalExit = 0;
#ifdef HAVE_LIBACARS
int skip_reassembly = 0;
#endif
#ifdef WITH_RTL
int gain = -100;
int ppm = 0;
int rtlMult = 160;
#endif
#ifdef WITH_AIR
int gain = 18;
#endif
#ifdef WITH_SDRPLAY
int lnaState = 2;
int GRdB = 20;
int ppm = 0;
#endif
#ifdef WITH_SOAPY
char *antenna=NULL;
double gain = -10.0;
int ppm = 0;
int rateMult = 160;
int freq = 0;
#endif
#ifdef WITH_MQTT
char *mqtt_urls[16];
int mqtt_nburls=0;
char *mqtt_topic=NULL;
char *mqtt_user=NULL;
char *mqtt_passwd=NULL;
#endif
char *Rawaddr = NULL;
char *logfilename = NULL;
static void usage(void)
{
fprintf(stderr,
"Acarsdec/acarsserv %s Copyright (c) 2022 Thierry Leconte\n", ACARSDEC_VERSION);
#ifdef HAVE_LIBACARS
fprintf(stderr, "(libacars %s)\n", LA_VERSION);
#endif
fprintf(stderr,
"\nUsage: acarsdec [-o lv] [-t time] [-A] [-b 'labels,..'] [-e] [-i station_id] [-n|-j|-N ipaddr:port] [-l logfile [-H|-D]]");
#ifdef HAVE_LIBACARS
fprintf(stderr, " [--skip-reassembly] ");
#endif
#ifdef WITH_MQTT
fprintf(stderr, " [ -M mqtt_url");
fprintf(stderr, " [-T mqtt_topic] |");
fprintf(stderr, " [-U mqtt_user |");
fprintf(stderr, " -P mqtt_passwd]]|");
#endif
#ifdef WITH_ALSA
fprintf(stderr, " -a alsapcmdevice |");
#endif
#ifdef WITH_SNDFILE
fprintf(stderr, " -f inputwavfile |");
#endif
#ifdef WITH_RTL
fprintf(stderr,
" [-g gain] [-p ppm] -r rtldevicenumber f1 [f2] ... [fN]");
#endif
#ifdef WITH_AIR
fprintf(stderr,
"[-g linearity_gain] -s airspydevicenumber f1 [f2] ... [fN]");
#endif
#ifdef WITH_SDRPLAY
fprintf (stderr, " [-L lnaState] [-G GRdB] [-p ppm] -s f1 [f2] .. [fN]");
#endif
#ifdef WITH_SOAPY
fprintf (stderr, " [--antenna antenna] [-g gain] [-p ppm] [-c freq] -d devicestring f1 [f2] .. [fN]");
#endif
fprintf(stderr, "\n\n");
#ifdef HAVE_LIBACARS
fprintf(stderr, " --skip-reassembly\t: disable reassembling fragmented ACARS messages\n");
#endif
fprintf(stderr,
" -i stationid\t\t: station id used in acarsdec network format.\n");
fprintf(stderr,
" -A\t\t\t: don't output uplink messages (ie : only aircraft messages)\n");
fprintf(stderr,
" -e\t\t\t: don't output empty messages (ie : _d,Q0, etc ...)\n");
fprintf(stderr,
" -b filter\t\t: filter output by label (ex: -b \"H1:Q0\" : only output messages with label H1 or Q0)\n");
fprintf(stderr,
" -o lv\t\t\t: output format : 0 : no log, 1 : one line by msg, 2 : full (default) , 3 : monitor , 4 : msg JSON, 5 : route JSON\n");
fprintf(stderr,
" -t time\t\t: set forget time (TTL) in seconds for monitor mode (default=600s)\n");
fprintf(stderr,
" -l logfile\t\t: append log messages to logfile (Default : stdout).\n");
fprintf(stderr,
" -H\t\t\t: rotate log file once every hour\n");
fprintf(stderr,
" -D\t\t\t: rotate log file once every day\n");
fprintf(stderr, "\n");
fprintf(stderr,
" -n ipaddr:port\t\t: send acars messages to addr:port on UDP in planeplotter compatible format\n");
fprintf(stderr,
" -N ipaddr:port\t\t: send acars messages to addr:port on UDP in acarsdec native format\n");
fprintf(stderr,
" -j ipaddr:port\t\t: send acars messages to addr:port on UDP in acarsdec json format\n");
#ifdef WITH_MQTT
fprintf(stderr, " -M mqtt_url\t\t: Url of MQTT broker\n");
fprintf(stderr, " -T mqtt_topic\t\t: Optionnal MQTT topic (default : acarsdec/${station_id})\n");
fprintf(stderr, " -U mqtt_user\t\t: Optional MQTT username\n");
fprintf(stderr, " -P mqtt_passwd\t\t: Optional MQTT password\n");
#endif
fprintf(stderr, "\n");
#ifdef WITH_ALSA
fprintf(stderr,
" -a alsapcmdevice\t: decode from soundcard input alsapcmdevice (ie: hw:0,0)\n");
#endif
#ifdef WITH_SNDFILE
fprintf(stderr,
" -f inputwavfile\t: decode from a wav file at %d sampling rate\n",INTRATE);
#endif
#ifdef WITH_RTL
fprintf(stderr,
" -g gain\t\t: set rtl gain in db (0 to 49.6; >52 and -10 will result in AGC; default is AGC)\n");
fprintf(stderr, " -p ppm\t\t\t: set rtl ppm frequency correction\n");
fprintf(stderr, " -m rtlMult\t\t\t: set rtl sample rate multiplier: 160 for 2 MS/s or 192 for 2.4 MS/s (default: 160)\n");
fprintf(stderr,
" -r rtldevice f1 [f2]...[f%d]\t: decode from rtl dongle number or S/N rtldevice receiving at VHF frequencies f1 and optionally f2 to f%d in Mhz (ie : -r 0 131.525 131.725 131.825 )\n", MAXNBCHANNELS, MAXNBCHANNELS);
#endif
#ifdef WITH_AIR
fprintf(stderr,
" -g linearity_gain\t: set linearity gain [0-21] default : 18\n");
fprintf(stderr,
" -s airspydevice f1 [f2]...[f%d]\t: decode from airspy dongle number or hex serial number receiving at VHF frequencies f1 and optionally f2 to f%d in Mhz (ie : -s 131.525 131.725 131.825 )\n", MAXNBCHANNELS, MAXNBCHANNELS);
#endif
#ifdef WITH_SDRPLAY
fprintf (stderr,
"-L lnaState: set the lnaState (depends on the device)\n"\
"-G Gain reducction in dB's, range 20 .. 59 (-100 is autogain)\n"\
" -s f1 [f2]...[f%d]\t: decode from sdrplay receiving at VHF frequencies f1 and optionally f2 to f%d in Mhz (ie : -s 131.525 131.725 131.825 )\n", MAXNBCHANNELS, MAXNBCHANNELS);
#endif
#ifdef WITH_SOAPY
fprintf(stderr,
" --antenna antenna\t: set antenna port to use\n");
fprintf(stderr,
" -g gain\t\t: set gain in db (-10 will result in AGC; default is AGC)\n");
fprintf(stderr, " -p ppm\t\t\t: set ppm frequency correction\n");
fprintf(stderr, " c freq\t\t\t: set center frequency to tune to\n");
fprintf(stderr, " -m rateMult\t\t\t: set sample rate multiplier: 160 for 2 MS/s or 192 for 2.4 MS/s (default: 160)\n");
fprintf (stderr,
" -d devicestring f1 [f2] .. [f%d]\t: decode from a SoapySDR device located by devicestring at VHF frequencies f1 and optionally f2 to f%d in Mhz (ie : -d driver=rtltcp 131.525 131.725 131.825 )\n", MAXNBCHANNELS, MAXNBCHANNELS);
#endif
fprintf(stderr,
" Up to %d channels may be simultaneously decoded\n", MAXNBCHANNELS);
exit(1);
}
static void sigintHandler(int signum)
{
fprintf(stderr, "Received signal %s, exiting.\n", strsignal(signum));
#ifdef DEBUG
SndWriteClose();
#endif
#ifdef WITH_RTL
signalExit = 1;
runRtlCancel();
#elif WITH_SOAPY
signalExit = 1;
#else
exit(0);
#endif
}
int main(int argc, char **argv)
{
int c;
int res, n;
struct sigaction sigact;
struct option long_opts[] = {
{ "verbose", no_argument, NULL, 'v' },
{ "skip-reassembly", no_argument, NULL, 1 },
#ifdef WITH_SOAPY
{ "antenna", required_argument, NULL, 2},
#endif
{ NULL, 0, NULL, 0 }
};
char sys_hostname[HOST_NAME_MAX+1];
char *lblf=NULL;
gethostname(sys_hostname, HOST_NAME_MAX);
sys_hostname[HOST_NAME_MAX]=0;
idstation = strdup(sys_hostname);
res = 0;
while ((c = getopt_long(argc, argv, "HDvarfdsRo:t:g:m:Aep:n:N:j:l:c:i:L:G:b:M:P:U:T:", long_opts, NULL)) != EOF) {
switch (c) {
case 'v':
verbose = 1;
break;
case 'o':
outtype = atoi(optarg);
break;
case 't':
mdly = atoi(optarg);
break;
case 'b':
lblf=optarg;
break;
#ifdef HAVE_LIBACARS
case 1:
skip_reassembly = 1;
break;
#endif
#ifdef WITH_ALSA
case 'a':
res = initAlsa(argv, optind);
inmode = 1;
break;
#endif
#ifdef WITH_SNDFILE
case 'f':
res = initSoundfile(argv, optind);
inmode = 2;
break;
#endif
#ifdef WITH_RTL
case 'r':
res = initRtl(argv, optind);
inmode = 3;
break;
case 'p':
ppm = atoi(optarg);
break;
case 'g':
gain = 10 * atof(optarg);
break;
case 'm':
rtlMult = atoi(optarg);
break;
#endif
#ifdef WITH_SDRPLAY
case 's':
res = initSdrplay (argv, optind);
inmode = 5;
break;
case 'p':
ppm = atoi(optarg);
break;
case 'L':
lnaState = atoi(optarg);
break;
case 'G':
GRdB = atoi(optarg);
break;
#endif
#ifdef WITH_SOAPY
case 2:
antenna = optarg;
break;
case 'd':
res = initSoapy(argv, optind);
inmode = 6;
break;
case 'p':
ppm = atoi(optarg);
break;
case 'c':
freq = atoi(optarg);
break;
case 'g':
gain = atof(optarg);
break;
case 'm':
rateMult = atoi(optarg);
break;
#endif
#ifdef WITH_AIR
case 's':
res = initAirspy(argv, optind);
inmode = 4;
break;
case 'g':
gain = atoi(optarg);
break;
#endif
#ifdef WITH_MQTT
case 'M':
if(mqtt_nburls<15) {
mqtt_urls[mqtt_nburls]=strdup(optarg);
mqtt_nburls++;
mqtt_urls[mqtt_nburls]=NULL;
netout = NETLOG_MQTT;
}
break;
case 'U':
mqtt_user = strdup(optarg);
break;
case 'P':
mqtt_passwd = strdup(optarg);
break;
case 'T':
mqtt_topic = strdup(optarg);
break;
#endif
case 'n':
Rawaddr = optarg;
netout = NETLOG_PLANEPLOTTER;
break;
case 'N':
Rawaddr = optarg;
netout = NETLOG_NATIVE;
break;
case 'j':
Rawaddr = optarg;
netout = NETLOG_JSON;
break;
case 'A':
airflt = 1;
break;
case 'e':
emptymsg = 1;
break;
case 'l':
logfilename = optarg;
break;
case 'H':
hourly = 1;
break;
case 'D':
daily = 1;
break;
case 'i':
free(idstation);
idstation = strdup(optarg);
break;
default:
usage();
}
}
if (inmode == 0) {
fprintf(stderr, "Need at least one of -a|-f|-r|-R|-d options\n");
usage();
}
if (res) {
fprintf(stderr, "Unable to init input\n");
exit(res);
}
if(hourly && daily) {
fprintf(stderr, "Options: -H and -D are exclusive\n");
exit(1);
}
build_label_filter(lblf);
res = initOutput(logfilename, Rawaddr);
if (res) {
fprintf(stderr, "Unable to init output\n");
exit(res);
}
#ifdef WITH_MQTT
if (netout == NETLOG_MQTT) {
res = MQTTinit(mqtt_urls,idstation,mqtt_topic,mqtt_user,mqtt_passwd);
if (res) {
fprintf(stderr, "Unable to init MQTT\n");
exit(res);
}
}
#endif
#ifdef WITH_SOAPY
if (antenna) {
if (verbose) fprintf(stderr, "Setting soapy antenna to %s\n", antenna);
res = soapySetAntenna(antenna);
if (res) {
fprintf(stderr, "Unable to set antenna for SoapySDR\n");
exit(res);
}
}
#endif
sigact.sa_handler = sigintHandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
for (n = 0; n < nbch; n++) {
channel[n].chn = n;
res = initMsk(&(channel[n]));
if (res)
break;
res = initAcars(&(channel[n]));
if (res)
break;
}
if (res) {
fprintf(stderr, "Unable to init internal decoders\n");
exit(res);
}
#ifdef DEBUG
if (inmode !=2 ) {
initSndWrite();
}
#endif
if (verbose)
fprintf(stderr, "Decoding %d channels\n", nbch);
/* main decoding */
switch (inmode) {
#ifdef WITH_ALSA
case 1:
res = runAlsaSample();
break;
#endif
#ifdef WITH_SNDFILE
case 2:
res = runSoundfileSample();
break;
#endif
#ifdef WITH_RTL
case 3:
runRtlSample();
res = runRtlClose();
break;
#endif
#ifdef WITH_AIR
case 4:
res = runAirspySample();
break;
#endif
#ifdef WITH_SDRPLAY
case 5:
res = runSdrplaySample ();
break;
#endif
#ifdef WITH_SOAPY
case 6:
runSoapySample();
res = runSoapyClose();
break;
#endif
default:
res = -1;
}
fprintf(stderr, "exiting ...\n");
deinitAcars();
#ifdef WITH_MQTT
MQTTend();
#endif
exit(res);
}