Forked LoRaWAN-lib for Legacy Gateway testing

Fork of LoRaWAN-lib by Miguel Luis

Committer:
mluis
Date:
Tue Apr 04 11:40:18 2017 +0000
Revision:
9:d2da17dee1e2
Parent:
8:2d237e069df2
Synchronized with https://github.com/Lora-net/LoRaMac-node git commit 8050de6f98b4634672d989a27dc2d163fae985dd

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