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