Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Committer:
ubhat
Date:
Wed Sep 14 02:53:13 2016 +0000
Branch:
v4.2.0
Revision:
17:fca344a6ff18
Parent:
16:1e01c8728daa
Child:
18:5ccaea55a881
ADR fix - Update ChannelsMaskRemaining with ChannelsMask when linkAdrStatus = 0x07

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