forked from JoToSystems/Filter-SX1302
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lora_pkt_fwd.c
3743 lines (3290 loc) · 158 KB
/
lora_pkt_fwd.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
/*
/ _____) _ | |
( (____ _____ ____ _| |_ _____ ____| |__
\____ \| ___ | (_ _) ___ |/ ___) _ \
_____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
(C)2019 Semtech
Description:
Configure Lora concentrator and forward packets to a server
Use GPS for packet timestamping.
Send a becon at a regular interval without server intervention
License: Revised BSD License, see LICENSE.TXT file include in the project
*/
/* -------------------------------------------------------------------------- */
/* --- DEPENDANCIES --------------------------------------------------------- */
/* fix an issue between POSIX and C99 */
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif
#include <stdint.h> /* C99 types */
#include <stdbool.h> /* bool type */
#include <stdio.h> /* printf, fprintf, snprintf, fopen, fputs */
#include <inttypes.h> /* PRIx64, PRIu64... */
#include <assert.h>
#include <string.h> /* memset */
#include <signal.h> /* sigaction */
#include <time.h> /* time, clock_gettime, strftime, gmtime */
#include <sys/time.h> /* timeval */
#include <unistd.h> /* getopt, access */
#include <stdlib.h> /* atoi, exit */
#include <errno.h> /* error messages */
#include <math.h> /* modf */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gai_strerror */
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include "trace.h"
#include "jitqueue.h"
#include "parson.h"
#include "base64.h"
#include "loragw_hal.h"
#include "loragw_aux.h"
#include "loragw_reg.h"
#include "loragw_gps.h"
#include "cursor/packing.h"
/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define STRINGIFY(x) #x
#define STR(x) STRINGIFY(x)
/* -------------------------------------------------------------------------- */
/* --- PRIVATE CONSTANTS ---------------------------------------------------- */
#ifndef VERSION_STRING
#define VERSION_STRING "undefined"
#endif
#define JSON_CONF_DEFAULT "global_conf.json"
#define JSON_FILTER_DEFAULT "filter_conf.json"
#define DEFAULT_SERVER 127.0.0.1 /* hostname also supported */
#define DEFAULT_PORT_UP 1780
#define DEFAULT_PORT_DW 1782
#define DEFAULT_KEEPALIVE 5 /* default time interval for downstream keep-alive packet */
#define DEFAULT_STAT 30 /* default time interval for statistics */
#define PUSH_TIMEOUT_MS 100
#define PULL_TIMEOUT_MS 200
#define GPS_REF_MAX_AGE 30 /* maximum admitted delay in seconds of GPS loss before considering latest GPS sync unusable */
#define FETCH_SLEEP_MS 10 /* nb of ms waited when a fetch return no packets */
#define BEACON_POLL_MS 50 /* time in ms between polling of beacon TX status */
#define PROTOCOL_VERSION 2 /* v1.3 */
#define PROTOCOL_JSON_RXPK_FRAME_FORMAT 1
#define XERR_INIT_AVG 128 /* nb of measurements the XTAL correction is averaged on as initial value */
#define XERR_FILT_COEF 256 /* coefficient for low-pass XTAL error tracking */
#define PKT_PUSH_DATA 0
#define PKT_PUSH_ACK 1
#define PKT_PULL_DATA 2
#define PKT_PULL_RESP 3
#define PKT_PULL_ACK 4
#define PKT_TX_ACK 5
#define NB_PKT_MAX 255 /* max number of packets per fetch/send cycle */
#define MIN_LORA_PREAMB 6 /* minimum Lora preamble length for this application */
#define STD_LORA_PREAMB 8
#define MIN_FSK_PREAMB 3 /* minimum FSK preamble length for this application */
#define STD_FSK_PREAMB 5
#define STATUS_SIZE 200
#define TX_BUFF_SIZE ((560 * NB_PKT_MAX) + 30 + STATUS_SIZE)
#define ACK_BUFF_SIZE 80
#define UNIX_GPS_EPOCH_OFFSET 315964800 /* Number of seconds ellapsed between 01.Jan.1970 00:00:00
and 06.Jan.1980 00:00:00 */
#define DEFAULT_BEACON_FREQ_HZ 869525000
#define DEFAULT_BEACON_FREQ_NB 1
#define DEFAULT_BEACON_FREQ_STEP 0
#define DEFAULT_BEACON_DATARATE 9
#define DEFAULT_BEACON_BW_HZ 125000
#define DEFAULT_BEACON_POWER 14
#define DEFAULT_BEACON_INFODESC 0
/* ------- DevAddr Filter ------- */
#define DEFAULT_DEVADDR_FILTER 255
/* -------------------------------------------------------------------------- */
/* --- PRIVATE VARIABLES (GLOBAL) ------------------------------------------- */
/* signal handling variables */
volatile bool exit_sig = false; /* 1 -> application terminates cleanly (shut down hardware, close open files, etc) */
volatile bool quit_sig = false; /* 1 -> application terminates without shutting down the hardware */
/* packets filtering configuration variables */
static bool fwd_valid_pkt = true; /* packets with PAYLOAD CRC OK are forwarded */
static bool fwd_error_pkt = false; /* packets with PAYLOAD CRC ERROR are NOT forwarded */
static bool fwd_nocrc_pkt = false; /* packets with NO PAYLOAD CRC are NOT forwarded */
/* network configuration variables */
static uint64_t lgwm = 0; /* Lora gateway MAC address */
static char serv_addr[64] = STR(DEFAULT_SERVER); /* address of the server (host name or IPv4/IPv6) */
static char serv_port_up[8] = STR(DEFAULT_PORT_UP); /* server port for upstream traffic */
static char serv_port_down[8] = STR(DEFAULT_PORT_DW); /* server port for downstream traffic */
static int keepalive_time = DEFAULT_KEEPALIVE; /* send a PULL_DATA request every X seconds, negative = disabled */
/* statistics collection configuration variables */
static unsigned stat_interval = DEFAULT_STAT; /* time interval (in sec) at which statistics are collected and displayed */
/* gateway <-> MAC protocol variables */
static uint32_t net_mac_h; /* Most Significant Nibble, network order */
static uint32_t net_mac_l; /* Least Significant Nibble, network order */
/* network sockets */
static int sock_up; /* socket for upstream traffic */
static int sock_down; /* socket for downstream traffic */
/* network protocol variables */
static struct timeval push_timeout_half = {0, (PUSH_TIMEOUT_MS * 500)}; /* cut in half, critical for throughput */
static struct timeval pull_timeout = {0, (PULL_TIMEOUT_MS * 1000)}; /* non critical for throughput */
/* hardware access control and correction */
pthread_mutex_t mx_concent = PTHREAD_MUTEX_INITIALIZER; /* control access to the concentrator */
static pthread_mutex_t mx_xcorr = PTHREAD_MUTEX_INITIALIZER; /* control access to the XTAL correction */
static bool xtal_correct_ok = false; /* set true when XTAL correction is stable enough */
static double xtal_correct = 1.0;
/* GPS configuration and synchronization */
static char gps_dev_path[64] = "\0"; /* path of the TTY/I2C device GPS is connected on */
static int gps_dev_fd = -1; /* file descriptor of the GPS TTY port */
static enum { gps_dev_none = 0, gps_dev_tty = gps_interface_tty, gps_dev_i2c = gps_interface_i2c } gps_dev = gps_dev_none; /* GPS bus in use */
/* GPS time reference */
static pthread_mutex_t mx_timeref = PTHREAD_MUTEX_INITIALIZER; /* control access to GPS time reference */
static bool gps_ref_valid; /* is GPS reference acceptable (ie. not too old) */
static struct tref time_reference_gps; /* time reference used for GPS <-> timestamp conversion */
/* Reference coordinates, for broadcasting (beacon) */
static struct coord_s reference_coord;
/* Enable faking the GPS coordinates of the gateway */
static bool gps_fake_enable; /* enable the feature */
/* measurements to establish statistics */
static pthread_mutex_t mx_meas_up = PTHREAD_MUTEX_INITIALIZER; /* control access to the upstream measurements */
static uint32_t meas_nb_rx_rcv = 0; /* count packets received */
static uint32_t meas_nb_rx_ok = 0; /* count packets received with PAYLOAD CRC OK */
static uint32_t meas_nb_rx_bad = 0; /* count packets received with PAYLOAD CRC ERROR */
static uint32_t meas_nb_rx_nocrc = 0; /* count packets received with NO PAYLOAD CRC */
static uint32_t meas_up_pkt_fwd = 0; /* number of radio packet forwarded to the server */
static uint32_t meas_up_network_byte = 0; /* sum of UDP bytes sent for upstream traffic */
static uint32_t meas_up_payload_byte = 0; /* sum of radio payload bytes sent for upstream traffic */
static uint32_t meas_up_dgram_sent = 0; /* number of datagrams sent for upstream traffic */
static uint32_t meas_up_ack_rcv = 0; /* number of datagrams acknowledged for upstream traffic */
static pthread_mutex_t mx_meas_dw = PTHREAD_MUTEX_INITIALIZER; /* control access to the downstream measurements */
static uint32_t meas_dw_pull_sent = 0; /* number of PULL requests sent for downstream traffic */
static uint32_t meas_dw_ack_rcv = 0; /* number of PULL requests acknowledged for downstream traffic */
static uint32_t meas_dw_dgram_rcv = 0; /* count PULL response packets received for downstream traffic */
static uint32_t meas_dw_network_byte = 0; /* sum of UDP bytes sent for upstream traffic */
static uint32_t meas_dw_payload_byte = 0; /* sum of radio payload bytes sent for upstream traffic */
static uint32_t meas_nb_tx_ok = 0; /* count packets emitted successfully */
static uint32_t meas_nb_tx_fail = 0; /* count packets were TX failed for other reasons */
static uint32_t meas_nb_tx_requested = 0; /* count TX request from server (downlinks) */
static uint32_t meas_nb_tx_rejected_collision_packet = 0; /* count packets were TX request were rejected due to collision with another packet already programmed */
static uint32_t meas_nb_tx_rejected_collision_beacon = 0; /* count packets were TX request were rejected due to collision with a beacon already programmed */
static uint32_t meas_nb_tx_rejected_too_late = 0; /* count packets were TX request were rejected because it is too late to program it */
static uint32_t meas_nb_tx_rejected_too_early = 0; /* count packets were TX request were rejected because timestamp is too much in advance */
static uint32_t meas_nb_beacon_queued = 0; /* count beacon inserted in jit queue */
static uint32_t meas_nb_beacon_sent = 0; /* count beacon actually sent to concentrator */
static uint32_t meas_nb_beacon_rejected = 0; /* count beacon rejected for queuing */
static pthread_mutex_t mx_meas_gps = PTHREAD_MUTEX_INITIALIZER; /* control access to the GPS statistics */
static bool gps_coord_valid; /* could we get valid GPS coordinates ? */
static struct coord_s meas_gps_coord; /* GPS position of the gateway */
static pthread_mutex_t mx_stat_rep = PTHREAD_MUTEX_INITIALIZER; /* control access to the status report */
static bool report_ready = false; /* true when there is a new report to send to the server */
static char status_report[STATUS_SIZE]; /* status report as a JSON object */
/* beacon parameters */
static uint32_t beacon_period = 0; /* set beaconing period, must be a sub-multiple of 86400, the nb of sec in a day */
static uint32_t beacon_freq_hz = DEFAULT_BEACON_FREQ_HZ; /* set beacon TX frequency, in Hz */
static uint8_t beacon_freq_nb = DEFAULT_BEACON_FREQ_NB; /* set number of beaconing channels beacon */
static uint32_t beacon_freq_step = DEFAULT_BEACON_FREQ_STEP; /* set frequency step between beacon channels, in Hz */
static uint8_t beacon_datarate = DEFAULT_BEACON_DATARATE; /* set beacon datarate (SF) */
static uint32_t beacon_bw_hz = DEFAULT_BEACON_BW_HZ; /* set beacon bandwidth, in Hz */
static int8_t beacon_power = DEFAULT_BEACON_POWER; /* set beacon TX power, in dBm */
static uint8_t beacon_infodesc = DEFAULT_BEACON_INFODESC; /* set beacon information descriptor */
/* ------- DevAddr Filter ------- */
/* DevAddr filter settings */
static uint8_t dev_addr_filter_1 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_2 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_3 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_4 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_5 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_6 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_7 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_8 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_9 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr_filter_10 = DEFAULT_DEVADDR_FILTER;
static uint8_t dev_addr = 0;
static uint8_t drop_packet = 0; /*Flag to Drop packet based on DevAddr Filter */
static uint8_t fs_flag = 0;
static uint8_t fs_active = 0;
static uint64_t freq_temp;
static uint8_t power_threshold = 0; /* to adjust power up to 27dBm in Australia*/
/* auto-quit function */
static uint32_t autoquit_threshold = 0; /* enable auto-quit after a number of non-acknowledged PULL_DATA (0 = disabled)*/
/* Just In Time TX scheduling */
static struct jit_queue_s jit_queue[LGW_RF_CHAIN_NB];
/* Gateway specificities */
static int8_t antenna_gain = 0;
/* TX capabilities */
static struct lgw_tx_gain_lut_s txlut[LGW_RF_CHAIN_NB]; /* TX gain table */
static uint32_t tx_freq_min[LGW_RF_CHAIN_NB]; /* lowest frequency supported by TX chain */
static uint32_t tx_freq_max[LGW_RF_CHAIN_NB]; /* highest frequency supported by TX chain */
static uint32_t nb_pkt_log[LGW_IF_CHAIN_NB][8]; /* [CH][SF] */
static uint32_t nb_pkt_received_lora = 0;
static uint32_t nb_pkt_received_fsk = 0;
static struct lgw_conf_debug_s debugconf;
static uint32_t nb_pkt_received_ref[16];
/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */
static void usage(void);
static void sig_handler(int sigio);
static int parse_SX130x_configuration(const char * conf_file);
static int parse_gateway_configuration(const char * conf_file);
static int parse_debug_configuration(const char * conf_file);
static int parse_filter_configuration(const char * conf_file);
static uint16_t crc16(const uint8_t * data, unsigned size);
static double difftimespec(struct timespec end, struct timespec beginning);
static void gps_process_sync(void);
static void gps_process_coords(void);
static int get_tx_gain_lut_index(uint8_t rf_chain, int8_t rf_power, uint8_t * lut_index);
static int i2c_gps_available(size_t * avail);
static int i2c_gps_read(size_t n, uint8_t * dst);
/* threads */
void thread_up(void);
void thread_down(void);
void thread_jit(void);
void thread_gps_tty(void);
void thread_gps_i2c(void);
void thread_valid(void);
/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */
static void usage( void )
{
printf("~~~ Library version string~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf(" %s\n", lgw_version_info());
printf("~~~ Available options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf(" -h print this help\n");
printf(" -c <filename> use config file other than 'global_conf.json'\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
static void sig_handler(int sigio) {
if (sigio == SIGQUIT) {
quit_sig = true;
} else if ((sigio == SIGINT) || (sigio == SIGTERM)) {
exit_sig = true;
}
return;
}
static int parse_SX130x_configuration(const char * conf_file) {
int i, j;
char param_name[32]; /* used to generate variable parameter names */
const char *str; /* used to store string value from JSON object */
const char conf_obj_name[] = "SX130x_conf";
JSON_Value *root_val = NULL;
JSON_Value *val = NULL;
JSON_Object *conf_obj = NULL;
JSON_Object *conf_txgain_obj;
JSON_Object *conf_ts_obj;
JSON_Array *conf_txlut_array;
struct lgw_conf_board_s boardconf;
struct lgw_conf_rxrf_s rfconf;
struct lgw_conf_rxif_s ifconf;
struct lgw_conf_timestamp_s tsconf;
uint32_t sf, bw, fdev;
bool sx1250_tx_lut;
/* try to parse JSON */
root_val = json_parse_file_with_comments(conf_file);
if (root_val == NULL) {
MSG("ERROR: %s is not a valid JSON file\n", conf_file);
exit(EXIT_FAILURE);
}
/* point to the gateway configuration object */
conf_obj = json_object_get_object(json_value_get_object(root_val), conf_obj_name);
if (conf_obj == NULL) {
MSG("INFO: %s does not contain a JSON object named %s\n", conf_file, conf_obj_name);
return -1;
} else {
MSG("INFO: %s does contain a JSON object named %s, parsing SX1302 parameters\n", conf_file, conf_obj_name);
}
/* set board configuration */
memset(&boardconf, 0, sizeof boardconf); /* initialize configuration structure */
str = json_object_get_string(conf_obj, "spidev_path");
if (str != NULL) {
strncpy(boardconf.spidev_path, str, sizeof boardconf.spidev_path);
boardconf.spidev_path[sizeof boardconf.spidev_path - 1] = '\0'; /* ensure string termination */
} else {
MSG("ERROR: spidev path must be configured in %s\n", conf_file);
return -1;
}
val = json_object_get_value(conf_obj, "lorawan_public"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONBoolean) {
boardconf.lorawan_public = (bool)json_value_get_boolean(val);
} else {
MSG("WARNING: Data type for lorawan_public seems wrong, please check\n");
boardconf.lorawan_public = false;
}
val = json_object_get_value(conf_obj, "clksrc"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONNumber) {
boardconf.clksrc = (uint8_t)json_value_get_number(val);
} else {
MSG("WARNING: Data type for clksrc seems wrong, please check\n");
boardconf.clksrc = 0;
}
val = json_object_get_value(conf_obj, "full_duplex"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONBoolean) {
boardconf.full_duplex = (bool)json_value_get_boolean(val);
} else {
MSG("WARNING: Data type for full_duplex seems wrong, please check\n");
boardconf.full_duplex = false;
}
MSG("INFO: spidev_path %s, lorawan_public %d, clksrc %d, full_duplex %d\n", boardconf.spidev_path, boardconf.lorawan_public, boardconf.clksrc, boardconf.full_duplex);
/* all parameters parsed, submitting configuration to the HAL */
if (lgw_board_setconf(&boardconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: Failed to configure board\n");
return -1;
}
/* set antenna gain configuration */
val = json_object_get_value(conf_obj, "antenna_gain"); /* fetch value (if possible) */
if (val != NULL) {
if (json_value_get_type(val) == JSONNumber) {
antenna_gain = (int8_t)json_value_get_number(val);
} else {
MSG("WARNING: Data type for antenna_gain seems wrong, please check\n");
antenna_gain = 0;
}
}
MSG("INFO: antenna_gain %d dBi\n", antenna_gain);
/* set timestamp configuration */
conf_ts_obj = json_object_get_object(conf_obj, "precision_timestamp");
if (conf_ts_obj == NULL) {
MSG("INFO: %s does not contain a JSON object for precision timestamp\n", conf_file);
} else {
val = json_object_get_value(conf_ts_obj, "enable"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONBoolean) {
tsconf.enable_precision_ts = (bool)json_value_get_boolean(val);
} else {
MSG("WARNING: Data type for precision_timestamp.enable seems wrong, please check\n");
tsconf.enable_precision_ts = false;
}
if (tsconf.enable_precision_ts == true) {
val = json_object_get_value(conf_ts_obj, "max_ts_metrics"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONNumber) {
tsconf.max_ts_metrics = (uint8_t)json_value_get_number(val);
} else {
MSG("WARNING: Data type for precision_timestamp.max_ts_metrics seems wrong, please check\n");
tsconf.max_ts_metrics = 0xFF;
}
val = json_object_get_value(conf_ts_obj, "nb_symbols"); /* fetch value (if possible) */
if (json_value_get_type(val) == JSONNumber) {
tsconf.nb_symbols = (uint8_t)json_value_get_number(val);
} else {
MSG("WARNING: Data type for precision_timestamp.nb_symbols seems wrong, please check\n");
tsconf.nb_symbols = 1;
}
MSG("INFO: Configuring precision timestamp: max_ts_metrics:%u, nb_symbols:%u\n", tsconf.max_ts_metrics, tsconf.nb_symbols);
/* all parameters parsed, submitting configuration to the HAL */
if (lgw_timestamp_setconf(&tsconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: Failed to configure precision timestamp\n");
return -1;
}
} else {
MSG("INFO: Configuring legacy timestamp\n");
}
}
/* set configuration for RF chains */
for (i = 0; i < LGW_RF_CHAIN_NB; ++i) {
memset(&rfconf, 0, sizeof rfconf); /* initialize configuration structure */
snprintf(param_name, sizeof param_name, "radio_%i", i); /* compose parameter path inside JSON structure */
val = json_object_get_value(conf_obj, param_name); /* fetch value (if possible) */
if (json_value_get_type(val) != JSONObject) {
MSG("INFO: no configuration for radio %i\n", i);
continue;
}
/* there is an object to configure that radio, let's parse it */
snprintf(param_name, sizeof param_name, "radio_%i.enable", i);
val = json_object_dotget_value(conf_obj, param_name);
if (json_value_get_type(val) == JSONBoolean) {
rfconf.enable = (bool)json_value_get_boolean(val);
} else {
rfconf.enable = false;
}
if (rfconf.enable == false) { /* radio disabled, nothing else to parse */
MSG("INFO: radio %i disabled\n", i);
} else { /* radio enabled, will parse the other parameters */
snprintf(param_name, sizeof param_name, "radio_%i.freq", i);
rfconf.freq_hz = (uint32_t)json_object_dotget_number(conf_obj, param_name);
if (fs_flag == 1) {
if (rfconf.freq_hz == 923600000) {
rfconf.freq_hz = 917100000;
fs_active = 1;
}
if (rfconf.freq_hz == 924300000) {
rfconf.freq_hz = 917900000;
fs_active = 1;
}
}
snprintf(param_name, sizeof param_name, "radio_%i.rssi_offset", i);
rfconf.rssi_offset = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.rssi_tcomp.coeff_a", i);
rfconf.rssi_tcomp.coeff_a = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.rssi_tcomp.coeff_b", i);
rfconf.rssi_tcomp.coeff_b = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.rssi_tcomp.coeff_c", i);
rfconf.rssi_tcomp.coeff_c = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.rssi_tcomp.coeff_d", i);
rfconf.rssi_tcomp.coeff_d = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.rssi_tcomp.coeff_e", i);
rfconf.rssi_tcomp.coeff_e = (float)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.type", i);
str = json_object_dotget_string(conf_obj, param_name);
if (!strncmp(str, "SX1255", 6)) {
rfconf.type = LGW_RADIO_TYPE_SX1255;
} else if (!strncmp(str, "SX1257", 6)) {
rfconf.type = LGW_RADIO_TYPE_SX1257;
} else if (!strncmp(str, "SX1250", 6)) {
rfconf.type = LGW_RADIO_TYPE_SX1250;
} else {
MSG("WARNING: invalid radio type: %s (should be SX1255 or SX1257 or SX1250)\n", str);
}
snprintf(param_name, sizeof param_name, "radio_%i.single_input_mode", i);
val = json_object_dotget_value(conf_obj, param_name);
if (json_value_get_type(val) == JSONBoolean) {
rfconf.single_input_mode = (bool)json_value_get_boolean(val);
} else {
rfconf.single_input_mode = false;
}
snprintf(param_name, sizeof param_name, "radio_%i.tx_enable", i);
val = json_object_dotget_value(conf_obj, param_name);
if (json_value_get_type(val) == JSONBoolean) {
rfconf.tx_enable = (bool)json_value_get_boolean(val);
if (rfconf.tx_enable == true) {
/* tx is enabled on this rf chain, we need its frequency range */
snprintf(param_name, sizeof param_name, "radio_%i.tx_freq_min", i);
tx_freq_min[i] = (uint32_t)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "radio_%i.tx_freq_max", i);
tx_freq_max[i] = (uint32_t)json_object_dotget_number(conf_obj, param_name);
if ((tx_freq_min[i] == 0) || (tx_freq_max[i] == 0)) {
MSG("WARNING: no frequency range specified for TX rf chain %d\n", i);
}
/* set configuration for tx gains */
memset(&txlut[i], 0, sizeof txlut[i]); /* initialize configuration structure */
snprintf(param_name, sizeof param_name, "radio_%i.tx_gain_lut", i);
conf_txlut_array = json_object_dotget_array(conf_obj, param_name);
if (conf_txlut_array != NULL) {
txlut[i].size = json_array_get_count(conf_txlut_array);
/* Detect if we have a sx125x or sx1250 configuration */
conf_txgain_obj = json_array_get_object(conf_txlut_array, 0);
val = json_object_dotget_value(conf_txgain_obj, "pwr_idx");
if (val != NULL) {
printf("INFO: Configuring Tx Gain LUT for rf_chain %u with %u indexes for sx1250\n", i, txlut[i].size);
sx1250_tx_lut = true;
} else {
printf("INFO: Configuring Tx Gain LUT for rf_chain %u with %u indexes for sx125x\n", i, txlut[i].size);
sx1250_tx_lut = false;
}
/* Parse the table */
for (j = 0; j < (int)txlut[i].size; j++) {
/* Sanity check */
if (j >= TX_GAIN_LUT_SIZE_MAX) {
printf("ERROR: TX Gain LUT [%u] index %d not supported, skip it\n", i, j);
break;
}
/* Get TX gain object from LUT */
conf_txgain_obj = json_array_get_object(conf_txlut_array, j);
/* rf power */
val = json_object_dotget_value(conf_txgain_obj, "rf_power");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].rf_power = (int8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "rf_power", j);
txlut[i].lut[j].rf_power = 0;
}
/* PA gain */
val = json_object_dotget_value(conf_txgain_obj, "pa_gain");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].pa_gain = (uint8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "pa_gain", j);
txlut[i].lut[j].pa_gain = 0;
}
if (sx1250_tx_lut == false) {
/* DIG gain */
val = json_object_dotget_value(conf_txgain_obj, "dig_gain");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].dig_gain = (uint8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "dig_gain", j);
txlut[i].lut[j].dig_gain = 0;
}
/* DAC gain */
val = json_object_dotget_value(conf_txgain_obj, "dac_gain");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].dac_gain = (uint8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "dac_gain", j);
txlut[i].lut[j].dac_gain = 3; /* This is the only dac_gain supported for now */
}
/* MIX gain */
val = json_object_dotget_value(conf_txgain_obj, "mix_gain");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].mix_gain = (uint8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "mix_gain", j);
txlut[i].lut[j].mix_gain = 0;
}
} else {
/* TODO: rework this, should not be needed for sx1250 */
txlut[i].lut[j].mix_gain = 5;
/* power index */
val = json_object_dotget_value(conf_txgain_obj, "pwr_idx");
if (json_value_get_type(val) == JSONNumber) {
txlut[i].lut[j].pwr_idx = (uint8_t)json_value_get_number(val);
} else {
printf("WARNING: Data type for %s[%d] seems wrong, please check\n", "pwr_idx", j);
txlut[i].lut[j].pwr_idx = 0;
}
}
}
/* all parameters parsed, submitting configuration to the HAL */
if (txlut[i].size > 0) {
if (lgw_txgain_setconf(i, &txlut[i]) != LGW_HAL_SUCCESS) {
MSG("ERROR: Failed to configure concentrator TX Gain LUT for rf_chain %u\n", i);
return -1;
}
} else {
MSG("WARNING: No TX gain LUT defined for rf_chain %u\n", i);
}
} else {
MSG("WARNING: No TX gain LUT defined for rf_chain %u\n", i);
}
}
} else {
rfconf.tx_enable = false;
}
MSG("INFO: radio %i enabled (type %s), center frequency %u, RSSI offset %f, tx enabled %d, single input mode %d\n", i, str, rfconf.freq_hz, rfconf.rssi_offset, rfconf.tx_enable, rfconf.single_input_mode);
}
/* all parameters parsed, submitting configuration to the HAL */
if (lgw_rxrf_setconf(i, &rfconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: invalid configuration for radio %i\n", i);
return -1;
}
}
/* set configuration for Lora multi-SF channels (bandwidth cannot be set) */
for (i = 0; i < LGW_MULTI_NB; ++i) {
memset(&ifconf, 0, sizeof ifconf); /* initialize configuration structure */
snprintf(param_name, sizeof param_name, "chan_multiSF_%i", i); /* compose parameter path inside JSON structure */
val = json_object_get_value(conf_obj, param_name); /* fetch value (if possible) */
if (json_value_get_type(val) != JSONObject) {
MSG("INFO: no configuration for Lora multi-SF channel %i\n", i);
continue;
}
/* there is an object to configure that Lora multi-SF channel, let's parse it */
snprintf(param_name, sizeof param_name, "chan_multiSF_%i.enable", i);
val = json_object_dotget_value(conf_obj, param_name);
if (json_value_get_type(val) == JSONBoolean) {
ifconf.enable = (bool)json_value_get_boolean(val);
} else {
ifconf.enable = false;
}
if (ifconf.enable == false) { /* Lora multi-SF channel disabled, nothing else to parse */
MSG("INFO: Lora multi-SF channel %i disabled\n", i);
} else { /* Lora multi-SF channel enabled, will parse the other parameters */
snprintf(param_name, sizeof param_name, "chan_multiSF_%i.radio", i);
ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf_obj, param_name);
snprintf(param_name, sizeof param_name, "chan_multiSF_%i.if", i);
ifconf.freq_hz = (int32_t)json_object_dotget_number(conf_obj, param_name);
// TODO: handle individual SF enabling and disabling (spread_factor)
MSG("INFO: Lora multi-SF channel %i> radio %i, IF %i Hz, 125 kHz bw, SF 5 to 12\n", i, ifconf.rf_chain, ifconf.freq_hz);
}
/* all parameters parsed, submitting configuration to the HAL */
if (lgw_rxif_setconf(i, &ifconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: invalid configuration for Lora multi-SF channel %i\n", i);
return -1;
}
}
/* set configuration for Lora standard channel */
memset(&ifconf, 0, sizeof ifconf); /* initialize configuration structure */
val = json_object_get_value(conf_obj, "chan_Lora_std"); /* fetch value (if possible) */
if (json_value_get_type(val) != JSONObject) {
MSG("INFO: no configuration for Lora standard channel\n");
} else {
val = json_object_dotget_value(conf_obj, "chan_Lora_std.enable");
if (json_value_get_type(val) == JSONBoolean) {
ifconf.enable = (bool)json_value_get_boolean(val);
} else {
ifconf.enable = false;
}
if (ifconf.enable == false) {
MSG("INFO: Lora standard channel %i disabled\n", i);
} else {
ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf_obj, "chan_Lora_std.radio");
ifconf.freq_hz = (int32_t)json_object_dotget_number(conf_obj, "chan_Lora_std.if");
bw = (uint32_t)json_object_dotget_number(conf_obj, "chan_Lora_std.bandwidth");
switch(bw) {
case 500000: ifconf.bandwidth = BW_500KHZ; break;
case 250000: ifconf.bandwidth = BW_250KHZ; break;
case 125000: ifconf.bandwidth = BW_125KHZ; break;
default: ifconf.bandwidth = BW_UNDEFINED;
}
sf = (uint32_t)json_object_dotget_number(conf_obj, "chan_Lora_std.spread_factor");
switch(sf) {
case 5: ifconf.datarate = DR_LORA_SF5; break;
case 6: ifconf.datarate = DR_LORA_SF6; break;
case 7: ifconf.datarate = DR_LORA_SF7; break;
case 8: ifconf.datarate = DR_LORA_SF8; break;
case 9: ifconf.datarate = DR_LORA_SF9; break;
case 10: ifconf.datarate = DR_LORA_SF10; break;
case 11: ifconf.datarate = DR_LORA_SF11; break;
case 12: ifconf.datarate = DR_LORA_SF12; break;
default: ifconf.datarate = DR_UNDEFINED;
}
val = json_object_dotget_value(conf_obj, "chan_Lora_std.implicit_hdr");
if (json_value_get_type(val) == JSONBoolean) {
ifconf.implicit_hdr = (bool)json_value_get_boolean(val);
} else {
ifconf.implicit_hdr = false;
}
if (ifconf.implicit_hdr == true) {
val = json_object_dotget_value(conf_obj, "chan_Lora_std.implicit_payload_length");
if (json_value_get_type(val) == JSONNumber) {
ifconf.implicit_payload_length = (uint8_t)json_value_get_number(val);
} else {
MSG("ERROR: payload length setting is mandatory for implicit header mode\n");
return -1;
}
val = json_object_dotget_value(conf_obj, "chan_Lora_std.implicit_crc_en");
if (json_value_get_type(val) == JSONBoolean) {
ifconf.implicit_crc_en = (bool)json_value_get_boolean(val);
} else {
MSG("ERROR: CRC enable setting is mandatory for implicit header mode\n");
return -1;
}
val = json_object_dotget_value(conf_obj, "chan_Lora_std.implicit_coderate");
if (json_value_get_type(val) == JSONNumber) {
ifconf.implicit_coderate = (uint8_t)json_value_get_number(val);
} else {
MSG("ERROR: coding rate setting is mandatory for implicit header mode\n");
return -1;
}
}
MSG("INFO: Lora std channel> radio %i, IF %i Hz, %u Hz bw, SF %u, %s\n", ifconf.rf_chain, ifconf.freq_hz, bw, sf, (ifconf.implicit_hdr == true) ? "Implicit header" : "Explicit header");
}
if (lgw_rxif_setconf(8, &ifconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: invalid configuration for Lora standard channel\n");
return -1;
}
}
/* set configuration for FSK channel */
memset(&ifconf, 0, sizeof ifconf); /* initialize configuration structure */
val = json_object_get_value(conf_obj, "chan_FSK"); /* fetch value (if possible) */
if (json_value_get_type(val) != JSONObject) {
MSG("INFO: no configuration for FSK channel\n");
} else {
val = json_object_dotget_value(conf_obj, "chan_FSK.enable");
if (json_value_get_type(val) == JSONBoolean) {
ifconf.enable = (bool)json_value_get_boolean(val);
} else {
ifconf.enable = false;
}
if (ifconf.enable == false) {
MSG("INFO: FSK channel %i disabled\n", i);
} else {
ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf_obj, "chan_FSK.radio");
ifconf.freq_hz = (int32_t)json_object_dotget_number(conf_obj, "chan_FSK.if");
bw = (uint32_t)json_object_dotget_number(conf_obj, "chan_FSK.bandwidth");
fdev = (uint32_t)json_object_dotget_number(conf_obj, "chan_FSK.freq_deviation");
ifconf.datarate = (uint32_t)json_object_dotget_number(conf_obj, "chan_FSK.datarate");
/* if chan_FSK.bandwidth is set, it has priority over chan_FSK.freq_deviation */
if ((bw == 0) && (fdev != 0)) {
bw = 2 * fdev + ifconf.datarate;
}
if (bw == 0) ifconf.bandwidth = BW_UNDEFINED;
#if 0 /* TODO */
else if (bw <= 7800) ifconf.bandwidth = BW_7K8HZ;
else if (bw <= 15600) ifconf.bandwidth = BW_15K6HZ;
else if (bw <= 31200) ifconf.bandwidth = BW_31K2HZ;
else if (bw <= 62500) ifconf.bandwidth = BW_62K5HZ;
#endif
else if (bw <= 125000) ifconf.bandwidth = BW_125KHZ;
else if (bw <= 250000) ifconf.bandwidth = BW_250KHZ;
else if (bw <= 500000) ifconf.bandwidth = BW_500KHZ;
else ifconf.bandwidth = BW_UNDEFINED;
MSG("INFO: FSK channel> radio %i, IF %i Hz, %u Hz bw, %u bps datarate\n", ifconf.rf_chain, ifconf.freq_hz, bw, ifconf.datarate);
}
if (lgw_rxif_setconf(9, &ifconf) != LGW_HAL_SUCCESS) {
MSG("ERROR: invalid configuration for FSK channel\n");
return -1;
}
}
json_value_free(root_val);
return 0;
}
static int parse_gateway_configuration(const char * conf_file) {
const char conf_obj_name[] = "gateway_conf";
JSON_Value *root_val;
JSON_Object *conf_obj = NULL;
JSON_Value *val = NULL; /* needed to detect the absence of some fields */
const char *str; /* pointer to sub-strings in the JSON data */
unsigned long long ull = 0;
/* try to parse JSON */
root_val = json_parse_file_with_comments(conf_file);
if (root_val == NULL) {
MSG("ERROR: %s is not a valid JSON file\n", conf_file);
exit(EXIT_FAILURE);
}
/* point to the gateway configuration object */
conf_obj = json_object_get_object(json_value_get_object(root_val), conf_obj_name);
if (conf_obj == NULL) {
MSG("INFO: %s does not contain a JSON object named %s\n", conf_file, conf_obj_name);
return -1;
} else {
MSG("INFO: %s does contain a JSON object named %s, parsing gateway parameters\n", conf_file, conf_obj_name);
}
/* gateway unique identifier (aka MAC address) (optional) */
str = json_object_get_string(conf_obj, "gateway_ID");
if (str != NULL) {
sscanf(str, "%llx", &ull);
lgwm = ull;
MSG("INFO: gateway MAC address is configured to %016llX\n", ull);
}
/* server hostname or IP address (optional) */
str = json_object_get_string(conf_obj, "server_address");
if (str != NULL) {
strncpy(serv_addr, str, sizeof serv_addr);
serv_addr[sizeof serv_addr - 1] = '\0'; /* ensure string termination */
MSG("INFO: server hostname or IP address is configured to \"%s\"\n", serv_addr);
}
/* get up and down ports (optional) */
val = json_object_get_value(conf_obj, "serv_port_up");
if (val != NULL) {
snprintf(serv_port_up, sizeof serv_port_up, "%u", (uint16_t)json_value_get_number(val));
MSG("INFO: upstream port is configured to \"%s\"\n", serv_port_up);
}
val = json_object_get_value(conf_obj, "serv_port_down");
if (val != NULL) {
snprintf(serv_port_down, sizeof serv_port_down, "%u", (uint16_t)json_value_get_number(val));
MSG("INFO: downstream port is configured to \"%s\"\n", serv_port_down);
}
/* get keep-alive interval (in seconds) for downstream (optional) */
val = json_object_get_value(conf_obj, "keepalive_interval");
if (val != NULL) {
keepalive_time = (int)json_value_get_number(val);
MSG("INFO: downstream keep-alive interval is configured to %u seconds\n", keepalive_time);
}
/* get interval (in seconds) for statistics display (optional) */
val = json_object_get_value(conf_obj, "stat_interval");
if (val != NULL) {
stat_interval = (unsigned)json_value_get_number(val);
MSG("INFO: statistics display interval is configured to %u seconds\n", stat_interval);
}
/* get time-out value (in ms) for upstream datagrams (optional) */
val = json_object_get_value(conf_obj, "push_timeout_ms");
if (val != NULL) {
push_timeout_half.tv_usec = 500 * (long int)json_value_get_number(val);
MSG("INFO: upstream PUSH_DATA time-out is configured to %u ms\n", (unsigned)(push_timeout_half.tv_usec / 500));
}
/* packet filtering parameters */
val = json_object_get_value(conf_obj, "forward_crc_valid");
if (json_value_get_type(val) == JSONBoolean) {
fwd_valid_pkt = (bool)json_value_get_boolean(val);
}
MSG("INFO: packets received with a valid CRC will%s be forwarded\n", (fwd_valid_pkt ? "" : " NOT"));
val = json_object_get_value(conf_obj, "forward_crc_error");
if (json_value_get_type(val) == JSONBoolean) {
fwd_error_pkt = (bool)json_value_get_boolean(val);
}
MSG("INFO: packets received with a CRC error will%s be forwarded\n", (fwd_error_pkt ? "" : " NOT"));
val = json_object_get_value(conf_obj, "forward_crc_disabled");
if (json_value_get_type(val) == JSONBoolean) {
fwd_nocrc_pkt = (bool)json_value_get_boolean(val);
}
MSG("INFO: packets received with no CRC will%s be forwarded\n", (fwd_nocrc_pkt ? "" : " NOT"));
/* GPS module TTY or I2C path (optional) */
if (json_object_get_string(conf_obj, "gps_tty_path") && json_object_get_string(conf_obj, "gps_i2c_path")) {
MSG("ERROR: 'gps_i2c_path' and 'gps_tty_path' are mutually exclusive, pick only one\n");
exit(EXIT_FAILURE);
}
str = json_object_get_string(conf_obj, "gps_tty_path");
if (str != NULL) {
strncpy(gps_dev_path, str, sizeof gps_dev_path);
gps_dev_path[sizeof gps_dev_path - 1] = '\0'; /* ensure string termination */
gps_dev = gps_dev_tty;
MSG("INFO: GPS serial port path is configured to \"%s\"\n", gps_dev_path);
}
str = json_object_get_string(conf_obj, "gps_i2c_path");
if (str != NULL) {
strncpy(gps_dev_path, str, sizeof gps_dev_path);
gps_dev_path[sizeof gps_dev_path - 1] = '\0'; /* ensure string termination */
gps_dev = gps_dev_i2c;
MSG("INFO: GPS I2C path is configured to \"%s\"\n", gps_dev_path);
}
/* get reference coordinates */
val = json_object_get_value(conf_obj, "ref_latitude");
if (val != NULL) {
reference_coord.lat = (double)json_value_get_number(val);
MSG("INFO: Reference latitude is configured to %f deg\n", reference_coord.lat);
}
val = json_object_get_value(conf_obj, "ref_longitude");
if (val != NULL) {
reference_coord.lon = (double)json_value_get_number(val);
MSG("INFO: Reference longitude is configured to %f deg\n", reference_coord.lon);
}
val = json_object_get_value(conf_obj, "ref_altitude");
if (val != NULL) {
reference_coord.alt = (short)json_value_get_number(val);
MSG("INFO: Reference altitude is configured to %i meters\n", reference_coord.alt);
}
/* Gateway GPS coordinates hardcoding (aka. faking) option */
val = json_object_get_value(conf_obj, "fake_gps");
if (json_value_get_type(val) == JSONBoolean) {
gps_fake_enable = (bool)json_value_get_boolean(val);
if (gps_fake_enable == true) {
MSG("INFO: fake GPS is enabled\n");
} else {
MSG("INFO: fake GPS is disabled\n");
}
}
/* Beacon signal period (optional) */
val = json_object_get_value(conf_obj, "beacon_period");
if (val != NULL) {
beacon_period = (uint32_t)json_value_get_number(val);
if ((beacon_period > 0) && (beacon_period < 6)) {
MSG("ERROR: invalid configuration for Beacon period, must be >= 6s\n");
return -1;
} else {
MSG("INFO: Beaconing period is configured to %u seconds\n", beacon_period);
}
}
/* Beacon TX frequency (optional) */
val = json_object_get_value(conf_obj, "beacon_freq_hz");
if (val != NULL) {
beacon_freq_hz = (uint32_t)json_value_get_number(val);
MSG("INFO: Beaconing signal will be emitted at %u Hz\n", beacon_freq_hz);
}
/* Number of beacon channels (optional) */
val = json_object_get_value(conf_obj, "beacon_freq_nb");
if (val != NULL) {
beacon_freq_nb = (uint8_t)json_value_get_number(val);
MSG("INFO: Beaconing channel number is set to %u\n", beacon_freq_nb);
}
/* Frequency step between beacon channels (optional) */
val = json_object_get_value(conf_obj, "beacon_freq_step");
if (val != NULL) {
beacon_freq_step = (uint32_t)json_value_get_number(val);
MSG("INFO: Beaconing channel frequency step is set to %uHz\n", beacon_freq_step);
}
/* Beacon datarate (optional) */
val = json_object_get_value(conf_obj, "beacon_datarate");
if (val != NULL) {
beacon_datarate = (uint8_t)json_value_get_number(val);
MSG("INFO: Beaconing datarate is set to SF%d\n", beacon_datarate);
}
/* Beacon modulation bandwidth (optional) */
val = json_object_get_value(conf_obj, "beacon_bw_hz");
if (val != NULL) {
beacon_bw_hz = (uint32_t)json_value_get_number(val);
MSG("INFO: Beaconing modulation bandwidth is set to %dHz\n", beacon_bw_hz);
}
/* Beacon TX power (optional) */
val = json_object_get_value(conf_obj, "beacon_power");
if (val != NULL) {
beacon_power = (int8_t)json_value_get_number(val);
MSG("INFO: Beaconing TX power is set to %ddBm\n", beacon_power);
}
/* Beacon information descriptor (optional) */
val = json_object_get_value(conf_obj, "beacon_infodesc");