RadioLib
Universal wireless communication library for Arduino
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 // modulation type
19 #define RADIOLIB_LORAWAN_MODULATION_LORA (0)
20 #define RADIOLIB_LORAWAN_MODULATION_GFSK (1)
21 #define RADIOLIB_LORAWAN_MODULATION_LR_FHSS (2)
22 
23 // preamble format
24 #define RADIOLIB_LORAWAN_LORA_SYNC_WORD (0x34)
25 #define RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN (8)
26 #define RADIOLIB_LORAWAN_GFSK_SYNC_WORD (0xC194C1)
27 #define RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN (5)
28 #define RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD (0x2C0F7995)
29 
30 // MAC header field encoding MSB LSB DESCRIPTION
31 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5) // 7 5 message type: join request
32 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_ACCEPT (0x01 << 5) // 7 5 join accept
33 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_UP (0x02 << 5) // 7 5 unconfirmed data up
34 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_DOWN (0x03 << 5) // 7 5 unconfirmed data down
35 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP (0x04 << 5) // 7 5 confirmed data up
36 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_DOWN (0x05 << 5) // 7 5 confirmed data down
37 #define RADIOLIB_LORAWAN_MHDR_MTYPE_PROPRIETARY (0x07 << 5) // 7 5 proprietary
38 #define RADIOLIB_LORAWAN_MHDR_MTYPE_MASK (0x07 << 5) // 7 5 bitmask of all possible options
39 #define RADIOLIB_LORAWAN_MHDR_MAJOR_R1 (0x00 << 0) // 1 0 major version: LoRaWAN R1
40 
41 // frame control field encoding
42 #define RADIOLIB_LORAWAN_FCTRL_ADR_ENABLED (0x01 << 7) // 7 7 adaptive data rate: enabled
43 #define RADIOLIB_LORAWAN_FCTRL_ADR_DISABLED (0x00 << 7) // 7 7 disabled
44 #define RADIOLIB_LORAWAN_FCTRL_ADR_ACK_REQ (0x01 << 6) // 6 6 adaptive data rate ACK request
45 #define RADIOLIB_LORAWAN_FCTRL_ACK (0x01 << 5) // 5 5 confirmed message acknowledge
46 #define RADIOLIB_LORAWAN_FCTRL_FRAME_PENDING (0x01 << 4) // 4 4 downlink frame is pending
47 
48 // fPort field
49 #define RADIOLIB_LORAWAN_FPORT_MAC_COMMAND (0x00 << 0) // 7 0 payload contains MAC commands only
50 #define RADIOLIB_LORAWAN_FPORT_TS009 (0xE0 << 0) // 7 0 fPort used for TS009 testing
51 #define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0) // 7 0 fPort values equal to and larger than this are reserved
52 
53 // MAC commands - only those sent from end-device to gateway
54 #define RADIOLIB_LORAWAN_LINK_CHECK_REQ (0x02 << 0) // 7 0 MAC command: request to check connectivity to network
55 #define RADIOLIB_LORAWAN_LINK_ADR_ANS (0x03 << 0) // 7 0 answer to ADR change
56 #define RADIOLIB_LORAWAN_DUTY_CYCLE_ANS (0x04 << 0) // 7 0 answer to duty cycle change
57 #define RADIOLIB_LORAWAN_RX_PARAM_SETUP_ANS (0x05 << 0) // 7 0 answer to reception slot setup request
58 #define RADIOLIB_LORAWAN_DEV_STATUS_ANS (0x06 << 0) // 7 0 device status information
59 #define RADIOLIB_LORAWAN_NEW_CHANNEL_ANS (0x07 << 0) // 7 0 acknowledges change of a radio channel
60 #define RADIOLIB_LORAWAN_RX_TIMING_SETUP_ANS (0x08 << 0) // 7 0 acknowledges change of a reception slots timing
61 
62 #define RADIOLIB_LORAWAN_NOPTS_LEN (8)
63 
64 // data rate encoding
65 #define RADIOLIB_LORAWAN_DATA_RATE_FSK_50_K (0x01 << 7) // 7 7 FSK @ 50 kbps
66 #define RADIOLIB_LORAWAN_DATA_RATE_SF_12 (0x06 << 4) // 6 4 LoRa spreading factor: SF12
67 #define RADIOLIB_LORAWAN_DATA_RATE_SF_11 (0x05 << 4) // 6 4 SF11
68 #define RADIOLIB_LORAWAN_DATA_RATE_SF_10 (0x04 << 4) // 6 4 SF10
69 #define RADIOLIB_LORAWAN_DATA_RATE_SF_9 (0x03 << 4) // 6 4 SF9
70 #define RADIOLIB_LORAWAN_DATA_RATE_SF_8 (0x02 << 4) // 6 4 SF8
71 #define RADIOLIB_LORAWAN_DATA_RATE_SF_7 (0x01 << 4) // 6 4 SF7
72 #define RADIOLIB_LORAWAN_DATA_RATE_BW_500_KHZ (0x00 << 2) // 3 2 LoRa bandwidth: 500 kHz
73 #define RADIOLIB_LORAWAN_DATA_RATE_BW_250_KHZ (0x01 << 2) // 3 2 250 kHz
74 #define RADIOLIB_LORAWAN_DATA_RATE_BW_125_KHZ (0x02 << 2) // 3 2 125 kHz
75 #define RADIOLIB_LORAWAN_DATA_RATE_BW_RESERVED (0x03 << 2) // 3 2 reserved value
76 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_5 (0x00 << 0) // 1 0 LoRa coding rate: 4/5
77 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_6 (0x01 << 0) // 1 0 4/6
78 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_7 (0x02 << 0) // 1 0 4/7
79 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_8 (0x03 << 0) // 1 0 4/8
80 #define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0xFF << 0) // 7 0 unused data rate
81 
82 #define RADIOLIB_LORAWAN_CHANNEL_DIR_UPLINK (0x00 << 0)
83 #define RADIOLIB_LORAWAN_CHANNEL_DIR_DOWNLINK (0x01 << 0)
84 #define RADIOLIB_LORAWAN_CHANNEL_DIR_BOTH (0x02 << 0)
85 #define RADIOLIB_LORAWAN_CHANNEL_DIR_NONE (0x03 << 0)
86 #define RADIOLIB_LORAWAN_BAND_DYNAMIC (0)
87 #define RADIOLIB_LORAWAN_BAND_FIXED (1)
88 #define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (15)
89 #define RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE (0xFF >> 0)
90 
91 // recommended default settings
92 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
93 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
94 #define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
95 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
96 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
97 #define RADIOLIB_LORAWAN_MAX_FCNT_GAP (16384)
98 #define RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP (0x06)
99 #define RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP (0x05)
100 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
101 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
102 #define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
103 #define RADIOLIB_LORAWAN_REJOIN_MAX_COUNT_N (10) // send rejoin request 16384 uplinks
104 #define RADIOLIB_LORAWAN_REJOIN_MAX_TIME_N (15) // once every year, not actually implemented
105 
106 // join request message layout
107 #define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
108 #define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
109 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
110 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
111 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
112 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
113 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
114 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
115 
116 // join accept message layout
117 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
118 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
119 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
120 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
121 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_EUI_POS (4)
122 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
123 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
124 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_NONCE_POS (12)
125 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
126 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
127 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
128 
129 // join accept message variables
130 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7) // 7 7 LoRaWAN revision: 1.0
131 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7) // 7 7 1.1
132 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
133 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
134 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
135 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
136 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
137 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
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 // MAC commands
176 #define RADIOLIB_LORAWAN_NUM_MAC_COMMANDS (16)
177 
178 #define RADIOLIB_LORAWAN_MAC_RESET (0x01)
179 #define RADIOLIB_LORAWAN_MAC_LINK_CHECK (0x02)
180 #define RADIOLIB_LORAWAN_MAC_LINK_ADR (0x03)
181 #define RADIOLIB_LORAWAN_MAC_DUTY_CYCLE (0x04)
182 #define RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP (0x05)
183 #define RADIOLIB_LORAWAN_MAC_DEV_STATUS (0x06)
184 #define RADIOLIB_LORAWAN_MAC_NEW_CHANNEL (0x07)
185 #define RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP (0x08)
186 #define RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP (0x09)
187 #define RADIOLIB_LORAWAN_MAC_DL_CHANNEL (0x0A)
188 #define RADIOLIB_LORAWAN_MAC_REKEY (0x0B)
189 #define RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP (0x0C)
190 #define RADIOLIB_LORAWAN_MAC_DEVICE_TIME (0x0D)
191 #define RADIOLIB_LORAWAN_MAC_FORCE_REJOIN (0x0E)
192 #define RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP (0x0F)
193 #define RADIOLIB_LORAWAN_MAC_PROPRIETARY (0x80)
194 
195 // the length of internal MAC command queue - hopefully this is enough for most use cases
196 #define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (9)
197 
198 // the maximum number of simultaneously available channels
199 #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (16)
200 
201 // maximum MAC command sizes
202 #define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN (5)
203 #define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_UP (2)
204 #define RADIOLIB_LORAWAN_MAX_NUM_ADR_COMMANDS (8)
205 
212  const uint8_t cid;
213 
215  const uint8_t lenDn;
216 
218  const uint8_t lenUp;
219 
221  const bool user;
222 };
223 
224 constexpr LoRaWANMacSpec_t MacTable[RADIOLIB_LORAWAN_NUM_MAC_COMMANDS + 1] = {
225  { 0x00, 0, 0, false }, // not an actual MAC command, exists for index offsetting
226  { RADIOLIB_LORAWAN_MAC_RESET, 1, 1, false },
227  { RADIOLIB_LORAWAN_MAC_LINK_CHECK, 2, 0, true },
228  { RADIOLIB_LORAWAN_MAC_LINK_ADR, 4, 1, false },
229  { RADIOLIB_LORAWAN_MAC_DUTY_CYCLE, 1, 0, false },
230  { RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP, 4, 1, false },
231  { RADIOLIB_LORAWAN_MAC_DEV_STATUS, 0, 2, false },
232  { RADIOLIB_LORAWAN_MAC_NEW_CHANNEL, 5, 1, false },
233  { RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP, 1, 0, false },
234  { RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP, 1, 0, false },
235  { RADIOLIB_LORAWAN_MAC_DL_CHANNEL, 4, 1, false },
236  { RADIOLIB_LORAWAN_MAC_REKEY, 1, 1, false },
237  { RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP, 1, 0, false },
238  { RADIOLIB_LORAWAN_MAC_DEVICE_TIME, 5, 0, true },
239  { RADIOLIB_LORAWAN_MAC_FORCE_REJOIN, 2, 0, false },
240  { RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP, 1, 1, false },
241  { RADIOLIB_LORAWAN_MAC_PROPRIETARY, 5, 0, true }
242 };
243 
250  uint8_t cid;
251 
253  uint8_t payload[RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN];
254 
256  uint8_t len;
257 
259  uint8_t repeat;
260 };
261 
268  uint8_t numCommands;
269 
271  uint8_t len;
272 
274  LoRaWANMacCommand_t commands[RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE];
275 };
276 
277 #define RADIOLIB_LORAWAN_NONCES_VERSION_VAL (0x0001)
278 
279 enum LoRaWANSchemeBase_t {
280  RADIOLIB_LORAWAN_NONCES_START = 0x00,
281  RADIOLIB_LORAWAN_NONCES_VERSION = RADIOLIB_LORAWAN_NONCES_START, // 2 bytes
282  RADIOLIB_LORAWAN_NONCES_MODE = RADIOLIB_LORAWAN_NONCES_VERSION + sizeof(uint16_t), // 2 bytes
283  RADIOLIB_LORAWAN_NONCES_CLASS = RADIOLIB_LORAWAN_NONCES_MODE + sizeof(uint16_t), // 1 byte
284  RADIOLIB_LORAWAN_NONCES_PLAN = RADIOLIB_LORAWAN_NONCES_CLASS + sizeof(uint8_t), // 1 byte
285  RADIOLIB_LORAWAN_NONCES_CHECKSUM = RADIOLIB_LORAWAN_NONCES_PLAN + sizeof(uint8_t), // 2 bytes
286  RADIOLIB_LORAWAN_NONCES_DEV_NONCE = RADIOLIB_LORAWAN_NONCES_CHECKSUM + sizeof(uint16_t), // 2 bytes
287  RADIOLIB_LORAWAN_NONCES_JOIN_NONCE = RADIOLIB_LORAWAN_NONCES_DEV_NONCE + sizeof(uint16_t), // 3 bytes
288  RADIOLIB_LORAWAN_NONCES_ACTIVE = RADIOLIB_LORAWAN_NONCES_JOIN_NONCE + 3, // 1 byte
289  RADIOLIB_LORAWAN_NONCES_SIGNATURE = RADIOLIB_LORAWAN_NONCES_ACTIVE + sizeof(uint8_t), // 2 bytes
290  RADIOLIB_LORAWAN_NONCES_BUF_SIZE = RADIOLIB_LORAWAN_NONCES_SIGNATURE + sizeof(uint16_t) // Nonces buffer size
291 };
292 
293 enum LoRaWANSchemeSession_t {
294  RADIOLIB_LORAWAN_SESSION_START = 0x00,
295  RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY = RADIOLIB_LORAWAN_SESSION_START, // 16 bytes
296  RADIOLIB_LORAWAN_SESSION_APP_SKEY = RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY + RADIOLIB_AES128_BLOCK_SIZE, // 16 bytes
297  RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_APP_SKEY + RADIOLIB_AES128_BLOCK_SIZE, // 16 bytes
298  RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY = RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY + RADIOLIB_AES128_BLOCK_SIZE, // 16 bytes
299  RADIOLIB_LORAWAN_SESSION_DEV_ADDR = RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY + RADIOLIB_AES128_BLOCK_SIZE, // 4 bytes
300  RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE = RADIOLIB_LORAWAN_SESSION_DEV_ADDR + sizeof(uint32_t), // 2 bytes
301  RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE + sizeof(uint16_t), // 4 bytes
302  RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP = RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
303  RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP + sizeof(uint32_t), // 4 bytes
304  RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 = RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN + sizeof(uint32_t), // 2 bytes
305  RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 = RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 + sizeof(uint16_t), // 2 bytes
306  RADIOLIB_LORAWAN_SESSION_HOMENET_ID = RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 + sizeof(uint16_t), // 4 bytes
307  RADIOLIB_LORAWAN_SESSION_VERSION = RADIOLIB_LORAWAN_SESSION_HOMENET_ID + sizeof(uint32_t), // 1 byte
308  RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE = RADIOLIB_LORAWAN_SESSION_VERSION + sizeof(uint8_t), // 1 byte
309  RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE + MacTable[RADIOLIB_LORAWAN_MAC_DUTY_CYCLE].lenDn, // 4 bytes
310  RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP = RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP + MacTable[RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP].lenDn, // 1 byte
311  RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP + MacTable[RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP].lenDn, // 1 byte
312  RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP + MacTable[RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP].lenDn, // 1 byte
313  RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP = RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP + MacTable[RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP].lenDn, // 1 byte
314  RADIOLIB_LORAWAN_SESSION_BEACON_FREQ = RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP + MacTable[RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP].lenDn, // 3 bytes
315  RADIOLIB_LORAWAN_SESSION_PING_SLOT_CHANNEL = RADIOLIB_LORAWAN_SESSION_BEACON_FREQ + 3, // 4 bytes
316  RADIOLIB_LORAWAN_SESSION_PERIODICITY = RADIOLIB_LORAWAN_SESSION_PING_SLOT_CHANNEL + 4, // 1 byte
317  RADIOLIB_LORAWAN_SESSION_LAST_TIME = RADIOLIB_LORAWAN_SESSION_PERIODICITY + 1, // 4 bytes
318  RADIOLIB_LORAWAN_SESSION_UL_CHANNELS = RADIOLIB_LORAWAN_SESSION_LAST_TIME + 4, // 16*5 bytes
319  RADIOLIB_LORAWAN_SESSION_DL_CHANNELS = RADIOLIB_LORAWAN_SESSION_UL_CHANNELS + 16*MacTable[RADIOLIB_LORAWAN_MAC_NEW_CHANNEL].lenDn, // 16*4 bytes
320  RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_UL = RADIOLIB_LORAWAN_SESSION_DL_CHANNELS + 16*MacTable[RADIOLIB_LORAWAN_MAC_DL_CHANNEL].lenDn, // 9*8+2 bytes
321  RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN = RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_UL + sizeof(LoRaWANMacCommandQueue_t), // 4 bytes
322  RADIOLIB_LORAWAN_SESSION_ADR_FCNT = RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN + sizeof(uint32_t), // 4 bytes
323  RADIOLIB_LORAWAN_SESSION_LINK_ADR = RADIOLIB_LORAWAN_SESSION_ADR_FCNT + sizeof(uint32_t), // 4 bytes
324  RADIOLIB_LORAWAN_SESSION_FCNT_UP = RADIOLIB_LORAWAN_SESSION_LINK_ADR + MacTable[RADIOLIB_LORAWAN_MAC_LINK_ADR].lenDn, // 4 bytes
325  RADIOLIB_LORAWAN_SESSION_SIGNATURE = RADIOLIB_LORAWAN_SESSION_FCNT_UP + sizeof(uint32_t), // 2 bytes
326  RADIOLIB_LORAWAN_SESSION_BUF_SIZE = RADIOLIB_LORAWAN_SESSION_SIGNATURE + sizeof(uint16_t) // Session buffer size
327 };
328 
336  bool enabled;
337 
339  uint8_t idx;
340 
342  float freq;
343 
345  uint8_t drMin;
346 
348  uint8_t drMax;
349 };
350 
351 // alias for unused channel
352 #define RADIOLIB_LORAWAN_CHANNEL_NONE { .enabled = false, .idx = RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE, .freq = 0, .drMin = 0, .drMax = 0 }
353 
361  uint8_t numChannels;
362 
364  float freqStart;
365 
367  float freqStep;
368 
370  uint8_t drMin;
371 
373  uint8_t drMax;
374 
377 };
378 
379 // alias for unused channel span
380 #define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .numChannels = 0, .freqStart = 0, .freqStep = 0, .drMin = 0, .drMax = 0, .joinRequestDataRate = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
381 
388  uint8_t bandNum;
389 
391  uint8_t bandType;
392 
394  uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
395 
397  int8_t powerMax;
398 
401 
404 
407 
410 
413 
416 
418  uint8_t numTxSpans;
419 
422 
425 
428 
431 
433  uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
434 };
435 
436 // supported bands
437 extern const LoRaWANBand_t EU868;
438 extern const LoRaWANBand_t US915;
439 extern const LoRaWANBand_t EU433;
440 extern const LoRaWANBand_t AU915;
441 extern const LoRaWANBand_t CN500;
442 extern const LoRaWANBand_t AS923;
443 extern const LoRaWANBand_t AS923_2;
444 extern const LoRaWANBand_t AS923_3;
445 extern const LoRaWANBand_t AS923_4;
446 extern const LoRaWANBand_t KR920;
447 extern const LoRaWANBand_t IN865;
448 
453 enum LoRaWANBandNum_t {
454  BandEU868,
455  BandUS915,
456  BandEU433,
457  BandAU915,
458  BandCN500,
459  BandAS923,
460  BandAS923_2,
461  BandAS923_3,
462  BandAS923_4,
463  BandKR920,
464  BandIN865,
465  BandLast
466 };
467 
468 // provide easy access to the number of currently supported bands
469 #define RADIOLIB_LORAWAN_NUM_SUPPORTED_BANDS (BandLast - BandEU868)
470 
471 // array of currently supported bands
472 extern const LoRaWANBand_t* LoRaWANBands[];
473 
480  bool newSession = false;
481 
483  uint16_t devNonce = 0;
484 
486  uint32_t joinNonce = 0;
487 };
488 
495  uint8_t dir;
496 
498  bool confirmed;
499 
503 
505  uint8_t datarate;
506 
508  float freq;
509 
511  int16_t power;
512 
514  uint32_t fCnt;
515 
517  uint8_t fPort;
518 };
519 
524 class LoRaWANNode {
525  public:
526 
533  LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band, uint8_t subBand = 0);
534 
538  void clearSession();
539 
544  uint8_t* getBufferNonces();
545 
551  int16_t setBufferNonces(uint8_t* persistentBuffer);
552 
557  uint8_t* getBufferSession();
558 
564  int16_t setBufferSession(uint8_t* persistentBuffer);
565 
573  void beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey);
574 
581  int16_t activateOTAA(uint8_t initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, LoRaWANJoinEvent_t *joinEvent = NULL);
582 
592  void beginABP(uint32_t addr, uint8_t* fNwkSIntKey, uint8_t* sNwkSIntKey, uint8_t* nwkSEncKey, uint8_t* appSKey);
593 
600  int16_t activateABP(uint8_t initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED);
601 
603  bool isActivated();
604 
611  int16_t setRx2Dr(uint8_t dr);
612 
620  int16_t sendMacCommandReq(uint8_t cid);
621 
622  #if defined(RADIOLIB_BUILD_ARDUINO)
632  int16_t uplink(String& str, uint8_t fPort, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
633  #endif
634 
644  int16_t uplink(const char* str, uint8_t fPort, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
645 
656  int16_t uplink(uint8_t* data, size_t len, uint8_t fPort, bool isConfirmed = false, LoRaWANEvent_t* event = NULL);
657 
658  #if defined(RADIOLIB_BUILD_ARDUINO)
666  int16_t downlink(String& str, LoRaWANEvent_t* event = NULL);
667  #endif
668 
677  int16_t downlink(uint8_t* data, size_t* len, LoRaWANEvent_t* event = NULL);
678 
685  int16_t downlink(LoRaWANEvent_t* event = NULL);
686 
687  #if defined(RADIOLIB_BUILD_ARDUINO)
700  int16_t sendReceive(String& strUp, uint8_t fPort, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
701  #endif
702 
716  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);
717 
732  int16_t sendReceive(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);
733 
746  int16_t sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort = 1, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
747 
753  void setDeviceStatus(uint8_t battLevel);
754 
759  uint32_t getFCntUp();
760 
765  uint32_t getNFCntDown();
766 
771  uint32_t getAFCntDown();
772 
778  void resetFCntDown();
779 
785  int16_t setDatarate(uint8_t drUp);
786 
791  void setADR(bool enable = true);
792 
799  void setDutyCycle(bool enable = true, RadioLibTime_t msPerHour = 0);
800 
809 
812 
819  void setDwellTime(bool enable, RadioLibTime_t msPerUplink = 0);
820 
826  uint8_t maxPayloadDwellTime();
827 
833  int16_t setTxPower(int8_t txPower);
834 
843  int16_t getMacLinkCheckAns(uint8_t* margin, uint8_t* gwCnt);
844 
854  int16_t getMacDeviceTimeAns(uint32_t* gpsEpoch, uint8_t* fraction, bool returnUnix = true);
855 
860  uint64_t getDevAddr();
861 
867 
872  bool TS009 = false;
873 
874 #if !RADIOLIB_GODMODE
875  private:
876 #endif
877  PhysicalLayer* phyLayer = NULL;
878  const LoRaWANBand_t* band = NULL;
879 
880  static int16_t checkBufferCommon(uint8_t *buffer, uint16_t size);
881 
882  void activateCommon(uint8_t initialDr);
883 
884  // a buffer that holds all LW base parameters that should persist at all times!
885  uint8_t bufferNonces[RADIOLIB_LORAWAN_NONCES_BUF_SIZE] = { 0 };
886 
887  // a buffer that holds all LW session parameters that preferably persist, but can be afforded to get lost
888  uint8_t bufferSession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE] = { 0 };
889 
890  LoRaWANMacCommandQueue_t commandsUp = {
891  .numCommands = 0,
892  .len = 0,
893  .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
894  };
895  LoRaWANMacCommandQueue_t commandsDown = {
896  .numCommands = 0,
897  .len = 0,
898  .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
899  };
900 
901  uint16_t lwMode = RADIOLIB_LORAWAN_MODE_NONE;
902  uint8_t lwClass = RADIOLIB_LORAWAN_CLASS_A;
903  bool isActive = false;
904 
905  uint64_t joinEUI = 0;
906  uint64_t devEUI = 0;
907  uint8_t nwkKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
908  uint8_t appKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
909 
910  // the following is either provided by the network server (OTAA)
911  // or directly entered by the user (ABP)
912  uint32_t devAddr = 0;
913  uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
914  uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
915  uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
916  uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
917  uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
918 
919  uint16_t keyCheckSum = 0;
920 
921  // device-specific parameters, persistent through sessions
922  uint16_t devNonce = 0;
923  uint32_t joinNonce = 0;
924 
925  // session-specific parameters
926  uint32_t homeNetId = 0;
927  uint8_t adrLimitExp = RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP;
928  uint8_t adrDelayExp = RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP;
929  uint8_t nbTrans = 1; // Number of allowed frame retransmissions
930  uint8_t txPowerSteps = 0;
931  uint8_t txPowerMax = 0;
932  uint32_t fCntUp = 0;
933  uint32_t aFCntDown = 0;
934  uint32_t nFCntDown = 0;
935  uint32_t confFCntUp = RADIOLIB_LORAWAN_FCNT_NONE;
936  uint32_t confFCntDown = RADIOLIB_LORAWAN_FCNT_NONE;
937  uint32_t adrFCnt = 0;
938 
939  // modulation of the currently configured channel
940  uint8_t modulation = RADIOLIB_LORAWAN_MODULATION_LORA;
941 
942  // ADR is enabled by default
943  bool adrEnabled = true;
944 
945  // duty cycle is set upon initialization and activated in regions that impose this
946  bool dutyCycleEnabled = false;
947  uint32_t dutyCycle = 0;
948 
949  // dwell time is set upon initialization and activated in regions that impose this
950  bool dwellTimeEnabledUp = false;
951  uint16_t dwellTimeUp = 0;
952  bool dwellTimeEnabledDn = false;
953  uint16_t dwellTimeDn = 0;
954 
955  // enable/disable CSMA for LoRaWAN
956  bool enableCSMA;
957 
958  // number of backoff slots to be decremented after DIFS phase. 0 to disable BO.
959  // A random BO avoids collisions in the case where two or more nodes start the CSMA
960  // process at the same time.
961  uint8_t backoffMax;
962 
963  // number of CADs to estimate a clear CH
964  uint8_t difsSlots;
965 
966  // available channel frequencies from list passed during OTA activation
967  LoRaWANChannel_t availableChannels[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS];
968 
969  // currently configured channels for TX and RX1
970  LoRaWANChannel_t currentChannels[2] = { RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE };
971 
972  // currently configured datarates for TX and RX1
973  uint8_t dataRates[2] = { RADIOLIB_LORAWAN_DATA_RATE_UNUSED, RADIOLIB_LORAWAN_DATA_RATE_UNUSED };
974 
975  // Rx2 channel properties - may be changed by MAC command
976  LoRaWANChannel_t rx2 = RADIOLIB_LORAWAN_CHANNEL_NONE;
977 
978  // offset between TX and RX1 (such that RX1 has equal or lower DR)
979  uint8_t rx1DrOffset = 0;
980 
981  // LoRaWAN revision (1.0 vs 1.1)
982  uint8_t rev = 0;
983 
984  // Time on Air of last uplink
985  RadioLibTime_t lastToA = 0;
986 
987  // timestamp to measure the RX1/2 delay (from uplink end)
988  RadioLibTime_t rxDelayStart = 0;
989 
990  // timestamp when the Rx1/2 windows were closed (timeout or uplink received)
991  RadioLibTime_t rxDelayEnd = 0;
992 
993  // delays between the uplink and RX1/2 windows
994  RadioLibTime_t rxDelays[2] = { RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
995 
996  // device status - battery level
997  uint8_t battLevel = 0xFF;
998 
999  // indicates whether an uplink has MAC commands as payload
1000  bool isMACPayload = false;
1001 
1002  // save the selected sub-band in case this must be restored in ADR control
1003  uint8_t subBand = 0;
1004 
1005  // this will reset the device credentials, so the device starts completely new
1006  void clearNonces();
1007 
1008  // wait for, open and listen during Rx1 and Rx2 windows; only performs listening
1009  int16_t downlinkCommon();
1010 
1011  // method to generate message integrity code
1012  uint32_t generateMIC(uint8_t* msg, size_t len, uint8_t* key);
1013 
1014  // method to verify message integrity code
1015  // it assumes that the MIC is the last 4 bytes of the message
1016  bool verifyMIC(uint8_t* msg, size_t len, uint8_t* key);
1017 
1018  // configure the common physical layer properties (preamble, sync word etc.)
1019  // channels must be configured separately by setupChannelsDyn()!
1020  int16_t setPhyProperties(uint8_t dir);
1021 
1022  // setup uplink/downlink channel data rates and frequencies
1023  // for dynamic channels, there is a small set of predefined channels
1024  // in case of JoinRequest, add some optional extra frequencies
1025  int16_t setupChannelsDyn(bool joinRequest = false);
1026 
1027  // setup uplink/downlink channel data rates and frequencies
1028  // for fixed bands, we only allow one sub-band at a time to be selected
1029  int16_t setupChannelsFix(uint8_t subBand);
1030 
1031  // a join-accept can piggy-back a set of channels or channel masks
1032  int16_t processCFList(uint8_t* cfList);
1033 
1034  // select a set of random TX/RX channels for up- and downlink
1035  int16_t selectChannels();
1036 
1037  // find the first usable data rate for the given band
1038  int16_t findDataRate(uint8_t dr, DataRate_t* dataRate);
1039 
1040  // restore all available channels from persistent storage
1041  int16_t restoreChannels();
1042 
1043  // push MAC command to queue, done by copy
1044  int16_t pushMacCommand(LoRaWANMacCommand_t* cmd, LoRaWANMacCommandQueue_t* queue);
1045 
1046  // delete a specific MAC command from queue, indicated by the command ID
1047  // if a payload pointer is supplied, this returns the payload of the MAC command
1048  int16_t deleteMacCommand(uint8_t cid, LoRaWANMacCommandQueue_t* queue, uint8_t* payload = NULL);
1049 
1050  // execute mac command, return the number of processed bytes for sequential processing
1051  bool execMacCommand(LoRaWANMacCommand_t* cmd);
1052 
1053  // apply a channel mask to a set of readily defined channels (dynamic bands only)
1054  bool applyChannelMaskDyn(uint8_t chMaskCntl, uint16_t chMask);
1055 
1056  // define or delete channels from a fixed set of channels (fixed bands only)
1057  bool applyChannelMaskFix(uint8_t chMaskCntl, uint16_t chMask);
1058 
1059  // get the payload length for a specific MAC command
1060  uint8_t getMacPayloadLength(uint8_t cid);
1061 
1068  void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA = false);
1069 
1070  // Performs CSMA as per LoRa Alliance Technical Recommendation 13 (TR-013).
1071  void performCSMA();
1072 
1073  // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise.
1074  bool performCAD();
1075 
1076  // function to encrypt and decrypt payloads
1077  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);
1078 
1079  // 16-bit checksum method that takes a uint8_t array of even length and calculates the checksum
1080  static uint16_t checkSum16(uint8_t *key, uint16_t keyLen);
1081 
1082  // network-to-host conversion method - takes data from network packet and converts it to the host endians
1083  template<typename T>
1084  static T ntoh(uint8_t* buff, size_t size = 0);
1085 
1086  // host-to-network conversion method - takes data from host variable and and converts it to network packet endians
1087  template<typename T>
1088  static void hton(uint8_t* buff, T val, size_t size = 0);
1089 };
1090 
1091 #endif
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:524
void clearSession()
Clear an active session, so that the device will have to rejoin the network.
Definition: LoRaWAN.cpp:59
int16_t setBufferNonces(uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW base parameters with a supplied buffer.
Definition: LoRaWAN.cpp:75
int16_t setDatarate(uint8_t drUp)
Set uplink datarate. This should not be used when ADR is enabled.
Definition: LoRaWAN.cpp:2000
int16_t uplink(const char *str, uint8_t fPort, bool isConfirmed=false, LoRaWANEvent_t *event=NULL)
Send a message to the server.
Definition: LoRaWAN.cpp:868
void beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey)
Set the device credentials and activation configuration.
Definition: LoRaWAN.cpp:447
uint8_t maxPayloadDwellTime()
Returns the maximum payload given the currently present dwell time limits. WARNING: the addition of M...
Definition: LoRaWAN.cpp:2083
void setDutyCycle(bool enable=true, RadioLibTime_t msPerHour=0)
Toggle adherence to dutyCycle limits to on or off.
Definition: LoRaWAN.cpp:2041
RadioLibTime_t timeUntilUplink()
Returns time in milliseconds until next uplink is available under dutyCycle limits.
Definition: LoRaWAN.cpp:2065
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition: LoRaWAN.cpp:1681
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:2055
void setDwellTime(bool enable, RadioLibTime_t msPerUplink=0)
Toggle adherence to dwellTime limits to on or off.
Definition: LoRaWAN.cpp:2074
uint32_t getAFCntDown()
Returns the last application downlink's frame counter; also 0 if no application downlink occured yet.
Definition: LoRaWAN.cpp:1697
RadioLibTime_t getLastToA()
Get the Time-on-air of the last uplink message.
Definition: LoRaWAN.cpp:2919
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:792
bool TS009
TS009 Protocol Specification Verification switch (allows FPort 224 and cuts off uplink payload instea...
Definition: LoRaWAN.h:872
uint8_t * getBufferNonces()
Returns the pointer to the internal buffer that holds the LW base parameters.
Definition: LoRaWAN.cpp:67
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)
Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.
Definition: LoRaWAN.cpp:1661
uint8_t * getBufferSession()
Returns the pointer to the internal buffer that holds the LW session parameters.
Definition: LoRaWAN.cpp:111
void beginABP(uint32_t addr, uint8_t *fNwkSIntKey, uint8_t *sNwkSIntKey, uint8_t *nwkSEncKey, uint8_t *appSKey)
Set the device credentials and activation configuration.
Definition: LoRaWAN.cpp:765
bool isActivated()
Whether there is an ongoing session active.
Definition: LoRaWAN.cpp:858
int16_t setTxPower(int8_t txPower)
Configure TX power of the radio module.
Definition: LoRaWAN.cpp:2104
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:2898
uint32_t getNFCntDown()
Returns the last network downlink's frame counter; also 0 if no network downlink occured yet.
Definition: LoRaWAN.cpp:1693
uint64_t getDevAddr()
Returns the DevAddr of the device, regardless of OTAA or ABP mode.
Definition: LoRaWAN.cpp:2915
int16_t downlink(uint8_t *data, size_t *len, LoRaWANEvent_t *event=NULL)
Wait for downlink from the server in either RX1 or RX2 window.
Definition: LoRaWAN.cpp:1312
int16_t setBufferSession(uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW session parameters with a supplied buffer.
Definition: LoRaWAN.cpp:130
LoRaWANNode(PhysicalLayer *phy, const LoRaWANBand_t *band, uint8_t subBand=0)
Default constructor.
Definition: LoRaWAN.cpp:30
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:464
uint32_t getFCntUp()
Returns the last uplink's frame counter; also 0 if no uplink occured yet.
Definition: LoRaWAN.cpp:1686
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:2168
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:2195
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:2887
void setADR(bool enable=true)
Toggle ADR to on or off.
Definition: LoRaWAN.cpp:2037
void resetFCntDown()
Reset the downlink frame counters (application and network) This is unsafe and can possibly allow rep...
Definition: LoRaWAN.cpp:1701
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:54
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition: TypeDef.h:616
Structure to save information about LoRaWAN band.
Definition: LoRaWAN.h:386
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition: LoRaWAN.h:397
RadioLibTime_t dutyCycle
Number of milliseconds per hour of allowed Time-on-Air.
Definition: LoRaWAN.h:403
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
The corresponding datarates, bandwidths and coding rates for DR index.
Definition: LoRaWAN.h:433
RadioLibTime_t dwellTimeUp
Maximum dwell time per uplink message in milliseconds.
Definition: LoRaWAN.h:406
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum payload lengths for each data rate.
Definition: LoRaWAN.h:394
LoRaWANChannel_t txFreqs[3]
A set of default uplink (TX) channels for frequency-type bands.
Definition: LoRaWAN.h:412
LoRaWANChannel_t txJoinReq[3]
A set of possible extra channels for the Join-Request message for frequency-type bands.
Definition: LoRaWAN.h:415
int8_t powerNumSteps
Number of power steps in this band.
Definition: LoRaWAN.h:400
LoRaWANChannelSpan_t txSpans[2]
Default uplink (TX) channel spans for mask-type bands, including Join-Request parameters.
Definition: LoRaWAN.h:421
uint8_t rx1DataRateBase
The base downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:427
uint8_t bandNum
Identier for this band.
Definition: LoRaWAN.h:388
uint8_t bandType
Whether the channels are fixed per specification, or dynamically allocated through the network (plus ...
Definition: LoRaWAN.h:391
LoRaWANChannel_t rx2
Backup channel for downlink (RX2) window.
Definition: LoRaWAN.h:430
uint8_t numTxSpans
The number of TX channel spans for mask-type bands.
Definition: LoRaWAN.h:418
RadioLibTime_t dwellTimeDn
Maximum dwell time per downlink message in milliseconds.
Definition: LoRaWAN.h:409
LoRaWANChannelSpan_t rx1Span
Default downlink (RX1) channel span for mask-type bands.
Definition: LoRaWAN.h:424
IDs of all currently supported bands.
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:334
float freq
The channel frequency.
Definition: LoRaWAN.h:342
uint8_t idx
The channel number, as specified by defaults or the network.
Definition: LoRaWAN.h:339
uint8_t drMin
Minimum allowed datarate for this channel.
Definition: LoRaWAN.h:345
bool enabled
Whether this channel is enabled (can be used) or is disabled.
Definition: LoRaWAN.h:336
uint8_t drMax
Maximum allowed datarate for this channel (inclusive)
Definition: LoRaWAN.h:348
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:359
uint8_t joinRequestDataRate
Allowed data rates for a join request message.
Definition: LoRaWAN.h:376
float freqStart
Center frequency of the first channel in span.
Definition: LoRaWAN.h:364
uint8_t numChannels
Total number of channels in the span.
Definition: LoRaWAN.h:361
uint8_t drMax
Maximum allowed datarate for all channels in this span (inclusive)
Definition: LoRaWAN.h:373
float freqStep
Frequency step between adjacent channels.
Definition: LoRaWAN.h:367
uint8_t drMin
Minimum allowed datarate for all channels in this span.
Definition: LoRaWAN.h:370
Structure to save extra information about uplink/downlink event.
Definition: LoRaWAN.h:493
float freq
Frequency in MHz.
Definition: LoRaWAN.h:508
bool confirmed
Whether the event is confirmed or not (e.g., confirmed uplink sent by user application)
Definition: LoRaWAN.h:498
uint8_t fPort
Port number.
Definition: LoRaWAN.h:517
int16_t power
Transmit power in dBm for uplink, or RSSI for downlink.
Definition: LoRaWAN.h:511
bool confirming
Whether the event is confirming a previous request (e.g., server downlink reply to confirmed uplink s...
Definition: LoRaWAN.h:502
uint8_t datarate
Datarate.
Definition: LoRaWAN.h:505
uint8_t dir
Event direction, one of RADIOLIB_LORAWAN_CHANNEL_DIR_*.
Definition: LoRaWAN.h:495
uint32_t fCnt
The appropriate frame counter - for different events, different frame counters will be reported!
Definition: LoRaWAN.h:514
Structure to save extra information about activation event.
Definition: LoRaWAN.h:478
bool newSession
Whether a new session was started.
Definition: LoRaWAN.h:480
uint32_t joinNonce
The received Join-Request JoinNonce value.
Definition: LoRaWAN.h:486
uint16_t devNonce
The transmitted Join-Request DevNonce value.
Definition: LoRaWAN.h:483
Structure to save information about MAC command.
Definition: LoRaWAN.h:248
uint8_t payload[RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN]
Payload buffer (5 bytes is the longest possible)
Definition: LoRaWAN.h:253
uint8_t len
Length of the payload.
Definition: LoRaWAN.h:256
uint8_t cid
The command ID.
Definition: LoRaWAN.h:250
uint8_t repeat
Repetition counter (the command will be uplinked repeat + 1 times)
Definition: LoRaWAN.h:259
Structure to hold information about a queue of MAC commands.
Definition: LoRaWAN.h:266
LoRaWANMacCommand_t commands[RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE]
MAC command buffer.
Definition: LoRaWAN.h:274
uint8_t numCommands
Number of commands in the queue.
Definition: LoRaWAN.h:268
uint8_t len
Total length of the queue.
Definition: LoRaWAN.h:271
MAC command specification structure.
Definition: LoRaWAN.h:210
const uint8_t cid
Command ID.
Definition: LoRaWAN.h:212
const uint8_t lenDn
Uplink message length.
Definition: LoRaWAN.h:215
const uint8_t lenUp
Downlink message length.
Definition: LoRaWAN.h:218
const bool user
Whether this MAC command can be issued by the user or not.
Definition: LoRaWAN.h:221
Common data rate structure.
Definition: PhysicalLayer.h:38