-
Notifications
You must be signed in to change notification settings - Fork 364
/
LoRaMac.c
4495 lines (3958 loc) · 143 KB
/
LoRaMac.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)2013 Semtech
___ _____ _ ___ _ _____ ___ ___ ___ ___
/ __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
\__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
|___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
embedded.connectivity.solutions===============
Description: LoRa MAC layer implementation
License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jäckle ( STACKFORCE )
*/
#include <string.h>
#include <assert.h>
#include "node/radio.h"
#include "node/lora.h"
#include "radio/radio.h"
#include "node/utilities.h"
#include "node/mac/LoRaMacCrypto.h"
#include "node/mac/LoRaMac.h"
#include "node/mac/LoRaMacTest.h"
#include "os/os.h"
#include "hal/hal_timer.h"
#include "node/lora_priv.h"
#if MYNEWT_VAL(LORA_MAC_TIMER_NUM) == -1
#error "Must define a Lora MAC timer number"
#else
#define LORA_MAC_TIMER_NUM MYNEWT_VAL(LORA_MAC_TIMER_NUM)
#endif
/* The lora mac timer counts in 1 usec increments */
#define LORA_MAC_TIMER_FREQ 1000000
/* Convert mac state timeout to os ticks */
#define MAC_STATE_CHECK_OS_TICKS \
((MAC_STATE_CHECK_TIMEOUT * OS_TICKS_PER_SEC) / 1000)
/*!
* Maximum PHY layer payload size
*/
#define LORAMAC_PHY_MAXPAYLOAD 255
/*!
* Maximum MAC commands buffer size
*/
#define LORA_MAC_COMMAND_MAX_LENGTH 15
/*!
* FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
* in RxWindowSetup function.
* Maximum PHYPayload = MaxPayloadOfDatarate/MaxPayloadOfDatarateRepeater + LORA_MAC_FRMPAYLOAD_OVERHEAD
*/
#define LORA_MAC_FRMPAYLOAD_OVERHEAD 13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)
/*!
* LoRaMac duty cycle for the back-off procedure during the first hour.
*/
#define BACKOFF_DC_1_HOUR 100
/*!
* LoRaMac duty cycle for the back-off procedure during the next 10 hours.
*/
#define BACKOFF_DC_10_HOURS 1000
/*!
* LoRaMac duty cycle for the back-off procedure during the next 24 hours.
*/
#define BACKOFF_DC_24_HOURS 10000
/*!
* Device IEEE EUI
*/
static uint8_t *LoRaMacDevEui;
/*!
* Application IEEE EUI
*/
static uint8_t *LoRaMacAppEui;
/*!
* AES encryption/decryption cipher application key
*/
static uint8_t *LoRaMacAppKey;
/*!
* AES encryption/decryption cipher network session key
*/
static uint8_t LoRaMacNwkSKey[16];
/*!
* AES encryption/decryption cipher application session key
*/
static uint8_t LoRaMacAppSKey[16];
/*!
* Device nonce is a random value extracted by issuing a sequence of RSSI
* measurements
*/
static uint16_t LoRaMacDevNonce;
/*!
* Network ID ( 3 bytes )
*/
static uint32_t LoRaMacNetID;
/*!
* Mote Address
*/
static uint32_t LoRaMacDevAddr;
/*!
* Multicast channels linked list
*/
static MulticastParams_t *MulticastChannels = NULL;
/*!
* Actual device class
*/
static DeviceClass_t LoRaMacDeviceClass;
/*!
* Indicates if the node is connected to a private or public network
*/
static bool PublicNetwork;
/*!
* Indicates if the node supports repeaters
*/
static bool RepeaterSupport;
/*!
* Buffer containing the data to be sent or received.
*/
static uint8_t LoRaMacBuffer[LORAMAC_PHY_MAXPAYLOAD];
/*!
* Length of packet in LoRaMacBuffer
*/
static uint16_t LoRaMacBufferPktLen;
/*!
* Length of the payload in LoRaMacBuffer
*/
static uint8_t LoRaMacTxPayloadLen;
/*!
* Buffer containing the upper layer data.
*/
static uint8_t LoRaMacRxPayload[LORAMAC_PHY_MAXPAYLOAD];
/*!
* LoRaMAC frame counter. Each time a packet is sent the counter is incremented.
* Only the 16 LSB bits are sent
*/
static uint32_t UpLinkCounter;
/*!
* LoRaMAC frame counter. Each time a packet is received the counter is incremented.
* Only the 16 LSB bits are received
*/
static uint32_t DownLinkCounter;
/*!
* IsPacketCounterFixed enables the MIC field tests by fixing the
* UpLinkCounter value
*/
static bool IsUpLinkCounterFixed = false;
/*!
* Indicates if the MAC layer has already joined a network.
*/
static bool IsLoRaMacNetworkJoined = false;
/*!
* LoRaMac ADR control status
*/
static bool AdrCtrlOn = false;
/*!
* Counts the number of missed ADR acknowledgements
*/
static uint32_t AdrAckCounter;
/*!
* If the node has sent a FRAME_TYPE_DATA_CONFIRMED_UP this variable indicates
* if the nodes needs to manage the server acknowledgement.
*/
static bool NodeAckRequested = false;
/*!
* If the server has sent a FRAME_TYPE_DATA_CONFIRMED_DOWN this variable indicates
* if the ACK bit must be set for the next transmission
*/
static bool SrvAckRequested = false;
/*!
* Contains the current MacCommandsBuffer index
*/
static uint8_t MacCommandsBufferIndex;
/*!
* Contains the current MacCommandsBuffer index for MAC commands to repeat
*/
static uint8_t MacCommandsBufferToRepeatIndex;
/*!
* Buffer containing the MAC layer commands
*/
static uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH];
/*!
* Buffer containing the MAC layer commands which must be repeated
*/
static uint8_t MacCommandsBufferToRepeat[LORA_MAC_COMMAND_MAX_LENGTH];
#if defined( USE_BAND_433 )
/*!
* Data rates table definition
*/
const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
/*!
* Maximum payload with respect to the datarate index. Cannot operate with repeater.
*/
const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
/*!
* Maximum payload with respect to the datarate index. Can operate with repeater.
*/
const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
/*!
* Tx output powers table definition
*/
const int8_t TxPowers[] = { 10, 7, 4, 1, -2, -5 };
/*!
* LoRaMac bands
*/
static Band_t Bands[LORA_MAX_NB_BANDS] =
{
BAND0,
};
/*!
* LoRaMAC channels
*/
static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
{
LC1,
LC2,
LC3,
};
#elif defined( USE_BAND_470 )
/*!
* Data rates table definition
*/
const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7 };
/*!
* Maximum payload with respect to the datarate index. Cannot operate with repeater.
*/
const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 222, 222 };
/*!
* Maximum payload with respect to the datarate index. Can operate with repeater.
*/
const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222 };
/*!
* Tx output powers table definition
*/
const int8_t TxPowers[] = { 17, 16, 14, 12, 10, 7, 5, 2 };
/*!
* LoRaMac bands
*/
static Band_t Bands[LORA_MAX_NB_BANDS] =
{
BAND0,
};
/*!
* LoRaMAC channels
*/
static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
/*!
* Defines the first channel for RX window 1 for CN470 band
*/
#define LORAMAC_FIRST_RX1_CHANNEL ( (uint32_t) 500.3e6 )
/*!
* Defines the last channel for RX window 1 for CN470 band
*/
#define LORAMAC_LAST_RX1_CHANNEL ( (uint32_t) 509.7e6 )
/*!
* Defines the step width of the channels for RX window 1
*/
#define LORAMAC_STEPWIDTH_RX1_CHANNEL ( (uint32_t) 200e3 )
#elif defined( USE_BAND_780 )
/*!
* Data rates table definition
*/
const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
/*!
* Maximum payload with respect to the datarate index. Cannot operate with repeater.
*/
const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
/*!
* Maximum payload with respect to the datarate index. Can operate with repeater.
*/
const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
/*!
* Tx output powers table definition
*/
const int8_t TxPowers[] = { 10, 7, 4, 1, -2, -5 };
/*!
* LoRaMac bands
*/
static Band_t Bands[LORA_MAX_NB_BANDS] =
{
BAND0,
};
/*!
* LoRaMAC channels
*/
static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
{
LC1,
LC2,
LC3,
};
#elif defined( USE_BAND_868 )
/*!
* Data rates table definition
*/
const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
/*!
* Maximum payload with respect to the datarate index. Cannot operate with repeater.
*/
const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
/*!
* Maximum payload with respect to the datarate index. Can operate with repeater.
*/
const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
/*!
* Tx output powers table definition
*/
const int8_t TxPowers[] = { 20, 14, 11, 8, 5, 2 };
/*!
* LoRaMac bands
*/
static Band_t Bands[LORA_MAX_NB_BANDS] =
{
BAND0,
BAND1,
BAND2,
BAND3,
BAND4,
};
/*!
* LoRaMAC channels
*/
static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
{
LC1,
LC2,
LC3,
};
#elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
/*!
* Data rates table definition
*/
const uint8_t Datarates[] = { 10, 9, 8, 7, 8, 0, 0, 0, 12, 11, 10, 9, 8, 7, 0, 0 };
/*!
* Up/Down link data rates offset definition
*/
const int8_t datarateOffsets[5][4] =
{
{ DR_10, DR_9 , DR_8 , DR_8 }, // DR_0
{ DR_11, DR_10, DR_9 , DR_8 }, // DR_1
{ DR_12, DR_11, DR_10, DR_9 }, // DR_2
{ DR_13, DR_12, DR_11, DR_10 }, // DR_3
{ DR_13, DR_13, DR_12, DR_11 }, // DR_4
};
/*!
* Maximum payload with respect to the datarate index. Cannot operate with repeater.
*/
const uint8_t MaxPayloadOfDatarate[] = { 11, 53, 125, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
/*!
* Maximum payload with respect to the datarate index. Can operate with repeater.
*/
const uint8_t MaxPayloadOfDatarateRepeater[] = { 11, 53, 125, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0 };
#if (defined(USE_BAND_915) || defined(USE_BAND_915_HYBRID))
/* XXX: only looked at these for the US band */
/* (incoded) Symbol times, in usecs, for data rates */
const uint16_t g_lora_uncoded_symbol_len_usecs[] =
{
8192, 4096, 2048, 1024, 512, 0, 0, 0, 8192, 4096, 2048, 1024, 512, 256, 0, 0
};
#endif
/*!
* Tx output powers table definition
*/
const int8_t TxPowers[] = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
/*!
* LoRaMac bands
*/
static Band_t Bands[LORA_MAX_NB_BANDS] =
{
BAND0,
};
/*!
* LoRaMAC channels
*/
static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
/*!
* Contains the channels which remain to be applied.
*/
static uint16_t ChannelsMaskRemaining[6];
/*!
* Defines the first channel for RX window 1 for US band
*/
#define LORAMAC_FIRST_RX1_CHANNEL ( (uint32_t) 923.3e6 )
/*!
* Defines the last channel for RX window 1 for US band
*/
#define LORAMAC_LAST_RX1_CHANNEL ( (uint32_t) 927.5e6 )
/*!
* Defines the step width of the channels for RX window 1
*/
#define LORAMAC_STEPWIDTH_RX1_CHANNEL ( (uint32_t) 600e3 )
#else
#error "Please define a frequency band in the compiler options."
#endif
/*!
* LoRaMac parameters
*/
LoRaMacParams_t LoRaMacParams;
/*!
* LoRaMac default parameters
*/
LoRaMacParams_t LoRaMacParamsDefaults;
/*!
* Uplink messages repetitions counter
*/
static uint8_t ChannelsNbRepCounter = 0;
/*!
* Maximum duty cycle
* \remark Possibility to shutdown the device.
*/
static uint8_t MaxDCycle = 0;
/*!
* Aggregated duty cycle management
*/
static uint16_t AggregatedDCycle;
static uint32_t AggregatedLastTxDoneTime;
static uint32_t AggregatedTimeOff;
/*!
* Enables/Disables duty cycle management (Test only)
*/
static bool DutyCycleOn;
/*!
* Current channel index
*/
static uint8_t Channel;
/*!
* Stores the time at LoRaMac initialization.
*
* NOTE: units of this variable are in usecs.
*
* \remark Used for the BACKOFF_DC computation.
*/
static uint64_t LoRaMacInitializationTime;
/*!
* LoRaMac internal states
*/
enum eLoRaMacState
{
LORAMAC_IDLE = 0x00000000,
LORAMAC_TX_RUNNING = 0x00000001,
LORAMAC_RX = 0x00000002,
LORAMAC_ACK_REQ = 0x00000004,
LORAMAC_ACK_RETRY = 0x00000008,
LORAMAC_TX_DELAYED = 0x00000010,
LORAMAC_TX_CONFIG = 0x00000020,
};
/*!
* LoRaMac internal state
*/
uint32_t LoRaMacState;
/*!
* LoRaMac upper layer event functions
*/
static LoRaMacPrimitives_t *LoRaMacPrimitives;
/*!
* LoRaMac upper layer callback functions
*/
static LoRaMacCallback_t *LoRaMacCallbacks;
/*!
* Radio events function pointer
*/
static RadioEvents_t RadioEvents;
/*!
* LoRaMac duty cycle delayed Tx timer
*/
static struct hal_timer TxDelayedTimer;
/*!
* LoRaMac reception windows timers
*/
static struct hal_timer RxWindowTimer1;
static struct hal_timer RxWindowTimer2;
/*!
* LoRaMac reception windows delay
* \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
* join frame : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
*/
static uint32_t RxWindow1Delay;
static uint32_t RxWindow2Delay;
/*!
* Acknowledge timeout timer. Used for packet retransmissions.
*/
static struct hal_timer AckTimeoutTimer;
/*!
* Number of trials to get a frame acknowledged
*/
static uint8_t AckTimeoutRetries = 1;
/*!
* Number of trials to get a frame acknowledged
*/
static uint8_t AckTimeoutRetriesCounter = 1;
/*!
* Last transmission time on air
*/
uint32_t TxTimeOnAir = 0;
/*!
* Number of trials for the Join Request
*/
static uint8_t JoinRequestTrials;
/*!
* Maximum number of trials for the Join Request
*/
static uint8_t MaxJoinRequestTrials;
/*!
* Structure to hold an MCPS indication data.
*/
static McpsIndication_t McpsIndication;
/*!
* Structure to hold MCPS confirm data.
*/
static McpsConfirm_t McpsConfirm;
/*!
* Structure to hold MLME confirm data.
*/
static MlmeConfirm_t MlmeConfirm;
/*!
* Holds the current rx window slot
*/
static uint8_t RxSlot;
/*!
* LoRaMac tx/rx operation state
*/
LoRaMacFlags_t LoRaMacFlags;
/* Lengths of MAC commands */
static const uint8_t
g_lora_mac_cmd_lens[LORA_MAC_MAX_MAC_CMD_CID + 1] = {0, 0, 1, 2, 1, 2, 3, 2, 1};
/* Radio events */
struct os_event g_lora_mac_radio_tx_timeout_event;
struct os_event g_lora_mac_radio_tx_event;
struct os_event g_lora_mac_radio_rx_event;
struct os_event g_lora_mac_radio_rx_err_event;
struct os_event g_lora_mac_radio_rx_timeout_event;
struct os_event g_lora_mac_ack_timeout_event;
struct os_event g_lora_mac_rx_win1_event;
struct os_event g_lora_mac_rx_win2_event;
struct os_event g_lora_mac_tx_delay_timeout_event;
static void lora_mac_rx_on_window2(bool rx_continuous);
/*!
* \brief Function to be executed on Radio Tx Done event
*/
static void
OnRadioTxDone(void)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_radio_tx_event);
}
/**
* Processes radio received done interrupt
*
* Posts received packet event to MAC task for processing.
*
* Context: ISR
*
*/
static void
OnRadioRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_radio_rx_event);
/*
* XXX: for class C devices we may need to handle this differently as
* the device is continuously listening I believe and it may be possible
* to get a receive done event before the previous one has been handled.
*/
/* The ISR fills out the payload pointer, size, rssi and snr of rx pdu */
McpsIndication.Rssi = rssi;
McpsIndication.Snr = snr;
McpsIndication.Buffer = payload;
McpsIndication.BufferSize = size;
}
/**
* Radio transmit timeout event
*
* Context: ISR
*/
static void
OnRadioTxTimeout(void)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_radio_tx_timeout_event);
}
/*!
* \brief Function executed on Radio Rx error event
*/
static void
OnRadioRxError(void)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_radio_rx_err_event);
}
/*!
* \brief Function executed on Radio Rx Timeout event
*/
static void
OnRadioRxTimeout(void)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_radio_rx_timeout_event);
}
/*!
* \brief Function executed on duty cycle delayed Tx timer event
*/
static void
OnTxDelayedTimerEvent(void *unused)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_tx_delay_timeout_event);
}
/*!
* \brief Function executed on first Rx window timer event
*/
static void
OnRxWindow1TimerEvent(void *unused)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_rx_win1_event);
}
/*!
* \brief Function executed on second Rx window timer event
*/
static void
OnRxWindow2TimerEvent(void *unused)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_rx_win2_event);
}
/*!
* \brief Function executed on AckTimeout timer event
*/
static void
OnAckTimeoutTimerEvent(void *unused)
{
os_eventq_put(lora_node_mac_evq_get(), &g_lora_mac_ack_timeout_event);
}
/*!
* \brief Searches and set the next random available channel
*
* \param [OUT] Time to wait for the next transmission according to the duty
* cycle.
*
* \retval status Function status [1: OK, 0: Unable to find a channel on the
* current datarate]
*/
static bool SetNextChannel( uint32_t* time );
/*!
* \brief Initializes and opens the reception window
*
* \param [IN] freq window channel frequency
* \param [IN] datarate window channel datarate
* \param [IN] bandwidth window channel bandwidth
* \param [IN] timeout window channel timeout
*
* \retval status Operation status [true: Success, false: Fail]
*/
static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous );
/*!
* \brief Verifies if the RX window 2 frequency is in range
*
* \param [IN] freq window channel frequency
*
* \retval status Function status [1: OK, 0: Frequency not applicable]
*/
static bool Rx2FreqInRange( uint32_t freq );
/*!
* \brief Adds a new MAC command to be sent.
*
* \Remark MAC layer internal function
*
* \param [in] cmd MAC command to be added
* [MOTE_MAC_LINK_CHECK_REQ,
* MOTE_MAC_LINK_ADR_ANS,
* MOTE_MAC_DUTY_CYCLE_ANS,
* MOTE_MAC_RX2_PARAM_SET_ANS,
* MOTE_MAC_DEV_STATUS_ANS
* MOTE_MAC_NEW_CHANNEL_ANS]
* \param [in] p1 1st parameter ( optional depends on the command )
* \param [in] p2 2nd parameter ( optional depends on the command )
*
* \retval status Function status [0: OK, 1: Unknown command, 2: Buffer full]
*/
static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
/*!
* \brief Parses the MAC commands which must be repeated.
*
* \Remark MAC layer internal function
*
* \param [IN] cmdBufIn Buffer which stores the MAC commands to send
* \param [IN] length Length of the input buffer to parse
* \param [OUT] cmdBufOut Buffer which stores the MAC commands which must be
* repeated.
*
* \retval Size of the MAC commands to repeat.
*/
static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut );
/*!
* \brief Validates if the payload fits into the frame, taking the datarate
* into account.
*
* \details Refer to chapter 4.3.2 of the LoRaWAN specification, v1.0
*
* \param lenN Length of the application payload. The length depends on the
* datarate and is region specific
*
* \param datarate Current datarate
*
* \param fOptsLen Length of the fOpts field
*
* \retval [false: payload does not fit into the frame, true: payload fits into
* the frame]
*/
static bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen );
/*!
* \brief Counts the number of bits in a mask.
*
* \param [IN] mask A mask from which the function counts the active bits.
* \param [IN] nbBits The number of bits to check.
*
* \retval Number of enabled bits in the mask.
*/
static uint8_t CountBits( uint16_t mask, uint8_t nbBits );
#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
/*!
* \brief Counts the number of enabled 125 kHz channels in the channel mask.
* This function can only be applied to US915 band.
*
* \param [IN] channelsMask Pointer to the first element of the channel mask
*
* \retval Number of enabled channels in the channel mask
*/
static uint8_t CountNbEnabled125kHzChannels( uint16_t *channelsMask );
#if defined( USE_BAND_915_HYBRID )
/*!
* \brief Validates the correctness of the channel mask for US915, hybrid mode.
*
* \param [IN] mask Block definition to set.
* \param [OUT] channelsMask Pointer to the first element of the channel mask
*/
static void ReenableChannels( uint16_t mask, uint16_t* channelsMask );
/*!
* \brief Validates the correctness of the channel mask for US915, hybrid mode.
*
* \param [IN] channelsMask Pointer to the first element of the channel mask
*
* \retval [true: channel mask correct, false: channel mask not correct]
*/
static bool ValidateChannelMask( uint16_t* channelsMask );
#endif
#endif
/*!
* \brief Validates the correctness of the datarate against the enable channels.
*
* \param [IN] datarate Datarate to be check
* \param [IN] channelsMask Pointer to the first element of the channel mask
*
* \retval [true: datarate can be used, false: datarate can not be used]
*/
static bool ValidateDatarate( int8_t datarate, uint16_t* channelsMask );
/*!
* \brief Limits the Tx power according to the number of enabled channels
*
* \param [IN] txPower txPower to limit
* \param [IN] maxBandTxPower Maximum band allowed TxPower
*
* \retval Returns the maximum valid tx power
*/
static int8_t LimitTxPower( int8_t txPower, int8_t maxBandTxPower );
/*!
* \brief Verifies, if a value is in a given range.
*
* \param value Value to verify, if it is in range
*
* \param min Minimum possible value
*
* \param max Maximum possible value
*
* \retval Returns the maximum valid tx power
*/
static bool ValueInRange( int8_t value, int8_t min, int8_t max );
/*!
* \brief Calculates the next datarate to set, when ADR is on or off
*
* \param [IN] adrEnabled Specify whether ADR is on or off
*
* \param [IN] updateChannelMask Set to true, if the channel masks shall be updated
*
* \param [OUT] datarateOut Reports the datarate which will be used next
*
* \retval Returns the state of ADR ack request
*/
static bool AdrNextDr( bool adrEnabled, bool updateChannelMask, int8_t* datarateOut );
/*!
* \brief Disables channel in a specified channel mask
*
* \param [IN] id - Id of the channel
*
* \param [IN] mask - Pointer to the channel mask to edit
*
* \retval [true, if disable was successful, false if not]
*/
static bool DisableChannelInMask( uint8_t id, uint16_t* mask );
/*!
* \brief Decodes MAC commands in the fOpts field and in the payload
*/
static void ProcessMacCommands(uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr);
/*!
* \brief LoRaMAC layer generic send frame
*
* \param [IN] macHdr MAC header field
* \param [IN] fPort MAC payload port
* \param [IN] m mbuf containing MAC payload
* \retval status Status of the operation.
*/
LoRaMacStatus_t Send(LoRaMacHeader_t *macHdr, uint8_t fPort, struct os_mbuf *m);
/*!
* \brief LoRaMAC layer frame buffer initialization
*
* \param [IN] macHdr MAC header field
* \param [IN] fCtrl MAC frame control field
* \param [IN] fOpts MAC commands buffer
* \param [IN] fPort MAC payload port
* \param [IN] om mbuf with MAC payload
* \retval status Status of the operation.
*/
LoRaMacStatus_t PrepareFrame(LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl,
uint8_t fPort, struct os_mbuf *om);
/*
* \brief Schedules the frame according to the duty cycle
*
* \retval Status of the operation
*/
static LoRaMacStatus_t ScheduleTx(void);
/*
* \brief Sets the duty cycle for the join procedure.
*
* \retval Duty cycle
*/
static uint16_t JoinDutyCycle(void);
/*
* \brief Calculates the back-off time for the band of a channel.
*
* \param [IN] channel The last Tx channel index
*/
static void CalculateBackOff( uint8_t channel );
/*
* \brief Alternates the datarate of the channel for the join request.
*
* \param [IN] nbTrials Number of performed join requests.
* \retval Datarate to apply
*/
static int8_t AlternateDatarate( uint16_t nbTrials );
/*!
* \brief LoRaMAC layer prepared frame buffer transmission with channel specification
*
* \remark PrepareFrame must be called at least once before calling this
* function.
*
* \param [IN] channel Channel parameters
* \retval status Status of the operation.
*/
LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel );
/*!
* \brief Sets the radio in continuous transmission mode
*
* \remark Uses the radio parameters set on the previous transmission.
*
* \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
* \retval status Status of the operation.