Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Committer:
Shaun Nelson
Date:
Mon Aug 21 16:09:09 2017 -0400
Branch:
india_support
Revision:
43:eed39925bade
Parent:
31:930f949fdd9e
Added India channel plan

Who changed what in which revision?

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