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 (0x0A)
15#define RADIOLIB_LORAWAN_CLASS_B (0x0B)
16#define RADIOLIB_LORAWAN_CLASS_C (0x0C)
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_TS009 (0xE0 << 0) // 7 0 fPort used for TS009 testing
48#define RADIOLIB_LORAWAN_FPORT_TS011 (0xE2 << 0) // 7 0 fPort used for TS011 Forwarding
49#define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0) // 7 0 fPort values equal to and larger than this are reserved
50
51// data rate encoding
52#define RADIOLIB_LORAWAN_DATA_RATE_MODEM (0x03 << 6) // 7 6 modem mask
53#define RADIOLIB_LORAWAN_DATA_RATE_LORA (0x00 << 6) // 7 6 use LoRa modem
54#define RADIOLIB_LORAWAN_DATA_RATE_FSK (0x01 << 6) // 7 6 use FSK modem
55#define RADIOLIB_LORAWAN_DATA_RATE_LR_FHSS (0x02 << 6) // 7 6 use LR-FHSS modem
56#define RADIOLIB_LORAWAN_DATA_RATE_SF (0x07 << 3) // 5 3 LoRa spreading factor mask
57#define RADIOLIB_LORAWAN_DATA_RATE_SF_12 (0x05 << 3) // 5 3 LoRa spreading factor: SF12
58#define RADIOLIB_LORAWAN_DATA_RATE_SF_11 (0x04 << 3) // 5 3 SF11
59#define RADIOLIB_LORAWAN_DATA_RATE_SF_10 (0x03 << 3) // 5 3 SF10
60#define RADIOLIB_LORAWAN_DATA_RATE_SF_9 (0x02 << 3) // 5 3 SF9
61#define RADIOLIB_LORAWAN_DATA_RATE_SF_8 (0x01 << 3) // 5 3 SF8
62#define RADIOLIB_LORAWAN_DATA_RATE_SF_7 (0x00 << 3) // 5 3 SF7
63#define RADIOLIB_LORAWAN_DATA_RATE_BW (0x03 << 1) // 2 1 bandwidth mask
64#define RADIOLIB_LORAWAN_DATA_RATE_BW_125_KHZ (0x00 << 1) // 2 1 125 kHz
65#define RADIOLIB_LORAWAN_DATA_RATE_BW_250_KHZ (0x01 << 1) // 2 1 250 kHz
66#define RADIOLIB_LORAWAN_DATA_RATE_BW_500_KHZ (0x02 << 1) // 2 1 LoRa bandwidth: 500 kHz
67#define RADIOLIB_LORAWAN_DATA_RATE_BW_137_KHZ (0x00 << 1) // 2 1 137 kHz
68#define RADIOLIB_LORAWAN_DATA_RATE_BW_336_KHZ (0x01 << 1) // 2 1 336 kHz
69#define RADIOLIB_LORAWAN_DATA_RATE_BW_1523_KHZ (0x02 << 1) // 2 1 LR-FHSS bandwidth: 1523 kHz
70#define RADIOLIB_LORAWAN_DATA_RATE_CR (0x01 << 0) // 0 0 coding rate mask
71#define RADIOLIB_LORAWAN_DATA_RATE_CR_1_3 (0x00 << 0) // 0 0 LR-FHSS coding rate: 1/3
72#define RADIOLIB_LORAWAN_DATA_RATE_CR_2_3 (0x01 << 0) // 0 0 2/3
73#define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0xFF << 0) // 7 0 unused data rate
74
75// channels and channel plans
76#define RADIOLIB_LORAWAN_UPLINK (0x00 << 0)
77#define RADIOLIB_LORAWAN_DOWNLINK (0x01 << 0)
78#define RADIOLIB_LORAWAN_DIR_RX2 (0x02 << 0)
79#define RADIOLIB_LORAWAN_BAND_DYNAMIC (0)
80#define RADIOLIB_LORAWAN_BAND_FIXED (1)
81#define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (15)
82#define RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE (0xFF >> 0)
83
84// recommended default settings
85#define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
86#define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
87#define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
88#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
89#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
90#define RADIOLIB_LORAWAN_MAX_FCNT_GAP (16384)
91#define RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP (0x06)
92#define RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP (0x05)
93#define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
94#define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
95#define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
96#define RADIOLIB_LORAWAN_REJOIN_MAX_COUNT_N (10) // send rejoin request 16384 uplinks
97#define RADIOLIB_LORAWAN_REJOIN_MAX_TIME_N (15) // once every year, not actually implemented
98
99// join request message layout
100#define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
101#define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
102#define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
103#define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
104#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
105#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
106#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
107#define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
108
109// join accept message layout
110#define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
111#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
112#define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
113#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
114#define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
115#define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
116#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
117#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
118#define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
119
120// join accept key derivation layout
121#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_JOIN_NONCE_POS (1) // regular keys
122#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_JOIN_EUI_POS (4)
123#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_DEV_NONCE_POS (12)
124#define RADIOLIB_LORAWAN_JOIN_ACCEPT_AES_DEV_ADDR_POS (1) // relay keys
125
126// join accept message variables
127#define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7) // 7 7 LoRaWAN revision: 1.0
128#define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7) // 7 7 1.1
129#define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
130#define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
131#define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
132#define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
133#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
134#define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
135#define RADIOLIB_LORAWAN_JOIN_ACCEPT_ROOT_WOR_S_KEY (0x01)
136#define RADIOLIB_LORAWAN_JOIN_ACCEPT_WOR_S_INT_KEY (0x01)
137#define RADIOLIB_LORAWAN_JOIN_ACCEPT_WOR_S_ENC_KEY (0x02)
138
139// frame header layout
140#define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
141#define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
142#define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
143#define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
144#define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
145#define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
146#define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (15)
147#define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
148#define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
149#define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
150
151// payload encryption/MIC blocks common layout
152#define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
153#define RADIOLIB_LORAWAN_BLOCK_CONF_FCNT_POS (1)
154#define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
155#define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
156#define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
157
158// payload encryption block layout
159#define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
160#define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
161#define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
162
163// payload MIC blocks layout
164#define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
165#define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
166#define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
167#define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
168
169// maximum allowed dwell time on bands that implement dwell time limitations
170#define RADIOLIB_LORAWAN_DWELL_TIME (400)
171
172// unused frame counter value
173#define RADIOLIB_LORAWAN_FCNT_NONE (0xFFFFFFFF)
174
175// TR013 CSMA recommended values
176#define RADIOLIB_LORAWAN_DIFS_DEFAULT (2)
177#define RADIOLIB_LORAWAN_BACKOFF_MAX_DEFAULT (6)
178#define RADIOLIB_LORAWAN_MAX_CHANGES_DEFAULT (4)
179
180// MAC commands
181#define RADIOLIB_LORAWAN_NUM_MAC_COMMANDS (23)
182
183#define RADIOLIB_LORAWAN_MAC_RESET (0x01)
184#define RADIOLIB_LORAWAN_MAC_LINK_CHECK (0x02)
185#define RADIOLIB_LORAWAN_MAC_LINK_ADR (0x03)
186#define RADIOLIB_LORAWAN_MAC_DUTY_CYCLE (0x04)
187#define RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP (0x05)
188#define RADIOLIB_LORAWAN_MAC_DEV_STATUS (0x06)
189#define RADIOLIB_LORAWAN_MAC_NEW_CHANNEL (0x07)
190#define RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP (0x08)
191#define RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP (0x09)
192#define RADIOLIB_LORAWAN_MAC_DL_CHANNEL (0x0A)
193#define RADIOLIB_LORAWAN_MAC_REKEY (0x0B)
194#define RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP (0x0C)
195#define RADIOLIB_LORAWAN_MAC_DEVICE_TIME (0x0D)
196#define RADIOLIB_LORAWAN_MAC_FORCE_REJOIN (0x0E)
197#define RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP (0x0F)
198#define RADIOLIB_LORAWAN_MAC_PROPRIETARY (0x80)
199
200// the length of internal MAC command queue - hopefully this is enough for most use cases
201#define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (9)
202
203// the maximum number of simultaneously available channels
204#define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (16)
205
206// maximum MAC command sizes
207#define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN (5)
208#define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_UP (2)
209#define RADIOLIB_LORAWAN_MAX_NUM_ADR_COMMANDS (8)
210
217 const uint8_t cid;
218
220 const uint8_t lenDn;
221
223 const uint8_t lenUp;
224
226 const bool persist;
227
229 const bool user;
230};
231
232#define RADIOLIB_LORAWAN_MAC_COMMAND_NONE { .cid = 0, .lenDn = 0, .lenUp = 0, .persist = false, .user = false }
233
234constexpr LoRaWANMacCommand_t MacTable[RADIOLIB_LORAWAN_NUM_MAC_COMMANDS] = {
235 { RADIOLIB_LORAWAN_MAC_RESET, 1, 1, true, false },
236 { RADIOLIB_LORAWAN_MAC_LINK_CHECK, 2, 0, false, true },
237 { RADIOLIB_LORAWAN_MAC_LINK_ADR, 4, 1, false, false },
238 { RADIOLIB_LORAWAN_MAC_DUTY_CYCLE, 1, 0, false, false },
239 { RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP, 4, 1, true, false },
240 { RADIOLIB_LORAWAN_MAC_DEV_STATUS, 0, 2, false, false },
241 { RADIOLIB_LORAWAN_MAC_NEW_CHANNEL, 5, 1, false, false },
242 { RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP, 1, 0, true, false },
243 { RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP, 1, 0, true, false },
244 { RADIOLIB_LORAWAN_MAC_DL_CHANNEL, 4, 1, true, false },
245 { RADIOLIB_LORAWAN_MAC_REKEY, 1, 1, true, false },
246 { RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP, 1, 0, false, false },
247 { RADIOLIB_LORAWAN_MAC_DEVICE_TIME, 5, 0, false, true },
248 { RADIOLIB_LORAWAN_MAC_FORCE_REJOIN, 2, 0, false, false },
249 { RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP, 1, 1, false, false },
250 { RADIOLIB_LORAWAN_MAC_PROPRIETARY, 5, 0, false, true },
251};
252
253#define RADIOLIB_LORAWAN_NONCES_VERSION_VAL (0x0001)
254
255enum LoRaWANSchemeBase_t {
256 RADIOLIB_LORAWAN_NONCES_START = 0x00,
257 RADIOLIB_LORAWAN_NONCES_VERSION = RADIOLIB_LORAWAN_NONCES_START, // 2 bytes
258 RADIOLIB_LORAWAN_NONCES_MODE = RADIOLIB_LORAWAN_NONCES_VERSION + sizeof(uint16_t), // 2 bytes
259 RADIOLIB_LORAWAN_NONCES_CLASS = RADIOLIB_LORAWAN_NONCES_MODE + sizeof(uint16_t), // 1 byte
260 RADIOLIB_LORAWAN_NONCES_PLAN = RADIOLIB_LORAWAN_NONCES_CLASS + sizeof(uint8_t), // 1 byte
261 RADIOLIB_LORAWAN_NONCES_CHECKSUM = RADIOLIB_LORAWAN_NONCES_PLAN + sizeof(uint8_t), // 2 bytes
262 RADIOLIB_LORAWAN_NONCES_DEV_NONCE = RADIOLIB_LORAWAN_NONCES_CHECKSUM + sizeof(uint16_t), // 2 bytes
263 RADIOLIB_LORAWAN_NONCES_JOIN_NONCE = RADIOLIB_LORAWAN_NONCES_DEV_NONCE + sizeof(uint16_t), // 3 bytes
264 RADIOLIB_LORAWAN_NONCES_ACTIVE = RADIOLIB_LORAWAN_NONCES_JOIN_NONCE + 3, // 1 byte
265 RADIOLIB_LORAWAN_NONCES_SIGNATURE = RADIOLIB_LORAWAN_NONCES_ACTIVE + sizeof(uint8_t), // 2 bytes
266 RADIOLIB_LORAWAN_NONCES_BUF_SIZE = RADIOLIB_LORAWAN_NONCES_SIGNATURE + sizeof(uint16_t) // Nonces buffer size
267};
268
269enum LoRaWANSchemeSession_t {
270 RADIOLIB_LORAWAN_SESSION_START = 0x00,
271 RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY = RADIOLIB_LORAWAN_SESSION_START, // 16 bytes
272 RADIOLIB_LORAWAN_SESSION_APP_SKEY = RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
273 RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_APP_SKEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
274 RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY + RADIOLIB_AES128_KEY_SIZE, // 16 bytes
275 RADIOLIB_LORAWAN_SESSION_DEV_ADDR = RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY + RADIOLIB_AES128_KEY_SIZE, // 4 bytes
276 RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE = RADIOLIB_LORAWAN_SESSION_DEV_ADDR + sizeof(uint32_t), // 2 bytes
277 RADIOLIB_LORAWAN_SESSION_FCNT_UP = RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE + sizeof(uint16_t), // 4 bytes
278 RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_FCNT_UP + sizeof(uint32_t), // 4 bytes
279 RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
280 RADIOLIB_LORAWAN_SESSION_ADR_FCNT = RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
281 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP = RADIOLIB_LORAWAN_SESSION_ADR_FCNT + sizeof(uint32_t), // 4 bytes
282 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP + sizeof(uint32_t), // 4 bytes
283 RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN + sizeof(uint32_t), // 2 bytes
284 RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 = RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 + sizeof(uint16_t), // 2 bytes
285 RADIOLIB_LORAWAN_SESSION_HOMENET_ID = RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 + sizeof(uint16_t), // 4 bytes
286 RADIOLIB_LORAWAN_SESSION_VERSION = RADIOLIB_LORAWAN_SESSION_HOMENET_ID + sizeof(uint32_t), // 1 byte
287 RADIOLIB_LORAWAN_SESSION_LINK_ADR = RADIOLIB_LORAWAN_SESSION_VERSION + sizeof(uint8_t), // 14 bytes
288 RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE = RADIOLIB_LORAWAN_SESSION_LINK_ADR + 14, // 1 byte
289 RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE + 1, // 4 bytes
290 RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP = RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP + 4, // 1 byte
291 RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP + 1, // 1 byte
292 RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP + 1, // 1 byte
293 RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP + 1, // 1 byte
294 RADIOLIB_LORAWAN_SESSION_UL_CHANNELS = RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP + 1, // 16*5 bytes
295 RADIOLIB_LORAWAN_SESSION_DL_CHANNELS = RADIOLIB_LORAWAN_SESSION_UL_CHANNELS + RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS*5, // 16*4 bytes
296 RADIOLIB_LORAWAN_SESSION_AVAILABLE_CHANNELS = RADIOLIB_LORAWAN_SESSION_DL_CHANNELS + RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS*4, // 2 bytes
297 RADIOLIB_LORAWAN_SESSION_MAC_QUEUE = RADIOLIB_LORAWAN_SESSION_AVAILABLE_CHANNELS + sizeof(uint16_t), // 15 bytes
298 RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_LEN = RADIOLIB_LORAWAN_SESSION_MAC_QUEUE + RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN, // 1 byte
299 RADIOLIB_LORAWAN_SESSION_SIGNATURE = RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_LEN + sizeof(uint8_t), // 2 bytes
300 RADIOLIB_LORAWAN_SESSION_BUF_SIZE = RADIOLIB_LORAWAN_SESSION_SIGNATURE + sizeof(uint16_t) // Session buffer size
301};
302
311
313 uint8_t idx;
314
316 uint32_t freq;
317
319 uint8_t drMin;
320
322 uint8_t drMax;
323
325 uint8_t dr;
326
329};
330
331// alias for unused channel
332#define RADIOLIB_LORAWAN_CHANNEL_NONE { .enabled = false, .idx = RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE, .freq = 0, \
333 .drMin = 0, .drMax = 0, .dr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, .available = false }
334
342 uint8_t numChannels;
343
345 uint32_t freqStart;
346
348 uint32_t freqStep;
349
351 uint8_t drMin;
352
354 uint8_t drMax;
355
358};
359
360// alias for unused channel span
361#define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .numChannels = 0, .freqStart = 0, .freqStep = 0, .drMin = 0, .drMax = 0, .drJoinRequest = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
362
369 uint8_t bandNum;
370
372 uint8_t bandType;
373
375 uint32_t freqMin;
376
378 uint32_t freqMax;
379
381 uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
382
384 int8_t powerMax;
385
388
391
394
397
400
403
405 uint8_t numTxSpans;
406
409
412
413 uint8_t rx1DrTable[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES][8];
414
417
420
423
425 uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
426};
427
428// supported bands
429extern const LoRaWANBand_t EU868;
430extern const LoRaWANBand_t US915;
431extern const LoRaWANBand_t EU433;
432extern const LoRaWANBand_t AU915;
433extern const LoRaWANBand_t CN500;
434extern const LoRaWANBand_t AS923;
435extern const LoRaWANBand_t AS923_2;
436extern const LoRaWANBand_t AS923_3;
437extern const LoRaWANBand_t AS923_4;
438extern const LoRaWANBand_t KR920;
439extern const LoRaWANBand_t IN865;
440
445enum LoRaWANBandNum_t {
446 BandEU868,
447 BandUS915,
448 BandEU433,
449 BandAU915,
450 BandCN500,
451 BandAS923,
452 BandAS923_2,
453 BandAS923_3,
454 BandAS923_4,
455 BandKR920,
456 BandIN865,
457 BandLast
458};
459
460// provide easy access to the number of currently supported bands
461#define RADIOLIB_LORAWAN_NUM_SUPPORTED_BANDS (BandLast - BandEU868)
462
463// array of currently supported bands
464extern const LoRaWANBand_t* LoRaWANBands[];
465
472 bool newSession = false;
473
475 uint16_t devNonce = 0;
476
478 uint32_t joinNonce = 0;
479};
480
487 uint8_t dir;
488
491
495
498
500 uint8_t datarate;
501
503 float freq;
504
506 int16_t power;
507
509 uint32_t fCnt;
510
512 uint8_t fPort;
513
515 uint8_t nbTrans;
516};
517
523 public:
524
531 LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band, uint8_t subBand = 0);
532
537 uint8_t* getBufferNonces();
538
544 int16_t setBufferNonces(const uint8_t* persistentBuffer);
545
549 void clearSession();
550
555 uint8_t* getBufferSession();
556
562 int16_t setBufferSession(const uint8_t* persistentBuffer);
563
572 int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, const uint8_t* nwkKey, const uint8_t* appKey);
573
584 int16_t beginABP(uint32_t addr, const uint8_t* fNwkSIntKey, const uint8_t* sNwkSIntKey, const uint8_t* nwkSEncKey, const uint8_t* appSKey);
585
592 virtual int16_t activateOTAA(uint8_t initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, LoRaWANJoinEvent_t *joinEvent = NULL);
593
600 virtual int16_t activateABP(uint8_t initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED);
601
603 bool isActivated();
604
605 #if defined(RADIOLIB_BUILD_ARDUINO)
618 virtual int16_t sendReceive(const String& strUp, uint8_t fPort, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
619 #endif
620
632 virtual int16_t sendReceive(const char* strUp, uint8_t fPort, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
633
647 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);
648
661 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);
662
677 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);
678
686 int16_t sendMacCommandReq(uint8_t cid);
687
696 int16_t getMacLinkCheckAns(uint8_t* margin, uint8_t* gwCnt);
697
707 int16_t getMacDeviceTimeAns(uint32_t* gpsEpoch, uint8_t* fraction, bool returnUnix = true);
708
714 int16_t setDatarate(uint8_t drUp);
715
721 int16_t setTxPower(int8_t txPower);
722
729 int16_t setRx2Dr(uint8_t dr);
730
735 void setADR(bool enable = true);
736
743 void setDutyCycle(bool enable = true, RadioLibTime_t msPerHour = 0);
744
751 void setDwellTime(bool enable, RadioLibTime_t msPerUplink = 0);
752
760 void setCSMA(bool csmaEnabled, uint8_t maxChanges = 4, uint8_t backoffMax = 0, uint8_t difsSlots = 2);
761
767 void setDeviceStatus(uint8_t battLevel);
768
777
782 uint32_t getFCntUp();
783
788 uint32_t getNFCntDown();
789
794 uint32_t getAFCntDown();
795
801 void resetFCntDown();
802
807 uint32_t getDevAddr();
808
814
823
826
831 uint8_t getMaxPayloadLen();
832
837 bool TS009 = false;
838
852
853#if !RADIOLIB_GODMODE
854 protected:
855#endif
856 PhysicalLayer* phyLayer = NULL;
857 const LoRaWANBand_t* band = NULL;
858
859 // a buffer that holds all LW base parameters that should persist at all times!
860 uint8_t bufferNonces[RADIOLIB_LORAWAN_NONCES_BUF_SIZE] = { 0 };
861
862 // a buffer that holds all LW session parameters that preferably persist, but can be afforded to get lost
863 uint8_t bufferSession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE] = { 0 };
864
865 uint8_t fOptsUp[RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN] = { 0 };
866 uint8_t fOptsDown[RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN] = { 0 };
867 uint8_t fOptsUpLen = 0;
868 uint8_t fOptsDownLen = 0;
869
870 uint16_t lwMode = RADIOLIB_LORAWAN_MODE_NONE;
871 uint8_t lwClass = RADIOLIB_LORAWAN_CLASS_A;
872 bool isActive = false;
873
874 uint64_t joinEUI = 0;
875 uint64_t devEUI = 0;
876 uint8_t nwkKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
877 uint8_t appKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
878
879 // the following is either provided by the network server (OTAA)
880 // or directly entered by the user (ABP)
881 uint32_t devAddr = 0;
882 uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
883 uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
884 uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
885 uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
886 uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
887
888 uint16_t keyCheckSum = 0;
889
890 // device-specific parameters, persistent through sessions
891 uint16_t devNonce = 0;
892 uint32_t joinNonce = 0;
893
894 // session-specific parameters
895 uint32_t homeNetId = 0;
896 uint8_t adrLimitExp = RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP;
897 uint8_t adrDelayExp = RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP;
898 uint8_t nbTrans = 1; // Number of allowed frame retransmissions
899 uint8_t txPowerSteps = 0;
900 uint8_t txPowerMax = 0;
901 uint32_t fCntUp = 0;
902 uint32_t aFCntDown = 0;
903 uint32_t nFCntDown = 0;
904 uint32_t confFCntUp = RADIOLIB_LORAWAN_FCNT_NONE;
905 uint32_t confFCntDown = RADIOLIB_LORAWAN_FCNT_NONE;
906 uint32_t adrFCnt = 0;
907
908 // ADR is enabled by default
909 bool adrEnabled = true;
910
911 // duty cycle is set upon initialization and activated in regions that impose this
912 bool dutyCycleEnabled = false;
913 uint32_t dutyCycle = 0;
914
915 // dwell time is set upon initialization and activated in regions that impose this
916 uint16_t dwellTimeUp = 0;
917 uint16_t dwellTimeDn = 0;
918
919 RadioLibTime_t tUplink = 0; // scheduled uplink transmission time (internal clock)
920 RadioLibTime_t tDownlink = 0; // time at end of downlink reception
921
922 // enable/disable CSMA for LoRaWAN
923 bool csmaEnabled = false;
924
925 // maximum number of channel hops during CSMA
926 uint8_t maxChanges = RADIOLIB_LORAWAN_MAX_CHANGES_DEFAULT;
927
928 // number of backoff slots to be checked after DIFS phase.
929 // A random BO avoids collisions in the case where two or more nodes start the CSMA
930 // process at the same time.
931 uint8_t backoffMax = RADIOLIB_LORAWAN_BACKOFF_MAX_DEFAULT;
932
933 // number of CADs to estimate a clear CH
934 uint8_t difsSlots = RADIOLIB_LORAWAN_DIFS_DEFAULT;
935
936 // available channel frequencies from list passed during OTA activation
937 LoRaWANChannel_t channelPlan[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS];
938
939 // currently configured channels for TX, RX1, RX2
940 LoRaWANChannel_t channels[3] = { RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE,
941 RADIOLIB_LORAWAN_CHANNEL_NONE };
942
943 // delays between the uplink and RX1/2 windows
944 // the first field is meaningless, but is used for offsetting for Rx windows 1 and 2
945 RadioLibTime_t rxDelays[3] = { 0, RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
946
947 // offset between TX and RX1 (such that RX1 has equal or lower DR)
948 uint8_t rx1DrOffset = 0;
949
950 // LoRaWAN revision (1.0 vs 1.1)
951 uint8_t rev = 0;
952
953 // Time on Air of last uplink
954 RadioLibTime_t lastToA = 0;
955
956 // timestamp to measure the RX1/2 delay (from uplink end)
957 RadioLibTime_t rxDelayStart = 0;
958
959 // timestamp when the Rx1/2 windows were closed (timeout or uplink received)
960 RadioLibTime_t rxDelayEnd = 0;
961
962 // device status - battery level
963 uint8_t battLevel = 0xFF;
964
965 // indicates whether an uplink has MAC commands as payload
966 bool isMACPayload = false;
967
968 // save the selected sub-band in case this must be restored in ADR control
969 uint8_t subBand = 0;
970
971 // allow port 226 for devices implementing TS011
972 bool TS011 = false;
973
974 // this will reset the device credentials, so the device starts completely new
975 void clearNonces();
976
977 // start a fresh session using default parameters
978 void createSession(uint16_t lwMode, uint8_t initialDr);
979
980 // setup Join-Request payload
981 void composeJoinRequest(uint8_t* joinRequestMsg);
982
983 // extract Join-Accept payload and start a new session
984 int16_t processJoinAccept(LoRaWANJoinEvent_t *joinEvent);
985
986 // a join-accept can piggy-back a set of channels or channel masks
987 void processCFList(uint8_t* cfList);
988
989 // check whether payload length and fport are allowed
990 int16_t isValidUplink(uint8_t* len, uint8_t fPort);
991
992 // perform ADR backoff
993 void adrBackoff();
994
995 // create an encrypted uplink buffer, composing metadata, user data and MAC data
996 void composeUplink(const uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed);
997
998 // generate and set the MIC of an uplink buffer (depends on selected channels)
999 void micUplink(uint8_t* inOut, uint8_t lenInOut);
1000
1001 // transmit uplink buffer on a specified channel
1002 int16_t transmitUplink(LoRaWANChannel_t* chnl, uint8_t* in, uint8_t len, bool retrans);
1003
1004 // wait for, open and listen during receive windows; only performs listening
1005 int16_t receiveCommon(uint8_t dir, const LoRaWANChannel_t* dlChannels, const RadioLibTime_t* dlDelays, uint8_t numWindows, RadioLibTime_t tReference);
1006
1007 // extract downlink payload and process MAC commands
1008 int16_t parseDownlink(uint8_t* data, size_t* len, LoRaWANEvent_t* event = NULL);
1009
1010 // execute mac command, return the number of processed bytes for sequential processing
1011 bool execMacCommand(uint8_t cid, uint8_t* optIn, uint8_t lenIn);
1012 bool execMacCommand(uint8_t cid, uint8_t* optIn, uint8_t lenIn, uint8_t* optOut);
1013
1014 // possible override for additional MAC commands that are not in the base specification
1015 virtual bool derivedMacHandler(uint8_t cid, uint8_t* optIn, uint8_t lenIn, uint8_t* optOut);
1016
1017 // pre-process a (set of) LinkAdrReq commands into one super-channel-mask + Tx/Dr/NbTrans fields
1018 void preprocessMacLinkAdr(uint8_t* mPtr, uint8_t cLen, uint8_t* mAdrOpt);
1019
1020 // post-process a (set of) LinkAdrAns commands depending on LoRaWAN version
1021 void postprocessMacLinkAdr(uint8_t* ack, uint8_t cLen);
1022
1023 // get the properties of a MAC command given a certain command ID
1024 int16_t getMacCommand(uint8_t cid, LoRaWANMacCommand_t* cmd);
1025
1026 // possible override for additional MAC commands that are not in the base specification
1027 virtual int16_t derivedMacFinder(uint8_t cid, LoRaWANMacCommand_t* cmd);
1028
1029 // get the length of a certain MAC command in a specific direction (up/down)
1030 // if inclusive is true, add one for the CID byte
1031 int16_t getMacLen(uint8_t cid, uint8_t* len, uint8_t dir, bool inclusive = false);
1032
1033 // find out of a MAC command should persist destruction
1034 // in uplink direction, some commands must persist if no downlink is received
1035 // in downlink direction, the user-accessible MAC commands remain available for retrieval
1036 bool isPersistentMacCommand(uint8_t cid, uint8_t dir);
1037
1038 // push MAC command to queue, done by copy
1039 int16_t pushMacCommand(uint8_t cid, uint8_t* cOcts, uint8_t* out, uint8_t* lenOut, uint8_t dir);
1040
1041 // retrieve the payload of a certain MAC command, if present in the buffer
1042 int16_t getMacPayload(uint8_t cid, uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t dir);
1043
1044 // delete a specific MAC command from queue, indicated by the command ID
1045 int16_t deleteMacCommand(uint8_t cid, uint8_t* inOut, uint8_t* lenInOut, uint8_t dir);
1046
1047 // clear a MAC buffer, possible retaining persistent MAC commands
1048 void clearMacCommands(uint8_t* inOut, uint8_t* lenInOut, uint8_t dir);
1049
1050 // configure the common physical layer properties (frequency, sync word etc.)
1051 int16_t setPhyProperties(const LoRaWANChannel_t* chnl, uint8_t dir, int8_t pwr, size_t pre = 0);
1052
1053 // Performs CSMA as per LoRa Alliance Technical Recommendation 13 (TR-013).
1054 bool csmaChannelClear(uint8_t difs, uint8_t numBackoff);
1055
1056 // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise.
1057 bool cadChannelClear();
1058
1059 // (dynamic bands:) get or (fixed bands:) create a complete 80-bit channel mask for current configuration
1060 void getChannelPlanMask(uint64_t* chMaskGrp0123, uint32_t* chMaskGrp45);
1061
1062 // setup uplink/downlink channel data rates and frequencies
1063 // for dynamic channels, there is a small set of predefined channels
1064 // in case of JoinRequest, add some optional extra frequencies
1065 void selectChannelPlanDyn();
1066
1067 // setup uplink/downlink channel data rates and frequencies
1068 // for fixed bands, we only allow one sub-band at a time to be selected
1069 void selectChannelPlanFix();
1070
1071 // get the number of available channels,
1072 // along with a 16-bit mask indicating which channels can be used next for uplink/downlink
1073 uint8_t getAvailableChannels(uint16_t* mask);
1074
1075 // (re)set/restore which channels can be used next for uplink/downlink
1076 void setAvailableChannels(uint16_t mask);
1077
1078 // select a set of random TX/RX channels for up- and downlink
1079 int16_t selectChannels();
1080
1081 // apply a 96-bit channel mask
1082 bool applyChannelMask(uint64_t chMaskGrp0123, uint32_t chMaskGrp45);
1083
1084#if RADIOLIB_DEBUG_PROTOCOL
1085 // print the available channels through debug
1086 void printChannels();
1087#endif
1088
1089 // method to generate message integrity code
1090 uint32_t generateMIC(uint8_t* msg, size_t len, uint8_t* key);
1091
1092 // method to verify message integrity code
1093 // it assumes that the MIC is the last 4 bytes of the message
1094 bool verifyMIC(uint8_t* msg, size_t len, uint8_t* key);
1095
1096 // find the first usable data rate for the given band
1097 int16_t findDataRate(uint8_t dr, DataRate_t* dataRate);
1098
1099 // function to encrypt and decrypt payloads (regular uplink/downlink)
1100 void processAES(const uint8_t* in, size_t len, uint8_t* key, uint8_t* out, uint32_t fCnt, uint8_t dir, uint8_t ctrId, bool counter);
1101
1102 // 16-bit checksum method that takes a uint8_t array of even length and calculates the checksum
1103 static uint16_t checkSum16(const uint8_t *key, uint16_t keyLen);
1104
1105 // check the integrity of a buffer using a 16-bit checksum located in the last two bytes of the buffer
1106 static int16_t checkBufferCommon(const uint8_t *buffer, uint16_t size);
1107
1108 // network-to-host conversion method - takes data from network packet and converts it to the host endians
1109 template<typename T>
1110 static T ntoh(const uint8_t* buff, size_t size = 0);
1111
1112 // host-to-network conversion method - takes data from host variable and and converts it to network packet endians
1113 template<typename T>
1114 static void hton(uint8_t* buff, T val, size_t size = 0);
1115};
1116
1117template<typename T>
1118T LoRaWANNode::ntoh(const uint8_t* buff, size_t size) {
1119 const uint8_t* buffPtr = buff;
1120 size_t targetSize = sizeof(T);
1121 if(size != 0) {
1122 targetSize = size;
1123 }
1124 T res = 0;
1125 for(size_t i = 0; i < targetSize; i++) {
1126 res |= (uint32_t)(*(buffPtr++)) << 8*i;
1127 }
1128 return(res);
1129}
1130
1131template<typename T>
1132void LoRaWANNode::hton(uint8_t* buff, T val, size_t size) {
1133 uint8_t* buffPtr = buff;
1134 size_t targetSize = sizeof(T);
1135 if(size != 0) {
1136 targetSize = size;
1137 }
1138 for(size_t i = 0; i < targetSize; i++) {
1139 *(buffPtr++) = val >> 8*i;
1140 }
1141}
1142
1143#endif
LoRaWAN-compatible node (class A device).
Definition LoRaWAN.h:522
void clearSession()
Clear an active session, so that the device will have to rejoin the network.
Definition LoRaWAN.cpp:278
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:43
int16_t setDatarate(uint8_t drUp)
Set uplink datarate. This should not be used when ADR is enabled.
Definition LoRaWAN.cpp:2621
void scheduleTransmission(RadioLibTime_t tUplink)
Set the exact time a transmission should occur. Note: this is the internal clock time....
Definition LoRaWAN.cpp:2754
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:851
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:2736
void setDutyCycle(bool enable=true, RadioLibTime_t msPerHour=0)
Toggle adherence to dutyCycle limits to on or off.
Definition LoRaWAN.cpp:2711
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:613
RadioLibTime_t timeUntilUplink()
Returns time in milliseconds until next uplink is available under dutyCycle limits.
Definition LoRaWAN.cpp:3262
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition LoRaWAN.cpp:2750
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:3252
void setDwellTime(bool enable, RadioLibTime_t msPerUplink=0)
Set or disable uplink dwell time limitation; enabled by default if mandatory.
Definition LoRaWAN.cpp:2723
uint32_t getAFCntDown()
Returns the last application downlink's frame counter; also 0 if no application downlink occured yet.
Definition LoRaWAN.cpp:2770
RadioLibTime_t getLastToA()
Get the Time-on-air of the last uplink message (in milliseconds).
Definition LoRaWAN.cpp:2783
virtual int16_t activateABP(uint8_t initialDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
Join network by restoring ABP session or performing over-the-air activation. In this procedure,...
Definition LoRaWAN.cpp:958
bool TS009
TS009 Protocol Specification Verification switch (allows FPort 224 and cuts off uplink payload instea...
Definition LoRaWAN.h:837
uint8_t * getBufferNonces()
Returns the pointer to the internal buffer that holds the LW base parameters.
Definition LoRaWAN.cpp:227
uint8_t * getBufferSession()
Returns the pointer to the internal buffer that holds the LW session parameters.
Definition LoRaWAN.cpp:428
bool isActivated()
Whether there is an ongoing session active.
Definition LoRaWAN.cpp:1048
int16_t setTxPower(int8_t txPower)
Configure TX power of the radio module.
Definition LoRaWAN.cpp:2654
int16_t getMacDeviceTimeAns(uint32_t *gpsEpoch, uint8_t *fraction, bool returnUnix=true)
Returns the network time after requesting a DeviceTime MAC command. Returns 'true' if a network respo...
Definition LoRaWAN.cpp:2472
uint32_t getNFCntDown()
Returns the last network downlink's frame counter; also 0 if no network downlink occured yet.
Definition LoRaWAN.cpp:2766
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:584
uint8_t getMaxPayloadLen()
Returns the maximum allowed uplink payload size given the current MAC state. Most importantly,...
Definition LoRaWAN.cpp:3271
virtual int16_t activateOTAA(uint8_t initialDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED, LoRaWANJoinEvent_t *joinEvent=NULL)
Join network by restoring OTAA session or performing over-the-air activation. By this procedure,...
Definition LoRaWAN.cpp:868
uint32_t getFCntUp()
Returns the last uplink's frame counter; also 0 if no uplink occured yet.
Definition LoRaWAN.cpp:2759
int16_t setBufferNonces(const uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW base parameters with a supplied buffer.
Definition LoRaWAN.cpp:242
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:2680
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:2437
int16_t getMacLinkCheckAns(uint8_t *margin, uint8_t *gwCnt)
Returns the quality of connectivity after requesting a LinkCheck MAC command. Returns 'true' if a net...
Definition LoRaWAN.cpp:2461
void setADR(bool enable=true)
Toggle ADR to on or off.
Definition LoRaWAN.cpp:2707
int16_t setBufferSession(const uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW session parameters with a supplied buffer.
Definition LoRaWAN.cpp:460
uint32_t getDevAddr()
Returns the DevAddr of the device, regardless of OTAA or ABP mode.
Definition LoRaWAN.cpp:2779
void resetFCntDown()
Reset the downlink frame counters (application and network) This is unsafe and can possibly allow rep...
Definition LoRaWAN.cpp:2774
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition PhysicalLayer.h:151
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition TypeDef.h:642
Structure to save information about LoRaWAN band.
Definition LoRaWAN.h:367
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition LoRaWAN.h:384
bool txParamSupported
Whether this band implements the MAC command TxParamSetupReq.
Definition LoRaWAN.h:399
LoRaWANChannel_t txAck[2]
Relay channels for ACK downlink.
Definition LoRaWAN.h:422
RadioLibTime_t dutyCycle
Number of milliseconds per hour of allowed Time-on-Air.
Definition LoRaWAN.h:390
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
The corresponding datarates, bandwidths and coding rates for DR index.
Definition LoRaWAN.h:425
RadioLibTime_t dwellTimeUp
Maximum dwell time per uplink message in milliseconds.
Definition LoRaWAN.h:393
LoRaWANChannel_t txWoR[2]
Relay channels for WoR uplink.
Definition LoRaWAN.h:419
uint32_t freqMin
Minimum allowed frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:375
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum application payload lengths for each data rate (N-value)
Definition LoRaWAN.h:381
LoRaWANChannel_t txFreqs[3]
A set of default uplink (TX) channels for dynamic bands.
Definition LoRaWAN.h:402
int8_t powerNumSteps
Number of power steps in this band.
Definition LoRaWAN.h:387
LoRaWANChannelSpan_t txSpans[2]
Default uplink (TX) channel spans for fixed bands, including Join-Request parameters.
Definition LoRaWAN.h:408
uint8_t bandNum
Identier for this band.
Definition LoRaWAN.h:369
uint32_t freqMax
Maximum allowed frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:378
uint8_t bandType
Whether the channels are fixed per specification, or dynamically allocated through the network (plus ...
Definition LoRaWAN.h:372
LoRaWANChannel_t rx2
Backup channel for downlink (RX2) window.
Definition LoRaWAN.h:416
uint8_t numTxSpans
The number of TX channel spans for fixed bands.
Definition LoRaWAN.h:405
RadioLibTime_t dwellTimeDn
Maximum dwell time per downlink message in milliseconds.
Definition LoRaWAN.h:396
LoRaWANChannelSpan_t rx1Span
Default downlink (RX1) channel span for fixed bands.
Definition LoRaWAN.h:411
IDs of all currently supported bands.
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition LoRaWAN.h:308
bool available
Whether this channel is available for channel selection.
Definition LoRaWAN.h:328
uint8_t dr
Datarate currently in use on this channel.
Definition LoRaWAN.h:325
uint32_t freq
The channel frequency (coded in 100 Hz steps)
Definition LoRaWAN.h:316
uint8_t idx
The channel number, as specified by defaults or the network.
Definition LoRaWAN.h:313
uint8_t drMin
Minimum allowed datarate for this channel.
Definition LoRaWAN.h:319
bool enabled
Whether this channel is enabled (can be used) or is disabled.
Definition LoRaWAN.h:310
uint8_t drMax
Maximum allowed datarate for this channel (inclusive)
Definition LoRaWAN.h:322
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition LoRaWAN.h:340
uint8_t drJoinRequest
Allowed data rates for a join request message.
Definition LoRaWAN.h:357
uint32_t freqStart
Center frequency of the first channel in span (coded in 100 Hz steps)
Definition LoRaWAN.h:345
uint8_t numChannels
Total number of channels in the span.
Definition LoRaWAN.h:342
uint8_t drMax
Maximum allowed datarate for all channels in this span (inclusive)
Definition LoRaWAN.h:354
uint32_t freqStep
Frequency step between adjacent channels (coded in 100 Hz steps)
Definition LoRaWAN.h:348
uint8_t drMin
Minimum allowed datarate for all channels in this span.
Definition LoRaWAN.h:351
Structure to save extra information about uplink/downlink event.
Definition LoRaWAN.h:485
float freq
Frequency in MHz.
Definition LoRaWAN.h:503
bool confirmed
Whether the event is confirmed or not (e.g., confirmed uplink sent by user application)
Definition LoRaWAN.h:490
uint8_t fPort
Port number.
Definition LoRaWAN.h:512
int16_t power
Transmit power in dBm for uplink, or RSSI for downlink.
Definition LoRaWAN.h:506
bool confirming
Whether the event is confirming a previous request (e.g., server downlink reply to confirmed uplink s...
Definition LoRaWAN.h:494
uint8_t datarate
Datarate.
Definition LoRaWAN.h:500
uint8_t dir
Event direction, one of RADIOLIB_LORAWAN_CHANNEL_DIR_*.
Definition LoRaWAN.h:487
uint8_t nbTrans
Number of times this uplink was transmitted (ADR)
Definition LoRaWAN.h:515
uint32_t fCnt
The appropriate frame counter - for different events, different frame counters will be reported!
Definition LoRaWAN.h:509
bool frmPending
Whether further downlink messages are pending on the server side.
Definition LoRaWAN.h:497
Structure to save extra information about activation event.
Definition LoRaWAN.h:470
bool newSession
Whether a new session was started.
Definition LoRaWAN.h:472
uint32_t joinNonce
The received Join-Request JoinNonce value.
Definition LoRaWAN.h:478
uint16_t devNonce
The transmitted Join-Request DevNonce value.
Definition LoRaWAN.h:475
MAC command specification structure.
Definition LoRaWAN.h:215
const uint8_t lenDn
Uplink message length.
Definition LoRaWAN.h:220
const bool user
Whether this MAC command can be issued by the user or not.
Definition LoRaWAN.h:229
const uint8_t cid
Command ID.
Definition LoRaWAN.h:217
const uint8_t lenUp
Downlink message length.
Definition LoRaWAN.h:223
const bool persist
Some commands must be resent until Class A downlink received.
Definition LoRaWAN.h:226
Common data rate structure.
Definition PhysicalLayer.h:74