20171208

Fork of LoRaWAN-lib by Semtech

Committer:
cdebank
Date:
Fri Dec 08 14:22:31 2017 +0000
Revision:
11:de2cdd1f253e
Parent:
8:26002607de9c
20171208

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
mluis 2:14a5d6ad92d5 18 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel J├Ąckle ( STACKFORCE )
mluis 0:91d1a7783bb9 19 */
mluis 8:26002607de9c 20 #include <math.h>
mluis 0:91d1a7783bb9 21 #include "board.h"
mluis 0:91d1a7783bb9 22
mluis 0:91d1a7783bb9 23 #include "LoRaMacCrypto.h"
mluis 0:91d1a7783bb9 24 #include "LoRaMac.h"
mluis 2:14a5d6ad92d5 25 #include "LoRaMacTest.h"
mluis 0:91d1a7783bb9 26
mluis 0:91d1a7783bb9 27 /*!
mluis 0:91d1a7783bb9 28 * Maximum PHY layer payload size
mluis 0:91d1a7783bb9 29 */
mluis 1:91e4e6c60d1e 30 #define LORAMAC_PHY_MAXPAYLOAD 255
mluis 1:91e4e6c60d1e 31
mluis 1:91e4e6c60d1e 32 /*!
mluis 1:91e4e6c60d1e 33 * Maximum MAC commands buffer size
mluis 1:91e4e6c60d1e 34 */
mluis 1:91e4e6c60d1e 35 #define LORA_MAC_COMMAND_MAX_LENGTH 15
mluis 0:91d1a7783bb9 36
mluis 0:91d1a7783bb9 37 /*!
mluis 4:37c12dbc8dc7 38 * FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
mluis 4:37c12dbc8dc7 39 * in RxWindowSetup function.
mluis 4:37c12dbc8dc7 40 * Maximum PHYPayload = MaxPayloadOfDatarate/MaxPayloadOfDatarateRepeater + LORA_MAC_FRMPAYLOAD_OVERHEAD
mluis 4:37c12dbc8dc7 41 */
mluis 4:37c12dbc8dc7 42 #define LORA_MAC_FRMPAYLOAD_OVERHEAD 13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)
mluis 4:37c12dbc8dc7 43
mluis 4:37c12dbc8dc7 44 /*!
mluis 8:26002607de9c 45 * LoRaMac duty cycle for the back-off procedure during the first hour.
mluis 8:26002607de9c 46 */
mluis 8:26002607de9c 47 #define BACKOFF_DC_1_HOUR 100
mluis 8:26002607de9c 48
mluis 8:26002607de9c 49 /*!
mluis 8:26002607de9c 50 * LoRaMac duty cycle for the back-off procedure during the next 10 hours.
mluis 8:26002607de9c 51 */
mluis 8:26002607de9c 52 #define BACKOFF_DC_10_HOURS 1000
mluis 8:26002607de9c 53
mluis 8:26002607de9c 54 /*!
mluis 8:26002607de9c 55 * LoRaMac duty cycle for the back-off procedure during the next 24 hours.
mluis 8:26002607de9c 56 */
mluis 8:26002607de9c 57 #define BACKOFF_DC_24_HOURS 10000
mluis 8:26002607de9c 58
mluis 8:26002607de9c 59 /*!
mluis 0:91d1a7783bb9 60 * Device IEEE EUI
mluis 0:91d1a7783bb9 61 */
mluis 0:91d1a7783bb9 62 static uint8_t *LoRaMacDevEui;
mluis 0:91d1a7783bb9 63
mluis 0:91d1a7783bb9 64 /*!
mluis 0:91d1a7783bb9 65 * Application IEEE EUI
mluis 0:91d1a7783bb9 66 */
mluis 0:91d1a7783bb9 67 static uint8_t *LoRaMacAppEui;
mluis 0:91d1a7783bb9 68
mluis 0:91d1a7783bb9 69 /*!
mluis 0:91d1a7783bb9 70 * AES encryption/decryption cipher application key
mluis 0:91d1a7783bb9 71 */
mluis 0:91d1a7783bb9 72 static uint8_t *LoRaMacAppKey;
mluis 0:91d1a7783bb9 73
mluis 0:91d1a7783bb9 74 /*!
mluis 0:91d1a7783bb9 75 * AES encryption/decryption cipher network session key
mluis 0:91d1a7783bb9 76 */
mluis 0:91d1a7783bb9 77 static uint8_t LoRaMacNwkSKey[] =
mluis 0:91d1a7783bb9 78 {
mluis 0:91d1a7783bb9 79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mluis 0:91d1a7783bb9 80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:91d1a7783bb9 81 };
mluis 0:91d1a7783bb9 82
mluis 0:91d1a7783bb9 83 /*!
mluis 0:91d1a7783bb9 84 * AES encryption/decryption cipher application session key
mluis 0:91d1a7783bb9 85 */
mluis 0:91d1a7783bb9 86 static uint8_t LoRaMacAppSKey[] =
mluis 0:91d1a7783bb9 87 {
mluis 0:91d1a7783bb9 88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
mluis 0:91d1a7783bb9 89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:91d1a7783bb9 90 };
mluis 0:91d1a7783bb9 91
mluis 0:91d1a7783bb9 92 /*!
mluis 0:91d1a7783bb9 93 * Device nonce is a random value extracted by issuing a sequence of RSSI
mluis 0:91d1a7783bb9 94 * measurements
mluis 0:91d1a7783bb9 95 */
mluis 0:91d1a7783bb9 96 static uint16_t LoRaMacDevNonce;
mluis 0:91d1a7783bb9 97
mluis 0:91d1a7783bb9 98 /*!
mluis 0:91d1a7783bb9 99 * Network ID ( 3 bytes )
mluis 0:91d1a7783bb9 100 */
mluis 0:91d1a7783bb9 101 static uint32_t LoRaMacNetID;
mluis 0:91d1a7783bb9 102
mluis 0:91d1a7783bb9 103 /*!
mluis 0:91d1a7783bb9 104 * Mote Address
mluis 0:91d1a7783bb9 105 */
mluis 0:91d1a7783bb9 106 static uint32_t LoRaMacDevAddr;
mluis 0:91d1a7783bb9 107
mluis 0:91d1a7783bb9 108 /*!
mluis 2:14a5d6ad92d5 109 * Multicast channels linked list
mluis 0:91d1a7783bb9 110 */
mluis 0:91d1a7783bb9 111 static MulticastParams_t *MulticastChannels = NULL;
mluis 0:91d1a7783bb9 112
mluis 0:91d1a7783bb9 113 /*!
mluis 0:91d1a7783bb9 114 * Actual device class
mluis 0:91d1a7783bb9 115 */
mluis 0:91d1a7783bb9 116 static DeviceClass_t LoRaMacDeviceClass;
mluis 0:91d1a7783bb9 117
mluis 0:91d1a7783bb9 118 /*!
mluis 0:91d1a7783bb9 119 * Indicates if the node is connected to a private or public network
mluis 0:91d1a7783bb9 120 */
mluis 0:91d1a7783bb9 121 static bool PublicNetwork;
mluis 0:91d1a7783bb9 122
mluis 0:91d1a7783bb9 123 /*!
mluis 0:91d1a7783bb9 124 * Indicates if the node supports repeaters
mluis 0:91d1a7783bb9 125 */
mluis 0:91d1a7783bb9 126 static bool RepeaterSupport;
mluis 0:91d1a7783bb9 127
mluis 0:91d1a7783bb9 128 /*!
mluis 0:91d1a7783bb9 129 * Buffer containing the data to be sent or received.
mluis 0:91d1a7783bb9 130 */
mluis 0:91d1a7783bb9 131 static uint8_t LoRaMacBuffer[LORAMAC_PHY_MAXPAYLOAD];
mluis 0:91d1a7783bb9 132
mluis 0:91d1a7783bb9 133 /*!
mluis 0:91d1a7783bb9 134 * Length of packet in LoRaMacBuffer
mluis 0:91d1a7783bb9 135 */
mluis 0:91d1a7783bb9 136 static uint16_t LoRaMacBufferPktLen = 0;
mluis 0:91d1a7783bb9 137
mluis 0:91d1a7783bb9 138 /*!
mluis 8:26002607de9c 139 * Length of the payload in LoRaMacBuffer
mluis 8:26002607de9c 140 */
mluis 8:26002607de9c 141 static uint8_t LoRaMacTxPayloadLen = 0;
mluis 8:26002607de9c 142
mluis 8:26002607de9c 143 /*!
mluis 0:91d1a7783bb9 144 * Buffer containing the upper layer data.
mluis 0:91d1a7783bb9 145 */
mluis 0:91d1a7783bb9 146 static uint8_t LoRaMacRxPayload[LORAMAC_PHY_MAXPAYLOAD];
mluis 0:91d1a7783bb9 147
mluis 0:91d1a7783bb9 148 /*!
mluis 0:91d1a7783bb9 149 * LoRaMAC frame counter. Each time a packet is sent the counter is incremented.
mluis 0:91d1a7783bb9 150 * Only the 16 LSB bits are sent
mluis 0:91d1a7783bb9 151 */
mluis 8:26002607de9c 152 static uint32_t UpLinkCounter = 0;
mluis 0:91d1a7783bb9 153
mluis 0:91d1a7783bb9 154 /*!
mluis 0:91d1a7783bb9 155 * LoRaMAC frame counter. Each time a packet is received the counter is incremented.
mluis 0:91d1a7783bb9 156 * Only the 16 LSB bits are received
mluis 0:91d1a7783bb9 157 */
mluis 0:91d1a7783bb9 158 static uint32_t DownLinkCounter = 0;
mluis 0:91d1a7783bb9 159
mluis 0:91d1a7783bb9 160 /*!
mluis 0:91d1a7783bb9 161 * IsPacketCounterFixed enables the MIC field tests by fixing the
mluis 0:91d1a7783bb9 162 * UpLinkCounter value
mluis 0:91d1a7783bb9 163 */
mluis 0:91d1a7783bb9 164 static bool IsUpLinkCounterFixed = false;
mluis 0:91d1a7783bb9 165
mluis 0:91d1a7783bb9 166 /*!
mluis 0:91d1a7783bb9 167 * Used for test purposes. Disables the opening of the reception windows.
mluis 0:91d1a7783bb9 168 */
mluis 0:91d1a7783bb9 169 static bool IsRxWindowsEnabled = true;
mluis 0:91d1a7783bb9 170
mluis 0:91d1a7783bb9 171 /*!
mluis 0:91d1a7783bb9 172 * Indicates if the MAC layer has already joined a network.
mluis 0:91d1a7783bb9 173 */
mluis 0:91d1a7783bb9 174 static bool IsLoRaMacNetworkJoined = false;
mluis 0:91d1a7783bb9 175
mluis 0:91d1a7783bb9 176 /*!
mluis 0:91d1a7783bb9 177 * LoRaMac ADR control status
mluis 0:91d1a7783bb9 178 */
mluis 0:91d1a7783bb9 179 static bool AdrCtrlOn = false;
mluis 0:91d1a7783bb9 180
mluis 0:91d1a7783bb9 181 /*!
mluis 0:91d1a7783bb9 182 * Counts the number of missed ADR acknowledgements
mluis 0:91d1a7783bb9 183 */
mluis 0:91d1a7783bb9 184 static uint32_t AdrAckCounter = 0;
mluis 0:91d1a7783bb9 185
mluis 0:91d1a7783bb9 186 /*!
mluis 0:91d1a7783bb9 187 * If the node has sent a FRAME_TYPE_DATA_CONFIRMED_UP this variable indicates
mluis 0:91d1a7783bb9 188 * if the nodes needs to manage the server acknowledgement.
mluis 0:91d1a7783bb9 189 */
mluis 0:91d1a7783bb9 190 static bool NodeAckRequested = false;
mluis 0:91d1a7783bb9 191
mluis 0:91d1a7783bb9 192 /*!
mluis 0:91d1a7783bb9 193 * If the server has sent a FRAME_TYPE_DATA_CONFIRMED_DOWN this variable indicates
mluis 0:91d1a7783bb9 194 * if the ACK bit must be set for the next transmission
mluis 0:91d1a7783bb9 195 */
mluis 0:91d1a7783bb9 196 static bool SrvAckRequested = false;
mluis 0:91d1a7783bb9 197
mluis 0:91d1a7783bb9 198 /*!
mluis 0:91d1a7783bb9 199 * Indicates if the MAC layer wants to send MAC commands
mluis 0:91d1a7783bb9 200 */
mluis 0:91d1a7783bb9 201 static bool MacCommandsInNextTx = false;
mluis 0:91d1a7783bb9 202
mluis 0:91d1a7783bb9 203 /*!
mluis 0:91d1a7783bb9 204 * Contains the current MacCommandsBuffer index
mluis 0:91d1a7783bb9 205 */
mluis 0:91d1a7783bb9 206 static uint8_t MacCommandsBufferIndex = 0;
mluis 0:91d1a7783bb9 207
mluis 0:91d1a7783bb9 208 /*!
mluis 7:c16969e0f70f 209 * Contains the current MacCommandsBuffer index for MAC commands to repeat
mluis 7:c16969e0f70f 210 */
mluis 7:c16969e0f70f 211 static uint8_t MacCommandsBufferToRepeatIndex = 0;
mluis 7:c16969e0f70f 212
mluis 7:c16969e0f70f 213 /*!
mluis 0:91d1a7783bb9 214 * Buffer containing the MAC layer commands
mluis 0:91d1a7783bb9 215 */
mluis 1:91e4e6c60d1e 216 static uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH];
mluis 0:91d1a7783bb9 217
mluis 7:c16969e0f70f 218 /*!
mluis 7:c16969e0f70f 219 * Buffer containing the MAC layer commands which must be repeated
mluis 7:c16969e0f70f 220 */
mluis 7:c16969e0f70f 221 static uint8_t MacCommandsBufferToRepeat[LORA_MAC_COMMAND_MAX_LENGTH];
mluis 7:c16969e0f70f 222
mluis 0:91d1a7783bb9 223 #if defined( USE_BAND_433 )
mluis 0:91d1a7783bb9 224 /*!
mluis 0:91d1a7783bb9 225 * Data rates table definition
mluis 0:91d1a7783bb9 226 */
mluis 0:91d1a7783bb9 227 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 228
mluis 0:91d1a7783bb9 229 /*!
mluis 8:26002607de9c 230 * Bandwidths table definition in Hz
mluis 8:26002607de9c 231 */
mluis 8:26002607de9c 232 const uint32_t Bandwidths[] = { 125e3, 125e3, 125e3, 125e3, 125e3, 125e3, 250e3, 0 };
mluis 8:26002607de9c 233
mluis 8:26002607de9c 234 /*!
mluis 0:91d1a7783bb9 235 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 236 */
mluis 8:26002607de9c 237 const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
mluis 0:91d1a7783bb9 238
mluis 0:91d1a7783bb9 239 /*!
mluis 0:91d1a7783bb9 240 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 241 */
mluis 8:26002607de9c 242 const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
mluis 0:91d1a7783bb9 243
mluis 0:91d1a7783bb9 244 /*!
mluis 0:91d1a7783bb9 245 * Tx output powers table definition
mluis 0:91d1a7783bb9 246 */
mluis 8:26002607de9c 247 const int8_t TxPowers[] = { 10, 7, 4, 1, -2, -5 };
mluis 0:91d1a7783bb9 248
mluis 0:91d1a7783bb9 249 /*!
mluis 0:91d1a7783bb9 250 * LoRaMac bands
mluis 0:91d1a7783bb9 251 */
mluis 0:91d1a7783bb9 252 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 253 {
mluis 0:91d1a7783bb9 254 BAND0,
mluis 0:91d1a7783bb9 255 };
mluis 0:91d1a7783bb9 256
mluis 0:91d1a7783bb9 257 /*!
mluis 0:91d1a7783bb9 258 * LoRaMAC channels
mluis 0:91d1a7783bb9 259 */
mluis 0:91d1a7783bb9 260 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 261 {
mluis 0:91d1a7783bb9 262 LC1,
mluis 0:91d1a7783bb9 263 LC2,
mluis 0:91d1a7783bb9 264 LC3,
mluis 0:91d1a7783bb9 265 };
mluis 8:26002607de9c 266 #elif defined( USE_BAND_470 )
mluis 8:26002607de9c 267
mluis 8:26002607de9c 268 /*!
mluis 8:26002607de9c 269 * Data rates table definition
mluis 8:26002607de9c 270 */
mluis 8:26002607de9c 271 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7 };
mluis 8:26002607de9c 272
mluis 8:26002607de9c 273 /*!
mluis 8:26002607de9c 274 * Bandwidths table definition in Hz
mluis 8:26002607de9c 275 */
mluis 8:26002607de9c 276 const uint32_t Bandwidths[] = { 125e3, 125e3, 125e3, 125e3, 125e3, 125e3 };
mluis 8:26002607de9c 277
mluis 8:26002607de9c 278 /*!
mluis 8:26002607de9c 279 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 8:26002607de9c 280 */
mluis 8:26002607de9c 281 const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 222, 222 };
mluis 8:26002607de9c 282
mluis 8:26002607de9c 283 /*!
mluis 8:26002607de9c 284 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 8:26002607de9c 285 */
mluis 8:26002607de9c 286 const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222 };
mluis 8:26002607de9c 287
mluis 8:26002607de9c 288 /*!
mluis 8:26002607de9c 289 * Tx output powers table definition
mluis 8:26002607de9c 290 */
mluis 8:26002607de9c 291 const int8_t TxPowers[] = { 17, 16, 14, 12, 10, 7, 5, 2 };
mluis 8:26002607de9c 292
mluis 8:26002607de9c 293 /*!
mluis 8:26002607de9c 294 * LoRaMac bands
mluis 8:26002607de9c 295 */
mluis 8:26002607de9c 296 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 8:26002607de9c 297 {
mluis 8:26002607de9c 298 BAND0,
mluis 8:26002607de9c 299 };
mluis 8:26002607de9c 300
mluis 8:26002607de9c 301 /*!
mluis 8:26002607de9c 302 * LoRaMAC channels
mluis 8:26002607de9c 303 */
mluis 8:26002607de9c 304 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
mluis 8:26002607de9c 305
mluis 8:26002607de9c 306 /*!
mluis 8:26002607de9c 307 * Defines the first channel for RX window 1 for CN470 band
mluis 8:26002607de9c 308 */
mluis 8:26002607de9c 309 #define LORAMAC_FIRST_RX1_CHANNEL ( (uint32_t) 500.3e6 )
mluis 8:26002607de9c 310
mluis 8:26002607de9c 311 /*!
mluis 8:26002607de9c 312 * Defines the last channel for RX window 1 for CN470 band
mluis 8:26002607de9c 313 */
mluis 8:26002607de9c 314 #define LORAMAC_LAST_RX1_CHANNEL ( (uint32_t) 509.7e6 )
mluis 8:26002607de9c 315
mluis 8:26002607de9c 316 /*!
mluis 8:26002607de9c 317 * Defines the step width of the channels for RX window 1
mluis 8:26002607de9c 318 */
mluis 8:26002607de9c 319 #define LORAMAC_STEPWIDTH_RX1_CHANNEL ( (uint32_t) 200e3 )
mluis 8:26002607de9c 320
mluis 0:91d1a7783bb9 321 #elif defined( USE_BAND_780 )
mluis 0:91d1a7783bb9 322 /*!
mluis 0:91d1a7783bb9 323 * Data rates table definition
mluis 0:91d1a7783bb9 324 */
mluis 0:91d1a7783bb9 325 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 326
mluis 0:91d1a7783bb9 327 /*!
mluis 8:26002607de9c 328 * Bandwidths table definition in Hz
mluis 8:26002607de9c 329 */
mluis 8:26002607de9c 330 const uint32_t Bandwidths[] = { 125e3, 125e3, 125e3, 125e3, 125e3, 125e3, 250e3, 0 };
mluis 8:26002607de9c 331
mluis 8:26002607de9c 332 /*!
mluis 0:91d1a7783bb9 333 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 334 */
mluis 8:26002607de9c 335 const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
mluis 0:91d1a7783bb9 336
mluis 0:91d1a7783bb9 337 /*!
mluis 0:91d1a7783bb9 338 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 339 */
mluis 8:26002607de9c 340 const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
mluis 0:91d1a7783bb9 341
mluis 0:91d1a7783bb9 342 /*!
mluis 0:91d1a7783bb9 343 * Tx output powers table definition
mluis 0:91d1a7783bb9 344 */
mluis 8:26002607de9c 345 const int8_t TxPowers[] = { 10, 7, 4, 1, -2, -5 };
mluis 0:91d1a7783bb9 346
mluis 0:91d1a7783bb9 347 /*!
mluis 0:91d1a7783bb9 348 * LoRaMac bands
mluis 0:91d1a7783bb9 349 */
mluis 0:91d1a7783bb9 350 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 351 {
mluis 0:91d1a7783bb9 352 BAND0,
mluis 0:91d1a7783bb9 353 };
mluis 0:91d1a7783bb9 354
mluis 0:91d1a7783bb9 355 /*!
mluis 0:91d1a7783bb9 356 * LoRaMAC channels
mluis 0:91d1a7783bb9 357 */
mluis 0:91d1a7783bb9 358 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 359 {
mluis 0:91d1a7783bb9 360 LC1,
mluis 0:91d1a7783bb9 361 LC2,
mluis 0:91d1a7783bb9 362 LC3,
mluis 0:91d1a7783bb9 363 };
mluis 0:91d1a7783bb9 364 #elif defined( USE_BAND_868 )
mluis 0:91d1a7783bb9 365 /*!
mluis 0:91d1a7783bb9 366 * Data rates table definition
mluis 0:91d1a7783bb9 367 */
mluis 0:91d1a7783bb9 368 const uint8_t Datarates[] = { 12, 11, 10, 9, 8, 7, 7, 50 };
mluis 0:91d1a7783bb9 369
mluis 0:91d1a7783bb9 370 /*!
mluis 8:26002607de9c 371 * Bandwidths table definition in Hz
mluis 8:26002607de9c 372 */
mluis 8:26002607de9c 373 const uint32_t Bandwidths[] = { 125e3, 125e3, 125e3, 125e3, 125e3, 125e3, 250e3, 0 };
mluis 8:26002607de9c 374
mluis 8:26002607de9c 375 /*!
mluis 0:91d1a7783bb9 376 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 377 */
mluis 0:91d1a7783bb9 378 const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
mluis 0:91d1a7783bb9 379
mluis 0:91d1a7783bb9 380 /*!
mluis 0:91d1a7783bb9 381 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 382 */
mluis 0:91d1a7783bb9 383 const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
mluis 0:91d1a7783bb9 384
mluis 0:91d1a7783bb9 385 /*!
mluis 0:91d1a7783bb9 386 * Tx output powers table definition
mluis 0:91d1a7783bb9 387 */
mluis 0:91d1a7783bb9 388 const int8_t TxPowers[] = { 20, 14, 11, 8, 5, 2 };
mluis 0:91d1a7783bb9 389
mluis 0:91d1a7783bb9 390 /*!
mluis 0:91d1a7783bb9 391 * LoRaMac bands
mluis 0:91d1a7783bb9 392 */
mluis 0:91d1a7783bb9 393 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 394 {
mluis 0:91d1a7783bb9 395 BAND0,
mluis 0:91d1a7783bb9 396 BAND1,
mluis 0:91d1a7783bb9 397 BAND2,
mluis 0:91d1a7783bb9 398 BAND3,
mluis 0:91d1a7783bb9 399 BAND4,
mluis 0:91d1a7783bb9 400 };
mluis 0:91d1a7783bb9 401
mluis 0:91d1a7783bb9 402 /*!
mluis 0:91d1a7783bb9 403 * LoRaMAC channels
mluis 0:91d1a7783bb9 404 */
mluis 0:91d1a7783bb9 405 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
mluis 0:91d1a7783bb9 406 {
mluis 0:91d1a7783bb9 407 LC1,
cdebank 11:de2cdd1f253e 408 //Commented out by Chris
cdebank 11:de2cdd1f253e 409 //LC2,
cdebank 11:de2cdd1f253e 410 //LC3,
mluis 0:91d1a7783bb9 411 };
mluis 0:91d1a7783bb9 412 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 0:91d1a7783bb9 413 /*!
mluis 0:91d1a7783bb9 414 * Data rates table definition
mluis 0:91d1a7783bb9 415 */
mluis 0:91d1a7783bb9 416 const uint8_t Datarates[] = { 10, 9, 8, 7, 8, 0, 0, 0, 12, 11, 10, 9, 8, 7, 0, 0 };
mluis 0:91d1a7783bb9 417
mluis 0:91d1a7783bb9 418 /*!
mluis 8:26002607de9c 419 * Bandwidths table definition in Hz
mluis 8:26002607de9c 420 */
mluis 8:26002607de9c 421 const uint32_t Bandwidths[] = { 125e3, 125e3, 125e3, 125e3, 500e3, 0, 0, 0, 500e3, 500e3, 500e3, 500e3, 500e3, 500e3, 0, 0 };
mluis 8:26002607de9c 422
mluis 8:26002607de9c 423 /*!
mluis 0:91d1a7783bb9 424 * Up/Down link data rates offset definition
mluis 0:91d1a7783bb9 425 */
mluis 8:26002607de9c 426 const int8_t DatarateOffsets[5][4] =
mluis 0:91d1a7783bb9 427 {
mluis 0:91d1a7783bb9 428 { DR_10, DR_9 , DR_8 , DR_8 }, // DR_0
mluis 0:91d1a7783bb9 429 { DR_11, DR_10, DR_9 , DR_8 }, // DR_1
mluis 0:91d1a7783bb9 430 { DR_12, DR_11, DR_10, DR_9 }, // DR_2
mluis 0:91d1a7783bb9 431 { DR_13, DR_12, DR_11, DR_10 }, // DR_3
mluis 0:91d1a7783bb9 432 { DR_13, DR_13, DR_12, DR_11 }, // DR_4
mluis 0:91d1a7783bb9 433 };
mluis 0:91d1a7783bb9 434
mluis 0:91d1a7783bb9 435 /*!
mluis 0:91d1a7783bb9 436 * Maximum payload with respect to the datarate index. Cannot operate with repeater.
mluis 0:91d1a7783bb9 437 */
mluis 7:c16969e0f70f 438 const uint8_t MaxPayloadOfDatarate[] = { 11, 53, 125, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
mluis 0:91d1a7783bb9 439
mluis 0:91d1a7783bb9 440 /*!
mluis 0:91d1a7783bb9 441 * Maximum payload with respect to the datarate index. Can operate with repeater.
mluis 0:91d1a7783bb9 442 */
mluis 7:c16969e0f70f 443 const uint8_t MaxPayloadOfDatarateRepeater[] = { 11, 53, 125, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0 };
mluis 0:91d1a7783bb9 444
mluis 0:91d1a7783bb9 445 /*!
mluis 0:91d1a7783bb9 446 * Tx output powers table definition
mluis 0:91d1a7783bb9 447 */
mluis 0:91d1a7783bb9 448 const int8_t TxPowers[] = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
mluis 0:91d1a7783bb9 449
mluis 0:91d1a7783bb9 450 /*!
mluis 0:91d1a7783bb9 451 * LoRaMac bands
mluis 0:91d1a7783bb9 452 */
mluis 0:91d1a7783bb9 453 static Band_t Bands[LORA_MAX_NB_BANDS] =
mluis 0:91d1a7783bb9 454 {
mluis 0:91d1a7783bb9 455 BAND0,
mluis 0:91d1a7783bb9 456 };
mluis 0:91d1a7783bb9 457
mluis 0:91d1a7783bb9 458 /*!
mluis 0:91d1a7783bb9 459 * LoRaMAC channels
mluis 0:91d1a7783bb9 460 */
mluis 0:91d1a7783bb9 461 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
mluis 0:91d1a7783bb9 462
mluis 2:14a5d6ad92d5 463 /*!
mluis 2:14a5d6ad92d5 464 * Contains the channels which remain to be applied.
mluis 2:14a5d6ad92d5 465 */
mluis 2:14a5d6ad92d5 466 static uint16_t ChannelsMaskRemaining[6];
mluis 7:c16969e0f70f 467
mluis 7:c16969e0f70f 468 /*!
mluis 8:26002607de9c 469 * Defines the first channel for RX window 1 for US band
mluis 7:c16969e0f70f 470 */
mluis 8:26002607de9c 471 #define LORAMAC_FIRST_RX1_CHANNEL ( (uint32_t) 923.3e6 )
mluis 7:c16969e0f70f 472
mluis 7:c16969e0f70f 473 /*!
mluis 8:26002607de9c 474 * Defines the last channel for RX window 1 for US band
mluis 7:c16969e0f70f 475 */
mluis 8:26002607de9c 476 #define LORAMAC_LAST_RX1_CHANNEL ( (uint32_t) 927.5e6 )
mluis 7:c16969e0f70f 477
mluis 7:c16969e0f70f 478 /*!
mluis 8:26002607de9c 479 * Defines the step width of the channels for RX window 1
mluis 7:c16969e0f70f 480 */
mluis 8:26002607de9c 481 #define LORAMAC_STEPWIDTH_RX1_CHANNEL ( (uint32_t) 600e3 )
mluis 7:c16969e0f70f 482
mluis 0:91d1a7783bb9 483 #else
mluis 0:91d1a7783bb9 484 #error "Please define a frequency band in the compiler options."
mluis 0:91d1a7783bb9 485 #endif
mluis 0:91d1a7783bb9 486
mluis 0:91d1a7783bb9 487 /*!
mluis 7:c16969e0f70f 488 * LoRaMac parameters
mluis 0:91d1a7783bb9 489 */
mluis 7:c16969e0f70f 490 LoRaMacParams_t LoRaMacParams;
mluis 0:91d1a7783bb9 491
mluis 0:91d1a7783bb9 492 /*!
mluis 7:c16969e0f70f 493 * LoRaMac default parameters
mluis 0:91d1a7783bb9 494 */
mluis 7:c16969e0f70f 495 LoRaMacParams_t LoRaMacParamsDefaults;
mluis 0:91d1a7783bb9 496
mluis 0:91d1a7783bb9 497 /*!
mluis 0:91d1a7783bb9 498 * Uplink messages repetitions counter
mluis 0:91d1a7783bb9 499 */
mluis 0:91d1a7783bb9 500 static uint8_t ChannelsNbRepCounter = 0;
mluis 0:91d1a7783bb9 501
mluis 0:91d1a7783bb9 502 /*!
mluis 0:91d1a7783bb9 503 * Maximum duty cycle
mluis 0:91d1a7783bb9 504 * \remark Possibility to shutdown the device.
mluis 0:91d1a7783bb9 505 */
mluis 0:91d1a7783bb9 506 static uint8_t MaxDCycle = 0;
mluis 0:91d1a7783bb9 507
mluis 0:91d1a7783bb9 508 /*!
mluis 2:14a5d6ad92d5 509 * Aggregated duty cycle management
mluis 0:91d1a7783bb9 510 */
mluis 0:91d1a7783bb9 511 static uint16_t AggregatedDCycle;
mluis 0:91d1a7783bb9 512 static TimerTime_t AggregatedLastTxDoneTime;
mluis 0:91d1a7783bb9 513 static TimerTime_t AggregatedTimeOff;
mluis 0:91d1a7783bb9 514
mluis 0:91d1a7783bb9 515 /*!
mluis 0:91d1a7783bb9 516 * Enables/Disables duty cycle management (Test only)
mluis 0:91d1a7783bb9 517 */
mluis 0:91d1a7783bb9 518 static bool DutyCycleOn;
mluis 0:91d1a7783bb9 519
mluis 0:91d1a7783bb9 520 /*!
mluis 0:91d1a7783bb9 521 * Current channel index
mluis 0:91d1a7783bb9 522 */
mluis 0:91d1a7783bb9 523 static uint8_t Channel;
mluis 0:91d1a7783bb9 524
mluis 7:c16969e0f70f 525 /*!
mluis 8:26002607de9c 526 * Stores the time at LoRaMac initialization.
mluis 8:26002607de9c 527 *
mluis 8:26002607de9c 528 * \remark Used for the BACKOFF_DC computation.
mluis 7:c16969e0f70f 529 */
mluis 8:26002607de9c 530 static TimerTime_t LoRaMacInitializationTime = 0;
mluis 4:37c12dbc8dc7 531
mluis 0:91d1a7783bb9 532 /*!
mluis 0:91d1a7783bb9 533 * LoRaMac internal states
mluis 0:91d1a7783bb9 534 */
mluis 2:14a5d6ad92d5 535 enum eLoRaMacState
mluis 0:91d1a7783bb9 536 {
mluis 8:26002607de9c 537 LORAMAC_IDLE = 0x00000000,
mluis 8:26002607de9c 538 LORAMAC_TX_RUNNING = 0x00000001,
mluis 8:26002607de9c 539 LORAMAC_RX = 0x00000002,
mluis 8:26002607de9c 540 LORAMAC_ACK_REQ = 0x00000004,
mluis 8:26002607de9c 541 LORAMAC_ACK_RETRY = 0x00000008,
mluis 8:26002607de9c 542 LORAMAC_TX_DELAYED = 0x00000010,
mluis 8:26002607de9c 543 LORAMAC_TX_CONFIG = 0x00000020,
mluis 8:26002607de9c 544 LORAMAC_RX_ABORT = 0x00000040,
mluis 0:91d1a7783bb9 545 };
mluis 0:91d1a7783bb9 546
mluis 0:91d1a7783bb9 547 /*!
mluis 0:91d1a7783bb9 548 * LoRaMac internal state
mluis 0:91d1a7783bb9 549 */
mluis 8:26002607de9c 550 uint32_t LoRaMacState = LORAMAC_IDLE;
mluis 0:91d1a7783bb9 551
mluis 0:91d1a7783bb9 552 /*!
mluis 0:91d1a7783bb9 553 * LoRaMac timer used to check the LoRaMacState (runs every second)
mluis 0:91d1a7783bb9 554 */
mluis 0:91d1a7783bb9 555 static TimerEvent_t MacStateCheckTimer;
mluis 0:91d1a7783bb9 556
mluis 0:91d1a7783bb9 557 /*!
mluis 0:91d1a7783bb9 558 * LoRaMac upper layer event functions
mluis 0:91d1a7783bb9 559 */
mluis 2:14a5d6ad92d5 560 static LoRaMacPrimitives_t *LoRaMacPrimitives;
mluis 0:91d1a7783bb9 561
mluis 0:91d1a7783bb9 562 /*!
mluis 2:14a5d6ad92d5 563 * LoRaMac upper layer callback functions
mluis 0:91d1a7783bb9 564 */
mluis 2:14a5d6ad92d5 565 static LoRaMacCallback_t *LoRaMacCallbacks;
mluis 0:91d1a7783bb9 566
mluis 0:91d1a7783bb9 567 /*!
mluis 2:14a5d6ad92d5 568 * Radio events function pointer
mluis 0:91d1a7783bb9 569 */
mluis 2:14a5d6ad92d5 570 static RadioEvents_t RadioEvents;
mluis 0:91d1a7783bb9 571
mluis 0:91d1a7783bb9 572 /*!
mluis 0:91d1a7783bb9 573 * LoRaMac duty cycle delayed Tx timer
mluis 0:91d1a7783bb9 574 */
mluis 0:91d1a7783bb9 575 static TimerEvent_t TxDelayedTimer;
mluis 0:91d1a7783bb9 576
mluis 0:91d1a7783bb9 577 /*!
mluis 0:91d1a7783bb9 578 * LoRaMac reception windows timers
mluis 0:91d1a7783bb9 579 */
mluis 0:91d1a7783bb9 580 static TimerEvent_t RxWindowTimer1;
mluis 0:91d1a7783bb9 581 static TimerEvent_t RxWindowTimer2;
mluis 0:91d1a7783bb9 582
mluis 0:91d1a7783bb9 583 /*!
mluis 0:91d1a7783bb9 584 * LoRaMac reception windows delay
mluis 0:91d1a7783bb9 585 * \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
mluis 0:91d1a7783bb9 586 * join frame : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
mluis 0:91d1a7783bb9 587 */
mluis 0:91d1a7783bb9 588 static uint32_t RxWindow1Delay;
mluis 0:91d1a7783bb9 589 static uint32_t RxWindow2Delay;
mluis 0:91d1a7783bb9 590
mluis 0:91d1a7783bb9 591 /*!
mluis 8:26002607de9c 592 * Rx window parameters
mluis 8:26002607de9c 593 */
mluis 8:26002607de9c 594 typedef struct
mluis 8:26002607de9c 595 {
mluis 8:26002607de9c 596 int8_t Datarate;
mluis 8:26002607de9c 597 uint8_t Bandwidth;
mluis 8:26002607de9c 598 uint32_t RxWindowTimeout;
mluis 8:26002607de9c 599 int32_t RxOffset;
mluis 8:26002607de9c 600 }RxConfigParams_t;
mluis 8:26002607de9c 601
mluis 8:26002607de9c 602 /*!
mluis 8:26002607de9c 603 * Rx windows params
mluis 8:26002607de9c 604 */
mluis 8:26002607de9c 605 static RxConfigParams_t RxWindowsParams[2];
mluis 8:26002607de9c 606
mluis 8:26002607de9c 607 /*!
mluis 0:91d1a7783bb9 608 * Acknowledge timeout timer. Used for packet retransmissions.
mluis 0:91d1a7783bb9 609 */
mluis 0:91d1a7783bb9 610 static TimerEvent_t AckTimeoutTimer;
mluis 0:91d1a7783bb9 611
mluis 0:91d1a7783bb9 612 /*!
mluis 0:91d1a7783bb9 613 * Number of trials to get a frame acknowledged
mluis 0:91d1a7783bb9 614 */
mluis 0:91d1a7783bb9 615 static uint8_t AckTimeoutRetries = 1;
mluis 0:91d1a7783bb9 616
mluis 0:91d1a7783bb9 617 /*!
mluis 0:91d1a7783bb9 618 * Number of trials to get a frame acknowledged
mluis 0:91d1a7783bb9 619 */
mluis 0:91d1a7783bb9 620 static uint8_t AckTimeoutRetriesCounter = 1;
mluis 0:91d1a7783bb9 621
mluis 0:91d1a7783bb9 622 /*!
mluis 0:91d1a7783bb9 623 * Indicates if the AckTimeout timer has expired or not
mluis 0:91d1a7783bb9 624 */
mluis 0:91d1a7783bb9 625 static bool AckTimeoutRetry = false;
mluis 0:91d1a7783bb9 626
mluis 0:91d1a7783bb9 627 /*!
mluis 0:91d1a7783bb9 628 * Last transmission time on air
mluis 0:91d1a7783bb9 629 */
mluis 0:91d1a7783bb9 630 TimerTime_t TxTimeOnAir = 0;
mluis 0:91d1a7783bb9 631
mluis 0:91d1a7783bb9 632 /*!
mluis 7:c16969e0f70f 633 * Number of trials for the Join Request
mluis 7:c16969e0f70f 634 */
mluis 8:26002607de9c 635 static uint8_t JoinRequestTrials;
mluis 8:26002607de9c 636
mluis 8:26002607de9c 637 /*!
mluis 8:26002607de9c 638 * Maximum number of trials for the Join Request
mluis 8:26002607de9c 639 */
mluis 8:26002607de9c 640 static uint8_t MaxJoinRequestTrials;
mluis 7:c16969e0f70f 641
mluis 7:c16969e0f70f 642 /*!
mluis 2:14a5d6ad92d5 643 * Structure to hold an MCPS indication data.
mluis 2:14a5d6ad92d5 644 */
mluis 2:14a5d6ad92d5 645 static McpsIndication_t McpsIndication;
mluis 2:14a5d6ad92d5 646
mluis 2:14a5d6ad92d5 647 /*!
mluis 2:14a5d6ad92d5 648 * Structure to hold MCPS confirm data.
mluis 2:14a5d6ad92d5 649 */
mluis 2:14a5d6ad92d5 650 static McpsConfirm_t McpsConfirm;
mluis 2:14a5d6ad92d5 651
mluis 2:14a5d6ad92d5 652 /*!
mluis 2:14a5d6ad92d5 653 * Structure to hold MLME confirm data.
mluis 2:14a5d6ad92d5 654 */
mluis 2:14a5d6ad92d5 655 static MlmeConfirm_t MlmeConfirm;
mluis 2:14a5d6ad92d5 656
mluis 2:14a5d6ad92d5 657 /*!
mluis 2:14a5d6ad92d5 658 * Holds the current rx window slot
mluis 2:14a5d6ad92d5 659 */
mluis 2:14a5d6ad92d5 660 static uint8_t RxSlot = 0;
mluis 2:14a5d6ad92d5 661
mluis 2:14a5d6ad92d5 662 /*!
mluis 2:14a5d6ad92d5 663 * LoRaMac tx/rx operation state
mluis 2:14a5d6ad92d5 664 */
mluis 2:14a5d6ad92d5 665 LoRaMacFlags_t LoRaMacFlags;
mluis 2:14a5d6ad92d5 666
mluis 2:14a5d6ad92d5 667 /*!
mluis 2:14a5d6ad92d5 668 * \brief Function to be executed on Radio Tx Done event
mluis 0:91d1a7783bb9 669 */
mluis 0:91d1a7783bb9 670 static void OnRadioTxDone( void );
mluis 0:91d1a7783bb9 671
mluis 0:91d1a7783bb9 672 /*!
mluis 3:b9d87593a8ae 673 * \brief This function prepares the MAC to abort the execution of function
mluis 3:b9d87593a8ae 674 * OnRadioRxDone in case of a reception error.
mluis 3:b9d87593a8ae 675 */
mluis 3:b9d87593a8ae 676 static void PrepareRxDoneAbort( void );
mluis 3:b9d87593a8ae 677
mluis 3:b9d87593a8ae 678 /*!
mluis 2:14a5d6ad92d5 679 * \brief Function to be executed on Radio Rx Done event
mluis 0:91d1a7783bb9 680 */
mluis 0:91d1a7783bb9 681 static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
mluis 0:91d1a7783bb9 682
mluis 0:91d1a7783bb9 683 /*!
mluis 2:14a5d6ad92d5 684 * \brief Function executed on Radio Tx Timeout event
mluis 0:91d1a7783bb9 685 */
mluis 0:91d1a7783bb9 686 static void OnRadioTxTimeout( void );
mluis 0:91d1a7783bb9 687
mluis 0:91d1a7783bb9 688 /*!
mluis 2:14a5d6ad92d5 689 * \brief Function executed on Radio Rx error event
mluis 0:91d1a7783bb9 690 */
mluis 0:91d1a7783bb9 691 static void OnRadioRxError( void );
mluis 0:91d1a7783bb9 692
mluis 0:91d1a7783bb9 693 /*!
mluis 2:14a5d6ad92d5 694 * \brief Function executed on Radio Rx Timeout event
mluis 0:91d1a7783bb9 695 */
mluis 0:91d1a7783bb9 696 static void OnRadioRxTimeout( void );
mluis 0:91d1a7783bb9 697
mluis 0:91d1a7783bb9 698 /*!
mluis 2:14a5d6ad92d5 699 * \brief Function executed on Resend Frame timer event.
mluis 0:91d1a7783bb9 700 */
mluis 0:91d1a7783bb9 701 static void OnMacStateCheckTimerEvent( void );
mluis 0:91d1a7783bb9 702
mluis 0:91d1a7783bb9 703 /*!
mluis 2:14a5d6ad92d5 704 * \brief Function executed on duty cycle delayed Tx timer event
mluis 0:91d1a7783bb9 705 */
mluis 0:91d1a7783bb9 706 static void OnTxDelayedTimerEvent( void );
mluis 0:91d1a7783bb9 707
mluis 0:91d1a7783bb9 708 /*!
mluis 2:14a5d6ad92d5 709 * \brief Function executed on first Rx window timer event
mluis 0:91d1a7783bb9 710 */
mluis 0:91d1a7783bb9 711 static void OnRxWindow1TimerEvent( void );
mluis 0:91d1a7783bb9 712
mluis 0:91d1a7783bb9 713 /*!
mluis 2:14a5d6ad92d5 714 * \brief Function executed on second Rx window timer event
mluis 0:91d1a7783bb9 715 */
mluis 0:91d1a7783bb9 716 static void OnRxWindow2TimerEvent( void );
mluis 0:91d1a7783bb9 717
mluis 0:91d1a7783bb9 718 /*!
mluis 2:14a5d6ad92d5 719 * \brief Function executed on AckTimeout timer event
mluis 0:91d1a7783bb9 720 */
mluis 0:91d1a7783bb9 721 static void OnAckTimeoutTimerEvent( void );
mluis 0:91d1a7783bb9 722
mluis 0:91d1a7783bb9 723 /*!
mluis 2:14a5d6ad92d5 724 * \brief Searches and set the next random available channel
mluis 2:14a5d6ad92d5 725 *
mluis 3:b9d87593a8ae 726 * \param [OUT] Time to wait for the next transmission according to the duty
mluis 3:b9d87593a8ae 727 * cycle.
mluis 3:b9d87593a8ae 728 *
mluis 3:b9d87593a8ae 729 * \retval status Function status [1: OK, 0: Unable to find a channel on the
mluis 3:b9d87593a8ae 730 * current datarate]
mluis 2:14a5d6ad92d5 731 */
mluis 3:b9d87593a8ae 732 static bool SetNextChannel( TimerTime_t* time );
mluis 2:14a5d6ad92d5 733
mluis 2:14a5d6ad92d5 734 /*!
mluis 2:14a5d6ad92d5 735 * \brief Initializes and opens the reception window
mluis 2:14a5d6ad92d5 736 *
mluis 2:14a5d6ad92d5 737 * \param [IN] freq window channel frequency
mluis 2:14a5d6ad92d5 738 * \param [IN] datarate window channel datarate
mluis 2:14a5d6ad92d5 739 * \param [IN] bandwidth window channel bandwidth
mluis 2:14a5d6ad92d5 740 * \param [IN] timeout window channel timeout
mluis 8:26002607de9c 741 *
mluis 8:26002607de9c 742 * \retval status Operation status [true: Success, false: Fail]
mluis 0:91d1a7783bb9 743 */
mluis 8:26002607de9c 744 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous );
mluis 2:14a5d6ad92d5 745
mluis 2:14a5d6ad92d5 746 /*!
mluis 7:c16969e0f70f 747 * \brief Verifies if the RX window 2 frequency is in range
mluis 7:c16969e0f70f 748 *
mluis 7:c16969e0f70f 749 * \param [IN] freq window channel frequency
mluis 7:c16969e0f70f 750 *
mluis 7:c16969e0f70f 751 * \retval status Function status [1: OK, 0: Frequency not applicable]
mluis 7:c16969e0f70f 752 */
mluis 7:c16969e0f70f 753 static bool Rx2FreqInRange( uint32_t freq );
mluis 7:c16969e0f70f 754
mluis 7:c16969e0f70f 755 /*!
mluis 2:14a5d6ad92d5 756 * \brief Adds a new MAC command to be sent.
mluis 2:14a5d6ad92d5 757 *
mluis 2:14a5d6ad92d5 758 * \Remark MAC layer internal function
mluis 2:14a5d6ad92d5 759 *
mluis 2:14a5d6ad92d5 760 * \param [in] cmd MAC command to be added
mluis 2:14a5d6ad92d5 761 * [MOTE_MAC_LINK_CHECK_REQ,
mluis 2:14a5d6ad92d5 762 * MOTE_MAC_LINK_ADR_ANS,
mluis 2:14a5d6ad92d5 763 * MOTE_MAC_DUTY_CYCLE_ANS,
mluis 2:14a5d6ad92d5 764 * MOTE_MAC_RX2_PARAM_SET_ANS,
mluis 2:14a5d6ad92d5 765 * MOTE_MAC_DEV_STATUS_ANS
mluis 2:14a5d6ad92d5 766 * MOTE_MAC_NEW_CHANNEL_ANS]
mluis 2:14a5d6ad92d5 767 * \param [in] p1 1st parameter ( optional depends on the command )
mluis 2:14a5d6ad92d5 768 * \param [in] p2 2nd parameter ( optional depends on the command )
mluis 2:14a5d6ad92d5 769 *
mluis 2:14a5d6ad92d5 770 * \retval status Function status [0: OK, 1: Unknown command, 2: Buffer full]
mluis 2:14a5d6ad92d5 771 */
mluis 2:14a5d6ad92d5 772 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
mluis 0:91d1a7783bb9 773
mluis 0:91d1a7783bb9 774 /*!
mluis 7:c16969e0f70f 775 * \brief Parses the MAC commands which must be repeated.
mluis 7:c16969e0f70f 776 *
mluis 7:c16969e0f70f 777 * \Remark MAC layer internal function
mluis 7:c16969e0f70f 778 *
mluis 7:c16969e0f70f 779 * \param [IN] cmdBufIn Buffer which stores the MAC commands to send
mluis 7:c16969e0f70f 780 * \param [IN] length Length of the input buffer to parse
mluis 7:c16969e0f70f 781 * \param [OUT] cmdBufOut Buffer which stores the MAC commands which must be
mluis 7:c16969e0f70f 782 * repeated.
mluis 7:c16969e0f70f 783 *
mluis 7:c16969e0f70f 784 * \retval Size of the MAC commands to repeat.
mluis 7:c16969e0f70f 785 */
mluis 7:c16969e0f70f 786 static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut );
mluis 7:c16969e0f70f 787
mluis 7:c16969e0f70f 788 /*!
mluis 0:91d1a7783bb9 789 * \brief Validates if the payload fits into the frame, taking the datarate
mluis 0:91d1a7783bb9 790 * into account.
mluis 0:91d1a7783bb9 791 *
mluis 0:91d1a7783bb9 792 * \details Refer to chapter 4.3.2 of the LoRaWAN specification, v1.0
mluis 0:91d1a7783bb9 793 *
mluis 0:91d1a7783bb9 794 * \param lenN Length of the application payload. The length depends on the
mluis 0:91d1a7783bb9 795 * datarate and is region specific
mluis 0:91d1a7783bb9 796 *
mluis 0:91d1a7783bb9 797 * \param datarate Current datarate
mluis 0:91d1a7783bb9 798 *
mluis 2:14a5d6ad92d5 799 * \param fOptsLen Length of the fOpts field
mluis 2:14a5d6ad92d5 800 *
mluis 0:91d1a7783bb9 801 * \retval [false: payload does not fit into the frame, true: payload fits into
mluis 0:91d1a7783bb9 802 * the frame]
mluis 0:91d1a7783bb9 803 */
mluis 2:14a5d6ad92d5 804 static bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen );
mluis 0:91d1a7783bb9 805
mluis 4:37c12dbc8dc7 806 /*!
mluis 4:37c12dbc8dc7 807 * \brief Counts the number of bits in a mask.
mluis 4:37c12dbc8dc7 808 *
mluis 4:37c12dbc8dc7 809 * \param [IN] mask A mask from which the function counts the active bits.
mluis 4:37c12dbc8dc7 810 * \param [IN] nbBits The number of bits to check.
mluis 4:37c12dbc8dc7 811 *
mluis 4:37c12dbc8dc7 812 * \retval Number of enabled bits in the mask.
mluis 4:37c12dbc8dc7 813 */
mluis 4:37c12dbc8dc7 814 static uint8_t CountBits( uint16_t mask, uint8_t nbBits );
mluis 4:37c12dbc8dc7 815
mluis 0:91d1a7783bb9 816 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 0:91d1a7783bb9 817 /*!
mluis 0:91d1a7783bb9 818 * \brief Counts the number of enabled 125 kHz channels in the channel mask.
mluis 0:91d1a7783bb9 819 * This function can only be applied to US915 band.
mluis 0:91d1a7783bb9 820 *
mluis 4:37c12dbc8dc7 821 * \param [IN] channelsMask Pointer to the first element of the channel mask
mluis 0:91d1a7783bb9 822 *
mluis 0:91d1a7783bb9 823 * \retval Number of enabled channels in the channel mask
mluis 0:91d1a7783bb9 824 */
mluis 0:91d1a7783bb9 825 static uint8_t CountNbEnabled125kHzChannels( uint16_t *channelsMask );
mluis 4:37c12dbc8dc7 826
mluis 4:37c12dbc8dc7 827 #if defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 828 /*!
mluis 4:37c12dbc8dc7 829 * \brief Validates the correctness of the channel mask for US915, hybrid mode.
mluis 4:37c12dbc8dc7 830 *
mluis 4:37c12dbc8dc7 831 * \param [IN] mask Block definition to set.
mluis 4:37c12dbc8dc7 832 * \param [OUT] channelsMask Pointer to the first element of the channel mask
mluis 4:37c12dbc8dc7 833 */
mluis 8:26002607de9c 834 static void ReenableChannels( uint16_t mask, uint16_t* channelsMask );
mluis 4:37c12dbc8dc7 835
mluis 4:37c12dbc8dc7 836 /*!
mluis 4:37c12dbc8dc7 837 * \brief Validates the correctness of the channel mask for US915, hybrid mode.
mluis 4:37c12dbc8dc7 838 *
mluis 4:37c12dbc8dc7 839 * \param [IN] channelsMask Pointer to the first element of the channel mask
mluis 4:37c12dbc8dc7 840 *
mluis 4:37c12dbc8dc7 841 * \retval [true: channel mask correct, false: channel mask not correct]
mluis 4:37c12dbc8dc7 842 */
mluis 8:26002607de9c 843 static bool ValidateChannelMask( uint16_t* channelsMask );
mluis 4:37c12dbc8dc7 844 #endif
mluis 4:37c12dbc8dc7 845
mluis 0:91d1a7783bb9 846 #endif
mluis 0:91d1a7783bb9 847
mluis 0:91d1a7783bb9 848 /*!
mluis 8:26002607de9c 849 * \brief Validates the correctness of the datarate against the enable channels.
mluis 8:26002607de9c 850 *
mluis 8:26002607de9c 851 * \param [IN] datarate Datarate to be check
mluis 8:26002607de9c 852 * \param [IN] channelsMask Pointer to the first element of the channel mask
mluis 8:26002607de9c 853 *
mluis 8:26002607de9c 854 * \retval [true: datarate can be used, false: datarate can not be used]
mluis 8:26002607de9c 855 */
mluis 8:26002607de9c 856 static bool ValidateDatarate( int8_t datarate, uint16_t* channelsMask );
mluis 8:26002607de9c 857
mluis 8:26002607de9c 858 /*!
mluis 0:91d1a7783bb9 859 * \brief Limits the Tx power according to the number of enabled channels
mluis 0:91d1a7783bb9 860 *
mluis 8:26002607de9c 861 * \param [IN] txPower txPower to limit
mluis 8:26002607de9c 862 * \param [IN] maxBandTxPower Maximum band allowed TxPower
mluis 8:26002607de9c 863 *
mluis 0:91d1a7783bb9 864 * \retval Returns the maximum valid tx power
mluis 0:91d1a7783bb9 865 */
mluis 8:26002607de9c 866 static int8_t LimitTxPower( int8_t txPower, int8_t maxBandTxPower );
mluis 0:91d1a7783bb9 867
mluis 0:91d1a7783bb9 868 /*!
mluis 2:14a5d6ad92d5 869 * \brief Verifies, if a value is in a given range.
mluis 2:14a5d6ad92d5 870 *
mluis 2:14a5d6ad92d5 871 * \param value Value to verify, if it is in range
mluis 2:14a5d6ad92d5 872 *
mluis 2:14a5d6ad92d5 873 * \param min Minimum possible value
mluis 2:14a5d6ad92d5 874 *
mluis 2:14a5d6ad92d5 875 * \param max Maximum possible value
mluis 2:14a5d6ad92d5 876 *
mluis 2:14a5d6ad92d5 877 * \retval Returns the maximum valid tx power
mluis 2:14a5d6ad92d5 878 */
mluis 2:14a5d6ad92d5 879 static bool ValueInRange( int8_t value, int8_t min, int8_t max );
mluis 2:14a5d6ad92d5 880
mluis 2:14a5d6ad92d5 881 /*!
mluis 2:14a5d6ad92d5 882 * \brief Calculates the next datarate to set, when ADR is on or off
mluis 2:14a5d6ad92d5 883 *
mluis 2:14a5d6ad92d5 884 * \param [IN] adrEnabled Specify whether ADR is on or off
mluis 2:14a5d6ad92d5 885 *
mluis 2:14a5d6ad92d5 886 * \param [IN] updateChannelMask Set to true, if the channel masks shall be updated
mluis 2:14a5d6ad92d5 887 *
mluis 2:14a5d6ad92d5 888 * \param [OUT] datarateOut Reports the datarate which will be used next
mluis 2:14a5d6ad92d5 889 *
mluis 2:14a5d6ad92d5 890 * \retval Returns the state of ADR ack request
mluis 2:14a5d6ad92d5 891 */
mluis 2:14a5d6ad92d5 892 static bool AdrNextDr( bool adrEnabled, bool updateChannelMask, int8_t* datarateOut );
mluis 2:14a5d6ad92d5 893
mluis 2:14a5d6ad92d5 894 /*!
mluis 2:14a5d6ad92d5 895 * \brief Disables channel in a specified channel mask
mluis 2:14a5d6ad92d5 896 *
mluis 2:14a5d6ad92d5 897 * \param [IN] id - Id of the channel
mluis 2:14a5d6ad92d5 898 *
mluis 2:14a5d6ad92d5 899 * \param [IN] mask - Pointer to the channel mask to edit
mluis 0:91d1a7783bb9 900 *
mluis 2:14a5d6ad92d5 901 * \retval [true, if disable was successful, false if not]
mluis 2:14a5d6ad92d5 902 */
mluis 2:14a5d6ad92d5 903 static bool DisableChannelInMask( uint8_t id, uint16_t* mask );
mluis 2:14a5d6ad92d5 904
mluis 2:14a5d6ad92d5 905 /*!
mluis 2:14a5d6ad92d5 906 * \brief Decodes MAC commands in the fOpts field and in the payload
mluis 2:14a5d6ad92d5 907 */
mluis 2:14a5d6ad92d5 908 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr );
mluis 2:14a5d6ad92d5 909
mluis 2:14a5d6ad92d5 910 /*!
mluis 2:14a5d6ad92d5 911 * \brief LoRaMAC layer generic send frame
mluis 2:14a5d6ad92d5 912 *
mluis 2:14a5d6ad92d5 913 * \param [IN] macHdr MAC header field
mluis 2:14a5d6ad92d5 914 * \param [IN] fPort MAC payload port
mluis 2:14a5d6ad92d5 915 * \param [IN] fBuffer MAC data buffer to be sent
mluis 2:14a5d6ad92d5 916 * \param [IN] fBufferSize MAC data buffer size
mluis 2:14a5d6ad92d5 917 * \retval status Status of the operation.
mluis 2:14a5d6ad92d5 918 */
mluis 2:14a5d6ad92d5 919 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
mluis 2:14a5d6ad92d5 920
mluis 2:14a5d6ad92d5 921 /*!
mluis 2:14a5d6ad92d5 922 * \brief LoRaMAC layer frame buffer initialization
mluis 2:14a5d6ad92d5 923 *
mluis 2:14a5d6ad92d5 924 * \param [IN] macHdr MAC header field
mluis 2:14a5d6ad92d5 925 * \param [IN] fCtrl MAC frame control field
mluis 2:14a5d6ad92d5 926 * \param [IN] fOpts MAC commands buffer
mluis 2:14a5d6ad92d5 927 * \param [IN] fPort MAC payload port
mluis 2:14a5d6ad92d5 928 * \param [IN] fBuffer MAC data buffer to be sent
mluis 2:14a5d6ad92d5 929 * \param [IN] fBufferSize MAC data buffer size
mluis 2:14a5d6ad92d5 930 * \retval status Status of the operation.
mluis 2:14a5d6ad92d5 931 */
mluis 2:14a5d6ad92d5 932 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
mluis 2:14a5d6ad92d5 933
mluis 2:14a5d6ad92d5 934 /*
mluis 2:14a5d6ad92d5 935 * \brief Schedules the frame according to the duty cycle
mluis 2:14a5d6ad92d5 936 *
mluis 2:14a5d6ad92d5 937 * \retval Status of the operation
mluis 2:14a5d6ad92d5 938 */
mluis 2:14a5d6ad92d5 939 static LoRaMacStatus_t ScheduleTx( void );
mluis 2:14a5d6ad92d5 940
mluis 4:37c12dbc8dc7 941 /*
mluis 8:26002607de9c 942 * \brief Sets the duty cycle for the join procedure.
mluis 7:c16969e0f70f 943 *
mluis 7:c16969e0f70f 944 * \retval Duty cycle
mluis 7:c16969e0f70f 945 */
mluis 8:26002607de9c 946 static uint16_t JoinDutyCycle( void );
mluis 7:c16969e0f70f 947
mluis 7:c16969e0f70f 948 /*
mluis 4:37c12dbc8dc7 949 * \brief Calculates the back-off time for the band of a channel.
mluis 4:37c12dbc8dc7 950 *
mluis 4:37c12dbc8dc7 951 * \param [IN] channel The last Tx channel index
mluis 4:37c12dbc8dc7 952 */
mluis 4:37c12dbc8dc7 953 static void CalculateBackOff( uint8_t channel );
mluis 4:37c12dbc8dc7 954
mluis 7:c16969e0f70f 955 /*
mluis 7:c16969e0f70f 956 * \brief Alternates the datarate of the channel for the join request.
mluis 7:c16969e0f70f 957 *
mluis 7:c16969e0f70f 958 * \param [IN] nbTrials Number of performed join requests.
mluis 7:c16969e0f70f 959 * \retval Datarate to apply
mluis 7:c16969e0f70f 960 */
mluis 7:c16969e0f70f 961 static int8_t AlternateDatarate( uint16_t nbTrials );
mluis 7:c16969e0f70f 962
mluis 2:14a5d6ad92d5 963 /*!
mluis 2:14a5d6ad92d5 964 * \brief LoRaMAC layer prepared frame buffer transmission with channel specification
mluis 2:14a5d6ad92d5 965 *
mluis 2:14a5d6ad92d5 966 * \remark PrepareFrame must be called at least once before calling this
mluis 2:14a5d6ad92d5 967 * function.
mluis 2:14a5d6ad92d5 968 *
mluis 2:14a5d6ad92d5 969 * \param [IN] channel Channel parameters
mluis 2:14a5d6ad92d5 970 * \retval status Status of the operation.
mluis 0:91d1a7783bb9 971 */
mluis 2:14a5d6ad92d5 972 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel );
mluis 2:14a5d6ad92d5 973
mluis 7:c16969e0f70f 974 /*!
mluis 8:26002607de9c 975 * \brief Sets the radio in continuous transmission mode
mluis 8:26002607de9c 976 *
mluis 8:26002607de9c 977 * \remark Uses the radio parameters set on the previous transmission.
mluis 8:26002607de9c 978 *
mluis 8:26002607de9c 979 * \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
mluis 8:26002607de9c 980 * \retval status Status of the operation.
mluis 8:26002607de9c 981 */
mluis 8:26002607de9c 982 LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout );
mluis 8:26002607de9c 983
mluis 8:26002607de9c 984 /*!
mluis 8:26002607de9c 985 * \brief Sets the radio in continuous transmission mode
mluis 8:26002607de9c 986 *
mluis 8:26002607de9c 987 * \remark Uses the radio parameters set on the previous transmission.
mluis 8:26002607de9c 988 *
mluis 8:26002607de9c 989 * \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
mluis 8:26002607de9c 990 * \param [IN] frequency RF frequency to be set.
mluis 8:26002607de9c 991 * \param [IN] power RF ouptput power to be set.
mluis 8:26002607de9c 992 * \retval status Status of the operation.
mluis 8:26002607de9c 993 */
mluis 8:26002607de9c 994 LoRaMacStatus_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power );
mluis 8:26002607de9c 995
mluis 8:26002607de9c 996 /*!
mluis 7:c16969e0f70f 997 * \brief Resets MAC specific parameters to default
mluis 7:c16969e0f70f 998 */
mluis 7:c16969e0f70f 999 static void ResetMacParameters( void );
mluis 2:14a5d6ad92d5 1000
mluis 8:26002607de9c 1001 /*
mluis 8:26002607de9c 1002 * Rx window precise timing
mluis 8:26002607de9c 1003 *
mluis 8:26002607de9c 1004 * For more details please consult the following document, chapter 3.1.2.
mluis 8:26002607de9c 1005 * http://www.semtech.com/images/datasheet/SX1272_settings_for_LoRaWAN_v2.0.pdf
mluis 8:26002607de9c 1006 * or
mluis 8:26002607de9c 1007 * http://www.semtech.com/images/datasheet/SX1276_settings_for_LoRaWAN_v2.0.pdf
mluis 8:26002607de9c 1008 *
mluis 8:26002607de9c 1009 * Downlink start: T = Tx + 1s (+/- 20 us)
mluis 8:26002607de9c 1010 * |
mluis 8:26002607de9c 1011 * TRxEarly | TRxLate
mluis 8:26002607de9c 1012 * | | |
mluis 8:26002607de9c 1013 * | | +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1014 * | | | Latest Rx window |
mluis 8:26002607de9c 1015 * | | +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1016 * | | |
mluis 8:26002607de9c 1017 * +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1018 * | Earliest Rx window |
mluis 8:26002607de9c 1019 * +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1020 * |
mluis 8:26002607de9c 1021 * +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1022 *Downlink preamble 8 symbols | | | | | | | | |
mluis 8:26002607de9c 1023 * +---+---+---+---+---+---+---+---+
mluis 8:26002607de9c 1024 *
mluis 8:26002607de9c 1025 * Worst case Rx window timings
mluis 8:26002607de9c 1026 *
mluis 8:26002607de9c 1027 * TRxLate = DEFAULT_MIN_RX_SYMBOLS * tSymbol - RADIO_WAKEUP_TIME
mluis 8:26002607de9c 1028 * TRxEarly = 8 - DEFAULT_MIN_RX_SYMBOLS * tSymbol - RxWindowTimeout - RADIO_WAKEUP_TIME
mluis 8:26002607de9c 1029 *
mluis 8:26002607de9c 1030 * TRxLate - TRxEarly = 2 * DEFAULT_SYSTEM_MAX_RX_ERROR
mluis 8:26002607de9c 1031 *
mluis 8:26002607de9c 1032 * RxOffset = ( TRxLate + TRxEarly ) / 2
mluis 8:26002607de9c 1033 *
mluis 8:26002607de9c 1034 * RxWindowTimeout = ( 2 * DEFAULT_MIN_RX_SYMBOLS - 8 ) * tSymbol + 2 * DEFAULT_SYSTEM_MAX_RX_ERROR
mluis 8:26002607de9c 1035 * RxOffset = 4 * tSymbol - RxWindowTimeout / 2 - RADIO_WAKE_UP_TIME
mluis 8:26002607de9c 1036 *
mluis 8:26002607de9c 1037 * Minimal value of RxWindowTimeout must be 5 symbols which implies that the system always tolerates at least an error of 1.5 * tSymbol
mluis 8:26002607de9c 1038 */
mluis 8:26002607de9c 1039 /*!
mluis 8:26002607de9c 1040 * Computes the Rx window parameters.
mluis 8:26002607de9c 1041 *
mluis 8:26002607de9c 1042 * \param [IN] datarate Rx window datarate to be used
mluis 8:26002607de9c 1043 * \param [IN] rxError Maximum timing error of the receiver. in milliseconds
mluis 8:26002607de9c 1044 * The receiver will turn on in a [-rxError : +rxError] ms
mluis 8:26002607de9c 1045 * interval around RxOffset
mluis 8:26002607de9c 1046 *
mluis 8:26002607de9c 1047 * \retval rxConfigParams Returns a RxConfigParams_t structure.
mluis 8:26002607de9c 1048 */
mluis 8:26002607de9c 1049 static RxConfigParams_t ComputeRxWindowParameters( int8_t datarate, uint32_t rxError );
mluis 8:26002607de9c 1050
mluis 2:14a5d6ad92d5 1051 static void OnRadioTxDone( void )
mluis 2:14a5d6ad92d5 1052 {
mluis 2:14a5d6ad92d5 1053 TimerTime_t curTime = TimerGetCurrentTime( );
mluis 8:26002607de9c 1054
mluis 2:14a5d6ad92d5 1055 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1056 {
mluis 2:14a5d6ad92d5 1057 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1058 }
mluis 2:14a5d6ad92d5 1059 else
mluis 2:14a5d6ad92d5 1060 {
mluis 2:14a5d6ad92d5 1061 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1062 }
mluis 2:14a5d6ad92d5 1063
mluis 8:26002607de9c 1064 // Setup timers
mluis 2:14a5d6ad92d5 1065 if( IsRxWindowsEnabled == true )
mluis 2:14a5d6ad92d5 1066 {
mluis 2:14a5d6ad92d5 1067 TimerSetValue( &RxWindowTimer1, RxWindow1Delay );
mluis 2:14a5d6ad92d5 1068 TimerStart( &RxWindowTimer1 );
mluis 2:14a5d6ad92d5 1069 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1070 {
mluis 2:14a5d6ad92d5 1071 TimerSetValue( &RxWindowTimer2, RxWindow2Delay );
mluis 2:14a5d6ad92d5 1072 TimerStart( &RxWindowTimer2 );
mluis 2:14a5d6ad92d5 1073 }
mluis 2:14a5d6ad92d5 1074 if( ( LoRaMacDeviceClass == CLASS_C ) || ( NodeAckRequested == true ) )
mluis 2:14a5d6ad92d5 1075 {
mluis 2:14a5d6ad92d5 1076 TimerSetValue( &AckTimeoutTimer, RxWindow2Delay + ACK_TIMEOUT +
mluis 2:14a5d6ad92d5 1077 randr( -ACK_TIMEOUT_RND, ACK_TIMEOUT_RND ) );
mluis 2:14a5d6ad92d5 1078 TimerStart( &AckTimeoutTimer );
mluis 2:14a5d6ad92d5 1079 }
mluis 2:14a5d6ad92d5 1080 }
mluis 2:14a5d6ad92d5 1081 else
mluis 2:14a5d6ad92d5 1082 {
mluis 2:14a5d6ad92d5 1083 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1084 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 1085
mluis 2:14a5d6ad92d5 1086 if( LoRaMacFlags.Value == 0 )
mluis 2:14a5d6ad92d5 1087 {
mluis 2:14a5d6ad92d5 1088 LoRaMacFlags.Bits.McpsReq = 1;
mluis 2:14a5d6ad92d5 1089 }
mluis 2:14a5d6ad92d5 1090 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1091 }
mluis 2:14a5d6ad92d5 1092
mluis 8:26002607de9c 1093 // Update last tx done time for the current channel
mluis 8:26002607de9c 1094 Bands[Channels[Channel].Band].LastTxDoneTime = curTime;
mluis 8:26002607de9c 1095 // Update Aggregated last tx done time
mluis 8:26002607de9c 1096 AggregatedLastTxDoneTime = curTime;
mluis 8:26002607de9c 1097 // Update Backoff
mluis 8:26002607de9c 1098 CalculateBackOff( Channel );
mluis 8:26002607de9c 1099
mluis 2:14a5d6ad92d5 1100 if( NodeAckRequested == false )
mluis 2:14a5d6ad92d5 1101 {
mluis 2:14a5d6ad92d5 1102 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1103 ChannelsNbRepCounter++;
mluis 2:14a5d6ad92d5 1104 }
mluis 2:14a5d6ad92d5 1105 }
mluis 2:14a5d6ad92d5 1106
mluis 3:b9d87593a8ae 1107 static void PrepareRxDoneAbort( void )
mluis 3:b9d87593a8ae 1108 {
mluis 8:26002607de9c 1109 LoRaMacState |= LORAMAC_RX_ABORT;
mluis 3:b9d87593a8ae 1110
mluis 3:b9d87593a8ae 1111 if( NodeAckRequested )
mluis 3:b9d87593a8ae 1112 {
mluis 3:b9d87593a8ae 1113 OnAckTimeoutTimerEvent( );
mluis 3:b9d87593a8ae 1114 }
mluis 3:b9d87593a8ae 1115
mluis 3:b9d87593a8ae 1116 LoRaMacFlags.Bits.McpsInd = 1;
mluis 3:b9d87593a8ae 1117 LoRaMacFlags.Bits.MacDone = 1;
mluis 3:b9d87593a8ae 1118
mluis 3:b9d87593a8ae 1119 // Trig OnMacCheckTimerEvent call as soon as possible
mluis 8:26002607de9c 1120 TimerSetValue( &MacStateCheckTimer, 1 );
mluis 3:b9d87593a8ae 1121 TimerStart( &MacStateCheckTimer );
mluis 3:b9d87593a8ae 1122 }
mluis 3:b9d87593a8ae 1123
mluis 2:14a5d6ad92d5 1124 static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
mluis 0:91d1a7783bb9 1125 {
mluis 2:14a5d6ad92d5 1126 LoRaMacHeader_t macHdr;
mluis 2:14a5d6ad92d5 1127 LoRaMacFrameCtrl_t fCtrl;
mluis 4:37c12dbc8dc7 1128 bool skipIndication = false;
mluis 2:14a5d6ad92d5 1129
mluis 2:14a5d6ad92d5 1130 uint8_t pktHeaderLen = 0;
mluis 2:14a5d6ad92d5 1131 uint32_t address = 0;
mluis 2:14a5d6ad92d5 1132 uint8_t appPayloadStartIndex = 0;
mluis 2:14a5d6ad92d5 1133 uint8_t port = 0xFF;
mluis 2:14a5d6ad92d5 1134 uint8_t frameLen = 0;
mluis 2:14a5d6ad92d5 1135 uint32_t mic = 0;
mluis 2:14a5d6ad92d5 1136 uint32_t micRx = 0;
mluis 2:14a5d6ad92d5 1137
mluis 2:14a5d6ad92d5 1138 uint16_t sequenceCounter = 0;
mluis 2:14a5d6ad92d5 1139 uint16_t sequenceCounterPrev = 0;
mluis 2:14a5d6ad92d5 1140 uint16_t sequenceCounterDiff = 0;
mluis 2:14a5d6ad92d5 1141 uint32_t downLinkCounter = 0;
mluis 2:14a5d6ad92d5 1142
mluis 2:14a5d6ad92d5 1143 MulticastParams_t *curMulticastParams = NULL;
mluis 2:14a5d6ad92d5 1144 uint8_t *nwkSKey = LoRaMacNwkSKey;
mluis 2:14a5d6ad92d5 1145 uint8_t *appSKey = LoRaMacAppSKey;
mluis 2:14a5d6ad92d5 1146
mluis 2:14a5d6ad92d5 1147 uint8_t multicast = 0;
mluis 2:14a5d6ad92d5 1148
mluis 2:14a5d6ad92d5 1149 bool isMicOk = false;
mluis 2:14a5d6ad92d5 1150
mluis 2:14a5d6ad92d5 1151 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1152 McpsIndication.Rssi = rssi;
mluis 2:14a5d6ad92d5 1153 McpsIndication.Snr = snr;
mluis 2:14a5d6ad92d5 1154 McpsIndication.RxSlot = RxSlot;
mluis 2:14a5d6ad92d5 1155 McpsIndication.Port = 0;
mluis 2:14a5d6ad92d5 1156 McpsIndication.Multicast = 0;
mluis 2:14a5d6ad92d5 1157 McpsIndication.FramePending = 0;
mluis 2:14a5d6ad92d5 1158 McpsIndication.Buffer = NULL;
mluis 2:14a5d6ad92d5 1159 McpsIndication.BufferSize = 0;
mluis 2:14a5d6ad92d5 1160 McpsIndication.RxData = false;
mluis 2:14a5d6ad92d5 1161 McpsIndication.AckReceived = false;
mluis 2:14a5d6ad92d5 1162 McpsIndication.DownLinkCounter = 0;
mluis 2:14a5d6ad92d5 1163 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
mluis 2:14a5d6ad92d5 1164
mluis 8:26002607de9c 1165 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1166 TimerStop( &RxWindowTimer2 );
mluis 2:14a5d6ad92d5 1167
mluis 2:14a5d6ad92d5 1168 macHdr.Value = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1169
mluis 2:14a5d6ad92d5 1170 switch( macHdr.Bits.MType )
mluis 2:14a5d6ad92d5 1171 {
mluis 2:14a5d6ad92d5 1172 case FRAME_TYPE_JOIN_ACCEPT:
mluis 2:14a5d6ad92d5 1173 if( IsLoRaMacNetworkJoined == true )
mluis 2:14a5d6ad92d5 1174 {
mluis 8:26002607de9c 1175 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 8:26002607de9c 1176 PrepareRxDoneAbort( );
mluis 8:26002607de9c 1177 return;
mluis 2:14a5d6ad92d5 1178 }
mluis 2:14a5d6ad92d5 1179 LoRaMacJoinDecrypt( payload + 1, size - 1, LoRaMacAppKey, LoRaMacRxPayload + 1 );
mluis 2:14a5d6ad92d5 1180
mluis 2:14a5d6ad92d5 1181 LoRaMacRxPayload[0] = macHdr.Value;
mluis 2:14a5d6ad92d5 1182
mluis 2:14a5d6ad92d5 1183 LoRaMacJoinComputeMic( LoRaMacRxPayload, size - LORAMAC_MFR_LEN, LoRaMacAppKey, &mic );
mluis 2:14a5d6ad92d5 1184
mluis 2:14a5d6ad92d5 1185 micRx |= ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN];
mluis 2:14a5d6ad92d5 1186 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 1] << 8 );
mluis 2:14a5d6ad92d5 1187 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 2] << 16 );
mluis 2:14a5d6ad92d5 1188 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 3] << 24 );
mluis 2:14a5d6ad92d5 1189
mluis 2:14a5d6ad92d5 1190 if( micRx == mic )
mluis 2:14a5d6ad92d5 1191 {
mluis 2:14a5d6ad92d5 1192 LoRaMacJoinComputeSKeys( LoRaMacAppKey, LoRaMacRxPayload + 1, LoRaMacDevNonce, LoRaMacNwkSKey, LoRaMacAppSKey );
mluis 2:14a5d6ad92d5 1193
mluis 2:14a5d6ad92d5 1194 LoRaMacNetID = ( uint32_t )LoRaMacRxPayload[4];
mluis 2:14a5d6ad92d5 1195 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[5] << 8 );
mluis 2:14a5d6ad92d5 1196 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[6] << 16 );
mluis 2:14a5d6ad92d5 1197
mluis 2:14a5d6ad92d5 1198 LoRaMacDevAddr = ( uint32_t )LoRaMacRxPayload[7];
mluis 2:14a5d6ad92d5 1199 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[8] << 8 );
mluis 2:14a5d6ad92d5 1200 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[9] << 16 );
mluis 2:14a5d6ad92d5 1201 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[10] << 24 );
mluis 2:14a5d6ad92d5 1202
mluis 2:14a5d6ad92d5 1203 // DLSettings
mluis 7:c16969e0f70f 1204 LoRaMacParams.Rx1DrOffset = ( LoRaMacRxPayload[11] >> 4 ) & 0x07;
mluis 7:c16969e0f70f 1205 LoRaMacParams.Rx2Channel.Datarate = LoRaMacRxPayload[11] & 0x0F;
mluis 8:26002607de9c 1206
mluis 2:14a5d6ad92d5 1207 // RxDelay
mluis 7:c16969e0f70f 1208 LoRaMacParams.ReceiveDelay1 = ( LoRaMacRxPayload[12] & 0x0F );
mluis 7:c16969e0f70f 1209 if( LoRaMacParams.ReceiveDelay1 == 0 )
mluis 2:14a5d6ad92d5 1210 {
mluis 7:c16969e0f70f 1211 LoRaMacParams.ReceiveDelay1 = 1;
mluis 2:14a5d6ad92d5 1212 }
mluis 8:26002607de9c 1213 LoRaMacParams.ReceiveDelay1 *= 1e3;
mluis 8:26002607de9c 1214 LoRaMacParams.ReceiveDelay2 = LoRaMacParams.ReceiveDelay1 + 1e3;
mluis 2:14a5d6ad92d5 1215
mluis 2:14a5d6ad92d5 1216 #if !( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 2:14a5d6ad92d5 1217 //CFList
mluis 2:14a5d6ad92d5 1218 if( ( size - 1 ) > 16 )
mluis 2:14a5d6ad92d5 1219 {
mluis 2:14a5d6ad92d5 1220 ChannelParams_t param;
mluis 2:14a5d6ad92d5 1221 param.DrRange.Value = ( DR_5 << 4 ) | DR_0;
mluis 2:14a5d6ad92d5 1222
mluis 8:26002607de9c 1223 LoRaMacState |= LORAMAC_TX_CONFIG;
mluis 2:14a5d6ad92d5 1224 for( uint8_t i = 3, j = 0; i < ( 5 + 3 ); i++, j += 3 )
mluis 2:14a5d6ad92d5 1225 {
mluis 2:14a5d6ad92d5 1226 param.Frequency = ( ( uint32_t )LoRaMacRxPayload[13 + j] | ( ( uint32_t )LoRaMacRxPayload[14 + j] << 8 ) | ( ( uint32_t )LoRaMacRxPayload[15 + j] << 16 ) ) * 100;
mluis 8:26002607de9c 1227 if( param.Frequency != 0 )
mluis 8:26002607de9c 1228 {
mluis 8:26002607de9c 1229 LoRaMacChannelAdd( i, param );
mluis 8:26002607de9c 1230 }
mluis 8:26002607de9c 1231 else
mluis 8:26002607de9c 1232 {
mluis 8:26002607de9c 1233 LoRaMacChannelRemove( i );
mluis 8:26002607de9c 1234 }
mluis 2:14a5d6ad92d5 1235 }
mluis 8:26002607de9c 1236 LoRaMacState &= ~LORAMAC_TX_CONFIG;
mluis 2:14a5d6ad92d5 1237 }
mluis 2:14a5d6ad92d5 1238 #endif
mluis 2:14a5d6ad92d5 1239 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1240 IsLoRaMacNetworkJoined = true;
mluis 7:c16969e0f70f 1241 LoRaMacParams.ChannelsDatarate = LoRaMacParamsDefaults.ChannelsDatarate;
mluis 2:14a5d6ad92d5 1242 }
mluis 2:14a5d6ad92d5 1243 else
mluis 2:14a5d6ad92d5 1244 {
mluis 2:14a5d6ad92d5 1245 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
mluis 2:14a5d6ad92d5 1246 }
mluis 2:14a5d6ad92d5 1247 break;
mluis 2:14a5d6ad92d5 1248 case FRAME_TYPE_DATA_CONFIRMED_DOWN:
mluis 2:14a5d6ad92d5 1249 case FRAME_TYPE_DATA_UNCONFIRMED_DOWN:
mluis 2:14a5d6ad92d5 1250 {
mluis 2:14a5d6ad92d5 1251 address = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1252 address |= ( (uint32_t)payload[pktHeaderLen++] << 8 );
mluis 2:14a5d6ad92d5 1253 address |= ( (uint32_t)payload[pktHeaderLen++] << 16 );
mluis 2:14a5d6ad92d5 1254 address |= ( (uint32_t)payload[pktHeaderLen++] << 24 );
mluis 2:14a5d6ad92d5 1255
mluis 2:14a5d6ad92d5 1256 if( address != LoRaMacDevAddr )
mluis 2:14a5d6ad92d5 1257 {
mluis 2:14a5d6ad92d5 1258 curMulticastParams = MulticastChannels;
mluis 2:14a5d6ad92d5 1259 while( curMulticastParams != NULL )
mluis 2:14a5d6ad92d5 1260 {
mluis 2:14a5d6ad92d5 1261 if( address == curMulticastParams->Address )
mluis 2:14a5d6ad92d5 1262 {
mluis 2:14a5d6ad92d5 1263 multicast = 1;
mluis 2:14a5d6ad92d5 1264 nwkSKey = curMulticastParams->NwkSKey;
mluis 2:14a5d6ad92d5 1265 appSKey = curMulticastParams->AppSKey;
mluis 2:14a5d6ad92d5 1266 downLinkCounter = curMulticastParams->DownLinkCounter;
mluis 2:14a5d6ad92d5 1267 break;
mluis 2:14a5d6ad92d5 1268 }
mluis 2:14a5d6ad92d5 1269 curMulticastParams = curMulticastParams->Next;
mluis 2:14a5d6ad92d5 1270 }
mluis 2:14a5d6ad92d5 1271 if( multicast == 0 )
mluis 2:14a5d6ad92d5 1272 {
mluis 2:14a5d6ad92d5 1273 // We are not the destination of this frame.
mluis 2:14a5d6ad92d5 1274 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL;
mluis 3:b9d87593a8ae 1275 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1276 return;
mluis 2:14a5d6ad92d5 1277 }
mluis 2:14a5d6ad92d5 1278 }
mluis 2:14a5d6ad92d5 1279 else
mluis 2:14a5d6ad92d5 1280 {
mluis 2:14a5d6ad92d5 1281 multicast = 0;
mluis 2:14a5d6ad92d5 1282 nwkSKey = LoRaMacNwkSKey;
mluis 2:14a5d6ad92d5 1283 appSKey = LoRaMacAppSKey;
mluis 2:14a5d6ad92d5 1284 downLinkCounter = DownLinkCounter;
mluis 2:14a5d6ad92d5 1285 }
mluis 2:14a5d6ad92d5 1286
mluis 2:14a5d6ad92d5 1287 fCtrl.Value = payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1288
mluis 2:14a5d6ad92d5 1289 sequenceCounter = ( uint16_t )payload[pktHeaderLen++];
mluis 2:14a5d6ad92d5 1290 sequenceCounter |= ( uint16_t )payload[pktHeaderLen++] << 8;
mluis 2:14a5d6ad92d5 1291
mluis 2:14a5d6ad92d5 1292 appPayloadStartIndex = 8 + fCtrl.Bits.FOptsLen;
mluis 2:14a5d6ad92d5 1293
mluis 2:14a5d6ad92d5 1294 micRx |= ( uint32_t )payload[size - LORAMAC_MFR_LEN];
mluis 2:14a5d6ad92d5 1295 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 1] << 8 );
mluis 2:14a5d6ad92d5 1296 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 2] << 16 );
mluis 2:14a5d6ad92d5 1297 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 3] << 24 );
mluis 2:14a5d6ad92d5 1298
mluis 2:14a5d6ad92d5 1299 sequenceCounterPrev = ( uint16_t )downLinkCounter;
mluis 2:14a5d6ad92d5 1300 sequenceCounterDiff = ( sequenceCounter - sequenceCounterPrev );
mluis 2:14a5d6ad92d5 1301
mluis 2:14a5d6ad92d5 1302 if( sequenceCounterDiff < ( 1 << 15 ) )
mluis 2:14a5d6ad92d5 1303 {
mluis 2:14a5d6ad92d5 1304 downLinkCounter += sequenceCounterDiff;
mluis 2:14a5d6ad92d5 1305 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounter, &mic );
mluis 2:14a5d6ad92d5 1306 if( micRx == mic )
mluis 2:14a5d6ad92d5 1307 {
mluis 2:14a5d6ad92d5 1308 isMicOk = true;
mluis 2:14a5d6ad92d5 1309 }
mluis 2:14a5d6ad92d5 1310 }
mluis 2:14a5d6ad92d5 1311 else
mluis 2:14a5d6ad92d5 1312 {
mluis 2:14a5d6ad92d5 1313 // check for sequence roll-over
mluis 2:14a5d6ad92d5 1314 uint32_t downLinkCounterTmp = downLinkCounter + 0x10000 + ( int16_t )sequenceCounterDiff;
mluis 2:14a5d6ad92d5 1315 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounterTmp, &mic );
mluis 2:14a5d6ad92d5 1316 if( micRx == mic )
mluis 2:14a5d6ad92d5 1317 {
mluis 2:14a5d6ad92d5 1318 isMicOk = true;
mluis 2:14a5d6ad92d5 1319 downLinkCounter = downLinkCounterTmp;
mluis 2:14a5d6ad92d5 1320 }
mluis 2:14a5d6ad92d5 1321 }
mluis 2:14a5d6ad92d5 1322
mluis 4:37c12dbc8dc7 1323 // Check for a the maximum allowed counter difference
mluis 4:37c12dbc8dc7 1324 if( sequenceCounterDiff >= MAX_FCNT_GAP )
mluis 4:37c12dbc8dc7 1325 {
mluis 4:37c12dbc8dc7 1326 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS;
mluis 4:37c12dbc8dc7 1327 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 4:37c12dbc8dc7 1328 PrepareRxDoneAbort( );
mluis 4:37c12dbc8dc7 1329 return;
mluis 4:37c12dbc8dc7 1330 }
mluis 4:37c12dbc8dc7 1331
mluis 2:14a5d6ad92d5 1332 if( isMicOk == true )
mluis 2:14a5d6ad92d5 1333 {
mluis 2:14a5d6ad92d5 1334 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1335 McpsIndication.Multicast = multicast;
mluis 2:14a5d6ad92d5 1336 McpsIndication.FramePending = fCtrl.Bits.FPending;
mluis 2:14a5d6ad92d5 1337 McpsIndication.Buffer = NULL;
mluis 2:14a5d6ad92d5 1338 McpsIndication.BufferSize = 0;
mluis 2:14a5d6ad92d5 1339 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1340
mluis 2:14a5d6ad92d5 1341 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1342
mluis 2:14a5d6ad92d5 1343 AdrAckCounter = 0;
mluis 7:c16969e0f70f 1344 MacCommandsBufferToRepeatIndex = 0;
mluis 2:14a5d6ad92d5 1345
mluis 2:14a5d6ad92d5 1346 // Update 32 bits downlink counter
mluis 2:14a5d6ad92d5 1347 if( multicast == 1 )
mluis 2:14a5d6ad92d5 1348 {
mluis 2:14a5d6ad92d5 1349 McpsIndication.McpsIndication = MCPS_MULTICAST;
mluis 2:14a5d6ad92d5 1350
mluis 2:14a5d6ad92d5 1351 if( ( curMulticastParams->DownLinkCounter == downLinkCounter ) &&
mluis 2:14a5d6ad92d5 1352 ( curMulticastParams->DownLinkCounter != 0 ) )
mluis 2:14a5d6ad92d5 1353 {
mluis 2:14a5d6ad92d5 1354 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
mluis 2:14a5d6ad92d5 1355 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 3:b9d87593a8ae 1356 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1357 return;
mluis 2:14a5d6ad92d5 1358 }
mluis 2:14a5d6ad92d5 1359 curMulticastParams->DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1360 }
mluis 2:14a5d6ad92d5 1361 else
mluis 2:14a5d6ad92d5 1362 {
mluis 2:14a5d6ad92d5 1363 if( macHdr.Bits.MType == FRAME_TYPE_DATA_CONFIRMED_DOWN )
mluis 2:14a5d6ad92d5 1364 {
mluis 3:b9d87593a8ae 1365 SrvAckRequested = true;
mluis 2:14a5d6ad92d5 1366 McpsIndication.McpsIndication = MCPS_CONFIRMED;
mluis 4:37c12dbc8dc7 1367
mluis 4:37c12dbc8dc7 1368 if( ( DownLinkCounter == downLinkCounter ) &&
mluis 4:37c12dbc8dc7 1369 ( DownLinkCounter != 0 ) )
mluis 4:37c12dbc8dc7 1370 {
mluis 4:37c12dbc8dc7 1371 // Duplicated confirmed downlink. Skip indication.
mluis 8:26002607de9c 1372 // In this case, the MAC layer shall accept the MAC commands
mluis 8:26002607de9c 1373 // which are included in the downlink retransmission.
mluis 8:26002607de9c 1374 // It should not provide the same frame to the application
mluis 8:26002607de9c 1375 // layer again.
mluis 4:37c12dbc8dc7 1376 skipIndication = true;
mluis 4:37c12dbc8dc7 1377 }
mluis 2:14a5d6ad92d5 1378 }
mluis 2:14a5d6ad92d5 1379 else
mluis 2:14a5d6ad92d5 1380 {
mluis 3:b9d87593a8ae 1381 SrvAckRequested = false;
mluis 2:14a5d6ad92d5 1382 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
mluis 4:37c12dbc8dc7 1383
mluis 4:37c12dbc8dc7 1384 if( ( DownLinkCounter == downLinkCounter ) &&
mluis 4:37c12dbc8dc7 1385 ( DownLinkCounter != 0 ) )
mluis 4:37c12dbc8dc7 1386 {
mluis 4:37c12dbc8dc7 1387 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
mluis 4:37c12dbc8dc7 1388 McpsIndication.DownLinkCounter = downLinkCounter;
mluis 4:37c12dbc8dc7 1389 PrepareRxDoneAbort( );
mluis 4:37c12dbc8dc7 1390 return;
mluis 4:37c12dbc8dc7 1391 }
mluis 2:14a5d6ad92d5 1392 }
mluis 2:14a5d6ad92d5 1393 DownLinkCounter = downLinkCounter;
mluis 2:14a5d6ad92d5 1394 }
mluis 2:14a5d6ad92d5 1395
mluis 8:26002607de9c 1396 // This must be done before parsing the payload and the MAC commands.
mluis 8:26002607de9c 1397 // We need to reset the MacCommandsBufferIndex here, since we need
mluis 8:26002607de9c 1398 // to take retransmissions and repititions into account. Error cases
mluis 8:26002607de9c 1399 // will be handled in function OnMacStateCheckTimerEvent.
mluis 8:26002607de9c 1400 if( McpsConfirm.McpsRequest == MCPS_CONFIRMED )
mluis 2:14a5d6ad92d5 1401 {
mluis 8:26002607de9c 1402 if( fCtrl.Bits.Ack == 1 )
mluis 8:26002607de9c 1403 {// Reset MacCommandsBufferIndex when we have received an ACK.
mluis 8:26002607de9c 1404 MacCommandsBufferIndex = 0;
mluis 8:26002607de9c 1405 }
mluis 2:14a5d6ad92d5 1406 }
mluis 2:14a5d6ad92d5 1407 else
mluis 8:26002607de9c 1408 {// Reset the variable if we have received any valid frame.
mluis 8:26002607de9c 1409 MacCommandsBufferIndex = 0;
mluis 2:14a5d6ad92d5 1410 }
mluis 2:14a5d6ad92d5 1411
mluis 8:26002607de9c 1412 // Process payload and MAC commands
mluis 2:14a5d6ad92d5 1413 if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 )
mluis 2:14a5d6ad92d5 1414 {
mluis 2:14a5d6ad92d5 1415 port = payload[appPayloadStartIndex++];
mluis 2:14a5d6ad92d5 1416 frameLen = ( size - 4 ) - appPayloadStartIndex;
mluis 2:14a5d6ad92d5 1417
mluis 2:14a5d6ad92d5 1418 McpsIndication.Port = port;
mluis 2:14a5d6ad92d5 1419
mluis 2:14a5d6ad92d5 1420 if( port == 0 )
mluis 2:14a5d6ad92d5 1421 {
mluis 8:26002607de9c 1422 // Only allow frames which do not have fOpts
mluis 7:c16969e0f70f 1423 if( fCtrl.Bits.FOptsLen == 0 )
mluis 7:c16969e0f70f 1424 {
mluis 7:c16969e0f70f 1425 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
mluis 7:c16969e0f70f 1426 frameLen,
mluis 7:c16969e0f70f 1427 nwkSKey,
mluis 7:c16969e0f70f 1428 address,
mluis 7:c16969e0f70f 1429 DOWN_LINK,
mluis 7:c16969e0f70f 1430 downLinkCounter,
mluis 7:c16969e0f70f 1431 LoRaMacRxPayload );
mluis 7:c16969e0f70f 1432
mluis 7:c16969e0f70f 1433 // Decode frame payload MAC commands
mluis 7:c16969e0f70f 1434 ProcessMacCommands( LoRaMacRxPayload, 0, frameLen, snr );
mluis 7:c16969e0f70f 1435 }
mluis 7:c16969e0f70f 1436 else
mluis 7:c16969e0f70f 1437 {
mluis 7:c16969e0f70f 1438 skipIndication = true;
mluis 7:c16969e0f70f 1439 }
mluis 2:14a5d6ad92d5 1440 }
mluis 2:14a5d6ad92d5 1441 else
mluis 2:14a5d6ad92d5 1442 {
mluis 7:c16969e0f70f 1443 if( fCtrl.Bits.FOptsLen > 0 )
mluis 7:c16969e0f70f 1444 {
mluis 7:c16969e0f70f 1445 // Decode Options field MAC commands. Omit the fPort.
mluis 7:c16969e0f70f 1446 ProcessMacCommands( payload, 8, appPayloadStartIndex - 1, snr );
mluis 7:c16969e0f70f 1447 }
mluis 7:c16969e0f70f 1448
mluis 2:14a5d6ad92d5 1449 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
mluis 2:14a5d6ad92d5 1450 frameLen,
mluis 2:14a5d6ad92d5 1451 appSKey,
mluis 2:14a5d6ad92d5 1452 address,
mluis 2:14a5d6ad92d5 1453 DOWN_LINK,
mluis 2:14a5d6ad92d5 1454 downLinkCounter,
mluis 2:14a5d6ad92d5 1455 LoRaMacRxPayload );
mluis 2:14a5d6ad92d5 1456
mluis 4:37c12dbc8dc7 1457 if( skipIndication == false )
mluis 4:37c12dbc8dc7 1458 {
mluis 4:37c12dbc8dc7 1459 McpsIndication.Buffer = LoRaMacRxPayload;
mluis 4:37c12dbc8dc7 1460 McpsIndication.BufferSize = frameLen;
mluis 4:37c12dbc8dc7 1461 McpsIndication.RxData = true;
mluis 4:37c12dbc8dc7 1462 }
mluis 2:14a5d6ad92d5 1463 }
mluis 2:14a5d6ad92d5 1464 }
mluis 7:c16969e0f70f 1465 else
mluis 7:c16969e0f70f 1466 {
mluis 7:c16969e0f70f 1467 if( fCtrl.Bits.FOptsLen > 0 )
mluis 7:c16969e0f70f 1468 {
mluis 7:c16969e0f70f 1469 // Decode Options field MAC commands
mluis 7:c16969e0f70f 1470 ProcessMacCommands( payload, 8, appPayloadStartIndex, snr );
mluis 7:c16969e0f70f 1471 }
mluis 7:c16969e0f70f 1472 }
mluis 7:c16969e0f70f 1473
mluis 4:37c12dbc8dc7 1474 if( skipIndication == false )
mluis 4:37c12dbc8dc7 1475 {
mluis 8:26002607de9c 1476 // Check if the frame is an acknowledgement
mluis 8:26002607de9c 1477 if( fCtrl.Bits.Ack == 1 )
mluis 8:26002607de9c 1478 {
mluis 8:26002607de9c 1479 McpsConfirm.AckReceived = true;
mluis 8:26002607de9c 1480 McpsIndication.AckReceived = true;
mluis 8:26002607de9c 1481
mluis 8:26002607de9c 1482 // Stop the AckTimeout timer as no more retransmissions
mluis 8:26002607de9c 1483 // are needed.
mluis 8:26002607de9c 1484 TimerStop( &AckTimeoutTimer );
mluis 8:26002607de9c 1485 }
mluis 8:26002607de9c 1486 else
mluis 8:26002607de9c 1487 {
mluis 8:26002607de9c 1488 McpsConfirm.AckReceived = false;
mluis 8:26002607de9c 1489
mluis 8:26002607de9c 1490 if( AckTimeoutRetriesCounter > AckTimeoutRetries )
mluis 8:26002607de9c 1491 {
mluis 8:26002607de9c 1492 // Stop the AckTimeout timer as no more retransmissions
mluis 8:26002607de9c 1493 // are needed.
mluis 8:26002607de9c 1494 TimerStop( &AckTimeoutTimer );
mluis 8:26002607de9c 1495 }
mluis 8:26002607de9c 1496 }
mluis 4:37c12dbc8dc7 1497 }
mluis 8:26002607de9c 1498 // Provide always an indication, skip the callback to the user application,
mluis 8:26002607de9c 1499 // in case of a confirmed downlink retransmission.
mluis 8:26002607de9c 1500 LoRaMacFlags.Bits.McpsInd = 1;
mluis 8:26002607de9c 1501 LoRaMacFlags.Bits.McpsIndSkip = skipIndication;
mluis 2:14a5d6ad92d5 1502 }
mluis 2:14a5d6ad92d5 1503 else
mluis 2:14a5d6ad92d5 1504 {
mluis 2:14a5d6ad92d5 1505 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL;
mluis 3:b9d87593a8ae 1506
mluis 3:b9d87593a8ae 1507 PrepareRxDoneAbort( );
mluis 3:b9d87593a8ae 1508 return;
mluis 2:14a5d6ad92d5 1509 }
mluis 2:14a5d6ad92d5 1510 }
mluis 2:14a5d6ad92d5 1511 break;
mluis 2:14a5d6ad92d5 1512 case FRAME_TYPE_PROPRIETARY:
mluis 2:14a5d6ad92d5 1513 {
mluis 2:14a5d6ad92d5 1514 memcpy1( LoRaMacRxPayload, &payload[pktHeaderLen], size );
mluis 2:14a5d6ad92d5 1515
mluis 2:14a5d6ad92d5 1516 McpsIndication.McpsIndication = MCPS_PROPRIETARY;
mluis 2:14a5d6ad92d5 1517 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
mluis 2:14a5d6ad92d5 1518 McpsIndication.Buffer = LoRaMacRxPayload;
mluis 2:14a5d6ad92d5 1519 McpsIndication.BufferSize = size - pktHeaderLen;
mluis 3:b9d87593a8ae 1520
mluis 3:b9d87593a8ae 1521 LoRaMacFlags.Bits.McpsInd = 1;
mluis 2:14a5d6ad92d5 1522 break;
mluis 2:14a5d6ad92d5 1523 }
mluis 2:14a5d6ad92d5 1524 default:
mluis 2:14a5d6ad92d5 1525 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR;
mluis 3:b9d87593a8ae 1526 PrepareRxDoneAbort( );
mluis 2:14a5d6ad92d5 1527 break;
mluis 2:14a5d6ad92d5 1528 }
mluis 2:14a5d6ad92d5 1529 LoRaMacFlags.Bits.MacDone = 1;
mluis 3:b9d87593a8ae 1530
mluis 3:b9d87593a8ae 1531 // Trig OnMacCheckTimerEvent call as soon as possible
mluis 8:26002607de9c 1532 TimerSetValue( &MacStateCheckTimer, 1 );
mluis 3:b9d87593a8ae 1533 TimerStart( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1534 }
mluis 2:14a5d6ad92d5 1535
mluis 2:14a5d6ad92d5 1536 static void OnRadioTxTimeout( void )
mluis 2:14a5d6ad92d5 1537 {
mluis 2:14a5d6ad92d5 1538 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1539 {
mluis 2:14a5d6ad92d5 1540 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1541 }
mluis 2:14a5d6ad92d5 1542 else
mluis 2:14a5d6ad92d5 1543 {
mluis 2:14a5d6ad92d5 1544 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1545 }
mluis 2:14a5d6ad92d5 1546
mluis 2:14a5d6ad92d5 1547 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
mluis 2:14a5d6ad92d5 1548 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
mluis 2:14a5d6ad92d5 1549 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1550 }
mluis 2:14a5d6ad92d5 1551
mluis 2:14a5d6ad92d5 1552 static void OnRadioRxError( void )
mluis 2:14a5d6ad92d5 1553 {
mluis 2:14a5d6ad92d5 1554 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1555 {
mluis 2:14a5d6ad92d5 1556 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1557 }
mluis 2:14a5d6ad92d5 1558 else
mluis 2:14a5d6ad92d5 1559 {
mluis 2:14a5d6ad92d5 1560 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1561 }
mluis 2:14a5d6ad92d5 1562
mluis 8:26002607de9c 1563 if( RxSlot == 0 )
mluis 8:26002607de9c 1564 {
mluis 8:26002607de9c 1565 if( NodeAckRequested == true )
mluis 8:26002607de9c 1566 {
mluis 8:26002607de9c 1567 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX1_ERROR;
mluis 8:26002607de9c 1568 }
mluis 8:26002607de9c 1569 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX1_ERROR;
mluis 8:26002607de9c 1570
mluis 8:26002607de9c 1571 if( TimerGetElapsedTime( AggregatedLastTxDoneTime ) >= RxWindow2Delay )
mluis 8:26002607de9c 1572 {
mluis 8:26002607de9c 1573 LoRaMacFlags.Bits.MacDone = 1;
mluis 8:26002607de9c 1574 }
mluis 8:26002607de9c 1575 }
mluis 8:26002607de9c 1576 else
mluis 2:14a5d6ad92d5 1577 {
mluis 2:14a5d6ad92d5 1578 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1579 {
mluis 2:14a5d6ad92d5 1580 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_ERROR;
mluis 2:14a5d6ad92d5 1581 }
mluis 2:14a5d6ad92d5 1582 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_ERROR;
mluis 2:14a5d6ad92d5 1583 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1584 }
mluis 2:14a5d6ad92d5 1585 }
mluis 2:14a5d6ad92d5 1586
mluis 2:14a5d6ad92d5 1587 static void OnRadioRxTimeout( void )
mluis 2:14a5d6ad92d5 1588 {
mluis 2:14a5d6ad92d5 1589 if( LoRaMacDeviceClass != CLASS_C )
mluis 2:14a5d6ad92d5 1590 {
mluis 2:14a5d6ad92d5 1591 Radio.Sleep( );
mluis 2:14a5d6ad92d5 1592 }
mluis 2:14a5d6ad92d5 1593 else
mluis 2:14a5d6ad92d5 1594 {
mluis 2:14a5d6ad92d5 1595 OnRxWindow2TimerEvent( );
mluis 2:14a5d6ad92d5 1596 }
mluis 2:14a5d6ad92d5 1597
mluis 2:14a5d6ad92d5 1598 if( RxSlot == 1 )
mluis 2:14a5d6ad92d5 1599 {
mluis 2:14a5d6ad92d5 1600 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1601 {
mluis 2:14a5d6ad92d5 1602 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 1603 }
mluis 2:14a5d6ad92d5 1604 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX2_TIMEOUT;
mluis 2:14a5d6ad92d5 1605 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1606 }
mluis 2:14a5d6ad92d5 1607 }
mluis 2:14a5d6ad92d5 1608
mluis 2:14a5d6ad92d5 1609 static void OnMacStateCheckTimerEvent( void )
mluis 2:14a5d6ad92d5 1610 {
mluis 2:14a5d6ad92d5 1611 TimerStop( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1612 bool txTimeout = false;
mluis 2:14a5d6ad92d5 1613
mluis 2:14a5d6ad92d5 1614 if( LoRaMacFlags.Bits.MacDone == 1 )
mluis 2:14a5d6ad92d5 1615 {
mluis 8:26002607de9c 1616 if( ( LoRaMacState & LORAMAC_RX_ABORT ) == LORAMAC_RX_ABORT )
mluis 4:37c12dbc8dc7 1617 {
mluis 8:26002607de9c 1618 LoRaMacState &= ~LORAMAC_RX_ABORT;
mluis 8:26002607de9c 1619 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 4:37c12dbc8dc7 1620 }
mluis 4:37c12dbc8dc7 1621
mluis 2:14a5d6ad92d5 1622 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
mluis 2:14a5d6ad92d5 1623 {
mluis 2:14a5d6ad92d5 1624 if( ( McpsConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) ||
mluis 2:14a5d6ad92d5 1625 ( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) )
mluis 2:14a5d6ad92d5 1626 {
mluis 2:14a5d6ad92d5 1627 // Stop transmit cycle due to tx timeout.
mluis 8:26002607de9c 1628 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1629 MacCommandsBufferIndex = 0;
mluis 2:14a5d6ad92d5 1630 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1631 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1632 McpsConfirm.TxTimeOnAir = 0;
mluis 2:14a5d6ad92d5 1633 txTimeout = true;
mluis 2:14a5d6ad92d5 1634 }
mluis 2:14a5d6ad92d5 1635 }
mluis 2:14a5d6ad92d5 1636
mluis 2:14a5d6ad92d5 1637 if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
mluis 2:14a5d6ad92d5 1638 {
mluis 2:14a5d6ad92d5 1639 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
mluis 2:14a5d6ad92d5 1640 {
mluis 8:26002607de9c 1641 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
mluis 8:26002607de9c 1642 {// Procedure for the join request
mluis 8:26002607de9c 1643 MlmeConfirm.NbRetries = JoinRequestTrials;
mluis 8:26002607de9c 1644
mluis 8:26002607de9c 1645 if( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 8:26002607de9c 1646 {// Node joined successfully
mluis 8:26002607de9c 1647 UpLinkCounter = 0;
mluis 8:26002607de9c 1648 ChannelsNbRepCounter = 0;
mluis 8:26002607de9c 1649 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1650 }
mluis 8:26002607de9c 1651 else
mluis 2:14a5d6ad92d5 1652 {
mluis 8:26002607de9c 1653 if( JoinRequestTrials >= MaxJoinRequestTrials )
mluis 8:26002607de9c 1654 {
mluis 8:26002607de9c 1655 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1656 }
mluis 8:26002607de9c 1657 else
mluis 8:26002607de9c 1658 {
mluis 8:26002607de9c 1659 LoRaMacFlags.Bits.MacDone = 0;
mluis 8:26002607de9c 1660 // Sends the same frame again
mluis 8:26002607de9c 1661 OnTxDelayedTimerEvent( );
mluis 8:26002607de9c 1662 }
mluis 2:14a5d6ad92d5 1663 }
mluis 2:14a5d6ad92d5 1664 }
mluis 2:14a5d6ad92d5 1665 else
mluis 8:26002607de9c 1666 {// Procedure for all other frames
mluis 8:26002607de9c 1667 if( ( ChannelsNbRepCounter >= LoRaMacParams.ChannelsNbRep ) || ( LoRaMacFlags.Bits.McpsInd == 1 ) )
mluis 8:26002607de9c 1668 {
mluis 8:26002607de9c 1669 if( LoRaMacFlags.Bits.McpsInd == 0 )
mluis 8:26002607de9c 1670 { // Maximum repititions without downlink. Reset MacCommandsBufferIndex. Increase ADR Ack counter.
mluis 8:26002607de9c 1671 // Only process the case when the MAC did not receive a downlink.
mluis 8:26002607de9c 1672 MacCommandsBufferIndex = 0;
mluis 8:26002607de9c 1673 AdrAckCounter++;
mluis 8:26002607de9c 1674 }
mluis 8:26002607de9c 1675
mluis 8:26002607de9c 1676 ChannelsNbRepCounter = 0;
mluis 8:26002607de9c 1677
mluis 8:26002607de9c 1678 if( IsUpLinkCounterFixed == false )
mluis 8:26002607de9c 1679 {
mluis 8:26002607de9c 1680 UpLinkCounter++;
mluis 8:26002607de9c 1681 }
mluis 8:26002607de9c 1682
mluis 8:26002607de9c 1683 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1684 }
mluis 8:26002607de9c 1685 else
mluis 8:26002607de9c 1686 {
mluis 8:26002607de9c 1687 LoRaMacFlags.Bits.MacDone = 0;
mluis 8:26002607de9c 1688 // Sends the same frame again
mluis 8:26002607de9c 1689 OnTxDelayedTimerEvent( );
mluis 8:26002607de9c 1690 }
mluis 2:14a5d6ad92d5 1691 }
mluis 2:14a5d6ad92d5 1692 }
mluis 2:14a5d6ad92d5 1693 }
mluis 2:14a5d6ad92d5 1694
mluis 2:14a5d6ad92d5 1695 if( LoRaMacFlags.Bits.McpsInd == 1 )
mluis 8:26002607de9c 1696 {// Procedure if we received a frame
mluis 2:14a5d6ad92d5 1697 if( ( McpsConfirm.AckReceived == true ) || ( AckTimeoutRetriesCounter > AckTimeoutRetries ) )
mluis 2:14a5d6ad92d5 1698 {
mluis 2:14a5d6ad92d5 1699 AckTimeoutRetry = false;
mluis 3:b9d87593a8ae 1700 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 1701 if( IsUpLinkCounterFixed == false )
mluis 2:14a5d6ad92d5 1702 {
mluis 2:14a5d6ad92d5 1703 UpLinkCounter++;
mluis 2:14a5d6ad92d5 1704 }
mluis 2:14a5d6ad92d5 1705 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1706
mluis 8:26002607de9c 1707 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 2:14a5d6ad92d5 1708 }
mluis 2:14a5d6ad92d5 1709 }
mluis 2:14a5d6ad92d5 1710
mluis 8:26002607de9c 1711 if( ( AckTimeoutRetry == true ) && ( ( LoRaMacState & LORAMAC_TX_DELAYED ) == 0 ) )
mluis 8:26002607de9c 1712 {// Retransmissions procedure for confirmed uplinks
mluis 2:14a5d6ad92d5 1713 AckTimeoutRetry = false;
mluis 2:14a5d6ad92d5 1714 if( ( AckTimeoutRetriesCounter < AckTimeoutRetries ) && ( AckTimeoutRetriesCounter <= MAX_ACK_RETRIES ) )
mluis 2:14a5d6ad92d5 1715 {
mluis 2:14a5d6ad92d5 1716 AckTimeoutRetriesCounter++;
mluis 2:14a5d6ad92d5 1717
mluis 2:14a5d6ad92d5 1718 if( ( AckTimeoutRetriesCounter % 2 ) == 1 )
mluis 2:14a5d6ad92d5 1719 {
mluis 7:c16969e0f70f 1720 LoRaMacParams.ChannelsDatarate = MAX( LoRaMacParams.ChannelsDatarate - 1, LORAMAC_TX_MIN_DATARATE );
mluis 2:14a5d6ad92d5 1721 }
mluis 8:26002607de9c 1722 // Try to send the frame again
mluis 8:26002607de9c 1723 if( ScheduleTx( ) == LORAMAC_STATUS_OK )
mluis 8:26002607de9c 1724 {
mluis 8:26002607de9c 1725 LoRaMacFlags.Bits.MacDone = 0;
mluis 8:26002607de9c 1726 }
mluis 8:26002607de9c 1727 else
mluis 8:26002607de9c 1728 {
mluis 8:26002607de9c 1729 // The DR is not applicable for the payload size
mluis 8:26002607de9c 1730 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR;
mluis 8:26002607de9c 1731
mluis 8:26002607de9c 1732 MacCommandsBufferIndex = 0;
mluis 8:26002607de9c 1733 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1734 NodeAckRequested = false;
mluis 8:26002607de9c 1735 McpsConfirm.AckReceived = false;
mluis 8:26002607de9c 1736 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 8:26002607de9c 1737 McpsConfirm.Datarate = LoRaMacParams.ChannelsDatarate;
mluis 8:26002607de9c 1738 if( IsUpLinkCounterFixed == false )
mluis 8:26002607de9c 1739 {
mluis 8:26002607de9c 1740 UpLinkCounter++;
mluis 8:26002607de9c 1741 }
mluis 8:26002607de9c 1742 }
mluis 2:14a5d6ad92d5 1743 }
mluis 2:14a5d6ad92d5 1744 else
mluis 2:14a5d6ad92d5 1745 {
mluis 2:14a5d6ad92d5 1746 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 1747 // Re-enable default channels LC1, LC2, LC3
mluis 7:c16969e0f70f 1748 LoRaMacParams.ChannelsMask[0] = LoRaMacParams.ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
mluis 8:26002607de9c 1749 #elif defined( USE_BAND_470 )
mluis 8:26002607de9c 1750 // Re-enable default channels
mluis 8:26002607de9c 1751 memcpy1( ( uint8_t* )LoRaMacParams.ChannelsMask, ( uint8_t* )LoRaMacParamsDefaults.ChannelsMask, sizeof( LoRaMacParams.ChannelsMask ) );
mluis 2:14a5d6ad92d5 1752 #elif defined( USE_BAND_915 )
mluis 2:14a5d6ad92d5 1753 // Re-enable default channels
mluis 8:26002607de9c 1754 memcpy1( ( uint8_t* )LoRaMacParams.ChannelsMask, ( uint8_t* )LoRaMacParamsDefaults.ChannelsMask, sizeof( LoRaMacParams.ChannelsMask ) );
mluis 2:14a5d6ad92d5 1755 #elif defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 1756 // Re-enable default channels
mluis 8:26002607de9c 1757 ReenableChannels( LoRaMacParamsDefaults.ChannelsMask[4], LoRaMacParams.ChannelsMask );
mluis 2:14a5d6ad92d5 1758 #else
mluis 2:14a5d6ad92d5 1759 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 1760 #endif
mluis 8:26002607de9c 1761 LoRaMacState &= ~LORAMAC_TX_RUNNING;
mluis 8:26002607de9c 1762
mluis 8:26002607de9c 1763 MacCommandsBufferIndex = 0;
mluis 3:b9d87593a8ae 1764 NodeAckRequested = false;
mluis 2:14a5d6ad92d5 1765 McpsConfirm.AckReceived = false;
mluis 2:14a5d6ad92d5 1766 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
mluis 2:14a5d6ad92d5 1767 if( IsUpLinkCounterFixed == false )
mluis 2:14a5d6ad92d5 1768 {
mluis 2:14a5d6ad92d5 1769 UpLinkCounter++;
mluis 2:14a5d6ad92d5 1770 }
mluis 2:14a5d6ad92d5 1771 }
mluis 2:14a5d6ad92d5 1772 }
mluis 2:14a5d6ad92d5 1773 }
mluis 2:14a5d6ad92d5 1774 // Handle reception for Class B and Class C
mluis 8:26002607de9c 1775 if( ( LoRaMacState & LORAMAC_RX ) == LORAMAC_RX )
mluis 2:14a5d6ad92d5 1776 {
mluis 8:26002607de9c 1777 LoRaMacState &= ~LORAMAC_RX;
mluis 2:14a5d6ad92d5 1778 }
mluis 8:26002607de9c 1779 if( LoRaMacState == LORAMAC_IDLE )
mluis 2:14a5d6ad92d5 1780 {
mluis 2:14a5d6ad92d5 1781 if( LoRaMacFlags.Bits.McpsReq == 1 )
mluis 2:14a5d6ad92d5 1782 {
mluis 2:14a5d6ad92d5 1783 LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
mluis 2:14a5d6ad92d5 1784 LoRaMacFlags.Bits.McpsReq = 0;
mluis 2:14a5d6ad92d5 1785 }
mluis 2:14a5d6ad92d5 1786
mluis 2:14a5d6ad92d5 1787 if( LoRaMacFlags.Bits.MlmeReq == 1 )
mluis 2:14a5d6ad92d5 1788 {
mluis 2:14a5d6ad92d5 1789 LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
mluis 2:14a5d6ad92d5 1790 LoRaMacFlags.Bits.MlmeReq = 0;
mluis 2:14a5d6ad92d5 1791 }
mluis 2:14a5d6ad92d5 1792
mluis 8:26002607de9c 1793 // Procedure done. Reset variables.
mluis 2:14a5d6ad92d5 1794 LoRaMacFlags.Bits.MacDone = 0;
mluis 2:14a5d6ad92d5 1795 }
mluis 2:14a5d6ad92d5 1796 else
mluis 2:14a5d6ad92d5 1797 {
mluis 2:14a5d6ad92d5 1798 // Operation not finished restart timer
mluis 3:b9d87593a8ae 1799 TimerSetValue( &MacStateCheckTimer, MAC_STATE_CHECK_TIMEOUT );
mluis 2:14a5d6ad92d5 1800 TimerStart( &MacStateCheckTimer );
mluis 2:14a5d6ad92d5 1801 }
mluis 2:14a5d6ad92d5 1802
mluis 2:14a5d6ad92d5 1803 if( LoRaMacFlags.Bits.McpsInd == 1 )
mluis 2:14a5d6ad92d5 1804 {
mluis 8:26002607de9c 1805 if( LoRaMacDeviceClass == CLASS_C )
mluis 8:26002607de9c 1806 {// Activate RX2 window for Class C
mluis 8:26002607de9c 1807 OnRxWindow2TimerEvent( );
mluis 8:26002607de9c 1808 }
mluis 8:26002607de9c 1809 if( LoRaMacFlags.Bits.McpsIndSkip == 0 )
mluis 8:26002607de9c 1810 {
mluis 8:26002607de9c 1811 LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
mluis 8:26002607de9c 1812 }
mluis 8:26002607de9c 1813 LoRaMacFlags.Bits.McpsIndSkip = 0;
mluis 2:14a5d6ad92d5 1814 LoRaMacFlags.Bits.McpsInd = 0;
mluis 2:14a5d6ad92d5 1815 }
mluis 2:14a5d6ad92d5 1816 }
mluis 2:14a5d6ad92d5 1817
mluis 2:14a5d6ad92d5 1818 static void OnTxDelayedTimerEvent( void )
mluis 2:14a5d6ad92d5 1819 {
mluis 7:c16969e0f70f 1820 LoRaMacHeader_t macHdr;
mluis 7:c16969e0f70f 1821 LoRaMacFrameCtrl_t fCtrl;
mluis 7:c16969e0f70f 1822
mluis 2:14a5d6ad92d5 1823 TimerStop( &TxDelayedTimer );
mluis 8:26002607de9c 1824 LoRaMacState &= ~LORAMAC_TX_DELAYED;
mluis 2:14a5d6ad92d5 1825
mluis 7:c16969e0f70f 1826 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
mluis 7:c16969e0f70f 1827 {
mluis 8:26002607de9c 1828 ResetMacParameters( );
mluis 8:26002607de9c 1829 // Add a +1, since we start to count from 0
mluis 8:26002607de9c 1830 LoRaMacParams.ChannelsDatarate = AlternateDatarate( JoinRequestTrials + 1 );
mluis 8:26002607de9c 1831
mluis 7:c16969e0f70f 1832 macHdr.Value = 0;
mluis 7:c16969e0f70f 1833 macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
mluis 7:c16969e0f70f 1834
mluis 7:c16969e0f70f 1835 fCtrl.Value = 0;
mluis 7:c16969e0f70f 1836 fCtrl.Bits.Adr = AdrCtrlOn;
mluis 7:c16969e0f70f 1837
mluis 8:26002607de9c 1838 /* In case of join request retransmissions, the stack must prepare
mluis 7:c16969e0f70f 1839 * the frame again, because the network server keeps track of the random
mluis 7:c16969e0f70f 1840 * LoRaMacDevNonce values to prevent reply attacks. */
mluis 7:c16969e0f70f 1841 PrepareFrame( &macHdr, &fCtrl, 0, NULL, 0 );
mluis 7:c16969e0f70f 1842 }
mluis 7:c16969e0f70f 1843
mluis 2:14a5d6ad92d5 1844 ScheduleTx( );
mluis 2:14a5d6ad92d5 1845 }
mluis 2:14a5d6ad92d5 1846
mluis 2:14a5d6ad92d5 1847 static void OnRxWindow1TimerEvent( void )
mluis 2:14a5d6ad92d5 1848 {
mluis 2:14a5d6ad92d5 1849 TimerStop( &RxWindowTimer1 );
mluis 2:14a5d6ad92d5 1850 RxSlot = 0;
mluis 2:14a5d6ad92d5 1851
mluis 2:14a5d6ad92d5 1852 if( LoRaMacDeviceClass == CLASS_C )
mluis 2:14a5d6ad92d5 1853 {
mluis 2:14a5d6ad92d5 1854 Radio.Standby( );
mluis 2:14a5d6ad92d5 1855 }
mluis 2:14a5d6ad92d5 1856
mluis 2:14a5d6ad92d5 1857 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 8:26002607de9c 1858 RxWindowSetup( Channels[Channel].Frequency, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
mluis 8:26002607de9c 1859 #elif defined( USE_BAND_470 )
mluis 8:26002607de9c 1860 RxWindowSetup( LORAMAC_FIRST_RX1_CHANNEL + ( Channel % 48 ) * LORAMAC_STEPWIDTH_RX1_CHANNEL, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
mluis 2:14a5d6ad92d5 1861 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
mluis 8:26002607de9c 1862 RxWindowSetup( LORAMAC_FIRST_RX1_CHANNEL + ( Channel % 8 ) * LORAMAC_STEPWIDTH_RX1_CHANNEL, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
mluis 2:14a5d6ad92d5 1863 #else
mluis 2:14a5d6ad92d5 1864 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 1865 #endif
mluis 2:14a5d6ad92d5 1866 }
mluis 2:14a5d6ad92d5 1867
mluis 2:14a5d6ad92d5 1868 static void OnRxWindow2TimerEvent( void )
mluis 2:14a5d6ad92d5 1869 {
mluis 8:26002607de9c 1870 bool rxContinuousMode = false;
mluis 2:14a5d6ad92d5 1871
mluis 2:14a5d6ad92d5 1872 TimerStop( &RxWindowTimer2 );
mluis 8:26002607de9c 1873
mluis 8:26002607de9c 1874 if( LoRaMacDeviceClass == CLASS_C )
mluis 4:37c12dbc8dc7 1875 {
mluis 8:26002607de9c 1876 rxContinuousMode = true;
mluis 2:14a5d6ad92d5 1877 }
mluis 8:26002607de9c 1878 if( RxWindowSetup( LoRaMacParams.Rx2Channel.Frequency, RxWindowsParams[1].Datarate, RxWindowsParams[1].Bandwidth, RxWindowsParams[1].RxWindowTimeout, rxContinuousMode ) == true )
mluis 2:14a5d6ad92d5 1879 {
mluis 8:26002607de9c 1880 RxSlot = 1;
mluis 2:14a5d6ad92d5 1881 }
mluis 2:14a5d6ad92d5 1882 }
mluis 2:14a5d6ad92d5 1883
mluis 2:14a5d6ad92d5 1884 static void OnAckTimeoutTimerEvent( void )
mluis 2:14a5d6ad92d5 1885 {
mluis 2:14a5d6ad92d5 1886 TimerStop( &AckTimeoutTimer );
mluis 2:14a5d6ad92d5 1887
mluis 2:14a5d6ad92d5 1888 if( NodeAckRequested == true )
mluis 2:14a5d6ad92d5 1889 {
mluis 2:14a5d6ad92d5 1890 AckTimeoutRetry = true;
mluis 8:26002607de9c 1891 LoRaMacState &= ~LORAMAC_ACK_REQ;
mluis 2:14a5d6ad92d5 1892 }
mluis 2:14a5d6ad92d5 1893 if( LoRaMacDeviceClass == CLASS_C )
mluis 2:14a5d6ad92d5 1894 {
mluis 2:14a5d6ad92d5 1895 LoRaMacFlags.Bits.MacDone = 1;
mluis 2:14a5d6ad92d5 1896 }
mluis 2:14a5d6ad92d5 1897 }
mluis 2:14a5d6ad92d5 1898
mluis 3:b9d87593a8ae 1899 static bool SetNextChannel( TimerTime_t* time )
mluis 2:14a5d6ad92d5 1900 {
mluis 0:91d1a7783bb9 1901 uint8_t nbEnabledChannels = 0;
mluis 3:b9d87593a8ae 1902 uint8_t delayTx = 0;
mluis 0:91d1a7783bb9 1903 uint8_t enabledChannels[LORA_MAX_NB_CHANNELS];
mluis 2:14a5d6ad92d5 1904 TimerTime_t nextTxDelay = ( TimerTime_t )( -1 );
mluis 0:91d1a7783bb9 1905
mluis 1:91e4e6c60d1e 1906 memset1( enabledChannels, 0, LORA_MAX_NB_CHANNELS );
mluis 0:91d1a7783bb9 1907
mluis 2:14a5d6ad92d5 1908 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 1909 if( CountNbEnabled125kHzChannels( ChannelsMaskRemaining ) == 0 )
mluis 3:b9d87593a8ae 1910 { // Restore default channels
mluis 7:c16969e0f70f 1911 memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) LoRaMacParams.ChannelsMask, 8 );
mluis 3:b9d87593a8ae 1912 }
mluis 7:c16969e0f70f 1913 if( ( LoRaMacParams.ChannelsDatarate >= DR_4 ) && ( ( ChannelsMaskRemaining[4] & 0x00FF ) == 0 ) )
mluis 3:b9d87593a8ae 1914 { // Make sure, that the channels are activated
mluis 7:c16969e0f70f 1915 ChannelsMaskRemaining[4] = LoRaMacParams.ChannelsMask[4];
mluis 3:b9d87593a8ae 1916 }
mluis 8:26002607de9c 1917 #elif defined( USE_BAND_470 )
mluis 8:26002607de9c 1918 if( ( CountBits( LoRaMacParams.ChannelsMask[0], 16 ) == 0 ) &&
mluis 8:26002607de9c 1919 ( CountBits( LoRaMacParams.ChannelsMask[1], 16 ) == 0 ) &&
mluis 8:26002607de9c 1920 ( CountBits( LoRaMacParams.ChannelsMask[2], 16 ) == 0 ) &&
mluis 8:26002607de9c 1921 ( CountBits( LoRaMacParams.ChannelsMask[3], 16 ) == 0 ) &&
mluis 8:26002607de9c 1922 ( CountBits( LoRaMacParams.ChannelsMask[4], 16 ) == 0 ) &&
mluis 8:26002607de9c 1923 ( CountBits( LoRaMacParams.ChannelsMask[5], 16 ) == 0 ) )
mluis 8:26002607de9c 1924 {
mluis 8:26002607de9c 1925 memcpy1( ( uint8_t* )LoRaMacParams.ChannelsMask, ( uint8_t* )LoRaMacParamsDefaults.ChannelsMask, sizeof( LoRaMacParams.ChannelsMask ) );
mluis 8:26002607de9c 1926 }
mluis 3:b9d87593a8ae 1927 #else
mluis 7:c16969e0f70f 1928 if( CountBits( LoRaMacParams.ChannelsMask[0], 16 ) == 0 )
mluis 3:b9d87593a8ae 1929 {
mluis 3:b9d87593a8ae 1930 // Re-enable default channels, if no channel is enabled
mluis 7:c16969e0f70f 1931 LoRaMacParams.ChannelsMask[0] = LoRaMacParams.ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
mluis 2:14a5d6ad92d5 1932 }
mluis 2:14a5d6ad92d5 1933 #endif
mluis 2:14a5d6ad92d5 1934
mluis 0:91d1a7783bb9 1935 // Update Aggregated duty cycle
mluis 4:37c12dbc8dc7 1936 if( AggregatedTimeOff <= TimerGetElapsedTime( AggregatedLastTxDoneTime ) )
mluis 0:91d1a7783bb9 1937 {
mluis 0:91d1a7783bb9 1938 AggregatedTimeOff = 0;
mluis 2:14a5d6ad92d5 1939
mluis 2:14a5d6ad92d5 1940 // Update bands Time OFF
mluis 2:14a5d6ad92d5 1941 for( uint8_t i = 0; i < LORA_MAX_NB_BANDS; i++ )
mluis 0:91d1a7783bb9 1942 {
mluis 8:26002607de9c 1943 if( ( IsLoRaMacNetworkJoined == false ) || ( DutyCycleOn == true ) )
mluis 0:91d1a7783bb9 1944 {
mluis 4:37c12dbc8dc7 1945 if( Bands[i].TimeOff <= TimerGetElapsedTime( Bands[i].LastTxDoneTime ) )
mluis 2:14a5d6ad92d5 1946 {
mluis 2:14a5d6ad92d5 1947 Bands[i].TimeOff = 0;
mluis 2:14a5d6ad92d5 1948 }
mluis 2:14a5d6ad92d5 1949 if( Bands[i].TimeOff != 0 )
mluis 2:14a5d6ad92d5 1950 {
mluis 8:26002607de9c 1951 nextTxDelay = MIN( Bands[i].TimeOff - TimerGetElapsedTime( Bands[i].LastTxDoneTime ), nextTxDelay );
mluis 2:14a5d6ad92d5 1952 }
mluis 2:14a5d6ad92d5 1953 }
mluis 2:14a5d6ad92d5 1954 else
mluis 2:14a5d6ad92d5 1955 {
mluis 8:26002607de9c 1956 if( DutyCycleOn == false )
mluis 8:26002607de9c 1957 {
mluis 8:26002607de9c 1958 Bands[i].TimeOff = 0;
mluis 8:26002607de9c 1959 }
mluis 0:91d1a7783bb9 1960 }
mluis 2:14a5d6ad92d5 1961 }
mluis 2:14a5d6ad92d5 1962
mluis 2:14a5d6ad92d5 1963 // Search how many channels are enabled
mluis 2:14a5d6ad92d5 1964 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS; i += 16, k++ )
mluis 2:14a5d6ad92d5 1965 {
mluis 2:14a5d6ad92d5 1966 for( uint8_t j = 0; j < 16; j++ )
mluis 0:91d1a7783bb9 1967 {
mluis 2:14a5d6ad92d5 1968 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 1969 if( ( ChannelsMaskRemaining[k] & ( 1 << j ) ) != 0 )
mluis 2:14a5d6ad92d5 1970 #else
mluis 7:c16969e0f70f 1971 if( ( LoRaMacParams.ChannelsMask[k] & ( 1 << j ) ) != 0 )
mluis 2:14a5d6ad92d5 1972 #endif
mluis 2:14a5d6ad92d5 1973 {
mluis 2:14a5d6ad92d5 1974 if( Channels[i + j].Frequency == 0 )
mluis 2:14a5d6ad92d5 1975 { // Check if the channel is enabled
mluis 2:14a5d6ad92d5 1976 continue;
mluis 2:14a5d6ad92d5 1977 }
mluis 4:37c12dbc8dc7 1978 #if defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 )
mluis 4:37c12dbc8dc7 1979 if( IsLoRaMacNetworkJoined == false )
mluis 4:37c12dbc8dc7 1980 {
mluis 4:37c12dbc8dc7 1981 if( ( JOIN_CHANNELS & ( 1 << j ) ) == 0 )
mluis 4:37c12dbc8dc7 1982 {
mluis 4:37c12dbc8dc7 1983 continue;
mluis 4:37c12dbc8dc7 1984 }
mluis 4:37c12dbc8dc7 1985 }
mluis 4:37c12dbc8dc7 1986 #endif
mluis 7:c16969e0f70f 1987 if( ( ( Channels[i + j].DrRange.Fields.Min <= LoRaMacParams.ChannelsDatarate ) &&
mluis 7:c16969e0f70f 1988 ( LoRaMacParams.ChannelsDatarate <= Channels[i + j].DrRange.Fields.Max ) ) == false )
mluis 2:14a5d6ad92d5 1989 { // Check if the current channel selection supports the given datarate
mluis 2:14a5d6ad92d5 1990 continue;
mluis 2:14a5d6ad92d5 1991 }
mluis 2:14a5d6ad92d5 1992 if( Bands[Channels[i + j].Band].TimeOff > 0 )
mluis 2:14a5d6ad92d5 1993 { // Check if the band is available for transmission
mluis 3:b9d87593a8ae 1994 delayTx++;
mluis 2:14a5d6ad92d5 1995 continue;
mluis 2:14a5d6ad92d5 1996 }
mluis 2:14a5d6ad92d5 1997 enabledChannels[nbEnabledChannels++] = i + j;
mluis 2:14a5d6ad92d5 1998 }
mluis 2:14a5d6ad92d5 1999 }
mluis 2:14a5d6ad92d5 2000 }
mluis 2:14a5d6ad92d5 2001 }
mluis 2:14a5d6ad92d5 2002 else
mluis 2:14a5d6ad92d5 2003 {
mluis 3:b9d87593a8ae 2004 delayTx++;
mluis 4:37c12dbc8dc7 2005 nextTxDelay = AggregatedTimeOff - TimerGetElapsedTime( AggregatedLastTxDoneTime );
mluis 2:14a5d6ad92d5 2006 }
mluis 2:14a5d6ad92d5 2007
mluis 2:14a5d6ad92d5 2008 if( nbEnabledChannels > 0 )
mluis 2:14a5d6ad92d5 2009 {
mluis 2:14a5d6ad92d5 2010 Channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
mluis 2:14a5d6ad92d5 2011 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 2012 if( Channel < ( LORA_MAX_NB_CHANNELS - 8 ) )
mluis 3:b9d87593a8ae 2013 {
mluis 3:b9d87593a8ae 2014 DisableChannelInMask( Channel, ChannelsMaskRemaining );
mluis 3:b9d87593a8ae 2015 }
mluis 2:14a5d6ad92d5 2016 #endif
mluis 3:b9d87593a8ae 2017 *time = 0;
mluis 3:b9d87593a8ae 2018 return true;
mluis 2:14a5d6ad92d5 2019 }
mluis 2:14a5d6ad92d5 2020 else
mluis 2:14a5d6ad92d5 2021 {
mluis 3:b9d87593a8ae 2022 if( delayTx > 0 )
mluis 3:b9d87593a8ae 2023 {
mluis 3:b9d87593a8ae 2024 // Delay transmission due to AggregatedTimeOff or to a band time off
mluis 3:b9d87593a8ae 2025 *time = nextTxDelay;
mluis 3:b9d87593a8ae 2026 return true;
mluis 3:b9d87593a8ae 2027 }
mluis 3:b9d87593a8ae 2028 // Datarate not supported by any channel
mluis 3:b9d87593a8ae 2029 *time = 0;
mluis 3:b9d87593a8ae 2030 return false;
mluis 2:14a5d6ad92d5 2031 }
mluis 2:14a5d6ad92d5 2032 }
mluis 2:14a5d6ad92d5 2033
mluis 8:26002607de9c 2034 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous )
mluis 2:14a5d6ad92d5 2035 {
mluis 2:14a5d6ad92d5 2036 uint8_t downlinkDatarate = Datarates[datarate];
mluis 2:14a5d6ad92d5 2037 RadioModems_t modem;
mluis 2:14a5d6ad92d5 2038
mluis 2:14a5d6ad92d5 2039 if( Radio.GetStatus( ) == RF_IDLE )
mluis 2:14a5d6ad92d5 2040 {
mluis 2:14a5d6ad92d5 2041 Radio.SetChannel( freq );
mluis 2:14a5d6ad92d5 2042
mluis 2:14a5d6ad92d5 2043 // Store downlink datarate
mluis 2:14a5d6ad92d5 2044 McpsIndication.RxDatarate = ( uint8_t ) datarate;
mluis 2:14a5d6ad92d5 2045
mluis 2:14a5d6ad92d5 2046 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 2:14a5d6ad92d5 2047 if( datarate == DR_7 )
mluis 2:14a5d6ad92d5 2048 {
mluis 2:14a5d6ad92d5 2049 modem = MODEM_FSK;
mluis 8:26002607de9c 2050 Radio.SetRxConfig( modem, 50e3, downlinkDatarate * 1e3, 0, 83.333e3, 5, timeout, false, 0, true, 0, 0, false, rxContinuous );
mluis 2:14a5d6ad92d5 2051 }
mluis 2:14a5d6ad92d5 2052 else
mluis 2:14a5d6ad92d5 2053 {
mluis 2:14a5d6ad92d5 2054 modem = MODEM_LORA;
mluis 2:14a5d6ad92d5 2055 Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
mluis 2:14a5d6ad92d5 2056 }
mluis 8:26002607de9c 2057 #elif defined( USE_BAND_470 ) || defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 2058 modem = MODEM_LORA;
mluis 2:14a5d6ad92d5 2059 Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
mluis 2:14a5d6ad92d5 2060 #endif
mluis 2:14a5d6ad92d5 2061
mluis 2:14a5d6ad92d5 2062 if( RepeaterSupport == true )
mluis 2:14a5d6ad92d5 2063 {
mluis 4:37c12dbc8dc7 2064 Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateRepeater[datarate] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
mluis 2:14a5d6ad92d5 2065 }
mluis 2:14a5d6ad92d5 2066 else
mluis 2:14a5d6ad92d5 2067 {
mluis 4:37c12dbc8dc7 2068 Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarate[datarate] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
mluis 2:14a5d6ad92d5 2069 }
mluis 2:14a5d6ad92d5 2070
mluis 2:14a5d6ad92d5 2071 if( rxContinuous == false )
mluis 2:14a5d6ad92d5 2072 {
mluis 7:c16969e0f70f 2073 Radio.Rx( LoRaMacParams.MaxRxWindow );
mluis 2:14a5d6ad92d5 2074 }
mluis 2:14a5d6ad92d5 2075 else
mluis 2:14a5d6ad92d5 2076 {
mluis 2:14a5d6ad92d5 2077 Radio.Rx( 0 ); // Continuous mode
mluis 2:14a5d6ad92d5 2078 }
mluis 8:26002607de9c 2079 return true;
mluis 2:14a5d6ad92d5 2080 }
mluis 8:26002607de9c 2081 return false;
mluis 2:14a5d6ad92d5 2082 }
mluis 2:14a5d6ad92d5 2083
mluis 7:c16969e0f70f 2084 static bool Rx2FreqInRange( uint32_t freq )
mluis 7:c16969e0f70f 2085 {
mluis 7:c16969e0f70f 2086 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 7:c16969e0f70f 2087 if( Radio.CheckRfFrequency( freq ) == true )
mluis 8:26002607de9c 2088 #elif defined( USE_BAND_470 ) || defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 7:c16969e0f70f 2089 if( ( Radio.CheckRfFrequency( freq ) == true ) &&
mluis 8:26002607de9c 2090 ( freq >= LORAMAC_FIRST_RX1_CHANNEL ) &&
mluis 8:26002607de9c 2091 ( freq <= LORAMAC_LAST_RX1_CHANNEL ) &&
mluis 8:26002607de9c 2092 ( ( ( freq - ( uint32_t ) LORAMAC_FIRST_RX1_CHANNEL ) % ( uint32_t ) LORAMAC_STEPWIDTH_RX1_CHANNEL ) == 0 ) )
mluis 7:c16969e0f70f 2093 #endif
mluis 7:c16969e0f70f 2094 {
mluis 7:c16969e0f70f 2095 return true;
mluis 7:c16969e0f70f 2096 }
mluis 7:c16969e0f70f 2097 return false;
mluis 7:c16969e0f70f 2098 }
mluis 7:c16969e0f70f 2099
mluis 2:14a5d6ad92d5 2100 static bool ValidatePayloadLength( uint8_t lenN, int8_t datarate, uint8_t fOptsLen )
mluis 2:14a5d6ad92d5 2101 {
mluis 2:14a5d6ad92d5 2102 uint16_t maxN = 0;
mluis 2:14a5d6ad92d5 2103 uint16_t payloadSize = 0;
mluis 2:14a5d6ad92d5 2104
mluis 2:14a5d6ad92d5 2105 // Get the maximum payload length
mluis 2:14a5d6ad92d5 2106 if( RepeaterSupport == true )
mluis 2:14a5d6ad92d5 2107 {
mluis 2:14a5d6ad92d5 2108 maxN = MaxPayloadOfDatarateRepeater[datarate];
mluis 2:14a5d6ad92d5 2109 }
mluis 2:14a5d6ad92d5 2110 else
mluis 2:14a5d6ad92d5 2111 {
mluis 2:14a5d6ad92d5 2112 maxN = MaxPayloadOfDatarate[datarate];
mluis 2:14a5d6ad92d5 2113 }
mluis 2:14a5d6ad92d5 2114
mluis 2:14a5d6ad92d5 2115 // Calculate the resulting payload size
mluis 2:14a5d6ad92d5 2116 payloadSize = ( lenN + fOptsLen );
mluis 2:14a5d6ad92d5 2117
mluis 2:14a5d6ad92d5 2118 // Validation of the application payload size
mluis 2:14a5d6ad92d5 2119 if( ( payloadSize <= maxN ) && ( payloadSize <= LORAMAC_PHY_MAXPAYLOAD ) )
mluis 2:14a5d6ad92d5 2120 {
mluis 2:14a5d6ad92d5 2121 return true;
mluis 2:14a5d6ad92d5 2122 }
mluis 2:14a5d6ad92d5 2123 return false;
mluis 2:14a5d6ad92d5 2124 }
mluis 2:14a5d6ad92d5 2125
mluis 4:37c12dbc8dc7 2126 static uint8_t CountBits( uint16_t mask, uint8_t nbBits )
mluis 4:37c12dbc8dc7 2127 {
mluis 4:37c12dbc8dc7 2128 uint8_t nbActiveBits = 0;
mluis 4:37c12dbc8dc7 2129
mluis 4:37c12dbc8dc7 2130 for( uint8_t j = 0; j < nbBits; j++ )
mluis 4:37c12dbc8dc7 2131 {
mluis 4:37c12dbc8dc7 2132 if( ( mask & ( 1 << j ) ) == ( 1 << j ) )
mluis 4:37c12dbc8dc7 2133 {
mluis 4:37c12dbc8dc7 2134 nbActiveBits++;
mluis 4:37c12dbc8dc7 2135 }
mluis 4:37c12dbc8dc7 2136 }
mluis 4:37c12dbc8dc7 2137 return nbActiveBits;
mluis 4:37c12dbc8dc7 2138 }
mluis 4:37c12dbc8dc7 2139
mluis 2:14a5d6ad92d5 2140 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 2:14a5d6ad92d5 2141 static uint8_t CountNbEnabled125kHzChannels( uint16_t *channelsMask )
mluis 2:14a5d6ad92d5 2142 {
mluis 2:14a5d6ad92d5 2143 uint8_t nb125kHzChannels = 0;
mluis 2:14a5d6ad92d5 2144
mluis 2:14a5d6ad92d5 2145 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS - 8; i += 16, k++ )
mluis 2:14a5d6ad92d5 2146 {
mluis 4:37c12dbc8dc7 2147 nb125kHzChannels += CountBits( channelsMask[k], 16 );
mluis 2:14a5d6ad92d5 2148 }
mluis 2:14a5d6ad92d5 2149
mluis 2:14a5d6ad92d5 2150 return nb125kHzChannels;
mluis 2:14a5d6ad92d5 2151 }
mluis 4:37c12dbc8dc7 2152
mluis 4:37c12dbc8dc7 2153 #if defined( USE_BAND_915_HYBRID )
mluis 8:26002607de9c 2154 static void ReenableChannels( uint16_t mask, uint16_t* channelsMask )
mluis 4:37c12dbc8dc7 2155 {
mluis 4:37c12dbc8dc7 2156 uint16_t blockMask = mask;
mluis 4:37c12dbc8dc7 2157
mluis 4:37c12dbc8dc7 2158 for( uint8_t i = 0, j = 0; i < 4; i++, j += 2 )
mluis 4:37c12dbc8dc7 2159 {
mluis 8:26002607de9c 2160 channelsMask[i] = 0;
mluis 4:37c12dbc8dc7 2161 if( ( blockMask & ( 1 << j ) ) != 0 )
mluis 4:37c12dbc8dc7 2162 {
mluis 8:26002607de9c 2163 channelsMask[i] |= 0x00FF;
mluis 4:37c12dbc8dc7 2164 }
mluis 4:37c12dbc8dc7 2165 if( ( blockMask & ( 1 << ( j + 1 ) ) ) != 0 )
mluis 4:37c12dbc8dc7 2166 {
mluis 8:26002607de9c 2167 channelsMask[i] |= 0xFF00;
mluis 4:37c12dbc8dc7 2168 }
mluis 4:37c12dbc8dc7 2169 }
mluis 8:26002607de9c 2170 channelsMask[4] = blockMask;
mluis 8:26002607de9c 2171 channelsMask[5] = 0x0000;
mluis 4:37c12dbc8dc7 2172 }
mluis 4:37c12dbc8dc7 2173
mluis 8:26002607de9c 2174 static bool ValidateChannelMask( uint16_t* channelsMask )
mluis 4:37c12dbc8dc7 2175 {
mluis 4:37c12dbc8dc7 2176 bool chanMaskState = false;
mluis 4:37c12dbc8dc7 2177 uint16_t block1 = 0;
mluis 4:37c12dbc8dc7 2178 uint16_t block2 = 0;
mluis 4:37c12dbc8dc7 2179 uint8_t index = 0;
mluis 4:37c12dbc8dc7 2180
mluis 4:37c12dbc8dc7 2181 for( uint8_t i = 0; i < 4; i++ )
mluis 4:37c12dbc8dc7 2182 {
mluis 8:26002607de9c 2183 block1 = channelsMask[i] & 0x00FF;
mluis 8:26002607de9c 2184 block2 = channelsMask[i] & 0xFF00;
mluis 4:37c12dbc8dc7 2185
mluis 4:37c12dbc8dc7 2186 if( ( CountBits( block1, 16 ) > 5 ) && ( chanMaskState == false ) )
mluis 4:37c12dbc8dc7 2187 {
mluis 8:26002607de9c 2188 channelsMask[i] &= block1;
mluis 8:26002607de9c 2189 channelsMask[4] = 1 << ( i * 2 );
mluis 4:37c12dbc8dc7 2190 chanMaskState = true;
mluis 4:37c12dbc8dc7 2191 index = i;
mluis 4:37c12dbc8dc7 2192 }
mluis 4:37c12dbc8dc7 2193 else if( ( CountBits( block2, 16 ) > 5 ) && ( chanMaskState == false ) )
mluis 4:37c12dbc8dc7 2194 {
mluis 8:26002607de9c 2195 channelsMask[i] &= block2;
mluis 8:26002607de9c 2196 channelsMask[4] = 1 << ( i * 2 + 1 );
mluis 4:37c12dbc8dc7 2197 chanMaskState = true;
mluis 4:37c12dbc8dc7 2198 index = i;
mluis 4:37c12dbc8dc7 2199 }
mluis 4:37c12dbc8dc7 2200 }
mluis 4:37c12dbc8dc7 2201
mluis 4:37c12dbc8dc7 2202 // Do only change the channel mask, if we have found a valid block.
mluis 4:37c12dbc8dc7 2203 if( chanMaskState == true )
mluis 4:37c12dbc8dc7 2204 {
mluis 4:37c12dbc8dc7 2205 for( uint8_t i = 0; i < 4; i++ )
mluis 4:37c12dbc8dc7 2206 {
mluis 4:37c12dbc8dc7 2207 if( i != index )
mluis 4:37c12dbc8dc7 2208 {
mluis 8:26002607de9c 2209 channelsMask[i] = 0;
mluis 4:37c12dbc8dc7 2210 }
mluis 4:37c12dbc8dc7 2211 }
mluis 4:37c12dbc8dc7 2212 }
mluis 4:37c12dbc8dc7 2213 return chanMaskState;
mluis 4:37c12dbc8dc7 2214 }
mluis 4:37c12dbc8dc7 2215 #endif
mluis 2:14a5d6ad92d5 2216 #endif
mluis 2:14a5d6ad92d5 2217
mluis 8:26002607de9c 2218 static bool ValidateDatarate( int8_t datarate, uint16_t* channelsMask )
mluis 8:26002607de9c 2219 {
mluis 8:26002607de9c 2220 if( ValueInRange( datarate, LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) == false )
mluis 8:26002607de9c 2221 {
mluis 8:26002607de9c 2222 return false;
mluis 8:26002607de9c 2223 }
mluis 8:26002607de9c 2224 for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS; i += 16, k++ )
mluis 8:26002607de9c 2225 {
mluis 8:26002607de9c 2226 for( uint8_t j = 0; j < 16; j++ )
mluis 8:26002607de9c 2227 {
mluis 8:26002607de9c 2228 if( ( ( channelsMask[k] & ( 1 << j ) ) != 0 ) )
mluis 8:26002607de9c 2229 {// Check datarate validity for enabled channels
mluis 8:26002607de9c 2230 if( ValueInRange( datarate, Channels[i + j].DrRange.Fields.Min, Channels[i + j].DrRange.Fields.Max ) == true )
mluis 8:26002607de9c 2231 {
mluis 8:26002607de9c 2232 // At least 1 channel has been found we can return OK.
mluis 8:26002607de9c 2233 return true;
mluis 8:26002607de9c 2234 }
mluis 8:26002607de9c 2235 }
mluis 8:26002607de9c 2236 }
mluis 8:26002607de9c 2237 }
mluis 8:26002607de9c 2238 return false;
mluis 8:26002607de9c 2239 }
mluis 8:26002607de9c 2240
mluis 8:26002607de9c 2241 static int8_t LimitTxPower( int8_t txPower, int8_t maxBandTxPower )
mluis 2:14a5d6ad92d5 2242 {
mluis 2:14a5d6ad92d5 2243 int8_t resultTxPower = txPower;
mluis 8:26002607de9c 2244
mluis 8:26002607de9c 2245 // Limit tx power to the band max
mluis 8:26002607de9c 2246 resultTxPower = MAX( txPower, maxBandTxPower );
mluis 8:26002607de9c 2247
mluis 2:14a5d6ad92d5 2248 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 7:c16969e0f70f 2249 if( ( LoRaMacParams.ChannelsDatarate == DR_4 ) ||
mluis 7:c16969e0f70f 2250 ( ( LoRaMacParams.ChannelsDatarate >= DR_8 ) && ( LoRaMacParams.ChannelsDatarate <= DR_13 ) ) )
mluis 2:14a5d6ad92d5 2251 {// Limit tx power to max 26dBm
mluis 2:14a5d6ad92d5 2252 resultTxPower = MAX( txPower, TX_POWER_26_DBM );
mluis 2:14a5d6ad92d5 2253 }
mluis 2:14a5d6ad92d5 2254 else
mluis 2:14a5d6ad92d5 2255 {
mluis 7:c16969e0f70f 2256 if( CountNbEnabled125kHzChannels( LoRaMacParams.ChannelsMask ) < 50 )
mluis 2:14a5d6ad92d5 2257 {// Limit tx power to max 21dBm
mluis 2:14a5d6ad92d5 2258 resultTxPower = MAX( txPower, TX_POWER_20_DBM );
mluis 2:14a5d6ad92d5 2259 }
mluis 2:14a5d6ad92d5 2260 }
mluis 2:14a5d6ad92d5 2261 #endif
mluis 2:14a5d6ad92d5 2262 return resultTxPower;
mluis 2:14a5d6ad92d5 2263 }
mluis 2:14a5d6ad92d5 2264
mluis 2:14a5d6ad92d5 2265 static bool ValueInRange( int8_t value, int8_t min, int8_t max )
mluis 2:14a5d6ad92d5 2266 {
mluis 2:14a5d6ad92d5 2267 if( ( value >= min ) && ( value <= max ) )
mluis 2:14a5d6ad92d5 2268 {
mluis 2:14a5d6ad92d5 2269 return true;
mluis 2:14a5d6ad92d5 2270 }
mluis 2:14a5d6ad92d5 2271 return false;
mluis 2:14a5d6ad92d5 2272 }
mluis 2:14a5d6ad92d5 2273
mluis 2:14a5d6ad92d5 2274 static bool DisableChannelInMask( uint8_t id, uint16_t* mask )
mluis 2:14a5d6ad92d5 2275 {
mluis 2:14a5d6ad92d5 2276 uint8_t index = 0;
mluis 2:14a5d6ad92d5 2277 index = id / 16;
mluis 2:14a5d6ad92d5 2278
mluis 2:14a5d6ad92d5 2279 if( ( index > 4 ) || ( id >= LORA_MAX_NB_CHANNELS ) )
mluis 2:14a5d6ad92d5 2280 {
mluis 2:14a5d6ad92d5 2281 return false;
mluis 2:14a5d6ad92d5 2282 }
mluis 2:14a5d6ad92d5 2283
mluis 2:14a5d6ad92d5 2284 // Deactivate channel
mluis 2:14a5d6ad92d5 2285 mask[index] &= ~( 1 << ( id % 16 ) );
mluis 2:14a5d6ad92d5 2286
mluis 2:14a5d6ad92d5 2287 return true;
mluis 2:14a5d6ad92d5 2288 }
mluis 2:14a5d6ad92d5 2289
mluis 2:14a5d6ad92d5 2290 static bool AdrNextDr( bool adrEnabled, bool updateChannelMask, int8_t* datarateOut )
mluis 2:14a5d6ad92d5 2291 {
mluis 2:14a5d6ad92d5 2292 bool adrAckReq = false;
mluis 7:c16969e0f70f 2293 int8_t datarate = LoRaMacParams.ChannelsDatarate;
mluis 2:14a5d6ad92d5 2294
mluis 2:14a5d6ad92d5 2295 if( adrEnabled == true )
mluis 2:14a5d6ad92d5 2296 {
mluis 4:37c12dbc8dc7 2297 if( datarate == LORAMAC_TX_MIN_DATARATE )
mluis 2:14a5d6ad92d5 2298 {
mluis 2:14a5d6ad92d5 2299 AdrAckCounter = 0;
mluis 2:14a5d6ad92d5 2300 adrAckReq = false;
mluis 0:91d1a7783bb9 2301 }
mluis 0:91d1a7783bb9 2302 else
mluis 0:91d1a7783bb9 2303 {
mluis 3:b9d87593a8ae 2304 if( AdrAckCounter >= ADR_ACK_LIMIT )
mluis 2:14a5d6ad92d5 2305 {
mluis 2:14a5d6ad92d5 2306 adrAckReq = true;
mluis 8:26002607de9c 2307 LoRaMacParams.ChannelsTxPower = LORAMAC_MAX_TX_POWER;
mluis 2:14a5d6ad92d5 2308 }
mluis 2:14a5d6ad92d5 2309 else
mluis 2:14a5d6ad92d5 2310 {
mluis 2:14a5d6ad92d5 2311 adrAckReq = false;
mluis 2:14a5d6ad92d5 2312 }
mluis 3:b9d87593a8ae 2313 if( AdrAckCounter >= ( ADR_ACK_LIMIT + ADR_ACK_DELAY ) )
mluis 0:91d1a7783bb9 2314 {
mluis 8:26002607de9c 2315 if( ( AdrAckCounter % ADR_ACK_DELAY ) == 1 )
mluis 3:b9d87593a8ae 2316 {
mluis 2:14a5d6ad92d5 2317 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
mluis 4:37c12dbc8dc7 2318 if( datarate > LORAMAC_TX_MIN_DATARATE )
mluis 3:b9d87593a8ae 2319 {
mluis 3:b9d87593a8ae 2320 datarate--;
mluis 3:b9d87593a8ae 2321 }
mluis 4:37c12dbc8dc7 2322 if( datarate == LORAMAC_TX_MIN_DATARATE )
mluis 3:b9d87593a8ae 2323 {
mluis 3:b9d87593a8ae 2324 if( updateChannelMask == true )
mluis 3:b9d87593a8ae 2325 {
mluis 3:b9d87593a8ae 2326 // Re-enable default channels LC1, LC2, LC3
mluis 7:c16969e0f70f 2327 LoRaMacParams.ChannelsMask[0] = LoRaMacParams.ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
mluis 3:b9d87593a8ae 2328 }
mluis 3:b9d87593a8ae 2329 }
mluis 8:26002607de9c 2330 #elif defined( USE_BAND_470 )
mluis 8:26002607de9c 2331 if( datarate > LORAMAC_TX_MIN_DATARATE )
mluis 8:26002607de9c 2332 {
mluis 8:26002607de9c 2333 datarate--;
mluis 8:26002607de9c 2334 }
mluis 8:26002607de9c 2335 if( datarate == LORAMAC_TX_MIN_DATARATE )
mluis 8:26002607de9c 2336 {
mluis 8:26002607de9c 2337 if( updateChannelMask == true )
mluis 8:26002607de9c 2338 {
mluis 8:26002607de9c 2339 // Re-enable default channels
mluis 8:26002607de9c 2340 memcpy1( ( uint8_t* )LoRaMacParams.ChannelsMask, ( uint8_t* )LoRaMacParamsDefaults.ChannelsMask, sizeof( LoRaMacParams.ChannelsMask ) );
mluis 8:26002607de9c 2341 }
mluis 8:26002607de9c 2342 }
mluis 2:14a5d6ad92d5 2343 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
mluis 4:37c12dbc8dc7 2344 if( ( datarate > LORAMAC_TX_MIN_DATARATE ) && ( datarate == DR_8 ) )
mluis 3:b9d87593a8ae 2345 {
mluis 3:b9d87593a8ae 2346 datarate = DR_4;
mluis 3:b9d87593a8ae 2347 }
mluis 4:37c12dbc8dc7 2348 else if( datarate > LORAMAC_TX_MIN_DATARATE )
mluis 3:b9d87593a8ae 2349 {
mluis 3:b9d87593a8ae 2350 datarate--;
mluis 3:b9d87593a8ae 2351 }
mluis 4:37c12dbc8dc7 2352 if( datarate == LORAMAC_TX_MIN_DATARATE )
mluis 3:b9d87593a8ae 2353 {
mluis 3:b9d87593a8ae 2354 if( updateChannelMask == true )
mluis 3:b9d87593a8ae 2355 {
mluis 3:b9d87593a8ae 2356 #if defined( USE_BAND_915 )
mluis 3:b9d87593a8ae 2357 // Re-enable default channels
mluis 8:26002607de9c 2358 memcpy1( ( uint8_t* )LoRaMacParams.ChannelsMask, ( uint8_t* )LoRaMacParamsDefaults.ChannelsMask, sizeof( LoRaMacParams.ChannelsMask ) );
mluis 3:b9d87593a8ae 2359 #else // defined( USE_BAND_915_HYBRID )
mluis 3:b9d87593a8ae 2360 // Re-enable default channels
mluis 8:26002607de9c 2361 ReenableChannels( LoRaMacParamsDefaults.ChannelsMask[4], LoRaMacParams.ChannelsMask );
mluis 3:b9d87593a8ae 2362 #endif
mluis 3:b9d87593a8ae 2363 }
mluis 3:b9d87593a8ae 2364 }
mluis 2:14a5d6ad92d5 2365 #else
mluis 2:14a5d6ad92d5 2366 #error "Please define a frequency band in the compiler options."
mluis 2:14a5d6ad92d5 2367 #endif
mluis 3:b9d87593a8ae 2368 }
mluis 0:91d1a7783bb9 2369 }
mluis 0:91d1a7783bb9 2370 }
mluis 0:91d1a7783bb9 2371 }
mluis 2:14a5d6ad92d5 2372
mluis 2:14a5d6ad92d5 2373 *datarateOut = datarate;
mluis 2:14a5d6ad92d5 2374
mluis 2:14a5d6ad92d5 2375 return adrAckReq;
mluis 0:91d1a7783bb9 2376 }
mluis 0:91d1a7783bb9 2377
mluis 2:14a5d6ad92d5 2378 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 )
mluis 0:91d1a7783bb9 2379 {
mluis 2:14a5d6ad92d5 2380 LoRaMacStatus_t status = LORAMAC_STATUS_BUSY;
mluis 7:c16969e0f70f 2381 // The maximum buffer length must take MAC commands to re-send into account.
mluis 7:c16969e0f70f 2382 uint8_t bufLen = LORA_MAC_COMMAND_MAX_LENGTH - MacCommandsBufferToRepeatIndex;
mluis 1:91e4e6c60d1e 2383
mluis 0:91d1a7783bb9 2384 switch( cmd )
mluis 0:91d1a7783bb9 2385 {
mluis 0:91d1a7783bb9 2386 case MOTE_MAC_LINK_CHECK_REQ:
mluis 7:c16969e0f70f 2387 if( MacCommandsBufferIndex < bufLen )
mluis 1:91e4e6c60d1e 2388 {
mluis 1:91e4e6c60d1e 2389 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2390 // No payload for this command
mluis 2:14a5d6ad92d5 2391 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2392 }
mluis 0:91d1a7783bb9 2393 break;
mluis 0:91d1a7783bb9 2394 case MOTE_MAC_LINK_ADR_ANS:
mluis 7:c16969e0f70f 2395 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
mluis 1:91e4e6c60d1e 2396 {
mluis 1:91e4e6c60d1e 2397 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2398 // Margin
mluis 1:91e4e6c60d1e 2399 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
mluis 2:14a5d6ad92d5 2400 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2401 }
mluis 0:91d1a7783bb9 2402 break;
mluis 0:91d1a7783bb9 2403 case MOTE_MAC_DUTY_CYCLE_ANS:
mluis 7:c16969e0f70f 2404 if( MacCommandsBufferIndex < bufLen )
mluis 1:91e4e6c60d1e 2405 {
mluis 1:91e4e6c60d1e 2406 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
mluis 1:91e4e6c60d1e 2407 // No payload for this answer
mluis 2:14a5d6ad92d5 2408 status = LORAMAC_STATUS_OK;
mluis 1:91e4e6c60d1e 2409 }
mluis 0:91d1a7783bb9 2410 break;
mluis 0:91d1a7783bb9 2411 case MOTE_MAC_RX_PARAM_SETUP_ANS:
mluis 7:c16969e0f70f 2412 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
mluis 1:91e4e6c60d1e 2413 {
mluis