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_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_CFLIST_TYPE_FREQUENCIES (0)
73 #define RADIOLIB_LORAWAN_CFLIST_TYPE_MASK (1)
74 #define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (16)
75
76 // recommended default settings
77 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
78 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
79 #define RADIOLIB_LORAWAN_RX_WINDOW_LEN_MS (500)
80 #define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
81 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
82 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
83 #define RADIOLIB_LORAWAN_MAX_FCNT_GAP (16384)
84 #define RADIOLIB_LORAWAN_ADR_ACK_LIMIT (64)
85 #define RADIOLIB_LORAWAN_ADR_ACK_DELAY (32)
86 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
87 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
88 #define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
89
90 // join request message layout
91 #define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
92 #define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
93 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
94 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
95 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
96 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
97 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
98 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
99
100 // join accept message layout
101 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
102 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
103 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
104 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
105 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_EUI_POS (4)
106 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
107 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
108 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_NONCE_POS (12)
109 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
110 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
111 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
112
113 // join accept message variables
114 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7) // 7 7 LoRaWAN revision: 1.0
115 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7) // 7 7 1.1
116 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
117 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
118 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
119 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
120 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
121 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
122
123 // frame header layout
124 #define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
125 #define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
126 #define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
127 #define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
128 #define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
129 #define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
130 #define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 16)
131 #define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
132 #define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
133 #define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
134
135 // payload encryption/MIC blocks common layout
136 #define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
137 #define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
138 #define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
139 #define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
140
141 // payload encryption block layout
142 #define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
143 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
144 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
145
146 // payload MIC blocks layout
147 #define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
148 #define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
149 #define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
150 #define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
151
152 // magic word saved in persistent memory upon activation
153 #define RADIOLIB_LORAWAN_MAGIC (0x12AD101B)
154
155 // MAC commands
156 #define RADIOLIB_LORAWAN_MAC_CMD_RESET (0x01)
157 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_CHECK (0x02)
158 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_ADR (0x03)
159 #define RADIOLIB_LORAWAN_MAC_CMD_DUTY_CYCLE (0x04)
160 #define RADIOLIB_LORAWAN_MAC_CMD_RX_PARAM_SETUP (0x05)
161 #define RADIOLIB_LORAWAN_MAC_CMD_DEV_STATUS (0x06)
162 #define RADIOLIB_LORAWAN_MAC_CMD_NEW_CHANNEL (0x07)
163 #define RADIOLIB_LORAWAN_MAC_CMD_RX_TIMING_SETUP (0x08)
164 #define RADIOLIB_LORAWAN_MAC_CMD_TX_PARAM_SETUP (0x09)
165 #define RADIOLIB_LORAWAN_MAC_CMD_DL_CHANNEL (0x0A)
166 #define RADIOLIB_LORAWAN_MAC_CMD_REKEY (0x0B)
167 #define RADIOLIB_LORAWAN_MAC_CMD_ADR_PARAM_SETUP (0x0C)
168 #define RADIOLIB_LORAWAN_MAC_CMD_DEVICE_TIME (0x0D)
169 #define RADIOLIB_LORAWAN_MAC_CMD_FORCE_REJOIN (0x0E)
170 #define RADIOLIB_LORAWAN_MAC_CMD_REJOIN_PARAM_SETUP (0x0F)
171 #define RADIOLIB_LORAWAN_MAC_CMD_PROPRIETARY (0x80)
172
173 // the length of internal MAC command queue - hopefully this is enough for most use cases
174 #define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (8)
175
176 // the maximum number of simultaneously available channels
177 #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (8)
178
184 struct LoRaWANChannelSpan_t {
186 uint8_t direction;
187
189 uint8_t joinRequestDataRate;
190
192 uint8_t numChannels;
193
195 float freqStart;
196
198 float freqStep;
199
201 uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
202 };
203
204 // alias for unused channel span
205 #define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .direction = RADIOLIB_LORAWAN_CHANNEL_DIR_NONE, .joinRequestDataRate = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, .numChannels = 0, .freqStart = 0, .freqStep = 0, .dataRates = { 0 } }
206
211 struct LoRaWANBand_t {
213 uint8_t downlinkDataRateBase;
214
216 uint8_t downlinkDataRateMin;
217
219 uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
220
222 int8_t powerMax;
223
225 int8_t powerNumSteps;
226
228 uint8_t cfListType;
229
231 float fskFreq;
232
234 uint8_t numChannelSpans;
235
237 LoRaWANChannelSpan_t defaultChannels[3];
238
240 LoRaWANChannelSpan_t backupChannel;
241 };
242
243 // supported bands
244 extern const LoRaWANBand_t EU868;
245 extern const LoRaWANBand_t US915;
246 extern const LoRaWANBand_t CN780;
247 extern const LoRaWANBand_t EU433;
248 extern const LoRaWANBand_t AU915;
249 extern const LoRaWANBand_t CN500;
250 extern const LoRaWANBand_t AS923;
251 extern const LoRaWANBand_t KR920;
252 extern const LoRaWANBand_t IN865;
253
258 struct LoRaWANMacCommand_t {
260 uint8_t cid;
261
263 uint8_t len;
264
266 uint8_t payload[5];
267
269 uint8_t repeat;
270 };
271
272 struct LoRaWANMacCommandQueue_t {
273 LoRaWANMacCommand_t commands[RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE];
274 size_t numCommands;
275 size_t len;
276 };
277
282 class LoRaWANNode {
283 public:
285 bool FSK;
286
291 int8_t startChannel;
292
297 int8_t numChannels;
298
302 uint8_t backoffMax;
303
305 uint8_t difsSlots;
306
308 bool enableCSMA;
309
315 LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band);
316
321 void wipe();
322
329 void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA = false);
330
335 int16_t restoreOTAA();
336
347 int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey, bool force = false);
348
359 int16_t beginABP(uint32_t addr, uint8_t* nwkSKey, uint8_t* appSKey, uint8_t* fNwkSIntKey = NULL, uint8_t* sNwkSIntKey = NULL);
360
361 #if defined(RADIOLIB_BUILD_ARDUINO)
368 int16_t uplink(String& str, uint8_t port);
369 #endif
370
377 int16_t uplink(const char* str, uint8_t port);
378
386 int16_t uplink(uint8_t* data, size_t len, uint8_t port);
387
388 #if defined(RADIOLIB_BUILD_ARDUINO)
394 int16_t downlink(String& str);
395 #endif
396
403 int16_t downlink(uint8_t* data, size_t* len);
404
410 void setDeviceStatus(uint8_t battLevel);
411
412 #if !defined(RADIOLIB_GODMODE)
413 private:
414 #endif
415 PhysicalLayer* phyLayer = NULL;
416 const LoRaWANBand_t* band = NULL;
417
418 LoRaWANMacCommandQueue_t commandsUp = {
419 .commands = { { .cid = 0, .len = 0, .payload = { 0 }, .repeat = 0, } },
420 .numCommands = 0,
421 .len = 0,
422 };
423 LoRaWANMacCommandQueue_t commandsDown = {
424 .commands = { { .cid = 0, .len = 0, .payload = { 0 }, .repeat = 0, } },
425 .numCommands = 0,
426 .len = 0,
427 };
428
429 // the following is either provided by the network server (OTAA)
430 // or directly entered by the user (ABP)
431 uint32_t devAddr = 0;
432 uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
433 uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
434 uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
435 uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
436 uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
437
438 // available channel frequencies from list passed during OTA activation
439 float availableChannelsFreq[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS] = { { 0 }, { 0 } };
440
441 // currently configured channel frequency
442 float channelFreq[2] = { 0 };
443
444 // LoRaWAN revision (1.0 vs 1.1)
445 uint8_t rev = 0;
446
447 // currently configured data rate for uplink and downlink: DR0 - DR15 (band-dependent!)
448 uint8_t dataRate[2] = { 0 };
449
450 // currently configured channel for uplink and downlink (band-dependent!)
451 uint8_t chIndex[2] = { 0 };
452
453 // backup channel properties - may be changed by MAC command
454 float backupFreq = 0;
455 uint8_t backupDataRate = 0;
456
457 // timestamp to measure the RX1/2 delay (from uplink end)
458 uint32_t rxDelayStart = 0;
459
460 // delays between the uplink and RX1/2 windows
461 uint32_t rxDelays[2] = { RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
462
463 // device status - battery level
464 uint8_t battLevel = 0xFF;
465
466 // indicates whether an uplink has MAC commands as payload
467 bool isMACPayload = false;
468
469 // method to generate message integrity code
470 uint32_t generateMIC(uint8_t* msg, size_t len, uint8_t* key);
471
472 // method to verify message integrity code
473 // it assumes that the MIC is the last 4 bytes of the message
474 bool verifyMIC(uint8_t* msg, size_t len, uint8_t* key);
475
476 // configure the common physical layer properties (preamble, sync word etc.)
477 // channels must be configured separately by setupChannels()!
478 int16_t setPhyProperties();
479
480 // setup uplink/downlink channel data rates and frequencies
481 // will attempt to randomly select based on currently used band plan
482 int16_t setupChannels();
483
484 // find the first usable data rate in a given channel span
485 uint8_t findDataRate(uint8_t dr, DataRate_t* dataRate, const LoRaWANChannelSpan_t* span);
486
487 // find a channel ID that conforms to the requested direction and ID range
488 int16_t findChannelId(uint8_t dir, uint8_t* ch, uint8_t* dr, int8_t min, int8_t max);
489
490 // find a channel span that any given channel id belongs to
491 LoRaWANChannelSpan_t* findChannelSpan(uint8_t dir, uint8_t ch, uint8_t* spanChannelId);
492
493 // calculate channel frequency in MHz based on channel ID and direction
494 int16_t findChannelFreq(uint8_t dir, uint8_t ch, float* freq);
495
496 // configure channel based on cached data rate ID and frequency
497 int16_t configureChannel(uint8_t dir);
498
499 // send a MAC command to the network server
500 int16_t sendMacCommand(uint8_t cid, uint8_t* payload, size_t payloadLen, uint8_t* reply, size_t replyLen);
501
502 // push MAC command to queue, done by copy
503 int16_t pushMacCommand(LoRaWANMacCommand_t* cmd, LoRaWANMacCommandQueue_t* queue);
504
505 // pop MAC command from queue, done by copy unless CMD is NULL
506 int16_t popMacCommand(LoRaWANMacCommand_t* cmd, LoRaWANMacCommandQueue_t* queue, size_t index);
507
508 // delete a specific MAC command from queue, indicated by the command ID
509 int16_t deleteMacCommand(uint8_t cid, LoRaWANMacCommandQueue_t* queue);
510
511 // execute mac command, return the number of processed bytes for sequential processing
512 size_t execMacCommand(LoRaWANMacCommand_t* cmd);
513
514 // function to encrypt and decrypt payloads
515 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);
516
517 // network-to-host conversion method - takes data from network packet and converts it to the host endians
518 template<typename T>
519 static T ntoh(uint8_t* buff, size_t size = 0);
520
521 // host-to-network conversion method - takes data from host variable and and converts it to network packet endians
522 template<typename T>
523 static void hton(uint8_t* buff, T val, size_t size = 0);
524
525 // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise.
526 bool performCAD();
527
528 // Performs CSMA as per LoRa Alliance Technical Reccomendation 13 (TR-013).
529 void performCSMA();
530 };
531
532 #endif
LoRaWANNode
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:282
LoRaWANNode::beginOTAA
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey, bool force=false)
Join network by performing over-the-air activation. By this procedure, the device will perform an exc...
Definition: LoRaWAN.cpp:165
LoRaWANNode::FSK
bool FSK
Set to true to force the node to only use FSK channels. Set to false by default.
Definition: LoRaWAN.h:285
LoRaWANNode::difsSlots
uint8_t difsSlots
Num of CADs to estimate a clear CH.
Definition: LoRaWAN.h:305
LoRaWANNode::enableCSMA
bool enableCSMA
enable/disable CSMA for LoRaWAN.
Definition: LoRaWAN.h:308
LoRaWANNode::setDeviceStatus
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition: LoRaWAN.cpp:1028
LoRaWANNode::downlink
int16_t downlink(uint8_t *data, size_t *len)
Wait for downlink from the server in either RX1 or RX2 window.
Definition: LoRaWAN.cpp:696
LoRaWANNode::numChannels
int8_t numChannels
Number of supported channels. Some band plans only support a subset of available channels....
Definition: LoRaWAN.h:297
LoRaWANNode::wipe
void wipe()
Wipe internal persistent parameters. This will reset all counters and saved variables,...
Definition: LoRaWAN.cpp:46
LoRaWANNode::backoffMax
uint8_t backoffMax
Num of Back Off(BO) slots to be decremented after DIFS phase. 0 to disable BO. A random BO avoids col...
Definition: LoRaWAN.h:302
LoRaWANNode::restoreOTAA
int16_t restoreOTAA()
Restore OTAA session by loading information from persistent storage.
Definition: LoRaWAN.cpp:58
LoRaWANNode::beginABP
int16_t beginABP(uint32_t addr, uint8_t *nwkSKey, uint8_t *appSKey, uint8_t *fNwkSIntKey=NULL, uint8_t *sNwkSIntKey=NULL)
Join network by performing activation by personalization. In this procedure, all necessary configurat...
Definition: LoRaWAN.cpp:472
LoRaWANNode::startChannel
int8_t startChannel
Starting channel offset. Some band plans only support a subset of available channels....
Definition: LoRaWAN.h:291
LoRaWANNode::LoRaWANNode
LoRaWANNode(PhysicalLayer *phy, const LoRaWANBand_t *band)
Default constructor.
Definition: LoRaWAN.cpp:33
LoRaWANNode::setCSMA
void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA=false)
Configures CSMA for LoRaWAN as per TR-13, LoRa Alliance.
Definition: LoRaWAN.cpp:51
LoRaWANNode::uplink
int16_t uplink(const char *str, uint8_t port)
Send a message to the server.
Definition: LoRaWAN.cpp:507
PhysicalLayer
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:34
LoRaWANBand_t
Structure to save information about LoRaWAN band.
Definition: LoRaWAN.h:211
LoRaWANBand_t::cfListType
uint8_t cfListType
Whether the optional channels are defined as list of frequencies or bit mask.
Definition: LoRaWAN.h:228
LoRaWANBand_t::backupChannel
LoRaWANChannelSpan_t backupChannel
Backup downlink (RX2) channel - just a single channel, but using the same structure for convenience.
Definition: LoRaWAN.h:240
LoRaWANBand_t::downlinkDataRateBase
uint8_t downlinkDataRateBase
The base downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:213
LoRaWANBand_t::powerMax
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition: LoRaWAN.h:222
LoRaWANBand_t::defaultChannels
LoRaWANChannelSpan_t defaultChannels[3]
Default uplink (TX/RX1) channels defined by LoRaWAN Regional Parameters.
Definition: LoRaWAN.h:237
LoRaWANBand_t::payloadLenMax
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum payload lengths for each data rate.
Definition: LoRaWAN.h:219
LoRaWANBand_t::powerNumSteps
int8_t powerNumSteps
Number of power steps in this band.
Definition: LoRaWAN.h:225
LoRaWANBand_t::fskFreq
float fskFreq
FSK channel frequency.
Definition: LoRaWAN.h:231
LoRaWANBand_t::numChannelSpans
uint8_t numChannelSpans
Number of channel spans in the band.
Definition: LoRaWAN.h:234
LoRaWANBand_t::downlinkDataRateMin
uint8_t downlinkDataRateMin
The minimum allowed downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:216
LoRaWANChannelSpan_t
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:184
LoRaWANChannelSpan_t::joinRequestDataRate
uint8_t joinRequestDataRate
Allowed data rates for a join request message.
Definition: LoRaWAN.h:189
LoRaWANChannelSpan_t::freqStart
float freqStart
Center frequency of the first channel in span.
Definition: LoRaWAN.h:195
LoRaWANChannelSpan_t::numChannels
uint8_t numChannels
Total number of channels in the span.
Definition: LoRaWAN.h:192
LoRaWANChannelSpan_t::dataRates
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of datarates supported by all channels in the span.
Definition: LoRaWAN.h:201
LoRaWANChannelSpan_t::freqStep
float freqStep
Frequency step between adjacent channels.
Definition: LoRaWAN.h:198
LoRaWANChannelSpan_t::direction
uint8_t direction
Whether this channel span is for uplink, downlink, or both directions.
Definition: LoRaWAN.h:186
LoRaWANMacCommand_t
Structure to save information about MAC command.
Definition: LoRaWAN.h:258
LoRaWANMacCommand_t::len
uint8_t len
Length of the payload.
Definition: LoRaWAN.h:263
LoRaWANMacCommand_t::cid
uint8_t cid
The command ID.
Definition: LoRaWAN.h:260
LoRaWANMacCommand_t::repeat
uint8_t repeat
Repetition counter (the command will be uplinked repeat + 1 times)
Definition: LoRaWAN.h:269
LoRaWANMacCommand_t::payload
uint8_t payload[5]
Payload buffer (5 bytes is the longest possible)
Definition: LoRaWAN.h:266
LoRaWANMacCommandQueue_t
Definition: LoRaWAN.h:272
DataRate_t
Definition: PhysicalLayer.h:21