1 #if !defined(_RADIOLIB_LORAWAN_H) && !RADIOLIB_EXCLUDE_LORAWAN
2 #define _RADIOLIB_LORAWAN_H
4 #include "../../TypeDef.h"
5 #include "../PhysicalLayer/PhysicalLayer.h"
6 #include "../../utils/Cryptography.h"
9 #define RADIOLIB_LORAWAN_MODE_OTAA (0x07AA)
10 #define RADIOLIB_LORAWAN_MODE_ABP (0x0AB9)
11 #define RADIOLIB_LORAWAN_MODE_NONE (0x0000)
14 #define RADIOLIB_LORAWAN_CLASS_A (0x0A)
15 #define RADIOLIB_LORAWAN_CLASS_B (0x0B)
16 #define RADIOLIB_LORAWAN_CLASS_C (0x0C)
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)
25 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5)
26 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_ACCEPT (0x01 << 5)
27 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_UP (0x02 << 5)
28 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_DOWN (0x03 << 5)
29 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP (0x04 << 5)
30 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_DOWN (0x05 << 5)
31 #define RADIOLIB_LORAWAN_MHDR_MTYPE_PROPRIETARY (0x07 << 5)
32 #define RADIOLIB_LORAWAN_MHDR_MTYPE_MASK (0x07 << 5)
33 #define RADIOLIB_LORAWAN_MHDR_MAJOR_R1 (0x00 << 0)
36 #define RADIOLIB_LORAWAN_FCTRL_ADR_ENABLED (0x01 << 7)
37 #define RADIOLIB_LORAWAN_FCTRL_ADR_DISABLED (0x00 << 7)
38 #define RADIOLIB_LORAWAN_FCTRL_ADR_ACK_REQ (0x01 << 6)
39 #define RADIOLIB_LORAWAN_FCTRL_ACK (0x01 << 5)
40 #define RADIOLIB_LORAWAN_FCTRL_FRAME_PENDING (0x01 << 4)
43 #define RADIOLIB_LORAWAN_FPORT_MAC_COMMAND (0x00 << 0)
44 #define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0)
47 #define RADIOLIB_LORAWAN_LINK_CHECK_REQ (0x02 << 0)
48 #define RADIOLIB_LORAWAN_LINK_ADR_ANS (0x03 << 0)
49 #define RADIOLIB_LORAWAN_DUTY_CYCLE_ANS (0x04 << 0)
50 #define RADIOLIB_LORAWAN_RX_PARAM_SETUP_ANS (0x05 << 0)
51 #define RADIOLIB_LORAWAN_DEV_STATUS_ANS (0x06 << 0)
52 #define RADIOLIB_LORAWAN_NEW_CHANNEL_ANS (0x07 << 0)
53 #define RADIOLIB_LORAWAN_RX_TIMING_SETUP_ANS (0x08 << 0)
55 #define RADIOLIB_LORAWAN_NOPTS_LEN (8)
58 #define RADIOLIB_LORAWAN_DATA_RATE_FSK_50_K (0x01 << 7)
59 #define RADIOLIB_LORAWAN_DATA_RATE_SF_12 (0x06 << 4)
60 #define RADIOLIB_LORAWAN_DATA_RATE_SF_11 (0x05 << 4)
61 #define RADIOLIB_LORAWAN_DATA_RATE_SF_10 (0x04 << 4)
62 #define RADIOLIB_LORAWAN_DATA_RATE_SF_9 (0x03 << 4)
63 #define RADIOLIB_LORAWAN_DATA_RATE_SF_8 (0x02 << 4)
64 #define RADIOLIB_LORAWAN_DATA_RATE_SF_7 (0x01 << 4)
65 #define RADIOLIB_LORAWAN_DATA_RATE_BW_500_KHZ (0x00 << 2)
66 #define RADIOLIB_LORAWAN_DATA_RATE_BW_250_KHZ (0x01 << 2)
67 #define RADIOLIB_LORAWAN_DATA_RATE_BW_125_KHZ (0x02 << 2)
68 #define RADIOLIB_LORAWAN_DATA_RATE_BW_RESERVED (0x03 << 2)
69 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_5 (0x00 << 0)
70 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_6 (0x01 << 0)
71 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_7 (0x02 << 0)
72 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_8 (0x03 << 0)
73 #define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0xFF << 0)
75 #define RADIOLIB_LORAWAN_CHANNEL_DIR_UPLINK (0x00 << 0)
76 #define RADIOLIB_LORAWAN_CHANNEL_DIR_DOWNLINK (0x01 << 0)
77 #define RADIOLIB_LORAWAN_CHANNEL_DIR_BOTH (0x02 << 0)
78 #define RADIOLIB_LORAWAN_CHANNEL_DIR_NONE (0x03 << 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)
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)
97 #define RADIOLIB_LORAWAN_REJOIN_MAX_TIME_N (15)
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)
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_JOIN_EUI_POS (4)
115 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
116 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
117 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_NONCE_POS (12)
118 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
119 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
120 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
123 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7)
124 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7)
125 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
126 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
127 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
128 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
129 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
130 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
133 #define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
134 #define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
135 #define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
136 #define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
137 #define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
138 #define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
139 #define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 16)
140 #define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
141 #define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
142 #define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
145 #define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
146 #define RADIOLIB_LORAWAN_BLOCK_CONF_FCNT_POS (1)
147 #define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
148 #define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
149 #define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
152 #define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
153 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
154 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
157 #define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
158 #define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
159 #define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
160 #define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
163 #define RADIOLIB_LORAWAN_NUM_MAC_COMMANDS (16)
165 #define RADIOLIB_LORAWAN_MAC_RESET (0x01)
166 #define RADIOLIB_LORAWAN_MAC_LINK_CHECK (0x02)
167 #define RADIOLIB_LORAWAN_MAC_LINK_ADR (0x03)
168 #define RADIOLIB_LORAWAN_MAC_DUTY_CYCLE (0x04)
169 #define RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP (0x05)
170 #define RADIOLIB_LORAWAN_MAC_DEV_STATUS (0x06)
171 #define RADIOLIB_LORAWAN_MAC_NEW_CHANNEL (0x07)
172 #define RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP (0x08)
173 #define RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP (0x09)
174 #define RADIOLIB_LORAWAN_MAC_DL_CHANNEL (0x0A)
175 #define RADIOLIB_LORAWAN_MAC_REKEY (0x0B)
176 #define RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP (0x0C)
177 #define RADIOLIB_LORAWAN_MAC_DEVICE_TIME (0x0D)
178 #define RADIOLIB_LORAWAN_MAC_FORCE_REJOIN (0x0E)
179 #define RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP (0x0F)
180 #define RADIOLIB_LORAWAN_MAC_PROPRIETARY (0x80)
183 #define RADIOLIB_LORAWAN_DWELL_TIME (400)
186 #define RADIOLIB_LORAWAN_VERSION_NONE (0xFF)
189 #define RADIOLIB_LORAWAN_FCNT_NONE (0xFFFFFFFF)
192 #define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (9)
195 #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (16)
198 #define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_DOWN (5)
199 #define RADIOLIB_LORAWAN_MAX_MAC_COMMAND_LEN_UP (2)
200 #define RADIOLIB_LORAWAN_MAX_NUM_ADR_COMMANDS (8)
210 { 0x00, 0, 0,
false },
211 { RADIOLIB_LORAWAN_MAC_RESET, 1, 1,
false },
212 { RADIOLIB_LORAWAN_MAC_LINK_CHECK, 2, 0,
true },
213 { RADIOLIB_LORAWAN_MAC_LINK_ADR, 4, 1,
false },
214 { RADIOLIB_LORAWAN_MAC_DUTY_CYCLE, 1, 0,
false },
215 { RADIOLIB_LORAWAN_MAC_RX_PARAM_SETUP, 4, 1,
false },
216 { RADIOLIB_LORAWAN_MAC_DEV_STATUS, 0, 2,
false },
217 { RADIOLIB_LORAWAN_MAC_NEW_CHANNEL, 5, 1,
false },
218 { RADIOLIB_LORAWAN_MAC_RX_TIMING_SETUP, 1, 0,
false },
219 { RADIOLIB_LORAWAN_MAC_TX_PARAM_SETUP, 1, 0,
false },
220 { RADIOLIB_LORAWAN_MAC_DL_CHANNEL, 4, 1,
false },
221 { RADIOLIB_LORAWAN_MAC_REKEY, 1, 1,
false },
222 { RADIOLIB_LORAWAN_MAC_ADR_PARAM_SETUP, 1, 0,
false },
223 { RADIOLIB_LORAWAN_MAC_DEVICE_TIME, 5, 0,
true },
224 { RADIOLIB_LORAWAN_MAC_FORCE_REJOIN, 2, 0,
false },
225 { RADIOLIB_LORAWAN_MAC_REJOIN_PARAM_SETUP, 1, 1,
false },
226 { RADIOLIB_LORAWAN_MAC_PROPRIETARY, 5, 0,
true }
229 #define RADIOLIB_LORAWAN_NONCES_VERSION_VAL (0x0001)
231 enum LoRaWANSchemeBase_t {
232 RADIOLIB_LORAWAN_NONCES_VERSION = 0x00,
233 RADIOLIB_LORAWAN_NONCES_MODE = 0x02,
234 RADIOLIB_LORAWAN_NONCES_CLASS = 0x04,
235 RADIOLIB_LORAWAN_NONCES_PLAN = 0x05,
236 RADIOLIB_LORAWAN_NONCES_CHECKSUM = 0x06,
237 RADIOLIB_LORAWAN_NONCES_DEV_NONCE = 0x08,
238 RADIOLIB_LORAWAN_NONCES_JOIN_NONCE = 0x0A,
239 RADIOLIB_LORAWAN_NONCES_ACTIVE = 0x0D,
240 RADIOLIB_LORAWAN_NONCES_SIGNATURE = 0x0E,
241 RADIOLIB_LORAWAN_NONCES_BUF_SIZE = 0x10
244 enum LoRaWANSchemeSession_t {
245 RADIOLIB_LORAWAN_SESSION_NWK_SENC_KEY = 0x00,
246 RADIOLIB_LORAWAN_SESSION_APP_SKEY = 0x10,
247 RADIOLIB_LORAWAN_SESSION_FNWK_SINT_KEY = 0x20,
248 RADIOLIB_LORAWAN_SESSION_SNWK_SINT_KEY = 0x30,
249 RADIOLIB_LORAWAN_SESSION_DEV_ADDR = 0x40,
250 RADIOLIB_LORAWAN_SESSION_NONCES_SIGNATURE = 0x44,
251 RADIOLIB_LORAWAN_SESSION_A_FCNT_DOWN = 0x46,
252 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_UP = 0x4A,
253 RADIOLIB_LORAWAN_SESSION_CONF_FCNT_DOWN = 0x4E,
254 RADIOLIB_LORAWAN_SESSION_RJ_COUNT0 = 0x52,
255 RADIOLIB_LORAWAN_SESSION_RJ_COUNT1 = 0x54,
256 RADIOLIB_LORAWAN_SESSION_HOMENET_ID = 0x56,
257 RADIOLIB_LORAWAN_SESSION_VERSION = 0x5A,
258 RADIOLIB_LORAWAN_SESSION_DUTY_CYCLE = 0x5B,
259 RADIOLIB_LORAWAN_SESSION_RX_PARAM_SETUP = 0x5C,
260 RADIOLIB_LORAWAN_SESSION_RX_TIMING_SETUP = 0x60,
261 RADIOLIB_LORAWAN_SESSION_TX_PARAM_SETUP = 0x61,
262 RADIOLIB_LORAWAN_SESSION_ADR_PARAM_SETUP = 0x62,
263 RADIOLIB_LORAWAN_SESSION_REJOIN_PARAM_SETUP = 0x63,
264 RADIOLIB_LORAWAN_SESSION_BEACON_FREQ = 0x64,
265 RADIOLIB_LORAWAN_SESSION_PING_SLOT_CHANNEL = 0x67,
266 RADIOLIB_LORAWAN_SESSION_PERIODICITY = 0x6B,
267 RADIOLIB_LORAWAN_SESSION_LAST_TIME = 0x6C,
268 RADIOLIB_LORAWAN_SESSION_UL_CHANNELS = 0x70,
269 RADIOLIB_LORAWAN_SESSION_DL_CHANNELS = 0xF0,
270 RADIOLIB_LORAWAN_SESSION_MAC_QUEUE_UL = 0x0130,
271 RADIOLIB_LORAWAN_SESSION_N_FCNT_DOWN = 0x017A,
272 RADIOLIB_LORAWAN_SESSION_ADR_FCNT = 0x017E,
273 RADIOLIB_LORAWAN_SESSION_LINK_ADR = 0x0182,
274 RADIOLIB_LORAWAN_SESSION_FCNT_UP = 0x0186,
275 RADIOLIB_LORAWAN_SESSION_SIGNATURE = 0x018A,
276 RADIOLIB_LORAWAN_SESSION_BUF_SIZE = 0x018C
302 #define RADIOLIB_LORAWAN_CHANNEL_NONE { .enabled = false, .idx = RADIOLIB_LORAWAN_CHANNEL_INDEX_NONE, .freq = 0, .drMin = 0, .drMax = 0 }
330 #define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .numChannels = 0, .freqStart = 0, .freqStep = 0, .drMin = 0, .drMax = 0, .joinRequestDataRate = RADIOLIB_LORAWAN_DATA_RATE_UNUSED }
357 uint32_t dwellTimeDn;
381 uint8_t
dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
413 #define RADIOLIB_LORAWAN_NUM_SUPPORTED_BANDS (BandLast - BandEU868)
481 uint8_t rx1DrOffset = 0;
530 int16_t
restore(uint16_t checkSum, uint16_t lwMode, uint8_t lwClass, uint8_t freqPlan);
543 int16_t
beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey,
bool force =
false, uint8_t joinDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED);
557 int16_t
beginABP(uint32_t addr, uint8_t* nwkSKey, uint8_t* appSKey, uint8_t* fNwkSIntKey = NULL, uint8_t* sNwkSIntKey = NULL,
bool force =
false, uint8_t initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED);
577 #if defined(RADIOLIB_BUILD_ARDUINO)
587 int16_t
uplink(String& str, uint8_t port,
bool isConfirmed =
false,
LoRaWANEvent_t* event = NULL);
599 int16_t
uplink(
const char* str, uint8_t port,
bool isConfirmed =
false,
LoRaWANEvent_t* event = NULL);
611 int16_t
uplink(uint8_t* data,
size_t len, uint8_t port,
bool isConfirmed =
false,
LoRaWANEvent_t* event = NULL);
613 #if defined(RADIOLIB_BUILD_ARDUINO)
642 #if defined(RADIOLIB_BUILD_ARDUINO)
687 int16_t
sendReceive(uint8_t* dataUp,
size_t lenUp, uint8_t port, uint8_t* dataDown,
size_t* lenDown,
bool isConfirmed =
false,
LoRaWANEvent_t* eventUp = NULL,
LoRaWANEvent_t* eventDown = NULL);
746 void setADR(
bool enable =
true);
754 void setDutyCycle(
bool enable =
true, uint32_t msPerHour = 0);
774 void setDwellTime(
bool enable, uint32_t msPerUplink = 0);
796 void setCSMA(uint8_t backoffMax, uint8_t difsSlots,
bool enableCSMA =
false);
817 int16_t
getMacDeviceTimeAns(uint32_t* gpsEpoch, uint8_t* fraction,
bool returnUnix =
true);
825 #if !RADIOLIB_GODMODE
831 static int16_t checkBufferCommon(uint8_t *buffer, uint16_t size);
833 void beginCommon(uint8_t initialDr);
836 uint8_t bufferNonces[RADIOLIB_LORAWAN_NONCES_BUF_SIZE] = { 0 };
839 uint8_t bufferSession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE] = { 0 };
844 .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
849 .commands = { { .cid = 0, .payload = { 0 }, .len = 0, .repeat = 0, } },
854 uint32_t devAddr = 0;
855 uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
856 uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
857 uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
858 uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
859 uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
862 uint16_t devNonce = 0;
863 uint32_t joinNonce = 0;
866 uint32_t homeNetId = 0;
867 uint8_t adrLimitExp = RADIOLIB_LORAWAN_ADR_ACK_LIMIT_EXP;
868 uint8_t adrDelayExp = RADIOLIB_LORAWAN_ADR_ACK_DELAY_EXP;
870 uint8_t txPowerCur = 0;
871 uint8_t txPowerMax = 0;
873 uint32_t aFcntDown = 0;
874 uint32_t nFcntDown = 0;
875 uint32_t confFcntUp = RADIOLIB_LORAWAN_FCNT_NONE;
876 uint32_t confFcntDown = RADIOLIB_LORAWAN_FCNT_NONE;
877 uint32_t adrFcnt = 0;
883 uint16_t activeMode = RADIOLIB_LORAWAN_MODE_NONE;
886 bool adrEnabled =
true;
889 bool dutyCycleEnabled =
false;
890 uint32_t dutyCycle = 0;
893 bool dwellTimeEnabledUp =
false;
894 uint16_t dwellTimeUp = 0;
895 bool dwellTimeEnabledDn =
false;
896 uint16_t dwellTimeDn = 0;
910 LoRaWANChannel_t availableChannels[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS];
913 LoRaWANChannel_t currentChannels[2] = { RADIOLIB_LORAWAN_CHANNEL_NONE, RADIOLIB_LORAWAN_CHANNEL_NONE };
916 uint8_t dataRates[2] = { RADIOLIB_LORAWAN_DATA_RATE_UNUSED, RADIOLIB_LORAWAN_DATA_RATE_UNUSED };
922 uint32_t lastToA = 0;
925 uint32_t rxDelayStart = 0;
928 uint32_t rxDelayEnd = 0;
931 uint32_t rxDelays[2] = { RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
934 uint8_t battLevel = 0xFF;
937 bool isMACPayload =
false;
943 int16_t downlinkCommon();
946 uint32_t generateMIC(uint8_t* msg,
size_t len, uint8_t* key);
950 bool verifyMIC(uint8_t* msg,
size_t len, uint8_t* key);
954 int16_t setPhyProperties();
959 int16_t setupChannelsDyn(
bool joinRequest =
false);
963 int16_t setupChannelsFix(uint8_t subBand);
966 int16_t processCFList(uint8_t* cfList);
969 int16_t selectChannels();
972 int16_t findDataRate(uint8_t dr,
DataRate_t* dataRate);
975 int16_t configureChannel(uint8_t dir);
978 int16_t restoreChannels();
991 bool applyChannelMaskDyn(uint8_t chMaskCntl, uint16_t chMask);
994 bool applyChannelMaskFix(uint8_t chMaskCntl, uint16_t chMask);
997 uint8_t getMacPayloadLength(uint8_t cid);
1006 void processAES(uint8_t* in,
size_t len, uint8_t* key, uint8_t* out, uint32_t fcnt, uint8_t dir, uint8_t ctrId,
bool counter);
1009 static uint16_t checkSum16(uint8_t *key, uint16_t keyLen);
1012 template<
typename T>
1013 static T ntoh(uint8_t* buff,
size_t size = 0);
1016 template<
typename T>
1017 static void hton(uint8_t* buff, T val,
size_t size = 0);
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:477
int16_t uplink(const char *str, uint8_t port, bool isConfirmed=false, LoRaWANEvent_t *event=NULL)
Send a message to the server.
Definition: LoRaWAN.cpp:860
int16_t setBufferNonces(uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW base parameters with a supplied buffer.
Definition: LoRaWAN.cpp:56
int16_t setDatarate(uint8_t drUp)
Set uplink datarate. This should not be used when ADR is enabled.
Definition: LoRaWAN.cpp:1902
int16_t sendReceive(const char *strUp, uint8_t port, uint8_t *dataDown, size_t *lenDown, bool isConfirmed=false, LoRaWANEvent_t *eventUp=NULL, LoRaWANEvent_t *eventDown=NULL)
Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.
Definition: LoRaWAN.cpp:1611
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey, bool force=false, uint8_t joinDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
Join network by performing over-the-air activation. By this procedure, the device will perform an exc...
Definition: LoRaWAN.cpp:455
uint8_t maxPayloadDwellTime()
Returns the maximum payload given the currently present dwell time limits. WARNING: the addition of M...
Definition: LoRaWAN.cpp:1982
void setDwellTime(bool enable, uint32_t msPerUplink=0)
Toggle adherence to dwellTime limits to on or off.
Definition: LoRaWAN.cpp:1973
uint32_t getFcntUp()
Returns the last uplink's frame counter; also 0 if no uplink occured yet.
Definition: LoRaWAN.cpp:1636
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition: LoRaWAN.cpp:1631
int16_t beginABP(uint32_t addr, uint8_t *nwkSKey, uint8_t *appSKey, uint8_t *fNwkSIntKey=NULL, uint8_t *sNwkSIntKey=NULL, bool force=false, uint8_t initialDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
Join network by performing activation by personalization. In this procedure, all necessary configurat...
Definition: LoRaWAN.cpp:735
uint32_t dutyCycleInterval(uint32_t msPerHour, uint32_t airtime)
Calculate the minimum interval to adhere to a certain dutyCycle. This interval is based on the ToA of...
Definition: LoRaWAN.cpp:1954
void resetFcntDown()
Reset the downlink frame counters (application and network) This is unsafe and can possibly allow rep...
Definition: LoRaWAN.cpp:1651
int16_t restore(uint16_t checkSum, uint16_t lwMode, uint8_t lwClass, uint8_t freqPlan)
Restore session by loading information from persistent storage.
Definition: LoRaWAN.cpp:129
bool sendMacCommandReq(uint8_t cid)
Add a MAC command to the uplink queue. Only LinkCheck and DeviceTime are available to the user....
Definition: LoRaWAN.cpp:2092
int16_t saveSession()
Save the current state of the session to the session buffer.
Definition: LoRaWAN.cpp:820
uint8_t * getBufferNonces()
Returns the pointer to the internal buffer that holds the LW base parameters.
Definition: LoRaWAN.cpp:52
uint32_t getNFcntDown()
Returns the last network downlink's frame counter; also 0 if no network downlink occured yet.
Definition: LoRaWAN.cpp:1643
uint32_t getAFcntDown()
Returns the last application downlink's frame counter; also 0 if no application downlink occured yet.
Definition: LoRaWAN.cpp:1647
uint8_t * getBufferSession()
Returns the pointer to the internal buffer that holds the LW session parameters.
Definition: LoRaWAN.cpp:74
void wipe()
Wipe internal persistent parameters. This will reset all counters and saved variables,...
Definition: LoRaWAN.cpp:47
int16_t setTxPower(int8_t txPower)
Configure TX power of the radio module.
Definition: LoRaWAN.cpp:2002
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:2778
uint64_t getDevAddr()
Returns the DevAddr of the device, regardless of OTAA or ABP mode.
Definition: LoRaWAN.cpp:2795
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:1285
int16_t setBufferSession(uint8_t *persistentBuffer)
Fill the internal buffer that holds the LW session parameters with a supplied buffer.
Definition: LoRaWAN.cpp:81
LoRaWANNode(PhysicalLayer *phy, const LoRaWANBand_t *band, uint8_t subBand=0)
Default constructor.
Definition: LoRaWAN.cpp:30
void setDutyCycle(bool enable=true, uint32_t msPerHour=0)
Toggle adherence to dutyCycle limits to on or off.
Definition: LoRaWAN.cpp:1943
uint32_t timeUntilUplink()
Returns time in milliseconds until next uplink is available under dutyCycle limits.
Definition: LoRaWAN.cpp:1964
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:2767
void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA=false)
Configures CSMA for LoRaWAN as per TR-13, LoRa Alliance.
Definition: LoRaWAN.cpp:41
void setADR(bool enable=true)
Toggle ADR to on or off.
Definition: LoRaWAN.cpp:1939
bool isJoined()
Whether there is an ongoing session active.
Definition: LoRaWAN.cpp:816
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:34
Structure to save information about LoRaWAN band.
Definition: LoRaWAN.h:336
uint32_t dwellTimeUp
Maximum dwell time per message in milliseconds.
Definition: LoRaWAN.h:356
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition: LoRaWAN.h:347
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
The corresponding datarates, bandwidths and coding rates for DR index.
Definition: LoRaWAN.h:381
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum payload lengths for each data rate.
Definition: LoRaWAN.h:344
LoRaWANChannel_t txFreqs[3]
A set of default uplink (TX) channels for frequency-type bands.
Definition: LoRaWAN.h:360
LoRaWANChannel_t txJoinReq[3]
A set of possible extra channels for the Join-Request message for frequency-type bands.
Definition: LoRaWAN.h:363
int8_t powerNumSteps
Number of power steps in this band.
Definition: LoRaWAN.h:350
LoRaWANChannelSpan_t txSpans[2]
Default uplink (TX) channel spans for mask-type bands, including Join-Request parameters.
Definition: LoRaWAN.h:369
uint8_t rx1DataRateBase
The base downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:375
uint8_t bandNum
Identier for this band.
Definition: LoRaWAN.h:338
uint8_t bandType
Whether the channels are fixed per specification, or dynamically allocated through the network (plus ...
Definition: LoRaWAN.h:341
LoRaWANChannel_t rx2
Backup channel for downlink (RX2) window.
Definition: LoRaWAN.h:378
uint8_t numTxSpans
The number of TX channel spans for mask-type bands.
Definition: LoRaWAN.h:366
LoRaWANChannelSpan_t rx1Span
Default downlink (RX1) channel span for mask-type bands.
Definition: LoRaWAN.h:372
uint32_t dutyCycle
Number of milliseconds per hour of allowed Time-on-Air.
Definition: LoRaWAN.h:353
IDs of all currently supported bands.
Definition: LoRaWAN.h:284
float freq
The channel frequency.
Definition: LoRaWAN.h:292
uint8_t idx
The channel number, as specified by defaults or the network.
Definition: LoRaWAN.h:289
uint8_t drMin
Minimum allowed datarate for this channel.
Definition: LoRaWAN.h:295
bool enabled
Whether this channel is enabled (can be used) or is disabled.
Definition: LoRaWAN.h:286
uint8_t drMax
Maximum allowed datarate for this channel (inclusive)
Definition: LoRaWAN.h:298
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:309
uint8_t joinRequestDataRate
Allowed data rates for a join request message.
Definition: LoRaWAN.h:326
float freqStart
Center frequency of the first channel in span.
Definition: LoRaWAN.h:314
uint8_t numChannels
Total number of channels in the span.
Definition: LoRaWAN.h:311
uint8_t drMax
Maximum allowed datarate for all channels in this span (inclusive)
Definition: LoRaWAN.h:323
float freqStep
Frequency step between adjacent channels.
Definition: LoRaWAN.h:317
uint8_t drMin
Minimum allowed datarate for all channels in this span.
Definition: LoRaWAN.h:320
Structure to save extra information about uplink/downlink event.
Definition: LoRaWAN.h:446
float freq
Frequency in MHz.
Definition: LoRaWAN.h:461
bool confirmed
Whether the event is confirmed or not (e.g., confirmed uplink sent by user application)
Definition: LoRaWAN.h:451
int16_t power
Transmit power in dBm for uplink, or RSSI for downlink.
Definition: LoRaWAN.h:464
bool confirming
Whether the event is confirming a previous request (e.g., server downlink reply to confirmed uplink s...
Definition: LoRaWAN.h:455
uint8_t datarate
Datarate.
Definition: LoRaWAN.h:458
uint8_t dir
Event direction, one of RADIOLIB_LORAWAN_CHANNEL_DIR_*.
Definition: LoRaWAN.h:448
uint32_t fcnt
The appropriate frame counter - for different events, different frame counters will be reported!
Definition: LoRaWAN.h:467
uint8_t port
Port number.
Definition: LoRaWAN.h:470
Structure to save information about MAC command.
Definition: LoRaWAN.h:422
uint8_t len
Length of the payload.
Definition: LoRaWAN.h:430
uint8_t cid
The command ID.
Definition: LoRaWAN.h:424
uint8_t repeat
Repetition counter (the command will be uplinked repeat + 1 times)
Definition: LoRaWAN.h:433
uint8_t payload[5]
Payload buffer (5 bytes is the longest possible)
Definition: LoRaWAN.h:427
Definition: LoRaWAN.h:436
Definition: LoRaWAN.h:202
Definition: PhysicalLayer.h:21