summaryrefslogtreecommitdiff
path: root/util_tx_test/src/util_tx_test.c
blob: a59e5e535ef6caffa40b26a1c3b69b7d46cd1f41 (plain)
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
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
  (C)2013 Semtech-Cycleo

Description:
    Send a bunch of packets on a settable frequency

License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Sylvain Miermont
*/


/* -------------------------------------------------------------------------- */
/* --- 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 sprintf fopen fputs */

#include <string.h>     /* memset */
#include <signal.h>     /* sigaction */
#include <unistd.h>     /* getopt access */
#include <stdlib.h>     /* exit codes */
#include <getopt.h>     /* getopt_long */

#include "loragw_hal.h"
#include "loragw_reg.h"
#include "loragw_aux.h"
#include "loragw_spi.h"

/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define MSG(args...) fprintf(stderr, args) /* message that is destined to the user */

/* -------------------------------------------------------------------------- */
/* --- PRIVATE CONSTANTS ---------------------------------------------------- */

#define TX_RF_CHAIN                 0 /* TX only supported on radio A */
#define DEFAULT_RSSI_OFFSET         0.0
#define DEFAULT_MODULATION          "LORA"
#define DEFAULT_BR_KBPS             50
#define DEFAULT_FDEV_KHZ            25
#define DEFAULT_NOTCH_FREQ          129000U /* 129 kHz */
#define DEFAULT_SX127X_RSSI_OFFSET  -4 /* dB */

/* -------------------------------------------------------------------------- */
/* --- PRIVATE VARIABLES (GLOBAL) ------------------------------------------- */

/* signal handling variables */
struct sigaction sigact; /* SIGQUIT&SIGINT&SIGTERM signal handling */
static int exit_sig = 0; /* 1 -> application terminates cleanly (shut down hardware, close open files, etc) */
static int quit_sig = 0; /* 1 -> application terminates without shutting down the hardware */

/* TX gain LUT table */
static struct lgw_tx_gain_lut_s txgain_lut = {
    .size = 5,
    .lut[0] = {
        .dig_gain = 0,
        .pa_gain = 0,
        .dac_gain = 3,
        .mix_gain = 12,
        .rf_power = 0
    },
    .lut[1] = {
        .dig_gain = 0,
        .pa_gain = 1,
        .dac_gain = 3,
        .mix_gain = 12,
        .rf_power = 10
    },
    .lut[2] = {
        .dig_gain = 0,
        .pa_gain = 2,
        .dac_gain = 3,
        .mix_gain = 10,
        .rf_power = 14
    },
    .lut[3] = {
        .dig_gain = 0,
        .pa_gain = 3,
        .dac_gain = 3,
        .mix_gain = 9,
        .rf_power = 20
    },
    .lut[4] = {
        .dig_gain = 0,
        .pa_gain = 3,
        .dac_gain = 3,
        .mix_gain = 14,
        .rf_power = 27
    }};

/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */

static void sig_handler(int sigio);

void usage (void);

/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */

static void sig_handler(int sigio) {
    if (sigio == SIGQUIT) {
        quit_sig = 1;;
    } else if ((sigio == SIGINT) || (sigio == SIGTERM)) {
        exit_sig = 1;
    }
}

