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