RadioLib
Universal wireless communication library for Arduino
Loading...
Searching...
No Matches
LoRaWAN.h
1#if !defined(_RADIOLIB_LORAWAN_H) && !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// activation mode
9#define RADIOLIB_LORAWAN_MODE_OTAA (0x07AA)
10#define RADIOLIB_LORAWAN_MODE_ABP (0x0AB9)
11#define RADIOLIB_LORAWAN_MODE_NONE (0x0000)
12
13// operation mode
14#define RADIOLIB_LORAWAN_CLASS_A (0x00)
15#define RADIOLIB_LORAWAN_CLASS_B (0x01)
16#define RADIOLIB_LORAWAN_CLASS_C (0x02)
17
18// preamble format
19#define RADIOLIB_LORAWAN_LORA_SYNC_WORD (0x34)
20#define RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN (8)
21#define RADIOLIB_LORAWAN_GFSK_SYNC_WORD (0xC194C1)
22#define RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN (5)
23#define RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD (0x2C0F7995)
24
25// MAC header field encoding MSB LSB DESCRIPTION
26#define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5) // 7 5 message type: join request
27#define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_ACCEPT (0x01 << 5) // 7 5 join accept
28#define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_UP (0x02 << 5) // 7 5 unconfirmed data up
29#define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_DOWN (0x03 << 5) // 7 5 unconfirmed data down
30#define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP (0x04 << 5) // 7 5 confirmed data up
31#define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_DOWN (0x05 << 5) // 7 5 confirmed data down
32#define RADIOLIB_LORAWAN_MHDR_MTYPE_PROPRIETARY (0x07 << 5) // 7 5 proprietary
33#define RADIOLIB_LORAWAN_MHDR_MTYPE_MASK (0x07 << 5) // 7 5 bitmask of all possible options
34#define RADIOLIB_LORAWAN_MHDR_MAJOR_R1 (0x00 << 0) // 1 0 major version: LoRaWAN R1
35
36// frame control field encoding
37#define RADIOLIB_LORAWAN_FCTRL_ADR_ENABLED (0x01 << 7) // 7 7 adaptive data rate: enabled
38#define RADIOLIB_LORAWAN_FCTRL_ADR_DISABLED (0x00 << 7) // 7 7 disabled
39#define RADIOLIB_LORAWAN_FCTRL_ADR_ACK_REQ (0x01 << 6) // 6 6 adaptive data rate ACK request
40#define RADIOLIB_LORAWAN_FCTRL_ACK (0x01 << 5) // 5 5 confirmed message acknowledge
41#define RADIOLIB_LORAWAN_FCTRL_FRAME_PENDING (0x01 << 4) // 4 4 downlink frame is pending
42
43// fPort field
44#define RADIOLIB_LORAWAN_FPORT_MAC_COMMAND (0x00 << 0) // 7 0 payload contains MAC commands only
45#define RADIOLIB_LORAWAN_FPORT_PAYLOAD_MIN (0x01 << 0) // 7 0 start of user-allowed fPort range
46#define RADIOLIB_LORAWAN_FPORT_PAYLOAD_MAX (0xDF << 0) // 7 0 end of user-allowed fPort range
47#define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0) // 7 0 fPort values equal to and larger than this are reserved
48
49// data rate encoding
50#define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0x0F << 0) // reserved / unused data rate
51#define RADIOLIB_LORAWAN_TX_POWER_UNUSED (0x0F << 0) // reserved / unused Tx power
52
53// channels and channel plans
54#define RADIOLIB_LORAWAN_UPLINK (0x00 << 0)
55#define RADIOLIB_LORAWAN_DOWNLINK (0x01 << 0)
56#define RADIOLIB_LORAWAN_RX1 (0x01 << 0)
57#define RADIOLIB_LORAWAN_RX2 (0x02 << 0)
58#define RADIOLIB_LORAWAN_RX_BC (0x03 << 0)
59#define RADIOLIB_LORAWAN_BAND_DYNAMIC (0)
60#define RADIOLIB_LORAWAN_BAND_FIXED (1)
61#define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (15)
62
63// recommended default settings
64#define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
65#define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
66#define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
67#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
68#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
69#define RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP (0x06)
70#define RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP (0x05)
71#define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
72#define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
73#define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
74#define RADIOLIB_LORAWAN_REJOIN_MAX_COUNT_N (10) // send rejoin request 16384 uplinks
75#define RADIOLIB_LORAWAN_REJOIN_MAX_TIME_N (15) // once every year, not actually implemented
76
77// join request message layout
78#define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
79#define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
80#define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
81#define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
82#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
83#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
84#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
85#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
86
87// join accept message layout
88#define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
89#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
90#define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
91#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
92#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
93#define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
94#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
95#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
96#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
97
98// join accept key derivation layout
99#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_JOIN_NONCE_POS (1) // regular keys
100#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_JOIN_EUI_POS (4)
101#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_DEV_NONCE_POS (12)
102#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_DEV_ADDR_POS (1) // relay keys
103
104// join accept message variables
105#define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7) // 7 7 LoRaWAN revision: 1.0
106#define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7) // 7 7 1.1
107#define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
108#define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
109#define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
110#define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
111#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
112#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
113#define RADIOLIB_LORAWAN_JOIN_ACCEPT_ROOT_WOR_S_KEY (0x01)
114#define RADIOLIB_LORAWAN_JOIN_ACCEPT_WOR_S_INT_KEY (0x01)
115#define RADIOLIB_LORAWAN_JOIN_ACCEPT_WOR_S_ENC_KEY (0x02)
116
117// frame header layout
118#define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
119#define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
120#define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
121#define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
122#define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
123#define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
124#define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (15)
125#define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
126#define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
127#define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
128
129// payload encryption/MIC blocks common layout
130#define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
131#define RADIOLIB_LORAWAN_BLOCK_CONF_FCNT_POS (1)
132#define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
133#define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
134#define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
135
136// payload encryption block layout
137#define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
138#define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
139#define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
140
141// payload MIC blocks layout
142#define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
143#define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
144#define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
145#define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
146
147// maximum allowed dwell time on bands that implement dwell time limitations
148#define RADIOLIB_LORAWAN_DWELL_TIME (400)
149
150// unused frame counter value
151#define RADIOLIB_LORAWAN_FCNT_NONE (0xFFFFFFFF)
152
153// TR013 CSMA recommended values
154#define RADIOLIB_LORAWAN_DIFS_DEFAULT (2)
155#define RADIOLIB_LORAWAN_BACKOFF_MAX_DEFAULT (6)
156#define RADIOLIB_LORAWAN_MAX_CHANGES_DEFAULT (4)
157
158// MAC commands
159#define RADIOLIB_LORAWAN_NUM_MAC_COMMANDS (24)
160
161#define RADIOLIB_LORAWAN_MAC_RESET (0x01)
162#define RADIOLIB_LORAWAN_MAC_LINK_CHECK (0x02)
163#define RADIOLIB_LORAWAN_MAC_LINK_ADR (0x03)
164#define RADIOLIB_LORAWAN_MAC_DUTY_CYCLE (0x04)
165#define RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP (0x05)
166#define RADIOLIB_LORAWAN_MAC_DEV_STATUS (0x06)
167#define RADIOLIB_LORAWAN_MAC_NEW_CHANNEL (0x07)
168#define RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP (0x08)
169#define RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP (0x09)
170#define RADIOLIB_LORAWAN_MAC_DL_CHANNEL (0x0A)
171#define RADIOLIB_LORAWAN_MAC_REKEY (0x0B)
172#define RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP (0x0C)
173#define RADIOLIB_LORAWAN_MAC_DEVICE_TIME (0x0D)
174#define RADIOLIB_LORAWAN_MAC_FORCE_REJOIN (0x0E)
175#define RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP (0x0F)
176#define RADIOLIB_LORAWAN_MAC_DEVICE_MODE (0x20)
177#define RADIOLIB_LORAWAN_MAC_PROPRIETARY (0x80)
178
179// the maximum number of simultaneously available channels
180#define RADIOLIB_LORAWAN_MAX_NUM_DYNAMIC_CHANNELS (16)
181#define RADIOLIB_LORAWAN_MAX_NUM_SUBBANDS (12)
182#define RADIOLIB_LORAWAN_MAX_NUM_FIXED_CHANNELS (96)
183
184// maximum MAC command sizes
185#define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN (5)
186#define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_UP (2)
187#define RADIOLIB_LORAWAN_MAX_NUM_ADR_COMMANDS (8)
188
189#define RADIOLIB_LORAWAN_MAX_PAYLOAD_SIZE (242)
190
191// session states
192#define RADIOLIB_LORAWAN_SESSION_NONE (0x00)
193#define RADIOLIB_LORAWAN_SESSION_ACTIVATING (0x01)
194#define RADIOLIB_LORAWAN_SESSION_PENDING (0x02)
195#define RADIOLIB_LORAWAN_SESSION_ACTIVE (0x03)
196
197// threshold at which sleeping via user callback enabled, in ms
198#define RADIOLIB_LORAWAN_DELAY_SLEEP_THRESHOLD (50)
199
206 const uint8_t cid;
207
209 const uint8_t lenDn;
210
212 const uint8_t lenUp;
213
215 const bool persist;
216
218 const bool user;
219};
220
221#define RADIOLIB_LORAWAN_MAC_COMMAND_NONE { .cid = 0, .lenDn = 0, .lenUp = 0, .persist = false, .user = false }
222
223constexpr LoRaWANMacCommand_t MacTable[RADIOLIB_LORAWAN_NUM_MAC_COMMANDS] = {
224 { RADIOLIB_LORAWAN_MAC_RESET, 1, 1, true, false },
225 { RADIOLIB_LORAWAN_MAC_LINK_CHECK, 2, 0, false, true },
226 { RADIOLIB_LORAWAN_MAC_LINK_ADR, 4, 1, false, false },
227 { RADIOLIB_LORAWAN_MAC_DUTY_CYCLE, 1, 0, false, false },
228 { RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP, 4, 1, true, false },
229 { RADIOLIB_LORAWAN_MAC_DEV_STATUS, 0, 2, false, false },
230 { RADIOLIB_LORAWAN_MAC_NEW_CHANNEL, 5, 1, false, false },
231 { RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP, 1, 0, true, false },
232 { RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP, 1, 0, true, false },
233 { RADIOLIB_LORAWAN_MAC_DL_CHANNEL, 4, 1, true, false },
234 { RADIOLIB_LORAWAN_MAC_REKEY, 1, 1, true, false },
235 { RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP, 1, 0, false, false },
236 { RADIOLIB_LORAWAN_MAC_DEVICE_TIME, 5, 0, false, true },
237 { RADIOLIB_LORAWAN_MAC_FORCE_REJOIN, 2, 0, false, false },
238 { RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP, 1, 1, false, false },
239 { RADIOLIB_LORAWAN_MAC_DEVICE_MODE, 1, 1, true, false },
240 { RADIOLIB_LORAWAN_MAC_PROPRIETARY, 5, 0, false, true },
241};
242
243// maximum number of simultaneously available multicast groups
244#define RADIOLIB_LORAWAN_MAX_NUM_MC_GROUPS (4)
245
248 uint8_t cls;
249
251 uint32_t mcAddr;
252
254 uint8_t mcAppSKey[RADIOLIB_AES128_KEY_SIZE];
255
257 uint8_t mcNwkSKey[RADIOLIB_AES128_KEY_SIZE];
258
260 uint32_t mcFCnt;
261
263 uint32_t mcFCntMax;
264
266 uint32_t rxFCnt;
267
269 uint32_t mcFreq;
270
272 uint8_t mcDr;
273};
274
275#define RADIOLIB_MULTICAST_GROUP_NONE \
276 { .cls = RADIOLIB_LORAWAN_CLASS_A, .mcAddr = 0, .mcAppSKey = { 0 }, .mcNwkSKey = { 0 }, \
277 .mcFCnt = 0, .mcFCntMax = 0xFFFFFFFF, .rxFCnt = 0, .mcFreq = 0, .mcDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
278
279// currently known packages with reserved FPorts:
280// TS007 (MultiPackage), TS009 (Certification), TS011 (Relay)
281#define RADIOLIB_LORAWAN_NUM_RESERVED_PACKAGES (3)
282
290
293};
294
295#define RADIOLIB_LORAWAN_NONCES_VERSION_VAL (0x0003)
296
297enum LoRaWANSchemeBase_t {
298 RADIOLIB_LORAWAN_NONCES_START = 0x00,
299 RADIOLIB_LORAWAN_NONCES_VERSION = RADIOLIB_LORAWAN_NONCES_START, // 2 bytes
300 RADIOLIB_LORAWAN_NONCES_MODE = RADIOLIB_LORAWAN_NONCES_VERSION + sizeof(uint16_t), // 2 bytes
301 RADIOLIB_LORAWAN_NONCES_PLAN = RADIOLIB_LORAWAN_NONCES_MODE + sizeof(uint16_t), // 1 byte
302 RADIOLIB_LORAWAN_NONCES_CHECKSUM = RADIOLIB_LORAWAN_NONCES_PLAN + sizeof(uint8_t), // 2 bytes
303 RADIOLIB_LORAWAN_NONCES_DEV_NONCE = RADIOLIB_LORAWAN_NONCES_CHECKSUM + sizeof(uint16_t), // 2 bytes
304 RADIOLIB_LORAWAN_NONCES_JOIN_NONCE = RADIOLIB_LORAWAN_NONCES_DEV_NONCE + sizeof(uint16_t), // 3 bytes
305 RADIOLIB_LORAWAN_NONCES_SIGNATURE = RADIOLIB_LORAWAN_NONCES_JOIN_NONCE + 3, // 2 bytes
306 RADIOLIB_LORAWAN_NONCES_BUF_SIZE = RADIOLIB_LORAWAN_NONCES_SIGNATURE + sizeof(uint16_t) // Nonces buffer size
307};
308
309enum LoRaWANSchemeSession_t {
310 RADIOLIB_LORAWAN_SESSION_START = 0x00,
311 RADIOLIB_LORAWAN_SESSION_STATUS = RADIOLIB_LORAWAN_SESSION_START, // 1 byte
312 RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY = RADIOLIB_LORAWAN_SESSION_STATUS + 1, // 16 bytes
313 RADIOLIB_LORAWAN_SESSION_APP_SKEY = RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
314 RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_APP_SKEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
315 RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
316 RADIOLIB_LORAWAN_SESSION_DEV_ADDR = RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY + RADIOLIB_AES128_KEY_SIZE, // 4 bytes
317 RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE = RADIOLIB_LORAWAN_SESSION_DEV_ADDR + sizeof(uint32_t), // 2 bytes
318 RADIOLIB_LORAWAN_SESSION_FCNT_UP = RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE + sizeof(uint16_t), // 4 bytes
319 RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_FCNT_UP + sizeof(uint32_t), // 4 bytes
320 RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
321 RADIOLIB_LORAWAN_SESSION_ADR_FCNT = RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
322 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP = RADIOLIB_LORAWAN_SESSION_ADR_FCNT + sizeof(uint32_t), // 4 bytes
323 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP + sizeof(uint32_t), // 4 bytes
324 RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN + sizeof(uint32_t), // 2 bytes
325 RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 = RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 + sizeof(uint16_t), // 2 bytes
326 RADIOLIB_LORAWAN_SESSION_RX_A_FCNT = RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 + sizeof(uint16_t), // 4 bytes
327 RADIOLIB_LORAWAN_SESSION_VERSION = RADIOLIB_LORAWAN_SESSION_RX_A_FCNT + sizeof(uint32_t), // 1 byte
328 RADIOLIB_LORAWAN_SESSION_CLASS = RADIOLIB_LORAWAN_SESSION_VERSION + 1, // 1 byte
329 RADIOLIB_LORAWAN_SESSION_LINK_ADR = RADIOLIB_LORAWAN_SESSION_CLASS + sizeof(uint8_t), // 14 bytes
330 RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE = RADIOLIB_LORAWAN_SESSION_LINK_ADR + 14, // 1 byte
331 RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE + 1, // 4 bytes
332 RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP = RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP + 4, // 1 byte
333 RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP + 1, // 1 byte
334 RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP + 1, // 1 byte
335 RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP + 1, // 1 byte
336 RADIOLIB_LORAWAN_SESSION_UL_CHANNELS = RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP + 1, // 16*5 bytes
337 RADIOLIB_LORAWAN_SESSION_DL_CHANNELS = RADIOLIB_LORAWAN_SESSION_UL_CHANNELS + RADIOLIB_LORAWAN_MAX_NUM_DYNAMIC_CHANNELS*5, // 16*4 bytes
338 RADIOLIB_LORAWAN_SESSION_AVAILABLE_CHANNELS = RADIOLIB_LORAWAN_SESSION_DL_CHANNELS + RADIOLIB_LORAWAN_MAX_NUM_DYNAMIC_CHANNELS*4, // 2 bytes
339 RADIOLIB_LORAWAN_SESSION_MAC_QUEUE = RADIOLIB_LORAWAN_SESSION_AVAILABLE_CHANNELS + RADIOLIB_LORAWAN_MAX_NUM_SUBBANDS, // 12 bytes // 15 bytes
340 RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_LEN = RADIOLIB_LORAWAN_SESSION_MAC_QUEUE + RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN, // 1 byte
341 RADIOLIB_LORAWAN_SESSION_SIGNATURE = RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_LEN + sizeof(uint8_t), // 2 bytes
342 RADIOLIB_LORAWAN_SESSION_BUF_SIZE = RADIOLIB_LORAWAN_SESSION_SIGNATURE + sizeof(uint16_t) // Session buffer size
343};
344
352 uint8_t idx;
353
355 uint32_t freq;
356
358 uint8_t drMin;
359
361 uint8_t drMax;
362
364 uint8_t dr;
365};
366
367// alias for unused channel
368#define RADIOLIB_LORAWAN_CHANNEL_NONE { .idx = 0, .freq = 0, .drMin = 0, .drMax = 0, \
369 .dr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
370
378 uint8_t numChannels;
379
381 uint32_t freqStart;
382
384 uint32_t freqStep;
385
387 uint8_t drMin;
388
390 uint8_t drMax;
391
394};
395
396// alias for unused channel span
397#define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .numChannels = 0, .freqStart = 0, .freqStep = 0, .drMin = 0, .drMax = 0, .drJoinRequest = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
398
400 ModemType_t modem;
401 DataRate_t dr;
403};
404
405#define RADIOLIB_DATARATE_NONE { .modem = RADIOLIB_MODEM_NONE, .dr = {.lora = {0, 0, 0}}, .pc = {.lora = { 8, 0, 0, 0}}}
406
413 uint8_t bandNum;
414
416 uint8_t bandType;
417
419 uint32_t freqMin;
420
422 uint32_t freqMax;
423
425 uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
426
428 int8_t powerMax;
429
432
435
438
441
444
447
449 uint8_t numTxSpans;
450
453
456
457 uint8_t rx1DrTable[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES][8];
458
461
464
467
469 LoRaWANDataRate_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
470};
471
472// supported bands
473extern const LoRaWANBand_t EU868;
474extern const LoRaWANBand_t US915;
475extern const LoRaWANBand_t EU433;
476extern const LoRaWANBand_t AU915;
477extern const LoRaWANBand_t CN470;
478extern const LoRaWANBand_t AS923;
479extern const LoRaWANBand_t AS923_2;
480extern const LoRaWANBand_t AS923_3;
481extern const LoRaWANBand_t AS923_4;
482extern const LoRaWANBand_t KR920;
483extern const LoRaWANBand_t IN865;
484
489enum LoRaWANBandNum_t {
490 BandEU868,
491 BandUS915,
492 BandEU433,
493 BandAU915,
494 BandCN470,
495 BandAS923,
496 BandAS923_2,
497 BandAS923_3,
498 BandAS923_4,
499 BandKR920,
500 BandIN865,
501 BandLast
502};
503
504// provide easy access to the number of currently supported bands
505#define RADIOLIB_LORAWAN_NUM_SUPPORTED_BANDS (BandLast - BandEU868)
506
507// array of currently supported bands
508extern const LoRaWANBand_t* LoRaWANBands[];
509
516 bool newSession = false;
517
519 uint16_t devNonce = 0;
520
522 uint32_t joinNonce = 0;
523};
524
531 uint8_t dir;
532
535
539
542
544 uint8_t datarate;
545
547 float freq;
548
550 int16_t power;
551
553 uint32_t fCnt;
554
556 uint8_t fPort;
557
559 uint8_t nbTrans;
560
563
565 uint8_t mcGroupId;
566};
567
573 public:
574
581 LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band, uint8_t subBand = 0);
582
587 uint8_t* getBufferNonces();
588
594 int16_t setBufferNonces(const uint8_t* persistentBuffer);
595
599 void clearSession();
600
605 uint8_t* getBufferSession();
606
612 int16_t setBufferSession(const uint8_t* persistentBuffer);
613
622 int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, const uint8_t* nwkKey, const uint8_t* appKey);
623
634 int16_t beginABP(uint32_t addr, const uint8_t* fNwkSIntKey, const uint8_t* sNwkSIntKey, const uint8_t* nwkSEncKey, const uint8_t* appSKey);
635
641 virtual int16_t activateOTAA(LoRaWANJoinEvent_t *joinEvent = NULL);
642
648 virtual int16_t activateABP();
649
651 bool isActivated();
652
654 int16_t setClass(uint8_t cls);
655
668 int16_t startMulticastSession(uint8_t cls, uint32_t mcAddr, const uint8_t* mcAppSKey, const uint8_t* mcNwkSKey,
669 uint32_t mcFCntMin = 0, uint32_t mcFCntMax = 0xFFFFFFFF, uint32_t mcFreq = 0,
670 uint8_t mcDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED);
671
679 int16_t startMulticastSession(uint8_t id, MulticastGroup_t* mcGroup);
680
687 bool stopMulticastSession(uint8_t id, bool addRxFCnt = false);
688
689 #if defined(RADIOLIB_BUILD_ARDUINO)
702 virtual int16_t sendReceive(const String& strUp, uint8_t fPort, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
703 #endif
704
716 virtual int16_t sendReceive(const char* strUp, uint8_t fPort, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
717
731 virtual int16_t sendReceive(const char* strUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
732
745 virtual int16_t sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort = 1, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
746
761 virtual int16_t sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
762
771 int16_t getDownlinkClassC(uint8_t* dataDown, size_t* lenDown, LoRaWANEvent_t* eventDown = NULL);
772
780 int16_t sendMacCommandReq(uint8_t cid);
781
788 int16_t getMacLinkCheckAns(uint8_t* margin, uint8_t* gwCnt);
789
800 int16_t getMacDeviceTimeAns(uint32_t* timestamp, uint16_t* milliseconds, bool returnUnix = true);
801
807 int16_t setDatarate(uint8_t drUp);
808
814 int16_t setTxPower(int8_t txPower);
815
822 int16_t setRx2Dr(uint8_t dr);
823
828 void setADR(bool enable = true);
829
836 void setDutyCycle(bool enable = true, RadioLibTime_t msPerHour = 0);
837
844 void setDwellTime(bool enable, RadioLibTime_t msPerUplink = 0);
845
853 void setCSMA(bool csmaEnabled, uint8_t maxChanges = 4, uint8_t backoffMax = 0, uint8_t difsSlots = 2);
854
860 void setDeviceStatus(uint8_t battLevel);
861
867 void setActivityLeds(const uint32_t pins[4]);
868
877
882 const LoRaWANBand_t* getBand();
883
888 uint8_t getClass();
889
894 uint8_t getVersionMajor();
895
900 uint32_t getFCntUp();
901
906 uint32_t getNFCntDown();
907
912 uint32_t getAFCntDown();
913
917 uint32_t getRxFCnt();
918
922 uint32_t getRxFCntMulticast(uint8_t mcGroupId);
923
928 uint32_t getDevAddr();
929
935
940 uint8_t getMacUplinkLen();
941
950
953
958 virtual uint8_t getMaxPayloadLen();
959
964 uint8_t getMulticastClass();
965
967 typedef void (*SleepCb_t)(RadioLibTime_t ms);
968
976 void setSleepFunction(SleepCb_t cb);
977
983 int16_t addAppPackage(uint8_t fPort);
984
990 int16_t addNwkPackage(uint8_t fPort);
991
996 void removePackage(uint8_t fPort);
997
1011
1012#if !RADIOLIB_GODMODE
1013 protected:
1014#endif
1015 PhysicalLayer* phyLayer = NULL;
1016 const LoRaWANBand_t* band = NULL;
1017
1018 // a buffer that holds all LW base parameters that should persist at all times!
1019 uint8_t bufferNonces[RADIOLIB_LORAWAN_NONCES_BUF_SIZE] = { 0 };
1020
1021 // a buffer that holds all LW session parameters that preferably persist, but can be afforded to get lost
1022 uint8_t bufferSession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE] = { 0 };
1023
1024 uint8_t fOptsUp[RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN] = { 0 };
1025 uint8_t fOptsDown[RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN] = { 0 };
1026 uint8_t fOptsUpLen = 0;
1027 uint8_t fOptsDownLen = 0;
1028
1029 uint16_t lwMode = RADIOLIB_LORAWAN_MODE_NONE;
1030 uint8_t lwClass = RADIOLIB_LORAWAN_CLASS_A;
1031 uint8_t sessionStatus = RADIOLIB_LORAWAN_SESSION_NONE;
1032
1033 uint64_t joinEUI = 0;
1034 uint64_t devEUI = 0;
1035 uint8_t nwkKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1036 uint8_t appKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1037
1038 // the following is either provided by the network server (OTAA)
1039 // or directly entered by the user (ABP)
1040 uint32_t devAddr = 0;
1041 uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1042 uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1043 uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1044 uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1045 uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
1046
1047 uint16_t keyCheckSum = 0;
1048
1049 // device-specific parameters, persistent through sessions
1050 uint16_t devNonce = 0;
1051 uint32_t joinNonce = 0;
1052
1053 // session-specific parameters
1054 uint8_t adrLimitExp = RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP;
1055 uint8_t adrDelayExp = RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP;
1056 uint8_t nbTrans = 1; // Number of allowed frame retransmissions
1057 uint8_t txPowerSteps = 0;
1058 uint8_t txPowerMax = 0;
1059 uint32_t fCntUp = 0;
1060 uint32_t aFCntDown = 0;
1061 uint32_t nFCntDown = 0;
1062 uint32_t confFCntUp = RADIOLIB_LORAWAN_FCNT_NONE;
1063 uint32_t confFCntDown = RADIOLIB_LORAWAN_FCNT_NONE;
1064 uint32_t adrFCnt = 0;
1065 uint32_t rxAFCnt = 0; // Number of received downlinks
1066
1067 // ADR is enabled by default
1068 bool adrEnabled = true;
1069
1070 // duty cycle is set upon initialization and activated in regions that impose this
1071 bool dutyCycleEnabled = false;
1072 uint32_t dutyCycle = 0;
1073
1074 // dwell time is set upon initialization and activated in regions that impose this
1075 uint16_t dwellTimeUp = 0;
1076 uint16_t dwellTimeDn = 0;
1077
1078 RadioLibTime_t tUplink = 0; // scheduled uplink transmission time (internal clock)
1079 RadioLibTime_t tDownlink = 0; // time at end of downlink reception
1080
1081 // multicast groups
1082 MulticastGroup_t mcGroups[RADIOLIB_LORAWAN_MAX_NUM_MC_GROUPS];
1083
1084 // enabled app/nwk packages in the Reserved FPort range
1085 LoRaWANPackage_t packages[RADIOLIB_LORAWAN_NUM_RESERVED_PACKAGES];
1086
1087 // enable/disable CSMA for LoRaWAN
1088 bool csmaEnabled = false;
1089
1090 // maximum number of channel hops during CSMA
1091 uint8_t maxChanges = RADIOLIB_LORAWAN_MAX_CHANGES_DEFAULT;
1092
1093 // number of backoff slots to be checked after DIFS phase.
1094 // A random BO avoids collisions in the case where two or more nodes start the CSMA
1095 // process at the same time.
1096 uint8_t backoffMax = RADIOLIB_LORAWAN_BACKOFF_MAX_DEFAULT;
1097
1098 // number of CADs to estimate a clear channel
1099 uint8_t difsSlots = RADIOLIB_LORAWAN_DIFS_DEFAULT;
1100
1101 // dynamic channels
1102 LoRaWANChannel_t dynamicChannels[2][RADIOLIB_LORAWAN_MAX_NUM_DYNAMIC_CHANNELS];
1103
1104 // masks: which channels are defined/active; flags: which should be used next
1105 uint16_t channelMasks[RADIOLIB_LORAWAN_MAX_NUM_FIXED_CHANNELS / 16] = { 0 };
1106 uint16_t channelFlags[RADIOLIB_LORAWAN_MAX_NUM_FIXED_CHANNELS / 16] = { 0 };
1107
1108 // currently configured channels for Tx, Rx1, Rx2, RxBC
1109 LoRaWANChannel_t channels[4] = { RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE,
1110 RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE };
1111
1112 // delays between the uplink and Rx windows
1113 // the first field is meaningless, but is used for offsetting the Rx windows
1114 RadioLibTime_t rxDelays[4] = { 0,
1115 RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS,
1116 RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS,
1117 0 };
1118
1119 uint32_t ledPins[4] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
1120
1121 // offset between Tx and Rx1 (such that Rx1 has equal or lower DR)
1122 uint8_t rx1DrOffset = 0;
1123
1124 // LoRaWAN revision (1.0 vs 1.1)
1125 uint8_t rev = 0;
1126
1127 // Time on Air of last uplink
1128 RadioLibTime_t lastToA = 0;
1129
1130 // timestamp to measure the Rx1/2 delay (from uplink end)
1131 RadioLibTime_t tUplinkEnd = 0;
1132
1133 // duration of SPI transaction for phyLayer->launchMode()
1134 RadioLibTime_t launchDuration = 0;
1135
1136 // device status - battery level
1137 uint8_t battLevel = 0xFF;
1138
1139 // indicates whether an uplink has MAC commands as payload
1140 bool isMACPayload = false;
1141
1142 // save the selected sub-band in case this must be restored in ADR control
1143 uint8_t subBand = 0;
1144
1145 // user-provided sleep callback
1146 SleepCb_t sleepCb = nullptr;
1147
1148 // this will reset the device credentials, so the device starts completely new
1149 void clearNonces();
1150
1151 // setup an empty session with default parameters
1152 void createSession();
1153
1154 // setup Join-Request payload
1155 void composeJoinRequest(uint8_t* joinRequestMsg);
1156
1157 // extract Join-Accept payload and start a new session
1158 int16_t processJoinAccept(LoRaWANJoinEvent_t *joinEvent);
1159
1160 // a join-accept can piggy-back a set of channels or channel masks
1161 void processCFList(const uint8_t* cfList);
1162
1163 // check whether payload length and fport are allowed
1164 int16_t isValidUplink(size_t len, uint8_t fPort);
1165
1166 // perform ADR backoff
1167 void adrBackoff();
1168
1169 // create an encrypted uplink buffer, composing metadata, user data and MAC data
1170 void composeUplink(const uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed);
1171
1172 // generate and set the MIC of an uplink buffer (depends on selected channels)
1173 void micUplink(uint8_t* inOut, size_t lenInOut);
1174
1175 // transmit uplink buffer on a specified channel
1176 int16_t transmitUplink(const LoRaWANChannel_t* chnl, uint8_t* in, uint8_t len);
1177
1178 // handle one of the Class A receive windows with a given channel and certain timestamps
1179 int16_t receiveClassA(uint8_t dir, const LoRaWANChannel_t* dlChannel, uint8_t window, const RadioLibTime_t dlDelay, RadioLibTime_t tReference);
1180
1181 // handle a Class C receive window with timeout (between Class A windows) or without (between uplinks)
1182 int16_t receiveClassC(RadioLibTime_t timeout = 0);
1183
1184 // open a series of Class A (and C) downlinks
1185 virtual int16_t receiveDownlink();
1186
1187 // extract downlink payload and process MAC commands
1188 int16_t parseDownlink(uint8_t* data, size_t* len, uint8_t window, LoRaWANEvent_t* event = NULL);
1189
1190 // execute mac command, return the number of processed bytes for sequential processing
1191 bool execMacCommand(uint8_t cid, uint8_t* optIn, uint8_t lenIn);
1192 bool execMacCommand(uint8_t cid, uint8_t* optIn, uint8_t lenIn, uint8_t* optOut);
1193
1194 // possible override for additional MAC commands that are not in the base specification
1195 virtual bool derivedMacHandler(uint8_t cid, uint8_t* optIn, uint8_t lenIn, uint8_t* optOut);
1196
1197 // pre-process a (set of) LinkAdrReq commands into one super-channel-mask + Tx/Dr/NbTrans fields
1198 void preprocessMacLinkAdr(uint8_t* mPtr, uint8_t cLen, uint8_t* mAdrOpt);
1199
1200 // post-process a (set of) LinkAdrAns commands depending on LoRaWAN version
1201 void postprocessMacLinkAdr(uint8_t* ack, uint8_t cLen);
1202
1203 // get the properties of a MAC command given a certain command ID
1204 int16_t getMacCommand(uint8_t cid, LoRaWANMacCommand_t* cmd);
1205
1206 // possible override for additional MAC commands that are not in the base specification
1207 virtual int16_t derivedMacFinder(uint8_t cid, LoRaWANMacCommand_t* cmd);
1208
1209 // get the length of a certain MAC command in a specific direction (up/down)
1210 // if inclusive is true, add one for the CID byte
1211 // include payload in case the MAC command has a dynamic length
1212 virtual int16_t getMacLen(uint8_t cid, uint8_t* len, uint8_t dir, bool inclusive = false, uint8_t* payload = NULL);
1213
1214 // find out of a MAC command should persist destruction
1215 // in uplink direction, some commands must persist if no downlink is received
1216 // in downlink direction, the user-accessible MAC commands remain available for retrieval
1217 bool isPersistentMacCommand(uint8_t cid, uint8_t dir);
1218
1219 // push MAC command to queue, done by copy
1220 int16_t pushMacCommand(uint8_t cid, const uint8_t* cOcts, uint8_t* out, uint8_t* lenOut, uint8_t dir);
1221
1222 // retrieve the payload of a certain MAC command, if present in the buffer
1223 int16_t getMacPayload(uint8_t cid, const uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t dir);
1224
1225 // delete a specific MAC command from queue, indicated by the command ID
1226 int16_t deleteMacCommand(uint8_t cid, uint8_t* inOut, uint8_t* lenInOut, uint8_t dir);
1227
1228 // clear a MAC buffer, possible retaining persistent MAC commands
1229 void clearMacCommands(uint8_t* inOut, uint8_t* lenInOut, uint8_t dir);
1230
1231 // configure the common physical layer properties (frequency, sync word etc.)
1232 int16_t setPhyProperties(const LoRaWANChannel_t* chnl, uint8_t dir, int8_t pwr, size_t pre = 0);
1233
1234 // Performs CSMA as per LoRa Alliance Technical Recommendation 13 (TR-013).
1235 bool csmaChannelClear(uint8_t difs, uint8_t numBackoff);
1236
1237 // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise.
1238 bool cadChannelClear();
1239
1240 // enable all default channels on top of the current channels
1241 void enableDefaultChannels(bool addDynamic = false);
1242
1243 // calculate which channels are available given the current datarate
1244 // returns true if there is any such channel, false otherwise
1245 bool calculateChannelFlags();
1246
1247 // select a set of random TX/RX channels for up- and downlink
1248 int16_t selectChannels();
1249
1250 // get the properties of the first active multicast session, if any
1251 uint32_t getMulticastFrequency();
1252 uint8_t getMulticastDatarate();
1253 bool isMulticastDevAddr(uint32_t devAddr, uint8_t* mcGroupId);
1254
1255 // method to generate message integrity code
1256 uint32_t generateMIC(const uint8_t* msg, size_t len, uint8_t* key);
1257
1258 // method to verify message integrity code
1259 // it assumes that the MIC is the last 4 bytes of the message
1260 bool verifyMIC(uint8_t* msg, size_t len, uint8_t* key);
1261
1262 // function to encrypt and decrypt payloads (regular uplink/downlink)
1263 void processAES(const uint8_t* in, size_t len, uint8_t* key, uint8_t* out, uint32_t addr, uint32_t fCnt, uint8_t dir, uint8_t ctrId, bool counter);
1264
1265 // common function add an app/nwk package, used by both addAppPackage and addNwkPackage
1266 int16_t addPackage(uint8_t fPort, bool isApp);
1267
1268 // function that allows sleeping via user-provided callback
1269 void sleepDelay(RadioLibTime_t ms, bool radioOff = true);
1270
1271 // 16-bit checksum method that takes a uint8_t array of even length and calculates the checksum
1272 static uint16_t checkSum16(const uint8_t *key, uint16_t keyLen);
1273
1274 // check the integrity of a buffer using a 16-bit checksum located in the last two bytes of the buffer
1275 static int16_t checkBufferCommon(const uint8_t *buffer, uint16_t size);
1276
1277 // network-to-host conversion method - takes data from network packet and converts it to the host endians
1278 template<typename T>
1279 static T ntoh(const uint8_t* buff, size_t size = 0);
1280
1281 // host-to-network conversion method - takes data from host variable and and converts it to network packet endians
1282 template<typename T>
1283 static void hton(uint8_t* buff, T val, size_t size = 0);
1284};
1285
1286template<typename T>
1287T LoRaWANNode::ntoh(const uint8_t* buff, size_t size) {
1288 const uint8_t* buffPtr = buff;
1289 size_t targetSize = sizeof(T);
1290 if(size != 0) {
1291 targetSize = size;
1292 }
1293 T res = 0;
1294 for(size_t i = 0; i < targetSize; i++) {
1295 res |= (uint32_t)(*(buffPtr++)) << 8*i;
1296 }
1297 return(res);
1298}
1299
1300template<typename T>
1301void LoRaWANNode::hton(uint8_t* buff, T val, size_t size) {
1302 uint8_t* buffPtr = buff;
1303 size_t targetSize = sizeof(T);
1304 if(size != 0) {
1305 targetSize = size;
1306 }
1307 for(size_t i = 0; i < targetSize; i++) {
1308 *(buffPtr++) = val >> 8*i;
1309 }
1310}
1311
1312#endif
LoRaWAN-compatible node (class A device).
Definition LoRaWAN.h:572
void clearSession()
Clear an active session. This requires the device to rejoin the network.
Definition LoRaWAN.cpp:327
virtual int16_t sendReceive(const char *strUp, uint8_t fPort, 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:47
void setSleepFunction(SleepCb_t cb)
Set custom delay/sleep function callback. If set, LoRaWAN node will call this function to wait for pe...
Definition LoRaWAN.cpp:3773
int16_t startMulticastSession(uint8_t cls, uint32_t mcAddr, const uint8_t *mcAppSKey, const uint8_t *mcNwkSKey, uint32_t mcFCntMin=0, uint32_t mcFCntMax=0xFFFFFFFF, uint32_t mcFreq=0, uint8_t mcDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
Start a Multicast session.
Definition LoRaWAN.cpp:1136
int16_t setDatarate(uint8_t drUp)
Set uplink datarate. This should not be used when ADR is enabled.
Definition LoRaWAN.cpp:3112
void scheduleTransmission(RadioLibTime_t tUplink)
Set the exact time a transmission should occur. Note: this is the internal clock time....
Definition LoRaWAN.cpp:3255
RadioLibTime_t scanGuard
Rx window padding in milliseconds according to the spec, the Rx window must be at least enough time t...
Definition LoRaWAN.h:1010
uint8_t getClass()
Get the LoRaWAN class of this node.
Definition LoRaWAN.cpp:3263
void setCSMA(bool csmaEnabled, uint8_t maxChanges=4, uint8_t backoffMax=0, uint8_t difsSlots=2)
Configures CSMA for LoRaWAN as per TR013, LoRa Alliance.
Definition LoRaWAN.cpp:3226
uint32_t getRxFCnt()
Returns the number of received application downlinks.
Definition LoRaWAN.cpp:3287
const LoRaWANBand_t * getBand()
Get the LoRaWAN band used by this node.
Definition LoRaWAN.cpp:3259
void(* SleepCb_t)(RadioLibTime_t ms)
Callback to a user-provided sleep function.
Definition LoRaWAN.h:967
void setDutyCycle(bool enable=true, RadioLibTime_t msPerHour=0)
Toggle adherence to dutyCycle limits to on or off.
Definition LoRaWAN.cpp:3200
uint8_t getMulticastClass()
Request the currently configured class for Multicast.
Definition LoRaWAN.cpp:3643
int16_t beginABP(uint32_t addr, const uint8_t *fNwkSIntKey, const uint8_t *sNwkSIntKey, const uint8_t *nwkSEncKey, const uint8_t *appSKey)
Set the device credentials and activation configuration.
Definition LoRaWAN.cpp:666
RadioLibTime_t timeUntilUplink()
Returns time in milliseconds until next uplink is available under dutyCycle limits.
Definition LoRaWAN.cpp:3727
int16_t getDownlinkClassC(uint8_t *dataDown, size_t *lenDown, LoRaWANEvent_t *eventDown=NULL)
Check if there is an RxC downlink and parse it if available.
Definition LoRaWAN.cpp:2266
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition LoRaWAN.cpp:3240
RadioLibTime_t dutyCycleInterval(RadioLibTime_t msPerHour, RadioLibTime_t airtime)
Calculate the minimum interval to adhere to a certain dutyCycle. This interval is based on the ToA of...
Definition LoRaWAN.cpp:3717
uint8_t getVersionMajor()
Get the LoRaWAN version of this node.
Definition LoRaWAN.cpp:3267
void removePackage(uint8_t fPort)
Disable a package that was previously added.
Definition LoRaWAN.cpp:3795
void setDwellTime(bool enable, RadioLibTime_t msPerUplink=0)
Set or disable uplink dwell time limitation; enabled by default if mandatory.
Definition LoRaWAN.cpp:3213
uint32_t getAFCntDown()
Returns the last application downlink's frame counter; also 0 if no application downlink occured yet.
Definition LoRaWAN.cpp:3283
RadioLibTime_t getLastToA()
Get the Time-on-air of the last uplink message (in milliseconds).
Definition LoRaWAN.cpp:3302
virtual int16_t activateOTAA(LoRaWANJoinEvent_t *joinEvent=NULL)
Join network by restoring OTAA session or performing over-the-air activation. By this procedure,...
Definition LoRaWAN.cpp:905
uint8_t * getBufferNonces()
Returns the pointer to the internal buffer that holds the LW base parameters.
Definition LoRaWAN.cpp:276
uint8_t * getBufferSession()
Returns the pointer to the internal buffer that holds the LW session parameters.
Definition LoRaWAN.cpp:480
bool isActivated()
Whether there is an ongoing session active.
Definition LoRaWAN.cpp:1095
int16_t setTxPower(int8_t txPower)
Configure TX power of the radio module.
Definition LoRaWAN.cpp:3134
int16_t addNwkPackage(uint8_t fPort)
Enable a reserved FPort that can be used for network traffic.
Definition LoRaWAN.cpp:3781
uint32_t getNFCntDown()
Returns the last network downlink's frame counter; also 0 if no network downlink occured yet.
Definition LoRaWAN.cpp:3279
uint32_t getRxFCntMulticast(uint8_t mcGroupId)
Returns the number of received downlinks for a certain multicast group.
Definition LoRaWAN.cpp:3291
int16_t setClass(uint8_t cls)
Configure class (RADIOLIB_LORAWAN_CLASS_A or RADIOLIB_LORAWAN_CLASS_C)
Definition LoRaWAN.cpp:1099
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, const uint8_t *nwkKey, const uint8_t *appKey)
Set the device credentials and activation configuration.
Definition LoRaWAN.cpp:637
virtual uint8_t getMaxPayloadLen()
Returns the maximum allowed uplink payload size given the current MAC state. Most importantly,...
Definition LoRaWAN.cpp:3736
virtual int16_t activateABP()
Join network by restoring ABP session or performing over-the-air activation. In this procedure,...
Definition LoRaWAN.cpp:1002
int16_t addAppPackage(uint8_t fPort)
Enable a reserved FPort that can be used for application traffic.
Definition LoRaWAN.cpp:3777
bool stopMulticastSession(uint8_t id, bool addRxFCnt=false)
Stop an ongoing multicast session.
Definition LoRaWAN.cpp:1209
uint32_t getFCntUp()
Returns the last uplink's frame counter; also 0 if no uplink occured yet.
Definition LoRaWAN.cpp:3272
int16_t setBufferNonces(const uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW base parameters with a supplied buffer.
Definition LoRaWAN.cpp:290
int16_t setRx2Dr(uint8_t dr)
Configure the Rx2 datarate for ABP mode. This should not be needed for LoRaWAN 1.1 as it is configure...
Definition LoRaWAN.cpp:3164
int16_t sendMacCommandReq(uint8_t cid)
Add a MAC command to the uplink queue. Only LinkCheck and DeviceTime are available to the user....
Definition LoRaWAN.cpp:2914
int16_t getMacLinkCheckAns(uint8_t *margin, uint8_t *gwCnt)
Returns the quality of connectivity after requesting a LinkCheck MAC command.
Definition LoRaWAN.cpp:2938
void setADR(bool enable=true)
Toggle ADR to on or off.
Definition LoRaWAN.cpp:3196
void setActivityLeds(const uint32_t pins[4])
Set pins for activity LEDs that will indicate when the radio is transmitting (Tx) or receiving (Rx).
Definition LoRaWAN.cpp:3244
int16_t getMacDeviceTimeAns(uint32_t *timestamp, uint16_t *milliseconds, bool returnUnix=true)
Returns the network time after requesting a DeviceTime MAC command. Note: the network returns the tim...
Definition LoRaWAN.cpp:2949
int16_t setBufferSession(const uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW session parameters with a supplied buffer.
Definition LoRaWAN.cpp:509
uint32_t getDevAddr()
Returns the DevAddr of the device, regardless of OTAA or ABP mode.
Definition LoRaWAN.cpp:3298
uint8_t getMacUplinkLen()
Get the length of the pending MAC uplink payload.
Definition LoRaWAN.cpp:3306
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition PhysicalLayer.h:257
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition TypeDef.h:679
Structure to save information about LoRaWAN band.
Definition LoRaWAN.h:411
LoRaWANDataRate_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
The corresponding datarates, bandwidths and coding rates for DR index.
Definition LoRaWAN.h:469
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition LoRaWAN.h:428
bool txParamSupported
Whether this band implements the MAC command TxParamSetupReq.
Definition LoRaWAN.h:443
LoRaWANChannel_t txAck[2]
Relay channels for ACK downlink.
Definition LoRaWAN.h:466
RadioLibTime_t dutyCycle
Number of milliseconds per hour of allowed Time-on-Air.
Definition LoRaWAN.h:434
RadioLibTime_t dwellTimeUp
Maximum dwell time per uplink message in milliseconds.
Definition LoRaWAN.h:437
LoRaWANChannel_t txWoR[2]
Relay channels for WoR uplink.
Definition LoRaWAN.h:463
uint32_t freqMin
Minimum allowed frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:419
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum application payload lengths for each data rate (N-value)
Definition LoRaWAN.h:425
LoRaWANChannel_t txFreqs[3]
A set of default uplink (TX) channels for dynamic bands.
Definition LoRaWAN.h:446
int8_t powerNumSteps
Number of power steps in this band.
Definition LoRaWAN.h:431
LoRaWANChannelSpan_t txSpans[2]
Default uplink (TX) channel spans for fixed bands, including Join-Request parameters.
Definition LoRaWAN.h:452
uint8_t bandNum
Identier for this band.
Definition LoRaWAN.h:413
uint32_t freqMax
Maximum allowed frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:422
uint8_t bandType
Whether the channels are fixed per specification, or dynamically allocated through the network (plus ...
Definition LoRaWAN.h:416
LoRaWANChannel_t rx2
Backup channel for downlink (RX2) window.
Definition LoRaWAN.h:460
uint8_t numTxSpans
The number of TX channel spans for fixed bands.
Definition LoRaWAN.h:449
RadioLibTime_t dwellTimeDn
Maximum dwell time per downlink message in milliseconds.
Definition LoRaWAN.h:440
LoRaWANChannelSpan_t rx1Span
Default downlink (RX1) channel span for fixed bands.
Definition LoRaWAN.h:455
IDs of all currently supported bands.
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition LoRaWAN.h:350
uint8_t dr
Datarate currently in use on this channel.
Definition LoRaWAN.h:364
uint32_t freq
The channel frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:355
uint8_t idx
The channel number, as specified by defaults or the network.
Definition LoRaWAN.h:352
uint8_t drMin
Minimum allowed datarate for this channel.
Definition LoRaWAN.h:358
uint8_t drMax
Maximum allowed datarate for this channel (inclusive)
Definition LoRaWAN.h:361
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition LoRaWAN.h:376
uint8_t drJoinRequest
Allowed data rates for a join request message.
Definition LoRaWAN.h:393
uint32_t freqStart
Center frequency of the first channel in span (coded in 100 Hz steps)
Definition LoRaWAN.h:381
uint8_t numChannels
Total number of channels in the span.
Definition LoRaWAN.h:378
uint8_t drMax
Maximum allowed datarate for all channels in this span (inclusive)
Definition LoRaWAN.h:390
uint32_t freqStep
Frequency step between adjacent channels (coded in 100 Hz steps)
Definition LoRaWAN.h:384
uint8_t drMin
Minimum allowed datarate for all channels in this span.
Definition LoRaWAN.h:387
Definition LoRaWAN.h:399
Structure to save extra information about uplink/downlink event.
Definition LoRaWAN.h:529
float freq
Frequency in MHz.
Definition LoRaWAN.h:547
bool confirmed
Whether the event is confirmed or not (e.g., confirmed uplink sent by user application)
Definition LoRaWAN.h:534
uint8_t fPort
Port number.
Definition LoRaWAN.h:556
int16_t power
Transmit power in dBm for uplink, or RSSI for downlink.
Definition LoRaWAN.h:550
bool confirming
Whether the event is confirming a previous request (e.g., server downlink reply to confirmed uplink s...
Definition LoRaWAN.h:538
uint8_t datarate
Datarate.
Definition LoRaWAN.h:544
uint8_t dir
Event direction, one of RADIOLIB_LORAWAN_CHANNEL_DIR_*.
Definition LoRaWAN.h:531
uint8_t nbTrans
Number of times this uplink was transmitted (ADR)
Definition LoRaWAN.h:559
uint32_t fCnt
The appropriate frame counter - for different events, different frame counters will be reported!
Definition LoRaWAN.h:553
bool multicast
Multicast or unicast.
Definition LoRaWAN.h:562
uint8_t mcGroupId
Multicast Group ID if this was a multicast downlink.
Definition LoRaWAN.h:565
bool frmPending
Whether further downlink messages are pending on the server side.
Definition LoRaWAN.h:541
Structure to save extra information about activation event.
Definition LoRaWAN.h:514
bool newSession
Whether a new session was started.
Definition LoRaWAN.h:516
uint32_t joinNonce
The received Join-Request JoinNonce value.
Definition LoRaWAN.h:522
uint16_t devNonce
The transmitted Join-Request DevNonce value.
Definition LoRaWAN.h:519
MAC command specification structure.
Definition LoRaWAN.h:204
const uint8_t lenDn
Uplink message length.
Definition LoRaWAN.h:209
const bool user
Whether this MAC command can be issued by the user or not.
Definition LoRaWAN.h:218
const uint8_t cid
Command ID.
Definition LoRaWAN.h:206
const uint8_t lenUp
Downlink message length.
Definition LoRaWAN.h:212
const bool persist
Some commands must be resent until Class A downlink received.
Definition LoRaWAN.h:215
LoRaWAN Packages structure (for TSxxx documents).
Definition LoRaWAN.h:287
bool isAppPack
Whether the package runs through the Application layer.
Definition LoRaWAN.h:292
bool enabled
Whether the package is currently in use.
Definition LoRaWAN.h:289
Definition LoRaWAN.h:246
uint8_t mcDr
The datarate used for the Multicast downlinks.
Definition LoRaWAN.h:272
uint32_t mcFreq
The frequency used for the Multicast downlinks (in Hz).
Definition LoRaWAN.h:269
uint32_t mcAddr
The Multicast address.
Definition LoRaWAN.h:251
uint8_t cls
The LoRaWAN Class used for this session (only C is supported)
Definition LoRaWAN.h:248
uint32_t mcFCntMax
The maximum allowed Multicast frame counter.
Definition LoRaWAN.h:263
uint32_t rxFCnt
The number of received Multicast frames in this group.
Definition LoRaWAN.h:266
uint32_t mcFCnt
The minimum (next) expected Multicast frame counter.
Definition LoRaWAN.h:260
uint8_t mcAppSKey[RADIOLIB_AES128_KEY_SIZE]
The Multicast payload session key.
Definition LoRaWAN.h:254
uint8_t mcNwkSKey[RADIOLIB_AES128_KEY_SIZE]
The Multicast network session key.
Definition LoRaWAN.h:257
Common data rate structure.
Definition PhysicalLayer.h:74
Common packet configuration structure.
Definition PhysicalLayer.h:119