/* describe command line options */
void usage(void) {
    int i;

    printf("*** Library version information ***\n%s\n\n", lgw_version_info());
    printf("Available options:\n");
    printf(" -h                 print this help\n");
    printf(" -r         <int>   radio type (SX1255:1255, SX1257:1257)\n");
    printf(" -n         <uint>  TX notch filter frequency in kHz [126..250]\n");
    printf(" -f         <float> target frequency in MHz\n");
    printf(" -k         <uint>  concentrator clock source (0:Radio A, 1:Radio B)\n");
    printf(" -m         <str>   modulation type ['LORA', 'FSK']\n");
    printf(" -b         <uint>  LoRa bandwidth in kHz [125, 250, 500]\n");
    printf(" -s         <uint>  LoRa Spreading Factor [7-12]\n");
    printf(" -c         <uint>  LoRa Coding Rate [1-4]\n");
    printf(" -d         <uint>  FSK frequency deviation in kHz [1:250]\n");
    printf(" -q         <float> FSK bitrate in kbps [0.5:250]\n");
    printf(" -p         <int>   RF power (dBm) [ ");
    for (i = 0; i < txgain_lut.size; i++) {
        printf("%ddBm ", txgain_lut.lut[i].rf_power);
    }
    printf("]\n");
    printf(" -l         <uint>  LoRa preamble length (symbols)\n");
    printf(" -z         <uint>  payload size (bytes, <256)\n");
    printf(" -i                 send packet using inverted modulation polarity\n");
    printf(" -t         <uint>  pause between packets (ms)\n");
    printf(" -x         <int>   nb of times the sequence is repeated (-1 loop until stopped)\n");
    printf(" --lbt-freq         <float>  lbt first channel frequency in MHz\n");
    printf(" --lbt-nbch         <uint>   lbt number of channels [1..8]\n");
    printf(" --lbt-sctm         <uint>   lbt scan time in usec to be applied to all channels [128, 5000]\n");
    printf(" --lbt-rssi         <int>    lbt rssi target in dBm [-128..0]\n");
    printf(" --lbt-rssi-offset  <int>    rssi offset in dB to be applied to SX127x RSSI [-128..127]\n");
    printf(" --pa               <uint>   pa gain\n");
    printf(" --mix              <uint>   mix gain\n");
    printf(" --dig              <uint>   dig gain\n");
    printf(" --dac              <uint>   dac gain\n");
    printf(" --path             <string> path of SPIDEV e.g. /dev/spidev0.0\n");
}

/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */

