forked from airnavsystems/rbfeeder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairnav_main.c
executable file
·1937 lines (1504 loc) · 80.6 KB
/
airnav_main.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 - AirNav Systems
*
* https://www.radarbox.com
*
* More info: https://github.com/AirNav-Systems/rbfeeder
*
*/
#include "rbfeeder.h"
#include "airnav_main.h"
/*
* Load configuration from ini file
*/
void airnav_loadConfig(int argc, char **argv) {
char tmp_str[7] = {0};
char* endptr = "0123456789";
int j = 0;
c_version_int = strtoimax(BDTIME, &endptr, 10); // Store version in integer format
configuration_file = malloc(strlen(AIRNAV_INIFILE) + 1);
strcpy(configuration_file, AIRNAV_INIFILE);
short device_n_changed = 0;
AN_NOTUSED(device_n_changed);
short show_key = 0;
AN_NOTUSED(show_key);
short define_key = 0;
AN_NOTUSED(define_key);
short network_mode = 0;
AN_NOTUSED(network_mode);
short network_mode_changed = 0;
AN_NOTUSED(network_mode_changed);
char *network_host;
AN_NOTUSED(network_host);
short network_host_changed = 0;
AN_NOTUSED(network_host_changed);
int network_port = 0;
AN_NOTUSED(network_port);
short network_port_changed = 0;
AN_NOTUSED(network_port_changed);
short no_start = 0;
AN_NOTUSED(no_start);
short protocol = 0;
AN_NOTUSED(protocol);
short protocol_changed = 0;
AN_NOTUSED(protocol_changed);
// Print version information
if (argc > 1) {
for (j = 1; j < argc; j++) {
if (!strcmp(argv[j], "--version") || !strcmp(argv[j], "-v")) {
printf("RBFeeder v%s (build %" PRIu64 ")\n", AIRNAV_VER_PRINT, c_version_int);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[j], "--version2") || !strcmp(argv[j], "-v2")) {
printf("RBFeeder v%s (build %" PRIu64 " Arch: %s)\n", AIRNAV_VER_PRINT, c_version_int, F_ARCH);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[j], "--version3") || !strcmp(argv[j], "-v3")) {
printf("%" PRIu64, c_version_int);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[j], "--config") || !strcmp(argv[j], "-c")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
configuration_file = strdup(argv[++j]);
} else {
airnav_log("Invalid argument for configuration file (--config).\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--device") || !strcmp(argv[j], "-d")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
Modes.dev_name = strdup(argv[++j]);
device_n = atoi(Modes.dev_name);
device_n_changed = 1;
} else {
airnav_log("Invalid argument for device (--device).\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--help") || !strcmp(argv[j], "-h")) {
airnav_showHelp();
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[j], "--showkey") || !strcmp(argv[j], "-sw")) {
show_key = 1;
} else if (!strcmp(argv[j], "--setkey") || !strcmp(argv[j], "-sk")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
char *tmp_key;
tmp_key = strdup(argv[++j]);
if (strlen(tmp_key) != 32) {
airnav_log("Invalid sharing key. Sharing key must have exactly 32 chars.\n");
exit(EXIT_FAILURE);
} else {
sharing_key = strdup(tmp_key);
define_key = 1;
}
} else {
airnav_log("Invalid argument for sharing key.\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--set-network-mode") || !strcmp(argv[j], "-snm")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
if (!strcmp(argv[j + 1], "1") || !strcmp(argv[j + 1], "on")) {
network_mode = 1;
network_mode_changed = 1;
++j;
} else if (!strcmp(argv[j + 1], "0") || !strcmp(argv[j + 1], "off")) {
network_mode = 0;
network_mode_changed = 1;
++j;
} else {
airnav_log("Invalid argument for network mode.\n");
exit(EXIT_FAILURE);
}
} else {
airnav_log("Invalid argument for network mode.\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--set-network-protocol") || !strcmp(argv[j], "-snp")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
if (!strcmp(argv[j + 1], "beast")) {
protocol = 1;
protocol_changed = 1;
++j;
} else if (!strcmp(argv[j + 1], "raw")) {
protocol = 2;
protocol_changed = 1;
++j;
} else {
airnav_log("Invalid argument for network protocol.\n");
exit(EXIT_FAILURE);
}
} else {
airnav_log("Invalid argument for network protocol.\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--set-network-host") || !strcmp(argv[j], "-snh")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
network_host = strdup(argv[++j]);
network_host_changed = 1;
} else {
airnav_log("Invalid argument for network host.\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--set-network-port")) {
if (argc - 1 > j && argv[j + 1][0] != '-') {
network_port = atoi(argv[++j]);
network_port_changed = 1;
} else {
airnav_log("Invalid argument for network port.\n");
exit(EXIT_FAILURE);
}
} else if (!strcmp(argv[j], "--interactive")) {
//Modes.interactive = Modes.throttle = 1;
} else if (!strcmp(argv[j], "--no-start")) {
no_start = 1;
}
}
}
if (access(configuration_file, F_OK) != -1) {
airnav_log_level(5, "Configuration file exist and is valid\n");
} else {
airnav_log("Configuration file (%s) doesn't exist.\n", configuration_file);
exit(EXIT_FAILURE);
}
if (device_n_changed == 1) {
ini_saveGeneric(configuration_file, "client", "dump_device", Modes.dev_name);
} else {
device_n = ini_getInteger(configuration_file, "client", "dump_device", -1);
}
// If device number is set (>-1), set in dump too
if (device_n > -1) {
Modes.dev_name = malloc(5);
// sprintf(Modes.dev_name, "%d", device_n);
airnav_log_level(5, "Device number: %s\n", Modes.dev_name);
}
debug_level = ini_getInteger(configuration_file, "client", "debug_level", 0);
log_file = NULL;
ini_getString(&log_file, configuration_file, "client", "log_file", NULL);
if (define_key == 1) {
ini_saveGeneric(configuration_file, "client", "key", sharing_key);
} else {
ini_getString(&sharing_key, configuration_file, "client", "key", "");
}
airnav_log_level(5, "Key carregada: %s\n", sharing_key);
// Is network mode has changed using parameters, save to ini
if (network_mode_changed == 1) {
if (network_mode == 1) {
ini_saveGeneric(configuration_file, "client", "network_mode", "true");
} else {
ini_saveGeneric(configuration_file, "client", "network_mode", "false");
}
}
// Is network protocol has changed using parameters, save to ini
if (protocol_changed == 1) {
if (protocol == 1) {
ini_saveGeneric(configuration_file, "network", "mode", "beast");
} else {
ini_saveGeneric(configuration_file, "network", "mode", "raw");
}
}
// If network host has changed
if (network_host_changed == 1) {
ini_saveGeneric(configuration_file, "network", "external_host", network_host);
}
// If network port has changed
if (network_port_changed == 1) {
char *t_port = calloc(1, 20);
sprintf(t_port, "%d", network_port);
ini_saveGeneric(configuration_file, "network", "external_port", t_port);
}
// This must be the last item
if (show_key == 1) {
if (strlen(sharing_key) == 32) {
printf("\nSharing key: %s\n", sharing_key);
printf("You can link this sharing key to your account at http://www.radarbox24.com\n");
printf("Configuration file: %s\n\n", configuration_file);
exit(EXIT_SUCCESS);
} else {
printf("\nYour sharing key is not set or is not valid. If is not set, a new sharing key will be create on first connection.\n");
printf("Configuration file: %s\n\n", configuration_file);
exit(EXIT_SUCCESS);
}
}
if (no_start == 1) {
exit(EXIT_SUCCESS);
}
// Disable or not log
disable_log = ini_getBoolean(configuration_file, "client", "disable_log", 0);
if (disable_log == 1) {
printf("Log disabled in rbfeeder.ini configuration.\n");
}
#ifdef DEBUG_RELEASE
ini_getString(&airnav_host, configuration_file, "server", "a_host", DEFAULT_AIRNAV_HOST);
#else
ini_getString(&airnav_host, configuration_file, "server", "a_host", DEFAULT_AIRNAV_HOST);
#endif
airnav_port = ini_getInteger(configuration_file, "server", "a_port", 33755);
airnav_port_v2 = ini_getInteger(configuration_file, "server", "a_port_v2", 33756);
airnav_log_level(2, "Server address: %s\n", airnav_host);
airnav_log_level(2, "Server port: %d\n", airnav_port);
// Asterix configuration
loadAsterixConfiguration();
// Port for data input in beast format (MLAT Return)
ini_getString(&beast_in_port, configuration_file, "network", "beast_input_port", "32004");
ini_getString(&local_input_port, configuration_file, "network", "intern_port", "32008");
/*
* Test if client is setup for network data
* or for local RTL data.
*/
if (ini_getBoolean(configuration_file, "client", "network_mode", 0)) {
char *external_host_name = NULL;
Modes.net = 1;
Modes.net_only = 1;
Modes.sdr_type = SDR_NONE;
airnav_log_level(3, "Network mode selected from ini\n");
net_mode = 1;
// Load remote host name from ini file
ini_getString(&external_host_name, configuration_file, "network", "external_host", NULL);
if (external_host_name == NULL) {
airnav_log("When 'network_mode' is enabled, you must specify one remote host for connection using\n");
airnav_log("'external_host' parameter in [network] section of configuration file.\n");
airnav_log("If it's your first time running this program, please check configuration file and setup\n");
airnav_log("basic configuration.\n");
exit(EXIT_FAILURE);
}
// Resolve host name
external_host = (char *) malloc(100 * sizeof (char));
if (net_hostname_to_ip(external_host_name, external_host) != 0) {
airnav_log("Could not resolve host name specified in 'external_host'.\n");
exit(EXIT_FAILURE);
}
// Now we get external port for connection
external_port = ini_getInteger(configuration_file, "network", "external_port", 0);
if (external_port == 0) {
airnav_log("When 'network_mode' is enabled, you must specify one remote port for connection using\n");
airnav_log("'external_port' parameter in [network] section of configuration file.\n");
airnav_log("If it's your first time running this program, please check configuration file and setup\n");
airnav_log("basic configuration.\n");
exit(EXIT_FAILURE);
}
char *network_mode = NULL;
ini_getString(&network_mode, configuration_file, "network", "mode", NULL);
// Let's define the network mode (RAW or BEAST)
if (network_mode == NULL) {
airnav_log("Unknow network mode (%s). Only RAW and BEAST are supported at this time.\n", tmp_str);
airnav_log("If it's your first time running this program, please check configuration file and setup\n");
airnav_log("basic configuration.\n");
exit(EXIT_FAILURE);
}
if (strstr(network_mode, "raw") != NULL) { // RAW mode
net_mode = 1;
free(Modes.net_input_raw_ports);
Modes.net_input_raw_ports = strdup(local_input_port);
} else if (strstr(network_mode, "beast") != NULL) { // beast mode
net_mode = 2;
free(Modes.net_input_beast_ports);
Modes.net_input_beast_ports = strdup(beast_in_port);
} else {
airnav_log("Unknow network mode (%s). Only RAW and BEAST are supported at this time.\n", network_mode);
airnav_log("If it's your first time running this program, please check configuration file and setup\n");
airnav_log("basic configuration.\n");
exit(EXIT_FAILURE);
}
} else {
Modes.net = 1;
Modes.net_only = 0;
net_mode = 0;
}
external_port = ini_getInteger(configuration_file, "network", "external_port", 30005);
// Dump978 JSON port to connect
dump978_port = ini_getInteger(configuration_file, "dump978", "dump978_port", 28380);
airnav_log("Starting RBFeeder Version %s (build %" PRIu64 ")\n", AIRNAV_VER_PRINT, c_version_int);
airnav_log("Using configuration file: %s\n", configuration_file);
if (net_mode > 0) {
airnav_log("Network-mode enabled.\n");
airnav_log("\t\tRemote host to fetch data: %s\n", external_host);
airnav_log("\t\tRemote port: %d\n", external_port);
airnav_log("\t\tRemote protocol: %s\n", (net_mode == 1 ? "RAW" : "BEAST"));
} else {
airnav_log("Network-mode disabled. Using local dongle.\n");
if (device_n > -1) {
airnav_log("\tDevice selected in configuration file: %d\n", device_n);
}
}
// configs
g_lat = ini_getDouble(configuration_file, "client", "lat", 0);
g_lon = ini_getDouble(configuration_file, "client", "lon", 0);
g_alt = ini_getInteger(configuration_file, "client", "alt", -999);
ini_getString(&sn, configuration_file, "client", "sn", NULL);
dump_gain = ini_getDouble(configuration_file, "client", "dump_gain", -10);
if (dump_gain != -10) {
Modes.gain = (int) ((double) dump_gain * (double) 10);
}
dump_agc = ini_getBoolean(configuration_file, "client", "dump_agc", 0);
Modes.nfix_crc = ini_getBoolean(configuration_file, "client", "dump_fix", 1);
Modes.mode_ac = ini_getBoolean(configuration_file, "client", "dump_mode_ac", 1);
#ifdef RBCSRBLC
Modes.dc_filter = 0;
#else
Modes.dc_filter = ini_getBoolean(configuration_file, "client", "dump_dc_filter", 1);
#endif
Modes.fUserLat = ini_getDouble(configuration_file, "client", "lat", 0.0);
Modes.fUserLon = ini_getDouble(configuration_file, "client", "lon", 0.0);
Modes.check_crc = ini_getBoolean(configuration_file, "client", "dump_check_crc", 1);
ini_getString(&pidfile, configuration_file, "client", "pid", "/var/run/rbfeeder/rbfeeder.pid");
if (ini_hasSection(configuration_file, "vhf") == 1) {
// VHF
loadVhfConfig();
// Force create of VHF config
if (generateVHFConfig() == 0) {
airnav_log("Error creating VHF configuration. Check permissions.\n");
}
}
// MLAT
ini_getString(&mlat_cmd, configuration_file, "mlat", "mlat_cmd", NULL);
if (mlat_cmd == NULL) {
if (file_exist("/usr/bin/mlat-client")) {
ini_getString(&mlat_cmd, configuration_file, "mlat", "mlat_cmd", "/usr/bin/mlat-client");
}
}
// dump1090-rb
ini_getString(&dumprb_cmd, configuration_file, "client", "dumprb_cmd", NULL);
if (dumprb_cmd == NULL) {
if (file_exist("/usr/local/bin/dump1090-rb")) {
ini_getString(&dumprb_cmd, configuration_file, "client", "dumprb_cmd", "/usr/local/bin/dump1090-rb");
}
if (file_exist("/usr/bin/dump1090-rb")) {
ini_getString(&dumprb_cmd, configuration_file, "client", "dumprb_cmd", "/usr/bin/dump1090-rb");
}
}
// rtl_power / RFSurvey
ini_getString(&rtlpower_cmd, configuration_file, "rtl_power", "rtlpower_cmd", NULL);
if (rtlpower_cmd == NULL) {
if (file_exist("/usr/bin/rtl_power")) {
ini_getString(&rtlpower_cmd, configuration_file, "rtl_power", "rtlpower_cmd", "/usr/bin/rtl_power");
}
// If still not found, try to locate in /usr/local/bin
if (rtlpower_cmd == NULL) {
if (file_exist("/usr/local/bin/rtl_power")) {
ini_getString(&rtlpower_cmd, configuration_file, "rtl_power", "rtlpower_cmd", "/usr/local/bin/rtl_power");
}
}
}
ini_getString(&rfsurvey_url, configuration_file, "rtl_power", "rfsurvey_url", DEFAULT_RTLPOWER_URL);
ini_getString(&rfsurvey_file, configuration_file, "rtl_power", "rfsurvey_file", DEFAULT_RTLPOWER_FILE);
ini_getString(&rfsurvey_samplerate, configuration_file, "rtl_power", "rfsurvey_samplerate", DEFAULT_RTLPOWER_SAMPLERATE);
rfsurvey_gain = ini_getDouble(configuration_file, "rtl_power", "rfsurvey_gain", 0);
rfsurvey_dongle = ini_getInteger(configuration_file, "rtl_power", "rfsurvey_dongle", 0);
ini_getString(&mlat_server, configuration_file, "mlat", "server", DEFAULT_MLAT_SERVER);
ini_getString(&mlat_pidfile, configuration_file, "mlat", "pid", NULL);
if (mlat_pidfile == NULL) {
if (file_exist("/usr/bin/mlat-client") && file_exist("/etc/default/mlat-client-config-rb")) {
ini_getString(&mlat_pidfile, configuration_file, "mlat", "pid", "/run/mlat-client-config-rb.pid");
}
}
autostart_mlat = ini_getBoolean(configuration_file, "mlat", "autostart_mlat", 1);
ini_getString(&mlat_config, configuration_file, "mlat", "config", NULL);
airnav_log_level(3, "MLAT Configuration file: %s\n", mlat_config);
if (mlat_config == NULL) {
if (file_exist("/etc/default/mlat-client-config-rb")) {
ini_getString(&mlat_config, configuration_file, "mlat", "config", "/etc/default/mlat-client-config-rb");
airnav_log_level(3, "MLAT Configuration file(2): %s\n", mlat_config);
}
}
// dump978
ini_getString(&dump978_cmd, configuration_file, "dump978", "dump978_cmd", NULL);
if (dump978_cmd == NULL) {
airnav_log_level(3, "No 978 cmd defined! Let's try default....\n");
if (file_exist("/usr/bin/dump978-rb")) {
ini_getString(&dump978_cmd, configuration_file, "dump978", "dump978_cmd", "/usr/bin/dump978-rb");
dump978_enabled = ini_getBoolean(configuration_file, "dump978", "dump978_enabled", 1);
} else {
airnav_log_level(3, "No 978 binary found\n");
dump978_enabled = ini_getBoolean(configuration_file, "dump978", "dump978_enabled", 0);
}
} else {
dump978_enabled = ini_getBoolean(configuration_file, "dump978", "dump978_enabled", 1);
}
autostart_978 = ini_getBoolean(configuration_file, "dump978", "autostart_dump978", 0);
// ACARS
ini_getString(&acars_pidfile, configuration_file, "acars", "pid", NULL);
ini_getString(&acars_cmd, configuration_file, "acars", "acars_cmd", NULL);
ini_getString(&acars_server, configuration_file, "acars", "server", "airnavsystems.com:9743");
acars_device = ini_getInteger(configuration_file, "acars", "device", 1);
ini_getString(&acars_freqs, configuration_file, "acars", "freqs", "131.550");
autostart_acars = ini_getBoolean(configuration_file, "acars", "autostart_acars", 0);
anrb_port = ini_getInteger(configuration_file, "client", "anrb_port", 32088);
ini_getString(&xorkey, configuration_file, "client", "xorkey", DEFAULT_XOR_KEY);
// Always enable net mode.
Modes.quiet = ini_getBoolean(configuration_file, "client", "dump_quiet", 1);
int closed = 1;
// Port for data output in beast format
ini_getString(&beast_out_port, configuration_file, "network", "beast_output_port", "32457");
ini_getString(&raw_out_port, configuration_file, "network", "raw_output_port", "32458");
ini_getString(&sbs_out_port, configuration_file, "network", "sbs_output_port", "32459");
// JSON output to shared-mem dir
if ((strcmp(F_ARCH, "rbcs") == 0) || (strcmp(F_ARCH, "rblc") == 0) || (strcmp(F_ARCH, "rblc2") == 0)) {
ini_getString(&Modes.json_dir, configuration_file, "client", "json_dir", "/run/shm/");
closed = ini_getBoolean(configuration_file, "client", "dump_closed", 1);
} else if ((strcmp(F_ARCH, "raspberry") == 0)) {
ini_getString(&Modes.json_dir, configuration_file, "client", "json_dir", "/dev/shm/");
closed = ini_getBoolean(configuration_file, "client", "dump_closed", 0);
} else {
ini_getString(&Modes.json_dir, configuration_file, "client", "json_dir", "/dev/shm/");
closed = ini_getBoolean(configuration_file, "client", "dump_closed", 1);
}
Modes.net = 1;
Modes.mlat = 1;
// If we should open or not network mode
if (closed) {
free(Modes.net_bind_address);
Modes.net_bind_address = strdup("127.0.0.1");
}
// Enable input for MLAT returning from server
// Always enable input-port
free(Modes.net_input_beast_ports);
Modes.net_input_beast_ports = strdup(beast_in_port);
// n RBCS, always listen on this port for MLAT client
free(Modes.net_output_beast_ports);
Modes.net_output_beast_ports = strdup(beast_out_port);
free(Modes.net_output_raw_ports);
Modes.net_output_raw_ports = strdup(raw_out_port);
free(Modes.net_output_sbs_ports);
Modes.net_output_sbs_ports = strdup(sbs_out_port);
free(Modes.net_input_raw_ports);
Modes.net_input_raw_ports = strdup(local_input_port);
// Dump978
ini_getString(&dump978_soapy_params, configuration_file, "dump978", "dump978_soapy_params", "--sdr driver=rtlsdr --sdr-auto-gain");
rf_filter_status = ini_getBoolean(configuration_file, "client", "rf_filter", 0);
led_pin_adsb = ini_getInteger(configuration_file, "led", "pin_adsb", RPI_LED_ADSB);
led_pin_status = ini_getInteger(configuration_file, "led", "pin_status", RPI_LED_STATUS);
use_leds = ini_getInteger(configuration_file, "led", "active", 0);
airnav_log_level(2, "Use leds: %d\n", use_leds);
// GNSS
use_gnss = ini_getBoolean(configuration_file, "client", "use_gnss", 1);
if (use_gnss == 1) {
Modes.use_gnss = 1;
airnav_log("Using GNSS (when available)\n");
}
// Send weather data
send_weather_data = ini_getBoolean(configuration_file, "client", "send_weather", 0);
send_beast_config = ini_getBoolean(configuration_file, "client", "send_beast_config", 1);
}
/*
* Display help message in console
*/
void airnav_showHelp(void) {
fprintf(stderr, "Starting RBFeeder Version %s\n", BDTIME);
fprintf(stderr, "\n");
fprintf(stderr, "\t--config [-c]\t <filename>\t\tSpecify configuration file to use. (default: /etc/rbfeeder.ini)\n");
fprintf(stderr, "\t--device [-d]\t <number>\t\tSpecify device number to use.\n");
fprintf(stderr, "\t--showkey [-sw]\t\t\t\tShow current sharing key set in configuration file.\n");
fprintf(stderr, "\t--setkey [-sk] <sharing key>\t\tSet sharing key and store in configuration file.\n");
fprintf(stderr, "\t--set-network-mode <on / off>\t\tEnable or disable network mode (get data from external host).\n");
fprintf(stderr, "\t--set-network-protocol <beast/raw>\tSet network protocol for external host.\n");
fprintf(stderr, "\t--set-network-port <port>\t\tSet network port for external host.\n");
fprintf(stderr, "\t--no-start\t\t\t\tDon't start application, just set options and save to configuration file.\n");
fprintf(stderr, "\t--help [-h]\t\t\t\tDisplay this message.\n");
fprintf(stderr, "\n");
}
/*
* Main function for AirNav Feeder
*/
void airnav_main() {
createPidFile();
// LED's nor available (this way) for RBCS/RBLC
#ifdef ENABLE_LED
if (use_leds) {
wiringPiSetupGpio();
pinMode(RPI_LED_STATUS, OUTPUT);
pinMode(RPI_LED_ADSB, OUTPUT);
// Turn off ADSB LED and on Status
digitalWrite(RPI_LED_STATUS, HIGH);
digitalWrite(RPI_LED_ADSB, LOW);
}
#endif
for (int ab = 0; ab < MAX_ANRB; ab++) {
anrbList[ab].active = 0;
anrbList[ab].socket = malloc(sizeof (int));
*anrbList[ab].socket = -1;
}
flist = NULL;
if (cat21_loaded == 1) {
airnav_log("ASTERIX CAT 021 Version Loaded: %s\n", cat21_version);
}
#ifdef DEBUG_RELEASE
airnav_log("****** Debug RELEASE ******\n");
#endif
airnav_log("Start date/time: %s\n", start_datetime);
struct sigaction sigchld_action = {
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
};
sigaction(SIGCHLD, &sigchld_action, NULL);
signal(SIGPIPE, net_sigpipe_handler);
// Init all mutex
airnav_init_mutex();
}
void airnav_init_mutex(void) {
// Create mutex for counters
if (pthread_mutex_init(&m_packets_counter, NULL) != 0) {
printf("\n mutex init failed\n");
exit(EXIT_FAILURE);
}
/*
* Socket Mutex
*/
if (pthread_mutex_init(&m_socket, NULL) != 0) {
printf("\n mutex init failed\n");
exit(EXIT_FAILURE);
}
/*
* Copy Mutex
*/
if (pthread_mutex_init(&m_copy, NULL) != 0) {
printf("\n mutex init failed\n");
exit(EXIT_FAILURE);
}
/*
* Cmd Mutex
*/
if (pthread_mutex_init(&m_cmd, NULL) != 0) {
printf("\n mutex init failed\n");
exit(EXIT_FAILURE);
}
/*
* Led ADSB Mutex
*/
//if (pthread_mutex_init(&m_led_adsb, NULL) != 0) {
// printf("\n mutex init failed\n");
// exit(EXIT_FAILURE);
//}
airnav_create_thread();
}
void airnav_create_thread(void) {
// Thread to wait commands
pthread_create(&t_waitcmd, NULL, net_thread_WaitCmds, NULL);
// is dump978 configured?
if (dump978_enabled > 0) {
airnav_log_level(1, "Creating dump978 thread...\n");
pthread_create(&t_dump978, NULL, uat_airnav_ext978, NULL);
}
// Thread to monitor connection with AirNav server
pthread_create(&t_monitor, NULL, airnav_monitorConnection, NULL);
// Start thread that prepare data and send
pthread_create(&t_prepareData, NULL, airnav_prepareData, NULL);
// Thread to show statistics on screen
pthread_create(&t_statistics, NULL, airnav_statistics, NULL);
// Thread to send stats
pthread_create(&t_stats, NULL, airnav_send_stats_thread, NULL);
// Thread to send data
pthread_create(&t_send_data, NULL, airnav_threadSendData, NULL);
// Thread for ANRB
pthread_create(&t_anrb, NULL, anrb_threadWaitNewANRB, NULL);
pthread_create(&t_anrb_send, NULL, anrb_threadSendDataANRB, NULL);
// Thread to updated ADS-B Led
//pthread_create(&t_led_adsb, NULL, thread_LED_ADSB, NULL);
if (autostart_acars) {
if (!acars_checkACARSRunning()) {
acars_startACARS();
}
}
if (autostart_vhf) {
if (!checkVhfRunning()) {
startVhf();
}
}
// Create asterix socket, if enabled
if (asterix_enabled == 1) {
airnav_log_level(1, "Asterix enabled. Creating socket\n");
asterix_CreateUdpSocket();
}
p_mlat = 0;
if (autostart_mlat) {
if (!mlat_checkMLATRunning()) {
mlat_startMLAT();
}
}
p_978 = 0;
if (autostart_978) {
airnav_log_level(1, "Lets check 978 to autostart....\n");
if (!uat_check978Running()) {
airnav_log_level(1, "978 not running! Let's start it\n");
uat_start978();
}
}
}
/*
* Function to monitor connection with AirNav
* Server.
*/
void *airnav_monitorConnection(void *arg) {
MODES_NOTUSED(arg);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, net_sigpipe_handler);
signal(SIGINT, rbfeederSigintHandler);
signal(SIGTERM, rbfeederSigtermHandler);
int local_counter = 0;
sleep(1);
net_initial_com();
while (!Modes.exit) {
if (local_counter >= AIRNAV_MONITOR_SECONDS) {
local_counter = 0;
// Check if we need to create VHF configuration
if (ini_getBoolean(configuration_file, "vhf", "force_create", 0) == 1) {
airnav_log_level(1, "VHF Configuration file creating requested. doing.....");
if (generateVHFConfig() == 1) {
airnav_log_level(1, "done!\n");
// Restart VHF daemon, if is running
if (checkVhfRunning() == 1) {
restartVhf();
}
} else {
airnav_log_level(1, "error creating vhf configuration.\n");
}
ini_saveGeneric(configuration_file, "vhf", "force_create", "false");
}
// Check if VHF auto start is enabled and if VHF is running
if (autostart_vhf) {
if (!checkVhfRunning()) {
startVhf();
}
}
// Check if we need to reload ASTERIX configuration
if (ini_getBoolean(configuration_file, "asterix", "force_reload", 0) == 1) {
airnav_log_level(1, "ASTERIX Reload requested. doing.....");
loadAsterixConfiguration();
ini_saveGeneric(configuration_file, "asterix", "force_reload", "false");
airnav_log_level(1, "Reload done!\n");
}
if (airnav_com_inited == 0) {
airnav_log_level(5, "[MONITOR1] Connection not initialized. Trying init protocol.\n");
close(airnav_socket);
airnav_socket = -1;
net_initial_com();
} else {
airnav_log_level(5, "[MONITOR4] Connection OK. Sending Ping...\n");
sendPing();
// sendMultipleFlights();
}
} else {
local_counter++;
}
sleep(1);
}
airnav_log_level(1, "Exited thread_monitorConnection Successfull!\n");
pthread_exit(EXIT_SUCCESS);
}
/*
* Thread to show statistics on screen
*/
void *airnav_statistics(void *arg) {
MODES_NOTUSED(arg);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, net_sigpipe_handler);
signal(SIGINT, rbfeederSigintHandler);
signal(SIGTERM, rbfeederSigtermHandler);
int local_counter = 0;
while (!Modes.exit) {
if (local_counter == AIRNV_STATISTICS_INTERVAL) {
debug_level = ini_getInteger(configuration_file, "client", "debug_level", 0);
local_counter = 0;
airnav_log("******** Statistics updated every %d seconds ********\n", AIRNV_STATISTICS_INTERVAL);
pthread_mutex_lock(&m_packets_counter);
airnav_log("Packets sent in the last %d seconds: %ld, Total packets sent since startup: %d\n", AIRNV_STATISTICS_INTERVAL, packets_last, packets_total);
packets_last = 0;
double count = 0;
count = global_data_sent;
pthread_mutex_unlock(&m_packets_counter);
const char* suffixes[7];
suffixes[0] = "B";
suffixes[1] = "KB";
suffixes[2] = "MB";
suffixes[3] = "GB";
suffixes[4] = "TB";
suffixes[5] = "PB";
suffixes[6] = "EB";
uint s = 0; // which suffix to use
while (count >= 1024 && s < 7) {
s++;
count /= 1024;
}
if (count - floor(count) == 0.0) {
airnav_log("Data sent: %d %s\n", (int) count, suffixes[s]);
} else {
airnav_log("Data sent: %.1f %s\n", count, suffixes[s]);
airnav_log_level(1, "Data sent (bytes) %lu\n", (int) global_data_sent);
}
s = 0;
count = data_received;
while (count >= 1024 && s < 7) {
s++;
count /= 1024;
}
if (count - floor(count) == 0.0) {
airnav_log("Data received: %d %s\n", (int) count, suffixes[s]);
} else {
airnav_log("Data received: %.1f %s\n", count, suffixes[s]);
}
//airnav_log("Currently tracked flights: %d\n", currently_tracked_flights);
} else {
local_counter++;
}
sleep(1);
}
airnav_log_level(1, "Exited airnav_statistics Successfull!\n");
pthread_exit(EXIT_SUCCESS);
}
/*
* Send statistics to FW
*/
void *airnav_send_stats_thread(void *argv) {
MODES_NOTUSED(argv);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, net_sigpipe_handler);
signal(SIGINT, rbfeederSigintHandler);
signal(SIGTERM, rbfeederSigtermHandler);
static uint64_t next_update;
static uint64_t next_mlat_check;
uint64_t now = mstime();
next_update = now + (AIRNAV_STATS_SEND_TIME * 1000);
next_mlat_check = now + 30 * 1000;
airnav_log_level(3, "Starting stats thread...\n");
while (Modes.exit == 0) {
now = mstime();
if (now >= next_mlat_check) {
next_mlat_check = now + 30 * 1000;
if (autostart_mlat) {
mlat_startMLAT();
}
}
if (now >= next_update) {
next_update = now + (AIRNAV_STATS_SEND_TIME * 1000);
net_sendStats();
}
usleep(1000000);
}
airnav_log_level(1, "Exited send_stats Successfull!\n");
pthread_exit(EXIT_SUCCESS);
}
/*
* Thread to send data
*/
void *airnav_threadSendData(void *argv) {
MODES_NOTUSED(argv);
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, net_sigpipe_handler);
signal(SIGINT, rbfeederSigintHandler);
signal(SIGTERM, rbfeederSigtermHandler);
struct packet_list *local_list = NULL, *tmp_counter = NULL;
unsigned qtd = 0;