-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathairnav.c
5775 lines (4638 loc) · 173 KB
/
airnav.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
/*
* AirNav
*/
#include "airnav.h"
#include "util.h"
#include "dump1090.h"
#include "dictionary.h"
#include "iniparser.h"
#include <stdlib.h>
#ifdef RBCSRBLC
#include "mod_mmio.h"
unsigned int SUNXI_PIO_BASE = 0;
static volatile long int *gpio_map = NULL;
#endif
/*
* Main function for AirNav Feeder
*/
void airnav_main() {
createPidFile();
date_time_set = 0;
#ifdef RBCSRBLC
// CPU Temperature
mmio_write(0x01c25000, 0x0027003f);
mmio_write(0x01c25010, 0x00040000);
mmio_write(0x01c25018, 0x00010fff);
mmio_write(0x01c25004, 0x00000090);
int result;
result = sunxi_gpio_init();
if (result == SETUP_DEVMEM_FAIL) {
printf("No access to /dev/mem. Try running as root!");
exit(EXIT_FAILURE);
} else if (result == SETUP_MALLOC_FAIL) {
printf("No memory");
exit(EXIT_FAILURE);
} else if (result == SETUP_MMAP_FAIL) {
printf("Mmap failed on module import");
exit(EXIT_FAILURE);
}
// Configure GPIO for output
sunxi_gpio_set_cfgpin(PIN_PG4, OUTPUT); // ADS-B
sunxi_gpio_set_cfgpin(PIN_PG5, OUTPUT); // Status
sunxi_gpio_set_cfgpin(PIN_PG6, OUTPUT); // GPS
sunxi_gpio_set_cfgpin(PIN_PG7, OUTPUT); // PC
sunxi_gpio_set_cfgpin(PIN_PG8, OUTPUT); // Error
sunxi_gpio_set_cfgpin(PIN_PG9, OUTPUT); // VHF
sunxi_gpio_set_cfgpin(PIN_PE8, OUTPUT); // RF_SW1
sunxi_gpio_set_cfgpin(PIN_PE9, OUTPUT); // RF_SW2
led_off(LED_ADSB);
led_on(LED_STATUS);
led_off(LED_GPS);
led_off(LED_PC);
led_off(LED_ERROR);
led_off(LED_VHF);
#endif
for (int ab = 0; ab < MAX_ANRB; ab++) {
anrbList[ab].active = 0;
anrbList[ab].socket = malloc(sizeof (int));
*anrbList[ab].socket = -1;
}
airnav_log("System: %s\n", F_ARCH);
#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, sigpipe_handler);
txend[0] = '~';
txend[1] = '*';
// Initialize encryption variables
strcpy((char *)key, DEF_CONN_KEY );
strcpy((char *)nonce, DEF_CONN_NONCE);
#ifdef RBCS
// Init GPS
if ((rc = gps_open("localhost", "2947", &gps_data_airnav)) == -1) {
airnav_log("code: %d, reason: %s\n", rc, gps_errstr(rc));
gps_ok = 0;
} else {
gps_stream(&gps_data_airnav, WATCH_ENABLE | WATCH_JSON, NULL);
gps_ok = 1;
}
/*
* RF Filter
* SW1=1 + SW2=0 => Filter ON
* SW1=0 + SW2=1 => Filter OFF
*/
if (rf_filter_status == 0) {
sunxi_gpio_output(RF_SW1, LOW);
sunxi_gpio_output(RF_SW2, HIGH);
} else {
sunxi_gpio_output(RF_SW1, HIGH);
sunxi_gpio_output(RF_SW2, LOW);
}
#endif
// 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);
}
/*
* Copy Mutex2
*/
if (pthread_mutex_init(&m_copy2, 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);
}
/*
* INI Mutex
*/
if (pthread_mutex_init(&m_ini, NULL) != 0) {
printf("\n mutex init failed\n");
exit(EXIT_FAILURE);
}
packets_total = 0;
packets_last = 0;
// Thread to wait commands
pthread_create(&t_waitcmd, NULL, tWaitCmds, NULL);
if (net_mode > 0) {
/*
* Create thread that read from external dump and
* send data to internal (local) dump
*/
airnav_log_level(3, "Creating external source thread...\n");
pthread_create(&t_ext_source, NULL, airnav_extSourceProccess, 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, threadSendData, NULL);
// Thread for ANRB
pthread_create(&t_anrb, NULL, thread_waitNewANRB, NULL);
pthread_create(&t_anrb_send, NULL, thread_SendDataANRB, NULL);
#ifdef RBCS
// Thread for VHF LED
pthread_create(&t_vhf_led, NULL, airnavMonitorVhfLed, NULL);
#endif
#ifdef RBCSRBLC
// Thread to updated ADS-B Led
pthread_create(&t_led_adsb, NULL, thread_LED_ADSB, NULL);
#endif
#ifdef RBCS
if (autostart_acars) {
if (!checkACARSRunning()) {
startACARS();
}
}
if (autostart_vhf) {
if (!checkVhfRunning()) {
startVhf();
}
}
#endif
p_mlat = 0;
if (autostart_mlat) {
if (!checkMLATRunning()) {
startMLAT();
}
}
}
/*
* Close airnav socket
*/
void closeCon() {
if (airnav_socket != -1) {
close(airnav_socket);
airnav_socket = -1;
}
return;
}
/*
* Get patch of current file
*/
void call_realpath(char * argv0) {
char resolved_path[PATH_MAX];
binpath = realpath(argv0, resolved_path);
if (binpath != NULL) {
return;
}
int length;
char fullpath[250] = {0};
length = readlink("/proc/self/exe", fullpath, sizeof (fullpath));
if (length < 0) {
return;
}
if (length >= sizeof (fullpath)) {
return;
}
binpath = malloc(length + 1);
memset(binpath, '\0', length + 1);
#ifdef RBCSRBLC
sprintf(binpath, "/radarbox/client/rbfeeder");
#else
sprintf(binpath, "%s", fullpath);
#endif
return;
}
/*
* Update current executable
*/
void doUpdate(char payload[501]) {
MODES_NOTUSED(payload);
airnav_log_level(3, "Running doUpdate function\n");
call_realpath(program_invocation_name);
char t_file[200] = {0};
airnav_log_level(3, "Bin Path: '%s'\n", binpath);
char *path = strdup(binpath);
sprintf(t_file, "%s.tmp", path);
if (last_payload[0] == '1') {
if (strlen(last_payload) < 40) {
airnav_log_level(3, "Invalid data from server, payload too small for update confirmation and url with checksum.\n");
free(path);
return;
}
// Extract CheckSum from payload
char csum[33] = {0};
memset(&csum, 0, 33);
for (int x = 2; x < 34; x++) {
csum[x - 2] = last_payload[x];
}
airnav_log_level(3, "Checksum (expected): %s\n", csum);
char url[150] = {0};
memset(&url, 0, 150);
for (int x = 35; x < strlen(last_payload); x++) {
url[x - 35] = last_payload[x];
}
airnav_log_level(3, "There is an update available at: %s\n", url);
airnav_log_level(3, "Downloading to file: %s\n", t_file);
if (access(t_file, F_OK) != -1) {
// file exists
airnav_log_level(3, "TMP file already exists!Trying to remove...\n");
if (remove(t_file) != 0) {
airnav_log_level(3, "Failed to delete temporary files.\n");
free(path);
return;
}
}
if (get_page(url, t_file) == 1) {
char *cs = md5sumFile(t_file);
airnav_log_level(3, "Checksum received file: %s, informed by RPiserver: %s\n", cs, csum);
if (strcmp(cs, csum) == 0) { // Checksum match
char mode[] = "0555";
int i;
i = strtol(mode, 0, 8);
if (chmod(t_file, i) < 0) {
fprintf(stderr, "error in chmod(%s, %s) - %d (%s)\n", t_file, mode, errno, strerror(errno));
free(cs);
free(path);
return;
}
char oldfile[1024] = {0};
char curfile[1024] = {0};
strcpy(curfile, path);
sprintf(curfile, "%s", path);
sprintf(oldfile, "%s.old", curfile);
// If there's any old version, delete
if (access(oldfile, F_OK) != -1) {
// file exists
if (remove(oldfile) != 0) {
airnav_log_level(3, "Failed to delete old file.\n");
perror("Error deleting file");
free(path);
return;
} else {
airnav_log_level(3, "OLD file deleted (%s)\n", oldfile);
}
}
if (access(curfile, F_OK) != -1) {
// Move original filename
if (rename(curfile, oldfile) != 0) {
airnav_log_level(3, "Error renaming file to older\n");
perror("Error renaming file");
free(path);
return;
} else {
airnav_log_level(3, "Rename of current file (%s) to old file (%s) succesfull\n", curfile, oldfile);
}
}
sleep(1);
if (access(curfile, F_OK) != -1) {
airnav_log_level(3, "Curfile exist (and should not exist!)! (%s)\n", curfile);
}
if (access(t_file, F_OK) != -1) {
airnav_log_level(3, "t_file exist! (%s)\n", t_file);
}
// Move tmp file to destination file
if (rename(t_file, curfile) != 0) {
airnav_log_level(3, "Error renaming file to latest version. t_file: %s, curfile: %s\n", t_file, curfile);
perror("Error renaming file");
free(path);
return;
} else {
airnav_log_level(3, "Rename succesfull\n");
}
airnav_log("Finished downloading file!Update completed\nExiting now to restart...\n");
//system("/sbin/reboot");
Modes.exit = 1;
free(path);
Modes.exit = 1;
return;
} else {
airnav_log_level(3, "Checksum of download file doesn't match.\n");
free(cs);
free(path);
return;
}
} else {
airnav_log_level(3, "Failed to download update file at: %s\n", url);
free(path);
return;
}
} else if (last_payload[0] == '0') {
airnav_log_level(3, "There is no update available at this time.\n");
free(path);
return;
}
free(path);
return;
}
/*
* Send ACK and wait for reply from server
*/
int sendAck(void) {
if (airnav_socket == -1) {
airnav_log_level(7, "Socket not created!\n");
return 0;
}
struct p_data *tmp = preparePacket();
tmp->cmd = 4;
if (!sendPacket(tmp)) {
airnav_log_level(5, "Error sending ACK packet.\n");
return -1;
}
return 1;
}
/*
* Wait for specific CMD on socket
*/
int waitCmd(int cmd) {
signal(SIGPIPE, sigpipe_handler);
static uint64_t next_update;
uint64_t now = mstime();
int timeout = 0;
int abort = 0;
next_update = now + 1000;
if (airnav_socket == -1) {
airnav_log("Not connected to AirNAv Server\n");
return 0;
}
pthread_mutex_lock(&m_cmd);
expected = cmd;
expected_arrived = 0;
pthread_mutex_unlock(&m_cmd);
// Initial com
while (!abort) {
if (Modes.exit) {
abort = 1;
}
now = mstime();
if (now >= next_update) {
next_update = now + 1000;
timeout++;
airnav_log_level(4, "Timeout...%d (of %d)\n", timeout, AIRNAV_WAIT_PACKET_TIMEOUT);
}
if (timeout >= AIRNAV_WAIT_PACKET_TIMEOUT) {
abort = 1;
}
pthread_mutex_lock(&m_cmd);
if (expected_arrived == 1) {
airnav_log_level(4, "Expected CMD has arrived!\n");
expected_arrived = 0;
expected = 0;
pthread_mutex_unlock(&m_cmd);
return cmd;
}
pthread_mutex_unlock(&m_cmd);
usleep(100000);
}
airnav_log_level(4, "Expected packet did not arrived :(\n");
return 0;
}
/*
* Return the number of strings in char array
*/
int getArraySize(char *array) {
int i = 0;
if (array != NULL) {
while (array[i] != '\0') {
i++;
}
}
return i;
}
/*
* Send key and version to server.
*/
int sendKey(void) {
if (airnav_socket == -1) {
airnav_log_level(5, "Socket not created!\n");
return 0;
}
airnav_log_level(3, "Sending key....\n");
// KEY cmd = 1
struct p_data *tmp = preparePacket();
tmp->cmd = 1;
tmp->c_version = c_version_int;
tmp->c_version_set = 1;
strcpy(tmp->c_key, sharing_key);
tmp->c_key[32] = '\0';
tmp->c_key_set = 1;
tmp->c_type_set = 1;
sendPacket(tmp);
airnav_log_level(3, "Step 1 of sending key\n");
// Wait for reply from server
//
int reply = waitCmd(2);
airnav_log_level(3, "WaitCMD done!\n");
if (reply == 2) {
airnav_log_level(7, "Got OK from server!\n");
return 1;
} else if (reply == 0) {
airnav_log_level(7, "Got no response from server\n");
return 0;
} else if (reply == 3) {
airnav_log("Could not authenticate sharing key: %s\n", last_payload);
return -1;
} else {
airnav_log_level(7, "Got another cmd from server :( (%d)\n", reply);
return -1;
}
airnav_log_level(3, "Something wrong....\n");
return 1;
}
/*
* Send key request
*/
int sendKeyRequest(void) {
if (airnav_socket == -1) {
airnav_log_level(5, "Socket not created!\n");
return 0;
}
airnav_log_level(5, "Requesting new key!\n");
// Request CMD = 6
struct p_data *tmp = preparePacket();
tmp->cmd = 6;
tmp->c_version = c_version_int;
tmp->c_version_set = 1;
if ((strcmp(F_ARCH, "raspberry") == 0) || (strcmp(F_ARCH, "rblc2") == 0)) {
uint64_t cpuserial = getSerial2();
if (cpuserial != 0) {
char cpserial[60] = {0};
//sprintf(cpserial, "%016"PRIx32"", cpuserial);
sprintf(cpserial, "%016llx", getSerial2());
strcpy(tmp->payload, cpserial);
airnav_log_level(2, "CPU Serial to send for key request: %s\n", cpserial);
tmp->payload_set = 1;
} else {
airnav_log_level(2, "CPU Serial empty.\n");
}
} else if ((strcmp(F_ARCH, "rblc") == 0)) {
char *cpuserial = getCPUSerial_RBLC();
airnav_log_level(3, "Got CPU Serial: '%s'\n", cpuserial);
if (cpuserial != NULL) {
if (strlen(cpuserial) == 16) {
sprintf(tmp->payload, "%s", cpuserial);
tmp->payload_set = 1;
airnav_log_level(2, "Sending key request with this serial: '%s'\n", cpuserial);
} else {
airnav_log_level(2, "Erro in serial size: '%d'\n", strlen(cpuserial));
}
}
}
if (strcmp(F_ARCH, "raspberry") == 0) {
tmp->c_type = 0;
tmp->c_type_set = 1;
airnav_log_level(3, "Setting client type to 0 (Raspberry)\n");
} else if (strcmp(F_ARCH, "rbcs") == 0) {
tmp->c_type = 1;
tmp->c_type_set = 1;
airnav_log_level(3, "Setting client type to 1 (RBCS)\n");
} else if (strcmp(F_ARCH, "rblc") == 0) {
tmp->c_type = 2;
tmp->c_type_set = 1;
airnav_log_level(3, "Setting client type to 2 (RBLC)\n");
} else if (strcmp(F_ARCH, "rblc2") == 0) {
tmp->c_type = 2;
tmp->c_type_set = 1;
airnav_log_level(3, "Setting client type to 2 (RBLC2)\n");
}
if (!sendPacket(tmp)) {
airnav_log("Error sending sharing key request.\n");
return -1;
}
// Wait for reply from server
int reply = waitCmd(2);
if (reply == 2) {
airnav_log_level(7, "Got OK from server! Payload on OK Message: %s\n", last_payload);
return 1;
} else if (reply == 0) {
airnav_log_level(7, "Got no response from server\n");
return 0;
} else if (reply == 3) {
airnav_log("Error requesting new sharing key: %s.\n", last_payload);
return -1;
} else {
airnav_log_level(7, "Got another cmd from server :( (%d)\n", reply);
return -1;
}
}
/*
* Connect to airnav server (socket)
*/
int airnav_connect(void) {
//
signal(SIGPIPE, sigpipe_handler);
if (airnav_socket != -1) {
close(airnav_socket);
}
airnav_socket = socket(AF_INET, SOCK_STREAM, 0);
char *hostname = malloc(strlen(airnav_host) + 1);
strcpy(hostname, airnav_host);
char ip[100] = {0};
if (hostname_to_ip(hostname, ip)) { // Error
airnav_log_level(2, "Could not resolve hostname....using default IP.\n");
strcpy(ip, "45.63.1.41"); // Default IP
}
airnav_log_level(3, "Host %s resolved as %s\n", hostname, ip);
free(hostname);
addr_airnav.sin_family = AF_INET;
addr_airnav.sin_port = htons(airnav_port);
inet_pton(AF_INET, ip, &(addr_airnav.sin_addr));
enable_keepalive(airnav_socket);
if (connect(airnav_socket, (struct sockaddr *) &addr_airnav, sizeof (addr_airnav)) != -1) {
/* Success */
airnav_log("Connection established.\n");
airnav_log_level(3, "Connected to %s on port %d\n", airnav_host, airnav_port);
return 1;
} else {
airnav_log("Can't connect to AirNav Server. Retry in %d seconds.\n", AIRNAV_MONITOR_SECONDS);
airnav_log_level(3, "Can't connect to %s on port %d\n", airnav_host, airnav_port);
airnav_socket = -1;
return 0;
}
}
/*
* Initial communication with AirNavServer
*/
int airnav_initial_com(void) {
airnav_log_level(7, "Starting initial protocol...\n");
signal(SIGPIPE, sigpipe_handler);
int reply = -2;
ini_getString(&sharing_key, configuration_file, "client", "key", "");
// Check if sharing-key is valid
if (getArraySize((char*) sharing_key) > 0 && getArraySize((char*) sharing_key) < 32) {
airnav_log("Error: invalid sharing-key. Check your key and try again.\n");
airnav_log("If you don't have a sharing-key, leave field 'key' empty (in rbfeeder.ini) and the feeder will try to auto-generate a new sharing key.\n");
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 0;
}
if (airnav_connect() != 1) {
return 0;
}
if (getArraySize((char*) sharing_key) == 0) {
airnav_log("Empty sharing key. We will try to create a new one for you!\n");
reply = sendKeyRequest();
if (reply == 1) {
airnav_log_level(5, "New key generated! This is the key: %s\n", last_payload);
airnav_log_level(5, "Size of new payload (KEY): %d\n", getArraySize((char*) &last_payload));
if (getArraySize((char*) &last_payload) == 32) { // Check if is exactly 32 chars on key
ini_saveGeneric(configuration_file, "client", "key", last_payload);
sharing_key = malloc(33);
memcpy(sharing_key, &last_payload, 32);
sharing_key[32] = '\0';
airnav_log("Your new key is %s. Please save this key for future use. You will have to know this key to link this receiver to your account in RadarBox24.com. This key is also saved in configuration file (%s)\n", sharing_key, configuration_file);
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 1;
} else {
airnav_log("Key received from server is invalid.\n");
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 0;
}
} else if (reply == 0) {
airnav_log_level(5, "Timeout waiting for new key.\n");
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 0;
} else {
airnav_log_level(5, "Could not generate new key. Error from server: %s\n", last_payload);
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 0;
}
} else {
airnav_log_level(7, "Sending sharing key to server...\n");
reply = sendKey();
if (reply == 1) {
airnav_com_inited = 1;
airnav_log("Connection with RadarBox24 server OK! Key accepted by server.\n");
// Request our serial number
struct p_data *tmp1 = preparePacket();
tmp1->cmd = 8; // Request our SN
sendPacket(tmp1);
// Request date/time
struct p_data *jon1 = preparePacket();
jon1->cmd = 14;
sendPacket(jon1);
airnav_log_level(3, "DATE/TIME request sent!\n");
sleep(1);
// Request update information
struct p_data *jon = preparePacket();
char str[200];
memset(str, 0, 200);
jon->cmd = 7;
sprintf(str, "%s|%s", c_version_str, F_ARCH);
strcpy(jon->payload, str);
jon->payload_set = 1;
sendPacket(jon);
// Send our system version
sendSystemVersion();
airnav_log_level(3, "Init OK!!!\n");
return 1;
} else {
airnav_log("Could not start connection. Timeout.\n");
if (last_cmd == 3) {
airnav_log("Last server error: %s\n", last_payload);
}
airnav_com_inited = 0;
close(airnav_socket);
airnav_socket = -1;
return 0;
}
}
return 1;
}
/*
* Function to monitor connection with AirNav
* Server.
*/
void *airnav_monitorConnection(void *arg) {
MODES_NOTUSED(arg);
int local_counter = 0;
int local_counter2 = 0;
int position_send_time = SEND_POSITION_TIME - 60;
gps_led = 0;
error_led = 0;
int pc_led = 0;
sleep(1);
airnav_initial_com();
struct p_data *tmp = NULL;
while (!Modes.exit) {
#ifdef RBCSRBLC
getCPUTemp();
#ifdef RBCS
// Update GPS localization
if (gps_ok == 1) {
updateGPSData();
if (!gps_fixed) {
if (!gps_led) {
gps_led = 1;
led_on(LED_GPS);
} else {
gps_led = 0;
led_off(LED_GPS);
}
} else {
if (!gps_led) {
led_on(LED_GPS);
gps_led = 1;
}
}
}
#endif
// Error led
if (airnav_com_inited != 1) {
if (!error_led) {
error_led = 1;
led_on(LED_ERROR);
} else {
error_led = 0;
led_off(LED_ERROR);
}
} else {
if (error_led) {
led_off(LED_ERROR);
error_led = 0;
}
}
// PC Led
if (isANRBConnected() == 1) {
if (pc_led == 0) {
led_on(LED_PC);
pc_led = 1;
}
} else {
if (pc_led == 1) {
led_off(LED_PC);
pc_led = 0;
}
}
#endif
// Check update interval
if (local_counter2 >= UPDATE_CHECK_TIME) {
local_counter2 = 0;
if (airnav_com_inited == 1) {
airnav_log_level(2, "Sending update request information...\n");
struct p_data *jon = preparePacket();
char str[200];
memset(str, 0, 200);
jon->cmd = 7;
sprintf(str, "%s|%s", c_version_str, F_ARCH);
strcpy(jon->payload, str);
jon->payload_set = 1;
sendPacket(jon);
}
} else {
local_counter2++;
}
#ifndef RBLC
// Send position interval
if (position_send_time >= SEND_POSITION_TIME) {
position_send_time = 0;
if (g_lat != 0 && g_lon != 0 && g_alt != -999) {
if (g_lat != 0) {
char slat[30] = {0};
sprintf(slat, "%f", g_lat);
ini_saveGeneric(configuration_file, "client", "lat", slat);
}
if (g_lon != 0) {
char slon[30] = {0};
sprintf(slon, "%f", g_lon);
ini_saveGeneric(configuration_file, "client", "lon", slon);
}
if (g_alt != 0) {
char salt[30] = {0};
sprintf(salt, "%d", g_alt);
ini_saveGeneric(configuration_file, "client", "alt", salt);
}
char s_used[30] = {0};
char s_visi[30] = {0};
sprintf(s_used, "%d", sats_used);
sprintf(s_visi, "%d", sats_visible);
ini_saveGeneric(configuration_file, "client", "sat_used", s_used);
ini_saveGeneric(configuration_file, "client", "sat_visible", s_visi);
if (airnav_com_inited == 1) {
airnav_log_level(2, "Sending position packet...\n");
tmp = preparePacket();
tmp->cmd = 9;
tmp->lat = g_lat;
tmp->lon = g_lon;
tmp->altitude = g_alt;
tmp->position_set = 1;
tmp->altitude_set = 1;
sendPacket(tmp);
}
}
} else {
position_send_time++;
}
#endif
if (local_counter >= AIRNAV_MONITOR_SECONDS) {
local_counter = 0;
if (airnav_com_inited == 0) {
airnav_log_level(5, "[MONITOR1] Connection not initialized. Trying init protocol.\n");