Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Committer:
shaunkrnelson
Date:
Thu Oct 13 19:40:56 2016 +0000
Branch:
v4.2.0
Revision:
24:69286d8b3962
Parent:
23:80965acdd311
Child:
25:8d0a79bd607b
ADRACkReq datarate decay fix

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