int main(int argc, char **argv)
{
    int i;
    uint8_t status_var;

    /* user entry parameters */
    int xi = 0;
    unsigned int xu = 0;
    double xd = 0.0;
    float xf = 0.0;
    char arg_s[64];

    /* application parameters */
    char mod[64] = DEFAULT_MODULATION;
    uint32_t f_target = 0; /* target frequency - invalid default value, has to be specified by user */
    int sf = 10; /* SF10 by default */
    int cr = 1; /* CR1 aka 4/5 by default */
    int bw = 125; /* 125kHz bandwidth by default */
    int pow = 14; /* 14 dBm by default */
    int preamb = 8; /* 8 symbol preamble by default */
    int pl_size = 16; /* 16 bytes payload by default */
    int delay = 1000; /* 1 second between packets by default */
    int repeat = -1; /* by default, repeat until stopped */
    bool invert = false;
    float br_kbps = DEFAULT_BR_KBPS;
    uint8_t fdev_khz = DEFAULT_FDEV_KHZ;
    bool lbt_enable = false;
    uint32_t lbt_f_target = 0;
    uint32_t lbt_sc_time = 5000;
    int8_t lbt_rssi_target_dBm = -80;
    int8_t lbt_rssi_offset_dB = DEFAULT_SX127X_RSSI_OFFSET;
    uint8_t  lbt_nb_channel = 1;
    uint32_t sx1301_count_us;
    uint32_t tx_notch_freq = DEFAULT_NOTCH_FREQ;
    bool force_tx_lut = false;
    uint8_t dig_gain = 0;   /*!> 2 bits, control of the digital gain of SX1301 */
    uint8_t pa_gain = 0;    /*!> 2 bits, control of the external PA (SX1301 I/O) */
    uint8_t dac_gain = 3;   /*!> 2 bits, control of the radio DAC */
    uint8_t mix_gain = 0;   /*!> 4 bits, control of the radio mixer */

    /* RF configuration (TX fail if RF chain is not enabled) */
    enum lgw_radio_type_e radio_type = LGW_RADIO_TYPE_NONE;
    uint8_t clocksource = 1; /* Radio B is source by default */
    struct lgw_conf_board_s boardconf;
    struct lgw_conf_lbt_s lbtconf;
    struct lgw_conf_rxrf_s rfconf;

    /* allocate memory for packet sending */
    struct lgw_pkt_tx_s txpkt; /* array containing 1 outbound packet + metadata */

    /* loop variables (also use as counters in the packet payload) */
    uint16_t cycle_count = 0;

    /* Parameter parsing */
    int option_index = 0;
    static struct option long_options[] = {
        {"lbt-freq", required_argument, 0, 0},
        {"lbt-sctm", required_argument, 0, 0},
        {"lbt-rssi", required_argument, 0, 0},
        {"lbt-nbch", required_argument, 0, 0},
        {"lbt-rssi-offset", required_argument, 0, 0},
        {"path", required_argument, 0, 0},
        {"pa", required_argument, 0, 0},
        {"mix", required_argument, 0, 0},
        {"dig", required_argument, 0, 0},
        {"dac", required_argument, 0, 0},
        {0, 0, 0, 0}
    };

    /* parse command line options */
    while ((i = getopt_long (argc, argv, "hif:n:m:b:s:c:p:l:z:t:x:r:k:d:q:", long_options, &option_index)) != -1) {
        switch (i) {
            case 'h':
                usage();
                return EXIT_FAILURE;
                break;

            case 'f': /* <float> Target frequency in MHz */
                i = sscanf(optarg, "%lf", &xd);
                if ((i != 1) || (xd < 30.0) || (xd > 3000.0)) {
                    MSG("ERROR: invalid TX frequency\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    f_target = (uint32_t)((xd*1e6) + 0.5); /* .5 Hz offset to get rounding instead of truncating */
                }
                break;

            case 'n': /* <uint> TX notch filter frequency in kHz */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || ((xi < 126) || (xi > 250))) {
                    MSG("ERROR: invalid TX notch filter frequency\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    tx_notch_freq = xi*1000;
                }
                break;

            case 'm': /* <str> Modulation type */
                i = sscanf(optarg, "%s", arg_s);
                if ((i != 1) || ((strcmp(arg_s,"LORA") != 0) && (strcmp(arg_s,"FSK")))) {
                    MSG("ERROR: invalid modulation type\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    sprintf(mod, "%s", arg_s);
                }
                break;

            case 'b': /* <int> Modulation bandwidth in kHz */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || ((xi != 125) && (xi != 250) && (xi != 500))) {
                    MSG("ERROR: invalid LoRa bandwidth\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    bw = xi;
                }
                break;

            case 's': /* <int> Spreading Factor */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < 7) || (xi > 12)) {
                    MSG("ERROR: invalid spreading factor\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    sf = xi;
                }
                break;

            case 'c': /* <int> Coding Rate */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < 1) || (xi > 4)) {
                    MSG("ERROR: invalid coding rate\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    cr = xi;
                }
                break;

            case 'p': /* <int> RF power */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < -60) || (xi > 60)) {
                    MSG("ERROR: invalid RF power\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    pow = xi;
                }
                break;

            case 'd': /* <uint> FSK frequency deviation */
                i = sscanf(optarg, "%u", &xu);
                if ((i != 1) || (xu < 1) || (xu > 250)) {
                    MSG("ERROR: invalid FSK frequency deviation\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    fdev_khz = (uint8_t)xu;
                }
                break;

            case 'q': /* <float> FSK bitrate */
                i = sscanf(optarg, "%f", &xf);
                if ((i != 1) || (xf < 0.5) || (xf > 250)) {
                    MSG("ERROR: invalid FSK bitrate\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    br_kbps = xf;
                }
                break;

            case 'l': /* <uint> preamble length (symbols) */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < 6)) {
                    MSG("ERROR: preamble length must be >6 symbols \n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    preamb = xi;
                }
                break;

            case 'z': /* <uint> payload length (bytes) */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi <= 0)) {
                    MSG("ERROR: invalid payload size\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    pl_size = xi;
                }
                break;

            case 't': /* <int> pause between packets (ms) */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < 0)) {
                    MSG("ERROR: invalid time between packets\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    delay = xi;
                }
                break;

            case 'x': /* <int> numbers of times the sequence is repeated */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || (xi < -1)) {
                    MSG("ERROR: invalid number of repeats\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    repeat = xi;
                }
                break;

            case 'r': /* <int> Radio type (1255, 1257) */
                sscanf(optarg, "%i", &xi);
                switch (xi) {
                    case 1255:
                        radio_type = LGW_RADIO_TYPE_SX1255;
                        break;
                    case 1257:
                        radio_type = LGW_RADIO_TYPE_SX1257;
                        break;
                    default:
                        printf("ERROR: invalid radio type\n");
                        usage();
                        return EXIT_FAILURE;
                }
                break;

            case 'i': /* Send packet using inverted modulation polarity */
                invert = true;
                break;

            case 'k': /* <int> Concentrator clock source (Radio A or Radio B) */
                i = sscanf(optarg, "%i", &xi);
                if ((i != 1) || ((xi != 0) && (xi != 1))) {
                    MSG("ERROR: invalid clock source\n");
                    usage();
                    return EXIT_FAILURE;
                } else {
                    clocksource = (uint8_t)xi;
                }
                break;

            case 0:
                if( strcmp(long_options[option_index].name, "lbt-freq") == 0 ) { /* <float> LBT first channel frequency in MHz */
                    i = sscanf(optarg, "%lf", &xd);
                    if ((i != 1) || (xd < 30.0) || (xd > 3000.0)) {
                        MSG("ERROR: invalid LBT start frequency\n");
                        usage();
                        return EXIT_FAILURE;
                    } else {
                        lbt_f_target = (uint32_t)((xd*1e6) + 0.5); /* .5 Hz offset to get rounding instead of truncating */
                        lbt_enable = true;
                    }
                } else if( strcmp(long_options[option_index].name, "lbt-sctm") == 0 ) { /* <int> LBT scan time in usec */
                    if (lbt_enable == true) {
                        i = sscanf(optarg, "%i", &xi);
                        if ((i != 1) || (xi < 0)) {
                            MSG("ERROR: invalid LBT scan time\n");
                            usage();
                            return EXIT_FAILURE;
                        } else {
                            lbt_sc_time = xi;
                        }
                    } else {
                        MSG("ERROR: invalid parameter, LBT start frequency must be set\n");
                        usage();
                        return EXIT_FAILURE;
                    }
                } else if( strcmp(long_options[option_index].name, "lbt-rssi") == 0 ) { /* <int> LBT RSSI target */
                    if (lbt_enable == true) {
                        i = sscanf(optarg, "%i", &xi);
                        if ((i != 1) || ((xi < -128) && (xi > 0))) {
                            MSG("ERROR: invalid LBT RSSI target\n");
                            usage();
                            return EXIT_FAILURE;
                        } else {
                            lbt_rssi_target_dBm = xi;
                        }
                    } else {
                        MSG("ERROR: invalid parameter, LBT start frequency must be set\n");
                        usage();
                        return EXIT_FAILURE;
                    }
                } else if( strcmp(long_options[option_index].name, "lbt-rssi-offset") == 0 ) { /* <int> LBT RSSI offset */
                    if (lbt_enable == true) {
                        i = sscanf(optarg, "%i", &xi);
                        if ((i != 1) || ((xi < -128) && (xi > 127))) {
                            MSG("ERROR: invalid LBT RSSI offset\n");
                            usage();
                            return EXIT_FAILURE;
                        } else {
                            lbt_rssi_offset_dB = xi;
                        }
                    } else {
                        MSG("ERROR: invalid parameter, LBT start frequency must be set\n");
                        usage();
                        return EXIT_FAILURE;
                    }
                } else if( strcmp(long_options[option_index].name, "lbt-nbch") == 0 ) { /* <int> LBT number of channels */
                    if (lbt_enable == true) {
                        i = sscanf(optarg, "%i", &xi);
                        if ((i != 1) || (xi < 0)) {
                            MSG("ERROR: invalid LBT number of channels\n");
                            usage();
                            return EXIT_FAILURE;
                        } else {
                            lbt_nb_channel = xi;
                        }
                    } else {
                        MSG("ERROR: invalid parameter, LBT start frequency must be set\n");
                        usage();
                        return EXIT_FAILURE;
                    }
                } else if( strcmp(long_options[option_index].name, "pa") == 0 ) { /* <uint> */
                    i = sscanf(optarg, "%i", &xi);
                    if ((i != 1) || (xi < 0)) {
                        MSG("ERROR: invalid PA gain\n");
                        usage();
                        return EXIT_FAILURE;
                    } else {
                        pa_gain = xi;
                        force_tx_lut = true;
                    }
                } else if( strcmp(long_options[option_index].name, "dig") == 0 ) { /* <uint> */
                    i = sscanf(optarg, "%i", &xi);
                    if ((i != 1) || (xi < 0)) {
                        MSG("ERROR: invalid Dig gain\n");
                        usage();
                        return EXIT_FAILURE;
                    } else {
                        dig_gain = xi;
                        force_tx_lut = true;
                    }
                } else if( strcmp(long_options[option_index].name, "dac") == 0 ) { /* <uint> */
                    i = sscanf(optarg, "%i", &xi);
                    if ((i != 1) || (xi < 0)) {
                        MSG("ERROR: invalid Dac gain\n");
                        usage();
                        return EXIT_FAILURE;
                    } else {
                        dac_gain = xi;
                        force_tx_lut = true;
                    }
                } else if( strcmp(long_options[option_index].name, "mix") == 0 ) { /* <uint> */
                    i = sscanf(optarg, "%i", &xi);
                    if ((i != 1) || (xi < 0)) {
                        MSG("ERROR: invalid mix gain\n");
                        usage();
                        return EXIT_FAILURE;
                    } else {
                        mix_gain = xi;
                        force_tx_lut = true;
                    }
                } else if (strcmp(long_options[option_index].name,"path") == 0) { /* <string> Path to spi device */
                    i = sscanf(optarg, "%s", arg_s);
                    if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
                        printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
                        return EXIT_FAILURE;
                    }
                    else {
                        lgw_spi_set_path(arg_s);
                    }
                }
                break;
            default:
                MSG("ERROR: argument parsing\n");
                usage();
                return EXIT_FAILURE;
        }
    }

    /* check parameter sanity */
    if (f_target == 0) {
        MSG("ERROR: frequency parameter not set, please use -f option to specify it.\n");
        return EXIT_FAILURE;
    }
    if (radio_type == LGW_RADIO_TYPE_NONE) {
        MSG("ERROR: radio type parameter not properly set, please use -r option to specify it.\n");
        return EXIT_FAILURE;
    }

    /* Summary of packet parameters */
    if (strcmp(mod, "FSK") == 0) {
        printf("Sending %i FSK packets on %u Hz (FDev %u kHz, Bitrate %.2f, %i bytes payload, %i symbols preamble) at %i dBm, with %i ms between each\n", repeat, f_target, fdev_khz, br_kbps, pl_size, preamb, pow, delay);
    } else {
        printf("Sending %i LoRa packets on %u Hz (BW %i kHz, SF %i, CR %i, %i bytes payload, %i symbols preamble) at %i dBm, with %i ms between each\n", repeat, f_target, bw, sf, cr, pl_size, preamb, pow, delay);
    }

    /* configure signal handling */
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigact.sa_handler = sig_handler;
    sigaction(SIGQUIT, &sigact, NULL);
    sigaction(SIGINT, &sigact, NULL);
    sigaction(SIGTERM, &sigact, NULL);

    /* starting the concentrator */
    /* board config */
    memset(&boardconf, 0, sizeof(boardconf));
    boardconf.lorawan_public = true;
    boardconf.clksrc = clocksource;
    lgw_board_setconf(boardconf);

    /* LBT config */
    if (lbt_enable) {
        memset(&lbtconf, 0, sizeof(lbtconf));
        lbtconf.enable = true;
        lbtconf.nb_channel = lbt_nb_channel;
        lbtconf.rssi_target = lbt_rssi_target_dBm;
        lbtconf.rssi_offset = lbt_rssi_offset_dB;
        lbtconf.channels[0].freq_hz = lbt_f_target;
        lbtconf.channels[0].scan_time_us = lbt_sc_time;
        for (i=1; i<lbt_nb_channel; i++) {
            lbtconf.channels[i].freq_hz = lbtconf.channels[i-1].freq_hz + 200E3; /* 200kHz offset for all channels */
            lbtconf.channels[i].scan_time_us = lbt_sc_time;
        }
        lgw_lbt_setconf(lbtconf);
    }

    /* RF config */
    memset(&rfconf, 0, sizeof(rfconf));
    rfconf.enable = true;
    rfconf.freq_hz = f_target;
    rfconf.rssi_offset = DEFAULT_RSSI_OFFSET;
    rfconf.type = radio_type;
    for (i = 0; i < LGW_RF_CHAIN_NB; i++) {
        if (i == TX_RF_CHAIN) {
            rfconf.tx_enable = true;
            rfconf.tx_notch_freq = tx_notch_freq;
        } else {
            rfconf.tx_enable = false;
        }
        lgw_rxrf_setconf(i, rfconf);
    }

    /* TX gain config */
    if (force_tx_lut) {
        struct lgw_tx_gain_lut_s txgain_lut_forced = {
            .size = 1,
            .lut[0] = {
                .dig_gain = dig_gain,
                .pa_gain = pa_gain,
                .dac_gain = dac_gain,
                .mix_gain = mix_gain,
                .rf_power = 0
            }
        };
        lgw_txgain_setconf(&txgain_lut_forced);
        MSG("Using forced tx gain lut: dig_gain: %d pa_gain: %d dac_gain: %d mix_gain: %d\n", dig_gain, pa_gain, dac_gain, mix_gain);
    } else {
        lgw_txgain_setconf(&txgain_lut);
    }

    /* Start concentrator */
    i = lgw_start();
    if (i == LGW_HAL_SUCCESS) {
        MSG("INFO: concentrator started, packet can be sent\n");
    } else {
        MSG("ERROR: failed to start the concentrator\n");
        return EXIT_FAILURE;
    }

    /* fill-up payload and parameters */
    memset(&txpkt, 0, sizeof(txpkt));
    txpkt.freq_hz = f_target;
    if (lbt_enable == true) {
        txpkt.tx_mode = TIMESTAMPED;
    } else {
        txpkt.tx_mode = IMMEDIATE;
    }
    txpkt.rf_chain = TX_RF_CHAIN;
    txpkt.rf_power = pow;
    if( strcmp( mod, "FSK" ) == 0 ) {
        txpkt.modulation = MOD_FSK;
        txpkt.datarate = br_kbps * 1e3;
        txpkt.f_dev = fdev_khz;
    } else {
        txpkt.modulation = MOD_LORA;
        switch (bw) {
            case 125: txpkt.bandwidth = BW_125KHZ; break;
            case 250: txpkt.bandwidth = BW_250KHZ; break;
            case 500: txpkt.bandwidth = BW_500KHZ; break;
            default:
                MSG("ERROR: invalid 'bw' variable\n");
                return EXIT_FAILURE;
        }
        switch (sf) {
            case  7: txpkt.datarate = DR_LORA_SF7;  break;
            case  8: txpkt.datarate = DR_LORA_SF8;  break;
            case  9: txpkt.datarate = DR_LORA_SF9;  break;
            case 10: txpkt.datarate = DR_LORA_SF10; break;
            case 11: txpkt.datarate = DR_LORA_SF11; break;
            case 12: txpkt.datarate = DR_LORA_SF12; break;
            default:
                MSG("ERROR: invalid 'sf' variable\n");
                return EXIT_FAILURE;
        }
        switch (cr) {
            case 1: txpkt.coderate = CR_LORA_4_5; break;
            case 2: txpkt.coderate = CR_LORA_4_6; break;
            case 3: txpkt.coderate = CR_LORA_4_7; break;
            case 4: txpkt.coderate = CR_LORA_4_8; break;
            default:
                MSG("ERROR: invalid 'cr' variable\n");
                return EXIT_FAILURE;
        }
    }
    txpkt.invert_pol = invert;
    txpkt.preamble = preamb;
    txpkt.size = pl_size;
    strcpy((char *)txpkt.payload, "TEST**abcdefghijklmnopqrstuvwxyz#0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ#0123456789#abcdefghijklmnopqrstuvwxyz#0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ#0123456789#abcdefghijklmnopqrstuvwxyz#0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ#0123456789#abcdefghijklmnopqrs#" ); /* abc.. is for padding */

    /* main loop */
    cycle_count = 0;
    while ((repeat == -1) || (cycle_count < repeat)) {
        ++cycle_count;

        /* refresh counters in payload (big endian, for readability) */
        txpkt.payload[4] = (uint8_t)(cycle_count >> 8); /* MSB */
        txpkt.payload[5] = (uint8_t)(cycle_count & 0x00FF); /* LSB */

        /* When LBT is enabled, immediate send is not allowed, so we need
            to set a timestamp to the packet */
        if (lbt_enable == true) {
            /* Get the current SX1301 time */
            lgw_reg_w(LGW_GPS_EN, 0);
            lgw_get_trigcnt(&sx1301_count_us);
            lgw_reg_w(LGW_GPS_EN, 1);

            /* Set packet timestamp to current time + few milliseconds */
            txpkt.count_us = sx1301_count_us + 50E3;
        }

        /* send packet */
        printf("Sending packet number %u ...", cycle_count);
        i = lgw_send(txpkt); /* non-blocking scheduling of TX packet */
        if (i == LGW_HAL_ERROR) {
            printf("ERROR\n");
            return EXIT_FAILURE;
        } else if (i == LGW_LBT_ISSUE ) {
            printf("Failed: Not allowed (LBT)\n");
        } else {
            /* wait for packet to finish sending */
            do {
                wait_ms(5);
                lgw_status(TX_STATUS, &status_var); /* get TX status */
            } while (status_var != TX_FREE);
            printf("OK\n");
        }

        /* wait inter-packet delay */
        wait_ms(delay);

        /* exit loop on user signals */
        if ((quit_sig == 1) || (exit_sig == 1)) {
            break;
        }
    }

    /* clean up before leaving */
    lgw_stop();

    printf("Exiting LoRa concentrator TX test program\n");
    return EXIT_SUCCESS;
}

/* --- EOF ------------------------------------------------------------------ */