Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Committer:
Shaun Nelson
Date:
Fri Nov 11 16:03:36 2016 -0500
Branch:
v4.2.0
Revision:
27:cda377b2a41f
Parent:
26:4627985dc5be
Child:
29:c7583fcfad8c
Back port 4.3 retransmission of MAC RxParamSetup and RxTimingSetup answer until downlink.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:91d1a7783bb9 1 /*
mluis 0:91d1a7783bb9 2 / _____) _ | |
mluis 0:91d1a7783bb9 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:91d1a7783bb9 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:91d1a7783bb9 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:91d1a7783bb9 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:91d1a7783bb9 7 (C)2013 Semtech
mluis 2:14a5d6ad92d5 8 ___ _____ _ ___ _ _____ ___ ___ ___ ___
mluis 2:14a5d6ad92d5 9 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
mluis 2:14a5d6ad92d5 10 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
mluis 2:14a5d6ad92d5 11 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
mluis 2:14a5d6ad92d5 12 embedded.connectivity.solutions===============
mluis 0:91d1a7783bb9 13
mluis 0:91d1a7783bb9 14 Description: LoRa MAC layer implementation
mluis 0:91d1a7783bb9 15
mluis 0:91d1a7783bb9 16 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:91d1a7783bb9 17
ubhat 20:92764fda7371 18 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jäckle ( STACKFORCE )
mluis 0:91d1a7783bb9 19 */
mluis 0:91d1a7783bb9 20 #include "board.h"
mluis 0:91d1a7783bb9 21
mluis 0:91d1a7783bb9 22 #include "LoRaMacCrypto.h"
mluis 0:91d1a7783bb9 23 #include "LoRaMac.h"
mluis 2:14a5d6ad92d5 24 #include "LoRaMacTest.h"
mluis 0:91d1a7783bb9 25
mluis 0:91d1a7783bb9 26 /*!
mluis 0:91d1a7783bb9 27 * Maximum PHY layer payload size
mluis 0:91d1a7783bb9 28 */
mluis 1:91e4e6c60d1e 29 #define LORAMAC_PHY_MAXPAYLOAD 255
mluis 1:91e4e6c60d1e 30
mluis 1:91e4e6c60d1e 31 /*!
mluis 1:91e4e6c60d1e 32 * Maximum MAC commands buffer size
mluis 1:91e4e6c60d1e 33 */
mluis 1:91e4e6c60d1e 34 #define LORA_MAC_COMMAND_MAX_LENGTH 15
mluis 0:91d1a7783bb9 35
mluis 0:91d1a7783bb9 36 /*!
mluis 4:37c12dbc8dc7 37 * FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
mluis 4:37c12dbc8dc7 38 * in RxWindowSetup function.
mluis 4:37c12dbc8dc7 39 * Maximum PHYPayload = MaxPayloadOfDatarate/MaxPayloadOfDatarateRepeater + LORA_MAC_FRMPAYLOAD_OVERHEAD
mluis 4:37c12dbc8dc7 40 */
mluis 4:37c12dbc8dc7 41 #define LORA_MAC_FRMPAYLOAD_OVERHEAD 13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)
mluis 4:37c12dbc8dc7 42
mluis 4:37c12dbc8dc7 43 /*!
ubhat 20:92764fda7371 44 * Minimum number of available channels in Frequency Hopping Mode
ubhat 20:92764fda7371 45 */
ubhat 20:92764fda7371 46 #define FRQ_HOP_CHNLS_MIN 50
ubhat 20:92764fda7371 47
ubhat 20:92764fda7371 48 /*!
ubhat 20:92764fda7371 49 * Minimum number of available channels in Hybrid Mode
ubhat 20:92764fda7371 50 */
ubhat 20:92764fda7371 51 #define HYBRD_CHNLS_MIN 4
ubhat 20:92764fda7371 52
ubhat 20:92764fda7371 53
ubhat 20:92764fda7371 54 /*!
mluis 0:91d1a7783bb9 55 * Device IEEE EUI
mluis 0:91d1a7783bb9 56 */
mluis 0:91d1a7783bb9 57 static uint8_t *LoRaMacDevEui;
mluis 0:91d1a7783bb9 58
mluis 0:91d1a7783bb9 59 /*!
mluis 0:91d1a7783bb9 60 * Application IEEE EUI
mluis 0:91d1a7783bb9 61 */
mluis 0:91d1a7783bb9 62 static uint8_t *LoRaMacAppEui;
mluis 0:91d1a7783bb9 63
mluis 0:91d1a7783bb9 64 /*!
mluis 0:91d1a7783bb9 65 * AES encryption/decryption cipher application key
mluis 0:91d1a7783bb9 66 */
mluis 0:91d1a7783bb9 67 static uint8_t *LoRaMacAppKey;
mluis 0:91d1a7783bb9 68
mluis 0:91d1a7783bb9 69 /*!
mluis 0:91d1a7783bb9 70 * AES encryption/decryption cipher network session key
mluis 0:91d1a7783bb9 71 */
mluis 0:91d1a7783bb9 72 static uint8_t LoRaMacNwkSKey[] =
mluis 0:91d1a7783bb9 73 {
mluis 0:91d1a7783bb9 74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mluis 0:91d1a7783bb9 75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:91d1a7783bb9 76 };
mluis 0:91d1a7783bb9 77
mluis 0:91d1a7783bb9 78 /*!
mluis 0:91d1a7783bb9 79 * AES encryption/decryption cipher application session key
mluis 0:91d1a7783bb9 80 */
mluis 0:91d1a7783bb9 81 static uint8_t LoRaMacAppSKey[] =
mluis 0:91d1a7783bb9 82 {
mluis 0:91d1a7783bb9 83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mluis 0:91d1a7783bb9 84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:91d1a7783bb9 85 };
mluis 0:91d1a7783bb9 86
mluis 0:91d1a7783bb9 87 /*!
mluis 0:91d1a7783bb9 88 * Device nonce is a random value extracted by issuing a sequence of RSSI
mluis 0:91d1a7783bb9 89 * measurements
mluis 0:91d1a7783bb9 90 */
mluis 0:91d1a7783bb9 91 static uint16_t LoRaMacDevNonce;
mluis 0:91d1a7783bb9 92
mluis 0:91d1a7783bb9 93 /*!
mluis 0:91d1a7783bb9 94 * Network ID ( 3 bytes )
mluis 0:91d1a7783bb9 95 */
mluis 0:91d1a7783bb9 96 static uint32_t LoRaMacNetID;
mluis 0:91d1a7783bb9 97
mluis 0:91d1a7783bb9 98 /*!
mluis 0:91d1a7783bb9 99 * Mote Address
mluis 0:91d1a7783bb9 100 */
mluis 0:91d1a7783bb9 101 static uint32_t LoRaMacDevAddr;
mluis 0:91d1a7783bb9 102
mluis 0:91d1a7783bb9 103 /*!
mluis 2:14a5d6ad92d5 104 * Multicast channels linked list
mluis 0:91d1a7783bb9 105 */
mluis 0:91d1a7783bb9 106 static MulticastParams_t *MulticastChannels = NULL;
mluis 0:91d1a7783bb9 107
mluis 0:91d1a7783bb9 108 /*!
mluis 0:91d1a7783bb9 109 * Actual device class
mluis 0:91d1a7783bb9 110 */
mluis 0:91d1a7783bb9 111 static DeviceClass_t LoRaMacDeviceClass;
mluis 0:91d1a7783bb9 112
mluis 0:91d1a7783bb9 113 /*!
mluis 0:91d1a7783bb9 114 * Indicates if the node is connected to a private or public network
mluis 0:91d1a7783bb9 115 */
mluis 0:91d1a7783bb9 116 static bool PublicNetwork;
mluis 0:91d1a7783bb9 117
mluis 0:91d1a7783bb9 118 /*!
mluis 0:91d1a7783bb9 119 * Indicates if the node supports repeaters
mluis 0:91d1a7783bb9 120 */
mluis 0:91d1a7783bb9 121 static bool RepeaterSupport;
mluis 0:91d1a7783bb9 122
mluis 0:91d1a7783bb9 123 /*!
mluis 0:91d1a7783bb9 124 * Buffer containing the data to be sent or received.
mluis 0:91d1a7783bb9 125 */
mluis 0:91d1a7783bb9 126 static uint8_t LoRaMacBuffer[LORAMAC_PHY_MAXPAYLOAD];
mluis 0:91d1a7783bb9 127
mluis 0:91d1a7783bb9 128 /*!
mluis 0:91d1a7783bb9 129 * Length of packet in LoRaMacBuffer
mluis 0:91d1a7783bb9 130 */
mluis 0:91d1a7783bb9 131 static uint16_t LoRaMacBufferPktLen = 0;
mluis 0:91d1a7783bb9 132
mluis 0:91d1a7783bb9 133 /*!
mluis 0:91d1a7783bb9 134 * Buffer containing the upper layer data.
mluis 0:91d1a7783bb9 135 */
mluis 0:91d1a7783bb9 136 static uint8_t LoRaMacPayload[LORAMAC_PHY_MAXPAYLOAD];
mluis 0:91d1a7783bb9 137 static uint8_t LoRaMacRxPayload[LORAMAC_PHY_MAXPAYLOAD];
mluis 0:91d1a7783bb9 138
mluis 0:91d1a7783bb9 139 /*!
mluis 0:91d1a7783bb9 140 * LoRaMAC frame counter. Each time a packet is sent the counter is incremented.
mluis 0:91d1a7783bb9 141 * Only the 16 LSB bits are sent
mluis 0:91d1a7783bb9 142 */
mluis 0:91d1a7783bb9 143 static uint32_t UpLinkCounter = 1;
mluis 0:91d1a7783bb9 144
mluis 0:91d1a7783bb9 145 /*!
mluis 0:91d1a7783bb9 146 * LoRaMAC frame counter. Each time a packet is received the counter is incremented.
mluis 0:91d1a7783bb9 147 * Only the 16 LSB bits are received
mluis 0:91d1a7783bb9 148 */
mluis 0:91d1a7783bb9 149 static uint32_t DownLinkCounter = 0;
mluis 0:91d1a7783bb9 150
mluis 0:91d1a7783bb9 151 /*!
mluis 0:91d1a7783bb9 152 * IsPacketCounterFixed enables the MIC field tests by fixing the
mluis 0:91d1a7783bb9 153 * UpLinkCounter value
mluis 0:91d1a7783bb9 154 */
mluis 0:91d1a7783bb9 155 static bool IsUpLinkCounterFixed = false;
mluis 0:91d1a7783bb9 156
mluis 0:91d1a7783bb9 157 /*!
mluis 0:91d1a7783bb9 158 * Used for test purposes. Disables the opening of the reception windows.
mluis 0:91d1a7783bb9 159 */
mluis 0:91d1a7783bb9 160 static bool IsRxWindowsEnabled = true;
mluis 0:91d1a7783bb9 161
mluis 0:91d1a7783bb9 162 /*!
mluis 0:91d1a7783bb9 163 * Indicates if the MAC layer has already joined a network.
mluis 0:91d1a7783bb9 164 */
mluis 0:91d1a7783bb9 165 static bool IsLoRaMacNetworkJoined = false;
mluis 0:91d1a7783bb9 166
mluis 0:91d1a7783bb9 167 /*!
mluis 0:91d1a7783bb9 168 * LoRaMac ADR control status
mluis 0:91d1a7783bb9 169 */
snelson 11:507de2ecd2b3 170 static bool AdrCtrlOn = true;
mluis 0:91d1a7783bb9 171
mluis 0:91d1a7783bb9 172 /*!
mluis 0:91d1a7783bb9 173 * Counts the number of missed ADR acknowledgements
mluis 0:91d1a7783bb9 174 */
mluis 0:91d1a7783bb9 175 static uint32_t AdrAckCounter = 0;
mluis 0:91d1a7783bb9 176
mluis 0:91d1a7783bb9 177 /*!
mluis 0:91d1a7783bb9 178 * If the node has sent a FRAME_TYPE_DATA_CONFIRMED_UP this variable indicates
mluis 0:91d1a7783bb9 179 * if the nodes needs to manage the server acknowledgement.
mluis 0:91d1a7783bb9 180 */
mluis 0:91d1a7783bb9 181 static bool NodeAckRequested = false;
mluis 0:91d1a7783bb9 182
mluis 0:91d1a7783bb9 183 /*!
mluis 0:91d1a7783bb9 184 * If the server has sent a FRAME_TYPE_DATA_CONFIRMED_DOWN this variable indicates
mluis 0:91d1a7783bb9 185 * if the ACK bit must be set for the next transmission
mluis 0:91d1a7783bb9 186 */
mluis 0:91d1a7783bb9 187 static bool SrvAckRequested = false;
mluis 0:91d1a7783bb9 188
mluis 0:91d1a7783bb9 189 /*!
mluis 0:91d1a7783bb9 190 * Indicates if the MAC layer wants to send MAC commands
mluis 0:91d1a7783bb9 191 */
mluis 0:91d1a7783bb9 192 static bool MacCommandsInNextTx = false;
mluis 0:91d1a7783bb9 193
mluis 0:91d1a7783bb9 194 /*!
mluis 0:91d1a7783bb9 195 * Contains the current MacCommandsBuffer index
mluis 0:91d1a7783bb9 196 */
mluis 0:91d1a7783bb9 197 static uint8_t MacCommandsBufferIndex = 0;
mluis 0:91d1a7783bb9 198
mluis 0:91d1a7783bb9 199 /*!
Shaun Nelson 27:cda377b2a41f 200 * Contains the current MacCommandsBuffer index for MAC commands to repeat
Shaun Nelson 27:cda377b2a41f 201 */
Shaun Nelson 27:cda377b2a41f 202 static uint8_t MacCommandsBufferToRepeatIndex = 0;
Shaun Nelson 27:cda377b2a41f 203
Shaun Nelson 27:cda377b2a41f 204 /*!
mluis 0:91d1a7783bb9 205 * Buffer containing the MAC layer commands
mluis 0:91d1a7783bb9 206 */
mluis 1:91e4e6c60d1e 207 static uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH];
mluis 0:91d1a7783bb9 208
Shaun Nelson 27:cda377b2a41f 209 /*!
Shaun Nelson 27:cda377b2a41f 210 * Buffer containing the MAC layer commands which must be repeated
Shaun Nelson 27:cda377b2a41f 211 */
Shaun Nelson 27:cda377b2a41f 212 static uint8_t MacCommandsBufferToRepeat[LORA_MAC_COMMAND_MAX_LENGTH];
Shaun Nelson 27:cda377b2a41f 213
mluis 0:91d1a7783bb9 214 #if defined( USE_BAND_433 )
mluis 0:91d1a7783bb9 215 /*!
mluis 0:91d1a7783bb9 216 * Data rates table definition
mluis 0:91d1a7783bb9 217 */
mluis 0:91d1a7783bb9 218 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 219
mluis 0:91d1a7783bb9 220 /*!
mluis 0:91d1a7783bb9 221 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 222 */
mluis 0:91d1a7783bb9 223 const uint8_t MaxPayloadOfDatarate[] = { 59, 59, 59, 123, 250, 250, 250, 250 };
mluis 0:91d1a7783bb9 224
mluis 0:91d1a7783bb9 225 /*!
mluis 0:91d1a7783bb9 226 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 227 */
mluis 0:91d1a7783bb9 228 const uint8_t MaxPayloadOfDatarateRepeater[] = { 59, 59, 59, 123, 230, 230, 230, 230 };
mluis 0:91d1a7783bb9 229
mluis 0:91d1a7783bb9 230 /*!
mluis 0:91d1a7783bb9 231 * Tx output powers table definition
mluis 0:91d1a7783bb9 232 */
mluis 0:91d1a7783bb9 233 const int8_t TxPowers[] = { 20, 14, 11, 8, 5, 2 };
mluis 0:91d1a7783bb9 234
mluis 0:91d1a7783bb9 235 /*!
mluis 0:91d1a7783bb9 236 * LoRaMac bands
mluis 0:91d1a7783bb9 237 */
mluis 0:91d1a7783bb9 238 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 239 {
mluis 0:91d1a7783bb9 240 BAND0,
mluis 0:91d1a7783bb9 241 };
mluis 0:91d1a7783bb9 242
mluis 0:91d1a7783bb9 243 /*!
mluis 0:91d1a7783bb9 244 * LoRaMAC channels
mluis 0:91d1a7783bb9 245 */
mluis 0:91d1a7783bb9 246 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 247 {
mluis 0:91d1a7783bb9 248 LC1,
mluis 0:91d1a7783bb9 249 LC2,
mluis 0:91d1a7783bb9 250 LC3,
mluis 0:91d1a7783bb9 251 };
mluis 0:91d1a7783bb9 252 #elif defined( USE_BAND_780 )
mluis 0:91d1a7783bb9 253 /*!
mluis 0:91d1a7783bb9 254 * Data rates table definition
mluis 0:91d1a7783bb9 255 */
mluis 0:91d1a7783bb9 256 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 257
mluis 0:91d1a7783bb9 258 /*!
mluis 0:91d1a7783bb9 259 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 260 */
mluis 0:91d1a7783bb9 261 const uint8_t MaxPayloadOfDatarate[] = { 59, 59, 59, 123, 250, 250, 250, 250 };
mluis 0:91d1a7783bb9 262
mluis 0:91d1a7783bb9 263 /*!
mluis 0:91d1a7783bb9 264 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 265 */
mluis 0:91d1a7783bb9 266 const uint8_t MaxPayloadOfDatarateRepeater[] = { 59, 59, 59, 123, 230, 230, 230, 230 };
mluis 0:91d1a7783bb9 267
mluis 0:91d1a7783bb9 268 /*!
mluis 0:91d1a7783bb9 269 * Tx output powers table definition
mluis 0:91d1a7783bb9 270 */
mluis 0:91d1a7783bb9 271 const int8_t TxPowers[] = { 20, 14, 11, 8, 5, 2 };
mluis 0:91d1a7783bb9 272
mluis 0:91d1a7783bb9 273 /*!
mluis 0:91d1a7783bb9 274 * LoRaMac bands
mluis 0:91d1a7783bb9 275 */
mluis 0:91d1a7783bb9 276 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 277 {
mluis 0:91d1a7783bb9 278 BAND0,
mluis 0:91d1a7783bb9 279 };
mluis 0:91d1a7783bb9 280
mluis 0:91d1a7783bb9 281 /*!
mluis 0:91d1a7783bb9 282 * LoRaMAC channels
mluis 0:91d1a7783bb9 283 */
mluis 0:91d1a7783bb9 284 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 285 {
mluis 0:91d1a7783bb9 286 LC1,
mluis 0:91d1a7783bb9 287 LC2,
mluis 0:91d1a7783bb9 288 LC3,
mluis 0:91d1a7783bb9 289 };
mluis 0:91d1a7783bb9 290 #elif defined( USE_BAND_868 )
mluis 0:91d1a7783bb9 291 /*!
mluis 0:91d1a7783bb9 292 * Data rates table definition
mluis 0:91d1a7783bb9 293 */
mluis 0:91d1a7783bb9 294 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 295
mluis 0:91d1a7783bb9 296 /*!
mluis 0:91d1a7783bb9 297 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 298 */
mluis 0:91d1a7783bb9 299 const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
mluis 0:91d1a7783bb9 300
mluis 0:91d1a7783bb9 301 /*!
mluis 0:91d1a7783bb9 302 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 303 */
mluis 0:91d1a7783bb9 304 const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
mluis 0:91d1a7783bb9 305
mluis 0:91d1a7783bb9 306 /*!
mluis 0:91d1a7783bb9 307 * Tx output powers table definition
mluis 0:91d1a7783bb9 308 */
mluis 0:91d1a7783bb9 309 const int8_t TxPowers[] = { 20, 14, 11, 8, 5, 2 };
mluis 0:91d1a7783bb9 310
mluis 0:91d1a7783bb9 311 /*!
mluis 0:91d1a7783bb9 312 * LoRaMac bands
mluis 0:91d1a7783bb9 313 */
mluis 0:91d1a7783bb9 314 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 315 {
mluis 0:91d1a7783bb9 316 BAND0,
mluis 0:91d1a7783bb9 317 BAND1,
mluis 0:91d1a7783bb9 318 BAND2,
mluis 0:91d1a7783bb9 319 BAND3,
mluis 0:91d1a7783bb9 320 BAND4,
mluis 0:91d1a7783bb9 321 };
mluis 0:91d1a7783bb9 322
mluis 0:91d1a7783bb9 323 /*!
mluis 0:91d1a7783bb9 324 * LoRaMAC channels
mluis 0:91d1a7783bb9 325 */
mluis 0:91d1a7783bb9 326 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 327 {
mluis 0:91d1a7783bb9 328 LC1,
mluis 0:91d1a7783bb9 329 LC2,
mluis 0:91d1a7783bb9 330 LC3,
mluis 0:91d1a7783bb9 331 };
mluis 0:91d1a7783bb9 332 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 0:91d1a7783bb9 333 /*!
mluis 0:91d1a7783bb9 334 * Data rates table definition
mluis 0:91d1a7783bb9 335 */
mluis 0:91d1a7783bb9 336 const uint8_t Datarates[] = { 10, 9, 8, 7, 8, 0, 0, 0, 12, 11, 10, 9, 8, 7, 0, 0 };
mluis 0:91d1a7783bb9 337
mluis 0:91d1a7783bb9 338 /*!
mluis 0:91d1a7783bb9 339 * Up/Down link data rates offset definition
mluis 0:91d1a7783bb9 340 */
snelson 8:4816c8449bf2 341 const uint8_t datarateOffsets[16][4] =
mluis 0:91d1a7783bb9 342 {
mluis 0:91d1a7783bb9 343 { DR_10, DR_9 , DR_8 , DR_8 }, // DR_0
mluis 0:91d1a7783bb9 344 { DR_11, DR_10, DR_9 , DR_8 }, // DR_1
mluis 0:91d1a7783bb9 345 { DR_12, DR_11, DR_10, DR_9 }, // DR_2
mluis 0:91d1a7783bb9 346 { DR_13, DR_12, DR_11, DR_10 }, // DR_3
mluis 0:91d1a7783bb9 347 { DR_13, DR_13, DR_12, DR_11 }, // DR_4
mluis 0:91d1a7783bb9 348 { 0xFF , 0xFF , 0xFF , 0xFF },
mluis 0:91d1a7783bb9 349 { 0xFF , 0xFF , 0xFF , 0xFF },
mluis 0:91d1a7783bb9 350 { 0xFF , 0xFF , 0xFF , 0xFF },
mluis 0:91d1a7783bb9 351 { DR_8 , DR_8 , DR_8 , DR_8 },
mluis 0:91d1a7783bb9 352 { DR_9 , DR_8 , DR_8 , DR_8 },
mluis 0:91d1a7783bb9 353 { DR_10, DR_9 , DR_8 , DR_8 },
mluis 0:91d1a7783bb9 354 { DR_11, DR_10, DR_9 , DR_8 },
mluis 0:91d1a7783bb9 355 { DR_12, DR_11, DR_10, DR_9 },
mluis 0:91d1a7783bb9 356 { DR_13, DR_12, DR_11, DR_10 },
mluis 0:91d1a7783bb9 357 { 0xFF , 0xFF , 0xFF , 0xFF },
mluis 0:91d1a7783bb9 358 { 0xFF , 0xFF , 0xFF , 0xFF },
mluis 0:91d1a7783bb9 359 };
mluis 0:91d1a7783bb9 360
mluis 0:91d1a7783bb9 361 /*!
mluis 0:91d1a7783bb9 362 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 363 */
shaunkrnelson 25:8d0a79bd607b 364 const uint8_t MaxPayloadOfDatarate[] = { 11, 53, 126, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
mluis 0:91d1a7783bb9 365
mluis 0:91d1a7783bb9 366 /*!
mluis 0:91d1a7783bb9 367 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 368 */
shaunkrnelson 25:8d0a79bd607b 369 const uint8_t MaxPayloadOfDatarateRepeater[] = { 11, 53, 126, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0 };
mluis 0:91d1a7783bb9 370
mluis 0:91d1a7783bb9 371 /*!
mluis 0:91d1a7783bb9 372 * Tx output powers table definition
mluis 0:91d1a7783bb9 373 */
mluis 0:91d1a7783bb9 374 const int8_t TxPowers[] = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
mluis 0:91d1a7783bb9 375
mluis 0:91d1a7783bb9 376 /*!
mluis 0:91d1a7783bb9 377 * LoRaMac bands
mluis 0:91d1a7783bb9 378 */
mluis 0:91d1a7783bb9 379 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 380 {
mluis 0:91d1a7783bb9 381 BAND0,
mluis 0:91d1a7783bb9 382 };
mluis 0:91d1a7783bb9 383
mluis 0:91d1a7783bb9 384 /*!
mluis 0:91d1a7783bb9 385 * LoRaMAC channels
mluis 0:91d1a7783bb9 386 */
mluis 0:91d1a7783bb9 387 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
mluis 0:91d1a7783bb9 388
mluis 2:14a5d6ad92d5 389 /*!
mluis 2:14a5d6ad92d5 390 * Contains the channels which remain to be applied.
mluis 2:14a5d6ad92d5 391 */
mluis 2:14a5d6ad92d5 392 static uint16_t ChannelsMaskRemaining[6];
snelson 8:4816c8449bf2 393
snelson 8:4816c8449bf2 394 #if defined( USE_BAND_915 )
snelson 8:4816c8449bf2 395 /*!
snelson 8:4816c8449bf2 396 * Last join request sub-band
snelson 8:4816c8449bf2 397 */
snelson 8:4816c8449bf2 398 static int8_t LastJoinBlock;
snelson 8:4816c8449bf2 399
snelson 8:4816c8449bf2 400 /*!
snelson 8:4816c8449bf2 401 * Next join sub-band block
snelson 8:4816c8449bf2 402 */
snelson 8:4816c8449bf2 403 static uint8_t NextJoinBlock;
snelson 8:4816c8449bf2 404
snelson 8:4816c8449bf2 405 /*!
snelson 8:4816c8449bf2 406 * Mask of 125 KHz sub-bands not used for join transmit
snelson 8:4816c8449bf2 407 */
snelson 8:4816c8449bf2 408 static uint8_t JoinBlocksRemaining;
snelson 8:4816c8449bf2 409
snelson 8:4816c8449bf2 410 /*!
snelson 8:4816c8449bf2 411 * Mask of 500 KHz sub-bands not used for join transmit
snelson 8:4816c8449bf2 412 */
snelson 8:4816c8449bf2 413 static uint8_t Join500KHzRemaining;
snelson 8:4816c8449bf2 414
snelson 8:4816c8449bf2 415 #ifndef JOIN_BLOCK_ORDER
snelson 8:4816c8449bf2 416 #define JOIN_BLOCK_ORDER { -1, -1, -1, -1, -1, -1, -1, -1 }
snelson 8:4816c8449bf2 417 #endif
snelson 8:4816c8449bf2 418
snelson 8:4816c8449bf2 419 /*!
snelson 8:4816c8449bf2 420 * join sub-band block order
snelson 8:4816c8449bf2 421 */
snelson 8:4816c8449bf2 422 static int8_t JoinBlock[8] = JOIN_BLOCK_ORDER;
snelson 8:4816c8449bf2 423
snelson 8:4816c8449bf2 424 #endif
snelson 8:4816c8449bf2 425
shaunkrnelson 16:1e01c8728daa 426 /*!
shaunkrnelson 16:1e01c8728daa 427 * Defines the first channel for RX window 2 for US band
shaunkrnelson 16:1e01c8728daa 428 */
shaunkrnelson 16:1e01c8728daa 429 #define LORAMAC_FIRST_RX2_CHANNEL ( (uint32_t) 923.3e6 )
shaunkrnelson 16:1e01c8728daa 430
shaunkrnelson 16:1e01c8728daa 431 /*!
shaunkrnelson 16:1e01c8728daa 432 * Defines the last channel for RX window 2 for US band
shaunkrnelson 16:1e01c8728daa 433 */
shaunkrnelson 16:1e01c8728daa 434 #define LORAMAC_LAST_RX2_CHANNEL ( (uint32_t) 927.5e6 )
shaunkrnelson 16:1e01c8728daa 435
shaunkrnelson 16:1e01c8728daa 436 /*!
shaunkrnelson 16:1e01c8728daa 437 * Defines the step width of the channels for RX window 2
shaunkrnelson 16:1e01c8728daa 438 */
shaunkrnelson 16:1e01c8728daa 439 #define LORAMAC_STEPWIDTH_RX2_CHANNEL ( (uint32_t) 600e3 )
shaunkrnelson 16:1e01c8728daa 440
shaunkrnelson 16:1e01c8728daa 441
mluis 0:91d1a7783bb9 442 #else
mluis 0:91d1a7783bb9 443 #error "Please define a frequency band in the compiler options."
mluis 0:91d1a7783bb9 444 #endif
mluis 0:91d1a7783bb9 445
mluis 0:91d1a7783bb9 446 /*!
mluis 0:91d1a7783bb9 447 * LoRaMAC 2nd reception window settings
mluis 0:91d1a7783bb9 448 */
mluis 0:91d1a7783bb9 449 static Rx2ChannelParams_t Rx2Channel = RX_WND_2_CHANNEL;
mluis 0:91d1a7783bb9 450
mluis 0:91d1a7783bb9 451 /*!
mluis 0:91d1a7783bb9 452 * Datarate offset between uplink and downlink on first window
mluis 0:91d1a7783bb9 453 */
mluis 0:91d1a7783bb9 454 static uint8_t Rx1DrOffset = 0;
mluis 0:91d1a7783bb9 455
mluis 0:91d1a7783bb9 456 /*!
mluis 0:91d1a7783bb9 457 * Mask indicating which channels are enabled
mluis 0:91d1a7783bb9 458 */
mluis 0:91d1a7783bb9 459 static uint16_t ChannelsMask[6];
mluis 0:91d1a7783bb9 460
mluis 0:91d1a7783bb9 461 /*!
mluis 0:91d1a7783bb9 462 * Channels Tx output power
mluis 0:91d1a7783bb9 463 */
mluis 0:91d1a7783bb9 464 static int8_t ChannelsTxPower = LORAMAC_DEFAULT_TX_POWER;
mluis 0:91d1a7783bb9 465
mluis 0:91d1a7783bb9 466 /*!
mluis 0:91d1a7783bb9 467 * Channels datarate
mluis 0:91d1a7783bb9 468 */
mluis 0:91d1a7783bb9 469 static int8_t ChannelsDatarate = LORAMAC_DEFAULT_DATARATE;
mluis 0:91d1a7783bb9 470
mluis 0:91d1a7783bb9 471 /*!
mluis 0:91d1a7783bb9 472 * Channels default datarate
mluis 0:91d1a7783bb9 473 */
mluis 0:91d1a7783bb9 474 static int8_t ChannelsDefaultDatarate = LORAMAC_DEFAULT_DATARATE;
mluis 0:91d1a7783bb9 475
mluis 0:91d1a7783bb9 476 /*!
mluis 0:91d1a7783bb9 477 * Number of uplink messages repetitions [1:15] (unconfirmed messages only)
mluis 0:91d1a7783bb9 478 */
mluis 0:91d1a7783bb9 479 static uint8_t ChannelsNbRep = 1;
mluis 0:91d1a7783bb9 480
mluis 0:91d1a7783bb9 481 /*!
mluis 0:91d1a7783bb9 482 * Uplink messages repetitions counter
mluis 0:91d1a7783bb9 483 */
mluis 0:91d1a7783bb9 484 static uint8_t ChannelsNbRepCounter = 0;
mluis 0:91d1a7783bb9 485
mluis 0:91d1a7783bb9 486 /*!
mluis 0:91d1a7783bb9 487 * Maximum duty cycle
mluis 0:91d1a7783bb9 488 * \remark Possibility to shutdown the device.
mluis 0:91d1a7783bb9 489 */
mluis 0:91d1a7783bb9 490 static uint8_t MaxDCycle = 0;
mluis 0:91d1a7783bb9 491
mluis 0:91d1a7783bb9 492 /*!
mluis 2:14a5d6ad92d5 493 * Aggregated duty cycle management
mluis 0:91d1a7783bb9 494 */
mluis 0:91d1a7783bb9 495 static uint16_t AggregatedDCycle;
mluis 0:91d1a7783bb9 496 static TimerTime_t AggregatedLastTxDoneTime;
mluis 0:91d1a7783bb9 497 static TimerTime_t AggregatedTimeOff;
mluis 0:91d1a7783bb9 498
mluis 0:91d1a7783bb9 499 /*!
mluis 0:91d1a7783bb9 500 * Enables/Disables duty cycle management (Test only)
mluis 0:91d1a7783bb9 501 */
mluis 0:91d1a7783bb9 502 static bool DutyCycleOn;
mluis 0:91d1a7783bb9 503
mluis 0:91d1a7783bb9 504 /*!
mluis 0:91d1a7783bb9 505 * Current channel index
mluis 0:91d1a7783bb9 506 */
mluis 0:91d1a7783bb9 507 static uint8_t Channel;
mluis 0:91d1a7783bb9 508
mluis 4:37c12dbc8dc7 509 static uint8_t LastTxChannel;
mluis 4:37c12dbc8dc7 510
mluis 0:91d1a7783bb9 511 /*!
mluis 0:91d1a7783bb9 512 * LoRaMac internal states
mluis 0:91d1a7783bb9 513 */
mluis 2:14a5d6ad92d5 514 enum eLoRaMacState
mluis 0:91d1a7783bb9 515 {
mluis 0:91d1a7783bb9 516 MAC_IDLE = 0x00000000,
mluis 0:91d1a7783bb9 517 MAC_TX_RUNNING = 0x00000001,
mluis 0:91d1a7783bb9 518 MAC_RX = 0x00000002,
mluis 0:91d1a7783bb9 519 MAC_ACK_REQ = 0x00000004,
mluis 0:91d1a7783bb9 520 MAC_ACK_RETRY = 0x00000008,
mluis 2:14a5d6ad92d5 521 MAC_TX_DELAYED = 0x00000010,
mluis 3:b9d87593a8ae 522 MAC_TX_CONFIG = 0x00000020,
mluis 4:37c12dbc8dc7 523 MAC_RX_ABORT = 0x00000040,
mluis 0:91d1a7783bb9 524 };
mluis 0:91d1a7783bb9 525
mluis 0:91d1a7783bb9 526 /*!
mluis 0:91d1a7783bb9 527 * LoRaMac internal state
mluis 0:91d1a7783bb9 528 */
mluis 0:91d1a7783bb9 529 uint32_t LoRaMacState = MAC_IDLE;
mluis 0:91d1a7783bb9 530
mluis 0:91d1a7783bb9 531 /*!
mluis 0:91d1a7783bb9 532 * LoRaMac timer used to check the LoRaMacState (runs every second)
mluis 0:91d1a7783bb9 533 */
mluis 0:91d1a7783bb9 534 static TimerEvent_t MacStateCheckTimer;
mluis 0:91d1a7783bb9 535
mluis 0:91d1a7783bb9 536 /*!
mluis 0:91d1a7783bb9 537 * LoRaMac upper layer event functions
mluis 0:91d1a7783bb9 538 */
mluis 2:14a5d6ad92d5 539 static LoRaMacPrimitives_t *LoRaMacPrimitives;
mluis 0:91d1a7783bb9 540
mluis 0:91d1a7783bb9 541 /*!
mluis 2:14a5d6ad92d5 542 * LoRaMac upper layer callback functions
mluis 0:91d1a7783bb9 543 */
mluis 2:14a5d6ad92d5 544 static LoRaMacCallback_t *LoRaMacCallbacks;
mluis 0:91d1a7783bb9 545
mluis 0:91d1a7783bb9 546 /*!
mluis 2:14a5d6ad92d5 547 * Radio events function pointer
mluis 0:91d1a7783bb9 548 */
mluis 2:14a5d6ad92d5 549 static RadioEvents_t RadioEvents;
mluis 0:91d1a7783bb9 550
mluis 0:91d1a7783bb9 551 /*!
mluis 0:91d1a7783bb9 552 * LoRaMac duty cycle delayed Tx timer
mluis 0:91d1a7783bb9 553 */
mluis 0:91d1a7783bb9 554 static TimerEvent_t TxDelayedTimer;
mluis 0:91d1a7783bb9 555
mluis 0:91d1a7783bb9 556 /*!
mluis 0:91d1a7783bb9 557 * LoRaMac reception windows timers
mluis 0:91d1a7783bb9 558 */
mluis 0:91d1a7783bb9 559 static TimerEvent_t RxWindowTimer1;
mluis 0:91d1a7783bb9 560 static TimerEvent_t RxWindowTimer2;
mluis 0:91d1a7783bb9 561
mluis 0:91d1a7783bb9 562 /*!
mluis 0:91d1a7783bb9 563 * LoRaMac reception windows delay from end of Tx
mluis 0:91d1a7783bb9 564 */
mluis 0:91d1a7783bb9 565 static uint32_t ReceiveDelay1;
mluis 0:91d1a7783bb9 566 static uint32_t ReceiveDelay2;
mluis 0:91d1a7783bb9 567 static uint32_t JoinAcceptDelay1;
mluis 0:91d1a7783bb9 568 static uint32_t JoinAcceptDelay2;
mluis 0:91d1a7783bb9 569
mluis 0:91d1a7783bb9 570 /*!
mluis 0:91d1a7783bb9 571 * LoRaMac reception windows delay
mluis 0:91d1a7783bb9 572 * \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
mluis 0:91d1a7783bb9 573 * join frame : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
mluis 0:91d1a7783bb9 574 */
mluis 0:91d1a7783bb9 575 static uint32_t RxWindow1Delay;
mluis 0:91d1a7783bb9 576 static uint32_t RxWindow2Delay;
mluis 0:91d1a7783bb9 577
mluis 0:91d1a7783bb9 578 /*!
mluis 0:91d1a7783bb9 579 * LoRaMac maximum time a reception window stays open
mluis 0:91d1a7783bb9 580 */
mluis 0:91d1a7783bb9 581 static uint32_t MaxRxWindow;
mluis 0:91d1a7783bb9 582
mluis 0:91d1a7783bb9 583 /*!
mluis 0:91d1a7783bb9 584 * Acknowledge timeout timer. Used for packet retransmissions.
mluis 0:91d1a7783bb9 585 */
mluis 0:91d1a7783bb9 586 static TimerEvent_t AckTimeoutTimer;
mluis 0:91d1a7783bb9 587
mluis 0:91d1a7783bb9 588 /*!
mluis 0:91d1a7783bb9 589 * Number of trials to get a frame acknowledged
mluis 0:91d1a7783bb9 590 */
mluis 0:91d1a7783bb9 591 static uint8_t AckTimeoutRetries = 1;
mluis 0:91d1a7783bb9 592
mluis 0:91d1a7783bb9 593 /*!
mluis 0:91d1a7783bb9 594 * Number of trials to get a frame acknowledged
mluis 0:91d1a7783bb9 595 */
mluis 0:91d1a7783bb9 596 static uint8_t AckTimeoutRetriesCounter = 1;
mluis 0:91d1a7783bb9 597
mluis 0:91d1a7783bb9 598 /*!
mluis 0:91d1a7783bb9 599 * Indicates if the AckTimeout timer has expired or not
mluis 0:91d1a7783bb9 600 */
mluis 0:91d1a7783bb9 601 static bool AckTimeoutRetry = false;
mluis 0:91d1a7783bb9 602
mluis 0:91d1a7783bb9 603 /*!
mluis 0:91d1a7783bb9 604 * Last transmission time on air
mluis 0:91d1a7783bb9 605 */
mluis 0:91d1a7783bb9 606 TimerTime_t TxTimeOnAir = 0;
mluis 0:91d1a7783bb9 607
mluis 0:91d1a7783bb9 608 /*!
mluis 2:14a5d6ad92d5 609 * Structure to hold an MCPS indication data.
mluis 2:14a5d6ad92d5 610 */
mluis 2:14a5d6ad92d5 611 static McpsIndication_t McpsIndication;
mluis 2:14a5d6ad92d5 612
mluis 2:14a5d6ad92d5 613 /*!
mluis 2:14a5d6ad92d5 614 * Structure to hold MCPS confirm data.
mluis 2:14a5d6ad92d5 615 */
mluis 2:14a5d6ad92d5 616 static McpsConfirm_t McpsConfirm;
mluis 2:14a5d6ad92d5 617
mluis 2:14a5d6ad92d5 618 /*!
mluis 2:14a5d6ad92d5 619 * Structure to hold MLME confirm data.
mluis 2:14a5d6ad92d5 620 */
mluis 2:14a5d6ad92d5 621 static MlmeConfirm_t MlmeConfirm;
mluis 2:14a5d6ad92d5 622
mluis 2:14a5d6ad92d5 623 /*!
mluis 2:14a5d6ad92d5 624 * Holds the current rx window slot
mluis 2:14a5d6ad92d5 625 */
mluis 2:14a5d6ad92d5 626 static uint8_t RxSlot = 0;
mluis 2:14a5d6ad92d5 627
mluis 2:14a5d6ad92d5 628 /*!
mluis 2:14a5d6ad92d5 629 * LoRaMac tx/rx operation state
mluis 2:14a5d6ad92d5 630 */
mluis 2:14a5d6ad92d5 631 LoRaMacFlags_t LoRaMacFlags;
mluis 2:14a5d6ad92d5 632
snelson 9:ad93de20d720 633 /*!
snelson 9:ad93de20d720 634 * Join Retransmission back-off configuration
snelson 8:4816c8449bf2 635 */
snelson 8:4816c8449bf2 636 LoRaMacRetransmissionDCycle_t JoinReTransmitDCycle[JOIN_NB_RETRANSMISSION_DCYCLES] =
snelson 8:4816c8449bf2 637 {
snelson 8:4816c8449bf2 638 JOIN_RETRANSMISSION_DCYCLE1,
snelson 8:4816c8449bf2 639 JOIN_RETRANSMISSION_DCYCLE2,
snelson 8:4816c8449bf2 640 JOIN_RETRANSMISSION_DCYCLE3
snelson 8:4816c8449bf2 641 };
snelson 8:4816c8449bf2 642
snelson 8:4816c8449bf2 643 /*!
snelson 8:4816c8449bf2 644 * Uptime of the last sent join request
snelson 8:4816c8449bf2 645 */
snelson 8:4816c8449bf2 646 static TimerTime_t LastJoinTxTime;
snelson 8:4816c8449bf2 647
snelson 8:4816c8449bf2 648 /*!
snelson 8:4816c8449bf2 649 * Aggregated join request time on air
snelson 8:4816c8449bf2 650 */
snelson 8:4816c8449bf2 651 static TimerTime_t JoinAggTimeOnAir;
snelson 8:4816c8449bf2 652
snelson 8:4816c8449bf2 653
mluis 2:14a5d6ad92d5 654 /*!
mluis 2:14a5d6ad92d5 655 * \brief Function to be executed on Radio Tx Done event
mluis 0:91d1a7783bb9 656 */
mluis 0:91d1a7783bb9 657 static void OnRadioTxDone( void );
mluis 0:91d1a7783bb9 658
mluis 0:91d1a7783bb9 659 /*!
mluis 3:b9d87593a8ae 660 * \brief This function prepares the MAC to abort the execution of function
mluis 3:b9d87593a8ae 661 * OnRadioRxDone in case of a reception error.
mluis 3:b9d87593a8ae 662 */
mluis 3:b9d87593a8ae 663 static void PrepareRxDoneAbort( void );
mluis 3:b9d87593a8ae 664
mluis 3:b9d87593a8ae 665 /*!
mluis 2:14a5d6ad92d5 666 * \brief Function to be executed on Radio Rx Done event
mluis 0:91d1a7783bb9 667 */
mluis 0:91d1a7783bb9 668 static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
mluis 0:91d1a7783bb9 669
mluis 0:91d1a7783bb9 670 /*!
mluis 2:14a5d6ad92d5 671 * \brief Function executed on Radio Tx Timeout event
mluis 0:91d1a7783bb9 672 */
mluis 0:91d1a7783bb9 673 static void OnRadioTxTimeout( void );
mluis 0:91d1a7783bb9 674
mluis 0:91d1a7783bb9 675 /*!
mluis 2:14a5d6ad92d5 676 * \brief Function executed on Radio Rx error event
mluis 0:91d1a7783bb9 677 */
mluis 0:91d1a7783bb9 678 static void OnRadioRxError( void );
mluis 0:91d1a7783bb9 679
mluis 0:91d1a7783bb9 680 /*!
mluis 2:14a5d6ad92d5 681 * \brief Function executed on Radio Rx Timeout event
mluis 0:91d1a7783bb9 682 */
mluis 0:91d1a7783bb9 683 static void OnRadioRxTimeout( void );
mluis 0:91d1a7783bb9 684
mluis 0:91d1a7783bb9 685 /*!
mluis 2:14a5d6ad92d5 686 * \brief Function executed on Resend Frame timer event.
mluis 0:91d1a7783bb9 687 */
mluis 0:91d1a7783bb9 688 static void OnMacStateCheckTimerEvent( void );
mluis 0:91d1a7783bb9 689
mluis 0:91d1a7783bb9 690 /*!
mluis 2:14a5d6ad92d5 691 * \brief Function executed on duty cycle delayed Tx timer event
mluis 0:91d1a7783bb9 692 */
mluis 0:91d1a7783bb9 693 static void OnTxDelayedTimerEvent( void );
mluis 0:91d1a7783bb9 694
mluis 0:91d1a7783bb9 695 /*!
mluis 2:14a5d6ad92d5 696 * \brief Function executed on first Rx window timer event
mluis 0:91d1a7783bb9 697 */
mluis 0:91d1a7783bb9 698 static void OnRxWindow1TimerEvent( void );
mluis 0:91d1a7783bb9 699
mluis 0:91d1a7783bb9 700 /*!
mluis 2:14a5d6ad92d5 701 * \brief Function executed on second Rx window timer event
mluis 0:91d1a7783bb9 702 */
mluis 0:91d1a7783bb9 703 static void OnRxWindow2TimerEvent( void );
mluis 0:91d1a7783bb9 704
mluis 0:91d1a7783bb9 705 /*!
mluis 2:14a5d6ad92d5 706 * \brief Function executed on AckTimeout timer event
mluis 0:91d1a7783bb9 707 */
mluis 0:91d1a7783bb9 708 static void OnAckTimeoutTimerEvent( void );
mluis 0:91d1a7783bb9 709
mluis 0:91d1a7783bb9 710 /*!
mluis 2:14a5d6ad92d5 711 * \brief Searches and set the next random available channel
mluis 2:14a5d6ad92d5 712 *
mluis 3:b9d87593a8ae 713 * \param [OUT] Time to wait for the next transmission according to the duty
mluis 3:b9d87593a8ae 714 * cycle.
mluis 3:b9d87593a8ae 715 *
mluis 3:b9d87593a8ae 716 * \retval status Function status [1: OK, 0: Unable to find a channel on the
mluis 3:b9d87593a8ae 717 * current datarate]
mluis 2:14a5d6ad92d5 718 */
mluis 3:b9d87593a8ae 719 static bool SetNextChannel( TimerTime_t* time );
mluis 2:14a5d6ad92d5 720
mluis 2:14a5d6ad92d5 721 /*!
mluis 2:14a5d6ad92d5 722 * \brief Sets the network to public or private. Updates the sync byte.
mluis 2:14a5d6ad92d5 723 *
mluis 2:14a5d6ad92d5 724 * \param [IN] enable if true, it enables a public network
mluis 2:14a5d6ad92d5 725 */
mluis 2:14a5d6ad92d5 726 static void SetPublicNetwork( bool enable );
mluis 2:14a5d6ad92d5 727
mluis 2:14a5d6ad92d5 728 /*!
mluis 2:14a5d6ad92d5 729 * \brief Initializes and opens the reception window
mluis 2:14a5d6ad92d5 730 *
mluis 2:14a5d6ad92d5 731 * \param [IN] freq window channel frequency
mluis 2:14a5d6ad92d5 732 * \param [IN] datarate window channel datarate
mluis 2:14a5d6ad92d5 733 * \param [IN] bandwidth window channel bandwidth
mluis 2:14a5d6ad92d5 734 * \param [IN] timeout window channel timeout
mluis 0:91d1a7783bb9 735 */
mluis 2:14a5d6ad92d5 736 static void RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous );
mluis 2:14a5d6ad92d5 737
mluis 2:14a5d6ad92d5 738 /*!
shaunkrnelson 16:1e01c8728daa 739 * \brief Verifies if the RX window 2 frequency is in range
shaunkrnelson 16:1e01c8728daa 740 *
shaunkrnelson 16:1e01c8728daa 741 * \param [IN] freq window channel frequency
shaunkrnelson 16:1e01c8728daa 742 *
shaunkrnelson 16:1e01c8728daa 743 * \retval status Function status [1: OK, 0: Frequency not applicable]
shaunkrnelson 16:1e01c8728daa 744 */
shaunkrnelson 16:1e01c8728daa 745 static bool Rx2FreqInRange( uint32_t freq );
shaunkrnelson 16:1e01c8728daa 746
shaunkrnelson 16:1e01c8728daa 747
shaunkrnelson 16:1e01c8728daa 748 /*!
mluis 2:14a5d6ad92d5 749 * \brief Adds a new MAC command to be sent.
mluis 2:14a5d6ad92d5 750 *
mluis 2:14a5d6ad92d5 751 * \Remark MAC layer internal function
mluis 2:14a5d6ad92d5 752 *
mluis 2:14a5d6ad92d5 753 * \param [in] cmd MAC command to be added
mluis 2:14a5d6ad92d5 754 * [MOTE_MAC_LINK_CHECK_REQ,
mluis 2:14a5d6ad92d5 755 * MOTE_MAC_LINK_ADR_ANS,
mluis 2:14a5d6ad92d5 756 * MOTE_MAC_DUTY_CYCLE_ANS,
mluis 2:14a5d6ad92d5 757 * MOTE_MAC_RX2_PARAM_SET_ANS,
mluis 2:14a5d6ad92d5 758 * MOTE_MAC_DEV_STATUS_ANS
mluis 2:14a5d6ad92d5 759 * MOTE_MAC_NEW_CHANNEL_ANS]
mluis 2:14a5d6ad92d5 760 * \param [in] p1 1st parameter ( optional depends on the command )
mluis 2:14a5d6ad92d5 761 * \param [in] p2 2nd parameter ( optional depends on the command )
mluis 2:14a5d6ad92d5 762 *
mluis 2:14a5d6ad92d5 763 * \retval status Function status [0: OK, 1: Unknown command, 2: Buffer full]
mluis 2:14a5d6ad92d5 764 */
mluis 2:14a5d6ad92d5 765 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
mluis 0:91d1a7783bb9 766
mluis 0:91d1a7783bb9 767 /*!
Shaun Nelson 27:cda377b2a41f 768 * \brief Parses the MAC commands which must be repeated.
Shaun Nelson 27:cda377b2a41f 769 *
Shaun Nelson 27:cda377b2a41f 770 * \Remark MAC layer internal function
Shaun Nelson 27:cda377b2a41f 771 *
Shaun Nelson 27:cda377b2a41f 772 * \param [IN] cmdBufIn Buffer which stores the MAC commands to send
Shaun Nelson 27:cda377b2a41f 773 * \param [IN] length Length of the input buffer to parse
Shaun Nelson 27:cda377b2a41f 774 * \param [OUT] cmdBufOut Buffer which stores the MAC commands which must be
Shaun Nelson 27:cda377b2a41f 775 * repeated.
Shaun Nelson 27:cda377b2a41f 776 *
Shaun Nelson 27:cda377b2a41f 777 * \retval Size of the MAC commands to repeat.
Shaun Nelson 27:cda377b2a41f 778 */
Shaun Nelson 27:cda377b2a41f 779 static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut );
Shaun Nelson 27:cda377b2a41f 780
Shaun Nelson 27:cda377b2a41f 781 /*!
mluis 0:91d1a7783bb9 782 * \brief Validates if the payload fits into the frame, taking the datarate
mluis 0:91d1a7783bb9 783 * into account.
mluis 0:91d1a7783bb9 784 *
mluis 0:91d1a7783bb9 785 * \details Refer to chapter 4.3.2 of the LoRaWAN specification, v1.0
mluis 0:91d1a7783bb9 786 *
mluis 0:91d1a7783bb9 787 * \param lenN Length of the application payload. The length depends on the
mluis 0:91d1a7783bb9 788 * datarate and is region specific
mluis 0:91d1a7783bb9 789 *
mluis 0:91d1a7783bb9 790 * \param datarate Current datarate
mluis 0:91d1a7783bb9 791 *
mluis 2:14a5d6ad92d5 792 * \param fOptsLen Length of the fOpts field
mluis 2:14a5d6ad92d5 793 *
mluis 0:91d1a7783bb9 794 * \retval [false: payload does not fit into the frame, true: payload fits into
mluis 0:91d1a7783bb9 795 * the frame]
mluis 0:91d1a7783bb9 796 */
mluis 2:14a5d6ad92d5 797 static bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen );
mluis 0:91d1a7783bb9 798
mluis 4:37c12dbc8dc7 799 /*!
mluis 4:37c12dbc8dc7 800 * \brief Counts the number of bits in a mask.
mluis 4:37c12dbc8dc7 801 *
mluis 4:37c12dbc8dc7 802 * \param [IN] mask A mask from which the function counts the active bits.
mluis 4:37c12dbc8dc7 803 * \param [IN] nbBits The number of bits to check.
mluis 4:37c12dbc8dc7 804 *
mluis 4:37c12dbc8dc7 805 * \retval Number of enabled bits in the mask.
mluis 4:37c12dbc8dc7 806 */
mluis 4:37c12dbc8dc7 807 static uint8_t CountBits( uint16_t mask, uint8_t nbBits );
mluis 4:37c12dbc8dc7 808
mluis 0:91d1a7783bb9 809 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 0:91d1a7783bb9 810 /*!
mluis 0:91d1a7783bb9 811 * \brief Counts the number of enabled 125 kHz channels in the channel mask.
mluis 0:91d1a7783bb9 812 * This function can only be applied to US915 band.
mluis 0:91d1a7783bb9 813 *
mluis 4:37c12dbc8dc7 814 * \param [IN] channelsMask Pointer to the first element of the channel mask
mluis 0:91d1a7783bb9 815 *
mluis 0:91d1a7783bb9 816 * \retval Number of enabled channels in the channel mask
mluis 0:91d1a7783bb9 817 */
mluis 0:91d1a7783bb9 818 static uint8_t CountNbEnabled125kHzChannels( uint16_t *channelsMask );
mluis 4:37c12dbc8dc7 819
mluis 4:37c12dbc8dc7 820 #if defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 821 /*!
mluis 4:37c12dbc8dc7 822 * \brief Validates the correctness of the channel mask for US915, hybrid mode.
mluis 4:37c12dbc8dc7 823 *
mluis 4:37c12dbc8dc7 824 * \param [IN] mask Block definition to set.
mluis 4:37c12dbc8dc7 825 * \param [OUT] channelsMask Pointer to the first element of the channel mask
mluis 4:37c12dbc8dc7 826 */
mluis 4:37c12dbc8dc7 827 static void ReenableChannels( uint16_t mask, uint16_t* channelMask );
mluis 4:37c12dbc8dc7 828
mluis 4:37c12dbc8dc7 829 /*!
mluis 4:37c12dbc8dc7 830 * \brief Validates the correctness of the channel mask for US915, hybrid mode.
mluis 4:37c12dbc8dc7 831 *
mluis 4:37c12dbc8dc7 832 * \param [IN] channelsMask Pointer to the first element of the channel mask
mluis 4:37c12dbc8dc7 833 *
mluis 4:37c12dbc8dc7 834 * \retval [true: channel mask correct, false: channel mask not correct]
mluis 4:37c12dbc8dc7 835 */
mluis 4:37c12dbc8dc7 836 static bool ValidateChannelMask( uint16_t* channelMask );
mluis 4:37c12dbc8dc7 837 #endif
mluis 4:37c12dbc8dc7 838
mluis 0:91d1a7783bb9 839 #endif
mluis 0:91d1a7783bb9 840
mluis 0:91d1a7783bb9 841 /*!
mluis 0:91d1a7783bb9 842 * \brief Limits the Tx power according to the number of enabled channels
mluis 0:91d1a7783bb9 843 *
mluis 0:91d1a7783bb9 844 * \retval Returns the maximum valid tx power
mluis 0:91d1a7783bb9 845 */
mluis 0:91d1a7783bb9 846 static int8_t LimitTxPower( int8_t txPower );
mluis 0:91d1a7783bb9 847
mluis 0:91d1a7783bb9 848 /*!
mluis 2:14a5d6ad92d5 849 * \brief Verifies, if a value is in a given range.
mluis 2:14a5d6ad92d5 850 *
mluis 2:14a5d6ad92d5 851 * \param value Value to verify, if it is in range
mluis 2:14a5d6ad92d5 852 *
mluis 2:14a5d6ad92d5 853 * \param min Minimum possible value
mluis 2:14a5d6ad92d5 854 *
mluis 2:14a5d6ad92d5 855 * \param max Maximum possible value
mluis 2:14a5d6ad92d5 856 *
mluis 2:14a5d6ad92d5 857 * \retval Returns the maximum valid tx power
mluis 2:14a5d6ad92d5 858 */
mluis 2:14a5d6ad92d5 859 static bool ValueInRange( int8_t value, int8_t min, int8_t max );
mluis 2:14a5d6ad92d5 860
mluis 2:14a5d6ad92d5 861 /*!
mluis 2:14a5d6ad92d5 862 * \brief Calculates the next datarate to set, when ADR is on or off
mluis 2:14a5d6ad92d5 863 *
mluis 2:14a5d6ad92d5 864 * \param [IN] adrEnabled Specify whether ADR is on or off
mluis 2:14a5d6ad92d5 865 *
snelson 8:4816c8449bf2 866 * \param [IN] isTx Set to true if called by transmit, set to false if a dry run
mluis 2:14a5d6ad92d5 867 *
mluis 2:14a5d6ad92d5 868 * \param [OUT] datarateOut Reports the datarate which will be used next
mluis 2:14a5d6ad92d5 869 *
snelson 8:4816c8449bf2 870 * \param [OUT] txpowerOut Reports the tx power which will be used next
snelson 8:4816c8449bf2 871 *
mluis 2:14a5d6ad92d5 872 * \retval Returns the state of ADR ack request
mluis 2:14a5d6ad92d5 873 */
snelson 8:4816c8449bf2 874 static bool AdrNextDr( bool adrEnabled, bool isTx, int8_t* datarateOut, int8_t* txpowerOut );
mluis 2:14a5d6ad92d5 875
mluis 2:14a5d6ad92d5 876 /*!
mluis 2:14a5d6ad92d5 877 * \brief Disables channel in a specified channel mask
mluis 2:14a5d6ad92d5 878 *
mluis 2:14a5d6ad92d5 879 * \param [IN] id - Id of the channel
mluis 2:14a5d6ad92d5 880 *
mluis 2:14a5d6ad92d5 881 * \param [IN] mask - Pointer to the channel mask to edit
mluis 0:91d1a7783bb9 882 *
mluis 2:14a5d6ad92d5 883 * \retval [true, if disable was successful, false if not]
mluis 2:14a5d6ad92d5 884 */
mluis 2:14a5d6ad92d5 885 static bool DisableChannelInMask( uint8_t id, uint16_t* mask );
mluis 2:14a5d6ad92d5 886
mluis 2:14a5d6ad92d5 887 /*!
mluis 2:14a5d6ad92d5 888 * \brief Decodes MAC commands in the fOpts field and in the payload
mluis 2:14a5d6ad92d5 889 */
mluis 2:14a5d6ad92d5 890 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr );
mluis 2:14a5d6ad92d5 891
mluis 2:14a5d6ad92d5 892 /*!
mluis 2:14a5d6ad92d5 893 * \brief LoRaMAC layer generic send frame
mluis 2:14a5d6ad92d5 894 *
mluis 2:14a5d6ad92d5 895 * \param [IN] macHdr MAC header field
mluis 2:14a5d6ad92d5 896 * \param [IN] fPort MAC payload port
mluis 2:14a5d6ad92d5 897 * \param [IN] fBuffer MAC data buffer to be sent
mluis 2:14a5d6ad92d5 898 * \param [IN] fBufferSize MAC data buffer size
mluis 2:14a5d6ad92d5 899 * \retval status Status of the operation.
mluis 2:14a5d6ad92d5 900 */
mluis 2:14a5d6ad92d5 901 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
mluis 2:14a5d6ad92d5 902
mluis 2:14a5d6ad92d5 903 /*!
mluis 2:14a5d6ad92d5 904 * \brief LoRaMAC layer frame buffer initialization
mluis 2:14a5d6ad92d5 905 *
mluis 2:14a5d6ad92d5 906 * \param [IN] macHdr MAC header field
mluis 2:14a5d6ad92d5 907 * \param [IN] fCtrl MAC frame control field
mluis 2:14a5d6ad92d5 908 * \param [IN] fOpts MAC commands buffer
mluis 2:14a5d6ad92d5 909 * \param [IN] fPort MAC payload port
mluis 2:14a5d6ad92d5 910 * \param [IN] fBuffer MAC data buffer to be sent
mluis 2:14a5d6ad92d5 911 * \param [IN] fBufferSize MAC data buffer size
mluis 2:14a5d6ad92d5 912 * \retval status Status of the operation.
mluis 2:14a5d6ad92d5 913 */
mluis 2:14a5d6ad92d5 914 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
mluis 2:14a5d6ad92d5 915
mluis 2:14a5d6ad92d5 916 /*
mluis 2:14a5d6ad92d5 917 * \brief Schedules the frame according to the duty cycle
mluis 2:14a5d6ad92d5 918 *
mluis 2:14a5d6ad92d5 919 * \retval Status of the operation
mluis 2:14a5d6ad92d5 920 */
mluis 2:14a5d6ad92d5 921 static LoRaMacStatus_t ScheduleTx( void );
mluis 2:14a5d6ad92d5 922
mluis 4:37c12dbc8dc7 923 /*
mluis 4:37c12dbc8dc7 924 * \brief Calculates the back-off time for the band of a channel.
mluis 4:37c12dbc8dc7 925 *
mluis 4:37c12dbc8dc7 926 * \param [IN] channel The last Tx channel index
mluis 4:37c12dbc8dc7 927 */
mluis 4:37c12dbc8dc7 928 static void CalculateBackOff( uint8_t channel );
mluis 4:37c12dbc8dc7 929
mluis 2:14a5d6ad92d5 930 /*!
mluis 2:14a5d6ad92d5 931 * \brief LoRaMAC layer prepared frame buffer transmission with channel specification
mluis 2:14a5d6ad92d5 932 *
mluis 2:14a5d6ad92d5 933 * \remark PrepareFrame must be called at least once before calling this
mluis 2:14a5d6ad92d5 934 * function.
mluis 2:14a5d6ad92d5 935 *
mluis 2:14a5d6ad92d5 936 * \param [IN] channel Channel parameters
mluis 2:14a5d6ad92d5 937 * \retval status Status of the operation.
mluis 0:91d1a7783bb9 938 */
mluis 2:14a5d6ad92d5 939 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel );
mluis 2:14a5d6ad92d5 940
mluis 2:14a5d6ad92d5 941
mluis 2:14a5d6ad92d5 942
mluis 2:14a5d6ad92d5 943 static void OnRadioTxDone( void )
mluis 2:14a5d6ad92d5 944 {
mluis 2:14a5d6ad92d5 945 TimerTime_t curTime = TimerGetCurrentTime( );
mluis 2:14a5d6ad92d5 946 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 947 {
mluis 2:14a5d6ad92d5 948 Radio.Sleep( );
mluis 2:14a5d6ad92d5 949 }
mluis 2:14a5d6ad92d5 950 else
mluis 2:14a5d6ad92d5 951 {
mluis 2:14a5d6ad92d5 952 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 953 }
mluis 2:14a5d6ad92d5 954
mluis 4:37c12dbc8dc7 955 // Store last Tx channel
mluis 4:37c12dbc8dc7 956 LastTxChannel = Channel;
mluis 4:37c12dbc8dc7 957 // Update last tx done time for the current channel
mluis 4:37c12dbc8dc7 958 Bands[Channels[LastTxChannel].Band].LastTxDoneTime = curTime;
mluis 4:37c12dbc8dc7 959 // Update Aggregated last tx done time
mluis 2:14a5d6ad92d5 960 AggregatedLastTxDoneTime = curTime;
mluis 2:14a5d6ad92d5 961
snelson 8:4816c8449bf2 962 // Update join tx done and time on air
snelson 8:4816c8449bf2 963 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
snelson 8:4816c8449bf2 964 {
snelson 8:4816c8449bf2 965 JoinAggTimeOnAir += TxTimeOnAir;
snelson 8:4816c8449bf2 966 LastJoinTxTime = curTime;
snelson 8:4816c8449bf2 967 }
snelson 8:4816c8449bf2 968
mluis 2:14a5d6ad92d5 969 if( IsRxWindowsEnabled == true )
mluis 2:14a5d6ad92d5 970 {
mluis 2:14a5d6ad92d5 971 TimerSetValue( &RxWindowTimer1, RxWindow1Delay );
mluis 2:14a5d6ad92d5 972 TimerStart( &RxWindowTimer1 );
mluis 2:14a5d6ad92d5 973 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 974 {
mluis 2:14a5d6ad92d5 975 TimerSetValue( &RxWindowTimer2, RxWindow2Delay );
mluis 2:14a5d6ad92d5 976 TimerStart( &RxWindowTimer2 );
mluis 2:14a5d6ad92d5 977 }
mluis 2:14a5d6ad92d5 978 if( ( LoRaMacDeviceClass == CLASS_C ) || ( NodeAckRequested == true ) )
mluis 2:14a5d6ad92d5 979 {
mluis 2:14a5d6ad92d5 980 TimerSetValue( &AckTimeoutTimer, RxWindow2Delay + ACK_TIMEOUT +
mluis 2:14a5d6ad92d5 981 randr( -ACK_TIMEOUT_RND, ACK_TIMEOUT_RND ) );
mluis 2:14a5d6ad92d5 982 TimerStart( &AckTimeoutTimer );
mluis 2:14a5d6ad92d5 983 }
mluis 2:14a5d6ad92d5 984 }
mluis 2:14a5d6ad92d5 985 else
mluis 2:14a5d6ad92d5 986 {
mluis 2:14a5d6ad92d5 987 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 988 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 989
mluis 2:14a5d6ad92d5 990 if( LoRaMacFlags.Value == 0 )
mluis 2:14a5d6ad92d5 991 {
mluis 2:14a5d6ad92d5 992 LoRaMacFlags.Bits.McpsReq = 1;
mluis 2:14a5d6ad92d5 993 }
mluis 2:14a5d6ad92d5 994 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 995 }
mluis 2:14a5d6ad92d5 996
mluis 2:14a5d6ad92d5 997 if( NodeAckRequested == false )
mluis 2:14a5d6ad92d5 998 {
mluis 2:14a5d6ad92d5 999 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1000 ChannelsNbRepCounter++;
mluis 2:14a5d6ad92d5 1001 }
mluis 2:14a5d6ad92d5 1002 }
mluis 2:14a5d6ad92d5 1003
mluis 3:b9d87593a8ae 1004 static void PrepareRxDoneAbort( void )
mluis 3:b9d87593a8ae 1005 {
mluis 4:37c12dbc8dc7 1006 LoRaMacState |= MAC_RX_ABORT;
mluis 3:b9d87593a8ae 1007
mluis 3:b9d87593a8ae 1008 if( NodeAckRequested )
mluis 3:b9d87593a8ae 1009 {
mluis 3:b9d87593a8ae 1010 OnAckTimeoutTimerEvent( );
mluis 3:b9d87593a8ae 1011 }
mluis 3:b9d87593a8ae 1012
mluis 3:b9d87593a8ae 1013 if( ( RxSlot == 0 ) && ( LoRaMacDeviceClass == CLASS_C ) )
mluis 3:b9d87593a8ae 1014 {
mluis 3:b9d87593a8ae 1015 OnRxWindow2TimerEvent( );
mluis 3:b9d87593a8ae 1016 }
mluis 3:b9d87593a8ae 1017
mluis 3:b9d87593a8ae 1018 LoRaMacFlags.Bits.McpsInd = 1;
mluis 3:b9d87593a8ae 1019 LoRaMacFlags.Bits.MacDone = 1;
mluis 3:b9d87593a8ae 1020
mluis 3:b9d87593a8ae 1021 // Trig OnMacCheckTimerEvent call as soon as possible
mluis 3:b9d87593a8ae 1022 TimerSetValue( &MacStateCheckTimer, 1000 );
mluis 3:b9d87593a8ae 1023 TimerStart( &MacStateCheckTimer );
mluis 3:b9d87593a8ae 1024 }
mluis 3:b9d87593a8ae 1025
mluis 2:14a5d6ad92d5 1026 static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
mluis 0:91d1a7783bb9 1027 {
mluis 2:14a5d6ad92d5 1028 LoRaMacHeader_t macHdr;
mluis 2:14a5d6ad92d5 1029 LoRaMacFrameCtrl_t fCtrl;
mluis 4:37c12dbc8dc7 1030 bool skipIndication = false;
mluis 2:14a5d6ad92d5 1031
mluis 2:14a5d6ad92d5 1032 uint8_t pktHeaderLen = 0;
mluis 2:14a5d6ad92d5 1033 uint32_t address = 0;
mluis 2:14a5d6ad92d5 1034 uint8_t appPayloadStartIndex = 0;
mluis 2:14a5d6ad92d5 1035 uint8_t port = 0xFF;
mluis 2:14a5d6ad92d5 1036 uint8_t frameLen = 0;
mluis 2:14a5d6ad92d5 1037 uint32_t mic = 0;
mluis 2:14a5d6ad92d5 1038 uint32_t micRx = 0;
mluis 2:14a5d6ad92d5 1039
mluis 2:14a5d6ad92d5 1040 uint16_t sequenceCounter = 0;
mluis 2:14a5d6ad92d5 1041 uint16_t sequenceCounterPrev = 0;
mluis 2:14a5d6ad92d5 1042 uint16_t sequenceCounterDiff = 0;
mluis 2:14a5d6ad92d5 1043 uint32_t downLinkCounter = 0;
mluis 2:14a5d6ad92d5 1044
mluis 2:14a5d6ad92d5 1045 MulticastParams_t *curMulticastParams = NULL;
mluis 2:14a5d6ad92d5 1046 uint8_t *nwkSKey = LoRaMacNwkSKey;
mluis 2:14a5d6ad92d5 1047 uint8_t *appSKey = LoRaMacAppSKey;
mluis 2:14a5d6ad92d5 1048
mluis 2:14a5d6ad92d5 1049 uint8_t multicast = 0;
mluis 2:14a5d6ad92d5 1050
mluis 2:14a5d6ad92d5 1051 bool isMicOk = false;
mluis 2:14a5d6ad92d5 1052
mluis 2:14a5d6ad92d5 1053 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1054 McpsIndication.Rssi = rssi;
mluis 2:14a5d6ad92d5 1055 McpsIndication.Snr = snr;
mluis 2:14a5d6ad92d5 1056 McpsIndication.RxSlot = RxSlot;
mluis 2:14a5d6ad92d5 1057 McpsIndication.Port = 0;
mluis 2:14a5d6ad92d5 1058 McpsIndication.Multicast = 0;
mluis 2:14a5d6ad92d5 1059 McpsIndication.FramePending = 0;
mluis 2:14a5d6ad92d5 1060 McpsIndication.Buffer = NULL;
mluis 2:14a5d6ad92d5 1061 McpsIndication.BufferSize = 0;
mluis 2:14a5d6ad92d5 1062 McpsIndication.RxData = false;
mluis 2:14a5d6ad92d5 1063 McpsIndication.AckReceived = false;
mluis 2:14a5d6ad92d5 1064 McpsIndication.DownLinkCounter = 0;
mluis 2:14a5d6ad92d5 1065 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
mluis 2:14a5d6ad92d5 1066
mluis 2:14a5d6ad92d5 1067 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1068 {
mluis 2:14a5d6ad92d5 1069 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1070 }
mluis 2:14a5d6ad92d5 1071 TimerStop( &RxWindowTimer2 );
mluis 2:14a5d6ad92d5 1072
mluis 2:14a5d6ad92d5 1073 macHdr.Value = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1074
mluis 2:14a5d6ad92d5 1075 switch( macHdr.Bits.MType )
mluis 2:14a5d6ad92d5 1076 {
mluis 2:14a5d6ad92d5 1077 case FRAME_TYPE_JOIN_ACCEPT:
mluis 2:14a5d6ad92d5 1078 if( IsLoRaMacNetworkJoined == true )
mluis 2:14a5d6ad92d5 1079 {
mluis 2:14a5d6ad92d5 1080 break;
mluis 2:14a5d6ad92d5 1081 }
mluis 2:14a5d6ad92d5 1082 LoRaMacJoinDecrypt( payload + 1, size - 1, LoRaMacAppKey, LoRaMacRxPayload + 1 );
mluis 2:14a5d6ad92d5 1083
mluis 2:14a5d6ad92d5 1084 LoRaMacRxPayload[0] = macHdr.Value;
mluis 2:14a5d6ad92d5 1085
mluis 2:14a5d6ad92d5 1086 LoRaMacJoinComputeMic( LoRaMacRxPayload, size - LORAMAC_MFR_LEN, LoRaMacAppKey, &mic );
mluis 2:14a5d6ad92d5 1087
mluis 2:14a5d6ad92d5 1088 micRx |= ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN];
mluis 2:14a5d6ad92d5 1089 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 1] << 8 );
mluis 2:14a5d6ad92d5 1090 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 2] << 16 );
mluis 2:14a5d6ad92d5 1091 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 3] << 24 );
mluis 2:14a5d6ad92d5 1092
mluis 2:14a5d6ad92d5 1093 if( micRx == mic )
mluis 2:14a5d6ad92d5 1094 {
mluis 2:14a5d6ad92d5 1095 LoRaMacJoinComputeSKeys( LoRaMacAppKey, LoRaMacRxPayload + 1, LoRaMacDevNonce, LoRaMacNwkSKey, LoRaMacAppSKey );
mluis 2:14a5d6ad92d5 1096
mluis 2:14a5d6ad92d5 1097 LoRaMacNetID = ( uint32_t )LoRaMacRxPayload[4];
mluis 2:14a5d6ad92d5 1098 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[5] << 8 );
mluis 2:14a5d6ad92d5 1099 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[6] << 16 );
mluis 2:14a5d6ad92d5 1100
mluis 2:14a5d6ad92d5 1101 LoRaMacDevAddr = ( uint32_t )LoRaMacRxPayload[7];
mluis 2:14a5d6ad92d5 1102 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[8] << 8 );
mluis 2:14a5d6ad92d5 1103 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[9] << 16 );
mluis 2:14a5d6ad92d5 1104 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[10] << 24 );
mluis 2:14a5d6ad92d5 1105
mluis 2:14a5d6ad92d5 1106 // DLSettings
mluis 2:14a5d6ad92d5 1107 Rx1DrOffset = ( LoRaMacRxPayload[11] >> 4 ) & 0x07;
mluis 2:14a5d6ad92d5 1108 Rx2Channel.Datarate = LoRaMacRxPayload[11] & 0x0F;
mluis 2:14a5d6ad92d5 1109 // RxDelay
mluis 2:14a5d6ad92d5 1110 ReceiveDelay1 = ( LoRaMacRxPayload[12] & 0x0F );
mluis 2:14a5d6ad92d5 1111 if( ReceiveDelay1 == 0 )
mluis 2:14a5d6ad92d5 1112 {
mluis 2:14a5d6ad92d5 1113 ReceiveDelay1 = 1;
mluis 2:14a5d6ad92d5 1114 }
mluis 2:14a5d6ad92d5 1115 ReceiveDelay1 *= 1e6;
mluis 2:14a5d6ad92d5 1116 ReceiveDelay2 = ReceiveDelay1 + 1e6;
mluis 2:14a5d6ad92d5 1117
mluis 2:14a5d6ad92d5 1118 #if !( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 2:14a5d6ad92d5 1119 //CFList
mluis 2:14a5d6ad92d5 1120 if( ( size - 1 ) > 16 )
mluis 2:14a5d6ad92d5 1121 {
mluis 2:14a5d6ad92d5 1122 ChannelParams_t param;
mluis 2:14a5d6ad92d5 1123 param.DrRange.Value = ( DR_5 << 4 ) | DR_0;
mluis 2:14a5d6ad92d5 1124
mluis 3:b9d87593a8ae 1125 LoRaMacState |= MAC_TX_CONFIG;
mluis 2:14a5d6ad92d5 1126 for( uint8_t i = 3, j = 0; i < ( 5 + 3 ); i++, j += 3 )
mluis 2:14a5d6ad92d5 1127 {
mluis 2:14a5d6ad92d5 1128 param.Frequency = ( ( uint32_t )LoRaMacRxPayload[13 + j] | ( ( uint32_t )LoRaMacRxPayload[14 + j] << 8 ) | ( ( uint32_t )LoRaMacRxPayload[15 + j] << 16 ) ) * 100;
mluis 2:14a5d6ad92d5 1129 LoRaMacChannelAdd( i, param );
mluis 2:14a5d6ad92d5 1130 }
mluis 3:b9d87593a8ae 1131 LoRaMacState &= ~MAC_TX_CONFIG;
mluis 2:14a5d6ad92d5 1132 }
mluis 2:14a5d6ad92d5 1133 #endif
mluis 2:14a5d6ad92d5 1134 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1135 IsLoRaMacNetworkJoined = true;
snelson 8:4816c8449bf2 1136
snelson 8:4816c8449bf2 1137 // Do not change the datarate
snelson 8:4816c8449bf2 1138 // ChannelsDatarate = ChannelsDefaultDatarate;
mluis 2:14a5d6ad92d5 1139 }
mluis 2:14a5d6ad92d5 1140 else
mluis 2:14a5d6ad92d5 1141 {
mluis 2:14a5d6ad92d5 1142 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
mluis 2:14a5d6ad92d5 1143 }
mluis 2:14a5d6ad92d5 1144 break;
mluis 2:14a5d6ad92d5 1145 case FRAME_TYPE_DATA_CONFIRMED_DOWN:
mluis 2:14a5d6ad92d5 1146 case FRAME_TYPE_DATA_UNCONFIRMED_DOWN:
mluis 2:14a5d6ad92d5 1147 {
mluis 2:14a5d6ad92d5 1148 address = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1149 address |= ( (uint32_t)payload[pktHeaderLen++] << 8 );
mluis 2:14a5d6ad92d5 1150 address |= ( (uint32_t)payload[pktHeaderLen++] << 16 );
mluis 2:14a5d6ad92d5 1151 address |= ( (uint32_t)payload[pktHeaderLen++] << 24 );
mluis 2:14a5d6ad92d5 1152
mluis 2:14a5d6ad92d5 1153 if( address != LoRaMacDevAddr )
mluis 2:14a5d6ad92d5 1154 {
mluis 2:14a5d6ad92d5 1155 curMulticastParams = MulticastChannels;
mluis 2:14a5d6ad92d5 1156 while( curMulticastParams != NULL )
mluis 2:14a5d6ad92d5 1157 {
mluis 2:14a5d6ad92d5 1158 if( address == curMulticastParams->Address )
mluis 2:14a5d6ad92d5 1159 {
mluis 2:14a5d6ad92d5 1160 multicast = 1;
mluis 2:14a5d6ad92d5 1161 nwkSKey = curMulticastParams->NwkSKey;
mluis 2:14a5d6ad92d5 1162 appSKey = curMulticastParams->AppSKey;
mluis 2:14a5d6ad92d5 1163 downLinkCounter = curMulticastParams->DownLinkCounter;
mluis 2:14a5d6ad92d5 1164 break;
mluis 2:14a5d6ad92d5 1165 }
mluis 2:14a5d6ad92d5 1166 curMulticastParams = curMulticastParams->Next;
mluis 2:14a5d6ad92d5 1167 }
mluis 2:14a5d6ad92d5 1168 if( multicast == 0 )
mluis 2:14a5d6ad92d5 1169 {
mluis 2:14a5d6ad92d5 1170 // We are not the destination of this frame.
mluis 2:14a5d6ad92d5 1171 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL;
mluis 3:b9d87593a8ae 1172 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1173 return;
mluis 2:14a5d6ad92d5 1174 }
mluis 2:14a5d6ad92d5 1175 }
mluis 2:14a5d6ad92d5 1176 else
mluis 2:14a5d6ad92d5 1177 {
mluis 2:14a5d6ad92d5 1178 multicast = 0;
mluis 2:14a5d6ad92d5 1179 nwkSKey = LoRaMacNwkSKey;
mluis 2:14a5d6ad92d5 1180 appSKey = LoRaMacAppSKey;
mluis 2:14a5d6ad92d5 1181 downLinkCounter = DownLinkCounter;
mluis 2:14a5d6ad92d5 1182 }
mluis 2:14a5d6ad92d5 1183
mluis 2:14a5d6ad92d5 1184 fCtrl.Value = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1185
mluis 2:14a5d6ad92d5 1186 sequenceCounter = ( uint16_t )payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1187 sequenceCounter |= ( uint16_t )payload[pktHeaderLen++] << 8;
mluis 2:14a5d6ad92d5 1188
mluis 2:14a5d6ad92d5 1189 appPayloadStartIndex = 8 + fCtrl.Bits.FOptsLen;
mluis 2:14a5d6ad92d5 1190
mluis 2:14a5d6ad92d5 1191 micRx |= ( uint32_t )payload[size - LORAMAC_MFR_LEN];
mluis 2:14a5d6ad92d5 1192 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 1] << 8 );
mluis 2:14a5d6ad92d5 1193 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 2] << 16 );
mluis 2:14a5d6ad92d5 1194 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 3] << 24 );
mluis 2:14a5d6ad92d5 1195
mluis 2:14a5d6ad92d5 1196 sequenceCounterPrev = ( uint16_t )downLinkCounter;
mluis 2:14a5d6ad92d5 1197 sequenceCounterDiff = ( sequenceCounter - sequenceCounterPrev );
mluis 2:14a5d6ad92d5 1198
mluis 2:14a5d6ad92d5 1199 if( sequenceCounterDiff < ( 1 << 15 ) )
mluis 2:14a5d6ad92d5 1200 {
mluis 2:14a5d6ad92d5 1201 downLinkCounter += sequenceCounterDiff;
mluis 2:14a5d6ad92d5 1202 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounter, &mic );
mluis 2:14a5d6ad92d5 1203 if( micRx == mic )
mluis 2:14a5d6ad92d5 1204 {
mluis 2:14a5d6ad92d5 1205 isMicOk = true;
mluis 2:14a5d6ad92d5 1206 }
mluis 2:14a5d6ad92d5 1207 }
mluis 2:14a5d6ad92d5 1208 else
mluis 2:14a5d6ad92d5 1209 {
mluis 2:14a5d6ad92d5 1210 // check for sequence roll-over
mluis 2:14a5d6ad92d5 1211 uint32_t downLinkCounterTmp = downLinkCounter + 0x10000 + ( int16_t )sequenceCounterDiff;
mluis 2:14a5d6ad92d5 1212 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounterTmp, &mic );
mluis 2:14a5d6ad92d5 1213 if( micRx == mic )
mluis 2:14a5d6ad92d5 1214 {
mluis 2:14a5d6ad92d5 1215 isMicOk = true;
mluis 2:14a5d6ad92d5 1216 downLinkCounter = downLinkCounterTmp;
mluis 2:14a5d6ad92d5 1217 }
mluis 2:14a5d6ad92d5 1218 }
mluis 2:14a5d6ad92d5 1219
mluis 4:37c12dbc8dc7 1220 // Check for a the maximum allowed counter difference
mluis 4:37c12dbc8dc7 1221 if( sequenceCounterDiff >= MAX_FCNT_GAP )
mluis 4:37c12dbc8dc7 1222 {
mluis 4:37c12dbc8dc7 1223 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS;
mluis 4:37c12dbc8dc7 1224 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 4:37c12dbc8dc7 1225 PrepareRxDoneAbort( );
mluis 4:37c12dbc8dc7 1226 return;
mluis 4:37c12dbc8dc7 1227 }
mluis 4:37c12dbc8dc7 1228
mluis 2:14a5d6ad92d5 1229 if( isMicOk == true )
mluis 2:14a5d6ad92d5 1230 {
mluis 2:14a5d6ad92d5 1231 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1232 McpsIndication.Multicast = multicast;
mluis 2:14a5d6ad92d5 1233 McpsIndication.FramePending = fCtrl.Bits.FPending;
mluis 2:14a5d6ad92d5 1234 McpsIndication.Buffer = NULL;
mluis 2:14a5d6ad92d5 1235 McpsIndication.BufferSize = 0;
mluis 2:14a5d6ad92d5 1236 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1237
mluis 2:14a5d6ad92d5 1238 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1239
mluis 2:14a5d6ad92d5 1240 AdrAckCounter = 0;
Shaun Nelson 27:cda377b2a41f 1241 MacCommandsBufferToRepeatIndex = 0;
mluis 2:14a5d6ad92d5 1242
mluis 2:14a5d6ad92d5 1243 // Update 32 bits downlink counter
mluis 2:14a5d6ad92d5 1244 if( multicast == 1 )
mluis 2:14a5d6ad92d5 1245 {
mluis 2:14a5d6ad92d5 1246 McpsIndication.McpsIndication = MCPS_MULTICAST;
mluis 2:14a5d6ad92d5 1247
mluis 2:14a5d6ad92d5 1248 if( ( curMulticastParams->DownLinkCounter == downLinkCounter ) &&
mluis 2:14a5d6ad92d5 1249 ( curMulticastParams->DownLinkCounter != 0 ) )
mluis 2:14a5d6ad92d5 1250 {
mluis 2:14a5d6ad92d5 1251 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
mluis 2:14a5d6ad92d5 1252 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 3:b9d87593a8ae 1253 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1254 return;
mluis 2:14a5d6ad92d5 1255 }
mluis 2:14a5d6ad92d5 1256 curMulticastParams->DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1257 }
mluis 2:14a5d6ad92d5 1258 else
mluis 2:14a5d6ad92d5 1259 {
mluis 2:14a5d6ad92d5 1260 if( macHdr.Bits.MType == FRAME_TYPE_DATA_CONFIRMED_DOWN )
mluis 2:14a5d6ad92d5 1261 {
mluis 3:b9d87593a8ae 1262 SrvAckRequested = true;
mluis 2:14a5d6ad92d5 1263 McpsIndication.McpsIndication = MCPS_CONFIRMED;
mluis 4:37c12dbc8dc7 1264
mluis 4:37c12dbc8dc7 1265 if( ( DownLinkCounter == downLinkCounter ) &&
mluis 4:37c12dbc8dc7 1266 ( DownLinkCounter != 0 ) )
mluis 4:37c12dbc8dc7 1267 {
mluis 4:37c12dbc8dc7 1268 // Duplicated confirmed downlink. Skip indication.
mluis 4:37c12dbc8dc7 1269 skipIndication = true;
mluis 4:37c12dbc8dc7 1270 }
mluis 2:14a5d6ad92d5 1271 }
mluis 2:14a5d6ad92d5 1272 else
mluis 2:14a5d6ad92d5 1273 {
mluis 3:b9d87593a8ae 1274 SrvAckRequested = false;
mluis 2:14a5d6ad92d5 1275 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
mluis 4:37c12dbc8dc7 1276
mluis 4:37c12dbc8dc7 1277 if( ( DownLinkCounter == downLinkCounter ) &&
mluis 4:37c12dbc8dc7 1278 ( DownLinkCounter != 0 ) )
mluis 4:37c12dbc8dc7 1279 {
mluis 4:37c12dbc8dc7 1280 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
mluis 4:37c12dbc8dc7 1281 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 4:37c12dbc8dc7 1282 PrepareRxDoneAbort( );
mluis 4:37c12dbc8dc7 1283 return;
mluis 4:37c12dbc8dc7 1284 }
mluis 2:14a5d6ad92d5 1285 }
mluis 2:14a5d6ad92d5 1286 DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1287 }
mluis 2:14a5d6ad92d5 1288
mluis 2:14a5d6ad92d5 1289
mluis 2:14a5d6ad92d5 1290 if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 )
mluis 2:14a5d6ad92d5 1291 {
mluis 2:14a5d6ad92d5 1292 port = payload[appPayloadStartIndex++];
mluis 2:14a5d6ad92d5 1293 frameLen = ( size - 4 ) - appPayloadStartIndex;
mluis 2:14a5d6ad92d5 1294
mluis 2:14a5d6ad92d5 1295 McpsIndication.Port = port;
mluis 2:14a5d6ad92d5 1296
mluis 2:14a5d6ad92d5 1297 if( port == 0 )
mluis 2:14a5d6ad92d5 1298 {
snelson 8:4816c8449bf2 1299 if( fCtrl.Bits.FOptsLen == 0 )
snelson 8:4816c8449bf2 1300 {
snelson 8:4816c8449bf2 1301 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
snelson 8:4816c8449bf2 1302 frameLen,
snelson 8:4816c8449bf2 1303 nwkSKey,
snelson 8:4816c8449bf2 1304 address,
snelson 8:4816c8449bf2 1305 DOWN_LINK,
snelson 8:4816c8449bf2 1306 downLinkCounter,
snelson 8:4816c8449bf2 1307 LoRaMacRxPayload );
snelson 8:4816c8449bf2 1308
snelson 8:4816c8449bf2 1309 // Decode frame payload MAC commands
snelson 8:4816c8449bf2 1310 ProcessMacCommands( LoRaMacRxPayload, 0, frameLen, snr );
snelson 8:4816c8449bf2 1311 }
snelson 8:4816c8449bf2 1312 else
snelson 8:4816c8449bf2 1313 {
snelson 8:4816c8449bf2 1314 skipIndication = true;
snelson 8:4816c8449bf2 1315 }
mluis 2:14a5d6ad92d5 1316 }
mluis 2:14a5d6ad92d5 1317 else
mluis 2:14a5d6ad92d5 1318 {
snelson 8:4816c8449bf2 1319 if( fCtrl.Bits.FOptsLen > 0 )
snelson 8:4816c8449bf2 1320 {
snelson 8:4816c8449bf2 1321 // Decode Options field MAC commands. Omit the fPort.
snelson 8:4816c8449bf2 1322 ProcessMacCommands( payload, 8, appPayloadStartIndex - 1, snr );
snelson 8:4816c8449bf2 1323 }
snelson 8:4816c8449bf2 1324
mluis 2:14a5d6ad92d5 1325 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
mluis 2:14a5d6ad92d5 1326 frameLen,
mluis 2:14a5d6ad92d5 1327 appSKey,
mluis 2:14a5d6ad92d5 1328 address,
mluis 2:14a5d6ad92d5 1329 DOWN_LINK,
mluis 2:14a5d6ad92d5 1330 downLinkCounter,
mluis 2:14a5d6ad92d5 1331 LoRaMacRxPayload );
mluis 2:14a5d6ad92d5 1332
mluis 4:37c12dbc8dc7 1333 if( skipIndication == false )
mluis 4:37c12dbc8dc7 1334 {
mluis 4:37c12dbc8dc7 1335 McpsIndication.Buffer = LoRaMacRxPayload;
mluis 4:37c12dbc8dc7 1336 McpsIndication.BufferSize = frameLen;
mluis 4:37c12dbc8dc7 1337 McpsIndication.RxData = true;
mluis 4:37c12dbc8dc7 1338 }
mluis 2:14a5d6ad92d5 1339 }
mluis 2:14a5d6ad92d5 1340 }
snelson 8:4816c8449bf2 1341 else
snelson 8:4816c8449bf2 1342 {
snelson 8:4816c8449bf2 1343 if( fCtrl.Bits.FOptsLen > 0 )
snelson 8:4816c8449bf2 1344 {
snelson 8:4816c8449bf2 1345 // Decode Options field MAC commands
snelson 8:4816c8449bf2 1346 ProcessMacCommands( payload, 8, appPayloadStartIndex, snr );
snelson 8:4816c8449bf2 1347 }
snelson 8:4816c8449bf2 1348 }
snelson 8:4816c8449bf2 1349
mluis 4:37c12dbc8dc7 1350 if( skipIndication == false )
mluis 4:37c12dbc8dc7 1351 {
shaunkrnelson 23:80965acdd311 1352 // Check if the frame is an acknowledgement
shaunkrnelson 23:80965acdd311 1353 if( fCtrl.Bits.Ack == 1 )
shaunkrnelson 23:80965acdd311 1354 {
shaunkrnelson 23:80965acdd311 1355 McpsConfirm.AckReceived = true;
shaunkrnelson 23:80965acdd311 1356 McpsIndication.AckReceived = true;
shaunkrnelson 23:80965acdd311 1357
shaunkrnelson 23:80965acdd311 1358 // Stop the AckTimeout timer as no more retransmissions
shaunkrnelson 23:80965acdd311 1359 // are needed.
shaunkrnelson 23:80965acdd311 1360 TimerStop( &AckTimeoutTimer );
shaunkrnelson 23:80965acdd311 1361 }
shaunkrnelson 23:80965acdd311 1362 else
shaunkrnelson 23:80965acdd311 1363 {
shaunkrnelson 23:80965acdd311 1364 McpsConfirm.AckReceived = false;
shaunkrnelson 23:80965acdd311 1365
shaunkrnelson 23:80965acdd311 1366 if( AckTimeoutRetriesCounter > AckTimeoutRetries )
shaunkrnelson 23:80965acdd311 1367 {
shaunkrnelson 23:80965acdd311 1368 // Stop the AckTimeout timer as no more retransmissions
shaunkrnelson 23:80965acdd311 1369 // are needed.
shaunkrnelson 23:80965acdd311 1370 TimerStop( &AckTimeoutTimer );
shaunkrnelson 23:80965acdd311 1371 }
shaunkrnelson 23:80965acdd311 1372 }
mluis 4:37c12dbc8dc7 1373 LoRaMacFlags.Bits.McpsInd = 1;
mluis 4:37c12dbc8dc7 1374 }
mluis 2:14a5d6ad92d5 1375 }
mluis 2:14a5d6ad92d5 1376 else
mluis 2:14a5d6ad92d5 1377 {
mluis 2:14a5d6ad92d5 1378 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL;
mluis 3:b9d87593a8ae 1379
mluis 3:b9d87593a8ae 1380 PrepareRxDoneAbort( );
mluis 3:b9d87593a8ae 1381 return;
mluis 2:14a5d6ad92d5 1382 }
mluis 2:14a5d6ad92d5 1383 }
mluis 2:14a5d6ad92d5 1384 break;
mluis 2:14a5d6ad92d5 1385 case FRAME_TYPE_PROPRIETARY:
mluis 2:14a5d6ad92d5 1386 {
mluis 2:14a5d6ad92d5 1387 memcpy1( LoRaMacRxPayload, &payload[pktHeaderLen], size );
mluis 2:14a5d6ad92d5 1388
mluis 2:14a5d6ad92d5 1389 McpsIndication.McpsIndication = MCPS_PROPRIETARY;
mluis 2:14a5d6ad92d5 1390 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1391 McpsIndication.Buffer = LoRaMacRxPayload;
mluis 2:14a5d6ad92d5 1392 McpsIndication.BufferSize = size - pktHeaderLen;
mluis 3:b9d87593a8ae 1393
mluis 3:b9d87593a8ae 1394 LoRaMacFlags.Bits.McpsInd = 1;
mluis 2:14a5d6ad92d5 1395 break;
mluis 2:14a5d6ad92d5 1396 }
mluis 2:14a5d6ad92d5 1397 default:
mluis 2:14a5d6ad92d5 1398 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 3:b9d87593a8ae 1399 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1400 break;
mluis 2:14a5d6ad92d5 1401 }
mluis 2:14a5d6ad92d5 1402
mluis 2:14a5d6ad92d5 1403 if( ( RxSlot == 0 ) && ( LoRaMacDeviceClass == CLASS_C ) )
mluis 2:14a5d6ad92d5 1404 {
mluis 2:14a5d6ad92d5 1405 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1406 }
mluis 2:14a5d6ad92d5 1407 LoRaMacFlags.Bits.MacDone = 1;
mluis 3:b9d87593a8ae 1408
mluis 3:b9d87593a8ae 1409 // Trig OnMacCheckTimerEvent call as soon as possible
mluis 3:b9d87593a8ae 1410 TimerSetValue( &MacStateCheckTimer, 1000 );
mluis 3:b9d87593a8ae 1411 TimerStart( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1412 }
mluis 2:14a5d6ad92d5 1413
mluis 2:14a5d6ad92d5 1414 static void OnRadioTxTimeout( void )
mluis 2:14a5d6ad92d5 1415 {
mluis 2:14a5d6ad92d5 1416 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1417 {
mluis 2:14a5d6ad92d5 1418 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1419 }
mluis 2:14a5d6ad92d5 1420 else
mluis 2:14a5d6ad92d5 1421 {
mluis 2:14a5d6ad92d5 1422 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1423 }
mluis 2:14a5d6ad92d5 1424
mluis 2:14a5d6ad92d5 1425 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
mluis 2:14a5d6ad92d5 1426 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
mluis 2:14a5d6ad92d5 1427 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1428 }
mluis 2:14a5d6ad92d5 1429
mluis 2:14a5d6ad92d5 1430 static void OnRadioRxError( void )
mluis 2:14a5d6ad92d5 1431 {
mluis 2:14a5d6ad92d5 1432 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1433 {
mluis 2:14a5d6ad92d5 1434 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1435 }
mluis 2:14a5d6ad92d5 1436 else
mluis 2:14a5d6ad92d5 1437 {
mluis 2:14a5d6ad92d5 1438 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1439 }
mluis 2:14a5d6ad92d5 1440
mluis 2:14a5d6ad92d5 1441 if( RxSlot == 1 )
mluis 2:14a5d6ad92d5 1442 {
mluis 2:14a5d6ad92d5 1443 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1444 {
mluis 2:14a5d6ad92d5 1445 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_ERROR;
mluis 2:14a5d6ad92d5 1446 }
mluis 2:14a5d6ad92d5 1447 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_ERROR;
mluis 2:14a5d6ad92d5 1448 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1449 }
mluis 2:14a5d6ad92d5 1450 }
mluis 2:14a5d6ad92d5 1451
mluis 2:14a5d6ad92d5 1452 static void OnRadioRxTimeout( void )
mluis 2:14a5d6ad92d5 1453 {
mluis 2:14a5d6ad92d5 1454 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1455 {
mluis 2:14a5d6ad92d5 1456 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1457 }
mluis 2:14a5d6ad92d5 1458 else
mluis 2:14a5d6ad92d5 1459 {
mluis 2:14a5d6ad92d5 1460 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1461 }
mluis 2:14a5d6ad92d5 1462
mluis 2:14a5d6ad92d5 1463 if( RxSlot == 1 )
mluis 2:14a5d6ad92d5 1464 {
mluis 2:14a5d6ad92d5 1465 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1466 {
mluis 2:14a5d6ad92d5 1467 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 1468 }
mluis 2:14a5d6ad92d5 1469 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 1470 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1471 }
mluis 2:14a5d6ad92d5 1472 }
mluis 2:14a5d6ad92d5 1473
mluis 2:14a5d6ad92d5 1474 static void OnMacStateCheckTimerEvent( void )
mluis 2:14a5d6ad92d5 1475 {
mluis 2:14a5d6ad92d5 1476 TimerStop( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1477 bool txTimeout = false;
mluis 2:14a5d6ad92d5 1478
mluis 2:14a5d6ad92d5 1479 if( LoRaMacFlags.Bits.MacDone == 1 )
mluis 2:14a5d6ad92d5 1480 {
mluis 4:37c12dbc8dc7 1481 if( ( LoRaMacState & MAC_RX_ABORT ) == MAC_RX_ABORT )
mluis 4:37c12dbc8dc7 1482 {
mluis 4:37c12dbc8dc7 1483 LoRaMacState &= ~MAC_RX_ABORT;
mluis 4:37c12dbc8dc7 1484 LoRaMacState &= ~MAC_TX_RUNNING;
mluis 4:37c12dbc8dc7 1485 }
mluis 4:37c12dbc8dc7 1486
mluis 2:14a5d6ad92d5 1487 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
mluis 2:14a5d6ad92d5 1488 {
mluis 2:14a5d6ad92d5 1489 if( ( McpsConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) ||
mluis 2:14a5d6ad92d5 1490 ( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) )
mluis 2:14a5d6ad92d5 1491 {
mluis 2:14a5d6ad92d5 1492 // Stop transmit cycle due to tx timeout.
mluis 2:14a5d6ad92d5 1493 LoRaMacState &= ~MAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 1494 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1495 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1496 McpsConfirm.TxTimeOnAir = 0;
mluis 2:14a5d6ad92d5 1497 txTimeout = true;
mluis 2:14a5d6ad92d5 1498 }
mluis 2:14a5d6ad92d5 1499 }
mluis 2:14a5d6ad92d5 1500
mluis 2:14a5d6ad92d5 1501 if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
mluis 2:14a5d6ad92d5 1502 {
mluis 2:14a5d6ad92d5 1503 if( LoRaMacFlags.Bits.MlmeReq == 1 )
mluis 2:14a5d6ad92d5 1504 {
mluis 2:14a5d6ad92d5 1505 if( MlmeConfirm.MlmeRequest == MLME_JOIN )
mluis 2:14a5d6ad92d5 1506 {
mluis 2:14a5d6ad92d5 1507 if( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 2:14a5d6ad92d5 1508 {
mluis 2:14a5d6ad92d5 1509 UpLinkCounter = 0;
mluis 2:14a5d6ad92d5 1510 }
mluis 2:14a5d6ad92d5 1511 // Join messages aren't repeated automatically
mluis 2:14a5d6ad92d5 1512 ChannelsNbRepCounter = ChannelsNbRep;
mluis 2:14a5d6ad92d5 1513 }
mluis 2:14a5d6ad92d5 1514 }
mluis 2:14a5d6ad92d5 1515 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
mluis 2:14a5d6ad92d5 1516 {
mluis 2:14a5d6ad92d5 1517 if( ( ChannelsNbRepCounter >= ChannelsNbRep ) || ( LoRaMacFlags.Bits.McpsInd == 1 ) )
mluis 2:14a5d6ad92d5 1518 {
mluis 2:14a5d6ad92d5 1519 ChannelsNbRepCounter = 0;
mluis 2:14a5d6ad92d5 1520
mluis 2:14a5d6ad92d5 1521 AdrAckCounter++;
mluis 2:14a5d6ad92d5 1522 if( IsUpLinkCounterFixed == false )
mluis 2:14a5d6ad92d5 1523 {
mluis 2:14a5d6ad92d5 1524 UpLinkCounter++;
mluis 2:14a5d6ad92d5 1525 }
mluis 2:14a5d6ad92d5 1526
mluis 2:14a5d6ad92d5 1527 LoRaMacState &= ~MAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 1528 }
mluis 2:14a5d6ad92d5 1529 else
mluis 2:14a5d6ad92d5 1530 {
mluis 2:14a5d6ad92d5 1531 LoRaMacFlags.Bits.MacDone = 0;
mluis 2:14a5d6ad92d5 1532 // Sends the same frame again
mluis 2:14a5d6ad92d5 1533 ScheduleTx( );
mluis 2:14a5d6ad92d5 1534 }
mluis 2:14a5d6ad92d5 1535 }
mluis 2:14a5d6ad92d5 1536 }
mluis 2:14a5d6ad92d5 1537
mluis 2:14a5d6ad92d5 1538 if( LoRaMacFlags.Bits.McpsInd == 1 )
mluis 2:14a5d6ad92d5 1539 {
mluis 2:14a5d6ad92d5 1540 if( ( McpsConfirm.AckReceived == true ) || ( AckTimeoutRetriesCounter > AckTimeoutRetries ) )
mluis 2:14a5d6ad92d5 1541 {
mluis 2:14a5d6ad92d5 1542 AckTimeoutRetry = false;
mluis 3:b9d87593a8ae 1543 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 1544 if( IsUpLinkCounterFixed == false )
mluis 2:14a5d6ad92d5 1545 {
mluis 2:14a5d6ad92d5 1546 UpLinkCounter++;
mluis 2:14a5d6ad92d5 1547 }
mluis 2:14a5d6ad92d5 1548 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1549
mluis 2:14a5d6ad92d5 1550 LoRaMacState &= ~MAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 1551 }
mluis 2:14a5d6ad92d5 1552 }
mluis 2:14a5d6ad92d5 1553
mluis 2:14a5d6ad92d5 1554 if( ( AckTimeoutRetry == true ) && ( ( LoRaMacState & MAC_TX_DELAYED ) == 0 ) )
mluis 2:14a5d6ad92d5 1555 {
mluis 2:14a5d6ad92d5 1556 AckTimeoutRetry = false;
mluis 2:14a5d6ad92d5 1557 if( ( AckTimeoutRetriesCounter < AckTimeoutRetries ) && ( AckTimeoutRetriesCounter <= MAX_ACK_RETRIES ) )
mluis 2:14a5d6ad92d5 1558 {
mluis 2:14a5d6ad92d5 1559 AckTimeoutRetriesCounter++;
mluis 2:14a5d6ad92d5 1560
mluis 2:14a5d6ad92d5 1561 if( ( AckTimeoutRetriesCounter % 2 ) == 1 )
mluis 2:14a5d6ad92d5 1562 {
mluis 4:37c12dbc8dc7 1563 ChannelsDatarate = MAX( ChannelsDatarate - 1, LORAMAC_TX_MIN_DATARATE );
ubhat 26:4627985dc5be 1564 // Check if new datarate is valid for the Payload length
ubhat 26:4627985dc5be 1565 if( ValidatePayloadLength( LoRaMacBufferPktLen - 13, ChannelsDatarate, 0 ) == false )
ubhat 26:4627985dc5be 1566 {
ubhat 26:4627985dc5be 1567 // If invalid payload length, then revert to previous datarate
ubhat 26:4627985dc5be 1568 ChannelsDatarate++;
ubhat 26:4627985dc5be 1569 }
mluis 2:14a5d6ad92d5 1570 }
mluis 2:14a5d6ad92d5 1571 LoRaMacFlags.Bits.MacDone = 0;
mluis 2:14a5d6ad92d5 1572 // Sends the same frame again
mluis 2:14a5d6ad92d5 1573 ScheduleTx( );
mluis 2:14a5d6ad92d5 1574 }
mluis 2:14a5d6ad92d5 1575 else
mluis 2:14a5d6ad92d5 1576 {
mluis 2:14a5d6ad92d5 1577 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 1578 // Re-enable default channels LC1, LC2, LC3
mluis 2:14a5d6ad92d5 1579 ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
Shaun Nelson 13:90a55bd536a1 1580 #elif !defined( USE_BAND_915 ) && !defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 1581 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 1582 #endif
mluis 2:14a5d6ad92d5 1583 LoRaMacState &= ~MAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 1584
mluis 3:b9d87593a8ae 1585 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 1586 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1587 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1588 if( IsUpLinkCounterFixed == false )
mluis 2:14a5d6ad92d5 1589 {
mluis 2:14a5d6ad92d5 1590 UpLinkCounter++;
mluis 2:14a5d6ad92d5 1591 }
mluis 2:14a5d6ad92d5 1592 }
mluis 2:14a5d6ad92d5 1593 }
mluis 2:14a5d6ad92d5 1594 }
mluis 2:14a5d6ad92d5 1595 // Handle reception for Class B and Class C
mluis 2:14a5d6ad92d5 1596 if( ( LoRaMacState & MAC_RX ) == MAC_RX )
mluis 2:14a5d6ad92d5 1597 {
mluis 2:14a5d6ad92d5 1598 LoRaMacState &= ~MAC_RX;
mluis 2:14a5d6ad92d5 1599 }
mluis 2:14a5d6ad92d5 1600 if( LoRaMacState == MAC_IDLE )
mluis 2:14a5d6ad92d5 1601 {
mluis 2:14a5d6ad92d5 1602 if( LoRaMacFlags.Bits.McpsReq == 1 )
mluis 2:14a5d6ad92d5 1603 {
mluis 2:14a5d6ad92d5 1604 LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
mluis 2:14a5d6ad92d5 1605 LoRaMacFlags.Bits.McpsReq = 0;
mluis 2:14a5d6ad92d5 1606 }
mluis 2:14a5d6ad92d5 1607
mluis 2:14a5d6ad92d5 1608 if( LoRaMacFlags.Bits.MlmeReq == 1 )
mluis 2:14a5d6ad92d5 1609 {
mluis 2:14a5d6ad92d5 1610 LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
mluis 2:14a5d6ad92d5 1611 LoRaMacFlags.Bits.MlmeReq = 0;
mluis 2:14a5d6ad92d5 1612 }
mluis 2:14a5d6ad92d5 1613
mluis 2:14a5d6ad92d5 1614 LoRaMacFlags.Bits.MacDone = 0;
mluis 2:14a5d6ad92d5 1615 }
mluis 2:14a5d6ad92d5 1616 else
mluis 2:14a5d6ad92d5 1617 {
mluis 2:14a5d6ad92d5 1618 // Operation not finished restart timer
mluis 3:b9d87593a8ae 1619 TimerSetValue( &MacStateCheckTimer, MAC_STATE_CHECK_TIMEOUT );
mluis 2:14a5d6ad92d5 1620 TimerStart( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1621 }
mluis 2:14a5d6ad92d5 1622
mluis 2:14a5d6ad92d5 1623 if( LoRaMacFlags.Bits.McpsInd == 1 )
mluis 2:14a5d6ad92d5 1624 {
mluis 2:14a5d6ad92d5 1625 LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
mluis 2:14a5d6ad92d5 1626 LoRaMacFlags.Bits.McpsInd = 0;
mluis 2:14a5d6ad92d5 1627 }
mluis 2:14a5d6ad92d5 1628 }
mluis 2:14a5d6ad92d5 1629
mluis 2:14a5d6ad92d5 1630 static void OnTxDelayedTimerEvent( void )
mluis 2:14a5d6ad92d5 1631 {
mluis 2:14a5d6ad92d5 1632 TimerStop( &TxDelayedTimer );
mluis 2:14a5d6ad92d5 1633 LoRaMacState &= ~MAC_TX_DELAYED;
mluis 2:14a5d6ad92d5 1634
mluis 2:14a5d6ad92d5 1635 ScheduleTx( );
mluis 2:14a5d6ad92d5 1636 }
mluis 2:14a5d6ad92d5 1637
mluis 2:14a5d6ad92d5 1638 static void OnRxWindow1TimerEvent( void )
mluis 2:14a5d6ad92d5 1639 {
mluis 2:14a5d6ad92d5 1640 uint16_t symbTimeout = 5; // DR_2, DR_1, DR_0
mluis 2:14a5d6ad92d5 1641 int8_t datarate = 0;
mluis 2:14a5d6ad92d5 1642 uint32_t bandwidth = 0; // LoRa 125 kHz
mluis 2:14a5d6ad92d5 1643
mluis 2:14a5d6ad92d5 1644 TimerStop( &RxWindowTimer1 );
mluis 2:14a5d6ad92d5 1645 RxSlot = 0;
mluis 2:14a5d6ad92d5 1646
mluis 2:14a5d6ad92d5 1647 if( LoRaMacDeviceClass == CLASS_C )
mluis 2:14a5d6ad92d5 1648 {
mluis 2:14a5d6ad92d5 1649 Radio.Standby( );
mluis 2:14a5d6ad92d5 1650 }
mluis 2:14a5d6ad92d5 1651
mluis 2:14a5d6ad92d5 1652 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 1653 datarate = ChannelsDatarate - Rx1DrOffset;
mluis 2:14a5d6ad92d5 1654 if( datarate < 0 )
mluis 2:14a5d6ad92d5 1655 {
mluis 2:14a5d6ad92d5 1656 datarate = DR_0;
mluis 2:14a5d6ad92d5 1657 }
mluis 2:14a5d6ad92d5 1658
mluis 2:14a5d6ad92d5 1659 // For higher datarates, we increase the number of symbols generating a Rx Timeout
mluis 4:37c12dbc8dc7 1660 if( ( datarate == DR_3 ) || ( datarate == DR_4 ) )
mluis 4:37c12dbc8dc7 1661 { // DR_4, DR_3
mluis 2:14a5d6ad92d5 1662 symbTimeout = 8;
mluis 2:14a5d6ad92d5 1663 }
mluis 4:37c12dbc8dc7 1664 else if( datarate == DR_5 )
mluis 4:37c12dbc8dc7 1665 {
mluis 4:37c12dbc8dc7 1666 symbTimeout = 10;
mluis 4:37c12dbc8dc7 1667 }
mluis 4:37c12dbc8dc7 1668 else if( datarate == DR_6 )
mluis 2:14a5d6ad92d5 1669 {// LoRa 250 kHz
mluis 2:14a5d6ad92d5 1670 bandwidth = 1;
mluis 4:37c12dbc8dc7 1671 symbTimeout = 14;
mluis 2:14a5d6ad92d5 1672 }
mluis 2:14a5d6ad92d5 1673 RxWindowSetup( Channels[Channel].Frequency, datarate, bandwidth, symbTimeout, false );
mluis 2:14a5d6ad92d5 1674 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 2:14a5d6ad92d5 1675 datarate = datarateOffsets[ChannelsDatarate][Rx1DrOffset];
mluis 2:14a5d6ad92d5 1676 if( datarate < 0 )
mluis 2:14a5d6ad92d5 1677 {
mluis 2:14a5d6ad92d5 1678 datarate = DR_0;
mluis 2:14a5d6ad92d5 1679 }
mluis 2:14a5d6ad92d5 1680 // For higher datarates, we increase the number of symbols generating a Rx Timeout
mluis 4:37c12dbc8dc7 1681 switch( datarate )
mluis 4:37c12dbc8dc7 1682 {
mluis 4:37c12dbc8dc7 1683 case DR_0: // SF10 - BW125
mluis 4:37c12dbc8dc7 1684 symbTimeout = 5;
mluis 4:37c12dbc8dc7 1685 break;
mluis 4:37c12dbc8dc7 1686
mluis 4:37c12dbc8dc7 1687 case DR_1: // SF9 - BW125
mluis 4:37c12dbc8dc7 1688 case DR_2: // SF8 - BW125
mluis 4:37c12dbc8dc7 1689 case DR_8: // SF12 - BW500
mluis 4:37c12dbc8dc7 1690 case DR_9: // SF11 - BW500
mluis 4:37c12dbc8dc7 1691 case DR_10: // SF10 - BW500
mluis 4:37c12dbc8dc7 1692 symbTimeout = 8;
mluis 4:37c12dbc8dc7 1693 break;
mluis 4:37c12dbc8dc7 1694
mluis 4:37c12dbc8dc7 1695 case DR_3: // SF7 - BW125
mluis 4:37c12dbc8dc7 1696 case DR_11: // SF9 - BW500
mluis 4:37c12dbc8dc7 1697 symbTimeout = 10;
mluis 4:37c12dbc8dc7 1698 break;
mluis 4:37c12dbc8dc7 1699
mluis 4:37c12dbc8dc7 1700 case DR_4: // SF8 - BW500
mluis 4:37c12dbc8dc7 1701 case DR_12: // SF8 - BW500
mluis 4:37c12dbc8dc7 1702 symbTimeout = 14;
mluis 4:37c12dbc8dc7 1703 break;
mluis 4:37c12dbc8dc7 1704
mluis 4:37c12dbc8dc7 1705 case DR_13: // SF7 - BW500
mluis 4:37c12dbc8dc7 1706 symbTimeout = 16;
mluis 4:37c12dbc8dc7 1707 break;
mluis 4:37c12dbc8dc7 1708 default:
mluis 4:37c12dbc8dc7 1709 break;
mluis 2:14a5d6ad92d5 1710 }
mluis 2:14a5d6ad92d5 1711 if( datarate >= DR_4 )
mluis 2:14a5d6ad92d5 1712 {// LoRa 500 kHz
mluis 2:14a5d6ad92d5 1713 bandwidth = 2;
mluis 2:14a5d6ad92d5 1714 }
mluis 2:14a5d6ad92d5 1715 RxWindowSetup( 923.3e6 + ( Channel % 8 ) * 600e3, datarate, bandwidth, symbTimeout, false );
mluis 2:14a5d6ad92d5 1716 #else
mluis 2:14a5d6ad92d5 1717 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 1718 #endif
mluis 2:14a5d6ad92d5 1719 }
mluis 2:14a5d6ad92d5 1720
mluis 2:14a5d6ad92d5 1721 static void OnRxWindow2TimerEvent( void )
mluis 2:14a5d6ad92d5 1722 {
mluis 2:14a5d6ad92d5 1723 uint16_t symbTimeout = 5; // DR_2, DR_1, DR_0
mluis 2:14a5d6ad92d5 1724 uint32_t bandwidth = 0; // LoRa 125 kHz
mluis 2:14a5d6ad92d5 1725
mluis 2:14a5d6ad92d5 1726 TimerStop( &RxWindowTimer2 );
mluis 2:14a5d6ad92d5 1727 RxSlot = 1;
mluis 2:14a5d6ad92d5 1728
mluis 2:14a5d6ad92d5 1729 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 1730 // For higher datarates, we increase the number of symbols generating a Rx Timeout
mluis 4:37c12dbc8dc7 1731 if( ( Rx2Channel.Datarate == DR_3 ) || ( Rx2Channel.Datarate == DR_4 ) )
mluis 4:37c12dbc8dc7 1732 { // DR_4, DR_3
mluis 2:14a5d6ad92d5 1733 symbTimeout = 8;
mluis 2:14a5d6ad92d5 1734 }
mluis 4:37c12dbc8dc7 1735 else if( Rx2Channel.Datarate == DR_5 )
mluis 4:37c12dbc8dc7 1736 {
mluis 4:37c12dbc8dc7 1737 symbTimeout = 10;
mluis 4:37c12dbc8dc7 1738 }
mluis 4:37c12dbc8dc7 1739 else if( Rx2Channel.Datarate == DR_6 )
mluis 2:14a5d6ad92d5 1740 {// LoRa 250 kHz
mluis 2:14a5d6ad92d5 1741 bandwidth = 1;
mluis 4:37c12dbc8dc7 1742 symbTimeout = 14;
mluis 2:14a5d6ad92d5 1743 }
mluis 2:14a5d6ad92d5 1744 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 2:14a5d6ad92d5 1745 // For higher datarates, we increase the number of symbols generating a Rx Timeout
mluis 4:37c12dbc8dc7 1746 switch( Rx2Channel.Datarate )
mluis 4:37c12dbc8dc7 1747 {
mluis 4:37c12dbc8dc7 1748 case DR_0: // SF10 - BW125
mluis 4:37c12dbc8dc7 1749 symbTimeout = 5;
mluis 4:37c12dbc8dc7 1750 break;
mluis 4:37c12dbc8dc7 1751
mluis 4:37c12dbc8dc7 1752 case DR_1: // SF9 - BW125
mluis 4:37c12dbc8dc7 1753 case DR_2: // SF8 - BW125
mluis 4:37c12dbc8dc7 1754 case DR_8: // SF12 - BW500
mluis 4:37c12dbc8dc7 1755 case DR_9: // SF11 - BW500
mluis 4:37c12dbc8dc7 1756 case DR_10: // SF10 - BW500
mluis 4:37c12dbc8dc7 1757 symbTimeout = 8;
mluis 4:37c12dbc8dc7 1758 break;
mluis 4:37c12dbc8dc7 1759
mluis 4:37c12dbc8dc7 1760 case DR_3: // SF7 - BW125
mluis 4:37c12dbc8dc7 1761 case DR_11: // SF9 - BW500
mluis 4:37c12dbc8dc7 1762 symbTimeout = 10;
mluis 4:37c12dbc8dc7 1763 break;
mluis 4:37c12dbc8dc7 1764
mluis 4:37c12dbc8dc7 1765 case DR_4: // SF8 - BW500
mluis 4:37c12dbc8dc7 1766 case DR_12: // SF8 - BW500
mluis 4:37c12dbc8dc7 1767 symbTimeout = 14;
mluis 4:37c12dbc8dc7 1768 break;
mluis 4:37c12dbc8dc7 1769
mluis 4:37c12dbc8dc7 1770 case DR_13: // SF7 - BW500
mluis 4:37c12dbc8dc7 1771 symbTimeout = 16;
mluis 4:37c12dbc8dc7 1772 break;
mluis 4:37c12dbc8dc7 1773 default:
mluis 4:37c12dbc8dc7 1774 break;
mluis 2:14a5d6ad92d5 1775 }
mluis 2:14a5d6ad92d5 1776 if( Rx2Channel.Datarate >= DR_4 )
mluis 2:14a5d6ad92d5 1777 {// LoRa 500 kHz
mluis 2:14a5d6ad92d5 1778 bandwidth = 2;
mluis 2:14a5d6ad92d5 1779 }
mluis 2:14a5d6ad92d5 1780 #else
mluis 2:14a5d6ad92d5 1781 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 1782 #endif
mluis 2:14a5d6ad92d5 1783 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1784 {
mluis 2:14a5d6ad92d5 1785 RxWindowSetup( Rx2Channel.Frequency, Rx2Channel.Datarate, bandwidth, symbTimeout, false );
mluis 2:14a5d6ad92d5 1786 }
mluis 2:14a5d6ad92d5 1787 else
mluis 2:14a5d6ad92d5 1788 {
mluis 2:14a5d6ad92d5 1789 RxWindowSetup( Rx2Channel.Frequency, Rx2Channel.Datarate, bandwidth, symbTimeout, true );
mluis 2:14a5d6ad92d5 1790 }
mluis 2:14a5d6ad92d5 1791 }
mluis 2:14a5d6ad92d5 1792
mluis 2:14a5d6ad92d5 1793 static void OnAckTimeoutTimerEvent( void )
mluis 2:14a5d6ad92d5 1794 {
mluis 2:14a5d6ad92d5 1795 TimerStop( &AckTimeoutTimer );
mluis 2:14a5d6ad92d5 1796
mluis 2:14a5d6ad92d5 1797 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1798 {
mluis 2:14a5d6ad92d5 1799 AckTimeoutRetry = true;
mluis 2:14a5d6ad92d5 1800 LoRaMacState &= ~MAC_ACK_REQ;
mluis 2:14a5d6ad92d5 1801 }
mluis 2:14a5d6ad92d5 1802 if( LoRaMacDeviceClass == CLASS_C )
mluis 2:14a5d6ad92d5 1803 {
mluis 2:14a5d6ad92d5 1804 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1805 }
mluis 2:14a5d6ad92d5 1806 }
mluis 2:14a5d6ad92d5 1807
snelson 8:4816c8449bf2 1808 #if defined( USE_BAND_915 )
snelson 8:4816c8449bf2 1809 uint8_t GetNextJoinChannel( uint8_t* enabledChannels, uint8_t nbEnabledChannels )
snelson 8:4816c8449bf2 1810 {
snelson 8:4816c8449bf2 1811 int8_t channel = -1;
snelson 8:4816c8449bf2 1812 uint8_t block;
snelson 8:4816c8449bf2 1813 uint8_t i;
snelson 8:4816c8449bf2 1814
snelson 8:4816c8449bf2 1815 // Use 125KHz channel
snelson 8:4816c8449bf2 1816 if( ChannelsDatarate < DR_4 )
snelson 8:4816c8449bf2 1817 {
snelson 8:4816c8449bf2 1818 block = JoinBlock[NextJoinBlock];
snelson 8:4816c8449bf2 1819 NextJoinBlock = (NextJoinBlock + 1) % 8;
snelson 8:4816c8449bf2 1820
snelson 8:4816c8449bf2 1821 // If next block is greater than max block then randomly select the next block
snelson 8:4816c8449bf2 1822 if( block >= 8 )
snelson 8:4816c8449bf2 1823 block = randr(0, 7);
snelson 8:4816c8449bf2 1824
snelson 8:4816c8449bf2 1825 // Start search for next join channel at the selected block
snelson 8:4816c8449bf2 1826 for(i = 0; ( i < 8 ) && ( channel == -1 ); i++)
snelson 8:4816c8449bf2 1827 {
snelson 8:4816c8449bf2 1828 uint8_t curBlock = (block + i) % 8;
snelson 8:4816c8449bf2 1829 uint8_t chMask;
snelson 8:4816c8449bf2 1830
ubhat 14:60c5d589a070 1831 // Cycle through all blocks before using a previously used block
ubhat 14:60c5d589a070 1832 if( ( ( JoinBlocksRemaining & ( 1 << curBlock ) ) != 0) )
snelson 8:4816c8449bf2 1833 {
snelson 8:4816c8449bf2 1834 chMask = ( ChannelsMaskRemaining[curBlock/2] >> ( curBlock & 1 ? 8 : 0 ) ) & 0xff;
snelson 8:4816c8449bf2 1835 if( chMask != 0)
snelson 8:4816c8449bf2 1836 {
snelson 8:4816c8449bf2 1837 channel = randr(0, 7);
snelson 8:4816c8449bf2 1838 for(uint8_t i = 0; ( i < 8 ); i++)
ubhat 14:60c5d589a070 1839 {
snelson 8:4816c8449bf2 1840 if( ( chMask & ( 1 << channel ) ) != 0 )
snelson 8:4816c8449bf2 1841 {
ubhat 14:60c5d589a070 1842 uint16_t chMaskTmp = ( 1 << channel );
ubhat 14:60c5d589a070 1843 chMaskTmp = chMaskTmp << ( curBlock & 1 ? 8 : 0 );
snelson 8:4816c8449bf2 1844 channel = channel + ( curBlock * 8 );
ubhat 14:60c5d589a070 1845 JoinBlocksRemaining &= ~(1 << curBlock);
ubhat 14:60c5d589a070 1846 ChannelsMaskRemaining[curBlock/2] &= ~chMaskTmp;
ubhat 14:60c5d589a070 1847 if( ( JoinBlocksRemaining == 0 ) )
ubhat 14:60c5d589a070 1848 {
ubhat 14:60c5d589a070 1849 JoinBlocksRemaining = 0xFF;
ubhat 14:60c5d589a070 1850 }
snelson 8:4816c8449bf2 1851 break;
snelson 8:4816c8449bf2 1852 }
ubhat 14:60c5d589a070 1853 else
ubhat 14:60c5d589a070 1854 {
ubhat 14:60c5d589a070 1855 channel = ( channel + 1 ) % 8;
ubhat 14:60c5d589a070 1856 }
snelson 8:4816c8449bf2 1857 }
snelson 8:4816c8449bf2 1858 }
snelson 8:4816c8449bf2 1859 }
snelson 8:4816c8449bf2 1860 }
snelson 8:4816c8449bf2 1861
snelson 8:4816c8449bf2 1862 // No next channel case should never happen since ScheduleTx has already
snelson 8:4816c8449bf2 1863 // checked for at least one enabled channel and if none re-enabled all channels.
snelson 8:4816c8449bf2 1864 // But if we find ourselves without a channel then randomly select one
snelson 8:4816c8449bf2 1865 if ( i >= 8 )
snelson 8:4816c8449bf2 1866 {
snelson 8:4816c8449bf2 1867 channel = randr( 0, 63 );
snelson 8:4816c8449bf2 1868 }
snelson 8:4816c8449bf2 1869 }
snelson 8:4816c8449bf2 1870 // Use 500 KHz channel
snelson 8:4816c8449bf2 1871 else
snelson 8:4816c8449bf2 1872 {
snelson 8:4816c8449bf2 1873 channel = randr( 0, 7 );
snelson 8:4816c8449bf2 1874 for(i = 0; i < 8; i++)
snelson 8:4816c8449bf2 1875 {
snelson 8:4816c8449bf2 1876 uint8_t curChannel = (channel + i) % 8;
ubhat 14:60c5d589a070 1877
ubhat 14:60c5d589a070 1878 if( ( ( Join500KHzRemaining & ( 1 << curChannel ) ) != 0 ) )
snelson 8:4816c8449bf2 1879 {
ubhat 14:60c5d589a070 1880 Join500KHzRemaining &= ~(1 << curChannel);
snelson 8:4816c8449bf2 1881 channel = 64 + curChannel;
Shaun Nelson 18:5ccaea55a881 1882 if( Join500KHzRemaining == 0 )
ubhat 14:60c5d589a070 1883 {
ubhat 14:60c5d589a070 1884 Join500KHzRemaining = 0xFF;
ubhat 14:60c5d589a070 1885 }
snelson 8:4816c8449bf2 1886 break;
snelson 8:4816c8449bf2 1887 }
snelson 8:4816c8449bf2 1888 }
snelson 8:4816c8449bf2 1889 }
snelson 8:4816c8449bf2 1890
snelson 8:4816c8449bf2 1891 for( i = 0; i < nbEnabledChannels; i++ )
snelson 8:4816c8449bf2 1892 {
snelson 8:4816c8449bf2 1893 if( enabledChannels[i] == channel )
snelson 8:4816c8449bf2 1894 break;
snelson 8:4816c8449bf2 1895 }
snelson 8:4816c8449bf2 1896
snelson 8:4816c8449bf2 1897 if( i == nbEnabledChannels )
snelson 8:4816c8449bf2 1898 {
snelson 8:4816c8449bf2 1899 channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
snelson 8:4816c8449bf2 1900 }
snelson 8:4816c8449bf2 1901
snelson 8:4816c8449bf2 1902 LastJoinBlock = channel / 8;
snelson 8:4816c8449bf2 1903
snelson 8:4816c8449bf2 1904 return channel;
snelson 8:4816c8449bf2 1905 }
snelson 8:4816c8449bf2 1906 #endif
snelson 8:4816c8449bf2 1907
mluis 3:b9d87593a8ae 1908 static bool SetNextChannel( TimerTime_t* time )
mluis 2:14a5d6ad92d5 1909 {
mluis 0:91d1a7783bb9 1910 uint8_t nbEnabledChannels = 0;
mluis 3:b9d87593a8ae 1911 uint8_t delayTx = 0;
mluis 0:91d1a7783bb9 1912 uint8_t enabledChannels[LORA_MAX_NB_CHANNELS];
mluis 2:14a5d6ad92d5 1913 TimerTime_t nextTxDelay = ( TimerTime_t )( -1 );
mluis 0:91d1a7783bb9 1914
mluis 1:91e4e6c60d1e 1915 memset1( enabledChannels, 0, LORA_MAX_NB_CHANNELS );
mluis 0:91d1a7783bb9 1916
mluis 2:14a5d6ad92d5 1917 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 1918 if( CountNbEnabled125kHzChannels( ChannelsMaskRemaining ) == 0 )
mluis 3:b9d87593a8ae 1919 { // Restore default channels
mluis 3:b9d87593a8ae 1920 memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, 8 );
mluis 3:b9d87593a8ae 1921 }
mluis 3:b9d87593a8ae 1922 if( ( ChannelsDatarate >= DR_4 ) && ( ( ChannelsMaskRemaining[4] & 0x00FF ) == 0 ) )
mluis 3:b9d87593a8ae 1923 { // Make sure, that the channels are activated
mluis 3:b9d87593a8ae 1924 ChannelsMaskRemaining[4] = ChannelsMask[4];
mluis 3:b9d87593a8ae 1925 }
mluis 3:b9d87593a8ae 1926 #else
mluis 4:37c12dbc8dc7 1927 if( CountBits( ChannelsMask[0], 16 ) == 0 )
mluis 3:b9d87593a8ae 1928 {
mluis 3:b9d87593a8ae 1929 // Re-enable default channels, if no channel is enabled
mluis 3:b9d87593a8ae 1930 ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
mluis 2:14a5d6ad92d5 1931 }
mluis 2:14a5d6ad92d5 1932 #endif
mluis 2:14a5d6ad92d5 1933
mluis 0:91d1a7783bb9 1934 // Update Aggregated duty cycle
mluis 4:37c12dbc8dc7 1935 if( AggregatedTimeOff <= TimerGetElapsedTime( AggregatedLastTxDoneTime ) )
mluis 0:91d1a7783bb9 1936 {
mluis 0:91d1a7783bb9 1937 AggregatedTimeOff = 0;
mluis 2:14a5d6ad92d5 1938
mluis 2:14a5d6ad92d5 1939 // Update bands Time OFF
mluis 2:14a5d6ad92d5 1940 for( uint8_t i = 0; i < LORA_MAX_NB_BANDS; i++ )
mluis 0:91d1a7783bb9 1941 {
mluis 2:14a5d6ad92d5 1942 if( DutyCycleOn == true )
mluis 0:91d1a7783bb9 1943 {
mluis 4:37c12dbc8dc7 1944 if( Bands[i].TimeOff <= TimerGetElapsedTime( Bands[i].LastTxDoneTime ) )
mluis 2:14a5d6ad92d5 1945 {
mluis 2:14a5d6ad92d5 1946 Bands[i].TimeOff = 0;
mluis 2:14a5d6ad92d5 1947 }
mluis 2:14a5d6ad92d5 1948 if( Bands[i].TimeOff != 0 )
mluis 2:14a5d6ad92d5 1949 {
mluis 2:14a5d6ad92d5 1950 nextTxDelay = MIN( Bands[i].TimeOff -
mluis 4:37c12dbc8dc7 1951 TimerGetElapsedTime( Bands[i].LastTxDoneTime ),
mluis 2:14a5d6ad92d5 1952 nextTxDelay );
mluis 2:14a5d6ad92d5 1953 }
mluis 2:14a5d6ad92d5 1954 }
mluis 2:14a5d6ad92d5 1955 else
mluis 2:14a5d6ad92d5 1956 {
mluis 2:14a5d6ad92d5 1957 nextTxDelay = 0;
mluis 0:91d1a7783bb9 1958 Bands[i].TimeOff = 0;
mluis 0:91d1a7783bb9 1959 }
mluis 2:14a5d6ad92d5 1960 }
mluis 2:14a5d6ad92d5 1961
mluis 2:14a5d6ad92d5 1962 // Search how many channels are enabled
mluis 2:14a5d6ad92d5 1963 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS; i += 16, k++ )
mluis 2:14a5d6ad92d5 1964 {
mluis 2:14a5d6ad92d5 1965 for( uint8_t j = 0; j < 16; j++ )
mluis 0:91d1a7783bb9 1966 {
mluis 2:14a5d6ad92d5 1967 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 1968 if( ( ChannelsMaskRemaining[k] & ( 1 << j ) ) != 0 )
mluis 2:14a5d6ad92d5 1969 #else
mluis 2:14a5d6ad92d5 1970 if( ( ChannelsMask[k] & ( 1 << j ) ) != 0 )
mluis 2:14a5d6ad92d5 1971 #endif
mluis 2:14a5d6ad92d5 1972 {
mluis 2:14a5d6ad92d5 1973 if( Channels[i + j].Frequency == 0 )
mluis 2:14a5d6ad92d5 1974 { // Check if the channel is enabled
mluis 2:14a5d6ad92d5 1975 continue;
mluis 2:14a5d6ad92d5 1976 }
mluis 4:37c12dbc8dc7 1977 #if defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 )
mluis 4:37c12dbc8dc7 1978 if( IsLoRaMacNetworkJoined == false )
mluis 4:37c12dbc8dc7 1979 {
mluis 4:37c12dbc8dc7 1980 if( ( JOIN_CHANNELS & ( 1 << j ) ) == 0 )
mluis 4:37c12dbc8dc7 1981 {
mluis 4:37c12dbc8dc7 1982 continue;
mluis 4:37c12dbc8dc7 1983 }
mluis 4:37c12dbc8dc7 1984 }
mluis 4:37c12dbc8dc7 1985 #endif
mluis 2:14a5d6ad92d5 1986 if( ( ( Channels[i + j].DrRange.Fields.Min <= ChannelsDatarate ) &&
mluis 2:14a5d6ad92d5 1987 ( ChannelsDatarate <= Channels[i + j].DrRange.Fields.Max ) ) == false )
mluis 2:14a5d6ad92d5 1988 { // Check if the current channel selection supports the given datarate
mluis 2:14a5d6ad92d5 1989 continue;
mluis 2:14a5d6ad92d5 1990 }
mluis 2:14a5d6ad92d5 1991 if( Bands[Channels[i + j].Band].TimeOff > 0 )
mluis 2:14a5d6ad92d5 1992 { // Check if the band is available for transmission
mluis 3:b9d87593a8ae 1993 delayTx++;
mluis 2:14a5d6ad92d5 1994 continue;
mluis 2:14a5d6ad92d5 1995 }
mluis 2:14a5d6ad92d5 1996 enabledChannels[nbEnabledChannels++] = i + j;
mluis 2:14a5d6ad92d5 1997 }
mluis 2:14a5d6ad92d5 1998 }
mluis 2:14a5d6ad92d5 1999 }
mluis 2:14a5d6ad92d5 2000 }
mluis 2:14a5d6ad92d5 2001 else
mluis 2:14a5d6ad92d5 2002 {
mluis 3:b9d87593a8ae 2003 delayTx++;
mluis 4:37c12dbc8dc7 2004 nextTxDelay = AggregatedTimeOff - TimerGetElapsedTime( AggregatedLastTxDoneTime );
mluis 2:14a5d6ad92d5 2005 }
ubhat 14:60c5d589a070 2006
mluis 2:14a5d6ad92d5 2007 if( nbEnabledChannels > 0 )
mluis 2:14a5d6ad92d5 2008 {
snelson 8:4816c8449bf2 2009 #if defined( USE_BAND_915 )
snelson 8:4816c8449bf2 2010 if ( IsLoRaMacNetworkJoined == false )
snelson 8:4816c8449bf2 2011 {
snelson 8:4816c8449bf2 2012 Channel = GetNextJoinChannel(enabledChannels, nbEnabledChannels);
snelson 8:4816c8449bf2 2013 }
snelson 8:4816c8449bf2 2014 // Send first uplink on channel from same sub-band as the join
snelson 8:4816c8449bf2 2015 else if( ( UpLinkCounter == 1 ) && ( LastJoinBlock != -1 ) )
snelson 8:4816c8449bf2 2016 {
snelson 8:4816c8449bf2 2017 uint8_t i;
snelson 8:4816c8449bf2 2018 uint8_t blockFirstChannel = LastJoinBlock*8;
snelson 8:4816c8449bf2 2019
snelson 8:4816c8449bf2 2020 Channel = randr( blockFirstChannel, blockFirstChannel + 7 );
snelson 8:4816c8449bf2 2021
snelson 8:4816c8449bf2 2022 // Check channel is enabled
snelson 8:4816c8449bf2 2023 for( i = 0; i < nbEnabledChannels; i++ )
snelson 8:4816c8449bf2 2024 {
snelson 8:4816c8449bf2 2025 if( Channel == enabledChannels[i] )
snelson 8:4816c8449bf2 2026 break;
snelson 8:4816c8449bf2 2027 }
snelson 8:4816c8449bf2 2028
snelson 8:4816c8449bf2 2029 // If channel is not enabled fallback to selecting from the list of
snelson 8:4816c8449bf2 2030 // enabled channels
snelson 8:4816c8449bf2 2031 if (i == nbEnabledChannels)
snelson 8:4816c8449bf2 2032 {
snelson 8:4816c8449bf2 2033 Channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
snelson 8:4816c8449bf2 2034 }
snelson 8:4816c8449bf2 2035 }
snelson 8:4816c8449bf2 2036 else
snelson 8:4816c8449bf2 2037 #endif
snelson 8:4816c8449bf2 2038 {
snelson 8:4816c8449bf2 2039 Channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
snelson 8:4816c8449bf2 2040 }
snelson 8:4816c8449bf2 2041
mluis 2:14a5d6ad92d5 2042 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 2043 if( Channel < ( LORA_MAX_NB_CHANNELS - 8 ) )
mluis 3:b9d87593a8ae 2044 {
mluis 3:b9d87593a8ae 2045 DisableChannelInMask( Channel, ChannelsMaskRemaining );
mluis 3:b9d87593a8ae 2046 }
mluis 2:14a5d6ad92d5 2047 #endif
mluis 3:b9d87593a8ae 2048 *time = 0;
mluis 3:b9d87593a8ae 2049 return true;
mluis 2:14a5d6ad92d5 2050 }
mluis 2:14a5d6ad92d5 2051 else
mluis 2:14a5d6ad92d5 2052 {
mluis 3:b9d87593a8ae 2053 if( delayTx > 0 )
mluis 3:b9d87593a8ae 2054 {
mluis 3:b9d87593a8ae 2055 // Delay transmission due to AggregatedTimeOff or to a band time off
mluis 3:b9d87593a8ae 2056 *time = nextTxDelay;
mluis 3:b9d87593a8ae 2057 return true;
mluis 3:b9d87593a8ae 2058 }
mluis 3:b9d87593a8ae 2059 // Datarate not supported by any channel
mluis 3:b9d87593a8ae 2060 *time = 0;
mluis 3:b9d87593a8ae 2061 return false;
mluis 2:14a5d6ad92d5 2062 }
mluis 2:14a5d6ad92d5 2063 }
mluis 2:14a5d6ad92d5 2064
mluis 2:14a5d6ad92d5 2065 static void SetPublicNetwork( bool enable )
mluis 2:14a5d6ad92d5 2066 {
mluis 2:14a5d6ad92d5 2067 PublicNetwork = enable;
mluis 2:14a5d6ad92d5 2068 Radio.SetModem( MODEM_LORA );
mluis 2:14a5d6ad92d5 2069 if( PublicNetwork == true )
mluis 2:14a5d6ad92d5 2070 {
mluis 2:14a5d6ad92d5 2071 // Change LoRa modem SyncWord
mluis 2:14a5d6ad92d5 2072 Radio.Write( REG_LR_SYNCWORD, LORA_MAC_PUBLIC_SYNCWORD );
mluis 2:14a5d6ad92d5 2073 }
mluis 2:14a5d6ad92d5 2074 else
mluis 2:14a5d6ad92d5 2075 {
mluis 2:14a5d6ad92d5 2076 // Change LoRa modem SyncWord
mluis 2:14a5d6ad92d5 2077 Radio.Write( REG_LR_SYNCWORD, LORA_MAC_PRIVATE_SYNCWORD );
mluis 2:14a5d6ad92d5 2078 }
mluis 2:14a5d6ad92d5 2079 }
mluis 2:14a5d6ad92d5 2080
mluis 2:14a5d6ad92d5 2081 static void RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous )
mluis 2:14a5d6ad92d5 2082 {
mluis 2:14a5d6ad92d5 2083 uint8_t downlinkDatarate = Datarates[datarate];
mluis 2:14a5d6ad92d5 2084 RadioModems_t modem;
mluis 2:14a5d6ad92d5 2085
mluis 2:14a5d6ad92d5 2086 if( Radio.GetStatus( ) == RF_IDLE )
mluis 2:14a5d6ad92d5 2087 {
mluis 2:14a5d6ad92d5 2088 Radio.SetChannel( freq );
mluis 2:14a5d6ad92d5 2089
mluis 2:14a5d6ad92d5 2090 // Store downlink datarate
mluis 2:14a5d6ad92d5 2091 McpsIndication.RxDatarate = ( uint8_t ) datarate;
mluis 2:14a5d6ad92d5 2092
mluis 2:14a5d6ad92d5 2093 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 2094 if( datarate == DR_7 )
mluis 2:14a5d6ad92d5 2095 {
mluis 2:14a5d6ad92d5 2096 modem = MODEM_FSK;
mluis 2:14a5d6ad92d5 2097 Radio.SetRxConfig( modem, 50e3, downlinkDatarate * 1e3, 0, 83.333e3, 5, 0, false, 0, true, 0, 0, false, rxContinuous );
mluis 2:14a5d6ad92d5 2098 }
mluis 2:14a5d6ad92d5 2099 else
mluis 2:14a5d6ad92d5 2100 {
mluis 2:14a5d6ad92d5 2101 modem = MODEM_LORA;
mluis 2:14a5d6ad92d5 2102 Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
mluis 2:14a5d6ad92d5 2103 }
mluis 2:14a5d6ad92d5 2104 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 2105 modem = MODEM_LORA;
mluis 2:14a5d6ad92d5 2106 Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
mluis 2:14a5d6ad92d5 2107 #endif
mluis 2:14a5d6ad92d5 2108
mluis 2:14a5d6ad92d5 2109 if( RepeaterSupport == true )
mluis 2:14a5d6ad92d5 2110 {
mluis 4:37c12dbc8dc7 2111 Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateRepeater[datarate] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
mluis 2:14a5d6ad92d5 2112 }
mluis 2:14a5d6ad92d5 2113 else
mluis 2:14a5d6ad92d5 2114 {
mluis 4:37c12dbc8dc7 2115 Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarate[datarate] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
mluis 2:14a5d6ad92d5 2116 }
mluis 2:14a5d6ad92d5 2117
mluis 2:14a5d6ad92d5 2118 if( rxContinuous == false )
mluis 2:14a5d6ad92d5 2119 {
mluis 2:14a5d6ad92d5 2120 Radio.Rx( MaxRxWindow );
mluis 2:14a5d6ad92d5 2121 }
mluis 2:14a5d6ad92d5 2122 else
mluis 2:14a5d6ad92d5 2123 {
mluis 2:14a5d6ad92d5 2124 Radio.Rx( 0 ); // Continuous mode
mluis 2:14a5d6ad92d5 2125 }
mluis 2:14a5d6ad92d5 2126 }
mluis 2:14a5d6ad92d5 2127 }
mluis 2:14a5d6ad92d5 2128
shaunkrnelson 16:1e01c8728daa 2129 static bool Rx2FreqInRange( uint32_t freq )
shaunkrnelson 16:1e01c8728daa 2130 {
shaunkrnelson 16:1e01c8728daa 2131 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
shaunkrnelson 16:1e01c8728daa 2132 if( Radio.CheckRfFrequency( freq ) == true )
shaunkrnelson 16:1e01c8728daa 2133 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
shaunkrnelson 16:1e01c8728daa 2134 if( ( Radio.CheckRfFrequency( freq ) == true ) &&
shaunkrnelson 16:1e01c8728daa 2135 ( freq >= LORAMAC_FIRST_RX2_CHANNEL ) &&
shaunkrnelson 16:1e01c8728daa 2136 ( freq <= LORAMAC_LAST_RX2_CHANNEL ) &&
shaunkrnelson 16:1e01c8728daa 2137 ( ( ( freq - ( uint32_t ) LORAMAC_FIRST_RX2_CHANNEL ) % ( uint32_t ) LORAMAC_STEPWIDTH_RX2_CHANNEL ) == 0 ) )
shaunkrnelson 16:1e01c8728daa 2138 #endif
shaunkrnelson 16:1e01c8728daa 2139 {
shaunkrnelson 16:1e01c8728daa 2140 return true;
shaunkrnelson 16:1e01c8728daa 2141 }
shaunkrnelson 16:1e01c8728daa 2142 return false;
shaunkrnelson 16:1e01c8728daa 2143 }
shaunkrnelson 16:1e01c8728daa 2144
shaunkrnelson 16:1e01c8728daa 2145
mluis 2:14a5d6ad92d5 2146 static bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen )
mluis 2:14a5d6ad92d5 2147 {
mluis 2:14a5d6ad92d5 2148 uint16_t maxN = 0;
mluis 2:14a5d6ad92d5 2149 uint16_t payloadSize = 0;
mluis 2:14a5d6ad92d5 2150
mluis 2:14a5d6ad92d5 2151 // Get the maximum payload length
mluis 2:14a5d6ad92d5 2152 if( RepeaterSupport == true )
mluis 2:14a5d6ad92d5 2153 {
mluis 2:14a5d6ad92d5 2154 maxN = MaxPayloadOfDatarateRepeater[datarate];
mluis 2:14a5d6ad92d5 2155 }
mluis 2:14a5d6ad92d5 2156 else
mluis 2:14a5d6ad92d5 2157 {
mluis 2:14a5d6ad92d5 2158 maxN = MaxPayloadOfDatarate[datarate];
mluis 2:14a5d6ad92d5 2159 }
mluis 2:14a5d6ad92d5 2160
mluis 2:14a5d6ad92d5 2161 // Calculate the resulting payload size
mluis 2:14a5d6ad92d5 2162 payloadSize = ( lenN + fOptsLen );
mluis 2:14a5d6ad92d5 2163
mluis 2:14a5d6ad92d5 2164 // Validation of the application payload size
mluis 2:14a5d6ad92d5 2165 if( ( payloadSize <= maxN ) && ( payloadSize <= LORAMAC_PHY_MAXPAYLOAD ) )
mluis 2:14a5d6ad92d5 2166 {
mluis 2:14a5d6ad92d5 2167 return true;
mluis 2:14a5d6ad92d5 2168 }
mluis 2:14a5d6ad92d5 2169 return false;
mluis 2:14a5d6ad92d5 2170 }
mluis 2:14a5d6ad92d5 2171
mluis 4:37c12dbc8dc7 2172 static uint8_t CountBits( uint16_t mask, uint8_t nbBits )
mluis 4:37c12dbc8dc7 2173 {
mluis 4:37c12dbc8dc7 2174 uint8_t nbActiveBits = 0;
mluis 4:37c12dbc8dc7 2175
mluis 4:37c12dbc8dc7 2176 for( uint8_t j = 0; j < nbBits; j++ )
mluis 4:37c12dbc8dc7 2177 {
mluis 4:37c12dbc8dc7 2178 if( ( mask & ( 1 << j ) ) == ( 1 << j ) )
mluis 4:37c12dbc8dc7 2179 {
mluis 4:37c12dbc8dc7 2180 nbActiveBits++;
mluis 4:37c12dbc8dc7 2181 }
mluis 4:37c12dbc8dc7 2182 }
mluis 4:37c12dbc8dc7 2183 return nbActiveBits;
mluis 4:37c12dbc8dc7 2184 }
mluis 4:37c12dbc8dc7 2185
mluis 2:14a5d6ad92d5 2186 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 2187 static uint8_t CountNbEnabled125kHzChannels( uint16_t *channelsMask )
mluis 2:14a5d6ad92d5 2188 {
mluis 2:14a5d6ad92d5 2189 uint8_t nb125kHzChannels = 0;
mluis 2:14a5d6ad92d5 2190
mluis 2:14a5d6ad92d5 2191 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS - 8; i += 16, k++ )
mluis 2:14a5d6ad92d5 2192 {
mluis 4:37c12dbc8dc7 2193 nb125kHzChannels += CountBits( channelsMask[k], 16 );
mluis 2:14a5d6ad92d5 2194 }
mluis 2:14a5d6ad92d5 2195
mluis 2:14a5d6ad92d5 2196 return nb125kHzChannels;
mluis 2:14a5d6ad92d5 2197 }
mluis 4:37c12dbc8dc7 2198
mluis 4:37c12dbc8dc7 2199 #if defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 2200 static void ReenableChannels( uint16_t mask, uint16_t* channelMask )
mluis 4:37c12dbc8dc7 2201 {
mluis 4:37c12dbc8dc7 2202 uint16_t blockMask = mask;
mluis 4:37c12dbc8dc7 2203
mluis 4:37c12dbc8dc7 2204 for( uint8_t i = 0, j = 0; i < 4; i++, j += 2 )
mluis 4:37c12dbc8dc7 2205 {
mluis 4:37c12dbc8dc7 2206 channelMask[i] = 0;
mluis 4:37c12dbc8dc7 2207 if( ( blockMask & ( 1 << j ) ) != 0 )
mluis 4:37c12dbc8dc7 2208 {
mluis 4:37c12dbc8dc7 2209 channelMask[i] |= 0x00FF;
mluis 4:37c12dbc8dc7 2210 }
mluis 4:37c12dbc8dc7 2211 if( ( blockMask & ( 1 << ( j + 1 ) ) ) != 0 )
mluis 4:37c12dbc8dc7 2212 {
mluis 4:37c12dbc8dc7 2213 channelMask[i] |= 0xFF00;
mluis 4:37c12dbc8dc7 2214 }
mluis 4:37c12dbc8dc7 2215 }
mluis 4:37c12dbc8dc7 2216 channelMask[4] = blockMask;
mluis 4:37c12dbc8dc7 2217 channelMask[5] = 0x0000;
mluis 4:37c12dbc8dc7 2218 }
mluis 4:37c12dbc8dc7 2219
mluis 4:37c12dbc8dc7 2220 static bool ValidateChannelMask( uint16_t* channelMask )
mluis 4:37c12dbc8dc7 2221 {
mluis 4:37c12dbc8dc7 2222 bool chanMaskState = false;
mluis 4:37c12dbc8dc7 2223 uint16_t block1 = 0;
mluis 4:37c12dbc8dc7 2224 uint16_t block2 = 0;
mluis 4:37c12dbc8dc7 2225 uint8_t index = 0;
mluis 4:37c12dbc8dc7 2226
mluis 4:37c12dbc8dc7 2227 for( uint8_t i = 0; i < 4; i++ )
mluis 4:37c12dbc8dc7 2228 {
mluis 4:37c12dbc8dc7 2229 block1 = channelMask[i] & 0x00FF;
mluis 4:37c12dbc8dc7 2230 block2 = channelMask[i] & 0xFF00;
mluis 4:37c12dbc8dc7 2231
mluis 4:37c12dbc8dc7 2232 if( ( CountBits( block1, 16 ) > 5 ) && ( chanMaskState == false ) )
mluis 4:37c12dbc8dc7 2233 {
mluis 4:37c12dbc8dc7 2234 channelMask[i] &= block1;
mluis 4:37c12dbc8dc7 2235 channelMask[4] = 1 << ( i * 2 );
mluis 4:37c12dbc8dc7 2236 chanMaskState = true;
mluis 4:37c12dbc8dc7 2237 index = i;
mluis 4:37c12dbc8dc7 2238 }
mluis 4:37c12dbc8dc7 2239 else if( ( CountBits( block2, 16 ) > 5 ) && ( chanMaskState == false ) )
mluis 4:37c12dbc8dc7 2240 {
mluis 4:37c12dbc8dc7 2241 channelMask[i] &= block2;
mluis 4:37c12dbc8dc7 2242 channelMask[4] = 1 << ( i * 2 + 1 );
mluis 4:37c12dbc8dc7 2243 chanMaskState = true;
mluis 4:37c12dbc8dc7 2244 index = i;
mluis 4:37c12dbc8dc7 2245 }
mluis 4:37c12dbc8dc7 2246 }
mluis 4:37c12dbc8dc7 2247
mluis 4:37c12dbc8dc7 2248 // Do only change the channel mask, if we have found a valid block.
mluis 4:37c12dbc8dc7 2249 if( chanMaskState == true )
mluis 4:37c12dbc8dc7 2250 {
mluis 4:37c12dbc8dc7 2251 for( uint8_t i = 0; i < 4; i++ )
mluis 4:37c12dbc8dc7 2252 {
mluis 4:37c12dbc8dc7 2253 if( i != index )
mluis 4:37c12dbc8dc7 2254 {
mluis 4:37c12dbc8dc7 2255 channelMask[i] = 0;
mluis 4:37c12dbc8dc7 2256 }
mluis 4:37c12dbc8dc7 2257 }
mluis 4:37c12dbc8dc7 2258 }
mluis 4:37c12dbc8dc7 2259 return chanMaskState;
mluis 4:37c12dbc8dc7 2260 }
mluis 4:37c12dbc8dc7 2261 #endif
mluis 2:14a5d6ad92d5 2262 #endif
mluis 2:14a5d6ad92d5 2263
mluis 2:14a5d6ad92d5 2264 static int8_t LimitTxPower( int8_t txPower )
mluis 2:14a5d6ad92d5 2265 {
mluis 2:14a5d6ad92d5 2266 int8_t resultTxPower = txPower;
mluis 2:14a5d6ad92d5 2267 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 2268 if( ( ChannelsDatarate == DR_4 ) ||
mluis 2:14a5d6ad92d5 2269 ( ( ChannelsDatarate >= DR_8 ) && ( ChannelsDatarate <= DR_13 ) ) )
mluis 2:14a5d6ad92d5 2270 {// Limit tx power to max 26dBm
mluis 2:14a5d6ad92d5 2271 resultTxPower = MAX( txPower, TX_POWER_26_DBM );
mluis 2:14a5d6ad92d5 2272 }
mluis 2:14a5d6ad92d5 2273 else
mluis 2:14a5d6ad92d5 2274 {
ubhat 20:92764fda7371 2275 if( CountNbEnabled125kHzChannels( ChannelsMask ) < FRQ_HOP_CHNLS_MIN )
mluis 2:14a5d6ad92d5 2276 {// Limit tx power to max 21dBm
mluis 2:14a5d6ad92d5 2277 resultTxPower = MAX( txPower, TX_POWER_20_DBM );
mluis 2:14a5d6ad92d5 2278 }
mluis 2:14a5d6ad92d5 2279 }
mluis 2:14a5d6ad92d5 2280 #endif
mluis 2:14a5d6ad92d5 2281 return resultTxPower;
mluis 2:14a5d6ad92d5 2282 }
mluis 2:14a5d6ad92d5 2283
mluis 2:14a5d6ad92d5 2284 static bool ValueInRange( int8_t value, int8_t min, int8_t max )
mluis 2:14a5d6ad92d5 2285 {
mluis 2:14a5d6ad92d5 2286 if( ( value >= min ) && ( value <= max ) )
mluis 2:14a5d6ad92d5 2287 {
mluis 2:14a5d6ad92d5 2288 return true;
mluis 2:14a5d6ad92d5 2289 }
mluis 2:14a5d6ad92d5 2290 return false;
mluis 2:14a5d6ad92d5 2291 }
mluis 2:14a5d6ad92d5 2292
mluis 2:14a5d6ad92d5 2293 static bool DisableChannelInMask( uint8_t id, uint16_t* mask )
mluis 2:14a5d6ad92d5 2294 {
mluis 2:14a5d6ad92d5 2295 uint8_t index = 0;
mluis 2:14a5d6ad92d5 2296 index = id / 16;
mluis 2:14a5d6ad92d5 2297
mluis 2:14a5d6ad92d5 2298 if( ( index > 4 ) || ( id >= LORA_MAX_NB_CHANNELS ) )
mluis 2:14a5d6ad92d5 2299 {
mluis 2:14a5d6ad92d5 2300 return false;
mluis 2:14a5d6ad92d5 2301 }
mluis 2:14a5d6ad92d5 2302
mluis 2:14a5d6ad92d5 2303 // Deactivate channel
mluis 2:14a5d6ad92d5 2304 mask[index] &= ~( 1 << ( id % 16 ) );
mluis 2:14a5d6ad92d5 2305
mluis 2:14a5d6ad92d5 2306 return true;
mluis 2:14a5d6ad92d5 2307 }
mluis 2:14a5d6ad92d5 2308
snelson 8:4816c8449bf2 2309 static bool AdrNextDr( bool adrEnabled, bool isTx, int8_t* datarateOut, int8_t* txpowerOut )
mluis 2:14a5d6ad92d5 2310 {
mluis 2:14a5d6ad92d5 2311 bool adrAckReq = false;
mluis 2:14a5d6ad92d5 2312 int8_t datarate = ChannelsDatarate;
snelson 8:4816c8449bf2 2313 int8_t txpower = ChannelsTxPower;
shaunkrnelson 24:69286d8b3962 2314
mluis 2:14a5d6ad92d5 2315 if( adrEnabled == true )
mluis 2:14a5d6ad92d5 2316 {
snelson 8:4816c8449bf2 2317 /* Request ADR Ack Request (including at lowest available datarate) */
snelson 8:4816c8449bf2 2318 adrAckReq = AdrAckCounter >= ADR_ACK_LIMIT ? true : false;
snelson 8:4816c8449bf2 2319
shaunkrnelson 24:69286d8b3962 2320 if( AdrAckCounter >= (ADR_ACK_LIMIT + ADR_ACK_DELAY) )
mluis 0:91d1a7783bb9 2321 {
snelson 8:4816c8449bf2 2322 // Step up power
snelson 8:4816c8449bf2 2323 txpower = LORAMAC_DEFAULT_TX_POWER;
snelson 8:4816c8449bf2 2324
shaunkrnelson 24:69286d8b3962 2325 if( ( AdrAckCounter % ADR_ACK_DELAY ) == 0 )
snelson 8:4816c8449bf2 2326 {
shaunkrnelson 24:69286d8b3962 2327 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
shaunkrnelson 24:69286d8b3962 2328 if( datarate > LORAMAC_TX_MIN_DATARATE )
shaunkrnelson 24:69286d8b3962 2329 {
shaunkrnelson 24:69286d8b3962 2330 datarate--;
shaunkrnelson 24:69286d8b3962 2331 }
shaunkrnelson 24:69286d8b3962 2332
shaunkrnelson 24:69286d8b3962 2333 if( datarate == LORAMAC_TX_MIN_DATARATE )
shaunkrnelson 24:69286d8b3962 2334 {
shaunkrnelson 24:69286d8b3962 2335 if( isTx == true )
shaunkrnelson 24:69286d8b3962 2336 {
shaunkrnelson 24:69286d8b3962 2337 // Re-enable default channels LC1, LC2, LC3
shaunkrnelson 24:69286d8b3962 2338 ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
shaunkrnelson 24:69286d8b3962 2339 }
shaunkrnelson 24:69286d8b3962 2340 }
shaunkrnelson 24:69286d8b3962 2341 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
shaunkrnelson 24:69286d8b3962 2342 if( datarate > LORAMAC_TX_MIN_DATARATE )
shaunkrnelson 24:69286d8b3962 2343 {
shaunkrnelson 24:69286d8b3962 2344 if ( datarate == DR_8)
shaunkrnelson 24:69286d8b3962 2345 datarate = DR_4;
shaunkrnelson 24:69286d8b3962 2346 else
shaunkrnelson 24:69286d8b3962 2347 datarate--;
shaunkrnelson 24:69286d8b3962 2348 }
shaunkrnelson 24:69286d8b3962 2349 else if( isTx == true )
shaunkrnelson 24:69286d8b3962 2350 {
mluis 3:b9d87593a8ae 2351 #if defined( USE_BAND_915 )
shaunkrnelson 24:69286d8b3962 2352 // Re-enable default channels
shaunkrnelson 24:69286d8b3962 2353 ChannelsMask[0] = 0xFFFF;
shaunkrnelson 24:69286d8b3962 2354 ChannelsMask[1] = 0xFFFF;
shaunkrnelson 24:69286d8b3962 2355 ChannelsMask[2] = 0xFFFF;
shaunkrnelson 24:69286d8b3962 2356 ChannelsMask[3] = 0xFFFF;
shaunkrnelson 24:69286d8b3962 2357 ChannelsMask[4] = 0x00FF;
shaunkrnelson 24:69286d8b3962 2358 ChannelsMask[5] = 0x0000;
mluis 3:b9d87593a8ae 2359 #else // defined( USE_BAND_915_HYBRID )
shaunkrnelson 24:69286d8b3962 2360 // Re-enable default channels
shaunkrnelson 24:69286d8b3962 2361 ChannelsMask[0] = 0x00FF;
shaunkrnelson 24:69286d8b3962 2362 ChannelsMask[1] = 0x0000;
shaunkrnelson 24:69286d8b3962 2363 ChannelsMask[2] = 0x0000;
shaunkrnelson 24:69286d8b3962 2364 ChannelsMask[3] = 0x0000;
shaunkrnelson 24:69286d8b3962 2365 ChannelsMask[4] = 0x0001;
shaunkrnelson 24:69286d8b3962 2366 ChannelsMask[5] = 0x0000;
mluis 3:b9d87593a8ae 2367 #endif
shaunkrnelson 24:69286d8b3962 2368 }
mluis 2:14a5d6ad92d5 2369 #else
mluis 2:14a5d6ad92d5 2370 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 2371 #endif
shaunkrnelson 24:69286d8b3962 2372 }
mluis 0:91d1a7783bb9 2373 }
mluis 0:91d1a7783bb9 2374 }
mluis 2:14a5d6ad92d5 2375
snelson 8:4816c8449bf2 2376 if(datarateOut != NULL)
snelson 8:4816c8449bf2 2377 *datarateOut = datarate;
snelson 8:4816c8449bf2 2378
snelson 8:4816c8449bf2 2379 if(txpowerOut != NULL)
snelson 8:4816c8449bf2 2380 *txpowerOut = txpower;
mluis 2:14a5d6ad92d5 2381
mluis 2:14a5d6ad92d5 2382 return adrAckReq;
shaunkrnelson 24:69286d8b3962 2383
mluis 0:91d1a7783bb9 2384 }
mluis 0:91d1a7783bb9 2385
mluis 2:14a5d6ad92d5 2386 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 )
mluis 0:91d1a7783bb9 2387 {
mluis 2:14a5d6ad92d5 2388 LoRaMacStatus_t status = LORAMAC_STATUS_BUSY;
Shaun Nelson 27:cda377b2a41f 2389 // The maximum buffer length must take MAC commands to re-send into account.
Shaun Nelson 27:cda377b2a41f 2390 uint8_t bufLen = LORA_MAC_COMMAND_MAX_LENGTH - MacCommandsBufferToRepeatIndex;
mluis 1:91e4e6c60d1e 2391
mluis 0:91d1a7783bb9 2392 switch( cmd )
mluis 0:91d1a7783bb9 2393 {
mluis 0:91d1a7783bb9 2394 case MOTE_MAC_LINK_CHECK_REQ:
Shaun Nelson 27:cda377b2a41f 2395 if( MacCommandsBufferIndex < bufLen )
mluis 1:91e4e6c60d1e 2396 {
mluis 1:91e4e6c60d1e 2397 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2398 // No payload for this command
mluis 2:14a5d6ad92d5 2399 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2400 }
mluis 0:91d1a7783bb9 2401 break;
mluis 0:91d1a7783bb9 2402 case MOTE_MAC_LINK_ADR_ANS:
Shaun Nelson 27:cda377b2a41f 2403 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
mluis 1:91e4e6c60d1e 2404 {
mluis 1:91e4e6c60d1e 2405 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2406 // Margin
mluis 1:91e4e6c60d1e 2407 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
mluis 2:14a5d6ad92d5 2408 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2409 }
mluis 0:91d1a7783bb9 2410 break;
mluis 0:91d1a7783bb9 2411 case MOTE_MAC_DUTY_CYCLE_ANS:
Shaun Nelson 27:cda377b2a41f 2412 if( MacCommandsBufferIndex < bufLen )
mluis 1:91e4e6c60d1e 2413 {
mluis 1:91e4e6c60d1e 2414 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2415 // No payload for this answer
mluis 2:14a5d6ad92d5 2416 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2417 }
mluis 0:91d1a7783bb9 2418 break;
mluis 0:91d1a7783bb9 2419 case MOTE_MAC_RX_PARAM_SETUP_ANS:
Shaun Nelson 27:cda377b2a41f 2420 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
mluis 1:91e4e6c60d1e 2421 {
mluis 1:91e4e6c60d1e 2422 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2423 // Status: Datarate ACK, Channel ACK
mluis 1:91e4e6c60d1e 2424 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
mluis 2:14a5d6ad92d5 2425 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2426 }
mluis 0:91d1a7783bb9 2427 break;
mluis 0:91d1a7783bb9 2428 case MOTE_MAC_DEV_STATUS_ANS:
Shaun Nelson 27:cda377b2a41f 2429 if( MacCommandsBufferIndex < ( bufLen - 2 ) )
mluis 1:91e4e6c60d1e 2430 {
mluis 1:91e4e6c60d1e 2431 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2432 // 1st byte Battery
mluis 1:91e4e6c60d1e 2433 // 2nd byte Margin
mluis 1:91e4e6c60d1e 2434 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
mluis 1:91e4e6c60d1e 2435 MacCommandsBuffer[MacCommandsBufferIndex++] = p2;
mluis 2:14a5d6ad92d5 2436 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2437 }
mluis 0:91d1a7783bb9 2438 break;
mluis 0:91d1a7783bb9 2439 case MOTE_MAC_NEW_CHANNEL_ANS:
Shaun Nelson 27:cda377b2a41f 2440 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
mluis 1:91e4e6c60d1e 2441 {
mluis 1:91e4e6c60d1e 2442 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2443 // Status: Datarate range OK, Channel frequency OK
mluis 1:91e4e6c60d1e 2444 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
mluis 2:14a5d6ad92d5 2445 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2446 }
mluis 0:91d1a7783bb9 2447 break;
mluis 0:91d1a7783bb9 2448 case MOTE_MAC_RX_TIMING_SETUP_ANS:
Shaun Nelson 27:cda377b2a41f 2449 if( MacCommandsBufferIndex < bufLen )
mluis 1:91e4e6c60d1e 2450 {
mluis 1:91e4e6c60d1e 2451 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2452 // No payload for this answer
mluis 2:14a5d6ad92d5 2453 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2454 }
mluis 0:91d1a7783bb9 2455 break;
mluis 0:91d1a7783bb9 2456 default:
mluis 2:14a5d6ad92d5 2457 return LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 0:91d1a7783bb9 2458 }
mluis 2:14a5d6ad92d5 2459 if( status == LORAMAC_STATUS_OK )
mluis 0:91d1a7783bb9 2460 {
mluis 0:91d1a7783bb9 2461 MacCommandsInNextTx = true;
mluis 0:91d1a7783bb9 2462 }
mluis 1:91e4e6c60d1e 2463 return status;
mluis 0:91d1a7783bb9 2464 }
mluis 0:91d1a7783bb9 2465
Shaun Nelson 27:cda377b2a41f 2466 static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut )
Shaun Nelson 27:cda377b2a41f 2467 {
Shaun Nelson 27:cda377b2a41f 2468 uint8_t i = 0;
Shaun Nelson 27:cda377b2a41f 2469 uint8_t cmdCount = 0;
Shaun Nelson 27:cda377b2a41f 2470
Shaun Nelson 27:cda377b2a41f 2471 if( ( cmdBufIn == NULL ) || ( cmdBufOut == NULL ) )
Shaun Nelson 27:cda377b2a41f 2472 {
Shaun Nelson 27:cda377b2a41f 2473 return 0;
Shaun Nelson 27:cda377b2a41f 2474 }
Shaun Nelson 27:cda377b2a41f 2475
Shaun Nelson 27:cda377b2a41f 2476 for( i = 0; i < length; i++ )
Shaun Nelson 27:cda377b2a41f 2477 {
Shaun Nelson 27:cda377b2a41f 2478 switch( cmdBufIn[i] )
Shaun Nelson 27:cda377b2a41f 2479 {
Shaun Nelson 27:cda377b2a41f 2480 case MOTE_MAC_RX_PARAM_SETUP_ANS:
Shaun Nelson 27:cda377b2a41f 2481 {
Shaun Nelson 27:cda377b2a41f 2482 cmdBufOut[cmdCount++] = cmdBufIn[i++];
Shaun Nelson 27:cda377b2a41f 2483 cmdBufOut[cmdCount++] = cmdBufIn[i++];
Shaun Nelson 27:cda377b2a41f 2484 cmdBufOut[cmdCount++] = cmdBufIn[i];
Shaun Nelson 27:cda377b2a41f 2485 break;
Shaun Nelson 27:cda377b2a41f 2486 }
Shaun Nelson 27:cda377b2a41f 2487 case MOTE_MAC_RX_TIMING_SETUP_ANS:
Shaun Nelson 27:cda377b2a41f 2488 {
Shaun Nelson 27:cda377b2a41f 2489 cmdBufOut[cmdCount++] = cmdBufIn[i];
Shaun Nelson 27:cda377b2a41f 2490 break;
Shaun Nelson 27:cda377b2a41f 2491 }
Shaun Nelson 27:cda377b2a41f 2492 default:
Shaun Nelson 27:cda377b2a41f 2493 break;
Shaun Nelson 27:cda377b2a41f 2494 }
Shaun Nelson 27:cda377b2a41f 2495 }
Shaun Nelson 27:cda377b2a41f 2496
Shaun Nelson 27:cda377b2a41f 2497 return cmdCount;
Shaun Nelson 27:cda377b2a41f 2498 }
Shaun Nelson 27:cda377b2a41f 2499
mluis 2:14a5d6ad92d5 2500 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr )
mluis 0:91d1a7783bb9 2501 {
snelson 11:507de2ecd2b3 2502 uint8_t validLinkAdrCmds = 0;
snelson 11:507de2ecd2b3 2503 int8_t txPower = 0;
snelson 11:507de2ecd2b3 2504 int8_t datarate = 0;
snelson 11:507de2ecd2b3 2505 uint8_t nbRep = 0;
snelson 11:507de2ecd2b3 2506 uint16_t channelsMask[6] = {0,0,0,0,0,0};
Shaun Nelson 18:5ccaea55a881 2507 uint8_t LinkAdrMacCmdBufferIndex = 0;
snelson 11:507de2ecd2b3 2508
mluis 0:91d1a7783bb9 2509 while( macIndex < commandsSize )
mluis 0:91d1a7783bb9 2510 {
mluis 0:91d1a7783bb9 2511 // Decode Frame MAC commands
mluis 0:91d1a7783bb9 2512 switch( payload[macIndex++] )
mluis 0:91d1a7783bb9 2513 {
mluis 0:91d1a7783bb9 2514 case SRV_MAC_LINK_CHECK_ANS:
mluis 2:14a5d6ad92d5 2515 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 2516 MlmeConfirm.DemodMargin = payload[macIndex++];
mluis 2:14a5d6ad92d5 2517 MlmeConfirm.NbGateways = payload[macIndex++];
mluis 0:91d1a7783bb9 2518 break;
mluis 0:91d1a7783bb9 2519 case SRV_MAC_LINK_ADR_REQ:
mluis 0:91d1a7783bb9 2520 {
snelson 11:507de2ecd2b3 2521 uint8_t chMaskCntl = 0;
Shaun Nelson 18:5ccaea55a881 2522 uint8_t status = 0x07;
mluis 0:91d1a7783bb9 2523 uint16_t chMask;
snelson 11:507de2ecd2b3 2524
mluis 0:91d1a7783bb9 2525 datarate = payload[macIndex++];
snelson 11:507de2ecd2b3 2526 txPower = datarate & 0x0F;
mluis 0:91d1a7783bb9 2527 datarate = ( datarate >> 4 ) & 0x0F;
mluis 0:91d1a7783bb9 2528
mluis 2:14a5d6ad92d5 2529 if( ( AdrCtrlOn == false ) &&
mluis 0:91d1a7783bb9 2530 ( ( ChannelsDatarate != datarate ) || ( ChannelsTxPower != txPower ) ) )
mluis 0:91d1a7783bb9 2531 { // ADR disabled don't handle ADR requests if server tries to change datarate or txpower
mluis 0:91d1a7783bb9 2532 // Answer the server with fail status
mluis 0:91d1a7783bb9 2533 // Power ACK = 0
mluis 0:91d1a7783bb9 2534 // Data rate ACK = 0
mluis 0:91d1a7783bb9 2535 // Channel mask = 0
mluis 0:91d1a7783bb9 2536 AddMacCommand( MOTE_MAC_LINK_ADR_ANS, 0, 0 );
mluis 1:91e4e6c60d1e 2537 macIndex += 3; // Skip over the remaining bytes of the request
mluis 0:91d1a7783bb9 2538 break;
mluis 0:91d1a7783bb9 2539 }
snelson 11:507de2ecd2b3 2540 else if ( validLinkAdrCmds++ == 0 )
snelson 11:507de2ecd2b3 2541 {
snelson 11:507de2ecd2b3 2542 // Initialize local copy of the channels mask array
snelson 11:507de2ecd2b3 2543 for(uint8_t i = 0; i < 6; i++ )
snelson 11:507de2ecd2b3 2544 {
snelson 11:507de2ecd2b3 2545 channelsMask[i] = ChannelsMask[i];
snelson 11:507de2ecd2b3 2546 }
snelson 11:507de2ecd2b3 2547 }
snelson 11:507de2ecd2b3 2548
snelson 11:507de2ecd2b3 2549
mluis 1:91e4e6c60d1e 2550 chMask = ( uint16_t )payload[macIndex++];
mluis 1:91e4e6c60d1e 2551 chMask |= ( uint16_t )payload[macIndex++] << 8;
mluis 0:91d1a7783bb9 2552
mluis 0:91d1a7783bb9 2553 nbRep = payload[macIndex++];
mluis 0:91d1a7783bb9 2554 chMaskCntl = ( nbRep >> 4 ) & 0x07;
mluis 0:91d1a7783bb9 2555 nbRep &= 0x0F;
mluis 0:91d1a7783bb9 2556 if( nbRep == 0 )
mluis 0:91d1a7783bb9 2557 {
mluis 0:91d1a7783bb9 2558 nbRep = 1;
mluis 0:91d1a7783bb9 2559 }
mluis 0:91d1a7783bb9 2560 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 0:91d1a7783bb9 2561 if( ( chMaskCntl == 0 ) && ( chMask == 0 ) )
mluis 0:91d1a7783bb9 2562 {
Shaun Nelson 18:5ccaea55a881 2563 status &= 0xFE; // Channel mask KO
mluis 0:91d1a7783bb9 2564 }
mluis 2:14a5d6ad92d5 2565 else if( ( ( chMaskCntl >= 1 ) && ( chMaskCntl <= 5 )) ||
mluis 2:14a5d6ad92d5 2566 ( chMaskCntl >= 7 ) )
mluis 0:91d1a7783bb9 2567 {
mluis 0:91d1a7783bb9 2568 // RFU
Shaun Nelson 18:5ccaea55a881 2569 status &= 0xFE; // Channel mask KO
mluis 0:91d1a7783bb9 2570 }
mluis 0:91d1a7783bb9 2571 else
mluis 0:91d1a7783bb9 2572 {
mluis 2:14a5d6ad92d5 2573 for( i = 0; i < LORA_MAX_NB_CHANNELS; i++ )
mluis 0:91d1a7783bb9 2574 {
mluis 0:91d1a7783bb9 2575 if( chMaskCntl == 6 )
mluis 0:91d1a7783bb9 2576 {
mluis 0:91d1a7783bb9 2577 if( Channels[i].Frequency != 0 )
mluis 0:91d1a7783bb9 2578 {
mluis 0:91d1a7783bb9 2579 chMask |= 1 << i;
mluis 0:91d1a7783bb9 2580 }
mluis 0:91d1a7783bb9 2581 }
mluis 0:91d1a7783bb9 2582 else
mluis 0:91d1a7783bb9 2583 {
mluis 0:91d1a7783bb9 2584 if( ( ( chMask & ( 1 << i ) ) != 0 ) &&
mluis 0:91d1a7783bb9 2585 ( Channels[i].Frequency == 0 ) )
mluis 0:91d1a7783bb9 2586 {// Trying to enable an undefined channel
shaunkrnelson 19:838d3e8dffc6 2587 status &= 0xFE; // Channel mask KO
mluis 0:91d1a7783bb9 2588 }
mluis 0:91d1a7783bb9 2589 }
mluis 0:91d1a7783bb9 2590 }
mluis 0:91d1a7783bb9 2591 channelsMask[0] = chMask;
mluis 0:91d1a7783bb9 2592 }
mluis 0:91d1a7783bb9 2593 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
Shaun Nelson 12:d09ccf69cc93 2594 if( chMaskCntl < 4 )
Shaun Nelson 12:d09ccf69cc93 2595 {
Shaun Nelson 12:d09ccf69cc93 2596 channelsMask[chMaskCntl] = chMask;
Shaun Nelson 12:d09ccf69cc93 2597 }
Shaun Nelson 12:d09ccf69cc93 2598 else if( chMaskCntl == 6 )
mluis 0:91d1a7783bb9 2599 {
mluis 0:91d1a7783bb9 2600 // Enable all 125 kHz channels
mluis 0:91d1a7783bb9 2601 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS - 8; i += 16, k++ )
mluis 0:91d1a7783bb9 2602 {
mluis 0:91d1a7783bb9 2603 for( uint8_t j = 0; j < 16; j++ )
mluis 0:91d1a7783bb9 2604 {
mluis 0:91d1a7783bb9 2605 if( Channels[i + j].Frequency != 0 )
mluis 0:91d1a7783bb9 2606 {
mluis 0:91d1a7783bb9 2607 channelsMask[k] |= 1 << j;
mluis 0:91d1a7783bb9 2608 }
mluis 0:91d1a7783bb9 2609 }
mluis 0:91d1a7783bb9 2610 }
snelson 8:4816c8449bf2 2611 // channel mask applied to 500 kHz channels
Shaun Nelson 12:d09ccf69cc93 2612 channelsMask[4] = chMask;
mluis 0:91d1a7783bb9 2613 }
mluis 0:91d1a7783bb9 2614 else if( chMaskCntl == 7 )
mluis 0:91d1a7783bb9 2615 {
mluis 0:91d1a7783bb9 2616 // Disable all 125 kHz channels
mluis 0:91d1a7783bb9 2617 channelsMask[0] = 0x0000;
mluis 0:91d1a7783bb9 2618 channelsMask[1] = 0x0000;
mluis 0:91d1a7783bb9 2619 channelsMask[2] = 0x0000;
mluis 0:91d1a7783bb9 2620 channelsMask[3] = 0x0000;
snelson 8:4816c8449bf2 2621
snelson 8:4816c8449bf2 2622 // channel mask applied to 500 kHz channels
Shaun Nelson 18:5ccaea55a881 2623 channelsMask[4] = chMask;
Shaun Nelson 18:5ccaea55a881 2624 }
Shaun Nelson 18:5ccaea55a881 2625 else
Shaun Nelson 18:5ccaea55a881 2626 {
Shaun Nelson 18:5ccaea55a881 2627 // RFU
Shaun Nelson 18:5ccaea55a881 2628 status &= 0xFE; // Channel mask KO
mluis 0:91d1a7783bb9 2629 }
mluis 0:91d1a7783bb9 2630 #else
mluis 0:91d1a7783bb9 2631 #error "Please define a frequency band in the compiler options."
mluis 0:91d1a7783bb9 2632 #endif
mluis 4:37c12dbc8dc7 2633 if( ValueInRange( datarate, LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) == false )
mluis 0:91d1a7783bb9 2634 {
Shaun Nelson 18:5ccaea55a881 2635 status &= 0xFD; // Datarate KO
mluis 0:91d1a7783bb9 2636 }
mluis 0:91d1a7783bb9 2637
mluis 0:91d1a7783bb9 2638 //
mluis 0:91d1a7783bb9 2639 // Remark MaxTxPower = 0 and MinTxPower = 5
mluis 0:91d1a7783bb9 2640 //
mluis 2:14a5d6ad92d5 2641 if( ValueInRange( txPower, LORAMAC_MAX_TX_POWER, LORAMAC_MIN_TX_POWER ) == false )
mluis 0:91d1a7783bb9 2642 {
Shaun Nelson 18:5ccaea55a881 2643 status &= 0xFB; // TxPower KO
Shaun Nelson 18:5ccaea55a881 2644 }
Shaun Nelson 18:5ccaea55a881 2645
Shaun Nelson 18:5ccaea55a881 2646
Shaun Nelson 18:5ccaea55a881 2647 // Save the mac command status location for possible channel mask KO later
Shaun Nelson 18:5ccaea55a881 2648 LinkAdrMacCmdBufferIndex = MacCommandsBufferIndex;
Shaun Nelson 18:5ccaea55a881 2649
Shaun Nelson 18:5ccaea55a881 2650 AddMacCommand( MOTE_MAC_LINK_ADR_ANS, status, 0 );
mluis 0:91d1a7783bb9 2651 }
mluis 0:91d1a7783bb9 2652 break;
mluis 0:91d1a7783bb9 2653 case SRV_MAC_DUTY_CYCLE_REQ:
mluis 0:91d1a7783bb9 2654 MaxDCycle = payload[macIndex++];
mluis 0:91d1a7783bb9 2655 AggregatedDCycle = 1 << MaxDCycle;
mluis 0:91d1a7783bb9 2656 AddMacCommand( MOTE_MAC_DUTY_CYCLE_ANS, 0, 0 );
mluis 0:91d1a7783bb9 2657 break;
mluis 0:91d1a7783bb9 2658 case SRV_MAC_RX_PARAM_SETUP_REQ:
mluis 0:91d1a7783bb9 2659 {
mluis 0:91d1a7783bb9 2660 uint8_t status = 0x07;
mluis 0:91d1a7783bb9 2661 int8_t datarate = 0;
mluis 0:91d1a7783bb9 2662 int8_t drOffset = 0;
mluis 0:91d1a7783bb9 2663 uint32_t freq = 0;
mluis 2:14a5d6ad92d5 2664
mluis 1:91e4e6c60d1e 2665 drOffset = ( payload[macIndex] >> 4 ) & 0x07;
mluis 1:91e4e6c60d1e 2666 datarate = payload[macIndex] & 0x0F;
mluis 1:91e4e6c60d1e 2667 macIndex++;
mluis 2:14a5d6ad92d5 2668
mluis 2:14a5d6ad92d5 2669 freq = ( uint32_t )payload[macIndex++];
mluis 0:91d1a7783bb9 2670 freq |= ( uint32_t )payload[macIndex++] << 8;
mluis 0:91d1a7783bb9 2671 freq |= ( uint32_t )payload[macIndex++] << 16;
mluis 0:91d1a7783bb9 2672 freq *= 100;
shaunkrnelson 16:1e01c8728daa 2673
shaunkrnelson 16:1e01c8728daa 2674 if( Rx2FreqInRange( freq ) == false )
ubhat 15:167ad88b7272 2675 {
ubhat 15:167ad88b7272 2676 status &= 0xFE; // Channel frequency KO
ubhat 15:167ad88b7272 2677 }
mluis 2:14a5d6ad92d5 2678
mluis 4:37c12dbc8dc7 2679 if( ValueInRange( datarate, LORAMAC_RX_MIN_DATARATE, LORAMAC_RX_MAX_DATARATE ) == false )
mluis 0:91d1a7783bb9 2680 {
mluis 0:91d1a7783bb9 2681 status &= 0xFD; // Datarate KO
mluis 0:91d1a7783bb9 2682 }
mluis 2:14a5d6ad92d5 2683 #if ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 2:14a5d6ad92d5 2684 if( ( ValueInRange( datarate, DR_5, DR_7 ) == true ) ||
mluis 2:14a5d6ad92d5 2685 ( datarate > DR_13 ) )
mluis 2:14a5d6ad92d5 2686 {
mluis 2:14a5d6ad92d5 2687 status &= 0xFD; // Datarate KO
mluis 2:14a5d6ad92d5 2688 }
mluis 2:14a5d6ad92d5 2689 #endif
mluis 2:14a5d6ad92d5 2690 if( ValueInRange( drOffset, LORAMAC_MIN_RX1_DR_OFFSET, LORAMAC_MAX_RX1_DR_OFFSET ) == false )
mluis 0:91d1a7783bb9 2691 {
mluis 0:91d1a7783bb9 2692 status &= 0xFB; // Rx1DrOffset range KO
mluis 0:91d1a7783bb9 2693 }
mluis 2:14a5d6ad92d5 2694
mluis 0:91d1a7783bb9 2695 if( ( status & 0x07 ) == 0x07 )
mluis 0:91d1a7783bb9 2696 {
mluis 0:91d1a7783bb9 2697 Rx2Channel.Datarate = datarate;
mluis 0:91d1a7783bb9 2698 Rx2Channel.Frequency = freq;
mluis 0:91d1a7783bb9 2699 Rx1DrOffset = drOffset;
mluis 0:91d1a7783bb9 2700 }
mluis 0:91d1a7783bb9 2701 AddMacCommand( MOTE_MAC_RX_PARAM_SETUP_ANS, status, 0 );
mluis 0:91d1a7783bb9 2702 }
mluis 0:91d1a7783bb9 2703 break;
mluis 0:91d1a7783bb9 2704 case SRV_MAC_DEV_STATUS_REQ:
mluis 1:91e4e6c60d1e 2705 {
mluis 1:91e4e6c60d1e 2706 uint8_t batteryLevel = BAT_LEVEL_NO_MEASURE;
mluis 1:91e4e6c60d1e 2707 if( ( LoRaMacCallbacks != NULL ) && ( LoRaMacCallbacks->GetBatteryLevel != NULL ) )
mluis 1:91e4e6c60d1e 2708 {
mluis 1:91e4e6c60d1e 2709 batteryLevel = LoRaMacCallbacks->GetBatteryLevel( );
mluis 1:91e4e6c60d1e 2710 }
mluis 2:14a5d6ad92d5 2711 AddMacCommand( MOTE_MAC_DEV_STATUS_ANS, batteryLevel, snr );
mluis 2:14a5d6ad92d5 2712 break;
mluis 1:91e4e6c60d1e 2713 }
mluis 0:91d1a7783bb9 2714 case SRV_MAC_NEW_CHANNEL_REQ:
mluis 0:91d1a7783bb9 2715 {
mluis 0:91d1a7783bb9 2716 uint8_t status = 0x03;
mluis 3:b9d87593a8ae 2717
mluis 2:14a5d6ad92d5 2718 #if ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 3:b9d87593a8ae 2719 status &= 0xFC; // Channel frequency and datarate KO
mluis 3:b9d87593a8ae 2720 macIndex += 5;
mluis 2:14a5d6ad92d5 2721 #else
mluis 0:91d1a7783bb9 2722 int8_t channelIndex = 0;
mluis 0:91d1a7783bb9 2723 ChannelParams_t chParam;
mluis 2:14a5d6ad92d5 2724
mluis 0:91d1a7783bb9 2725 channelIndex = payload[macIndex++];
mluis 0:91d1a7783bb9 2726 chParam.Frequency = ( uint32_t )payload[macIndex++];
mluis 0:91d1a7783bb9 2727 chParam.Frequency |= ( uint32_t )payload[macIndex++] << 8;
mluis 0:91d1a7783bb9 2728 chParam.Frequency |= ( uint32_t )payload[macIndex++] << 16;
mluis 0:91d1a7783bb9 2729 chParam.Frequency *= 100;
mluis 0:91d1a7783bb9 2730 chParam.DrRange.Value = payload[macIndex++];
mluis 2:14a5d6ad92d5 2731
mluis 3:b9d87593a8ae 2732 LoRaMacState |= MAC_TX_CONFIG;
mluis 3:b9d87593a8ae 2733 if( chParam.Frequency == 0 )
mluis 0:91d1a7783bb9 2734 {
mluis 3:b9d87593a8ae 2735 if( channelIndex < 3 )
mluis 3:b9d87593a8ae 2736 {
mluis 3:b9d87593a8ae 2737 status &= 0xFC;
mluis 3:b9d87593a8ae 2738 }
mluis 3:b9d87593a8ae 2739 else
mluis 3:b9d87593a8ae 2740 {
mluis 3:b9d87593a8ae 2741 if( LoRaMacChannelRemove( channelIndex ) != LORAMAC_STATUS_OK )
mluis 3:b9d87593a8ae 2742 {
mluis 3:b9d87593a8ae 2743 status &= 0xFC;
mluis 3:b9d87593a8ae 2744 }
mluis 3:b9d87593a8ae 2745 }
mluis 0:91d1a7783bb9 2746 }
mluis 3:b9d87593a8ae 2747 else
mluis 0:91d1a7783bb9 2748 {
mluis 3:b9d87593a8ae 2749 switch( LoRaMacChannelAdd( channelIndex, chParam ) )
mluis 3:b9d87593a8ae 2750 {
mluis 3:b9d87593a8ae 2751 case LORAMAC_STATUS_OK:
mluis 3:b9d87593a8ae 2752 {
mluis 3:b9d87593a8ae 2753 break;
mluis 3:b9d87593a8ae 2754 }
mluis 3:b9d87593a8ae 2755 case LORAMAC_STATUS_FREQUENCY_INVALID:
mluis 3:b9d87593a8ae 2756 {
mluis 3:b9d87593a8ae 2757 status &= 0xFE;
mluis 3:b9d87593a8ae 2758 break;
mluis 3:b9d87593a8ae 2759 }
mluis 3:b9d87593a8ae 2760 case LORAMAC_STATUS_DATARATE_INVALID:
mluis 3:b9d87593a8ae 2761 {
mluis 3:b9d87593a8ae 2762 status &= 0xFD;
mluis 3:b9d87593a8ae 2763 break;
mluis 3:b9d87593a8ae 2764 }
mluis 3:b9d87593a8ae 2765 case LORAMAC_STATUS_FREQ_AND_DR_INVALID:
mluis 3:b9d87593a8ae 2766 {
mluis 3:b9d87593a8ae 2767 status &= 0xFC;
mluis 3:b9d87593a8ae 2768 break;
mluis 3:b9d87593a8ae 2769 }
mluis 3:b9d87593a8ae 2770 default:
mluis 3:b9d87593a8ae 2771 {
mluis 3:b9d87593a8ae 2772 status &= 0xFC;
mluis 3:b9d87593a8ae 2773 break;
mluis 3:b9d87593a8ae 2774 }
mluis 3:b9d87593a8ae 2775 }
mluis 0:91d1a7783bb9 2776 }
mluis 3:b9d87593a8ae 2777 LoRaMacState &= ~MAC_TX_CONFIG;
mluis 2:14a5d6ad92d5 2778 #endif
mluis 0:91d1a7783bb9 2779 AddMacCommand( MOTE_MAC_NEW_CHANNEL_ANS, status, 0 );
mluis 0:91d1a7783bb9 2780 }
mluis 0:91d1a7783bb9 2781 break;
mluis 0:91d1a7783bb9 2782 case SRV_MAC_RX_TIMING_SETUP_REQ:
mluis 0:91d1a7783bb9 2783 {
mluis 0:91d1a7783bb9 2784 uint8_t delay = payload[macIndex++] & 0x0F;
mluis 2:14a5d6ad92d5 2785
mluis 0:91d1a7783bb9 2786 if( delay == 0 )
mluis 0:91d1a7783bb9 2787 {
mluis 0:91d1a7783bb9 2788 delay++;
mluis 0:91d1a7783bb9 2789 }
mluis 0:91d1a7783bb9 2790 ReceiveDelay1 = delay * 1e6;
mluis 0:91d1a7783bb9 2791 ReceiveDelay2 = ReceiveDelay1 + 1e6;
mluis 0:91d1a7783bb9 2792 AddMacCommand( MOTE_MAC_RX_TIMING_SETUP_ANS, 0, 0 );
mluis 0:91d1a7783bb9 2793 }
mluis 0:91d1a7783bb9 2794 break;
mluis 0:91d1a7783bb9 2795 default:
mluis 0:91d1a7783bb9 2796 // Unknown command. ABORT MAC commands processing
mluis 0:91d1a7783bb9 2797 return;
mluis 0:91d1a7783bb9 2798 }
mluis 0:91d1a7783bb9 2799 }
snelson 11:507de2ecd2b3 2800
snelson 11:507de2ecd2b3 2801 // Process accumulated Link ADR channel mask changes
snelson 11:507de2ecd2b3 2802 if( validLinkAdrCmds > 0 )
snelson 11:507de2ecd2b3 2803 {
Shaun Nelson 13:90a55bd536a1 2804 uint8_t drOkCounter = 0;
Shaun Nelson 18:5ccaea55a881 2805 uint8_t status = 0x07;
Shaun Nelson 13:90a55bd536a1 2806
snelson 11:507de2ecd2b3 2807 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
snelson 11:507de2ecd2b3 2808 for(uint8_t i=0; i< 6; i++)
snelson 11:507de2ecd2b3 2809 {
snelson 11:507de2ecd2b3 2810 for( uint8_t j = 0; j < 16; j++ )
snelson 11:507de2ecd2b3 2811 {
Shaun Nelson 13:90a55bd536a1 2812 if( ( channelsMask[i] & ( 1 << j ) ) != 0 )
Shaun Nelson 13:90a55bd536a1 2813 {
Shaun Nelson 13:90a55bd536a1 2814 if ( ( Channels[i * 16 + j].Frequency == 0 ) )
Shaun Nelson 13:90a55bd536a1 2815 {
Shaun Nelson 18:5ccaea55a881 2816 status &= 0xFE; // Channel mask KO
Shaun Nelson 13:90a55bd536a1 2817 break;
Shaun Nelson 13:90a55bd536a1 2818 }
Shaun Nelson 13:90a55bd536a1 2819 else if( ( datarate >= Channels[i * 16 + j].DrRange.Fields.Min ) &&
Shaun Nelson 13:90a55bd536a1 2820 ( datarate <= Channels[i * 16 + j].DrRange.Fields.Max ) )
Shaun Nelson 13:90a55bd536a1 2821 {
Shaun Nelson 13:90a55bd536a1 2822 drOkCounter++;
Shaun Nelson 13:90a55bd536a1 2823 }
snelson 11:507de2ecd2b3 2824 }
snelson 11:507de2ecd2b3 2825 }
snelson 11:507de2ecd2b3 2826 }
snelson 11:507de2ecd2b3 2827
Shaun Nelson 13:90a55bd536a1 2828 if ( drOkCounter == 0 )
Shaun Nelson 13:90a55bd536a1 2829 {
Shaun Nelson 18:5ccaea55a881 2830 status &= 0xFD; // Datarate KO
Shaun Nelson 13:90a55bd536a1 2831 }
Shaun Nelson 13:90a55bd536a1 2832
ubhat 20:92764fda7371 2833 if( ( CountNbEnabled125kHzChannels( channelsMask ) > 0 ) && ( CountNbEnabled125kHzChannels( channelsMask ) < HYBRD_CHNLS_MIN ) )
snelson 11:507de2ecd2b3 2834 {
Shaun Nelson 18:5ccaea55a881 2835 status &= 0xFE; // Channel mask KO
snelson 11:507de2ecd2b3 2836 }
snelson 11:507de2ecd2b3 2837
snelson 11:507de2ecd2b3 2838 #if defined( USE_BAND_915_HYBRID )
snelson 11:507de2ecd2b3 2839 if( ValidateChannelMask( channelsMask ) == false )
snelson 11:507de2ecd2b3 2840 {
Shaun Nelson 18:5ccaea55a881 2841 status &= 0xFE; // Channel mask KO
snelson 11:507de2ecd2b3 2842 }
snelson 11:507de2ecd2b3 2843 #endif
snelson 11:507de2ecd2b3 2844
snelson 11:507de2ecd2b3 2845 #endif
Shaun Nelson 18:5ccaea55a881 2846 if( ( status & 0x07 ) == 0x07 )
snelson 11:507de2ecd2b3 2847 {
snelson 11:507de2ecd2b3 2848 ChannelsDatarate = datarate;
ubhat 20:92764fda7371 2849 ChannelsDefaultDatarate = datarate;
snelson 11:507de2ecd2b3 2850 ChannelsTxPower = txPower;
snelson 11:507de2ecd2b3 2851
snelson 11:507de2ecd2b3 2852 ChannelsMask[0] = channelsMask[0];
snelson 11:507de2ecd2b3 2853 ChannelsMask[1] = channelsMask[1];
snelson 11:507de2ecd2b3 2854 ChannelsMask[2] = channelsMask[2];
snelson 11:507de2ecd2b3 2855 ChannelsMask[3] = channelsMask[3];
snelson 11:507de2ecd2b3 2856 ChannelsMask[4] = channelsMask[4];
snelson 11:507de2ecd2b3 2857 ChannelsMask[5] = channelsMask[5];
snelson 11:507de2ecd2b3 2858
Shaun Nelson 18:5ccaea55a881 2859 ChannelsNbRep = nbRep;
Shaun Nelson 18:5ccaea55a881 2860
Shaun Nelson 18:5ccaea55a881 2861 // Clear remaining channels mask
ubhat 17:fca344a6ff18 2862 memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, 10 );
snelson 11:507de2ecd2b3 2863 }
Shaun Nelson 18:5ccaea55a881 2864 // Channel Mask KO
Shaun Nelson 18:5ccaea55a881 2865 else
Shaun Nelson 18:5ccaea55a881 2866 {
Shaun Nelson 18:5ccaea55a881 2867 // Update the last Link ADR status with channel mask KO
Shaun Nelson 18:5ccaea55a881 2868 if( (LinkAdrMacCmdBufferIndex+1 ) < LORA_MAC_COMMAND_MAX_LENGTH )
Shaun Nelson 18:5ccaea55a881 2869 MacCommandsBuffer[LinkAdrMacCmdBufferIndex+1] &= status;
Shaun Nelson 18:5ccaea55a881 2870 }
snelson 11:507de2ecd2b3 2871 }
mluis 0:91d1a7783bb9 2872 }
mluis 0:91d1a7783bb9 2873
mluis 2:14a5d6ad92d5 2874 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize )
mluis 0:91d1a7783bb9 2875 {
mluis 2:14a5d6ad92d5 2876 LoRaMacFrameCtrl_t fCtrl;
mluis 2:14a5d6ad92d5 2877 LoRaMacStatus_t status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 2878
mluis 2:14a5d6ad92d5 2879 fCtrl.Value = 0;
mluis 2:14a5d6ad92d5 2880 fCtrl.Bits.FOptsLen = 0;
mluis 2:14a5d6ad92d5 2881 fCtrl.Bits.FPending = 0;
mluis 2:14a5d6ad92d5 2882 fCtrl.Bits.Ack = false;
mluis 2:14a5d6ad92d5 2883 fCtrl.Bits.AdrAckReq = false;
mluis 2:14a5d6ad92d5 2884 fCtrl.Bits.Adr = AdrCtrlOn;
mluis 2:14a5d6ad92d5 2885
mluis 2:14a5d6ad92d5 2886 // Prepare the frame
mluis 2:14a5d6ad92d5 2887 status = PrepareFrame( macHdr, &fCtrl, fPort, fBuffer, fBufferSize );
mluis 2:14a5d6ad92d5 2888
mluis 2:14a5d6ad92d5 2889 // Validate status
mluis 2:14a5d6ad92d5 2890 if( status != LORAMAC_STATUS_OK )
mluis 0:91d1a7783bb9 2891 {
mluis 2:14a5d6ad92d5 2892 return status;
mluis 0:91d1a7783bb9 2893 }
mluis 2:14a5d6ad92d5 2894
mluis 2:14a5d6ad92d5 2895 // Reset confirm parameters
mluis 2:14a5d6ad92d5 2896 McpsConfirm.NbRetries = 0;
mluis 2:14a5d6ad92d5 2897 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 2898 McpsConfirm.UpLinkCounter = UpLinkCounter;
mluis 2:14a5d6ad92d5 2899
mluis 2:14a5d6ad92d5 2900 status = ScheduleTx( );
mluis 2:14a5d6ad92d5 2901
mluis 2:14a5d6ad92d5 2902 return status;
mluis 0:91d1a7783bb9 2903 }
mluis 0:91d1a7783bb9 2904
mluis 2:14a5d6ad92d5 2905 static LoRaMacStatus_t ScheduleTx( )
mluis 0:91d1a7783bb9 2906 {
mluis 2:14a5d6ad92d5 2907 TimerTime_t dutyCycleTimeOff = 0;
mluis 2:14a5d6ad92d5 2908
mluis 2:14a5d6ad92d5 2909 // Check if the device is off
mluis 2:14a5d6ad92d5 2910 if( MaxDCycle == 255 )
mluis 2:14a5d6ad92d5 2911 {
mluis 2:14a5d6ad92d5 2912 return LORAMAC_STATUS_DEVICE_OFF;
mluis 2:14a5d6ad92d5 2913 }
mluis 2:14a5d6ad92d5 2914 if( MaxDCycle == 0 )
mluis 0:91d1a7783bb9 2915 {
mluis 2:14a5d6ad92d5 2916 AggregatedTimeOff = 0;
mluis 2:14a5d6ad92d5 2917 }
mluis 2:14a5d6ad92d5 2918
mluis 4:37c12dbc8dc7 2919 CalculateBackOff( LastTxChannel );
mluis 4:37c12dbc8dc7 2920
mluis 2:14a5d6ad92d5 2921 // Select channel
mluis 3:b9d87593a8ae 2922 while( SetNextChannel( &dutyCycleTimeOff ) == false )
mluis 3:b9d87593a8ae 2923 {
mluis 3:b9d87593a8ae 2924 // Set the default datarate
mluis 3:b9d87593a8ae 2925 ChannelsDatarate = ChannelsDefaultDatarate;
mluis 3:b9d87593a8ae 2926
mluis 3:b9d87593a8ae 2927 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 3:b9d87593a8ae 2928 // Re-enable default channels LC1, LC2, LC3
mluis 3:b9d87593a8ae 2929 ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
mluis 3:b9d87593a8ae 2930 #endif
mluis 3:b9d87593a8ae 2931 }
mluis 2:14a5d6ad92d5 2932
mluis 2:14a5d6ad92d5 2933 // Schedule transmission of frame
mluis 2:14a5d6ad92d5 2934 if( dutyCycleTimeOff == 0 )
mluis 2:14a5d6ad92d5 2935 {
mluis 2:14a5d6ad92d5 2936 // Try to send now
mluis 2:14a5d6ad92d5 2937 return SendFrameOnChannel( Channels[Channel] );
mluis 0:91d1a7783bb9 2938 }
mluis 0:91d1a7783bb9 2939 else
mluis 0:91d1a7783bb9 2940 {
mluis 2:14a5d6ad92d5 2941 // Send later - prepare timer
mluis 2:14a5d6ad92d5 2942 LoRaMacState |= MAC_TX_DELAYED;
mluis 2:14a5d6ad92d5 2943 TimerSetValue( &TxDelayedTimer, dutyCycleTimeOff );
mluis 2:14a5d6ad92d5 2944 TimerStart( &TxDelayedTimer );
mluis 2:14a5d6ad92d5 2945
mluis 2:14a5d6ad92d5 2946 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 2947 }
mluis 2:14a5d6ad92d5 2948 }
mluis 2:14a5d6ad92d5 2949
mluis 4:37c12dbc8dc7 2950 static void CalculateBackOff( uint8_t channel )
mluis 4:37c12dbc8dc7 2951 {
mluis 4:37c12dbc8dc7 2952 uint16_t dutyCycle = Bands[Channels[channel].Band].DCycle;
mluis 4:37c12dbc8dc7 2953
mluis 4:37c12dbc8dc7 2954 if( IsLoRaMacNetworkJoined == false )
mluis 4:37c12dbc8dc7 2955 {
mluis 4:37c12dbc8dc7 2956 #if defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 )
mluis 4:37c12dbc8dc7 2957 dutyCycle = JOIN_DC;
mluis 4:37c12dbc8dc7 2958 #endif
mluis 4:37c12dbc8dc7 2959 }
mluis 4:37c12dbc8dc7 2960
mluis 4:37c12dbc8dc7 2961 // Update Band Time OFF
mluis 4:37c12dbc8dc7 2962 if( DutyCycleOn == true )
mluis 4:37c12dbc8dc7 2963 {
mluis 4:37c12dbc8dc7 2964 Bands[Channels[channel].Band].TimeOff = TxTimeOnAir * dutyCycle - TxTimeOnAir;
mluis 4:37c12dbc8dc7 2965 }
mluis 4:37c12dbc8dc7 2966 else
mluis 4:37c12dbc8dc7 2967 {
mluis 4:37c12dbc8dc7 2968 Bands[Channels[channel].Band].TimeOff = 0;
mluis 4:37c12dbc8dc7 2969 }
mluis 4:37c12dbc8dc7 2970 // Update Aggregated Time OFF
mluis 4:37c12dbc8dc7 2971 AggregatedTimeOff = AggregatedTimeOff + ( TxTimeOnAir * AggregatedDCycle - TxTimeOnAir );
mluis 4:37c12dbc8dc7 2972 }
mluis 4:37c12dbc8dc7 2973
mluis 2:14a5d6ad92d5 2974 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize )
mluis 2:14a5d6ad92d5 2975 {
mluis 2:14a5d6ad92d5 2976 uint16_t i;
mluis 2:14a5d6ad92d5 2977 uint8_t pktHeaderLen = 0;
mluis 2:14a5d6ad92d5 2978 uint32_t mic = 0;
mluis 2:14a5d6ad92d5 2979 const void* payload = fBuffer;
mluis 2:14a5d6ad92d5 2980 uint8_t payloadSize = fBufferSize;
mluis 2:14a5d6ad92d5 2981 uint8_t framePort = fPort;
mluis 2:14a5d6ad92d5 2982
mluis 2:14a5d6ad92d5 2983 LoRaMacBufferPktLen = 0;
mluis 2:14a5d6ad92d5 2984
mluis 2:14a5d6ad92d5 2985 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 2986
mluis 2:14a5d6ad92d5 2987 if( fBuffer == NULL )
mluis 0:91d1a7783bb9 2988 {
mluis 2:14a5d6ad92d5 2989 fBufferSize = 0;
mluis 2:14a5d6ad92d5 2990 }
mluis 2:14a5d6ad92d5 2991
mluis 2:14a5d6ad92d5 2992 LoRaMacBuffer[pktHeaderLen++] = macHdr->Value;
mluis 2:14a5d6ad92d5 2993
mluis 2:14a5d6ad92d5 2994 switch( macHdr->Bits.MType )
mluis 2:14a5d6ad92d5 2995 {
mluis 2:14a5d6ad92d5 2996 case FRAME_TYPE_JOIN_REQ:
mluis 2:14a5d6ad92d5 2997 RxWindow1Delay = JoinAcceptDelay1 - RADIO_WAKEUP_TIME;
mluis 2:14a5d6ad92d5 2998 RxWindow2Delay = JoinAcceptDelay2 - RADIO_WAKEUP_TIME;
mluis 2:14a5d6ad92d5 2999
mluis 2:14a5d6ad92d5 3000 LoRaMacBufferPktLen = pktHeaderLen;
mluis 2:14a5d6ad92d5 3001
mluis 2:14a5d6ad92d5 3002 memcpyr( LoRaMacBuffer + LoRaMacBufferPktLen, LoRaMacAppEui, 8 );
mluis 2:14a5d6ad92d5 3003 LoRaMacBufferPktLen += 8;
mluis 2:14a5d6ad92d5 3004 memcpyr( LoRaMacBuffer + LoRaMacBufferPktLen, LoRaMacDevEui, 8 );
mluis 2:14a5d6ad92d5 3005 LoRaMacBufferPktLen += 8;
mluis 2:14a5d6ad92d5 3006
mluis 2:14a5d6ad92d5 3007 LoRaMacDevNonce = Radio.Random( );
mluis 2:14a5d6ad92d5 3008
mluis 2:14a5d6ad92d5 3009 LoRaMacBuffer[LoRaMacBufferPktLen++] = LoRaMacDevNonce & 0xFF;
mluis 2:14a5d6ad92d5 3010 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( LoRaMacDevNonce >> 8 ) & 0xFF;
mluis 2:14a5d6ad92d5 3011
mluis 2:14a5d6ad92d5 3012 LoRaMacJoinComputeMic( LoRaMacBuffer, LoRaMacBufferPktLen & 0xFF, LoRaMacAppKey, &mic );
mluis 2:14a5d6ad92d5 3013
mluis 2:14a5d6ad92d5 3014 LoRaMacBuffer[LoRaMacBufferPktLen++] = mic & 0xFF;
mluis 2:14a5d6ad92d5 3015 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 8 ) & 0xFF;
mluis 2:14a5d6ad92d5 3016 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 16 ) & 0xFF;
mluis 2:14a5d6ad92d5 3017 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 24 ) & 0xFF;
mluis 2:14a5d6ad92d5 3018
mluis 2:14a5d6ad92d5 3019 break;
mluis 2:14a5d6ad92d5 3020 case FRAME_TYPE_DATA_CONFIRMED_UP:
mluis 2:14a5d6ad92d5 3021 NodeAckRequested = true;
mluis 2:14a5d6ad92d5 3022 //Intentional falltrough
mluis 2:14a5d6ad92d5 3023 case FRAME_TYPE_DATA_UNCONFIRMED_UP:
mluis 2:14a5d6ad92d5 3024 if( IsLoRaMacNetworkJoined == false )
mluis 0:91d1a7783bb9 3025 {
mluis 2:14a5d6ad92d5 3026 return LORAMAC_STATUS_NO_NETWORK_JOINED; // No network has been joined yet
mluis 2:14a5d6ad92d5 3027 }
mluis 2:14a5d6ad92d5 3028
snelson 8:4816c8449bf2 3029 fCtrl->Bits.AdrAckReq = AdrNextDr( fCtrl->Bits.Adr, true, &ChannelsDatarate, &ChannelsTxPower );
mluis 3:b9d87593a8ae 3030
mluis 2:14a5d6ad92d5 3031 if( ValidatePayloadLength( fBufferSize, ChannelsDatarate, MacCommandsBufferIndex ) == false )
mluis 2:14a5d6ad92d5 3032 {
mluis 2:14a5d6ad92d5 3033 return LORAMAC_STATUS_LENGTH_ERROR;
mluis 2:14a5d6ad92d5 3034 }
mluis 2:14a5d6ad92d5 3035
mluis 2:14a5d6ad92d5 3036 RxWindow1Delay = ReceiveDelay1 - RADIO_WAKEUP_TIME;
mluis 2:14a5d6ad92d5 3037 RxWindow2Delay = ReceiveDelay2 - RADIO_WAKEUP_TIME;
mluis 2:14a5d6ad92d5 3038
mluis 2:14a5d6ad92d5 3039 if( SrvAckRequested == true )
mluis 2:14a5d6ad92d5 3040 {
mluis 2:14a5d6ad92d5 3041 SrvAckRequested = false;
mluis 2:14a5d6ad92d5 3042 fCtrl->Bits.Ack = 1;
mluis 2:14a5d6ad92d5 3043 }
mluis 2:14a5d6ad92d5 3044
mluis 2:14a5d6ad92d5 3045 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr ) & 0xFF;
mluis 2:14a5d6ad92d5 3046 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 8 ) & 0xFF;
mluis 2:14a5d6ad92d5 3047 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 16 ) & 0xFF;
mluis 2:14a5d6ad92d5 3048 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 24 ) & 0xFF;
mluis 2:14a5d6ad92d5 3049
mluis 2:14a5d6ad92d5 3050 LoRaMacBuffer[pktHeaderLen++] = fCtrl->Value;
mluis 2:14a5d6ad92d5 3051
mluis 2:14a5d6ad92d5 3052 LoRaMacBuffer[pktHeaderLen++] = UpLinkCounter & 0xFF;
mluis 2:14a5d6ad92d5 3053 LoRaMacBuffer[pktHeaderLen++] = ( UpLinkCounter >> 8 ) & 0xFF;
mluis 2:14a5d6ad92d5 3054
Shaun Nelson 27:cda377b2a41f 3055 // Copy the MAC commands which must be re-send into the MAC command buffer
Shaun Nelson 27:cda377b2a41f 3056 memcpy1( &MacCommandsBuffer[MacCommandsBufferIndex], MacCommandsBufferToRepeat, MacCommandsBufferToRepeatIndex );
Shaun Nelson 27:cda377b2a41f 3057 MacCommandsBufferIndex += MacCommandsBufferToRepeatIndex;
Shaun Nelson 27:cda377b2a41f 3058
mluis 2:14a5d6ad92d5 3059 if( ( payload != NULL ) && ( payloadSize > 0 ) )
mluis 2:14a5d6ad92d5 3060 {
mluis 2:14a5d6ad92d5 3061 if( ( MacCommandsBufferIndex <= LORA_MAC_COMMAND_MAX_LENGTH ) && ( MacCommandsInNextTx == true ) )
mluis 1:91e4e6c60d1e 3062 {
mluis 2:14a5d6ad92d5 3063 fCtrl->Bits.FOptsLen += MacCommandsBufferIndex;
mluis 2:14a5d6ad92d5 3064
mluis 2:14a5d6ad92d5 3065 // Update FCtrl field with new value of OptionsLength
mluis 2:14a5d6ad92d5 3066 LoRaMacBuffer[0x05] = fCtrl->Value;
mluis 2:14a5d6ad92d5 3067 for( i = 0; i < MacCommandsBufferIndex; i++ )
mluis 0:91d1a7783bb9 3068 {
mluis 2:14a5d6ad92d5 3069 LoRaMacBuffer[pktHeaderLen++] = MacCommandsBuffer[i];
mluis 0:91d1a7783bb9 3070 }
mluis 0:91d1a7783bb9 3071 }
mluis 0:91d1a7783bb9 3072 }
mluis 0:91d1a7783bb9 3073 else
mluis 0:91d1a7783bb9 3074 {
mluis 2:14a5d6ad92d5 3075 if( ( MacCommandsBufferIndex > 0 ) && ( MacCommandsInNextTx ) )
mluis 0:91d1a7783bb9 3076 {
mluis 2:14a5d6ad92d5 3077 payloadSize = MacCommandsBufferIndex;
mluis 2:14a5d6ad92d5 3078 payload = MacCommandsBuffer;
mluis 2:14a5d6ad92d5 3079 framePort = 0;
mluis 0:91d1a7783bb9 3080 }
mluis 2:14a5d6ad92d5 3081 }
mluis 2:14a5d6ad92d5 3082 MacCommandsInNextTx = false;
Shaun Nelson 27:cda377b2a41f 3083 // Store MAC commands which must be re-send in case the device does not receive a downlink anymore
Shaun Nelson 27:cda377b2a41f 3084 MacCommandsBufferToRepeatIndex = ParseMacCommandsToRepeat( MacCommandsBuffer, MacCommandsBufferIndex, MacCommandsBufferToRepeat );
Shaun Nelson 27:cda377b2a41f 3085 if( MacCommandsBufferToRepeatIndex > 0 )
Shaun Nelson 27:cda377b2a41f 3086 {
Shaun Nelson 27:cda377b2a41f 3087 MacCommandsInNextTx = true;
Shaun Nelson 27:cda377b2a41f 3088 }
mluis 2:14a5d6ad92d5 3089 MacCommandsBufferIndex = 0;
mluis 2:14a5d6ad92d5 3090
mluis 2:14a5d6ad92d5 3091 if( ( payload != NULL ) && ( payloadSize > 0 ) )
mluis 2:14a5d6ad92d5 3092 {
mluis 2:14a5d6ad92d5 3093 LoRaMacBuffer[pktHeaderLen++] = framePort;
mluis 2:14a5d6ad92d5 3094
mluis 2:14a5d6ad92d5 3095 if( framePort == 0 )
mluis 0:91d1a7783bb9 3096 {
mluis 2:14a5d6ad92d5 3097 LoRaMacPayloadEncrypt( (uint8_t* ) payload, payloadSize, LoRaMacNwkSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, LoRaMacPayload );
mluis 0:91d1a7783bb9 3098 }
mluis 0:91d1a7783bb9 3099 else
mluis 0:91d1a7783bb9 3100 {
mluis 2:14a5d6ad92d5 3101 LoRaMacPayloadEncrypt( (uint8_t* ) payload, payloadSize, LoRaMacAppSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, LoRaMacPayload );
mluis 0:91d1a7783bb9 3102 }
mluis 2:14a5d6ad92d5 3103 memcpy1( LoRaMacBuffer + pktHeaderLen, LoRaMacPayload, payloadSize );
mluis 2:14a5d6ad92d5 3104 }
mluis 2:14a5d6ad92d5 3105 LoRaMacBufferPktLen = pktHeaderLen + payloadSize;
mluis 2:14a5d6ad92d5 3106
mluis 2:14a5d6ad92d5 3107 LoRaMacComputeMic( LoRaMacBuffer, LoRaMacBufferPktLen, LoRaMacNwkSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, &mic );
mluis 2:14a5d6ad92d5 3108
mluis 2:14a5d6ad92d5 3109 LoRaMacBuffer[LoRaMacBufferPktLen + 0] = mic & 0xFF;
mluis 2:14a5d6ad92d5 3110 LoRaMacBuffer[LoRaMacBufferPktLen + 1] = ( mic >> 8 ) & 0xFF;
mluis 2:14a5d6ad92d5 3111 LoRaMacBuffer[LoRaMacBufferPktLen + 2] = ( mic >> 16 ) & 0xFF;
mluis 2:14a5d6ad92d5 3112 LoRaMacBuffer[LoRaMacBufferPktLen + 3] = ( mic >> 24 ) & 0xFF;
mluis 2:14a5d6ad92d5 3113
mluis 2:14a5d6ad92d5 3114 LoRaMacBufferPktLen += LORAMAC_MFR_LEN;
mluis 2:14a5d6ad92d5 3115
mluis 2:14a5d6ad92d5 3116 break;
mluis 2:14a5d6ad92d5 3117 case FRAME_TYPE_PROPRIETARY:
mluis 2:14a5d6ad92d5 3118 if( ( fBuffer != NULL ) && ( fBufferSize > 0 ) )
mluis 2:14a5d6ad92d5 3119 {
mluis 2:14a5d6ad92d5 3120 memcpy1( LoRaMacBuffer + pktHeaderLen, ( uint8_t* ) fBuffer, fBufferSize );
mluis 2:14a5d6ad92d5 3121 LoRaMacBufferPktLen = pktHeaderLen + fBufferSize;
mluis 0:91d1a7783bb9 3122 }
mluis 0:91d1a7783bb9 3123 break;
mluis 0:91d1a7783bb9 3124 default:
mluis 2:14a5d6ad92d5 3125 return LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 0:91d1a7783bb9 3126 }
mluis 2:14a5d6ad92d5 3127
mluis 2:14a5d6ad92d5 3128 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 3129 }
mluis 0:91d1a7783bb9 3130
mluis 2:14a5d6ad92d5 3131 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel )
mluis 0:91d1a7783bb9 3132 {
mluis 2:14a5d6ad92d5 3133 int8_t datarate = Datarates[ChannelsDatarate];
mluis 4:37c12dbc8dc7 3134 int8_t txPowerIndex = 0;
mluis 2:14a5d6ad92d5 3135 int8_t txPower = 0;
mluis 2:14a5d6ad92d5 3136
mluis 4:37c12dbc8dc7 3137 txPowerIndex = LimitTxPower( ChannelsTxPower );
mluis 4:37c12dbc8dc7 3138 txPower = TxPowers[txPowerIndex];
mluis 2:14a5d6ad92d5 3139
mluis 2:14a5d6ad92d5 3140 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 2:14a5d6ad92d5 3141 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 2:14a5d6ad92d5 3142 McpsConfirm.Datarate = ChannelsDatarate;
mluis 4:37c12dbc8dc7 3143 McpsConfirm.TxPower = txPowerIndex;
mluis 2:14a5d6ad92d5 3144
mluis 2:14a5d6ad92d5 3145 Radio.SetChannel( channel.Frequency );
ubhat 14:60c5d589a070 3146
mluis 2:14a5d6ad92d5 3147 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 3148 if( ChannelsDatarate == DR_7 )
mluis 2:14a5d6ad92d5 3149 { // High Speed FSK channel
mluis 2:14a5d6ad92d5 3150 Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3151 Radio.SetTxConfig( MODEM_FSK, txPower, 25e3, 0, datarate * 1e3, 0, 5, false, true, 0, 0, false, 3e6 );
mluis 2:14a5d6ad92d5 3152 TxTimeOnAir = Radio.TimeOnAir( MODEM_FSK, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3153
mluis 0:91d1a7783bb9 3154 }
mluis 2:14a5d6ad92d5 3155 else if( ChannelsDatarate == DR_6 )
mluis 2:14a5d6ad92d5 3156 { // High speed LoRa channel
mluis 2:14a5d6ad92d5 3157 Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3158 Radio.SetTxConfig( MODEM_LORA, txPower, 0, 1, datarate, 1, 8, false, true, 0, 0, false, 3e6 );
mluis 2:14a5d6ad92d5 3159 TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
mluis 0:91d1a7783bb9 3160 }
mluis 1:91e4e6c60d1e 3161 else
mluis 2:14a5d6ad92d5 3162 { // Normal LoRa channel
mluis 2:14a5d6ad92d5 3163 Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3164 Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e6 );
mluis 2:14a5d6ad92d5 3165 TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
mluis 0:91d1a7783bb9 3166 }
mluis 2:14a5d6ad92d5 3167 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 3168 Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3169 if( ChannelsDatarate >= DR_4 )
mluis 2:14a5d6ad92d5 3170 { // High speed LoRa channel BW500 kHz
mluis 2:14a5d6ad92d5 3171 Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e6 );
mluis 2:14a5d6ad92d5 3172 TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
mluis 0:91d1a7783bb9 3173 }
mluis 1:91e4e6c60d1e 3174 else
mluis 2:14a5d6ad92d5 3175 { // Normal LoRa channel
mluis 2:14a5d6ad92d5 3176 Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e6 );
mluis 2:14a5d6ad92d5 3177 TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
mluis 0:91d1a7783bb9 3178 }
mluis 0:91d1a7783bb9 3179 #else
mluis 0:91d1a7783bb9 3180 #error "Please define a frequency band in the compiler options."
mluis 0:91d1a7783bb9 3181 #endif
mluis 2:14a5d6ad92d5 3182
mluis 2:14a5d6ad92d5 3183 // Store the time on air
mluis 2:14a5d6ad92d5 3184 McpsConfirm.TxTimeOnAir = TxTimeOnAir;
mluis 2:14a5d6ad92d5 3185 MlmeConfirm.TxTimeOnAir = TxTimeOnAir;
mluis 2:14a5d6ad92d5 3186
mluis 2:14a5d6ad92d5 3187 // Starts the MAC layer status check timer
mluis 3:b9d87593a8ae 3188 TimerSetValue( &MacStateCheckTimer, MAC_STATE_CHECK_TIMEOUT );
mluis 2:14a5d6ad92d5 3189 TimerStart( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 3190
mluis 2:14a5d6ad92d5 3191 // Send now
mluis 2:14a5d6ad92d5 3192 Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
mluis 2:14a5d6ad92d5 3193
mluis 2:14a5d6ad92d5 3194 LoRaMacState |= MAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 3195
mluis 2:14a5d6ad92d5 3196 return LORAMAC_STATUS_OK;
mluis 2:14a5d6ad92d5 3197 }
mluis 2:14a5d6ad92d5 3198
Shaun Nelson 27:cda377b2a41f 3199
mluis 2:14a5d6ad92d5 3200 LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t *primitives, LoRaMacCallback_t *callbacks )
mluis 2:14a5d6ad92d5 3201 {
mluis 2:14a5d6ad92d5 3202 if( primitives == NULL )
mluis 2:14a5d6ad92d5 3203 {
mluis 2:14a5d6ad92d5 3204 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3205 }
mluis 2:14a5d6ad92d5 3206
mluis 2:14a5d6ad92d5 3207 if( ( primitives->MacMcpsConfirm == NULL ) ||
mluis 2:14a5d6ad92d5 3208 ( primitives->MacMcpsIndication == NULL ) ||
mluis 2:14a5d6ad92d5 3209 ( primitives->MacMlmeConfirm == NULL ))
mluis 2:14a5d6ad92d5 3210 {
mluis 2:14a5d6ad92d5 3211 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3212 }
mluis 2:14a5d6ad92d5 3213
mluis 2:14a5d6ad92d5 3214 LoRaMacPrimitives = primitives;
mluis 2:14a5d6ad92d5 3215 LoRaMacCallbacks = callbacks;
mluis 2:14a5d6ad92d5 3216
mluis 2:14a5d6ad92d5 3217 LoRaMacFlags.Value = 0;
mluis 2:14a5d6ad92d5 3218
mluis 2:14a5d6ad92d5 3219 LoRaMacDeviceClass = CLASS_A;
mluis 2:14a5d6ad92d5 3220
mluis 2:14a5d6ad92d5 3221 UpLinkCounter = 1;
mluis 2:14a5d6ad92d5 3222 DownLinkCounter = 0;
mluis 2:14a5d6ad92d5 3223 AdrAckCounter = 0;
mluis 2:14a5d6ad92d5 3224
Shaun Nelson 27:cda377b2a41f 3225 ChannelsNbRepCounter = 0;
Shaun Nelson 27:cda377b2a41f 3226
Shaun Nelson 27:cda377b2a41f 3227 AckTimeoutRetries = 1;
Shaun Nelson 27:cda377b2a41f 3228 AckTimeoutRetriesCounter = 1;
Shaun Nelson 27:cda377b2a41f 3229 AckTimeoutRetry = false;
Shaun Nelson 27:cda377b2a41f 3230
Shaun Nelson 27:cda377b2a41f 3231 MaxDCycle = 0;
Shaun Nelson 27:cda377b2a41f 3232 AggregatedDCycle = 1;
Shaun Nelson 27:cda377b2a41f 3233
Shaun Nelson 27:cda377b2a41f 3234 MacCommandsBufferIndex = 0;
Shaun Nelson 27:cda377b2a41f 3235 MacCommandsBufferToRepeatIndex = 0;
Shaun Nelson 27:cda377b2a41f 3236
Shaun Nelson 27:cda377b2a41f 3237 IsRxWindowsEnabled = true;
Shaun Nelson 27:cda377b2a41f 3238
mluis 2:14a5d6ad92d5 3239 RepeaterSupport = false;
mluis 2:14a5d6ad92d5 3240 IsRxWindowsEnabled = true;
mluis 2:14a5d6ad92d5 3241 IsLoRaMacNetworkJoined = false;
mluis 2:14a5d6ad92d5 3242 LoRaMacState = MAC_IDLE;
mluis 2:14a5d6ad92d5 3243
Shaun Nelson 27:cda377b2a41f 3244 NodeAckRequested = false;
Shaun Nelson 27:cda377b2a41f 3245 SrvAckRequested = false;
Shaun Nelson 27:cda377b2a41f 3246 MacCommandsInNextTx = false;
Shaun Nelson 27:cda377b2a41f 3247
mluis 2:14a5d6ad92d5 3248 #if defined( USE_BAND_433 )
mluis 2:14a5d6ad92d5 3249 ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
mluis 2:14a5d6ad92d5 3250 #elif defined( USE_BAND_780 )
mluis 2:14a5d6ad92d5 3251 ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
mluis 2:14a5d6ad92d5 3252 #elif defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 3253 ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
mluis 2:14a5d6ad92d5 3254 #elif defined( USE_BAND_915 )
mluis 2:14a5d6ad92d5 3255 ChannelsMask[0] = 0xFFFF;
mluis 2:14a5d6ad92d5 3256 ChannelsMask[1] = 0xFFFF;
mluis 2:14a5d6ad92d5 3257 ChannelsMask[2] = 0xFFFF;
mluis 2:14a5d6ad92d5 3258 ChannelsMask[3] = 0xFFFF;
mluis 2:14a5d6ad92d5 3259 ChannelsMask[4] = 0x00FF;
mluis 2:14a5d6ad92d5 3260 ChannelsMask[5] = 0x0000;
mluis 2:14a5d6ad92d5 3261
mluis 2:14a5d6ad92d5 3262 memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, sizeof( ChannelsMask ) );
mluis 2:14a5d6ad92d5 3263 #elif defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 3264 ChannelsMask[0] = 0x00FF;
mluis 2:14a5d6ad92d5 3265 ChannelsMask[1] = 0x0000;
mluis 2:14a5d6ad92d5 3266 ChannelsMask[2] = 0x0000;
mluis 2:14a5d6ad92d5 3267 ChannelsMask[3] = 0x0000;
mluis 2:14a5d6ad92d5 3268 ChannelsMask[4] = 0x0001;
mluis 2:14a5d6ad92d5 3269 ChannelsMask[5] = 0x0000;
mluis 2:14a5d6ad92d5 3270
mluis 2:14a5d6ad92d5 3271 memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, sizeof( ChannelsMask ) );
mluis 2:14a5d6ad92d5 3272 #else
mluis 2:14a5d6ad92d5 3273 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 3274 #endif
mluis 2:14a5d6ad92d5 3275
mluis 2:14a5d6ad92d5 3276 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 3277 // 125 kHz channels
mluis 2:14a5d6ad92d5 3278 for( uint8_t i = 0; i < LORA_MAX_NB_CHANNELS - 8; i++ )
mluis 0:91d1a7783bb9 3279 {
mluis 2:14a5d6ad92d5 3280 Channels[i].Frequency = 902.3e6 + i * 200e3;
mluis 2:14a5d6ad92d5 3281 Channels[i].DrRange.Value = ( DR_3 << 4 ) | DR_0;
mluis 2:14a5d6ad92d5 3282 Channels[i].Band = 0;
mluis 2:14a5d6ad92d5 3283 }
mluis 2:14a5d6ad92d5 3284 // 500 kHz channels
mluis 2:14a5d6ad92d5 3285 for( uint8_t i = LORA_MAX_NB_CHANNELS - 8; i < LORA_MAX_NB_CHANNELS; i++ )
mluis 2:14a5d6ad92d5 3286 {
mluis 2:14a5d6ad92d5 3287 Channels[i].Frequency = 903.0e6 + ( i - ( LORA_MAX_NB_CHANNELS - 8 ) ) * 1.6e6;
mluis 2:14a5d6ad92d5 3288 Channels[i].DrRange.Value = ( DR_4 << 4 ) | DR_4;
mluis 2:14a5d6ad92d5 3289 Channels[i].Band = 0;
mluis 2:14a5d6ad92d5 3290 }
mluis 2:14a5d6ad92d5 3291 #endif
mluis 2:14a5d6ad92d5 3292
mluis 2:14a5d6ad92d5 3293 ChannelsTxPower = LORAMAC_DEFAULT_TX_POWER;
mluis 2:14a5d6ad92d5 3294 ChannelsDefaultDatarate = ChannelsDatarate = LORAMAC_DEFAULT_DATARATE;
mluis 2:14a5d6ad92d5 3295 ChannelsNbRep = 1;
mluis 2:14a5d6ad92d5 3296 ChannelsNbRepCounter = 0;
mluis 2:14a5d6ad92d5 3297
shaunkrnelson 21:4ccde8ee9ac8 3298 Rx2Channel.Frequency = 923300000;
shaunkrnelson 21:4ccde8ee9ac8 3299 Rx2Channel.Datarate = DR_8;
shaunkrnelson 21:4ccde8ee9ac8 3300 Rx1DrOffset = 0;
shaunkrnelson 21:4ccde8ee9ac8 3301
shaunkrnelson 21:4ccde8ee9ac8 3302
mluis 2:14a5d6ad92d5 3303 MaxDCycle = 0;
mluis 2:14a5d6ad92d5 3304 AggregatedDCycle = 1;
mluis 2:14a5d6ad92d5 3305 AggregatedLastTxDoneTime = 0;
mluis 2:14a5d6ad92d5 3306 AggregatedTimeOff = 0;
mluis 2:14a5d6ad92d5 3307
mluis 2:14a5d6ad92d5 3308 #if defined( USE_BAND_433 )
mluis 2:14a5d6ad92d5 3309 DutyCycleOn = false;
mluis 2:14a5d6ad92d5 3310 #elif defined( USE_BAND_780 )
mluis 2:14a5d6ad92d5 3311 DutyCycleOn = false;
mluis 2:14a5d6ad92d5 3312 #elif defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 3313 DutyCycleOn = true;
mluis 2:14a5d6ad92d5 3314 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 3315 DutyCycleOn = false;
mluis 2:14a5d6ad92d5 3316 #else
mluis 2:14a5d6ad92d5 3317 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 3318 #endif
mluis 2:14a5d6ad92d5 3319
mluis 2:14a5d6ad92d5 3320 MaxRxWindow = MAX_RX_WINDOW;
mluis 2:14a5d6ad92d5 3321 ReceiveDelay1 = RECEIVE_DELAY1;
mluis 2:14a5d6ad92d5 3322 ReceiveDelay2 = RECEIVE_DELAY2;
mluis 2:14a5d6ad92d5 3323 JoinAcceptDelay1 = JOIN_ACCEPT_DELAY1;
mluis 2:14a5d6ad92d5 3324 JoinAcceptDelay2 = JOIN_ACCEPT_DELAY2;
mluis 2:14a5d6ad92d5 3325
snelson 8:4816c8449bf2 3326 #if defined( USE_BAND_915 )
snelson 8:4816c8449bf2 3327 NextJoinBlock = 0;
snelson 8:4816c8449bf2 3328 LastJoinBlock = -1;
snelson 8:4816c8449bf2 3329 JoinBlocksRemaining = 0xff;
snelson 8:4816c8449bf2 3330 Join500KHzRemaining = 0xff;
snelson 8:4816c8449bf2 3331 #endif
snelson 8:4816c8449bf2 3332
snelson 8:4816c8449bf2 3333 LastJoinTxTime = 0;
snelson 8:4816c8449bf2 3334 JoinAggTimeOnAir = 0;
snelson 8:4816c8449bf2 3335
mluis 2:14a5d6ad92d5 3336 TimerInit( &MacStateCheckTimer, OnMacStateCheckTimerEvent );
mluis 2:14a5d6ad92d5 3337 TimerSetValue( &MacStateCheckTimer, MAC_STATE_CHECK_TIMEOUT );
mluis 2:14a5d6ad92d5 3338
mluis 2:14a5d6ad92d5 3339 TimerInit( &TxDelayedTimer, OnTxDelayedTimerEvent );
mluis 2:14a5d6ad92d5 3340 TimerInit( &RxWindowTimer1, OnRxWindow1TimerEvent );
mluis 2:14a5d6ad92d5 3341 TimerInit( &RxWindowTimer2, OnRxWindow2TimerEvent );
mluis 2:14a5d6ad92d5 3342 TimerInit( &AckTimeoutTimer, OnAckTimeoutTimerEvent );
mluis 2:14a5d6ad92d5 3343
mluis 2:14a5d6ad92d5 3344 // Initialize Radio driver
mluis 2:14a5d6ad92d5 3345 RadioEvents.TxDone = OnRadioTxDone;
mluis 2:14a5d6ad92d5 3346 RadioEvents.RxDone = OnRadioRxDone;
mluis 2:14a5d6ad92d5 3347 RadioEvents.RxError = OnRadioRxError;
mluis 2:14a5d6ad92d5 3348 RadioEvents.TxTimeout = OnRadioTxTimeout;
mluis 2:14a5d6ad92d5 3349 RadioEvents.RxTimeout = OnRadioRxTimeout;
mluis 2:14a5d6ad92d5 3350 Radio.Init( &RadioEvents );
mluis 2:14a5d6ad92d5 3351
mluis 2:14a5d6ad92d5 3352 // Random seed initialization
mluis 2:14a5d6ad92d5 3353 srand1( Radio.Random( ) );
mluis 2:14a5d6ad92d5 3354
mluis 2:14a5d6ad92d5 3355 // Initialize channel index.
mluis 2:14a5d6ad92d5 3356 Channel = LORA_MAX_NB_CHANNELS;
mluis 2:14a5d6ad92d5 3357
mluis 2:14a5d6ad92d5 3358 PublicNetwork = true;
mluis 2:14a5d6ad92d5 3359 SetPublicNetwork( PublicNetwork );
mluis 2:14a5d6ad92d5 3360 Radio.Sleep( );
mluis 2:14a5d6ad92d5 3361
mluis 2:14a5d6ad92d5 3362 return LORAMAC_STATUS_OK;
mluis 2:14a5d6ad92d5 3363 }
mluis 2:14a5d6ad92d5 3364
mluis 2:14a5d6ad92d5 3365 LoRaMacStatus_t LoRaMacQueryTxPossible( uint8_t size, LoRaMacTxInfo_t* txInfo )
mluis 2:14a5d6ad92d5 3366 {
Shaun Nelson 27:cda377b2a41f 3367 int8_t datarate = ChannelsDefaultDatarate;
Shaun Nelson 27:cda377b2a41f 3368 uint8_t fOptLen = MacCommandsBufferIndex + MacCommandsBufferToRepeatIndex;
mluis 2:14a5d6ad92d5 3369
mluis 2:14a5d6ad92d5 3370 if( txInfo == NULL )
mluis 2:14a5d6ad92d5 3371 {
mluis 2:14a5d6ad92d5 3372 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3373 }
mluis 2:14a5d6ad92d5 3374
snelson 8:4816c8449bf2 3375 AdrNextDr( AdrCtrlOn, false, &datarate, NULL );
mluis 2:14a5d6ad92d5 3376
mluis 2:14a5d6ad92d5 3377 if( RepeaterSupport == true )
mluis 2:14a5d6ad92d5 3378 {
mluis 2:14a5d6ad92d5 3379 txInfo->CurrentPayloadSize = MaxPayloadOfDatarateRepeater[datarate];
mluis 0:91d1a7783bb9 3380 }
mluis 0:91d1a7783bb9 3381 else
mluis 0:91d1a7783bb9 3382 {
mluis 2:14a5d6ad92d5 3383 txInfo->CurrentPayloadSize = MaxPayloadOfDatarate[datarate];
mluis 0:91d1a7783bb9 3384 }
mluis 2:14a5d6ad92d5 3385
Shaun Nelson 27:cda377b2a41f 3386 if( txInfo->CurrentPayloadSize >= fOptLen )
mluis 0:91d1a7783bb9 3387 {
Shaun Nelson 27:cda377b2a41f 3388 txInfo->MaxPossiblePayload = txInfo->CurrentPayloadSize - fOptLen;
mluis 0:91d1a7783bb9 3389 }
mluis 0:91d1a7783bb9 3390 else
mluis 0:91d1a7783bb9 3391 {
mluis 2:14a5d6ad92d5 3392 return LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR;
mluis 2:14a5d6ad92d5 3393 }
mluis 2:14a5d6ad92d5 3394
mluis 2:14a5d6ad92d5 3395 if( ValidatePayloadLength( size, datarate, 0 ) == false )
mluis 2:14a5d6ad92d5 3396 {
mluis 2:14a5d6ad92d5 3397 return LORAMAC_STATUS_LENGTH_ERROR;
mluis 0:91d1a7783bb9 3398 }
mluis 2:14a5d6ad92d5 3399
Shaun Nelson 27:cda377b2a41f 3400 if( ValidatePayloadLength( size, datarate, fOptLen ) == false )
mluis 0:91d1a7783bb9 3401 {
mluis 2:14a5d6ad92d5 3402 return LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR;
mluis 0:91d1a7783bb9 3403 }
mluis 0:91d1a7783bb9 3404
mluis 2:14a5d6ad92d5 3405 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 3406 }
mluis 0:91d1a7783bb9 3407
mluis 2:14a5d6ad92d5 3408 LoRaMacStatus_t LoRaMacMibGetRequestConfirm( MibRequestConfirm_t *mibGet )
mluis 0:91d1a7783bb9 3409 {
mluis 2:14a5d6ad92d5 3410 LoRaMacStatus_t status = LORAMAC_STATUS_OK;
mluis 2:14a5d6ad92d5 3411
mluis 2:14a5d6ad92d5 3412 if( mibGet == NULL )
mluis 2:14a5d6ad92d5 3413 {
mluis 2:14a5d6ad92d5 3414 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3415 }
mluis 2:14a5d6ad92d5 3416
mluis 2:14a5d6ad92d5 3417 switch( mibGet->Type )
mluis 0:91d1a7783bb9 3418 {
mluis 2:14a5d6ad92d5 3419 case MIB_DEVICE_CLASS:
mluis 2:14a5d6ad92d5 3420 {
mluis 2:14a5d6ad92d5 3421 mibGet->Param.Class = LoRaMacDeviceClass;
mluis 2:14a5d6ad92d5 3422 break;
mluis 2:14a5d6ad92d5 3423 }
mluis 2:14a5d6ad92d5 3424 case MIB_NETWORK_JOINED:
mluis 2:14a5d6ad92d5 3425 {
mluis 2:14a5d6ad92d5 3426 mibGet->Param.IsNetworkJoined = IsLoRaMacNetworkJoined;
mluis 2:14a5d6ad92d5 3427 break;
mluis 2:14a5d6ad92d5 3428 }
mluis 2:14a5d6ad92d5 3429 case MIB_ADR:
mluis 2:14a5d6ad92d5 3430 {
mluis 2:14a5d6ad92d5 3431 mibGet->Param.AdrEnable = AdrCtrlOn;
mluis 2:14a5d6ad92d5 3432 break;
mluis 2:14a5d6ad92d5 3433 }
mluis 2:14a5d6ad92d5 3434 case MIB_NET_ID:
mluis 2:14a5d6ad92d5 3435 {
mluis 2:14a5d6ad92d5 3436 mibGet->Param.NetID = LoRaMacNetID;
mluis 2:14a5d6ad92d5 3437 break;
mluis 2:14a5d6ad92d5 3438 }
mluis 2:14a5d6ad92d5 3439 case MIB_DEV_ADDR:
mluis 2:14a5d6ad92d5 3440 {
mluis 2:14a5d6ad92d5 3441 mibGet->Param.DevAddr = LoRaMacDevAddr;
mluis 2:14a5d6ad92d5 3442 break;
mluis 2:14a5d6ad92d5 3443 }
mluis 2:14a5d6ad92d5 3444 case MIB_NWK_SKEY:
mluis 2:14a5d6ad92d5 3445 {
mluis 2:14a5d6ad92d5 3446 mibGet->Param.NwkSKey = LoRaMacNwkSKey;
mluis 2:14a5d6ad92d5 3447 break;
mluis 2:14a5d6ad92d5 3448 }
mluis 2:14a5d6ad92d5 3449 case MIB_APP_SKEY:
mluis 2:14a5d6ad92d5 3450 {
mluis 2:14a5d6ad92d5 3451 mibGet->Param.AppSKey = LoRaMacAppSKey;
mluis 2:14a5d6ad92d5 3452 break;
mluis 2:14a5d6ad92d5 3453 }
mluis 2:14a5d6ad92d5 3454 case MIB_PUBLIC_NETWORK:
mluis 2:14a5d6ad92d5 3455 {
mluis 2:14a5d6ad92d5 3456 mibGet->Param.EnablePublicNetwork = PublicNetwork;
mluis 2:14a5d6ad92d5 3457 break;
mluis 2:14a5d6ad92d5 3458 }
mluis 2:14a5d6ad92d5 3459 case MIB_REPEATER_SUPPORT:
mluis 2:14a5d6ad92d5 3460 {
mluis 2:14a5d6ad92d5 3461 mibGet->Param.EnableRepeaterSupport = RepeaterSupport;
mluis 2:14a5d6ad92d5 3462 break;
mluis 2:14a5d6ad92d5 3463 }
mluis 2:14a5d6ad92d5 3464 case MIB_CHANNELS:
mluis 2:14a5d6ad92d5 3465 {
mluis 2:14a5d6ad92d5 3466 mibGet->Param.ChannelList = Channels;
mluis 2:14a5d6ad92d5 3467 break;
mluis 2:14a5d6ad92d5 3468 }
mluis 2:14a5d6ad92d5 3469 case MIB_RX2_CHANNEL:
mluis 2:14a5d6ad92d5 3470 {
mluis 2:14a5d6ad92d5 3471 mibGet->Param.Rx2Channel = Rx2Channel;
mluis 2:14a5d6ad92d5 3472 break;
mluis 2:14a5d6ad92d5 3473 }
mluis 2:14a5d6ad92d5 3474 case MIB_CHANNELS_MASK:
mluis 2:14a5d6ad92d5 3475 {
mluis 2:14a5d6ad92d5 3476 mibGet->Param.ChannelsMask = ChannelsMask;
mluis 2:14a5d6ad92d5 3477 break;
mluis 0:91d1a7783bb9 3478 }
mluis 2:14a5d6ad92d5 3479 case MIB_CHANNELS_NB_REP:
mluis 2:14a5d6ad92d5 3480 {
mluis 2:14a5d6ad92d5 3481 mibGet->Param.ChannelNbRep = ChannelsNbRep;
mluis 2:14a5d6ad92d5 3482 break;
mluis 2:14a5d6ad92d5 3483 }
mluis 2:14a5d6ad92d5 3484 case MIB_MAX_RX_WINDOW_DURATION:
mluis 2:14a5d6ad92d5 3485 {
mluis 2:14a5d6ad92d5 3486 mibGet->Param.MaxRxWindow = MaxRxWindow;
mluis 2:14a5d6ad92d5 3487 break;
mluis 2:14a5d6ad92d5 3488 }
mluis 2:14a5d6ad92d5 3489 case MIB_RECEIVE_DELAY_1:
mluis 2:14a5d6ad92d5 3490 {
mluis 2:14a5d6ad92d5 3491 mibGet->Param.ReceiveDelay1 = ReceiveDelay1;
mluis 2:14a5d6ad92d5 3492 break;
mluis 2:14a5d6ad92d5 3493 }
mluis 2:14a5d6ad92d5 3494 case MIB_RECEIVE_DELAY_2:
mluis 2:14a5d6ad92d5 3495 {
mluis 2:14a5d6ad92d5 3496 mibGet->Param.ReceiveDelay2 = ReceiveDelay2;
mluis 2:14a5d6ad92d5 3497 break;
mluis 2:14a5d6ad92d5 3498 }
mluis 2:14a5d6ad92d5 3499 case MIB_JOIN_ACCEPT_DELAY_1:
mluis 2:14a5d6ad92d5 3500 {
mluis 2:14a5d6ad92d5 3501 mibGet->Param.JoinAcceptDelay1 = JoinAcceptDelay1;
mluis 2:14a5d6ad92d5 3502 break;
mluis 2:14a5d6ad92d5 3503 }
mluis 2:14a5d6ad92d5 3504 case MIB_JOIN_ACCEPT_DELAY_2:
mluis 2:14a5d6ad92d5 3505 {
mluis 2:14a5d6ad92d5 3506 mibGet->Param.JoinAcceptDelay2 = JoinAcceptDelay2;
mluis 2:14a5d6ad92d5 3507 break;
mluis 2:14a5d6ad92d5 3508 }
mluis 4:37c12dbc8dc7 3509 case MIB_CHANNELS_DEFAULT_DATARATE:
mluis 4:37c12dbc8dc7 3510 {
mluis 4:37c12dbc8dc7 3511 mibGet->Param.ChannelsDefaultDatarate = ChannelsDefaultDatarate;
mluis 4:37c12dbc8dc7 3512 break;
mluis 4:37c12dbc8dc7 3513 }
mluis 2:14a5d6ad92d5 3514 case MIB_CHANNELS_DATARATE:
mluis 2:14a5d6ad92d5 3515 {
mluis 2:14a5d6ad92d5 3516 mibGet->Param.ChannelsDatarate = ChannelsDatarate;
mluis 2:14a5d6ad92d5 3517 break;
mluis 2:14a5d6ad92d5 3518 }
mluis 2:14a5d6ad92d5 3519 case MIB_CHANNELS_TX_POWER:
mluis 2:14a5d6ad92d5 3520 {
mluis 2:14a5d6ad92d5 3521 mibGet->Param.ChannelsTxPower = ChannelsTxPower;
mluis 2:14a5d6ad92d5 3522 break;
mluis 2:14a5d6ad92d5 3523 }
mluis 2:14a5d6ad92d5 3524 case MIB_UPLINK_COUNTER:
mluis 2:14a5d6ad92d5 3525 {
mluis 2:14a5d6ad92d5 3526 mibGet->Param.UpLinkCounter = UpLinkCounter;
mluis 2:14a5d6ad92d5 3527 break;
mluis 2:14a5d6ad92d5 3528 }
mluis 2:14a5d6ad92d5 3529 case MIB_DOWNLINK_COUNTER:
mluis 2:14a5d6ad92d5 3530 {
mluis 2:14a5d6ad92d5 3531 mibGet->Param.DownLinkCounter = DownLinkCounter;
mluis 2:14a5d6ad92d5 3532 break;
mluis 2:14a5d6ad92d5 3533 }
mluis 2:14a5d6ad92d5 3534 case MIB_MULTICAST_CHANNEL:
mluis 2:14a5d6ad92d5 3535 {
mluis 2:14a5d6ad92d5 3536 mibGet->Param.MulticastList = MulticastChannels;
mluis 2:14a5d6ad92d5 3537 break;
mluis 2:14a5d6ad92d5 3538 }
mluis 2:14a5d6ad92d5 3539 default:
mluis 2:14a5d6ad92d5 3540 status = LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 2:14a5d6ad92d5 3541 break;
mluis 0:91d1a7783bb9 3542 }
mluis 0:91d1a7783bb9 3543
mluis 2:14a5d6ad92d5 3544 return status;
mluis 0:91d1a7783bb9 3545 }
mluis 2:14a5d6ad92d5 3546
mluis 2:14a5d6ad92d5 3547 LoRaMacStatus_t LoRaMacMibSetRequestConfirm( MibRequestConfirm_t *mibSet )
mluis 0:91d1a7783bb9 3548 {
mluis 2:14a5d6ad92d5 3549 LoRaMacStatus_t status = LORAMAC_STATUS_OK;
mluis 2:14a5d6ad92d5 3550
mluis 2:14a5d6ad92d5 3551 if( mibSet == NULL )
mluis 2:14a5d6ad92d5 3552 {
mluis 2:14a5d6ad92d5 3553 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 0:91d1a7783bb9 3554 }
mluis 0:91d1a7783bb9 3555 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 0:91d1a7783bb9 3556 {
mluis 2:14a5d6ad92d5 3557 return LORAMAC_STATUS_BUSY;
mluis 0:91d1a7783bb9 3558 }
mluis 2:14a5d6ad92d5 3559
mluis 2:14a5d6ad92d5 3560 switch( mibSet->Type )
mluis 0:91d1a7783bb9 3561 {
mluis 2:14a5d6ad92d5 3562 case MIB_DEVICE_CLASS:
mluis 2:14a5d6ad92d5 3563 {
mluis 2:14a5d6ad92d5 3564 LoRaMacDeviceClass = mibSet->Param.Class;
mluis 2:14a5d6ad92d5 3565 switch( LoRaMacDeviceClass )
mluis 2:14a5d6ad92d5 3566 {
mluis 2:14a5d6ad92d5 3567 case CLASS_A:
mluis 2:14a5d6ad92d5 3568 {
mluis 2:14a5d6ad92d5 3569 // Set the radio into sleep to setup a defined state
mluis 2:14a5d6ad92d5 3570 Radio.Sleep( );
mluis 2:14a5d6ad92d5 3571 break;
mluis 2:14a5d6ad92d5 3572 }
mluis 2:14a5d6ad92d5 3573 case CLASS_B:
mluis 2:14a5d6ad92d5 3574 {
mluis 2:14a5d6ad92d5 3575 break;
mluis 2:14a5d6ad92d5 3576 }
mluis 2:14a5d6ad92d5 3577 case CLASS_C:
mluis 2:14a5d6ad92d5 3578 {
mluis 2:14a5d6ad92d5 3579 // Set the NodeAckRequested indicator to default
mluis 2:14a5d6ad92d5 3580 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 3581 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 3582 break;
mluis 2:14a5d6ad92d5 3583 }
mluis 2:14a5d6ad92d5 3584 }
mluis 2:14a5d6ad92d5 3585 break;
mluis 2:14a5d6ad92d5 3586 }
mluis 2:14a5d6ad92d5 3587 case MIB_NETWORK_JOINED:
mluis 2:14a5d6ad92d5 3588 {
mluis 2:14a5d6ad92d5 3589 IsLoRaMacNetworkJoined = mibSet->Param.IsNetworkJoined;
snelson 8:4816c8449bf2 3590 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
snelson 8:4816c8449bf2 3591 if( IsLoRaMacNetworkJoined == false )
snelson 8:4816c8449bf2 3592 {
snelson 8:4816c8449bf2 3593 NextJoinBlock = 0;
snelson 8:4816c8449bf2 3594 JoinBlocksRemaining = 0xff;
snelson 8:4816c8449bf2 3595 Join500KHzRemaining = 0xff;
snelson 8:4816c8449bf2 3596 }
snelson 8:4816c8449bf2 3597 #endif
mluis 2:14a5d6ad92d5 3598 break;
mluis 2:14a5d6ad92d5 3599 }
mluis 2:14a5d6ad92d5 3600 case MIB_ADR:
mluis 2:14a5d6ad92d5 3601 {
mluis 2:14a5d6ad92d5 3602 AdrCtrlOn = mibSet->Param.AdrEnable;
mluis 2:14a5d6ad92d5 3603 break;
mluis 2:14a5d6ad92d5 3604 }
mluis 2:14a5d6ad92d5 3605 case MIB_NET_ID:
mluis 2:14a5d6ad92d5 3606 {
mluis 2:14a5d6ad92d5 3607 LoRaMacNetID = mibSet->Param.NetID;
mluis 2:14a5d6ad92d5 3608 break;
mluis 2:14a5d6ad92d5 3609 }
mluis 2:14a5d6ad92d5 3610 case MIB_DEV_ADDR:
mluis 2:14a5d6ad92d5 3611 {
mluis 2:14a5d6ad92d5 3612 LoRaMacDevAddr = mibSet->Param.DevAddr;
mluis 2:14a5d6ad92d5 3613 break;
mluis 2:14a5d6ad92d5 3614 }
mluis 2:14a5d6ad92d5 3615 case MIB_NWK_SKEY:
mluis 0:91d1a7783bb9 3616 {
mluis 2:14a5d6ad92d5 3617 if( mibSet->Param.NwkSKey != NULL )
mluis 2:14a5d6ad92d5 3618 {
mluis 2:14a5d6ad92d5 3619 memcpy1( LoRaMacNwkSKey, mibSet->Param.NwkSKey,
mluis 2:14a5d6ad92d5 3620 sizeof( LoRaMacNwkSKey ) );
mluis 2:14a5d6ad92d5 3621 }
mluis 2:14a5d6ad92d5 3622 else
mluis 2:14a5d6ad92d5 3623 {
mluis 2:14a5d6ad92d5 3624 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3625 }
mluis 2:14a5d6ad92d5 3626 break;
mluis 2:14a5d6ad92d5 3627 }
mluis 2:14a5d6ad92d5 3628 case MIB_APP_SKEY:
mluis 2:14a5d6ad92d5 3629 {
mluis 2:14a5d6ad92d5 3630 if( mibSet->Param.AppSKey != NULL )
mluis 2:14a5d6ad92d5 3631 {
mluis 2:14a5d6ad92d5 3632 memcpy1( LoRaMacAppSKey, mibSet->Param.AppSKey,
mluis 2:14a5d6ad92d5 3633 sizeof( LoRaMacAppSKey ) );
mluis 2:14a5d6ad92d5 3634 }
mluis 2:14a5d6ad92d5 3635 else
mluis 2:14a5d6ad92d5 3636 {
mluis 2:14a5d6ad92d5 3637 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3638 }
mluis 2:14a5d6ad92d5 3639 break;
mluis 2:14a5d6ad92d5 3640 }
mluis 2:14a5d6ad92d5 3641 case MIB_PUBLIC_NETWORK:
mluis 2:14a5d6ad92d5 3642 {
mluis 2:14a5d6ad92d5 3643 SetPublicNetwork( mibSet->Param.EnablePublicNetwork );
mluis 2:14a5d6ad92d5 3644 break;
mluis 0:91d1a7783bb9 3645 }
mluis 2:14a5d6ad92d5 3646 case MIB_REPEATER_SUPPORT:
mluis 2:14a5d6ad92d5 3647 {
mluis 2:14a5d6ad92d5 3648 RepeaterSupport = mibSet->Param.EnableRepeaterSupport;
mluis 2:14a5d6ad92d5 3649 break;
mluis 2:14a5d6ad92d5 3650 }
mluis 2:14a5d6ad92d5 3651 case MIB_RX2_CHANNEL:
mluis 2:14a5d6ad92d5 3652 {
mluis 2:14a5d6ad92d5 3653 Rx2Channel = mibSet->Param.Rx2Channel;
mluis 2:14a5d6ad92d5 3654 break;
mluis 2:14a5d6ad92d5 3655 }
mluis 2:14a5d6ad92d5 3656 case MIB_CHANNELS_MASK:
mluis 2:14a5d6ad92d5 3657 {
mluis 2:14a5d6ad92d5 3658 if( mibSet->Param.ChannelsMask )
mluis 2:14a5d6ad92d5 3659 {
mluis 2:14a5d6ad92d5 3660 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 3661 bool chanMaskState = true;
mluis 4:37c12dbc8dc7 3662
mluis 4:37c12dbc8dc7 3663 #if defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 3664 chanMaskState = ValidateChannelMask( mibSet->Param.ChannelsMask );
mluis 4:37c12dbc8dc7 3665 #endif
mluis 4:37c12dbc8dc7 3666 if( chanMaskState == true )
mluis 2:14a5d6ad92d5 3667 {
ubhat 20:92764fda7371 3668 if( ( CountNbEnabled125kHzChannels( mibSet->Param.ChannelsMask ) < HYBRD_CHNLS_MIN ) &&
mluis 4:37c12dbc8dc7 3669 ( CountNbEnabled125kHzChannels( mibSet->Param.ChannelsMask ) > 0 ) )
mluis 4:37c12dbc8dc7 3670 {
mluis 4:37c12dbc8dc7 3671 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 4:37c12dbc8dc7 3672 }
mluis 4:37c12dbc8dc7 3673 else
mluis 4:37c12dbc8dc7 3674 {
mluis 4:37c12dbc8dc7 3675 memcpy1( ( uint8_t* ) ChannelsMask,
mluis 4:37c12dbc8dc7 3676 ( uint8_t* ) mibSet->Param.ChannelsMask, sizeof( ChannelsMask ) );
mluis 4:37c12dbc8dc7 3677 for ( uint8_t i = 0; i < sizeof( ChannelsMask ) / 2; i++ )
mluis 4:37c12dbc8dc7 3678 {
mluis 4:37c12dbc8dc7 3679 // Disable channels which are no longer available
mluis 4:37c12dbc8dc7 3680 ChannelsMaskRemaining[i] &= ChannelsMask[i];
mluis 4:37c12dbc8dc7 3681 }
mluis 4:37c12dbc8dc7 3682 }
mluis 2:14a5d6ad92d5 3683 }
mluis 2:14a5d6ad92d5 3684 else
mluis 2:14a5d6ad92d5 3685 {
mluis 4:37c12dbc8dc7 3686 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3687 }
mluis 0:91d1a7783bb9 3688 #else
mluis 2:14a5d6ad92d5 3689 memcpy1( ( uint8_t* ) ChannelsMask,
mluis 2:14a5d6ad92d5 3690 ( uint8_t* ) mibSet->Param.ChannelsMask, 2 );
mluis 0:91d1a7783bb9 3691 #endif
mluis 2:14a5d6ad92d5 3692 }
mluis 2:14a5d6ad92d5 3693 else
mluis 2:14a5d6ad92d5 3694 {
mluis 2:14a5d6ad92d5 3695 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3696 }
mluis 2:14a5d6ad92d5 3697 break;
mluis 2:14a5d6ad92d5 3698 }
mluis 2:14a5d6ad92d5 3699 case MIB_CHANNELS_NB_REP:
mluis 2:14a5d6ad92d5 3700 {
mluis 2:14a5d6ad92d5 3701 if( ( mibSet->Param.ChannelNbRep >= 1 ) &&
mluis 2:14a5d6ad92d5 3702 ( mibSet->Param.ChannelNbRep <= 15 ) )
mluis 2:14a5d6ad92d5 3703 {
mluis 2:14a5d6ad92d5 3704 ChannelsNbRep = mibSet->Param.ChannelNbRep;
mluis 2:14a5d6ad92d5 3705 }
mluis 2:14a5d6ad92d5 3706 else
mluis 2:14a5d6ad92d5 3707 {
mluis 2:14a5d6ad92d5 3708 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3709 }
mluis 2:14a5d6ad92d5 3710 break;
mluis 2:14a5d6ad92d5 3711 }
mluis 2:14a5d6ad92d5 3712 case MIB_MAX_RX_WINDOW_DURATION:
mluis 2:14a5d6ad92d5 3713 {
mluis 2:14a5d6ad92d5 3714 MaxRxWindow = mibSet->Param.MaxRxWindow;
mluis 2:14a5d6ad92d5 3715 break;
mluis 2:14a5d6ad92d5 3716 }
mluis 2:14a5d6ad92d5 3717 case MIB_RECEIVE_DELAY_1:
mluis 2:14a5d6ad92d5 3718 {
mluis 2:14a5d6ad92d5 3719 ReceiveDelay1 = mibSet->Param.ReceiveDelay1;
mluis 2:14a5d6ad92d5 3720 break;
mluis 2:14a5d6ad92d5 3721 }
mluis 2:14a5d6ad92d5 3722 case MIB_RECEIVE_DELAY_2:
mluis 2:14a5d6ad92d5 3723 {
mluis 2:14a5d6ad92d5 3724 ReceiveDelay2 = mibSet->Param.ReceiveDelay2;
mluis 2:14a5d6ad92d5 3725 break;
mluis 2:14a5d6ad92d5 3726 }
mluis 2:14a5d6ad92d5 3727 case MIB_JOIN_ACCEPT_DELAY_1:
mluis 2:14a5d6ad92d5 3728 {
mluis 2:14a5d6ad92d5 3729 JoinAcceptDelay1 = mibSet->Param.JoinAcceptDelay1;
mluis 2:14a5d6ad92d5 3730 break;
mluis 2:14a5d6ad92d5 3731 }
mluis 2:14a5d6ad92d5 3732 case MIB_JOIN_ACCEPT_DELAY_2:
mluis 2:14a5d6ad92d5 3733 {
mluis 2:14a5d6ad92d5 3734 JoinAcceptDelay2 = mibSet->Param.JoinAcceptDelay2;
mluis 2:14a5d6ad92d5 3735 break;
mluis 2:14a5d6ad92d5 3736 }
mluis 4:37c12dbc8dc7 3737 case MIB_CHANNELS_DEFAULT_DATARATE:
mluis 4:37c12dbc8dc7 3738 {
mluis 4:37c12dbc8dc7 3739 if( ValueInRange( mibSet->Param.ChannelsDefaultDatarate,
mluis 4:37c12dbc8dc7 3740 LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) )
mluis 4:37c12dbc8dc7 3741 {
mluis 4:37c12dbc8dc7 3742 ChannelsDefaultDatarate = mibSet->Param.ChannelsDefaultDatarate;
mluis 4:37c12dbc8dc7 3743 }
mluis 4:37c12dbc8dc7 3744 else
mluis 4:37c12dbc8dc7 3745 {
mluis 4:37c12dbc8dc7 3746 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 4:37c12dbc8dc7 3747 }
mluis 4:37c12dbc8dc7 3748 break;
mluis 4:37c12dbc8dc7 3749 }
mluis 2:14a5d6ad92d5 3750 case MIB_CHANNELS_DATARATE:
mluis 2:14a5d6ad92d5 3751 {
mluis 2:14a5d6ad92d5 3752 if( ValueInRange( mibSet->Param.ChannelsDatarate,
mluis 4:37c12dbc8dc7 3753 LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) )
mluis 2:14a5d6ad92d5 3754 {
mluis 2:14a5d6ad92d5 3755 ChannelsDatarate = mibSet->Param.ChannelsDatarate;
mluis 2:14a5d6ad92d5 3756 }
mluis 2:14a5d6ad92d5 3757 else
mluis 2:14a5d6ad92d5 3758 {
mluis 2:14a5d6ad92d5 3759 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3760 }
mluis 2:14a5d6ad92d5 3761 break;
mluis 2:14a5d6ad92d5 3762 }
mluis 2:14a5d6ad92d5 3763 case MIB_CHANNELS_TX_POWER:
mluis 2:14a5d6ad92d5 3764 {
mluis 2:14a5d6ad92d5 3765 if( ValueInRange( mibSet->Param.ChannelsTxPower,
mluis 2:14a5d6ad92d5 3766 LORAMAC_MAX_TX_POWER, LORAMAC_MIN_TX_POWER ) )
mluis 2:14a5d6ad92d5 3767 {
snelson 9:ad93de20d720 3768 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
snelson 9:ad93de20d720 3769 int8_t txPower = LimitTxPower( mibSet->Param.ChannelsTxPower );
snelson 9:ad93de20d720 3770 if( txPower == mibSet->Param.ChannelsTxPower )
snelson 9:ad93de20d720 3771 {
snelson 9:ad93de20d720 3772 ChannelsTxPower = mibSet->Param.ChannelsTxPower;
snelson 9:ad93de20d720 3773 }
snelson 9:ad93de20d720 3774 else
snelson 9:ad93de20d720 3775 {
snelson 9:ad93de20d720 3776 status = LORAMAC_STATUS_PARAMETER_INVALID;
snelson 9:ad93de20d720 3777 }
snelson 9:ad93de20d720 3778 #else
mluis 2:14a5d6ad92d5 3779 ChannelsTxPower = mibSet->Param.ChannelsTxPower;
snelson 9:ad93de20d720 3780 #endif
mluis 2:14a5d6ad92d5 3781 }
mluis 2:14a5d6ad92d5 3782 else
mluis 2:14a5d6ad92d5 3783 {
mluis 2:14a5d6ad92d5 3784 status = LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3785 }
mluis 2:14a5d6ad92d5 3786 break;
mluis 2:14a5d6ad92d5 3787 }
mluis 4:37c12dbc8dc7 3788 case MIB_UPLINK_COUNTER:
mluis 4:37c12dbc8dc7 3789 {
mluis 4:37c12dbc8dc7 3790 UpLinkCounter = mibSet->Param.UpLinkCounter;
mluis 4:37c12dbc8dc7 3791 break;
mluis 4:37c12dbc8dc7 3792 }
mluis 4:37c12dbc8dc7 3793 case MIB_DOWNLINK_COUNTER:
mluis 4:37c12dbc8dc7 3794 {
mluis 4:37c12dbc8dc7 3795 DownLinkCounter = mibSet->Param.DownLinkCounter;
mluis 4:37c12dbc8dc7 3796 break;
mluis 4:37c12dbc8dc7 3797 }
mluis 2:14a5d6ad92d5 3798 default:
mluis 2:14a5d6ad92d5 3799 status = LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 2:14a5d6ad92d5 3800 break;
mluis 0:91d1a7783bb9 3801 }
mluis 0:91d1a7783bb9 3802
mluis 2:14a5d6ad92d5 3803 return status;
mluis 0:91d1a7783bb9 3804 }
mluis 0:91d1a7783bb9 3805
mluis 2:14a5d6ad92d5 3806 LoRaMacStatus_t LoRaMacChannelAdd( uint8_t id, ChannelParams_t params )
mluis 0:91d1a7783bb9 3807 {
mluis 3:b9d87593a8ae 3808 #if ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 3:b9d87593a8ae 3809 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 3:b9d87593a8ae 3810 #else
mluis 3:b9d87593a8ae 3811 bool datarateInvalid = false;
mluis 3:b9d87593a8ae 3812 bool frequencyInvalid = false;
mluis 3:b9d87593a8ae 3813 uint8_t band = 0;
mluis 3:b9d87593a8ae 3814
mluis 3:b9d87593a8ae 3815 // The id must not exceed LORA_MAX_NB_CHANNELS
mluis 2:14a5d6ad92d5 3816 if( id >= LORA_MAX_NB_CHANNELS )
mluis 0:91d1a7783bb9 3817 {
mluis 2:14a5d6ad92d5 3818 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 0:91d1a7783bb9 3819 }
mluis 3:b9d87593a8ae 3820 // Validate if the MAC is in a correct state
mluis 2:14a5d6ad92d5 3821 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 0:91d1a7783bb9 3822 {
mluis 3:b9d87593a8ae 3823 if( ( LoRaMacState & MAC_TX_CONFIG ) != MAC_TX_CONFIG )
mluis 3:b9d87593a8ae 3824 {
mluis 3:b9d87593a8ae 3825 return LORAMAC_STATUS_BUSY;
mluis 3:b9d87593a8ae 3826 }
mluis 0:91d1a7783bb9 3827 }
mluis 3:b9d87593a8ae 3828 // Validate the datarate
mluis 2:14a5d6ad92d5 3829 if( ( params.DrRange.Fields.Min > params.DrRange.Fields.Max ) ||
mluis 4:37c12dbc8dc7 3830 ( ValueInRange( params.DrRange.Fields.Min, LORAMAC_TX_MIN_DATARATE,
mluis 4:37c12dbc8dc7 3831 LORAMAC_TX_MAX_DATARATE ) == false ) ||
mluis 4:37c12dbc8dc7 3832 ( ValueInRange( params.DrRange.Fields.Max, LORAMAC_TX_MIN_DATARATE,
mluis 4:37c12dbc8dc7 3833 LORAMAC_TX_MAX_DATARATE ) == false ) )
mluis 0:91d1a7783bb9 3834 {
mluis 3:b9d87593a8ae 3835 datarateInvalid = true;
mluis 0:91d1a7783bb9 3836 }
mluis 2:14a5d6ad92d5 3837
mluis 3:b9d87593a8ae 3838 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 3:b9d87593a8ae 3839 if( id < 3 )
mluis 0:91d1a7783bb9 3840 {
mluis 3:b9d87593a8ae 3841 if( params.Frequency != Channels[id].Frequency )
mluis 0:91d1a7783bb9 3842 {
mluis 3:b9d87593a8ae 3843 frequencyInvalid = true;
mluis 3:b9d87593a8ae 3844 }
mluis 3:b9d87593a8ae 3845
mluis 4:37c12dbc8dc7 3846 if( params.DrRange.Fields.Min > ChannelsDefaultDatarate )
mluis 3:b9d87593a8ae 3847 {
mluis 3:b9d87593a8ae 3848 datarateInvalid = true;
mluis 3:b9d87593a8ae 3849 }
mluis 4:37c12dbc8dc7 3850 if( ValueInRange( params.DrRange.Fields.Max, DR_5, LORAMAC_TX_MAX_DATARATE ) == false )
mluis 3:b9d87593a8ae 3851 {
mluis 3:b9d87593a8ae 3852 datarateInvalid = true;
mluis 0:91d1a7783bb9 3853 }
mluis 0:91d1a7783bb9 3854 }
mluis 3:b9d87593a8ae 3855 #endif
mluis 3:b9d87593a8ae 3856
mluis 3:b9d87593a8ae 3857 // Validate the frequency
mluis 3:b9d87593a8ae 3858 if( ( Radio.CheckRfFrequency( params.Frequency ) == true ) && ( params.Frequency > 0 ) && ( frequencyInvalid == false ) )
mluis 0:91d1a7783bb9 3859 {
mluis 3:b9d87593a8ae 3860 #if defined( USE_BAND_868 )
mluis 3:b9d87593a8ae 3861 if( ( params.Frequency >= 865000000 ) && ( params.Frequency <= 868000000 ) )
mluis 0:91d1a7783bb9 3862 {
mluis 3:b9d87593a8ae 3863 band = BAND_G1_0;
mluis 0:91d1a7783bb9 3864 }
mluis 3:b9d87593a8ae 3865 else if( ( params.Frequency > 868000000 ) && ( params.Frequency <= 868600000 ) )
mluis 0:91d1a7783bb9 3866 {
mluis 3:b9d87593a8ae 3867 band = BAND_G1_1;
mluis 0:91d1a7783bb9 3868 }
mluis 3:b9d87593a8ae 3869 else if( ( params.Frequency >= 868700000 ) && ( params.Frequency <= 869200000 ) )
mluis 0:91d1a7783bb9 3870 {
mluis 3:b9d87593a8ae 3871 band = BAND_G1_2;
mluis 3:b9d87593a8ae 3872 }
mluis 3:b9d87593a8ae 3873 else if( ( params.Frequency >= 869400000 ) && ( params.Frequency <= 869650000 ) )
mluis 3:b9d87593a8ae 3874 {
mluis 3:b9d87593a8ae 3875 band = BAND_G1_3;
mluis 0:91d1a7783bb9 3876 }
mluis 3:b9d87593a8ae 3877 else if( ( params.Frequency >= 869700000 ) && ( params.Frequency <= 870000000 ) )
mluis 0:91d1a7783bb9 3878 {
mluis 3:b9d87593a8ae 3879 band = BAND_G1_4;
mluis 0:91d1a7783bb9 3880 }
mluis 3:b9d87593a8ae 3881 else
mluis 3:b9d87593a8ae 3882 {
mluis 3:b9d87593a8ae 3883 frequencyInvalid = true;
mluis 3:b9d87593a8ae 3884 }
mluis 3:b9d87593a8ae 3885 #endif
mluis 0:91d1a7783bb9 3886 }
mluis 0:91d1a7783bb9 3887 else
mluis 0:91d1a7783bb9 3888 {
mluis 3:b9d87593a8ae 3889 frequencyInvalid = true;
mluis 3:b9d87593a8ae 3890 }
mluis 3:b9d87593a8ae 3891
mluis 3:b9d87593a8ae 3892 if( ( datarateInvalid == true ) && ( frequencyInvalid == true ) )
mluis 3:b9d87593a8ae 3893 {
mluis 3:b9d87593a8ae 3894 return LORAMAC_STATUS_FREQ_AND_DR_INVALID;
mluis 0:91d1a7783bb9 3895 }
mluis 3:b9d87593a8ae 3896 if( datarateInvalid == true )
mluis 3:b9d87593a8ae 3897 {
mluis 3:b9d87593a8ae 3898 return LORAMAC_STATUS_DATARATE_INVALID;
mluis 3:b9d87593a8ae 3899 }
mluis 3:b9d87593a8ae 3900 if( frequencyInvalid == true )
mluis 0:91d1a7783bb9 3901 {
mluis 3:b9d87593a8ae 3902 return LORAMAC_STATUS_FREQUENCY_INVALID;
mluis 2:14a5d6ad92d5 3903 }
mluis 2:14a5d6ad92d5 3904
mluis 3:b9d87593a8ae 3905 // Every parameter is valid, activate the channel
mluis 3:b9d87593a8ae 3906 Channels[id] = params;
mluis 3:b9d87593a8ae 3907 Channels[id].Band = band;
mluis 3:b9d87593a8ae 3908 ChannelsMask[0] |= ( 1 << id );
mluis 3:b9d87593a8ae 3909
mluis 2:14a5d6ad92d5 3910 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 3911 #endif
mluis 0:91d1a7783bb9 3912 }
mluis 0:91d1a7783bb9 3913
mluis 2:14a5d6ad92d5 3914 LoRaMacStatus_t LoRaMacChannelRemove( uint8_t id )
mluis 0:91d1a7783bb9 3915 {
mluis 3:b9d87593a8ae 3916 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 3917 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 0:91d1a7783bb9 3918 {
mluis 3:b9d87593a8ae 3919 if( ( LoRaMacState & MAC_TX_CONFIG ) != MAC_TX_CONFIG )
mluis 3:b9d87593a8ae 3920 {
mluis 3:b9d87593a8ae 3921 return LORAMAC_STATUS_BUSY;
mluis 3:b9d87593a8ae 3922 }
mluis 0:91d1a7783bb9 3923 }
mluis 3:b9d87593a8ae 3924
mluis 4:37c12dbc8dc7 3925 if( ( id < 3 ) || ( id >= LORA_MAX_NB_CHANNELS ) )
mluis 0:91d1a7783bb9 3926 {
mluis 3:b9d87593a8ae 3927 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 3:b9d87593a8ae 3928 }
mluis 3:b9d87593a8ae 3929 else
mluis 3:b9d87593a8ae 3930 {
mluis 3:b9d87593a8ae 3931 // Remove the channel from the list of channels
mluis 3:b9d87593a8ae 3932 Channels[id] = ( ChannelParams_t ){ 0, { 0 }, 0 };
mluis 4:37c12dbc8dc7 3933
mluis 3:b9d87593a8ae 3934 // Disable the channel as it doesn't exist anymore
mluis 3:b9d87593a8ae 3935 if( DisableChannelInMask( id, ChannelsMask ) == false )
mluis 2:14a5d6ad92d5 3936 {
mluis 2:14a5d6ad92d5 3937 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3938 }
mluis 0:91d1a7783bb9 3939 }
mluis 3:b9d87593a8ae 3940 return LORAMAC_STATUS_OK;
mluis 3:b9d87593a8ae 3941 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 3:b9d87593a8ae 3942 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3943 #endif
mluis 2:14a5d6ad92d5 3944 }
mluis 2:14a5d6ad92d5 3945
mluis 2:14a5d6ad92d5 3946 LoRaMacStatus_t LoRaMacMulticastChannelLink( MulticastParams_t *channelParam )
mluis 2:14a5d6ad92d5 3947 {
mluis 2:14a5d6ad92d5 3948 if( channelParam == NULL )
mluis 2:14a5d6ad92d5 3949 {
mluis 2:14a5d6ad92d5 3950 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 3951 }
mluis 2:14a5d6ad92d5 3952 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 2:14a5d6ad92d5 3953 {
mluis 2:14a5d6ad92d5 3954 return LORAMAC_STATUS_BUSY;
mluis 2:14a5d6ad92d5 3955 }
mluis 2:14a5d6ad92d5 3956
mluis 2:14a5d6ad92d5 3957 // Reset downlink counter
mluis 2:14a5d6ad92d5 3958 channelParam->DownLinkCounter = 0;
mluis 2:14a5d6ad92d5 3959
mluis 2:14a5d6ad92d5 3960 if( MulticastChannels == NULL )
mluis 2:14a5d6ad92d5 3961 {
mluis 2:14a5d6ad92d5 3962 // New node is the fist element
mluis 2:14a5d6ad92d5 3963 MulticastChannels = channelParam;
mluis 0:91d1a7783bb9 3964 }
mluis 0:91d1a7783bb9 3965 else
mluis 0:91d1a7783bb9 3966 {
mluis 2:14a5d6ad92d5 3967 MulticastParams_t *cur = MulticastChannels;
mluis 2:14a5d6ad92d5 3968
mluis 2:14a5d6ad92d5 3969 // Search the last node in the list
mluis 2:14a5d6ad92d5 3970 while( cur->Next != NULL )
mluis 2:14a5d6ad92d5 3971 {
mluis 2:14a5d6ad92d5 3972 cur = cur->Next;
mluis 2:14a5d6ad92d5 3973 }
mluis 2:14a5d6ad92d5 3974 // This function always finds the last node
mluis 2:14a5d6ad92d5 3975 cur->Next = channelParam;
mluis 0:91d1a7783bb9 3976 }
mluis 2:14a5d6ad92d5 3977
mluis 2:14a5d6ad92d5 3978 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 3979 }
mluis 0:91d1a7783bb9 3980
mluis 2:14a5d6ad92d5 3981 LoRaMacStatus_t LoRaMacMulticastChannelUnlink( MulticastParams_t *channelParam )
mluis 0:91d1a7783bb9 3982 {
mluis 2:14a5d6ad92d5 3983 if( channelParam == NULL )
mluis 0:91d1a7783bb9 3984 {
mluis 2:14a5d6ad92d5 3985 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 0:91d1a7783bb9 3986 }
mluis 2:14a5d6ad92d5 3987 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 2:14a5d6ad92d5 3988 {
mluis 2:14a5d6ad92d5 3989 return LORAMAC_STATUS_BUSY;
mluis 2:14a5d6ad92d5 3990 }
mluis 2:14a5d6ad92d5 3991
mluis 2:14a5d6ad92d5 3992 if( MulticastChannels != NULL )
mluis 0:91d1a7783bb9 3993 {
mluis 2:14a5d6ad92d5 3994 if( MulticastChannels == channelParam )
mluis 2:14a5d6ad92d5 3995 {
mluis 2:14a5d6ad92d5 3996 // First element
mluis 2:14a5d6ad92d5 3997 MulticastChannels = channelParam->Next;
mluis 2:14a5d6ad92d5 3998 }
mluis 2:14a5d6ad92d5 3999 else
mluis 2:14a5d6ad92d5 4000 {
mluis 2:14a5d6ad92d5 4001 MulticastParams_t *cur = MulticastChannels;
mluis 2:14a5d6ad92d5 4002
mluis 2:14a5d6ad92d5 4003 // Search the node in the list
mluis 2:14a5d6ad92d5 4004 while( cur->Next && cur->Next != channelParam )
mluis 2:14a5d6ad92d5 4005 {
mluis 2:14a5d6ad92d5 4006 cur = cur->Next;
mluis 2:14a5d6ad92d5 4007 }
mluis 2:14a5d6ad92d5 4008 // If we found the node, remove it
mluis 2:14a5d6ad92d5 4009 if( cur->Next )
mluis 2:14a5d6ad92d5 4010 {
mluis 2:14a5d6ad92d5 4011 cur->Next = channelParam->Next;
mluis 2:14a5d6ad92d5 4012 }
mluis 2:14a5d6ad92d5 4013 }
mluis 2:14a5d6ad92d5 4014 channelParam->Next = NULL;
mluis 0:91d1a7783bb9 4015 }
mluis 2:14a5d6ad92d5 4016
mluis 2:14a5d6ad92d5 4017 return LORAMAC_STATUS_OK;
mluis 0:91d1a7783bb9 4018 }
mluis 0:91d1a7783bb9 4019
mluis 2:14a5d6ad92d5 4020 LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t *mlmeRequest )
mluis 0:91d1a7783bb9 4021 {
mluis 2:14a5d6ad92d5 4022 LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 2:14a5d6ad92d5 4023 LoRaMacHeader_t macHdr;
mluis 2:14a5d6ad92d5 4024
mluis 2:14a5d6ad92d5 4025 if( mlmeRequest == NULL )
mluis 2:14a5d6ad92d5 4026 {
mluis 2:14a5d6ad92d5 4027 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 4028 }
mluis 2:14a5d6ad92d5 4029 if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
mluis 2:14a5d6ad92d5 4030 {
mluis 2:14a5d6ad92d5 4031 return LORAMAC_STATUS_BUSY;
mluis 2:14a5d6ad92d5 4032 }
mluis 2:14a5d6ad92d5 4033
mluis 2:14a5d6ad92d5 4034 memset1( ( uint8_t* ) &MlmeConfirm, 0, sizeof( MlmeConfirm ) );
mluis 2:14a5d6ad92d5 4035
mluis 2:14a5d6ad92d5 4036 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 2:14a5d6ad92d5 4037
mluis 2:14a5d6ad92d5 4038 switch( mlmeRequest->Type )
mluis 2:14a5d6ad92d5 4039 {
mluis 2:14a5d6ad92d5 4040 case MLME_JOIN:
mluis 2:14a5d6ad92d5 4041 {
mluis 2:14a5d6ad92d5 4042 if( ( LoRaMacState & MAC_TX_DELAYED ) == MAC_TX_DELAYED )
mluis 2:14a5d6ad92d5 4043 {
mluis 6:d7a34ded7c87 4044 return LORAMAC_STATUS_BUSY;
mluis 2:14a5d6ad92d5 4045 }
mluis 2:14a5d6ad92d5 4046
mluis 2:14a5d6ad92d5 4047 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
mluis 2:14a5d6ad92d5 4048
mluis 2:14a5d6ad92d5 4049 if( ( mlmeRequest->Req.Join.DevEui == NULL ) ||
mluis 2:14a5d6ad92d5 4050 ( mlmeRequest->Req.Join.AppEui == NULL ) ||
mluis 2:14a5d6ad92d5 4051 ( mlmeRequest->Req.Join.AppKey == NULL ) )
mluis 2:14a5d6ad92d5 4052 {
mluis 2:14a5d6ad92d5 4053 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 4054 }
mluis 2:14a5d6ad92d5 4055
mluis 2:14a5d6ad92d5 4056 LoRaMacFlags.Bits.MlmeReq = 1;
mluis 2:14a5d6ad92d5 4057
mluis 2:14a5d6ad92d5 4058 LoRaMacDevEui = mlmeRequest->Req.Join.DevEui;
mluis 2:14a5d6ad92d5 4059 LoRaMacAppEui = mlmeRequest->Req.Join.AppEui;
mluis 2:14a5d6ad92d5 4060 LoRaMacAppKey = mlmeRequest->Req.Join.AppKey;
mluis 2:14a5d6ad92d5 4061
shaunkrnelson 22:43aa9a123248 4062 // Disable join retransmission dutycycle as it does not currently
shaunkrnelson 22:43aa9a123248 4063 // handle device reset
shaunkrnelson 22:43aa9a123248 4064 #if 0
snelson 8:4816c8449bf2 4065 if( LoRaMacCalcJoinBackOff( ) != 0 )
snelson 8:4816c8449bf2 4066 {
snelson 8:4816c8449bf2 4067 return LORAMAC_STATUS_TX_DCYCLE_EXCEEDED;
snelson 8:4816c8449bf2 4068 }
shaunkrnelson 22:43aa9a123248 4069 #endif
snelson 8:4816c8449bf2 4070
mluis 2:14a5d6ad92d5 4071 macHdr.Value = 0;
mluis 2:14a5d6ad92d5 4072 macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
mluis 2:14a5d6ad92d5 4073
snelson 8:4816c8449bf2 4074 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
snelson 8:4816c8449bf2 4075 if ( IsLoRaMacNetworkJoined == true )
snelson 8:4816c8449bf2 4076 {
snelson 8:4816c8449bf2 4077 NextJoinBlock = 0;
snelson 8:4816c8449bf2 4078 JoinBlocksRemaining = 0xff;
snelson 8:4816c8449bf2 4079 Join500KHzRemaining = 0xff;
snelson 8:4816c8449bf2 4080 IsLoRaMacNetworkJoined = false;
snelson 8:4816c8449bf2 4081 }
snelson 8:4816c8449bf2 4082 #endif
mluis 2:14a5d6ad92d5 4083
mluis 2:14a5d6ad92d5 4084 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 4085 #if defined( USE_BAND_915 )
mluis 3:b9d87593a8ae 4086 // Re-enable 500 kHz default channels
mluis 3:b9d87593a8ae 4087 ChannelsMask[4] = 0x00FF;
mluis 3:b9d87593a8ae 4088 #else // defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 4089 // Re-enable 500 kHz default channels
mluis 3:b9d87593a8ae 4090 ChannelsMask[4] = 0x0001;
mluis 3:b9d87593a8ae 4091 #endif
mluis 3:b9d87593a8ae 4092
mluis 2:14a5d6ad92d5 4093 static uint8_t drSwitch = 0;
mluis 2:14a5d6ad92d5 4094
mluis 2:14a5d6ad92d5 4095 if( ( ++drSwitch & 0x01 ) == 0x01 )
mluis 2:14a5d6ad92d5 4096 {
mluis 2:14a5d6ad92d5 4097 ChannelsDatarate = DR_0;
mluis 2:14a5d6ad92d5 4098 }
mluis 2:14a5d6ad92d5 4099 else
mluis 2:14a5d6ad92d5 4100 {
mluis 2:14a5d6ad92d5 4101 ChannelsDatarate = DR_4;
mluis 2:14a5d6ad92d5 4102 }
mluis 2:14a5d6ad92d5 4103 #endif
mluis 2:14a5d6ad92d5 4104
mluis 2:14a5d6ad92d5 4105 status = Send( &macHdr, 0, NULL, 0 );
mluis 2:14a5d6ad92d5 4106 break;
mluis 2:14a5d6ad92d5 4107 }
mluis 2:14a5d6ad92d5 4108 case MLME_LINK_CHECK:
mluis 2:14a5d6ad92d5 4109 {
mluis 2:14a5d6ad92d5 4110 LoRaMacFlags.Bits.MlmeReq = 1;
mluis 2:14a5d6ad92d5 4111 // LoRaMac will send this command piggy-pack
mluis 2:14a5d6ad92d5 4112 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
mluis 2:14a5d6ad92d5 4113
mluis 2:14a5d6ad92d5 4114 status = AddMacCommand( MOTE_MAC_LINK_CHECK_REQ, 0, 0 );
mluis 2:14a5d6ad92d5 4115 break;
mluis 2:14a5d6ad92d5 4116 }
mluis 2:14a5d6ad92d5 4117 default:
mluis 2:14a5d6ad92d5 4118 break;
mluis 2:14a5d6ad92d5 4119 }
mluis 2:14a5d6ad92d5 4120
mluis 2:14a5d6ad92d5 4121 if( status != LORAMAC_STATUS_OK )
mluis 2:14a5d6ad92d5 4122 {
mluis 2:14a5d6ad92d5 4123 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 4124 LoRaMacFlags.Bits.MlmeReq = 0;
mluis 2:14a5d6ad92d5 4125 }
mluis 2:14a5d6ad92d5 4126
mluis 2:14a5d6ad92d5 4127 return status;
mluis 0:91d1a7783bb9 4128 }
mluis 0:91d1a7783bb9 4129
mluis 2:14a5d6ad92d5 4130 LoRaMacStatus_t LoRaMacMcpsRequest( McpsReq_t *mcpsRequest )
mluis 0:91d1a7783bb9 4131 {
mluis 2:14a5d6ad92d5 4132 LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
mluis 2:14a5d6ad92d5 4133 LoRaMacHeader_t macHdr;
mluis 2:14a5d6ad92d5 4134 uint8_t fPort = 0;
mluis 2:14a5d6ad92d5 4135 void *fBuffer;
mluis 2:14a5d6ad92d5 4136 uint16_t fBufferSize;
mluis 2:14a5d6ad92d5 4137 int8_t datarate;
mluis 2:14a5d6ad92d5 4138 bool readyToSend = false;
mluis 2:14a5d6ad92d5 4139
mluis 2:14a5d6ad92d5 4140 if( mcpsRequest == NULL )
mluis 2:14a5d6ad92d5 4141 {
mluis 2:14a5d6ad92d5 4142 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 4143 }
mluis 2:14a5d6ad92d5 4144 if( ( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING ) ||
mluis 2:14a5d6ad92d5 4145 ( ( LoRaMacState & MAC_TX_DELAYED ) == MAC_TX_DELAYED ) )
mluis 2:14a5d6ad92d5 4146 {
mluis 2:14a5d6ad92d5 4147 return LORAMAC_STATUS_BUSY;
mluis 2:14a5d6ad92d5 4148 }
mluis 2:14a5d6ad92d5 4149
mluis 2:14a5d6ad92d5 4150 macHdr.Value = 0;
mluis 2:14a5d6ad92d5 4151 memset1 ( ( uint8_t* ) &McpsConfirm, 0, sizeof( McpsConfirm ) );
mluis 2:14a5d6ad92d5 4152 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 2:14a5d6ad92d5 4153
mluis 2:14a5d6ad92d5 4154 switch( mcpsRequest->Type )
mluis 2:14a5d6ad92d5 4155 {
mluis 2:14a5d6ad92d5 4156 case MCPS_UNCONFIRMED:
mluis 2:14a5d6ad92d5 4157 {
mluis 2:14a5d6ad92d5 4158 readyToSend = true;
mluis 2:14a5d6ad92d5 4159 AckTimeoutRetries = 1;
mluis 2:14a5d6ad92d5 4160
mluis 2:14a5d6ad92d5 4161 macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP;
mluis 2:14a5d6ad92d5 4162 fPort = mcpsRequest->Req.Unconfirmed.fPort;
mluis 2:14a5d6ad92d5 4163 fBuffer = mcpsRequest->Req.Unconfirmed.fBuffer;
mluis 2:14a5d6ad92d5 4164 fBufferSize = mcpsRequest->Req.Unconfirmed.fBufferSize;
mluis 2:14a5d6ad92d5 4165 datarate = mcpsRequest->Req.Unconfirmed.Datarate;
mluis 2:14a5d6ad92d5 4166 break;
mluis 2:14a5d6ad92d5 4167 }
mluis 2:14a5d6ad92d5 4168 case MCPS_CONFIRMED:
mluis 2:14a5d6ad92d5 4169 {
mluis 2:14a5d6ad92d5 4170 readyToSend = true;
mluis 2:14a5d6ad92d5 4171 AckTimeoutRetriesCounter = 1;
mluis 3:b9d87593a8ae 4172 AckTimeoutRetries = mcpsRequest->Req.Confirmed.NbTrials;
mluis 2:14a5d6ad92d5 4173
mluis 2:14a5d6ad92d5 4174 macHdr.Bits.MType = FRAME_TYPE_DATA_CONFIRMED_UP;
mluis 2:14a5d6ad92d5 4175 fPort = mcpsRequest->Req.Confirmed.fPort;
mluis 2:14a5d6ad92d5 4176 fBuffer = mcpsRequest->Req.Confirmed.fBuffer;
mluis 2:14a5d6ad92d5 4177 fBufferSize = mcpsRequest->Req.Confirmed.fBufferSize;
mluis 2:14a5d6ad92d5 4178 datarate = mcpsRequest->Req.Confirmed.Datarate;
mluis 2:14a5d6ad92d5 4179 break;
mluis 2:14a5d6ad92d5 4180 }
mluis 2:14a5d6ad92d5 4181 case MCPS_PROPRIETARY:
mluis 2:14a5d6ad92d5 4182 {
mluis 2:14a5d6ad92d5 4183 readyToSend = true;
mluis 2:14a5d6ad92d5 4184 AckTimeoutRetries = 1;
mluis 2:14a5d6ad92d5 4185
mluis 2:14a5d6ad92d5 4186 macHdr.Bits.MType = FRAME_TYPE_PROPRIETARY;
mluis 2:14a5d6ad92d5 4187 fBuffer = mcpsRequest->Req.Proprietary.fBuffer;
mluis 2:14a5d6ad92d5 4188 fBufferSize = mcpsRequest->Req.Proprietary.fBufferSize;
mluis 2:14a5d6ad92d5 4189 datarate = mcpsRequest->Req.Proprietary.Datarate;
mluis 2:14a5d6ad92d5 4190 break;
mluis 2:14a5d6ad92d5 4191 }
mluis 2:14a5d6ad92d5 4192 default:
mluis 2:14a5d6ad92d5 4193 break;
mluis 2:14a5d6ad92d5 4194 }
mluis 2:14a5d6ad92d5 4195
mluis 2:14a5d6ad92d5 4196 if( readyToSend == true )
mluis 2:14a5d6ad92d5 4197 {
mluis 2:14a5d6ad92d5 4198 if( AdrCtrlOn == false )
mluis 2:14a5d6ad92d5 4199 {
mluis 4:37c12dbc8dc7 4200 if( ValueInRange( datarate, LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) == true )
mluis 2:14a5d6ad92d5 4201 {
mluis 2:14a5d6ad92d5 4202 ChannelsDatarate = datarate;
mluis 2:14a5d6ad92d5 4203 }
mluis 2:14a5d6ad92d5 4204 else
mluis 2:14a5d6ad92d5 4205 {
mluis 2:14a5d6ad92d5 4206 return LORAMAC_STATUS_PARAMETER_INVALID;
mluis 2:14a5d6ad92d5 4207 }
mluis 2:14a5d6ad92d5 4208 }
mluis 2:14a5d6ad92d5 4209
mluis 2:14a5d6ad92d5 4210 status = Send( &macHdr, fPort, fBuffer, fBufferSize );
mluis 2:14a5d6ad92d5 4211 if( status == LORAMAC_STATUS_OK )
mluis 2:14a5d6ad92d5 4212 {
mluis 2:14a5d6ad92d5 4213 McpsConfirm.McpsRequest = mcpsRequest->Type;
mluis 2:14a5d6ad92d5 4214 LoRaMacFlags.Bits.McpsReq = 1;
mluis 2:14a5d6ad92d5 4215 }
mluis 2:14a5d6ad92d5 4216 else
mluis 2:14a5d6ad92d5 4217 {
mluis 2:14a5d6ad92d5 4218 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 4219 }
mluis 2:14a5d6ad92d5 4220 }
mluis 2:14a5d6ad92d5 4221
mluis 2:14a5d6ad92d5 4222 return status;
mluis 0:91d1a7783bb9 4223 }
mluis 0:91d1a7783bb9 4224
snelson 8:4816c8449bf2 4225 TimerTime_t LoRaMacCalcJoinBackOff( )
snelson 8:4816c8449bf2 4226 {
snelson 8:4816c8449bf2 4227 TimerTime_t timeOff = 0;
snelson 8:4816c8449bf2 4228 TimerTime_t uptime;
snelson 8:4816c8449bf2 4229 TimerTime_t currDCycleEndTime;
snelson 8:4816c8449bf2 4230 TimerTime_t prevDCycleEndTime;
snelson 8:4816c8449bf2 4231 TimerTime_t period;
snelson 8:4816c8449bf2 4232 TimerTime_t onAirTimeMax;
snelson 9:ad93de20d720 4233 TimerTime_t lastJoinTime;
snelson 9:ad93de20d720 4234 TimerTime_t elapsedTime;
snelson 9:ad93de20d720 4235 uint8_t dcyclePeriod;
snelson 9:ad93de20d720 4236
snelson 9:ad93de20d720 4237 // Check retransmit duty-cycle is enabled
snelson 8:4816c8449bf2 4238 if ( JOIN_NB_RETRANSMISSION_DCYCLES == 0 )
snelson 8:4816c8449bf2 4239 return 0;
snelson 8:4816c8449bf2 4240
snelson 9:ad93de20d720 4241 // Normalize system time values to seconds
Shaun Nelson 18:5ccaea55a881 4242 uptime = TimerGetCurrentTime( ) / 1e6;
Shaun Nelson 18:5ccaea55a881 4243 lastJoinTime = LastJoinTxTime / 1e6;
snelson 9:ad93de20d720 4244
snelson 9:ad93de20d720 4245 // Get dutycycle for current time
snelson 8:4816c8449bf2 4246 prevDCycleEndTime = 0;
snelson 8:4816c8449bf2 4247 currDCycleEndTime = 0;
snelson 9:ad93de20d720 4248 for(dcyclePeriod = 0; dcyclePeriod < JOIN_NB_RETRANSMISSION_DCYCLES; dcyclePeriod++)
snelson 8:4816c8449bf2 4249 {
snelson 8:4816c8449bf2 4250 prevDCycleEndTime = currDCycleEndTime;
snelson 9:ad93de20d720 4251 currDCycleEndTime += JoinReTransmitDCycle[dcyclePeriod].period;
snelson 8:4816c8449bf2 4252
snelson 8:4816c8449bf2 4253 bool isCurrentPeriod = uptime < currDCycleEndTime;
snelson 8:4816c8449bf2 4254
snelson 9:ad93de20d720 4255 if( isCurrentPeriod || ( dcyclePeriod == ( JOIN_NB_RETRANSMISSION_DCYCLES-1 ) ) )
snelson 8:4816c8449bf2 4256 {
snelson 9:ad93de20d720 4257 period = JoinReTransmitDCycle[dcyclePeriod].period;
snelson 9:ad93de20d720 4258 onAirTimeMax = JoinReTransmitDCycle[dcyclePeriod].onAirTimeMax;
snelson 8:4816c8449bf2 4259 if( isCurrentPeriod == true )
snelson 8:4816c8449bf2 4260 break;
snelson 8:4816c8449bf2 4261 }
snelson 8:4816c8449bf2 4262 }
snelson 8:4816c8449bf2 4263
snelson 9:ad93de20d720 4264 // Clear aggregate on air time if dutycycle period of last join has elapsed
snelson 9:ad93de20d720 4265 if( lastJoinTime < prevDCycleEndTime )
snelson 8:4816c8449bf2 4266 {
snelson 8:4816c8449bf2 4267 JoinAggTimeOnAir = 0;
snelson 8:4816c8449bf2 4268 }
snelson 9:ad93de20d720 4269 else
snelson 8:4816c8449bf2 4270 {
snelson 9:ad93de20d720 4271 elapsedTime = ( uptime - prevDCycleEndTime ) % period;
snelson 9:ad93de20d720 4272
snelson 9:ad93de20d720 4273 if( dcyclePeriod == JOIN_NB_RETRANSMISSION_DCYCLES )
snelson 8:4816c8449bf2 4274 {
snelson 9:ad93de20d720 4275 if( ( ( uptime - prevDCycleEndTime ) / period ) > ( ( lastJoinTime - prevDCycleEndTime ) / period ) )
snelson 9:ad93de20d720 4276 {
snelson 9:ad93de20d720 4277 JoinAggTimeOnAir = 0;
snelson 9:ad93de20d720 4278 }
snelson 8:4816c8449bf2 4279 }
snelson 8:4816c8449bf2 4280 }
snelson 8:4816c8449bf2 4281
snelson 8:4816c8449bf2 4282 if ( JoinAggTimeOnAir >= onAirTimeMax )
snelson 8:4816c8449bf2 4283 {
snelson 8:4816c8449bf2 4284 // time off is remaining time until beginning of next period
snelson 9:ad93de20d720 4285 timeOff = ( period - elapsedTime );
snelson 8:4816c8449bf2 4286 }
snelson 8:4816c8449bf2 4287
snelson 9:ad93de20d720 4288
Shaun Nelson 18:5ccaea55a881 4289 if(timeOff > JOIN_RETRANSMISSION_DC_WAIT_MAX)
Shaun Nelson 18:5ccaea55a881 4290 timeOff = JOIN_RETRANSMISSION_DC_WAIT_MAX;
Shaun Nelson 18:5ccaea55a881 4291
Shaun Nelson 18:5ccaea55a881 4292 return ( timeOff * 1e6 );
snelson 8:4816c8449bf2 4293 }
snelson 8:4816c8449bf2 4294
mluis 0:91d1a7783bb9 4295 void LoRaMacTestRxWindowsOn( bool enable )
mluis 0:91d1a7783bb9 4296 {
mluis 0:91d1a7783bb9 4297 IsRxWindowsEnabled = enable;
mluis 0:91d1a7783bb9 4298 }
mluis 0:91d1a7783bb9 4299
mluis 2:14a5d6ad92d5 4300 void LoRaMacTestSetMic( uint16_t txPacketCounter )
mluis 0:91d1a7783bb9 4301 {
mluis 2:14a5d6ad92d5 4302 UpLinkCounter = txPacketCounter;
mluis 0:91d1a7783bb9 4303 IsUpLinkCounterFixed = true;
mluis 0:91d1a7783bb9 4304 }
mluis 2:14a5d6ad92d5 4305
mluis 2:14a5d6ad92d5 4306 void LoRaMacTestSetDutyCycleOn( bool enable )
mluis 2:14a5d6ad92d5 4307 {
mluis 2:14a5d6ad92d5 4308 DutyCycleOn = enable;
mluis 2:14a5d6ad92d5 4309 }