1 #if !defined(_RADIOLIB_LORAWAN_H) && !defined(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_LORA_SYNC_WORD (0x34)
10 #define RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN (8)
11 #define RADIOLIB_LORAWAN_GFSK_SYNC_WORD (0xC194C1)
12 #define RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN (5)
15 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5)
16 #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_ACCEPT (0x01 << 5)
17 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_UP (0x02 << 5)
18 #define RADIOLIB_LORAWAN_MHDR_MTYPE_UNCONF_DATA_DOWN (0x03 << 5)
19 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP (0x04 << 5)
20 #define RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_DOWN (0x05 << 5)
21 #define RADIOLIB_LORAWAN_MHDR_MTYPE_PROPRIETARY (0x07 << 5)
22 #define RADIOLIB_LORAWAN_MHDR_MTYPE_MASK (0x07 << 5)
23 #define RADIOLIB_LORAWAN_MHDR_MAJOR_R1 (0x00 << 0)
26 #define RADIOLIB_LORAWAN_FCTRL_ADR_ENABLED (0x01 << 7)
27 #define RADIOLIB_LORAWAN_FCTRL_ADR_DISABLED (0x00 << 7)
28 #define RADIOLIB_LORAWAN_FCTRL_ADR_ACK_REQ (0x01 << 6)
29 #define RADIOLIB_LORAWAN_FCTRL_ACK (0x01 << 5)
30 #define RADIOLIB_LORAWAN_FCTRL_FRAME_PENDING (0x01 << 4)
33 #define RADIOLIB_LORAWAN_FPORT_MAC_COMMAND (0x00 << 0)
34 #define RADIOLIB_LORAWAN_FPORT_RESERVED (0xE0 << 0)
37 #define RADIOLIB_LORAWAN_LINK_CHECK_REQ (0x02 << 0)
38 #define RADIOLIB_LORAWAN_LINK_ADR_ANS (0x03 << 0)
39 #define RADIOLIB_LORAWAN_DUTY_CYCLE_ANS (0x04 << 0)
40 #define RADIOLIB_LORAWAN_RX_PARAM_SETUP_ANS (0x05 << 0)
41 #define RADIOLIB_LORAWAN_DEV_STATUS_ANS (0x06 << 0)
42 #define RADIOLIB_LORAWAN_NEW_CHANNEL_ANS (0x07 << 0)
43 #define RADIOLIB_LORAWAN_RX_TIMING_SETUP_ANS (0x08 << 0)
45 #define RADIOLIB_LORAWAN_NOPTS_LEN (8)
48 #define RADIOLIB_LORAWAN_DATA_RATE_FSK_50_K (0x01 << 7)
49 #define RADIOLIB_LORAWAN_DATA_RATE_SF_12 (0x06 << 4)
50 #define RADIOLIB_LORAWAN_DATA_RATE_SF_11 (0x05 << 4)
51 #define RADIOLIB_LORAWAN_DATA_RATE_SF_10 (0x04 << 4)
52 #define RADIOLIB_LORAWAN_DATA_RATE_SF_9 (0x03 << 4)
53 #define RADIOLIB_LORAWAN_DATA_RATE_SF_8 (0x02 << 4)
54 #define RADIOLIB_LORAWAN_DATA_RATE_SF_7 (0x01 << 4)
55 #define RADIOLIB_LORAWAN_DATA_RATE_BW_500_KHZ (0x00 << 2)
56 #define RADIOLIB_LORAWAN_DATA_RATE_BW_250_KHZ (0x01 << 2)
57 #define RADIOLIB_LORAWAN_DATA_RATE_BW_125_KHZ (0x02 << 2)
58 #define RADIOLIB_LORAWAN_DATA_RATE_BW_RESERVED (0x03 << 2)
59 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_5 (0x00 << 0)
60 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_6 (0x01 << 0)
61 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_7 (0x02 << 0)
62 #define RADIOLIB_LORAWAN_DATA_RATE_CR_4_8 (0x03 << 0)
63 #define RADIOLIB_LORAWAN_DATA_RATE_UNUSED (0xFF << 0)
65 #define RADIOLIB_LORAWAN_CHANNEL_DIR_UPLINK (0x00 << 0)
66 #define RADIOLIB_LORAWAN_CHANNEL_DIR_DOWNLINK (0x01 << 0)
67 #define RADIOLIB_LORAWAN_CHANNEL_DIR_BOTH (0x02 << 0)
68 #define RADIOLIB_LORAWAN_CHANNEL_DIR_NONE (0x03 << 0)
69 #define RADIOLIB_LORAWAN_CFLIST_TYPE_FREQUENCIES (0)
70 #define RADIOLIB_LORAWAN_CFLIST_TYPE_MASK (1)
71 #define RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES (16)
74 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS (1000)
75 #define RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS ((RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS) + 1000)
76 #define RADIOLIB_LORAWAN_RX_WINDOW_LEN_MS (500)
77 #define RADIOLIB_LORAWAN_RX1_DR_OFFSET (0)
78 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_1_MS (5000)
79 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DELAY_2_MS (6000)
80 #define RADIOLIB_LORAWAN_MAX_FCNT_GAP (16384)
81 #define RADIOLIB_LORAWAN_ADR_ACK_LIMIT (64)
82 #define RADIOLIB_LORAWAN_ADR_ACK_DELAY (32)
83 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MIN_MS (1000)
84 #define RADIOLIB_LORAWAN_RETRANSMIT_TIMEOUT_MAX_MS (3000)
85 #define RADIOLIB_LORAWAN_POWER_STEP_SIZE_DBM (-2)
88 #define RADIOLIB_LORAWAN_JOIN_REQUEST_LEN (23)
89 #define RADIOLIB_LORAWAN_JOIN_REQUEST_JOIN_EUI_POS (1)
90 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_EUI_POS (9)
91 #define RADIOLIB_LORAWAN_JOIN_REQUEST_DEV_NONCE_POS (17)
92 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE (0xFF)
93 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_0 (0x00)
94 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_1 (0x01)
95 #define RADIOLIB_LORAWAN_JOIN_REQUEST_TYPE_2 (0x02)
98 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN (33)
99 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_NONCE_POS (1)
100 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS (4)
101 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS (7)
102 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JOIN_EUI_POS (4)
103 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DL_SETTINGS_POS (11)
104 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_RX_DELAY_POS (12)
105 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_NONCE_POS (12)
106 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS (13)
107 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN (16)
108 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_TYPE_POS (RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS + RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN - 1)
111 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_0 (0x00 << 7)
112 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_R_1_1 (0x01 << 7)
113 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_F_NWK_S_INT_KEY (0x01)
114 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY (0x02)
115 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_S_NWK_S_INT_KEY (0x03)
116 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_NWK_S_ENC_KEY (0x04)
117 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_ENC_KEY (0x05)
118 #define RADIOLIB_LORAWAN_JOIN_ACCEPT_JS_INT_KEY (0x06)
121 #define RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS (16)
122 #define RADIOLIB_LORAWAN_FHDR_DEV_ADDR_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 1)
123 #define RADIOLIB_LORAWAN_FHDR_FCTRL_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 5)
124 #define RADIOLIB_LORAWAN_FHDR_FCNT_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 6)
125 #define RADIOLIB_LORAWAN_FHDR_FOPTS_POS (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8)
126 #define RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK (0x0F)
127 #define RADIOLIB_LORAWAN_FHDR_FOPTS_MAX_LEN (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 16)
128 #define RADIOLIB_LORAWAN_FHDR_FPORT_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 8 + (FOPTS))
129 #define RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(FOPTS) (RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS + 9 + (FOPTS))
130 #define RADIOLIB_LORAWAN_FRAME_LEN(PAYLOAD, FOPTS) (16 + 13 + (PAYLOAD) + (FOPTS))
133 #define RADIOLIB_LORAWAN_BLOCK_MAGIC_POS (0)
134 #define RADIOLIB_LORAWAN_BLOCK_DIR_POS (5)
135 #define RADIOLIB_LORAWAN_BLOCK_DEV_ADDR_POS (6)
136 #define RADIOLIB_LORAWAN_BLOCK_FCNT_POS (10)
139 #define RADIOLIB_LORAWAN_ENC_BLOCK_MAGIC (0x01)
140 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_ID_POS (4)
141 #define RADIOLIB_LORAWAN_ENC_BLOCK_COUNTER_POS (15)
144 #define RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC (0x49)
145 #define RADIOLIB_LORAWAN_MIC_BLOCK_LEN_POS (15)
146 #define RADIOLIB_LORAWAN_MIC_DATA_RATE_POS (3)
147 #define RADIOLIB_LORAWAN_MIC_CH_INDEX_POS (4)
150 #define RADIOLIB_LORAWAN_MAGIC (0x12AD101B)
153 #define RADIOLIB_LORAWAN_MAC_CMD_RESET (0x01)
154 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_CHECK (0x02)
155 #define RADIOLIB_LORAWAN_MAC_CMD_LINK_ADR (0x03)
156 #define RADIOLIB_LORAWAN_MAC_CMD_DUTY_CYCLE (0x04)
157 #define RADIOLIB_LORAWAN_MAC_CMD_RX_PARAM_SETUP (0x05)
158 #define RADIOLIB_LORAWAN_MAC_CMD_DEV_STATUS (0x06)
159 #define RADIOLIB_LORAWAN_MAC_CMD_NEW_CHANNEL (0x07)
160 #define RADIOLIB_LORAWAN_MAC_CMD_RX_TIMING_SETUP (0x08)
161 #define RADIOLIB_LORAWAN_MAC_CMD_TX_PARAM_SETUP (0x09)
162 #define RADIOLIB_LORAWAN_MAC_CMD_DL_CHANNEL (0x0A)
163 #define RADIOLIB_LORAWAN_MAC_CMD_REKEY (0x0B)
164 #define RADIOLIB_LORAWAN_MAC_CMD_ADR_PARAM_SETUP (0x0C)
165 #define RADIOLIB_LORAWAN_MAC_CMD_DEVICE_TIME (0x0D)
166 #define RADIOLIB_LORAWAN_MAC_CMD_FORCE_REJOIN (0x0E)
167 #define RADIOLIB_LORAWAN_MAC_CMD_REJOIN_PARAM_SETUP (0x0F)
168 #define RADIOLIB_LORAWAN_MAC_CMD_PROPRIETARY (0x80)
171 #define RADIOLIB_LORAWAN_MAC_COMMAND_QUEUE_SIZE (8)
174 #define RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS (8)
198 uint8_t
dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES];
202 #define RADIOLIB_LORAWAN_CHANNEL_SPAN_NONE { .direction = RADIOLIB_LORAWAN_CHANNEL_DIR_NONE, .joinRequestDataRate = RADIOLIB_LORAWAN_DATA_RATE_UNUSED, .numChannels = 0, .freqStart = 0, .freqStep = 0, .dataRates = { 0 } }
324 int16_t
beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey,
bool force =
false);
336 int16_t
beginAPB(uint32_t addr, uint8_t* nwkSKey, uint8_t* appSKey, uint8_t* fNwkSIntKey = NULL, uint8_t* sNwkSIntKey = NULL);
338 #if defined(RADIOLIB_BUILD_ARDUINO)
345 int16_t
uplink(String& str, uint8_t port);
354 int16_t
uplink(
const char* str, uint8_t port);
363 int16_t
uplink(uint8_t* data,
size_t len, uint8_t port);
365 #if defined(RADIOLIB_BUILD_ARDUINO)
380 int16_t
downlink(uint8_t* data,
size_t* len);
389 #if !defined(RADIOLIB_GODMODE)
396 .commands = { { .
cid = 0, .len = 0, .payload = { 0 }, .repeat = 0, } },
400 .commands = { { .
cid = 0, .len = 0, .payload = { 0 }, .repeat = 0, } },
406 uint32_t devAddr = 0;
407 uint8_t appSKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
408 uint8_t fNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
409 uint8_t sNwkSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
410 uint8_t nwkSEncKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
411 uint8_t jSIntKey[RADIOLIB_AES128_KEY_SIZE] = { 0 };
414 float availableChannelsFreq[2][RADIOLIB_LORAWAN_NUM_AVAILABLE_CHANNELS] = { { 0 }, { 0 } };
417 float channelFreq[2] = { 0 };
423 uint8_t dataRate[2] = { 0 };
426 uint8_t chIndex[2] = { 0 };
429 float backupFreq = 0;
430 uint8_t backupDataRate = 0;
433 uint32_t rxDelayStart = 0;
436 uint32_t rxDelays[2] = { RADIOLIB_LORAWAN_RECEIVE_DELAY_1_MS, RADIOLIB_LORAWAN_RECEIVE_DELAY_2_MS };
439 uint8_t battLevel = 0xFF;
442 uint32_t generateMIC(uint8_t* msg,
size_t len, uint8_t* key);
446 bool verifyMIC(uint8_t* msg,
size_t len, uint8_t* key);
450 int16_t setPhyProperties();
454 int16_t setupChannels();
460 int16_t findChannelId(uint8_t dir, uint8_t* ch, uint8_t* dr, int8_t min, int8_t max);
466 int16_t findChannelFreq(uint8_t dir, uint8_t ch,
float* freq);
469 int16_t configureChannel(uint8_t dir);
472 int16_t sendMacCommand(uint8_t cid, uint8_t* payload,
size_t payloadLen, uint8_t* reply,
size_t replyLen);
484 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);
488 static T ntoh(uint8_t* buff,
size_t size = 0);
492 static void hton(uint8_t* buff, T val,
size_t size = 0);
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:278
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey, bool force=false)
Join network by performing over-the-air activation. By this procedure, the device will perform an exc...
Definition: LoRaWAN.cpp:67
bool FSK
Set to true to force the node to only use FSK channels. Set to false by default.
Definition: LoRaWAN.h:281
int16_t beginAPB(uint32_t addr, uint8_t *nwkSKey, uint8_t *appSKey, uint8_t *fNwkSIntKey=NULL, uint8_t *sNwkSIntKey=NULL)
Join network by performing activation by personalization. In this procedure, all necessary configurat...
Definition: LoRaWAN.cpp:338
void setDeviceStatus(uint8_t battLevel)
Set device status.
Definition: LoRaWAN.cpp:781
int16_t downlink(uint8_t *data, size_t *len)
Wait for downlink from the server in either RX1 or RX2 window.
Definition: LoRaWAN.cpp:524
int8_t numChannels
Number of supported channels. Some band plans only support a subset of available channels....
Definition: LoRaWAN.h:293
void wipe()
Wipe internal persistent parameters. This will reset all counters and saved variables,...
Definition: LoRaWAN.cpp:42
int8_t startChannel
Starting channel offset. Some band plans only support a subset of available channels....
Definition: LoRaWAN.h:287
LoRaWANNode(PhysicalLayer *phy, const LoRaWANBand_t *band)
Default constructor.
Definition: LoRaWAN.cpp:33
int16_t uplink(const char *str, uint8_t port)
Send a message to the server.
Definition: LoRaWAN.cpp:367
int16_t begin()
Join network by loading information from persistent storage.
Definition: LoRaWAN.cpp:47
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:208
uint8_t cfListType
Whether the optional channels are defined as list of frequencies or bit mask.
Definition: LoRaWAN.h:225
LoRaWANChannelSpan_t backupChannel
Backup downlink (RX2) channel - just a single channel, but using the same structure for convenience.
Definition: LoRaWAN.h:237
uint8_t downlinkDataRateBase
The base downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:210
int8_t powerMax
Maximum allowed output power in this band in dBm.
Definition: LoRaWAN.h:219
LoRaWANChannelSpan_t defaultChannels[3]
Default uplink (TX/RX1) channels defined by LoRaWAN Regional Parameters.
Definition: LoRaWAN.h:234
uint8_t payloadLenMax[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of allowed maximum payload lengths for each data rate.
Definition: LoRaWAN.h:216
int8_t powerNumSteps
Number of power steps in this band.
Definition: LoRaWAN.h:222
float fskFreq
FSK channel frequency.
Definition: LoRaWAN.h:228
uint8_t numChannelSpans
Number of channel spans in the band.
Definition: LoRaWAN.h:231
uint8_t downlinkDataRateMin
The minimum allowed downlink data rate. Used to calculate data rate changes for adaptive data rate.
Definition: LoRaWAN.h:213
Structure to save information about LoRaWAN channels. To save space, adjacent channels are saved in "...
Definition: LoRaWAN.h:181
uint8_t joinRequestDataRate
Allowed data rates for a join request message.
Definition: LoRaWAN.h:186
float freqStart
Center frequency of the first channel in span.
Definition: LoRaWAN.h:192
uint8_t numChannels
Total number of channels in the span.
Definition: LoRaWAN.h:189
uint8_t dataRates[RADIOLIB_LORAWAN_CHANNEL_NUM_DATARATES]
Array of datarates supported by all channels in the span.
Definition: LoRaWAN.h:198
float freqStep
Frequency step between adjacent channels.
Definition: LoRaWAN.h:195
uint8_t direction
Whether this channel span is for uplink, downlink, or both directions.
Definition: LoRaWAN.h:183
Structure to save information about MAC command.
Definition: LoRaWAN.h:255
size_t len
Length of the payload.
Definition: LoRaWAN.h:260
uint8_t cid
The command ID.
Definition: LoRaWAN.h:257
uint8_t repeat
Repetition counter (the command will be uplinked repeat + 1 times)
Definition: LoRaWAN.h:266
uint8_t payload[5]
Payload buffer (5 bytes is the longest possible)
Definition: LoRaWAN.h:263
Definition: LoRaWAN.h:269
Definition: PhysicalLayer.h:21