Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Committer:
shaunkrnelson
Date:
Thu Nov 17 14:12:24 2016 +0000
Branch:
v4.2.0
Revision:
29:c7583fcfad8c
Merging branch with uplink double increment and AdrAckCounter fixes

Who changed what in which revision?

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