Forked LoRaWAN-lib for Legacy Gateway testing

Fork of LoRaWAN-lib by Miguel Luis

Committer:
mick_ccc
Date:
Thu Apr 20 14:26:48 2017 +0000
Revision:
10:e52525844705
Parent:
8:2d237e069df2
Hacked for Legacy Gateway testing

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