asif-test

Fork of LoRaWAN-lib by Semtech

Committer:
billvs
Date:
Wed Nov 01 20:44:06 2017 +0000
Revision:
8:8c9f22063c8d
Parent:
6:d7a34ded7c87
asif test

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