RadioLib
Universal wireless communication library for Arduino
LoRaWAN.h
1 #if !defined(_RADIOLIB_LORAWAN_H) && !defined(RADIOLIB_EXCLUDE_LORAWAN)
2 #define _RADIOLIB_LORAWAN_H
3 
4 #include "../../TypeDef.h"
5 #include "../PhysicalLayer/PhysicalLayer.h"
6 #include "../../utils/Cryptography.h"
7 
8 // version of NVM table layout (NOT the LoRaWAN version)
9 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_TABLE_VERSION (0x01)
10 
11 // preamble format
12 #define RADIOLIB_LORAWAN_LORA_SYNC_WORD (0x34)
13 #define RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN (8)
14 #define RADIOLIB_LORAWAN_GFSK_SYNC_WORD (0xC194C1)
15 #define RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN (5)
16 
17 // MAC header field encoding MSB LSB DESCRIPTION
18 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5) // 7 5 message type: join request
19 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_ACCEPT (0x01 << 5) // 7 5 join accept
20 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_UP (0x02 << 5) // 7 5 unconfirmed data up
21 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_DOWN (0x03 << 5) // 7 5 unconfirmed data down
22 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP (0x04 << 5) // 7 5 confirmed data up
23 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_DOWN (0x05 << 5) // 7 5 confirmed data down
24 #define RADIOLIB_LORAWAN_MHDR_MTYPE_PROPRIETARY (0x07 << 5) // 7 5 proprietary
25 #define RADIOLIB_LORAWAN_MHDR_MTYPE_MASK (0x07 << 5) // 7 5 bitmask of all possible options
26 #define RADIOLIB_LORAWAN_MHDR_MAJOR_R1 (0x00 << 0) // 1 0 major version: LoRaWAN R1
27 
28 // frame control field encoding
29 #define RADIOLIB_LORAWAN_FCTRL_ADR_ENABLED (0x01 << 7) // 7 7 adaptive data rate: enabled
30 #define RADIOLIB_LORAWAN_FCTRL_ADR_DISABLED (0x00 << 7) // 7 7 disabled
31 #define RADIOLIB_LORAWAN_FCTRL_ADR_ACK_REQ (0x01 << 6) // 6 6 adaptive data rate ACK request
32 #define RADIOLIB_LORAWAN_FCTRL_ACK (0x01 << 5) // 5 5 confirmed message acknowledge
33 #define RADIOLIB_LORAWAN_FCTRL_FRAME_PENDING (0x01 << 4) // 4 4 downlink frame is pending
34 
35 // port field
36 #define RADIOLIB_LORAWAN_FPORT_MAC_COMMAND (0x00 << 0) // 7 0 payload contains MAC commands only
37 #define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0) // 7 0 reserved port values
38 
39 // MAC commands - only those sent from end-device to gateway
40 #define RADIOLIB_LORAWAN_LINK_CHECK_REQ (0x02 << 0) // 7 0 MAC command: request to check connectivity to network
41 #define RADIOLIB_LORAWAN_LINK_ADR_ANS (0x03 << 0) // 7 0 answer to ADR change
42 #define RADIOLIB_LORAWAN_DUTY_CYCLE_ANS (0x04 << 0) // 7 0 answer to duty cycle change
43 #define RADIOLIB_LORAWAN_RX_PARAM_SETUP_ANS (0x05 << 0) // 7 0 answer to reception slot setup request
44 #define RADIOLIB_LORAWAN_DEV_STATUS_ANS (0x06 << 0) // 7 0 device status information
45 #define RADIOLIB_LORAWAN_NEW_CHANNEL_ANS (0x07 << 0) // 7 0 acknowledges change of a radio channel
46 #define RADIOLIB_LORAWAN_RX_TIMING_SETUP_ANS (0x08 << 0) // 7 0 acknowledges change of a reception slots timing
47 
48 #define RADIOLIB_LORAWAN_NOPTS_LEN (8)
49 
50 // data rate encoding
51 #define RADIOLIB_LORAWAN_DATA_RATE_FSK_50_K (0x01 << 7) // 7 7 FSK @ 50 kbps
52 #define RADIOLIB_LORAWAN_DATA_RATE_SF_12 (0x06 << 4) // 6 4 LoRa spreading factor: SF12
53 #define RADIOLIB_LORAWAN_DATA_RATE_SF_11 (0x05 << 4) // 6 4 SF11
54 #define RADIOLIB_LORAWAN_DATA_RATE_SF_10 (0x04 << 4) // 6 4 SF10
55 #define RADIOLIB_LORAWAN_DATA_RATE_SF_9 (0x03 << 4) // 6 4 SF9
56 #define RADIOLIB_LORAWAN_DATA_RATE_SF_8 (0x02 << 4) // 6 4 SF8
57 #define RADIOLIB_LORAWAN_DATA_RATE_SF_7 (0x01 << 4) // 6 4 SF7
58 #define RADIOLIB_LORAWAN_DATA_RATE_BW_500_KHZ (0x00 << 2) // 3 2 LoRa bandwidth: 500 kHz
59 #define RADIOLIB_LORAWAN_DATA_RATE_BW_250_KHZ (0x01 << 2) // 3 2 250 kHz
60 #define RADIOLIB_LORAWAN_DATA_RATE_BW_125_KHZ (0x02 << 2) // 3 2 125 kHz
61 #define RADIOLIB_LORAWAN_DATA_RATE_BW_RESERVED (0x03 << 2) // 3 2 reserved value
62 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_5 (0x00 << 0) // 1 0 LoRa coding rate: 4/5
63 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_6 (0x01 << 0) // 1 0 4/6
64 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_7 (0x02 << 0) // 1 0 4/7
65 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_8 (0x03 << 0) // 1 0 4/8
66 #define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0xFF << 0) // 7 0 unused data rate
67 
68 #define RADIOLIB_LORAWAN_CHANNEL_DIR_UPLINK (0x00 << 0)
69 #define RADIOLIB_LORAWAN_CHANNEL_DIR_DOWNLINK (0x01 << 0)
70 #define RADIOLIB_LORAWAN_CHANNEL_DIR_BOTH (0x02 << 0)
71 #define RADIOLIB_LORAWAN_CHANNEL_DIR_NONE (0x03 << 0)
72 #define RADIOLIB_LORAWAN_BAND_DYNAMIC (0)
73 #define RADIOLIB_LORAWAN_BAND_FIXED (1)
74 #define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (15)
75 #define RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE (0xFF >> 1) // reserve first bit for enable-flag
76 
77 // recommended default settings
78 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
79 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
80 #define RADIOLIB_LORAWAN_RX_WINDOW_LEN_MS (500)
81 #define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
82 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
83 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
84 #define RADIOLIB_LORAWAN_MAX_FCNT_GAP (16384)
85 #define RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP (0x06)
86 #define RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP (0x05)
87 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
88 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
89 #define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
90 
91 // join request message layout
92 #define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
93 #define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
94 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
95 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
96 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
97 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
98 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
99 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
100 
101 // join accept message layout
102 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
103 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
104 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
105 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
106 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_EUI_POS (4)
107 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
108 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
109 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_NONCE_POS (12)
110 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
111 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
112 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
113 
114 // join accept message variables
115 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7) // 7 7 LoRaWAN revision: 1.0
116 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7) // 7 7 1.1
117 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
118 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
119 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
120 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
121 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
122 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
123 
124 // frame header layout
125 #define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
126 #define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
127 #define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
128 #define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
129 #define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
130 #define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
131 #define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 16)
132 #define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
133 #define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
134 #define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
135 
136 // payload encryption/MIC blocks common layout
137 #define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
138 #define RADIOLIB_LORAWAN_BLOCK_CONF_FCNT_POS (1)
139 #define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
140 #define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
141 #define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
142 
143 // payload encryption block layout
144 #define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
145 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
146 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
147 
148 // payload MIC blocks layout
149 #define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
150 #define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
151 #define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
152 #define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
153 
154 // magic word saved in persistent memory upon activation
155 #define RADIOLIB_LORAWAN_MAGIC (0x39EA)
156 
157 // MAC commands
158 #define RADIOLIB_LORAWAN_MAC_CMD_RESET (0x01)
159 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_CHECK (0x02)
160 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_ADR (0x03)
161 #define RADIOLIB_LORAWAN_MAC_CMD_DUTY_CYCLE (0x04)
162 #define RADIOLIB_LORAWAN_MAC_CMD_RX_PARAM_SETUP (0x05)
163 #define RADIOLIB_LORAWAN_MAC_CMD_DEV_STATUS (0x06)
164 #define RADIOLIB_LORAWAN_MAC_CMD_NEW_CHANNEL (0x07)
165 #define RADIOLIB_LORAWAN_MAC_CMD_RX_TIMING_SETUP (0x08)
166 #define RADIOLIB_LORAWAN_MAC_CMD_TX_PARAM_SETUP (0x09)
167 #define RADIOLIB_LORAWAN_MAC_CMD_DL_CHANNEL (0x0A)
168 #define RADIOLIB_LORAWAN_MAC_CMD_REKEY (0x0B)
169 #define RADIOLIB_LORAWAN_MAC_CMD_ADR_PARAM_SETUP (0x0C)
170 #define RADIOLIB_LORAWAN_MAC_CMD_DEVICE_TIME (0x0D)
171 #define RADIOLIB_LORAWAN_MAC_CMD_FORCE_REJOIN (0x0E)
172 #define RADIOLIB_LORAWAN_MAC_CMD_REJOIN_PARAM_SETUP (0x0F)
173 #define RADIOLIB_LORAWAN_MAC_CMD_PROPRIETARY (0x80)
174 
175 // unused frame counter value
176 #define RADIOLIB_LORAWAN_FCNT_NONE (0xFFFFFFFF)
177 
178 // the length of internal MAC command queue - hopefully this is enough for most use cases
179 #define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (8)
180 
181 // the maximum number of simultaneously available channels
182 #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (16)
183 
191  bool enabled;
192 
194  uint8_t idx;
195 
197  float freq;
198 
200  uint8_t drMin;
201 
203  uint8_t drMax;
204 };
205 
206 // alias for unused channel
207 #define RADIOLIB_LORAWAN_CHANNEL_NONE { .enabled = false, .idx = RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE, .freq = 0, .drMin = 0, .drMax = 0 }
208 
216  uint8_t numChannels;
217 
219  float freqStart;
220 
222  float freqStep;
223 
225  uint8_t drMin;
226 
228  uint8_t drMax;
229 
232 };
233 
234 // alias for unused channel span
235 #define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .numChannels = 0, .freqStart = 0, .freqStep = 0, .drMin = 0, .drMax = 0, .joinRequestDataRate = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
236 
243  uint8_t bandType;
244 
246  uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
247 
249  int8_t powerMax;
250 
253 
256 
259 
261  uint8_t numTxSpans;
262 
265 
268 
271 
274 
276  uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
277 };
278 
279 // supported bands
280 extern const LoRaWANBand_t EU868;
281 extern const LoRaWANBand_t US915;
282 extern const LoRaWANBand_t CN780;
283 extern const LoRaWANBand_t EU433;
284 extern const LoRaWANBand_t AU915;
285 extern const LoRaWANBand_t CN500;
286 extern const LoRaWANBand_t AS923;
287 extern const LoRaWANBand_t KR920;
288 extern const LoRaWANBand_t IN865;
289 
296  uint8_t cid;
297 
299  uint8_t payload[5];
300 
302  uint8_t len;
303 
305  uint8_t repeat;
306 };
307 
309  uint8_t numCommands;
310  uint8_t len;
311  LoRaWANMacCommand_t commands[RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE];
312 };
313 
320  uint8_t dir;
321 
323  bool confirmed;
324 
328 
330  float freq;
331 
333  int16_t power;
334 
336  uint32_t fcnt;
337 
339  uint8_t port;
340 };
341 
346 class LoRaWANNode {
347  public:
348 
349  // Offset between TX and RX1 (such that RX1 has equal or lower DR)
350  uint8_t rx1DrOffset;
351 
352  // RX2 channel properties - may be changed by MAC command
353  LoRaWANChannel_t rx2;
354 
360  LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band);
361 
362 #if !defined(RADIOLIB_EEPROM_UNSUPPORTED)
367  void wipe();
368 
373  int16_t restore();
374 #endif
375 
388  int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey, uint8_t joinDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, bool force = false);
389 
401  int16_t beginABP(uint32_t addr, uint8_t* nwkSKey, uint8_t* appSKey, uint8_t* fNwkSIntKey = NULL, uint8_t* sNwkSIntKey = NULL, bool force = false);
402 
404  bool isJoined();
405 
411  int16_t saveSession();
412 
413  #if defined(RADIOLIB_BUILD_ARDUINO)
423  int16_t uplink(String& str, uint8_t port, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
424  #endif
425 
435  int16_t uplink(const char* str, uint8_t port, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
436 
447  int16_t uplink(uint8_t* data, size_t len, uint8_t port, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
448 
449  #if defined(RADIOLIB_BUILD_ARDUINO)
457  int16_t downlink(String& str, LoRaWANEvent_t* event = NULL);
458  #endif
459 
468  int16_t downlink(uint8_t* data, size_t* len, LoRaWANEvent_t* event = NULL);
469 
470  #if defined(RADIOLIB_BUILD_ARDUINO)
483  int16_t sendReceive(String& strUp, uint8_t port, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
484  #endif
485 
499  int16_t sendReceive(const char* strUp, uint8_t port, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
500 
515  int16_t sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t port, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
516 
522  void setDeviceStatus(uint8_t battLevel);
523 
525  uint32_t getFcntUp();
526 
532  int16_t setDatarate(uint8_t drUp);
533 
538  void setADR(bool enable = true);
539 
546  int16_t selectSubband(uint8_t idx);
547 
555  int16_t selectSubband(uint8_t startChannel, uint8_t endChannel);
556 
563  void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA = false);
564 
565 #if !defined(RADIOLIB_GODMODE)
566  private:
567 #endif
568  PhysicalLayer* phyLayer = NULL;
569  const LoRaWANBand_t* band = NULL;
570 
571  LoRaWANMacCommandQueue_t commandsUp = {
572  .numCommands = 0,
573  .len = 0,
574  .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
575  };
576  LoRaWANMacCommandQueue_t commandsDown = {
577  .numCommands = 0,
578  .len = 0,
579  .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
580  };
581 
582  // the following is either provided by the network server (OTAA)
583  // or directly entered by the user (ABP)
584  uint32_t devAddr = 0;
585  uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
586  uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
587  uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
588  uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
589  uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
590 
591  // device-specific parameters, persistent through sessions
592  uint16_t devNonce = 0;
593  uint32_t joinNonce = 0;
594 
595  // session-specific parameters
596  uint32_t homeNetId = 0;
597  uint8_t adrLimitExp = RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP;
598  uint8_t adrDelayExp = RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP;
599  uint8_t nbTrans = 1; // Number of allowed frame retransmissions
600  uint8_t txPwrCur = 0;
601  uint32_t fcntUp = 0;
602  uint32_t aFcntDown = 0;
603  uint32_t nFcntDown = 0;
604  uint32_t confFcntUp = RADIOLIB_LORAWAN_FCNT_NONE;
605  uint32_t confFcntDown = RADIOLIB_LORAWAN_FCNT_NONE;
606  uint32_t adrFcnt = 0;
607 
608  // whether the current configured channel is in FSK mode
609  bool FSK;
610 
611  // flag that shows whether the device is joined and there is an ongoing session
612  bool isJoinedFlag = false;
613 
614  // ADR is enabled by default
615  bool adrEnabled = true;
616 
617  // enable/disable CSMA for LoRaWAN
618  bool enableCSMA;
619 
620  // number of backoff slots to be decremented after DIFS phase. 0 to disable BO.
621  // A random BO avoids collisions in the case where two or more nodes start the CSMA
622  // process at the same time.
623  uint8_t backoffMax;
624 
625  // number of CADs to estimate a clear CH
626  uint8_t difsSlots;
627 
628  // available channel frequencies from list passed during OTA activation
629  LoRaWANChannel_t availableChannels[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS];
630 
631  // currently configured channels for TX and RX1
632  LoRaWANChannel_t currentChannels[2] = { RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE };
633 
634  // currently configured datarates for TX and RX1
635  uint8_t dataRates[2] = { RADIOLIB_LORAWAN_DATA_RATE_UNUSED, RADIOLIB_LORAWAN_DATA_RATE_UNUSED };
636 
637  // LoRaWAN revision (1.0 vs 1.1)
638  uint8_t rev = 0;
639 
640  // timestamp to measure the RX1/2 delay (from uplink end)
641  uint32_t rxDelayStart = 0;
642 
643  // timestamp when the Rx1/2 windows were closed (timeout or uplink received)
644  uint32_t rxDelayEnd = 0;
645 
646  // delays between the uplink and RX1/2 windows
647  uint32_t rxDelays[2] = { RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
648 
649  // device status - battery level
650  uint8_t battLevel = 0xFF;
651 
652  // indicates whether an uplink has MAC commands as payload
653  bool isMACPayload = false;
654 
655 #if !defined(RADIOLIB_EEPROM_UNSUPPORTED)
661  int16_t saveFcntUp();
662 
668  int16_t restoreFcntUp();
669 #endif
670 
671  // wait for, open and listen during Rx1 and Rx2 windows; only performs listening
672  int16_t downlinkCommon();
673 
674  // method to generate message integrity code
675  uint32_t generateMIC(uint8_t* msg, size_t len, uint8_t* key);
676 
677  // method to verify message integrity code
678  // it assumes that the MIC is the last 4 bytes of the message
679  bool verifyMIC(uint8_t* msg, size_t len, uint8_t* key);
680 
681  // configure the common physical layer properties (preamble, sync word etc.)
682  // channels must be configured separately by setupChannels()!
683  int16_t setPhyProperties();
684 
685  // setup uplink/downlink channel data rates and frequencies
686  // will attempt to randomly select based on currently used band plan
687  int16_t setupChannels(uint8_t* cfList);
688 
689  // select a set of semi-random TX/RX channels for the join-request and -accept message
690  int16_t selectChannelsJR(uint16_t devNonce, uint8_t drJoinSubband);
691 
692  // select a set of random TX/RX channels for up- and downlink
693  int16_t selectChannels();
694 
695  // find the first usable data rate for the given band
696  int16_t findDataRate(uint8_t dr, DataRate_t* dataRate);
697 
698  // configure channel based on cached data rate ID and frequency
699  int16_t configureChannel(uint8_t dir);
700 
701  // save all available channels to persistent storage
702  int16_t saveChannels();
703 
704  // restore all available channels from persistent storage
705  int16_t restoreChannels();
706 
707  // push MAC command to queue, done by copy
708  int16_t pushMacCommand(LoRaWANMacCommand_t* cmd, LoRaWANMacCommandQueue_t* queue);
709 
710  // delete a specific MAC command from queue, indicated by the command ID
711  int16_t deleteMacCommand(uint8_t cid, LoRaWANMacCommandQueue_t* queue);
712 
713  // execute mac command, return the number of processed bytes for sequential processing
714  size_t execMacCommand(LoRaWANMacCommand_t* cmd);
715 
716  // Performs CSMA as per LoRa Alliance Technical Reccomendation 13 (TR-013).
717  void performCSMA();
718 
719  // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise.
720  bool performCAD();
721 
722  // function to encrypt and decrypt payloads
723  void processAES(uint8_t* in, size_t len, uint8_t* key, uint8_t* out, uint32_t fcnt, uint8_t dir, uint8_t ctrId, bool counter);
724 
725  // network-to-host conversion method - takes data from network packet and converts it to the host endians
726  template<typename T>
727  static T ntoh(uint8_t* buff, size_t size = 0);
728 
729  // host-to-network conversion method - takes data from host variable and and converts it to network packet endians
730  template<typename T>
731  static void hton(uint8_t* buff, T val, size_t size = 0);
732 };
733 
734 #endif
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:346
int16_t uplink(const char *str, uint8_t port, bool isConfirmed=false, LoRaWANEvent_t *event=NULL)
Send a message to the server.
Definition: LoRaWAN.cpp:674
int16_t setDatarate(uint8_t drUp)
Set uplink datarate. This should not be used when ADR is enabled.
Definition: LoRaWAN.cpp:1666
int16_t sendReceive(const char *strUp, uint8_t port, uint8_t *dataDown, size_t *lenDown, bool isConfirmed=false, LoRaWANEvent_t *eventUp=NULL, LoRaWANEvent_t *eventDown=NULL)
Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.
Definition: LoRaWAN.cpp:1310
uint32_t getFcntUp()
Returns the last uplink's frame counter.
Definition: LoRaWAN.cpp:1334
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition: LoRaWAN.cpp:1330
int16_t saveSession()
Save the current state of the session. All variables are compared to what is saved and only the diffe...
Definition: LoRaWAN.cpp:518
void wipe()
Wipe internal persistent parameters. This will reset all counters and saved variables,...
Definition: LoRaWAN.cpp:47
int16_t beginABP(uint32_t addr, uint8_t *nwkSKey, uint8_t *appSKey, uint8_t *fNwkSIntKey=NULL, uint8_t *sNwkSIntKey=NULL, bool force=false)
Join network by performing activation by personalization. In this procedure, all necessary configurat...
Definition: LoRaWAN.cpp:457
int16_t downlink(uint8_t *data, size_t *len, LoRaWANEvent_t *event=NULL)
Wait for downlink from the server in either RX1 or RX2 window.
Definition: LoRaWAN.cpp:1046
int16_t selectSubband(uint8_t idx)
Select a single subband (8 channels) for fixed bands such as US915. Only available before joining a n...
Definition: LoRaWAN.cpp:1475
LoRaWANNode(PhysicalLayer *phy, const LoRaWANBand_t *band)
Default constructor.
Definition: LoRaWAN.cpp:31
void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA=false)
Configures CSMA for LoRaWAN as per TR-13, LoRa Alliance.
Definition: LoRaWAN.cpp:40
void setADR(bool enable=true)
Toggle ADR to on or off.
Definition: LoRaWAN.cpp:1687
int16_t restore()
Restore session by loading information from persistent storage.
Definition: LoRaWAN.cpp:52
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey, uint8_t joinDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED, bool force=false)
Join network by performing over-the-air activation. By this procedure, the device will perform an exc...
Definition: LoRaWAN.cpp:215
bool isJoined()
Whether there is an ongoing session active.
Definition: LoRaWAN.cpp:513
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:34
Structure to save information about LoRaWAN band.
Definition: LoRaWAN.h:241
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition: LoRaWAN.h:249
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
The corresponding datarates, bandwidths and coding rates for DR index.
Definition: LoRaWAN.h:276
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum payload lengths for each data rate.
Definition: LoRaWAN.h:246
LoRaWANChannel_t txFreqs[3]
A set of default uplink (TX) channels for frequency-type bands.
Definition: LoRaWAN.h:255
LoRaWANChannel_t txJoinReq[3]
A set of possible extra channels for the Join-Request message for frequency-type bands.
Definition: LoRaWAN.h:258
int8_t powerNumSteps
Number of power steps in this band.
Definition: LoRaWAN.h:252
LoRaWANChannelSpan_t txSpans[2]
Default uplink (TX) channel spans for mask-type bands, including Join-Request parameters.
Definition: LoRaWAN.h:264
uint8_t rx1DataRateBase
The base downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:270
uint8_t bandType
Whether the channels are fixed per specification, or dynamically allocated through the network (plus ...
Definition: LoRaWAN.h:243
LoRaWANChannel_t rx2
Backup channel for downlink (RX2) window.
Definition: LoRaWAN.h:273
uint8_t numTxSpans
The number of TX channel spans for mask-type bands.
Definition: LoRaWAN.h:261
LoRaWANChannelSpan_t rx1Span
Default downlink (RX1) channel span for mask-type bands.
Definition: LoRaWAN.h:267
Definition: LoRaWAN.h:189
float freq
The channel frequency.
Definition: LoRaWAN.h:197
uint8_t idx
The channel number, as specified by defaults or the network.
Definition: LoRaWAN.h:194
uint8_t drMin
Minimum allowed datarate for this channel.
Definition: LoRaWAN.h:200
bool enabled
Whether this channel is enabled (can be used) or is disabled.
Definition: LoRaWAN.h:191
uint8_t drMax
Maximum allowed datarate for this channel (inclusive)
Definition: LoRaWAN.h:203
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:214
uint8_t joinRequestDataRate
Allowed data rates for a join request message.
Definition: LoRaWAN.h:231
float freqStart
Center frequency of the first channel in span.
Definition: LoRaWAN.h:219
uint8_t numChannels
Total number of channels in the span.
Definition: LoRaWAN.h:216
uint8_t drMax
Maximum allowed datarate for all channels in this span (inclusive)
Definition: LoRaWAN.h:228
float freqStep
Frequency step between adjacent channels.
Definition: LoRaWAN.h:222
uint8_t drMin
Minimum allowed datarate for all channels in this span.
Definition: LoRaWAN.h:225
Structure to save extra information about uplink/downlink event.
Definition: LoRaWAN.h:318
float freq
Frequency in MHz.
Definition: LoRaWAN.h:330
bool confirmed
Whether the event is confirmed or not (e.g., confirmed uplink sent by user application)
Definition: LoRaWAN.h:323
int16_t power
Transmit power in dBm for uplink, or RSSI for downlink.
Definition: LoRaWAN.h:333
bool confirming
Whether the event is confirming a previous request (e.g., server downlink reply to confirmed uplink s...
Definition: LoRaWAN.h:327
uint8_t dir
Event direction, one of RADIOLIB_LORAWAN_CHANNEL_DIR_*.
Definition: LoRaWAN.h:320
uint32_t fcnt
The appropriate frame counter - for different events, different frame counters will be reported!
Definition: LoRaWAN.h:336
uint8_t port
Port number.
Definition: LoRaWAN.h:339
Structure to save information about MAC command.
Definition: LoRaWAN.h:294
uint8_t len
Length of the payload.
Definition: LoRaWAN.h:302
uint8_t cid
The command ID.
Definition: LoRaWAN.h:296
uint8_t repeat
Repetition counter (the command will be uplinked repeat + 1 times)
Definition: LoRaWAN.h:305
uint8_t payload[5]
Payload buffer (5 bytes is the longest possible)
Definition: LoRaWAN.h:299
Definition: LoRaWAN.h:308
Definition: PhysicalLayer.h:21