end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Committer:
dudmuck
Date:
Wed Jun 07 19:56:28 2017 +0000
Revision:
8:ab2f9a8d2eaa
Parent:
7:e238827f0e47
Child:
10:00997daeb0c0
prevent rx window from colliding with beacon reception

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:8f0d0ae0a077 1 /*
dudmuck 0:8f0d0ae0a077 2 / _____) _ | |
dudmuck 0:8f0d0ae0a077 3 ( (____ _____ ____ _| |_ _____ ____| |__
dudmuck 0:8f0d0ae0a077 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
dudmuck 0:8f0d0ae0a077 5 _____) ) ____| | | || |_| ____( (___| | | |
dudmuck 0:8f0d0ae0a077 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
dudmuck 0:8f0d0ae0a077 7 (C)2013 Semtech
dudmuck 0:8f0d0ae0a077 8 ___ _____ _ ___ _ _____ ___ ___ ___ ___
dudmuck 0:8f0d0ae0a077 9 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
dudmuck 0:8f0d0ae0a077 10 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
dudmuck 0:8f0d0ae0a077 11 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
dudmuck 0:8f0d0ae0a077 12 embedded.connectivity.solutions===============
dudmuck 0:8f0d0ae0a077 13
dudmuck 0:8f0d0ae0a077 14 Description: LoRa MAC layer implementation
dudmuck 0:8f0d0ae0a077 15
dudmuck 0:8f0d0ae0a077 16 License: Revised BSD License, see LICENSE.TXT file include in the project
dudmuck 0:8f0d0ae0a077 17
dudmuck 0:8f0d0ae0a077 18 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jäckle ( STACKFORCE )
dudmuck 0:8f0d0ae0a077 19 */
dudmuck 0:8f0d0ae0a077 20 #include <math.h>
dudmuck 0:8f0d0ae0a077 21 #include "board.h"
dudmuck 0:8f0d0ae0a077 22
dudmuck 0:8f0d0ae0a077 23 #include "LoRaMacCrypto.h"
dudmuck 0:8f0d0ae0a077 24 #include "LoRaMac.h"
dudmuck 0:8f0d0ae0a077 25 #include "LoRaMacTest.h"
dudmuck 0:8f0d0ae0a077 26
dudmuck 0:8f0d0ae0a077 27 #define PING_SLOT_RESOLUTION_us 30000
dudmuck 0:8f0d0ae0a077 28
dudmuck 0:8f0d0ae0a077 29 /*!
dudmuck 0:8f0d0ae0a077 30 * Maximum PHY layer payload size
dudmuck 0:8f0d0ae0a077 31 */
dudmuck 0:8f0d0ae0a077 32 #define LORAMAC_PHY_MAXPAYLOAD 255
dudmuck 0:8f0d0ae0a077 33
dudmuck 0:8f0d0ae0a077 34 /*!
dudmuck 0:8f0d0ae0a077 35 * Maximum MAC commands buffer size
dudmuck 0:8f0d0ae0a077 36 */
dudmuck 0:8f0d0ae0a077 37 #define LORA_MAC_COMMAND_MAX_LENGTH 15
dudmuck 0:8f0d0ae0a077 38
dudmuck 0:8f0d0ae0a077 39 /*!
dudmuck 0:8f0d0ae0a077 40 * FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
dudmuck 0:8f0d0ae0a077 41 * in RxWindowSetup function.
dudmuck 0:8f0d0ae0a077 42 * Maximum PHYPayload = MaxPayloadOfDatarate/MaxPayloadOfDatarateRepeater + LORA_MAC_FRMPAYLOAD_OVERHEAD
dudmuck 0:8f0d0ae0a077 43 */
dudmuck 0:8f0d0ae0a077 44 #define LORA_MAC_FRMPAYLOAD_OVERHEAD 13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)
dudmuck 0:8f0d0ae0a077 45
dudmuck 0:8f0d0ae0a077 46 /*!
dudmuck 0:8f0d0ae0a077 47 * LoRaMac duty cycle for the back-off procedure during the first hour.
dudmuck 0:8f0d0ae0a077 48 */
dudmuck 0:8f0d0ae0a077 49 #define BACKOFF_DC_1_HOUR 100
dudmuck 0:8f0d0ae0a077 50
dudmuck 0:8f0d0ae0a077 51 /*!
dudmuck 0:8f0d0ae0a077 52 * LoRaMac duty cycle for the back-off procedure during the next 10 hours.
dudmuck 0:8f0d0ae0a077 53 */
dudmuck 0:8f0d0ae0a077 54 #define BACKOFF_DC_10_HOURS 1000
dudmuck 0:8f0d0ae0a077 55
dudmuck 0:8f0d0ae0a077 56 /*!
dudmuck 0:8f0d0ae0a077 57 * LoRaMac duty cycle for the back-off procedure during the next 24 hours.
dudmuck 0:8f0d0ae0a077 58 */
dudmuck 0:8f0d0ae0a077 59 #define BACKOFF_DC_24_HOURS 10000
dudmuck 0:8f0d0ae0a077 60
dudmuck 0:8f0d0ae0a077 61 /*!
dudmuck 0:8f0d0ae0a077 62 * Device IEEE EUI
dudmuck 0:8f0d0ae0a077 63 */
dudmuck 0:8f0d0ae0a077 64 static uint8_t *LoRaMacDevEui;
dudmuck 0:8f0d0ae0a077 65
dudmuck 0:8f0d0ae0a077 66 /*!
dudmuck 0:8f0d0ae0a077 67 * Application IEEE EUI
dudmuck 0:8f0d0ae0a077 68 */
dudmuck 0:8f0d0ae0a077 69 static uint8_t *LoRaMacAppEui;
dudmuck 0:8f0d0ae0a077 70
dudmuck 0:8f0d0ae0a077 71 /*!
dudmuck 0:8f0d0ae0a077 72 * AES encryption/decryption cipher application key
dudmuck 0:8f0d0ae0a077 73 */
dudmuck 0:8f0d0ae0a077 74 static uint8_t *LoRaMacAppKey;
dudmuck 0:8f0d0ae0a077 75
dudmuck 0:8f0d0ae0a077 76 /*!
dudmuck 0:8f0d0ae0a077 77 * AES encryption/decryption cipher network session key
dudmuck 0:8f0d0ae0a077 78 */
dudmuck 0:8f0d0ae0a077 79 static uint8_t LoRaMacNwkSKey[] =
dudmuck 0:8f0d0ae0a077 80 {
dudmuck 0:8f0d0ae0a077 81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
dudmuck 0:8f0d0ae0a077 82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
dudmuck 0:8f0d0ae0a077 83 };
dudmuck 0:8f0d0ae0a077 84
dudmuck 0:8f0d0ae0a077 85 /*!
dudmuck 0:8f0d0ae0a077 86 * AES encryption/decryption cipher application session key
dudmuck 0:8f0d0ae0a077 87 */
dudmuck 0:8f0d0ae0a077 88 static uint8_t LoRaMacAppSKey[] =
dudmuck 0:8f0d0ae0a077 89 {
dudmuck 0:8f0d0ae0a077 90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
dudmuck 0:8f0d0ae0a077 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
dudmuck 0:8f0d0ae0a077 92 };
dudmuck 0:8f0d0ae0a077 93
dudmuck 0:8f0d0ae0a077 94 /*!
dudmuck 0:8f0d0ae0a077 95 * Device nonce is a random value extracted by issuing a sequence of RSSI
dudmuck 0:8f0d0ae0a077 96 * measurements
dudmuck 0:8f0d0ae0a077 97 */
dudmuck 0:8f0d0ae0a077 98 static uint16_t LoRaMacDevNonce;
dudmuck 0:8f0d0ae0a077 99
dudmuck 0:8f0d0ae0a077 100 /*!
dudmuck 0:8f0d0ae0a077 101 * Network ID ( 3 bytes )
dudmuck 0:8f0d0ae0a077 102 */
dudmuck 0:8f0d0ae0a077 103 static uint32_t LoRaMacNetID;
dudmuck 0:8f0d0ae0a077 104
dudmuck 0:8f0d0ae0a077 105 /*!
dudmuck 0:8f0d0ae0a077 106 * Mote Address
dudmuck 0:8f0d0ae0a077 107 */
dudmuck 0:8f0d0ae0a077 108 static uint32_t LoRaMacDevAddr;
dudmuck 0:8f0d0ae0a077 109
dudmuck 0:8f0d0ae0a077 110 /*!
dudmuck 0:8f0d0ae0a077 111 * Multicast channels linked list
dudmuck 0:8f0d0ae0a077 112 */
dudmuck 0:8f0d0ae0a077 113 static MulticastParams_t *MulticastChannels = NULL;
dudmuck 0:8f0d0ae0a077 114
dudmuck 0:8f0d0ae0a077 115 /*!
dudmuck 0:8f0d0ae0a077 116 * Actual device class
dudmuck 0:8f0d0ae0a077 117 */
dudmuck 0:8f0d0ae0a077 118 static DeviceClass_t LoRaMacDeviceClass;
dudmuck 0:8f0d0ae0a077 119
dudmuck 0:8f0d0ae0a077 120 /*!
dudmuck 0:8f0d0ae0a077 121 * Indicates if the node is connected to a private or public network
dudmuck 0:8f0d0ae0a077 122 */
dudmuck 0:8f0d0ae0a077 123 static bool PublicNetwork;
dudmuck 0:8f0d0ae0a077 124
dudmuck 0:8f0d0ae0a077 125
dudmuck 0:8f0d0ae0a077 126 /*!
dudmuck 0:8f0d0ae0a077 127 * Buffer containing the data to be sent or received.
dudmuck 0:8f0d0ae0a077 128 */
dudmuck 0:8f0d0ae0a077 129 static uint8_t LoRaMacBuffer[LORAMAC_PHY_MAXPAYLOAD];
dudmuck 0:8f0d0ae0a077 130
dudmuck 0:8f0d0ae0a077 131 /*!
dudmuck 0:8f0d0ae0a077 132 * Length of packet in LoRaMacBuffer
dudmuck 0:8f0d0ae0a077 133 */
dudmuck 0:8f0d0ae0a077 134 static uint16_t LoRaMacBufferPktLen = 0;
dudmuck 0:8f0d0ae0a077 135
dudmuck 0:8f0d0ae0a077 136 /*!
dudmuck 0:8f0d0ae0a077 137 * Length of the payload in LoRaMacBuffer
dudmuck 0:8f0d0ae0a077 138 */
dudmuck 0:8f0d0ae0a077 139 static uint8_t LoRaMacTxPayloadLen = 0;
dudmuck 0:8f0d0ae0a077 140
dudmuck 0:8f0d0ae0a077 141 /*!
dudmuck 0:8f0d0ae0a077 142 * Buffer containing the upper layer data.
dudmuck 0:8f0d0ae0a077 143 */
dudmuck 0:8f0d0ae0a077 144 static uint8_t LoRaMacRxPayload[LORAMAC_PHY_MAXPAYLOAD];
dudmuck 0:8f0d0ae0a077 145
dudmuck 0:8f0d0ae0a077 146 /*!
dudmuck 0:8f0d0ae0a077 147 * LoRaMAC frame counter. Each time a packet is sent the counter is incremented.
dudmuck 0:8f0d0ae0a077 148 * Only the 16 LSB bits are sent
dudmuck 0:8f0d0ae0a077 149 */
dudmuck 0:8f0d0ae0a077 150 static uint32_t UpLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 151
dudmuck 0:8f0d0ae0a077 152 /*!
dudmuck 0:8f0d0ae0a077 153 * LoRaMAC frame counter. Each time a packet is received the counter is incremented.
dudmuck 0:8f0d0ae0a077 154 * Only the 16 LSB bits are received
dudmuck 0:8f0d0ae0a077 155 */
dudmuck 0:8f0d0ae0a077 156 static uint32_t DownLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 157
dudmuck 0:8f0d0ae0a077 158 /*!
dudmuck 0:8f0d0ae0a077 159 * IsPacketCounterFixed enables the MIC field tests by fixing the
dudmuck 0:8f0d0ae0a077 160 * UpLinkCounter value
dudmuck 0:8f0d0ae0a077 161 */
dudmuck 0:8f0d0ae0a077 162 static bool IsUpLinkCounterFixed = false;
dudmuck 0:8f0d0ae0a077 163
dudmuck 0:8f0d0ae0a077 164 /*!
dudmuck 0:8f0d0ae0a077 165 * Used for test purposes. Disables the opening of the reception windows.
dudmuck 0:8f0d0ae0a077 166 */
dudmuck 0:8f0d0ae0a077 167 static bool IsRxWindowsEnabled = true;
dudmuck 0:8f0d0ae0a077 168 LowPowerTimeout rx_timeout;
dudmuck 0:8f0d0ae0a077 169
dudmuck 0:8f0d0ae0a077 170 /*!
dudmuck 0:8f0d0ae0a077 171 * Indicates if the MAC layer has already joined a network.
dudmuck 0:8f0d0ae0a077 172 */
dudmuck 0:8f0d0ae0a077 173 static bool IsLoRaMacNetworkJoined = false;
dudmuck 0:8f0d0ae0a077 174
dudmuck 0:8f0d0ae0a077 175 /*!
dudmuck 0:8f0d0ae0a077 176 * If the node has sent a FRAME_TYPE_DATA_CONFIRMED_UP this variable indicates
dudmuck 0:8f0d0ae0a077 177 * if the nodes needs to manage the server acknowledgement.
dudmuck 0:8f0d0ae0a077 178 */
dudmuck 0:8f0d0ae0a077 179 static bool NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 180
dudmuck 0:8f0d0ae0a077 181 /*!
dudmuck 0:8f0d0ae0a077 182 * If the server has sent a FRAME_TYPE_DATA_CONFIRMED_DOWN this variable indicates
dudmuck 0:8f0d0ae0a077 183 * if the ACK bit must be set for the next transmission
dudmuck 0:8f0d0ae0a077 184 */
dudmuck 0:8f0d0ae0a077 185 static bool SrvAckRequested = false;
dudmuck 0:8f0d0ae0a077 186
dudmuck 0:8f0d0ae0a077 187 /*!
dudmuck 0:8f0d0ae0a077 188 * Indicates if the MAC layer wants to send MAC commands
dudmuck 0:8f0d0ae0a077 189 */
dudmuck 0:8f0d0ae0a077 190 static bool MacCommandsInNextTx = false;
dudmuck 0:8f0d0ae0a077 191
dudmuck 0:8f0d0ae0a077 192 /*!
dudmuck 0:8f0d0ae0a077 193 * Contains the current MacCommandsBuffer index
dudmuck 0:8f0d0ae0a077 194 */
dudmuck 0:8f0d0ae0a077 195 static uint8_t MacCommandsBufferIndex = 0;
dudmuck 0:8f0d0ae0a077 196
dudmuck 0:8f0d0ae0a077 197 /*!
dudmuck 0:8f0d0ae0a077 198 * Contains the current MacCommandsBuffer index for MAC commands to repeat
dudmuck 0:8f0d0ae0a077 199 */
dudmuck 0:8f0d0ae0a077 200 static uint8_t MacCommandsBufferToRepeatIndex = 0;
dudmuck 0:8f0d0ae0a077 201
dudmuck 0:8f0d0ae0a077 202 /*!
dudmuck 0:8f0d0ae0a077 203 * Buffer containing the MAC layer commands
dudmuck 0:8f0d0ae0a077 204 */
dudmuck 0:8f0d0ae0a077 205 static uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH];
dudmuck 0:8f0d0ae0a077 206
dudmuck 0:8f0d0ae0a077 207 /*!
dudmuck 0:8f0d0ae0a077 208 * Buffer containing the MAC layer commands which must be repeated
dudmuck 0:8f0d0ae0a077 209 */
dudmuck 0:8f0d0ae0a077 210 static uint8_t MacCommandsBufferToRepeat[LORA_MAC_COMMAND_MAX_LENGTH];
dudmuck 0:8f0d0ae0a077 211
dudmuck 0:8f0d0ae0a077 212 #if defined( USE_BAND_915_SINGLE )
dudmuck 0:8f0d0ae0a077 213 /*!
dudmuck 0:8f0d0ae0a077 214 * Data rates table definition
dudmuck 0:8f0d0ae0a077 215 */
dudmuck 0:8f0d0ae0a077 216 const uint8_t Datarates[] = { 10, 9, 8, 7, 8, 0, 0, 0, 12, 11, 10, 9, 8, 7, 0, 0 };
dudmuck 0:8f0d0ae0a077 217
dudmuck 0:8f0d0ae0a077 218 /*!
dudmuck 0:8f0d0ae0a077 219 * Bandwidths table definition in Hz
dudmuck 0:8f0d0ae0a077 220 */
dudmuck 0:8f0d0ae0a077 221 const uint32_t Bandwidths[] = { 125000, 125000, 125000, 125000, 500000, 0, 0, 0, 500000, 500000, 500000, 500000, 500000, 500000, 0, 0 };
dudmuck 0:8f0d0ae0a077 222
dudmuck 0:8f0d0ae0a077 223 /*!
dudmuck 0:8f0d0ae0a077 224 * LoRaMac bands
dudmuck 0:8f0d0ae0a077 225 */
dudmuck 1:53c30224eda8 226 /*static Band_t Bands[LORA_MAX_NB_BANDS] =
dudmuck 0:8f0d0ae0a077 227 {
dudmuck 0:8f0d0ae0a077 228 BAND0,
dudmuck 1:53c30224eda8 229 };*/
dudmuck 0:8f0d0ae0a077 230
dudmuck 0:8f0d0ae0a077 231 /*!
dudmuck 0:8f0d0ae0a077 232 * LoRaMAC channels
dudmuck 0:8f0d0ae0a077 233 */
dudmuck 0:8f0d0ae0a077 234 static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS];
dudmuck 0:8f0d0ae0a077 235
dudmuck 0:8f0d0ae0a077 236 /*!
dudmuck 0:8f0d0ae0a077 237 * Tx output powers table definition
dudmuck 0:8f0d0ae0a077 238 */
dudmuck 0:8f0d0ae0a077 239 const int8_t TxPowers[] = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
dudmuck 0:8f0d0ae0a077 240 #define LORAMAC_FIRST_CHANNEL ( (uint32_t)910.0e6 )
dudmuck 0:8f0d0ae0a077 241 #define LORAMAC_STEPWIDTH_CHANNEL ( (uint32_t)800e3 )
dudmuck 0:8f0d0ae0a077 242
dudmuck 0:8f0d0ae0a077 243 #define BEACON_SIZE 6 /* bytes */
dudmuck 0:8f0d0ae0a077 244 #define BEACON_CHANNEL_BW 2 /* 2=500KHz */
dudmuck 0:8f0d0ae0a077 245 #define BEACON_CHANNEL_DR LORAMAC_DEFAULT_DATARATE
dudmuck 0:8f0d0ae0a077 246
dudmuck 0:8f0d0ae0a077 247 /* measured beacon duration (all at bw500)
dudmuck 0:8f0d0ae0a077 248 * latency assigned for correct rx-before-tx measurement */
dudmuck 0:8f0d0ae0a077 249 #if (LORAMAC_DEFAULT_DATARATE == DR_8)
dudmuck 0:8f0d0ae0a077 250 #define BEACON_RXDONE_LATENCY_us 6000
dudmuck 0:8f0d0ae0a077 251 #define BEACON_TOA_us 209000
dudmuck 0:8f0d0ae0a077 252 #elif (LORAMAC_DEFAULT_DATARATE == DR_9)
dudmuck 0:8f0d0ae0a077 253 #define BEACON_RXDONE_LATENCY_us 3500
dudmuck 0:8f0d0ae0a077 254 #define BEACON_TOA_us 105000
dudmuck 0:8f0d0ae0a077 255 #elif (LORAMAC_DEFAULT_DATARATE == DR_10)
dudmuck 0:8f0d0ae0a077 256 #define BEACON_RXDONE_LATENCY_us 1460
dudmuck 0:8f0d0ae0a077 257 #define BEACON_TOA_us 52800
dudmuck 0:8f0d0ae0a077 258 #elif (LORAMAC_DEFAULT_DATARATE == DR_11)
dudmuck 0:8f0d0ae0a077 259 #define BEACON_RXDONE_LATENCY_us 2000
dudmuck 0:8f0d0ae0a077 260 #define BEACON_TOA_us 26000
dudmuck 0:8f0d0ae0a077 261 #elif (LORAMAC_DEFAULT_DATARATE == DR_12)
dudmuck 0:8f0d0ae0a077 262 #define BEACON_RXDONE_LATENCY_us 1500
dudmuck 0:8f0d0ae0a077 263 #define BEACON_TOA_us 12800
dudmuck 0:8f0d0ae0a077 264 #elif (LORAMAC_DEFAULT_DATARATE == DR_13)
dudmuck 0:8f0d0ae0a077 265 #define BEACON_RXDONE_LATENCY_us 1300
dudmuck 0:8f0d0ae0a077 266 #define BEACON_TOA_us 6560
dudmuck 0:8f0d0ae0a077 267 #endif
dudmuck 0:8f0d0ae0a077 268
dudmuck 0:8f0d0ae0a077 269 #define BEACON_GUARD_us 2000000 // pre-beacon start
dudmuck 0:8f0d0ae0a077 270 #define BEACON_RESERVED_us 2120000 // post-beacon start
dudmuck 0:8f0d0ae0a077 271
dudmuck 0:8f0d0ae0a077 272 //#define BEACON_SYMBOL_TIMEOUT_UNLOCKED 100
dudmuck 0:8f0d0ae0a077 273
dudmuck 0:8f0d0ae0a077 274 #else
dudmuck 0:8f0d0ae0a077 275 #error "Please define a frequency band in the compiler options."
dudmuck 0:8f0d0ae0a077 276 #endif
dudmuck 0:8f0d0ae0a077 277
dudmuck 0:8f0d0ae0a077 278 #define BEACON_MIN_SYMBOL_TIMEOUT 8
dudmuck 0:8f0d0ae0a077 279
dudmuck 0:8f0d0ae0a077 280 LowPowerTimer lp_timer;
dudmuck 0:8f0d0ae0a077 281
dudmuck 0:8f0d0ae0a077 282 /*!
dudmuck 0:8f0d0ae0a077 283 * LoRaMac parameters
dudmuck 0:8f0d0ae0a077 284 */
dudmuck 0:8f0d0ae0a077 285 LoRaMacParams_t LoRaMacParams;
dudmuck 0:8f0d0ae0a077 286
dudmuck 0:8f0d0ae0a077 287 /*!
dudmuck 0:8f0d0ae0a077 288 * LoRaMac default parameters
dudmuck 0:8f0d0ae0a077 289 */
dudmuck 0:8f0d0ae0a077 290 LoRaMacParams_t LoRaMacParamsDefaults;
dudmuck 0:8f0d0ae0a077 291
dudmuck 0:8f0d0ae0a077 292 /*!
dudmuck 0:8f0d0ae0a077 293 * Uplink messages repetitions counter
dudmuck 0:8f0d0ae0a077 294 */
dudmuck 0:8f0d0ae0a077 295 static uint8_t ChannelsNbRepCounter = 0;
dudmuck 0:8f0d0ae0a077 296
dudmuck 0:8f0d0ae0a077 297 static uint32_t AggregatedLastTxDoneTime_us;
dudmuck 0:8f0d0ae0a077 298
dudmuck 0:8f0d0ae0a077 299 /*!
dudmuck 0:8f0d0ae0a077 300 * Current channel index
dudmuck 0:8f0d0ae0a077 301 */
dudmuck 0:8f0d0ae0a077 302 static uint8_t Channel;
dudmuck 0:8f0d0ae0a077 303
dudmuck 0:8f0d0ae0a077 304 /*!
dudmuck 0:8f0d0ae0a077 305 * Stores the time at LoRaMac initialization.
dudmuck 0:8f0d0ae0a077 306 *
dudmuck 0:8f0d0ae0a077 307 * \remark Used for the BACKOFF_DC computation.
dudmuck 0:8f0d0ae0a077 308 */
dudmuck 0:8f0d0ae0a077 309 //static TimerTime_t LoRaMacInitializationTime = 0;
dudmuck 0:8f0d0ae0a077 310
dudmuck 0:8f0d0ae0a077 311 /*!
dudmuck 0:8f0d0ae0a077 312 * LoRaMac internal states
dudmuck 0:8f0d0ae0a077 313 */
dudmuck 0:8f0d0ae0a077 314 enum eLoRaMacState
dudmuck 0:8f0d0ae0a077 315 {
dudmuck 0:8f0d0ae0a077 316 LORAMAC_IDLE = 0x00000000,
dudmuck 0:8f0d0ae0a077 317 LORAMAC_TX_RUNNING = 0x00000001,
dudmuck 0:8f0d0ae0a077 318 LORAMAC_RX = 0x00000002,
dudmuck 0:8f0d0ae0a077 319 LORAMAC_ACK_REQ = 0x00000004,
dudmuck 0:8f0d0ae0a077 320 LORAMAC_ACK_RETRY = 0x00000008,
dudmuck 0:8f0d0ae0a077 321 LORAMAC_TX_DELAYED = 0x00000010,
dudmuck 0:8f0d0ae0a077 322 LORAMAC_TX_CONFIG = 0x00000020,
dudmuck 0:8f0d0ae0a077 323 LORAMAC_RX_ABORT = 0x00000040,
dudmuck 0:8f0d0ae0a077 324 LORAMAC_TX_SCHED = 0x00000080,
dudmuck 0:8f0d0ae0a077 325 };
dudmuck 0:8f0d0ae0a077 326
dudmuck 0:8f0d0ae0a077 327 /*!
dudmuck 0:8f0d0ae0a077 328 * LoRaMac internal state
dudmuck 0:8f0d0ae0a077 329 */
dudmuck 0:8f0d0ae0a077 330 uint32_t LoRaMacState = LORAMAC_IDLE;
dudmuck 0:8f0d0ae0a077 331
dudmuck 0:8f0d0ae0a077 332 /*!
dudmuck 0:8f0d0ae0a077 333 * LoRaMac timer used to check the LoRaMacState (runs every second)
dudmuck 0:8f0d0ae0a077 334 */
dudmuck 0:8f0d0ae0a077 335 static LowPowerTimeout MacStateCheckTimer;
dudmuck 0:8f0d0ae0a077 336
dudmuck 0:8f0d0ae0a077 337 /*!
dudmuck 0:8f0d0ae0a077 338 * LoRaMac upper layer event functions
dudmuck 0:8f0d0ae0a077 339 */
dudmuck 0:8f0d0ae0a077 340 static LoRaMacPrimitives_t *LoRaMacPrimitives;
dudmuck 0:8f0d0ae0a077 341
dudmuck 0:8f0d0ae0a077 342 /*!
dudmuck 0:8f0d0ae0a077 343 * LoRaMac upper layer callback functions
dudmuck 0:8f0d0ae0a077 344 */
dudmuck 0:8f0d0ae0a077 345 static LoRaMacCallback_t *LoRaMacCallbacks;
dudmuck 0:8f0d0ae0a077 346
dudmuck 0:8f0d0ae0a077 347 /*!
dudmuck 0:8f0d0ae0a077 348 * Radio events function pointer
dudmuck 0:8f0d0ae0a077 349 */
dudmuck 0:8f0d0ae0a077 350 static RadioEvents_t RadioEvents;
dudmuck 0:8f0d0ae0a077 351
dudmuck 0:8f0d0ae0a077 352 /*!
dudmuck 0:8f0d0ae0a077 353 * LoRaMac duty cycle delayed Tx timer
dudmuck 0:8f0d0ae0a077 354 */
dudmuck 0:8f0d0ae0a077 355 static LowPowerTimeout TxDelayedTimer;
dudmuck 0:8f0d0ae0a077 356 LowPowerTimeout tx_timeout;
dudmuck 0:8f0d0ae0a077 357
dudmuck 0:8f0d0ae0a077 358
dudmuck 0:8f0d0ae0a077 359 /*!
dudmuck 0:8f0d0ae0a077 360 * LoRaMac reception windows delay
dudmuck 0:8f0d0ae0a077 361 * \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
dudmuck 0:8f0d0ae0a077 362 * join frame : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
dudmuck 0:8f0d0ae0a077 363 */
dudmuck 0:8f0d0ae0a077 364 static uint32_t RxWindowDelay_us;
dudmuck 0:8f0d0ae0a077 365
dudmuck 0:8f0d0ae0a077 366 typedef enum {
dudmuck 0:8f0d0ae0a077 367 BEACON_STATE_NONE = 0,
dudmuck 0:8f0d0ae0a077 368 BEACON_STATE_FIRST_ACQ,
dudmuck 0:8f0d0ae0a077 369 BEACON_STATE_ACQ_ERROR,
dudmuck 0:8f0d0ae0a077 370 BEACON_STATE_LOCKED_,
dudmuck 0:8f0d0ae0a077 371 } beacon_state_e;
dudmuck 0:8f0d0ae0a077 372
dudmuck 0:8f0d0ae0a077 373 struct beacon_struct {
dudmuck 0:8f0d0ae0a077 374 int rx_precession_us; // positive: rxing before tx start, negative: rxing after tx start
dudmuck 0:8f0d0ae0a077 375 unsigned int RxBeaconSetupAt_us;
dudmuck 0:8f0d0ae0a077 376 unsigned int LastBeaconRx_us; // updated only at beacon reception
dudmuck 0:8f0d0ae0a077 377 unsigned int LastBeaconStart_us; // updated at beacon reception and beacon reception timeout
dudmuck 0:8f0d0ae0a077 378 unsigned int next_beacon_expected_us;
dudmuck 0:8f0d0ae0a077 379 int BeaconRxTimerError_us;
dudmuck 0:8f0d0ae0a077 380
dudmuck 0:8f0d0ae0a077 381 float symbol_period_secs;
dudmuck 0:8f0d0ae0a077 382
dudmuck 0:8f0d0ae0a077 383 uint8_t Precess_symbols; // how many symbols we want to start receiver before expected transmitter
dudmuck 0:8f0d0ae0a077 384 uint8_t SymbolTimeout;
dudmuck 0:8f0d0ae0a077 385 float SymbolTimeout_sec;
dudmuck 0:8f0d0ae0a077 386 uint8_t num_missed;
dudmuck 0:8f0d0ae0a077 387
dudmuck 0:8f0d0ae0a077 388 beacon_state_e state;
dudmuck 0:8f0d0ae0a077 389
dudmuck 0:8f0d0ae0a077 390 uint16_t tx_slot_offset;
dudmuck 0:8f0d0ae0a077 391 uint16_t periodicity_slots;
dudmuck 0:8f0d0ae0a077 392
dudmuck 0:8f0d0ae0a077 393 LowPowerTimeout timeout;
dudmuck 0:8f0d0ae0a077 394 bool have_beacon;
dudmuck 0:8f0d0ae0a077 395 } BeaconCtx;
dudmuck 0:8f0d0ae0a077 396
dudmuck 0:8f0d0ae0a077 397 /*!
dudmuck 0:8f0d0ae0a077 398 * Rx window parameters
dudmuck 0:8f0d0ae0a077 399 */
dudmuck 0:8f0d0ae0a077 400 typedef struct
dudmuck 0:8f0d0ae0a077 401 {
dudmuck 0:8f0d0ae0a077 402 int8_t Datarate;
dudmuck 0:8f0d0ae0a077 403 uint8_t Bandwidth;
dudmuck 0:8f0d0ae0a077 404 uint32_t RxWindowTimeout;
dudmuck 0:8f0d0ae0a077 405 int32_t RxOffset;
dudmuck 0:8f0d0ae0a077 406 }RxConfigParams_t;
dudmuck 0:8f0d0ae0a077 407
dudmuck 0:8f0d0ae0a077 408 /*!
dudmuck 0:8f0d0ae0a077 409 * Rx windows params
dudmuck 0:8f0d0ae0a077 410 */
dudmuck 0:8f0d0ae0a077 411 static RxConfigParams_t RxWindowsParam;
dudmuck 0:8f0d0ae0a077 412
dudmuck 0:8f0d0ae0a077 413 static bool expecting_beacon;
dudmuck 0:8f0d0ae0a077 414 /*!
dudmuck 0:8f0d0ae0a077 415 * Acknowledge timeout timer. Used for packet retransmissions.
dudmuck 0:8f0d0ae0a077 416 */
dudmuck 0:8f0d0ae0a077 417 static LowPowerTimeout AckTimeoutTimer;
dudmuck 0:8f0d0ae0a077 418
dudmuck 0:8f0d0ae0a077 419 uint32_t TxTimeOnAir = 0;
dudmuck 0:8f0d0ae0a077 420
dudmuck 0:8f0d0ae0a077 421 /*!
dudmuck 0:8f0d0ae0a077 422 * Number of trials for the Join Request
dudmuck 0:8f0d0ae0a077 423 */
dudmuck 0:8f0d0ae0a077 424 static uint8_t JoinRequestTrials;
dudmuck 0:8f0d0ae0a077 425
dudmuck 0:8f0d0ae0a077 426 /*!
dudmuck 0:8f0d0ae0a077 427 * Maximum number of trials for the Join Request
dudmuck 0:8f0d0ae0a077 428 */
dudmuck 0:8f0d0ae0a077 429 static uint8_t MaxJoinRequestTrials;
dudmuck 0:8f0d0ae0a077 430
dudmuck 0:8f0d0ae0a077 431 /*!
dudmuck 0:8f0d0ae0a077 432 * Structure to hold an MCPS indication data.
dudmuck 0:8f0d0ae0a077 433 */
dudmuck 0:8f0d0ae0a077 434 static McpsIndication_t McpsIndication;
dudmuck 0:8f0d0ae0a077 435
dudmuck 0:8f0d0ae0a077 436 /*!
dudmuck 0:8f0d0ae0a077 437 * Structure to hold MCPS confirm data.
dudmuck 0:8f0d0ae0a077 438 */
dudmuck 0:8f0d0ae0a077 439 static McpsConfirm_t McpsConfirm;
dudmuck 0:8f0d0ae0a077 440
dudmuck 0:8f0d0ae0a077 441 /*!
dudmuck 0:8f0d0ae0a077 442 * Structure to hold MLME confirm data.
dudmuck 0:8f0d0ae0a077 443 */
dudmuck 0:8f0d0ae0a077 444 static MlmeConfirm_t MlmeConfirm;
dudmuck 0:8f0d0ae0a077 445
dudmuck 0:8f0d0ae0a077 446 /*!
dudmuck 0:8f0d0ae0a077 447 * Structure to hold MLME indication data.
dudmuck 0:8f0d0ae0a077 448 */
dudmuck 0:8f0d0ae0a077 449 static MlmeIndication_t MlmeIndication;
dudmuck 0:8f0d0ae0a077 450
dudmuck 0:8f0d0ae0a077 451 /*!
dudmuck 0:8f0d0ae0a077 452 * LoRaMac tx/rx operation state
dudmuck 0:8f0d0ae0a077 453 */
dudmuck 0:8f0d0ae0a077 454 LoRaMacFlags_t LoRaMacFlags;
dudmuck 0:8f0d0ae0a077 455
dudmuck 0:8f0d0ae0a077 456 /*!
dudmuck 0:8f0d0ae0a077 457 * \brief Function to be executed on Radio Tx Done event
dudmuck 0:8f0d0ae0a077 458 */
dudmuck 0:8f0d0ae0a077 459 static void OnRadioTxDone( unsigned int tx_done_us );
dudmuck 0:8f0d0ae0a077 460 unsigned int TxDone_us;
dudmuck 0:8f0d0ae0a077 461
dudmuck 0:8f0d0ae0a077 462 /*!
dudmuck 0:8f0d0ae0a077 463 * \brief This function prepares the MAC to abort the execution of function
dudmuck 0:8f0d0ae0a077 464 * OnRadioRxDone in case of a reception error.
dudmuck 0:8f0d0ae0a077 465 */
dudmuck 0:8f0d0ae0a077 466 static void PrepareRxDoneAbort( void );
dudmuck 0:8f0d0ae0a077 467
dudmuck 0:8f0d0ae0a077 468 /*!
dudmuck 0:8f0d0ae0a077 469 * \brief Function to be executed on Radio Rx Done event
dudmuck 0:8f0d0ae0a077 470 */
dudmuck 0:8f0d0ae0a077 471 static void OnRadioRxDone(unsigned rx_us, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
dudmuck 0:8f0d0ae0a077 472
dudmuck 0:8f0d0ae0a077 473 /*!
dudmuck 0:8f0d0ae0a077 474 * \brief Function executed on Radio Tx Timeout event
dudmuck 0:8f0d0ae0a077 475 */
dudmuck 0:8f0d0ae0a077 476 static void OnRadioTxTimeout( void );
dudmuck 0:8f0d0ae0a077 477
dudmuck 0:8f0d0ae0a077 478 /*!
dudmuck 0:8f0d0ae0a077 479 * \brief Function executed on Radio Rx error event
dudmuck 0:8f0d0ae0a077 480 */
dudmuck 0:8f0d0ae0a077 481 static void OnRadioRxError( void );
dudmuck 0:8f0d0ae0a077 482
dudmuck 0:8f0d0ae0a077 483 /*!
dudmuck 0:8f0d0ae0a077 484 * \brief Function executed on Radio Rx Timeout event
dudmuck 0:8f0d0ae0a077 485 */
dudmuck 0:8f0d0ae0a077 486 static void OnRadioRxTimeout( void );
dudmuck 0:8f0d0ae0a077 487
dudmuck 0:8f0d0ae0a077 488 /*!
dudmuck 0:8f0d0ae0a077 489 * \brief Function executed on Resend Frame timer event.
dudmuck 0:8f0d0ae0a077 490 */
dudmuck 0:8f0d0ae0a077 491 static void OnMacStateCheckTimerEvent( void );
dudmuck 0:8f0d0ae0a077 492
dudmuck 0:8f0d0ae0a077 493 /*!
dudmuck 0:8f0d0ae0a077 494 * \brief Function executed on duty cycle delayed Tx timer event
dudmuck 0:8f0d0ae0a077 495 */
dudmuck 0:8f0d0ae0a077 496 static void OnTxDelayedTimerEvent( void );
dudmuck 0:8f0d0ae0a077 497
dudmuck 0:8f0d0ae0a077 498 static void OnRxWindowTimerEvent( void );
dudmuck 0:8f0d0ae0a077 499
dudmuck 0:8f0d0ae0a077 500 /*!
dudmuck 0:8f0d0ae0a077 501 * \brief Function executed on AckTimeout timer event
dudmuck 0:8f0d0ae0a077 502 */
dudmuck 0:8f0d0ae0a077 503 static void OnAckTimeoutTimerEvent( void );
dudmuck 0:8f0d0ae0a077 504
dudmuck 0:8f0d0ae0a077 505
dudmuck 0:8f0d0ae0a077 506 /*!
dudmuck 0:8f0d0ae0a077 507 * \brief Initializes and opens the reception window
dudmuck 0:8f0d0ae0a077 508 *
dudmuck 0:8f0d0ae0a077 509 * \param [IN] freq window channel frequency
dudmuck 0:8f0d0ae0a077 510 * \param [IN] datarate window channel datarate
dudmuck 0:8f0d0ae0a077 511 * \param [IN] bandwidth window channel bandwidth
dudmuck 0:8f0d0ae0a077 512 * \param [IN] timeout window channel timeout
dudmuck 0:8f0d0ae0a077 513 *
dudmuck 0:8f0d0ae0a077 514 * \retval status Operation status [true: Success, false: Fail]
dudmuck 0:8f0d0ae0a077 515 */
dudmuck 0:8f0d0ae0a077 516 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous );
dudmuck 0:8f0d0ae0a077 517
dudmuck 0:8f0d0ae0a077 518 /*!
dudmuck 0:8f0d0ae0a077 519 * \brief Verifies if the RX window 2 frequency is in range
dudmuck 0:8f0d0ae0a077 520 *
dudmuck 0:8f0d0ae0a077 521 * \param [IN] freq window channel frequency
dudmuck 0:8f0d0ae0a077 522 *
dudmuck 0:8f0d0ae0a077 523 * \retval status Function status [1: OK, 0: Frequency not applicable]
dudmuck 0:8f0d0ae0a077 524 */
dudmuck 0:8f0d0ae0a077 525 //static bool Rx2FreqInRange( uint32_t freq );
dudmuck 0:8f0d0ae0a077 526
dudmuck 0:8f0d0ae0a077 527 /*!
dudmuck 0:8f0d0ae0a077 528 * \brief Adds a new MAC command to be sent.
dudmuck 0:8f0d0ae0a077 529 *
dudmuck 0:8f0d0ae0a077 530 * \Remark MAC layer internal function
dudmuck 0:8f0d0ae0a077 531 *
dudmuck 0:8f0d0ae0a077 532 * \param [in] cmd MAC command to be added
dudmuck 0:8f0d0ae0a077 533 * [MOTE_MAC_LINK_CHECK_REQ,
dudmuck 0:8f0d0ae0a077 534 * MOTE_MAC_LINK_ADR_ANS,
dudmuck 0:8f0d0ae0a077 535 * MOTE_MAC_DUTY_CYCLE_ANS,
dudmuck 0:8f0d0ae0a077 536 * MOTE_MAC_RX2_PARAM_SET_ANS,
dudmuck 0:8f0d0ae0a077 537 * MOTE_MAC_DEV_STATUS_ANS
dudmuck 0:8f0d0ae0a077 538 * MOTE_MAC_NEW_CHANNEL_ANS]
dudmuck 0:8f0d0ae0a077 539 * \param [in] p1 1st parameter ( optional depends on the command )
dudmuck 0:8f0d0ae0a077 540 * \param [in] p2 2nd parameter ( optional depends on the command )
dudmuck 0:8f0d0ae0a077 541 *
dudmuck 0:8f0d0ae0a077 542 * \retval status Function status [0: OK, 1: Unknown command, 2: Buffer full]
dudmuck 0:8f0d0ae0a077 543 */
dudmuck 0:8f0d0ae0a077 544 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
dudmuck 0:8f0d0ae0a077 545
dudmuck 0:8f0d0ae0a077 546 /*!
dudmuck 0:8f0d0ae0a077 547 * \brief Parses the MAC commands which must be repeated.
dudmuck 0:8f0d0ae0a077 548 *
dudmuck 0:8f0d0ae0a077 549 * \Remark MAC layer internal function
dudmuck 0:8f0d0ae0a077 550 *
dudmuck 0:8f0d0ae0a077 551 * \param [IN] cmdBufIn Buffer which stores the MAC commands to send
dudmuck 0:8f0d0ae0a077 552 * \param [IN] length Length of the input buffer to parse
dudmuck 0:8f0d0ae0a077 553 * \param [OUT] cmdBufOut Buffer which stores the MAC commands which must be
dudmuck 0:8f0d0ae0a077 554 * repeated.
dudmuck 0:8f0d0ae0a077 555 *
dudmuck 0:8f0d0ae0a077 556 * \retval Size of the MAC commands to repeat.
dudmuck 0:8f0d0ae0a077 557 */
dudmuck 0:8f0d0ae0a077 558 static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut );
dudmuck 0:8f0d0ae0a077 559
dudmuck 0:8f0d0ae0a077 560 /*!
dudmuck 0:8f0d0ae0a077 561 * \brief Verifies, if a value is in a given range.
dudmuck 0:8f0d0ae0a077 562 *
dudmuck 0:8f0d0ae0a077 563 * \param value Value to verify, if it is in range
dudmuck 0:8f0d0ae0a077 564 *
dudmuck 0:8f0d0ae0a077 565 * \param min Minimum possible value
dudmuck 0:8f0d0ae0a077 566 *
dudmuck 0:8f0d0ae0a077 567 * \param max Maximum possible value
dudmuck 0:8f0d0ae0a077 568 *
dudmuck 0:8f0d0ae0a077 569 * \retval Returns the maximum valid tx power
dudmuck 0:8f0d0ae0a077 570 */
dudmuck 0:8f0d0ae0a077 571 static bool ValueInRange( int8_t value, int8_t min, int8_t max );
dudmuck 0:8f0d0ae0a077 572
dudmuck 0:8f0d0ae0a077 573
dudmuck 0:8f0d0ae0a077 574 /*!
dudmuck 0:8f0d0ae0a077 575 * \brief Decodes MAC commands in the fOpts field and in the payload
dudmuck 0:8f0d0ae0a077 576 */
dudmuck 0:8f0d0ae0a077 577 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr );
dudmuck 0:8f0d0ae0a077 578
dudmuck 0:8f0d0ae0a077 579 /*!
dudmuck 0:8f0d0ae0a077 580 * \brief LoRaMAC layer generic send frame
dudmuck 0:8f0d0ae0a077 581 *
dudmuck 0:8f0d0ae0a077 582 * \param [IN] macHdr MAC header field
dudmuck 0:8f0d0ae0a077 583 * \param [IN] fPort MAC payload port
dudmuck 0:8f0d0ae0a077 584 * \param [IN] fBuffer MAC data buffer to be sent
dudmuck 0:8f0d0ae0a077 585 * \param [IN] fBufferSize MAC data buffer size
dudmuck 0:8f0d0ae0a077 586 * \retval status Status of the operation.
dudmuck 0:8f0d0ae0a077 587 */
dudmuck 0:8f0d0ae0a077 588 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
dudmuck 0:8f0d0ae0a077 589
dudmuck 0:8f0d0ae0a077 590 /*!
dudmuck 0:8f0d0ae0a077 591 * \brief LoRaMAC layer frame buffer initialization
dudmuck 0:8f0d0ae0a077 592 *
dudmuck 0:8f0d0ae0a077 593 * \param [IN] macHdr MAC header field
dudmuck 0:8f0d0ae0a077 594 * \param [IN] fCtrl MAC frame control field
dudmuck 0:8f0d0ae0a077 595 * \param [IN] fOpts MAC commands buffer
dudmuck 0:8f0d0ae0a077 596 * \param [IN] fPort MAC payload port
dudmuck 0:8f0d0ae0a077 597 * \param [IN] fBuffer MAC data buffer to be sent
dudmuck 0:8f0d0ae0a077 598 * \param [IN] fBufferSize MAC data buffer size
dudmuck 0:8f0d0ae0a077 599 * \retval status Status of the operation.
dudmuck 0:8f0d0ae0a077 600 */
dudmuck 0:8f0d0ae0a077 601 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize );
dudmuck 0:8f0d0ae0a077 602
dudmuck 0:8f0d0ae0a077 603 /*
dudmuck 0:8f0d0ae0a077 604 * \brief Schedules the frame according to the duty cycle
dudmuck 0:8f0d0ae0a077 605 *
dudmuck 0:8f0d0ae0a077 606 * \retval Status of the operation
dudmuck 0:8f0d0ae0a077 607 */
dudmuck 0:8f0d0ae0a077 608 static LoRaMacStatus_t ScheduleTx( void );
dudmuck 0:8f0d0ae0a077 609
dudmuck 0:8f0d0ae0a077 610
dudmuck 0:8f0d0ae0a077 611 /*!
dudmuck 0:8f0d0ae0a077 612 * \brief LoRaMAC layer prepared frame buffer transmission with channel specification
dudmuck 0:8f0d0ae0a077 613 *
dudmuck 0:8f0d0ae0a077 614 * \remark PrepareFrame must be called at least once before calling this
dudmuck 0:8f0d0ae0a077 615 * function.
dudmuck 0:8f0d0ae0a077 616 *
dudmuck 0:8f0d0ae0a077 617 * \param [IN] channel Channel parameters
dudmuck 0:8f0d0ae0a077 618 * \retval status Status of the operation.
dudmuck 0:8f0d0ae0a077 619 */
dudmuck 0:8f0d0ae0a077 620 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel );
dudmuck 0:8f0d0ae0a077 621
dudmuck 0:8f0d0ae0a077 622 /*!
dudmuck 0:8f0d0ae0a077 623 * \brief Sets the radio in continuous transmission mode
dudmuck 0:8f0d0ae0a077 624 *
dudmuck 0:8f0d0ae0a077 625 * \remark Uses the radio parameters set on the previous transmission.
dudmuck 0:8f0d0ae0a077 626 *
dudmuck 0:8f0d0ae0a077 627 * \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
dudmuck 0:8f0d0ae0a077 628 * \retval status Status of the operation.
dudmuck 0:8f0d0ae0a077 629 */
dudmuck 0:8f0d0ae0a077 630 LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout );
dudmuck 0:8f0d0ae0a077 631
dudmuck 0:8f0d0ae0a077 632 /*!
dudmuck 0:8f0d0ae0a077 633 * \brief Sets the radio in continuous transmission mode
dudmuck 0:8f0d0ae0a077 634 *
dudmuck 0:8f0d0ae0a077 635 * \remark Uses the radio parameters set on the previous transmission.
dudmuck 0:8f0d0ae0a077 636 *
dudmuck 0:8f0d0ae0a077 637 * \param [IN] timeout Time in seconds while the radio is kept in continuous wave mode
dudmuck 0:8f0d0ae0a077 638 * \param [IN] frequency RF frequency to be set.
dudmuck 0:8f0d0ae0a077 639 * \param [IN] power RF ouptput power to be set.
dudmuck 0:8f0d0ae0a077 640 * \retval status Status of the operation.
dudmuck 0:8f0d0ae0a077 641 */
dudmuck 0:8f0d0ae0a077 642 LoRaMacStatus_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power );
dudmuck 0:8f0d0ae0a077 643
dudmuck 0:8f0d0ae0a077 644 /*!
dudmuck 0:8f0d0ae0a077 645 * \brief Resets MAC specific parameters to default
dudmuck 0:8f0d0ae0a077 646 */
dudmuck 0:8f0d0ae0a077 647 static void ResetMacParameters( void );
dudmuck 0:8f0d0ae0a077 648
dudmuck 0:8f0d0ae0a077 649 void
dudmuck 0:8f0d0ae0a077 650 loramac_print_status()
dudmuck 0:8f0d0ae0a077 651 {
dudmuck 7:e238827f0e47 652 isr_printf("LoRaMacState:%lx DR%u=sf%u\r\n", LoRaMacState, LoRaMacParams.ChannelsDatarate_fixed, Datarates[LoRaMacParams.ChannelsDatarate_fixed]);
dudmuck 0:8f0d0ae0a077 653 }
dudmuck 0:8f0d0ae0a077 654
dudmuck 0:8f0d0ae0a077 655 /*
dudmuck 0:8f0d0ae0a077 656 * Rx window precise timing
dudmuck 0:8f0d0ae0a077 657 *
dudmuck 0:8f0d0ae0a077 658 * For more details please consult the following document, chapter 3.1.2.
dudmuck 0:8f0d0ae0a077 659 * http://www.semtech.com/images/datasheet/SX1272_settings_for_LoRaWAN_v2.0.pdf
dudmuck 0:8f0d0ae0a077 660 * or
dudmuck 0:8f0d0ae0a077 661 * http://www.semtech.com/images/datasheet/SX1276_settings_for_LoRaWAN_v2.0.pdf
dudmuck 0:8f0d0ae0a077 662 *
dudmuck 0:8f0d0ae0a077 663 * Downlink start: T = Tx + 1s (+/- 20 us)
dudmuck 0:8f0d0ae0a077 664 * |
dudmuck 0:8f0d0ae0a077 665 * TRxEarly | TRxLate
dudmuck 0:8f0d0ae0a077 666 * | | |
dudmuck 0:8f0d0ae0a077 667 * | | +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 668 * | | | Latest Rx window |
dudmuck 0:8f0d0ae0a077 669 * | | +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 670 * | | |
dudmuck 0:8f0d0ae0a077 671 * +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 672 * | Earliest Rx window |
dudmuck 0:8f0d0ae0a077 673 * +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 674 * |
dudmuck 0:8f0d0ae0a077 675 * +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 676 *Downlink preamble 8 symbols | | | | | | | | |
dudmuck 0:8f0d0ae0a077 677 * +---+---+---+---+---+---+---+---+
dudmuck 0:8f0d0ae0a077 678 *
dudmuck 0:8f0d0ae0a077 679 * Worst case Rx window timings
dudmuck 0:8f0d0ae0a077 680 *
dudmuck 0:8f0d0ae0a077 681 * TRxLate = DEFAULT_MIN_RX_SYMBOLS * tSymbol - RADIO_WAKEUP_TIME
dudmuck 0:8f0d0ae0a077 682 * TRxEarly = 8 - DEFAULT_MIN_RX_SYMBOLS * tSymbol - RxWindowTimeout - RADIO_WAKEUP_TIME
dudmuck 0:8f0d0ae0a077 683 *
dudmuck 0:8f0d0ae0a077 684 * TRxLate - TRxEarly = 2 * DEFAULT_SYSTEM_MAX_RX_ERROR
dudmuck 0:8f0d0ae0a077 685 *
dudmuck 0:8f0d0ae0a077 686 * RxOffset = ( TRxLate + TRxEarly ) / 2
dudmuck 0:8f0d0ae0a077 687 *
dudmuck 0:8f0d0ae0a077 688 * RxWindowTimeout = ( 2 * DEFAULT_MIN_RX_SYMBOLS - 8 ) * tSymbol + 2 * DEFAULT_SYSTEM_MAX_RX_ERROR
dudmuck 0:8f0d0ae0a077 689 * RxOffset = 4 * tSymbol - RxWindowTimeout / 2 - RADIO_WAKE_UP_TIME
dudmuck 0:8f0d0ae0a077 690 *
dudmuck 0:8f0d0ae0a077 691 * Minimal value of RxWindowTimeout must be 5 symbols which implies that the system always tolerates at least an error of 1.5 * tSymbol
dudmuck 0:8f0d0ae0a077 692 */
dudmuck 0:8f0d0ae0a077 693 /*!
dudmuck 0:8f0d0ae0a077 694 * Computes the Rx window parameters.
dudmuck 0:8f0d0ae0a077 695 *
dudmuck 0:8f0d0ae0a077 696 * \param [IN] datarate Rx window datarate to be used
dudmuck 0:8f0d0ae0a077 697 * \param [IN] rxError Maximum timing error of the receiver. in milliseconds
dudmuck 0:8f0d0ae0a077 698 * The receiver will turn on in a [-rxError : +rxError] ms
dudmuck 0:8f0d0ae0a077 699 * interval around RxOffset
dudmuck 0:8f0d0ae0a077 700 *
dudmuck 0:8f0d0ae0a077 701 * \retval rxConfigParams Returns a RxConfigParams_t structure.
dudmuck 0:8f0d0ae0a077 702 */
dudmuck 0:8f0d0ae0a077 703 static RxConfigParams_t ComputeRxWindowParameters( int8_t datarate, uint32_t rxError );
dudmuck 0:8f0d0ae0a077 704
dudmuck 0:8f0d0ae0a077 705 static void OnRadioTxDone( unsigned int tx_done_us )
dudmuck 0:8f0d0ae0a077 706 {
dudmuck 0:8f0d0ae0a077 707 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 708 TxDone_us = tx_done_us;
dudmuck 0:8f0d0ae0a077 709
dudmuck 0:8f0d0ae0a077 710 // Setup timers
dudmuck 0:8f0d0ae0a077 711 if( IsRxWindowsEnabled == true )
dudmuck 0:8f0d0ae0a077 712 {
dudmuck 8:ab2f9a8d2eaa 713 int us_to_beacon, us_since_beacon_start = lp_timer.read_us() - BeaconCtx.LastBeaconStart_us;
dudmuck 8:ab2f9a8d2eaa 714 us_to_beacon = BEACON_INTERVAL_us - us_since_beacon_start;
dudmuck 8:ab2f9a8d2eaa 715 if (us_to_beacon < 500000) {
dudmuck 8:ab2f9a8d2eaa 716 /* would step on beacon reception */
dudmuck 8:ab2f9a8d2eaa 717 isr_printf("<rxwinT %d>\r\n", us_to_beacon);
dudmuck 8:ab2f9a8d2eaa 718 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
dudmuck 8:ab2f9a8d2eaa 719 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
dudmuck 8:ab2f9a8d2eaa 720 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 8:ab2f9a8d2eaa 721 return;
dudmuck 8:ab2f9a8d2eaa 722 }
dudmuck 0:8f0d0ae0a077 723 rx_timeout.attach_us(&OnRxWindowTimerEvent, RxWindowDelay_us);
dudmuck 0:8f0d0ae0a077 724 if( NodeAckRequested == true )
dudmuck 0:8f0d0ae0a077 725 {
dudmuck 0:8f0d0ae0a077 726 AckTimeoutTimer.attach_us(&OnAckTimeoutTimerEvent, (RxWindowDelay_us/1000) + ACK_TIMEOUT_us + randr(-ACK_TIMEOUT_RND_us, ACK_TIMEOUT_RND_us));
dudmuck 0:8f0d0ae0a077 727 }
dudmuck 0:8f0d0ae0a077 728 }
dudmuck 0:8f0d0ae0a077 729 else
dudmuck 0:8f0d0ae0a077 730 {
dudmuck 0:8f0d0ae0a077 731 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 732 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
dudmuck 0:8f0d0ae0a077 733
dudmuck 0:8f0d0ae0a077 734 if( LoRaMacFlags.Value == 0 )
dudmuck 0:8f0d0ae0a077 735 {
dudmuck 0:8f0d0ae0a077 736 LoRaMacFlags.Bits.McpsReq = 1;
dudmuck 0:8f0d0ae0a077 737 }
dudmuck 0:8f0d0ae0a077 738 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 739 }
dudmuck 0:8f0d0ae0a077 740
dudmuck 0:8f0d0ae0a077 741 // Update last tx done time for the current channel
dudmuck 1:53c30224eda8 742 //Bands[Channels[Channel].Band].last_tx_done_us = tx_done_us;
dudmuck 0:8f0d0ae0a077 743 // Update Aggregated last tx done time
dudmuck 0:8f0d0ae0a077 744 AggregatedLastTxDoneTime_us = tx_done_us;
dudmuck 0:8f0d0ae0a077 745 // Update Backoff
dudmuck 0:8f0d0ae0a077 746
dudmuck 0:8f0d0ae0a077 747 if( NodeAckRequested == false )
dudmuck 0:8f0d0ae0a077 748 {
dudmuck 0:8f0d0ae0a077 749 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 750 ChannelsNbRepCounter++;
dudmuck 0:8f0d0ae0a077 751 }
dudmuck 0:8f0d0ae0a077 752 }
dudmuck 0:8f0d0ae0a077 753
dudmuck 0:8f0d0ae0a077 754 static void PrepareRxDoneAbort( void )
dudmuck 0:8f0d0ae0a077 755 {
dudmuck 0:8f0d0ae0a077 756 LoRaMacState |= LORAMAC_RX_ABORT;
dudmuck 0:8f0d0ae0a077 757
dudmuck 0:8f0d0ae0a077 758 if( NodeAckRequested )
dudmuck 0:8f0d0ae0a077 759 {
dudmuck 0:8f0d0ae0a077 760 OnAckTimeoutTimerEvent( );
dudmuck 0:8f0d0ae0a077 761 }
dudmuck 0:8f0d0ae0a077 762
dudmuck 0:8f0d0ae0a077 763 LoRaMacFlags.Bits.McpsInd = 1;
dudmuck 0:8f0d0ae0a077 764 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 765
dudmuck 0:8f0d0ae0a077 766 // Trig OnMacCheckTimerEvent call as soon as possible
dudmuck 0:8f0d0ae0a077 767 OnMacStateCheckTimerEvent();
dudmuck 0:8f0d0ae0a077 768 }
dudmuck 0:8f0d0ae0a077 769
dudmuck 8:ab2f9a8d2eaa 770 int us_at_sched;
dudmuck 0:8f0d0ae0a077 771 void send_callback()
dudmuck 0:8f0d0ae0a077 772 {
dudmuck 8:ab2f9a8d2eaa 773 LoRaMacState &= ~LORAMAC_TX_SCHED;
dudmuck 8:ab2f9a8d2eaa 774
dudmuck 5:c108560af4c3 775 Radio.SetTxConfig(
dudmuck 5:c108560af4c3 776 /* RadioModems_t modem */ MODEM_LORA,
dudmuck 5:c108560af4c3 777 /* int8_t power */ TxPowers[LoRaMacParams.ChannelsTxPower],
dudmuck 5:c108560af4c3 778 /* uint32_t fdev */ 0,
dudmuck 5:c108560af4c3 779 /* uint32_t bandwidth */ 2,
dudmuck 5:c108560af4c3 780 /* uint32_t datarate */ Datarates[LoRaMacParams.ChannelsDatarate_fixed],
dudmuck 5:c108560af4c3 781 /* uint8_t coderate */ 1,
dudmuck 5:c108560af4c3 782 /* uint16_t preambleLen */ 8,
dudmuck 5:c108560af4c3 783 /* bool fixLen */ false,
dudmuck 5:c108560af4c3 784 /* bool crcOn */ true,
dudmuck 5:c108560af4c3 785 /* bool freqHopOn */ 0,
dudmuck 5:c108560af4c3 786 /* uint8_t hopPeriod */ 0,
dudmuck 5:c108560af4c3 787 /* bool iqInverted */ false,
dudmuck 5:c108560af4c3 788 /* uint32_t timeout */ 3e3
dudmuck 5:c108560af4c3 789 );
dudmuck 0:8f0d0ae0a077 790 Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
dudmuck 0:8f0d0ae0a077 791
dudmuck 0:8f0d0ae0a077 792 LoRaMacState |= LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 793
dudmuck 0:8f0d0ae0a077 794 // Starts the MAC layer status check timer
dudmuck 0:8f0d0ae0a077 795 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
dudmuck 5:c108560af4c3 796
dudmuck 5:c108560af4c3 797 /*
dudmuck 5:c108560af4c3 798 unsigned int now_us = lp_timer.read_us();
dudmuck 5:c108560af4c3 799 int us_since_beacon_start = now_us - BeaconCtx.LastBeaconStart_us;
dudmuck 5:c108560af4c3 800 int now_slot = (us_since_beacon_start - BEACON_RESERVED_us) / 30000;
dudmuck 5:c108560af4c3 801 isr_printf("send now slot:%u\r\n", now_slot);
dudmuck 5:c108560af4c3 802 */
dudmuck 0:8f0d0ae0a077 803 }
dudmuck 0:8f0d0ae0a077 804
dudmuck 0:8f0d0ae0a077 805 void OnRxBeaconSetup()
dudmuck 0:8f0d0ae0a077 806 {
dudmuck 0:8f0d0ae0a077 807 BeaconCtx.RxBeaconSetupAt_us = lp_timer.read_us();
dudmuck 0:8f0d0ae0a077 808
dudmuck 0:8f0d0ae0a077 809 Radio.SetRxConfig(
dudmuck 0:8f0d0ae0a077 810 /* RadioModems_t */ MODEM_LORA,
dudmuck 0:8f0d0ae0a077 811 /* uint32_t bandwidth */ BEACON_CHANNEL_BW,
dudmuck 0:8f0d0ae0a077 812 /* uint32_t datarate */ Datarates[BEACON_CHANNEL_DR],
dudmuck 0:8f0d0ae0a077 813 /* uint8_t coderate */ 1,
dudmuck 0:8f0d0ae0a077 814 /* uint32_t bandwidthAfc */ 0,
dudmuck 0:8f0d0ae0a077 815 /* uint16_t preambleLen */ 10,
dudmuck 0:8f0d0ae0a077 816 /* uint16_t symbTimeout */ BeaconCtx.SymbolTimeout,
dudmuck 0:8f0d0ae0a077 817 /* bool fixLen */ true,
dudmuck 0:8f0d0ae0a077 818 /* uint8_t payloadLen */ BEACON_SIZE,
dudmuck 0:8f0d0ae0a077 819 /* bool crcOn */ false,
dudmuck 0:8f0d0ae0a077 820 /* bool freqHopOn */ 0,
dudmuck 0:8f0d0ae0a077 821 /* uint8_t hopPeriod */ 0,
dudmuck 0:8f0d0ae0a077 822 /* bool iqInverted */ false,
dudmuck 0:8f0d0ae0a077 823 /* bool rxContinuous */false
dudmuck 0:8f0d0ae0a077 824 );
dudmuck 0:8f0d0ae0a077 825
dudmuck 0:8f0d0ae0a077 826 Radio.Rx(2000);
dudmuck 0:8f0d0ae0a077 827
dudmuck 0:8f0d0ae0a077 828 expecting_beacon = true;
dudmuck 0:8f0d0ae0a077 829 }
dudmuck 0:8f0d0ae0a077 830
dudmuck 0:8f0d0ae0a077 831 static void set_beacon_symbol_timeout(float secs)
dudmuck 0:8f0d0ae0a077 832 {
dudmuck 0:8f0d0ae0a077 833 BeaconCtx.SymbolTimeout = secs / BeaconCtx.symbol_period_secs;
dudmuck 0:8f0d0ae0a077 834 if (BeaconCtx.SymbolTimeout < (BEACON_MIN_SYMBOL_TIMEOUT+BeaconCtx.Precess_symbols)) {
dudmuck 8:ab2f9a8d2eaa 835 BeaconCtx.SymbolTimeout = BEACON_MIN_SYMBOL_TIMEOUT+BeaconCtx.Precess_symbols;
dudmuck 0:8f0d0ae0a077 836 }
dudmuck 8:ab2f9a8d2eaa 837 BeaconCtx.SymbolTimeout_sec = BeaconCtx.SymbolTimeout * BeaconCtx.symbol_period_secs;
dudmuck 0:8f0d0ae0a077 838 }
dudmuck 0:8f0d0ae0a077 839
dudmuck 0:8f0d0ae0a077 840 static uint16_t beacon_crc( uint8_t *buffer, uint16_t length )
dudmuck 0:8f0d0ae0a077 841 {
dudmuck 0:8f0d0ae0a077 842 // The CRC calculation follows CCITT
dudmuck 0:8f0d0ae0a077 843 const uint16_t polynom = 0x1021;
dudmuck 0:8f0d0ae0a077 844 // CRC initial value
dudmuck 0:8f0d0ae0a077 845 uint16_t crc = 0x0000;
dudmuck 0:8f0d0ae0a077 846
dudmuck 0:8f0d0ae0a077 847 if( buffer == NULL )
dudmuck 0:8f0d0ae0a077 848 {
dudmuck 0:8f0d0ae0a077 849 return 0;
dudmuck 0:8f0d0ae0a077 850 }
dudmuck 0:8f0d0ae0a077 851
dudmuck 0:8f0d0ae0a077 852 for( uint16_t i = 0; i < length; ++i )
dudmuck 0:8f0d0ae0a077 853 {
dudmuck 0:8f0d0ae0a077 854 crc ^= ( uint16_t ) buffer[i] << 8;
dudmuck 0:8f0d0ae0a077 855 for( uint16_t j = 0; j < 8; ++j )
dudmuck 0:8f0d0ae0a077 856 {
dudmuck 0:8f0d0ae0a077 857 crc = ( crc & 0x8000 ) ? ( crc << 1 ) ^ polynom : ( crc << 1 );
dudmuck 0:8f0d0ae0a077 858 }
dudmuck 0:8f0d0ae0a077 859 }
dudmuck 0:8f0d0ae0a077 860
dudmuck 0:8f0d0ae0a077 861 return crc;
dudmuck 0:8f0d0ae0a077 862 }
dudmuck 0:8f0d0ae0a077 863
dudmuck 0:8f0d0ae0a077 864 /* low power timer needs larger value due to poor resolution */
dudmuck 0:8f0d0ae0a077 865 #define TARGET_PRECESSION_US 3000
dudmuck 0:8f0d0ae0a077 866 #define BEACON_RX_TIMEOUT_LOCKED 0.008
dudmuck 0:8f0d0ae0a077 867
dudmuck 0:8f0d0ae0a077 868 void rx_beacon(unsigned int rx_us, uint8_t* payload, uint16_t size)
dudmuck 0:8f0d0ae0a077 869 {
dudmuck 0:8f0d0ae0a077 870 static bool compensate_precession = false;
dudmuck 0:8f0d0ae0a077 871 int32_t compensation = 0;
dudmuck 0:8f0d0ae0a077 872 unsigned ThisBeaconRx_us = rx_us - (BEACON_TOA_us + BEACON_RXDONE_LATENCY_us);
dudmuck 0:8f0d0ae0a077 873
dudmuck 0:8f0d0ae0a077 874 BeaconCtx.rx_precession_us = ThisBeaconRx_us - BeaconCtx.RxBeaconSetupAt_us;
dudmuck 0:8f0d0ae0a077 875 if (BeaconCtx.state != BEACON_STATE_FIRST_ACQ) {
dudmuck 0:8f0d0ae0a077 876 unsigned int intervals_since_last = (ThisBeaconRx_us / BEACON_INTERVAL_us) - (BeaconCtx.LastBeaconRx_us / BEACON_INTERVAL_us);
dudmuck 0:8f0d0ae0a077 877 /* get average of error history */
dudmuck 0:8f0d0ae0a077 878 BeaconCtx.BeaconRxTimerError_us = (ThisBeaconRx_us - BeaconCtx.LastBeaconRx_us) % BEACON_INTERVAL_us;
dudmuck 0:8f0d0ae0a077 879 /* BeaconRxTimerError: positive means our clock is fast
dudmuck 0:8f0d0ae0a077 880 * negative means our clock is slow */
dudmuck 0:8f0d0ae0a077 881 if (BeaconCtx.BeaconRxTimerError_us > (BEACON_INTERVAL_us/2))
dudmuck 0:8f0d0ae0a077 882 BeaconCtx.BeaconRxTimerError_us -= BEACON_INTERVAL_us; // negative value representing slow crystal
dudmuck 0:8f0d0ae0a077 883
dudmuck 0:8f0d0ae0a077 884 if (intervals_since_last > 1) {
dudmuck 0:8f0d0ae0a077 885 /* timer error is measured over more than one beacon period */
dudmuck 0:8f0d0ae0a077 886 BeaconCtx.BeaconRxTimerError_us /= intervals_since_last;
dudmuck 0:8f0d0ae0a077 887 }
dudmuck 0:8f0d0ae0a077 888
dudmuck 0:8f0d0ae0a077 889 if (BeaconCtx.state == BEACON_STATE_ACQ_ERROR) {
dudmuck 1:53c30224eda8 890 isr_printf("-->LOCKED ");
dudmuck 0:8f0d0ae0a077 891 BeaconCtx.state = BEACON_STATE_LOCKED_;
dudmuck 0:8f0d0ae0a077 892 compensate_precession = true;
dudmuck 0:8f0d0ae0a077 893 set_beacon_symbol_timeout(BEACON_RX_TIMEOUT_LOCKED);
dudmuck 0:8f0d0ae0a077 894 }
dudmuck 0:8f0d0ae0a077 895 } else {
dudmuck 0:8f0d0ae0a077 896 /* ignore precession at first acquisition because it has slot resolution added */
dudmuck 1:53c30224eda8 897 isr_printf("-->ACQ_ERROR ");
dudmuck 0:8f0d0ae0a077 898 // next beacon will give us our crystal error
dudmuck 0:8f0d0ae0a077 899 BeaconCtx.state = BEACON_STATE_ACQ_ERROR;
dudmuck 0:8f0d0ae0a077 900 }
dudmuck 0:8f0d0ae0a077 901
dudmuck 1:53c30224eda8 902 isr_printf("err%d=%u-%u ", BeaconCtx.BeaconRxTimerError_us, ThisBeaconRx_us, BeaconCtx.LastBeaconRx_us);
dudmuck 1:53c30224eda8 903 isr_printf(" rx-before-tx:%d ", BeaconCtx.rx_precession_us);
dudmuck 0:8f0d0ae0a077 904 BeaconCtx.LastBeaconRx_us = ThisBeaconRx_us;
dudmuck 0:8f0d0ae0a077 905 BeaconCtx.LastBeaconStart_us = BeaconCtx.LastBeaconRx_us;
dudmuck 0:8f0d0ae0a077 906
dudmuck 0:8f0d0ae0a077 907 if (BeaconCtx.state == BEACON_STATE_LOCKED_) {
dudmuck 0:8f0d0ae0a077 908 if (compensate_precession) {
dudmuck 0:8f0d0ae0a077 909 compensation = BeaconCtx.rx_precession_us - TARGET_PRECESSION_US + BeaconCtx.BeaconRxTimerError_us;
dudmuck 1:53c30224eda8 910 isr_printf(" comp%ld", compensation);
dudmuck 0:8f0d0ae0a077 911 }
dudmuck 0:8f0d0ae0a077 912 }
dudmuck 0:8f0d0ae0a077 913
dudmuck 1:53c30224eda8 914 isr_printf("\r\n");
dudmuck 0:8f0d0ae0a077 915
dudmuck 0:8f0d0ae0a077 916 BeaconCtx.next_beacon_expected_us = BEACON_INTERVAL_us + compensation;
dudmuck 0:8f0d0ae0a077 917 unsigned now_us = lp_timer.read_us();
dudmuck 0:8f0d0ae0a077 918 unsigned us_since_rx_setup = now_us - BeaconCtx.RxBeaconSetupAt_us;
dudmuck 0:8f0d0ae0a077 919 BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, BeaconCtx.next_beacon_expected_us - us_since_rx_setup);
dudmuck 0:8f0d0ae0a077 920
dudmuck 0:8f0d0ae0a077 921 if (BeaconCtx.num_missed > 0) {
dudmuck 0:8f0d0ae0a077 922 /* restore rx symbol timeout */
dudmuck 0:8f0d0ae0a077 923 set_beacon_symbol_timeout(BEACON_RX_TIMEOUT_LOCKED);
dudmuck 0:8f0d0ae0a077 924 }
dudmuck 0:8f0d0ae0a077 925
dudmuck 0:8f0d0ae0a077 926 BeaconCtx.num_missed = 0;
dudmuck 0:8f0d0ae0a077 927 BeaconCtx.have_beacon = true;
dudmuck 0:8f0d0ae0a077 928
dudmuck 0:8f0d0ae0a077 929 MlmeIndication.MlmeIndication = MLME_BEACON;
dudmuck 0:8f0d0ae0a077 930 MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_BEACON_LOCKED;
dudmuck 0:8f0d0ae0a077 931 LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
dudmuck 0:8f0d0ae0a077 932 //LoRaMacFlags.Bits.MlmeInd = 1;
dudmuck 0:8f0d0ae0a077 933
dudmuck 0:8f0d0ae0a077 934 /* check beacon payload */
dudmuck 0:8f0d0ae0a077 935 uint16_t calc_crc = beacon_crc(payload, 4);
dudmuck 0:8f0d0ae0a077 936 uint16_t rx_crc = payload[4];
dudmuck 0:8f0d0ae0a077 937 rx_crc |= payload[5] << 8;
dudmuck 0:8f0d0ae0a077 938 if (rx_crc == calc_crc) {
dudmuck 0:8f0d0ae0a077 939 unsigned int rx = payload[0];
dudmuck 0:8f0d0ae0a077 940 rx |= payload[1] << 8;
dudmuck 0:8f0d0ae0a077 941 rx |= payload[2] << 16;
dudmuck 0:8f0d0ae0a077 942 rx |= payload[3] << 24;
dudmuck 0:8f0d0ae0a077 943 if (rx != 0)
dudmuck 1:53c30224eda8 944 isr_printf("beacon payload:%08x\r\n", rx);
dudmuck 0:8f0d0ae0a077 945 } else
dudmuck 1:53c30224eda8 946 isr_printf("calc_crc:%04x rx_crc:%04x\r\n", calc_crc, rx_crc);
dudmuck 0:8f0d0ae0a077 947 }
dudmuck 0:8f0d0ae0a077 948
dudmuck 0:8f0d0ae0a077 949 float get_symbol_period(uint8_t bw, uint8_t sf)
dudmuck 0:8f0d0ae0a077 950 {
dudmuck 0:8f0d0ae0a077 951 //bw:, // 0=125kHz, 1=250kHz, 2=500KHz
dudmuck 0:8f0d0ae0a077 952 float hz;
dudmuck 0:8f0d0ae0a077 953 switch( bw )
dudmuck 0:8f0d0ae0a077 954 {
dudmuck 0:8f0d0ae0a077 955 //case 0: // 7.8 kHz
dudmuck 0:8f0d0ae0a077 956 // bw = 78e2;
dudmuck 0:8f0d0ae0a077 957 // break;
dudmuck 0:8f0d0ae0a077 958 //case 1: // 10.4 kHz
dudmuck 0:8f0d0ae0a077 959 // bw = 104e2;
dudmuck 0:8f0d0ae0a077 960 // break;
dudmuck 0:8f0d0ae0a077 961 //case 2: // 15.6 kHz
dudmuck 0:8f0d0ae0a077 962 // bw = 156e2;
dudmuck 0:8f0d0ae0a077 963 // break;
dudmuck 0:8f0d0ae0a077 964 //case 3: // 20.8 kHz
dudmuck 0:8f0d0ae0a077 965 // bw = 208e2;
dudmuck 0:8f0d0ae0a077 966 // break;
dudmuck 0:8f0d0ae0a077 967 //case 4: // 31.2 kHz
dudmuck 0:8f0d0ae0a077 968 // bw = 312e2;
dudmuck 0:8f0d0ae0a077 969 // break;
dudmuck 0:8f0d0ae0a077 970 //case 5: // 41.4 kHz
dudmuck 0:8f0d0ae0a077 971 // bw = 414e2;
dudmuck 0:8f0d0ae0a077 972 // break;
dudmuck 0:8f0d0ae0a077 973 //case 6: // 62.5 kHz
dudmuck 0:8f0d0ae0a077 974 // bw = 625e2;
dudmuck 0:8f0d0ae0a077 975 // break;
dudmuck 0:8f0d0ae0a077 976 case 0: // 125 kHz
dudmuck 0:8f0d0ae0a077 977 hz = 125e3;
dudmuck 0:8f0d0ae0a077 978 break;
dudmuck 0:8f0d0ae0a077 979 case 1: // 250 kHz
dudmuck 0:8f0d0ae0a077 980 hz = 250e3;
dudmuck 0:8f0d0ae0a077 981 break;
dudmuck 0:8f0d0ae0a077 982 case 2: // 500 kHz
dudmuck 0:8f0d0ae0a077 983 hz = 500e3;
dudmuck 0:8f0d0ae0a077 984 break;
dudmuck 0:8f0d0ae0a077 985 default:
dudmuck 0:8f0d0ae0a077 986 return 0;
dudmuck 0:8f0d0ae0a077 987 }
dudmuck 0:8f0d0ae0a077 988
dudmuck 0:8f0d0ae0a077 989 // return symbol period in seconds
dudmuck 0:8f0d0ae0a077 990 return (1 << sf) / hz;
dudmuck 0:8f0d0ae0a077 991 }
dudmuck 0:8f0d0ae0a077 992
dudmuck 0:8f0d0ae0a077 993 static uint32_t GetRxBandwidth( int8_t datarate )
dudmuck 0:8f0d0ae0a077 994 {
dudmuck 0:8f0d0ae0a077 995 return 2; /* always 500KHz */
dudmuck 0:8f0d0ae0a077 996 }
dudmuck 0:8f0d0ae0a077 997
dudmuck 0:8f0d0ae0a077 998 static void OnRadioRxDone(unsigned rx_us, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
dudmuck 0:8f0d0ae0a077 999 {
dudmuck 0:8f0d0ae0a077 1000 LoRaMacHeader_t macHdr;
dudmuck 0:8f0d0ae0a077 1001 LoRaMacFrameCtrl_t fCtrl;
dudmuck 0:8f0d0ae0a077 1002 bool skipIndication = false;
dudmuck 0:8f0d0ae0a077 1003
dudmuck 0:8f0d0ae0a077 1004 uint8_t pktHeaderLen = 0;
dudmuck 0:8f0d0ae0a077 1005 uint32_t address = 0;
dudmuck 0:8f0d0ae0a077 1006 uint8_t appPayloadStartIndex = 0;
dudmuck 0:8f0d0ae0a077 1007 uint8_t port = 0xFF;
dudmuck 0:8f0d0ae0a077 1008 uint8_t frameLen = 0;
dudmuck 0:8f0d0ae0a077 1009 uint32_t mic = 0;
dudmuck 0:8f0d0ae0a077 1010 uint32_t micRx = 0;
dudmuck 0:8f0d0ae0a077 1011
dudmuck 0:8f0d0ae0a077 1012 uint16_t sequenceCounter = 0;
dudmuck 0:8f0d0ae0a077 1013 uint16_t sequenceCounterPrev = 0;
dudmuck 0:8f0d0ae0a077 1014 uint16_t sequenceCounterDiff = 0;
dudmuck 0:8f0d0ae0a077 1015 uint32_t downLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1016
dudmuck 0:8f0d0ae0a077 1017 MulticastParams_t *curMulticastParams = NULL;
dudmuck 0:8f0d0ae0a077 1018 uint8_t *nwkSKey = LoRaMacNwkSKey;
dudmuck 0:8f0d0ae0a077 1019 uint8_t *appSKey = LoRaMacAppSKey;
dudmuck 0:8f0d0ae0a077 1020
dudmuck 0:8f0d0ae0a077 1021 uint8_t multicast = 0;
dudmuck 0:8f0d0ae0a077 1022
dudmuck 0:8f0d0ae0a077 1023 bool isMicOk = false;
dudmuck 0:8f0d0ae0a077 1024
dudmuck 0:8f0d0ae0a077 1025 McpsConfirm.AckReceived = false;
dudmuck 0:8f0d0ae0a077 1026 McpsIndication.Rssi = rssi;
dudmuck 0:8f0d0ae0a077 1027 McpsIndication.Snr = snr;
dudmuck 0:8f0d0ae0a077 1028 McpsIndication.Port = 0;
dudmuck 0:8f0d0ae0a077 1029 McpsIndication.Multicast = 0;
dudmuck 0:8f0d0ae0a077 1030 McpsIndication.FramePending = 0;
dudmuck 0:8f0d0ae0a077 1031 McpsIndication.Buffer = NULL;
dudmuck 0:8f0d0ae0a077 1032 McpsIndication.BufferSize = 0;
dudmuck 0:8f0d0ae0a077 1033 McpsIndication.RxData = false;
dudmuck 0:8f0d0ae0a077 1034 McpsIndication.AckReceived = false;
dudmuck 0:8f0d0ae0a077 1035 McpsIndication.DownLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1036 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
dudmuck 0:8f0d0ae0a077 1037
dudmuck 0:8f0d0ae0a077 1038 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 1039
dudmuck 0:8f0d0ae0a077 1040 if (expecting_beacon) {
dudmuck 0:8f0d0ae0a077 1041 rx_beacon(rx_us, payload, size);
dudmuck 0:8f0d0ae0a077 1042 expecting_beacon = false;
dudmuck 0:8f0d0ae0a077 1043 return;
dudmuck 0:8f0d0ae0a077 1044 }
dudmuck 0:8f0d0ae0a077 1045
dudmuck 0:8f0d0ae0a077 1046 macHdr.Value = payload[pktHeaderLen++];
dudmuck 0:8f0d0ae0a077 1047
dudmuck 0:8f0d0ae0a077 1048 switch( macHdr.Bits.MType )
dudmuck 0:8f0d0ae0a077 1049 {
dudmuck 0:8f0d0ae0a077 1050 case FRAME_TYPE_JOIN_ACCEPT:
dudmuck 0:8f0d0ae0a077 1051 if( IsLoRaMacNetworkJoined == true )
dudmuck 0:8f0d0ae0a077 1052 {
dudmuck 2:f2d9aa163652 1053 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_JOIN_ACCEPT;
dudmuck 0:8f0d0ae0a077 1054 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1055 return;
dudmuck 0:8f0d0ae0a077 1056 }
dudmuck 0:8f0d0ae0a077 1057 LoRaMacJoinDecrypt( payload + 1, size - 1, LoRaMacAppKey, LoRaMacRxPayload + 1 );
dudmuck 0:8f0d0ae0a077 1058
dudmuck 0:8f0d0ae0a077 1059 LoRaMacRxPayload[0] = macHdr.Value;
dudmuck 0:8f0d0ae0a077 1060
dudmuck 0:8f0d0ae0a077 1061 LoRaMacJoinComputeMic( LoRaMacRxPayload, size - LORAMAC_MFR_LEN, LoRaMacAppKey, &mic );
dudmuck 0:8f0d0ae0a077 1062
dudmuck 0:8f0d0ae0a077 1063 micRx |= ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN];
dudmuck 0:8f0d0ae0a077 1064 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 1] << 8 );
dudmuck 0:8f0d0ae0a077 1065 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 2] << 16 );
dudmuck 0:8f0d0ae0a077 1066 micRx |= ( ( uint32_t )LoRaMacRxPayload[size - LORAMAC_MFR_LEN + 3] << 24 );
dudmuck 0:8f0d0ae0a077 1067
dudmuck 0:8f0d0ae0a077 1068 if( micRx == mic )
dudmuck 0:8f0d0ae0a077 1069 {
dudmuck 0:8f0d0ae0a077 1070 LoRaMacJoinComputeSKeys( LoRaMacAppKey, LoRaMacRxPayload + 1, LoRaMacDevNonce, LoRaMacNwkSKey, LoRaMacAppSKey );
dudmuck 1:53c30224eda8 1071 isr_printf("decr%u:", size);
dudmuck 0:8f0d0ae0a077 1072 for (mic = 0; mic < size; mic++)
dudmuck 1:53c30224eda8 1073 isr_printf("%02x ", LoRaMacRxPayload[mic]);
dudmuck 1:53c30224eda8 1074 isr_printf("\r\n");
dudmuck 0:8f0d0ae0a077 1075
dudmuck 0:8f0d0ae0a077 1076 LoRaMacNetID = ( uint32_t )LoRaMacRxPayload[4];
dudmuck 0:8f0d0ae0a077 1077 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[5] << 8 );
dudmuck 0:8f0d0ae0a077 1078 LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[6] << 16 );
dudmuck 0:8f0d0ae0a077 1079
dudmuck 0:8f0d0ae0a077 1080 LoRaMacDevAddr = ( uint32_t )LoRaMacRxPayload[7];
dudmuck 0:8f0d0ae0a077 1081 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[8] << 8 );
dudmuck 0:8f0d0ae0a077 1082 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[9] << 16 );
dudmuck 0:8f0d0ae0a077 1083 LoRaMacDevAddr |= ( ( uint32_t )LoRaMacRxPayload[10] << 24 );
dudmuck 0:8f0d0ae0a077 1084
dudmuck 0:8f0d0ae0a077 1085 // DLSettings
dudmuck 0:8f0d0ae0a077 1086 LoRaMacParams.Rx1DrOffset = ( LoRaMacRxPayload[11] >> 4 ) & 0x07;
dudmuck 0:8f0d0ae0a077 1087
dudmuck 0:8f0d0ae0a077 1088 LoRaMacParams.ReceiveDelay_us = ( LoRaMacRxPayload[12] & 0x0F );
dudmuck 0:8f0d0ae0a077 1089 if( LoRaMacParams.ReceiveDelay_us == 0 )
dudmuck 0:8f0d0ae0a077 1090 LoRaMacParams.ReceiveDelay_us = RECEIVE_DELAY_us;
dudmuck 0:8f0d0ae0a077 1091 else
dudmuck 0:8f0d0ae0a077 1092 LoRaMacParams.ReceiveDelay_us *= 10;
dudmuck 0:8f0d0ae0a077 1093
dudmuck 0:8f0d0ae0a077 1094 uint16_t beaconTimingDelay = LoRaMacRxPayload[13] & 0xff;
dudmuck 0:8f0d0ae0a077 1095 beaconTimingDelay |= LoRaMacRxPayload[14] << 8;
dudmuck 0:8f0d0ae0a077 1096
dudmuck 1:53c30224eda8 1097 isr_printf("%lx slots:%x (rxdelay %lu)", LoRaMacDevAddr, beaconTimingDelay, LoRaMacParams.ReceiveDelay_us);
dudmuck 0:8f0d0ae0a077 1098 // how long from tx-done of join-request is beacon going to occur at
dudmuck 0:8f0d0ae0a077 1099 uint32_t us_to_beacon = ( PING_SLOT_RESOLUTION_us * beaconTimingDelay );
dudmuck 0:8f0d0ae0a077 1100 // get time elapsed since last tx-done
dudmuck 0:8f0d0ae0a077 1101 unsigned int now_us = lp_timer.read_us();
dudmuck 0:8f0d0ae0a077 1102 unsigned int us_since_TxDone = now_us - TxDone_us;
dudmuck 0:8f0d0ae0a077 1103 BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, us_to_beacon - us_since_TxDone);
dudmuck 0:8f0d0ae0a077 1104 BeaconCtx.next_beacon_expected_us = now_us + us_to_beacon;
dudmuck 1:53c30224eda8 1105 isr_printf("us_to_beacon:%lu, since_tx_done:%u\r\n", us_to_beacon, us_since_TxDone);
dudmuck 0:8f0d0ae0a077 1106 BeaconCtx.have_beacon = false;
dudmuck 0:8f0d0ae0a077 1107 BeaconCtx.state = BEACON_STATE_FIRST_ACQ;
dudmuck 0:8f0d0ae0a077 1108 BeaconCtx.num_missed = 0;
dudmuck 0:8f0d0ae0a077 1109 BeaconCtx.rx_precession_us = 0;
dudmuck 0:8f0d0ae0a077 1110 BeaconCtx.BeaconRxTimerError_us = -PPM_100_BEACON_INTERVAL;
dudmuck 0:8f0d0ae0a077 1111 BeaconCtx.symbol_period_secs = get_symbol_period(GetRxBandwidth(LORAMAC_DEFAULT_DATARATE), Datarates[LORAMAC_DEFAULT_DATARATE]);
dudmuck 0:8f0d0ae0a077 1112 // N-ms: slot resolution + minimum for preamble detector + 100ppm fast crystal rxing 12ms early
dudmuck 0:8f0d0ae0a077 1113 BeaconCtx.Precess_symbols = TARGET_PRECESSION_US * (BeaconCtx.symbol_period_secs);
dudmuck 8:ab2f9a8d2eaa 1114 BeaconCtx.SymbolTimeout_sec = 0.1 + (BEACON_MIN_SYMBOL_TIMEOUT * BeaconCtx.symbol_period_secs);
dudmuck 0:8f0d0ae0a077 1115 BeaconCtx.SymbolTimeout = BeaconCtx.SymbolTimeout_sec / BeaconCtx.symbol_period_secs;
dudmuck 0:8f0d0ae0a077 1116
dudmuck 1:53c30224eda8 1117 isr_printf("sp:%f sto:%d\r\n", BeaconCtx.symbol_period_secs, BeaconCtx.SymbolTimeout);
dudmuck 0:8f0d0ae0a077 1118 BeaconCtx.tx_slot_offset = LoRaMacRxPayload[15];
dudmuck 0:8f0d0ae0a077 1119 BeaconCtx.tx_slot_offset |= LoRaMacRxPayload[16] << 8;
dudmuck 0:8f0d0ae0a077 1120 BeaconCtx.periodicity_slots = LoRaMacRxPayload[17];
dudmuck 0:8f0d0ae0a077 1121 BeaconCtx.periodicity_slots |= LoRaMacRxPayload[18] << 8;
dudmuck 1:53c30224eda8 1122 isr_printf("tso:%u, PS:%u\r\n", BeaconCtx.tx_slot_offset, BeaconCtx.periodicity_slots);
dudmuck 0:8f0d0ae0a077 1123
dudmuck 0:8f0d0ae0a077 1124 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1125 IsLoRaMacNetworkJoined = true;
dudmuck 0:8f0d0ae0a077 1126 LoRaMacParams.ChannelsDatarate_fixed = LoRaMacParamsDefaults.ChannelsDatarate_fixed;
dudmuck 0:8f0d0ae0a077 1127 }
dudmuck 0:8f0d0ae0a077 1128 else
dudmuck 0:8f0d0ae0a077 1129 {
dudmuck 0:8f0d0ae0a077 1130 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL;
dudmuck 1:53c30224eda8 1131 isr_printf("join-mic-fail\r\n");
dudmuck 0:8f0d0ae0a077 1132 JoinRequestTrials = MaxJoinRequestTrials; // stop trying
dudmuck 0:8f0d0ae0a077 1133 }
dudmuck 0:8f0d0ae0a077 1134 break;
dudmuck 0:8f0d0ae0a077 1135 case FRAME_TYPE_DATA_CONFIRMED_DOWN:
dudmuck 0:8f0d0ae0a077 1136 case FRAME_TYPE_DATA_UNCONFIRMED_DOWN:
dudmuck 0:8f0d0ae0a077 1137 {
dudmuck 0:8f0d0ae0a077 1138 address = payload[pktHeaderLen++];
dudmuck 0:8f0d0ae0a077 1139 address |= ( (uint32_t)payload[pktHeaderLen++] << 8 );
dudmuck 0:8f0d0ae0a077 1140 address |= ( (uint32_t)payload[pktHeaderLen++] << 16 );
dudmuck 0:8f0d0ae0a077 1141 address |= ( (uint32_t)payload[pktHeaderLen++] << 24 );
dudmuck 0:8f0d0ae0a077 1142
dudmuck 0:8f0d0ae0a077 1143 if( address != LoRaMacDevAddr )
dudmuck 0:8f0d0ae0a077 1144 {
dudmuck 0:8f0d0ae0a077 1145 curMulticastParams = MulticastChannels;
dudmuck 0:8f0d0ae0a077 1146 while( curMulticastParams != NULL )
dudmuck 0:8f0d0ae0a077 1147 {
dudmuck 0:8f0d0ae0a077 1148 if( address == curMulticastParams->Address )
dudmuck 0:8f0d0ae0a077 1149 {
dudmuck 0:8f0d0ae0a077 1150 multicast = 1;
dudmuck 0:8f0d0ae0a077 1151 nwkSKey = curMulticastParams->NwkSKey;
dudmuck 0:8f0d0ae0a077 1152 appSKey = curMulticastParams->AppSKey;
dudmuck 0:8f0d0ae0a077 1153 downLinkCounter = curMulticastParams->DownLinkCounter;
dudmuck 0:8f0d0ae0a077 1154 break;
dudmuck 0:8f0d0ae0a077 1155 }
dudmuck 0:8f0d0ae0a077 1156 curMulticastParams = curMulticastParams->Next;
dudmuck 0:8f0d0ae0a077 1157 }
dudmuck 0:8f0d0ae0a077 1158 if( multicast == 0 )
dudmuck 0:8f0d0ae0a077 1159 {
dudmuck 0:8f0d0ae0a077 1160 // We are not the destination of this frame.
dudmuck 0:8f0d0ae0a077 1161 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL;
dudmuck 0:8f0d0ae0a077 1162 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1163 return;
dudmuck 0:8f0d0ae0a077 1164 }
dudmuck 0:8f0d0ae0a077 1165 }
dudmuck 0:8f0d0ae0a077 1166 else
dudmuck 0:8f0d0ae0a077 1167 {
dudmuck 0:8f0d0ae0a077 1168 multicast = 0;
dudmuck 0:8f0d0ae0a077 1169 nwkSKey = LoRaMacNwkSKey;
dudmuck 0:8f0d0ae0a077 1170 appSKey = LoRaMacAppSKey;
dudmuck 0:8f0d0ae0a077 1171 downLinkCounter = DownLinkCounter;
dudmuck 0:8f0d0ae0a077 1172 }
dudmuck 0:8f0d0ae0a077 1173
dudmuck 0:8f0d0ae0a077 1174 fCtrl.Value = payload[pktHeaderLen++];
dudmuck 0:8f0d0ae0a077 1175
dudmuck 0:8f0d0ae0a077 1176 sequenceCounter = ( uint16_t )payload[pktHeaderLen++];
dudmuck 0:8f0d0ae0a077 1177 sequenceCounter |= ( uint16_t )payload[pktHeaderLen++] << 8;
dudmuck 0:8f0d0ae0a077 1178
dudmuck 0:8f0d0ae0a077 1179 appPayloadStartIndex = 8 + fCtrl.Bits.FOptsLen;
dudmuck 0:8f0d0ae0a077 1180
dudmuck 0:8f0d0ae0a077 1181 micRx |= ( uint32_t )payload[size - LORAMAC_MFR_LEN];
dudmuck 0:8f0d0ae0a077 1182 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 1] << 8 );
dudmuck 0:8f0d0ae0a077 1183 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 2] << 16 );
dudmuck 0:8f0d0ae0a077 1184 micRx |= ( ( uint32_t )payload[size - LORAMAC_MFR_LEN + 3] << 24 );
dudmuck 0:8f0d0ae0a077 1185
dudmuck 0:8f0d0ae0a077 1186 sequenceCounterPrev = ( uint16_t )downLinkCounter;
dudmuck 0:8f0d0ae0a077 1187 sequenceCounterDiff = ( sequenceCounter - sequenceCounterPrev );
dudmuck 0:8f0d0ae0a077 1188
dudmuck 0:8f0d0ae0a077 1189 if( sequenceCounterDiff < ( 1 << 15 ) )
dudmuck 0:8f0d0ae0a077 1190 {
dudmuck 0:8f0d0ae0a077 1191 downLinkCounter += sequenceCounterDiff;
dudmuck 0:8f0d0ae0a077 1192 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounter, &mic );
dudmuck 0:8f0d0ae0a077 1193 if( micRx == mic )
dudmuck 0:8f0d0ae0a077 1194 {
dudmuck 0:8f0d0ae0a077 1195 isMicOk = true;
dudmuck 0:8f0d0ae0a077 1196 }
dudmuck 0:8f0d0ae0a077 1197 }
dudmuck 0:8f0d0ae0a077 1198 else
dudmuck 0:8f0d0ae0a077 1199 {
dudmuck 0:8f0d0ae0a077 1200 // check for sequence roll-over
dudmuck 0:8f0d0ae0a077 1201 uint32_t downLinkCounterTmp = downLinkCounter + 0x10000 + ( int16_t )sequenceCounterDiff;
dudmuck 0:8f0d0ae0a077 1202 LoRaMacComputeMic( payload, size - LORAMAC_MFR_LEN, nwkSKey, address, DOWN_LINK, downLinkCounterTmp, &mic );
dudmuck 0:8f0d0ae0a077 1203 if( micRx == mic )
dudmuck 0:8f0d0ae0a077 1204 {
dudmuck 0:8f0d0ae0a077 1205 isMicOk = true;
dudmuck 0:8f0d0ae0a077 1206 downLinkCounter = downLinkCounterTmp;
dudmuck 0:8f0d0ae0a077 1207 }
dudmuck 0:8f0d0ae0a077 1208 }
dudmuck 0:8f0d0ae0a077 1209
dudmuck 0:8f0d0ae0a077 1210 // Check for a the maximum allowed counter difference
dudmuck 0:8f0d0ae0a077 1211 if( sequenceCounterDiff >= MAX_FCNT_GAP )
dudmuck 0:8f0d0ae0a077 1212 {
dudmuck 0:8f0d0ae0a077 1213 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS;
dudmuck 0:8f0d0ae0a077 1214 McpsIndication.DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1215 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1216 return;
dudmuck 0:8f0d0ae0a077 1217 }
dudmuck 0:8f0d0ae0a077 1218
dudmuck 0:8f0d0ae0a077 1219 if( isMicOk == true )
dudmuck 0:8f0d0ae0a077 1220 {
dudmuck 0:8f0d0ae0a077 1221 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1222 McpsIndication.Multicast = multicast;
dudmuck 0:8f0d0ae0a077 1223 McpsIndication.FramePending = fCtrl.Bits.FPending;
dudmuck 0:8f0d0ae0a077 1224 McpsIndication.Buffer = NULL;
dudmuck 0:8f0d0ae0a077 1225 McpsIndication.BufferSize = 0;
dudmuck 0:8f0d0ae0a077 1226 McpsIndication.DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1227
dudmuck 0:8f0d0ae0a077 1228 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1229
dudmuck 0:8f0d0ae0a077 1230 MacCommandsBufferToRepeatIndex = 0;
dudmuck 0:8f0d0ae0a077 1231
dudmuck 0:8f0d0ae0a077 1232 // Update 32 bits downlink counter
dudmuck 0:8f0d0ae0a077 1233 if( multicast == 1 )
dudmuck 0:8f0d0ae0a077 1234 {
dudmuck 0:8f0d0ae0a077 1235 McpsIndication.McpsIndication = MCPS_MULTICAST;
dudmuck 0:8f0d0ae0a077 1236
dudmuck 0:8f0d0ae0a077 1237 if( ( curMulticastParams->DownLinkCounter == downLinkCounter ) &&
dudmuck 0:8f0d0ae0a077 1238 ( curMulticastParams->DownLinkCounter != 0 ) )
dudmuck 0:8f0d0ae0a077 1239 {
dudmuck 0:8f0d0ae0a077 1240 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
dudmuck 0:8f0d0ae0a077 1241 McpsIndication.DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1242 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1243 return;
dudmuck 0:8f0d0ae0a077 1244 }
dudmuck 0:8f0d0ae0a077 1245 curMulticastParams->DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1246 }
dudmuck 0:8f0d0ae0a077 1247 else
dudmuck 0:8f0d0ae0a077 1248 {
dudmuck 0:8f0d0ae0a077 1249 if( macHdr.Bits.MType == FRAME_TYPE_DATA_CONFIRMED_DOWN )
dudmuck 0:8f0d0ae0a077 1250 {
dudmuck 0:8f0d0ae0a077 1251 SrvAckRequested = true;
dudmuck 0:8f0d0ae0a077 1252 McpsIndication.McpsIndication = MCPS_CONFIRMED;
dudmuck 0:8f0d0ae0a077 1253
dudmuck 0:8f0d0ae0a077 1254 if( ( DownLinkCounter == downLinkCounter ) &&
dudmuck 0:8f0d0ae0a077 1255 ( DownLinkCounter != 0 ) )
dudmuck 0:8f0d0ae0a077 1256 {
dudmuck 0:8f0d0ae0a077 1257 // Duplicated confirmed downlink. Skip indication.
dudmuck 0:8f0d0ae0a077 1258 // In this case, the MAC layer shall accept the MAC commands
dudmuck 0:8f0d0ae0a077 1259 // which are included in the downlink retransmission.
dudmuck 0:8f0d0ae0a077 1260 // It should not provide the same frame to the application
dudmuck 0:8f0d0ae0a077 1261 // layer again.
dudmuck 0:8f0d0ae0a077 1262 skipIndication = true;
dudmuck 0:8f0d0ae0a077 1263 }
dudmuck 0:8f0d0ae0a077 1264 }
dudmuck 0:8f0d0ae0a077 1265 else
dudmuck 0:8f0d0ae0a077 1266 {
dudmuck 0:8f0d0ae0a077 1267 SrvAckRequested = false;
dudmuck 0:8f0d0ae0a077 1268 McpsIndication.McpsIndication = MCPS_UNCONFIRMED;
dudmuck 0:8f0d0ae0a077 1269
dudmuck 0:8f0d0ae0a077 1270 if( ( DownLinkCounter == downLinkCounter ) &&
dudmuck 0:8f0d0ae0a077 1271 ( DownLinkCounter != 0 ) )
dudmuck 0:8f0d0ae0a077 1272 {
dudmuck 0:8f0d0ae0a077 1273 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED;
dudmuck 0:8f0d0ae0a077 1274 McpsIndication.DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1275 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1276 return;
dudmuck 0:8f0d0ae0a077 1277 }
dudmuck 0:8f0d0ae0a077 1278 }
dudmuck 0:8f0d0ae0a077 1279 DownLinkCounter = downLinkCounter;
dudmuck 0:8f0d0ae0a077 1280 }
dudmuck 0:8f0d0ae0a077 1281
dudmuck 0:8f0d0ae0a077 1282 // This must be done before parsing the payload and the MAC commands.
dudmuck 0:8f0d0ae0a077 1283 // We need to reset the MacCommandsBufferIndex here, since we need
dudmuck 0:8f0d0ae0a077 1284 // to take retransmissions and repititions into account. Error cases
dudmuck 0:8f0d0ae0a077 1285 // will be handled in function OnMacStateCheckTimerEvent.
dudmuck 0:8f0d0ae0a077 1286 if( McpsConfirm.McpsRequest == MCPS_CONFIRMED )
dudmuck 0:8f0d0ae0a077 1287 {
dudmuck 0:8f0d0ae0a077 1288 if( fCtrl.Bits.Ack == 1 )
dudmuck 0:8f0d0ae0a077 1289 {// Reset MacCommandsBufferIndex when we have received an ACK.
dudmuck 0:8f0d0ae0a077 1290 MacCommandsBufferIndex = 0;
dudmuck 0:8f0d0ae0a077 1291 }
dudmuck 0:8f0d0ae0a077 1292 }
dudmuck 0:8f0d0ae0a077 1293 else
dudmuck 0:8f0d0ae0a077 1294 {// Reset the variable if we have received any valid frame.
dudmuck 0:8f0d0ae0a077 1295 MacCommandsBufferIndex = 0;
dudmuck 0:8f0d0ae0a077 1296 }
dudmuck 0:8f0d0ae0a077 1297
dudmuck 0:8f0d0ae0a077 1298 // Process payload and MAC commands
dudmuck 0:8f0d0ae0a077 1299 if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 )
dudmuck 0:8f0d0ae0a077 1300 {
dudmuck 0:8f0d0ae0a077 1301 port = payload[appPayloadStartIndex++];
dudmuck 0:8f0d0ae0a077 1302 frameLen = ( size - 4 ) - appPayloadStartIndex;
dudmuck 0:8f0d0ae0a077 1303
dudmuck 0:8f0d0ae0a077 1304 McpsIndication.Port = port;
dudmuck 0:8f0d0ae0a077 1305
dudmuck 0:8f0d0ae0a077 1306 if( port == 0 )
dudmuck 0:8f0d0ae0a077 1307 {
dudmuck 0:8f0d0ae0a077 1308 // Only allow frames which do not have fOpts
dudmuck 0:8f0d0ae0a077 1309 if( fCtrl.Bits.FOptsLen == 0 )
dudmuck 0:8f0d0ae0a077 1310 {
dudmuck 0:8f0d0ae0a077 1311 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
dudmuck 0:8f0d0ae0a077 1312 frameLen,
dudmuck 0:8f0d0ae0a077 1313 nwkSKey,
dudmuck 0:8f0d0ae0a077 1314 address,
dudmuck 0:8f0d0ae0a077 1315 DOWN_LINK,
dudmuck 0:8f0d0ae0a077 1316 downLinkCounter,
dudmuck 0:8f0d0ae0a077 1317 LoRaMacRxPayload );
dudmuck 0:8f0d0ae0a077 1318
dudmuck 0:8f0d0ae0a077 1319 // Decode frame payload MAC commands
dudmuck 0:8f0d0ae0a077 1320 ProcessMacCommands( LoRaMacRxPayload, 0, frameLen, snr );
dudmuck 0:8f0d0ae0a077 1321 }
dudmuck 0:8f0d0ae0a077 1322 else
dudmuck 0:8f0d0ae0a077 1323 {
dudmuck 0:8f0d0ae0a077 1324 skipIndication = true;
dudmuck 0:8f0d0ae0a077 1325 }
dudmuck 0:8f0d0ae0a077 1326 }
dudmuck 0:8f0d0ae0a077 1327 else
dudmuck 0:8f0d0ae0a077 1328 {
dudmuck 0:8f0d0ae0a077 1329 if( fCtrl.Bits.FOptsLen > 0 )
dudmuck 0:8f0d0ae0a077 1330 {
dudmuck 0:8f0d0ae0a077 1331 // Decode Options field MAC commands. Omit the fPort.
dudmuck 0:8f0d0ae0a077 1332 ProcessMacCommands( payload, 8, appPayloadStartIndex - 1, snr );
dudmuck 0:8f0d0ae0a077 1333 }
dudmuck 0:8f0d0ae0a077 1334
dudmuck 0:8f0d0ae0a077 1335 LoRaMacPayloadDecrypt( payload + appPayloadStartIndex,
dudmuck 0:8f0d0ae0a077 1336 frameLen,
dudmuck 0:8f0d0ae0a077 1337 appSKey,
dudmuck 0:8f0d0ae0a077 1338 address,
dudmuck 0:8f0d0ae0a077 1339 DOWN_LINK,
dudmuck 0:8f0d0ae0a077 1340 downLinkCounter,
dudmuck 0:8f0d0ae0a077 1341 LoRaMacRxPayload );
dudmuck 0:8f0d0ae0a077 1342
dudmuck 0:8f0d0ae0a077 1343 if( skipIndication == false )
dudmuck 0:8f0d0ae0a077 1344 {
dudmuck 0:8f0d0ae0a077 1345 McpsIndication.Buffer = LoRaMacRxPayload;
dudmuck 0:8f0d0ae0a077 1346 McpsIndication.BufferSize = frameLen;
dudmuck 0:8f0d0ae0a077 1347 McpsIndication.RxData = true;
dudmuck 0:8f0d0ae0a077 1348 }
dudmuck 0:8f0d0ae0a077 1349 }
dudmuck 0:8f0d0ae0a077 1350 }
dudmuck 0:8f0d0ae0a077 1351 else
dudmuck 0:8f0d0ae0a077 1352 {
dudmuck 0:8f0d0ae0a077 1353 if( fCtrl.Bits.FOptsLen > 0 )
dudmuck 0:8f0d0ae0a077 1354 {
dudmuck 0:8f0d0ae0a077 1355 // Decode Options field MAC commands
dudmuck 0:8f0d0ae0a077 1356 ProcessMacCommands( payload, 8, appPayloadStartIndex, snr );
dudmuck 0:8f0d0ae0a077 1357 }
dudmuck 0:8f0d0ae0a077 1358 }
dudmuck 0:8f0d0ae0a077 1359
dudmuck 0:8f0d0ae0a077 1360 if( skipIndication == false )
dudmuck 0:8f0d0ae0a077 1361 {
dudmuck 0:8f0d0ae0a077 1362 // Check if the frame is an acknowledgement
dudmuck 0:8f0d0ae0a077 1363 if( fCtrl.Bits.Ack == 1 )
dudmuck 0:8f0d0ae0a077 1364 {
dudmuck 0:8f0d0ae0a077 1365 McpsConfirm.AckReceived = true;
dudmuck 0:8f0d0ae0a077 1366 McpsIndication.AckReceived = true;
dudmuck 0:8f0d0ae0a077 1367
dudmuck 0:8f0d0ae0a077 1368 // Stop the AckTimeout timer as no more retransmissions
dudmuck 0:8f0d0ae0a077 1369 // are needed.
dudmuck 0:8f0d0ae0a077 1370 }
dudmuck 0:8f0d0ae0a077 1371 else
dudmuck 0:8f0d0ae0a077 1372 {
dudmuck 0:8f0d0ae0a077 1373 McpsConfirm.AckReceived = false;
dudmuck 0:8f0d0ae0a077 1374 }
dudmuck 0:8f0d0ae0a077 1375 }
dudmuck 0:8f0d0ae0a077 1376 // Provide always an indication, skip the callback to the user application,
dudmuck 0:8f0d0ae0a077 1377 // in case of a confirmed downlink retransmission.
dudmuck 0:8f0d0ae0a077 1378 LoRaMacFlags.Bits.McpsInd = 1;
dudmuck 0:8f0d0ae0a077 1379 LoRaMacFlags.Bits.McpsIndSkip = skipIndication;
dudmuck 0:8f0d0ae0a077 1380 }
dudmuck 0:8f0d0ae0a077 1381 else
dudmuck 0:8f0d0ae0a077 1382 {
dudmuck 0:8f0d0ae0a077 1383 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL;
dudmuck 5:c108560af4c3 1384 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL;
dudmuck 0:8f0d0ae0a077 1385 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1386 return;
dudmuck 0:8f0d0ae0a077 1387 }
dudmuck 0:8f0d0ae0a077 1388 }
dudmuck 0:8f0d0ae0a077 1389 break;
dudmuck 0:8f0d0ae0a077 1390 case FRAME_TYPE_PROPRIETARY:
dudmuck 0:8f0d0ae0a077 1391 {
dudmuck 0:8f0d0ae0a077 1392 memcpy1( LoRaMacRxPayload, &payload[pktHeaderLen], size );
dudmuck 0:8f0d0ae0a077 1393
dudmuck 0:8f0d0ae0a077 1394 McpsIndication.McpsIndication = MCPS_PROPRIETARY;
dudmuck 0:8f0d0ae0a077 1395 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1396 McpsIndication.Buffer = LoRaMacRxPayload;
dudmuck 0:8f0d0ae0a077 1397 McpsIndication.BufferSize = size - pktHeaderLen;
dudmuck 0:8f0d0ae0a077 1398
dudmuck 0:8f0d0ae0a077 1399 LoRaMacFlags.Bits.McpsInd = 1;
dudmuck 0:8f0d0ae0a077 1400 break;
dudmuck 0:8f0d0ae0a077 1401 }
dudmuck 0:8f0d0ae0a077 1402 default:
dudmuck 2:f2d9aa163652 1403 McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_RX_MTYPE;
dudmuck 5:c108560af4c3 1404 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_RX_MTYPE;
dudmuck 0:8f0d0ae0a077 1405 PrepareRxDoneAbort( );
dudmuck 0:8f0d0ae0a077 1406 break;
dudmuck 0:8f0d0ae0a077 1407 }
dudmuck 0:8f0d0ae0a077 1408 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 1409
dudmuck 1:53c30224eda8 1410 /* run mac check quickly, but not immedately (for isr_printf) */
dudmuck 1:53c30224eda8 1411 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000);
dudmuck 1:53c30224eda8 1412 } // ..OnRadioRxDone();
dudmuck 0:8f0d0ae0a077 1413
dudmuck 0:8f0d0ae0a077 1414 static void OnRadioTxTimeout( void )
dudmuck 0:8f0d0ae0a077 1415 {
dudmuck 0:8f0d0ae0a077 1416 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 1417
dudmuck 0:8f0d0ae0a077 1418 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
dudmuck 0:8f0d0ae0a077 1419 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT;
dudmuck 0:8f0d0ae0a077 1420 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 1421 }
dudmuck 0:8f0d0ae0a077 1422
dudmuck 0:8f0d0ae0a077 1423 static void OnRadioRxError( void )
dudmuck 0:8f0d0ae0a077 1424 {
dudmuck 0:8f0d0ae0a077 1425 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 1426
dudmuck 0:8f0d0ae0a077 1427 if( NodeAckRequested == true )
dudmuck 0:8f0d0ae0a077 1428 {
dudmuck 0:8f0d0ae0a077 1429 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
dudmuck 0:8f0d0ae0a077 1430 }
dudmuck 0:8f0d0ae0a077 1431 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_ERROR;
dudmuck 0:8f0d0ae0a077 1432
dudmuck 0:8f0d0ae0a077 1433 if ((lp_timer.read_us() - AggregatedLastTxDoneTime_us) >= RxWindowDelay_us )
dudmuck 0:8f0d0ae0a077 1434 {
dudmuck 0:8f0d0ae0a077 1435 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 1436 }
dudmuck 0:8f0d0ae0a077 1437 }
dudmuck 0:8f0d0ae0a077 1438
dudmuck 0:8f0d0ae0a077 1439 static void OnRadioRxTimeout( void )
dudmuck 0:8f0d0ae0a077 1440 {
dudmuck 0:8f0d0ae0a077 1441 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 1442
dudmuck 0:8f0d0ae0a077 1443 if (expecting_beacon) {
dudmuck 0:8f0d0ae0a077 1444 /* generate simulated last beacon start */
dudmuck 0:8f0d0ae0a077 1445 BeaconCtx.LastBeaconStart_us += BEACON_INTERVAL_us + BeaconCtx.BeaconRxTimerError_us;
dudmuck 0:8f0d0ae0a077 1446
dudmuck 0:8f0d0ae0a077 1447 BeaconCtx.next_beacon_expected_us = BEACON_INTERVAL_us;
dudmuck 0:8f0d0ae0a077 1448 BeaconCtx.num_missed++;
dudmuck 0:8f0d0ae0a077 1449 unsigned now_us = lp_timer.read_us();
dudmuck 0:8f0d0ae0a077 1450 if (BeaconCtx.state == BEACON_STATE_FIRST_ACQ) {
dudmuck 0:8f0d0ae0a077 1451 BeaconCtx.next_beacon_expected_us -= 1000000;
dudmuck 0:8f0d0ae0a077 1452 set_beacon_symbol_timeout(2.000);
dudmuck 0:8f0d0ae0a077 1453 } else {
dudmuck 0:8f0d0ae0a077 1454 BeaconCtx.next_beacon_expected_us += BeaconCtx.BeaconRxTimerError_us;
dudmuck 0:8f0d0ae0a077 1455 // for measurement resolution and temperature drift while missing beacons:
dudmuck 0:8f0d0ae0a077 1456 BeaconCtx.next_beacon_expected_us -= 3000;
dudmuck 0:8f0d0ae0a077 1457 set_beacon_symbol_timeout(BeaconCtx.SymbolTimeout_sec + 0.003);
dudmuck 0:8f0d0ae0a077 1458 }
dudmuck 0:8f0d0ae0a077 1459 unsigned us_since_rx_setup = now_us - BeaconCtx.RxBeaconSetupAt_us;
dudmuck 0:8f0d0ae0a077 1460 BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, BeaconCtx.next_beacon_expected_us - us_since_rx_setup);
dudmuck 0:8f0d0ae0a077 1461
dudmuck 1:53c30224eda8 1462 isr_printf("beacon-rx-timeout %u %u next in %uus (%u)\r\n", BeaconCtx.num_missed, BeaconCtx.SymbolTimeout, BeaconCtx.next_beacon_expected_us, us_since_rx_setup);
dudmuck 0:8f0d0ae0a077 1463
dudmuck 0:8f0d0ae0a077 1464 MlmeIndication.MlmeIndication = MLME_BEACON;
dudmuck 0:8f0d0ae0a077 1465 MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_BEACON_LOST;
dudmuck 0:8f0d0ae0a077 1466 LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
dudmuck 0:8f0d0ae0a077 1467 //LoRaMacFlags.Bits.MlmeInd = 1;
dudmuck 0:8f0d0ae0a077 1468
dudmuck 0:8f0d0ae0a077 1469 expecting_beacon = false;
dudmuck 0:8f0d0ae0a077 1470 }
dudmuck 0:8f0d0ae0a077 1471
dudmuck 0:8f0d0ae0a077 1472 if( NodeAckRequested == true )
dudmuck 0:8f0d0ae0a077 1473 {
dudmuck 0:8f0d0ae0a077 1474 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
dudmuck 0:8f0d0ae0a077 1475 }
dudmuck 0:8f0d0ae0a077 1476 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT;
dudmuck 0:8f0d0ae0a077 1477 LoRaMacFlags.Bits.MacDone = 1;
dudmuck 0:8f0d0ae0a077 1478
dudmuck 1:53c30224eda8 1479 /* run mac check quickly, but not immedately (for isr_printf) */
dudmuck 7:e238827f0e47 1480 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000 + randr(0, 50000));
dudmuck 1:53c30224eda8 1481 } // ..OnRadioRxTimeout();
dudmuck 0:8f0d0ae0a077 1482
dudmuck 0:8f0d0ae0a077 1483 static void OnMacStateCheckTimerEvent( void )
dudmuck 0:8f0d0ae0a077 1484 {
dudmuck 0:8f0d0ae0a077 1485 bool txTimeout = false;
dudmuck 0:8f0d0ae0a077 1486
dudmuck 0:8f0d0ae0a077 1487 if( LoRaMacFlags.Bits.MacDone == 1 )
dudmuck 0:8f0d0ae0a077 1488 {
dudmuck 0:8f0d0ae0a077 1489 if( ( LoRaMacState & LORAMAC_RX_ABORT ) == LORAMAC_RX_ABORT )
dudmuck 0:8f0d0ae0a077 1490 {
dudmuck 0:8f0d0ae0a077 1491 LoRaMacState &= ~LORAMAC_RX_ABORT;
dudmuck 0:8f0d0ae0a077 1492 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 1493 }
dudmuck 0:8f0d0ae0a077 1494
dudmuck 0:8f0d0ae0a077 1495 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
dudmuck 0:8f0d0ae0a077 1496 {
dudmuck 0:8f0d0ae0a077 1497 if( ( McpsConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) ||
dudmuck 0:8f0d0ae0a077 1498 ( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT ) )
dudmuck 0:8f0d0ae0a077 1499 {
dudmuck 0:8f0d0ae0a077 1500 // Stop transmit cycle due to tx timeout.
dudmuck 0:8f0d0ae0a077 1501 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 1502 MacCommandsBufferIndex = 0;
dudmuck 0:8f0d0ae0a077 1503 McpsConfirm.AckReceived = false;
dudmuck 0:8f0d0ae0a077 1504 McpsConfirm.TxTimeOnAir = 0;
dudmuck 0:8f0d0ae0a077 1505 txTimeout = true;
dudmuck 0:8f0d0ae0a077 1506 }
dudmuck 0:8f0d0ae0a077 1507 }
dudmuck 0:8f0d0ae0a077 1508
dudmuck 0:8f0d0ae0a077 1509 if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
dudmuck 0:8f0d0ae0a077 1510 {
dudmuck 0:8f0d0ae0a077 1511 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
dudmuck 0:8f0d0ae0a077 1512 {
dudmuck 0:8f0d0ae0a077 1513 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
dudmuck 0:8f0d0ae0a077 1514 { // Procedure for the join request
dudmuck 0:8f0d0ae0a077 1515
dudmuck 0:8f0d0ae0a077 1516 if( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_OK )
dudmuck 0:8f0d0ae0a077 1517 { // Node joined successfully
dudmuck 1:53c30224eda8 1518 isr_printf("mac-check-join ok ");
dudmuck 0:8f0d0ae0a077 1519 UpLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1520 ChannelsNbRepCounter = 0;
dudmuck 0:8f0d0ae0a077 1521 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 1522 }
dudmuck 0:8f0d0ae0a077 1523 else
dudmuck 0:8f0d0ae0a077 1524 {
dudmuck 1:53c30224eda8 1525 isr_printf("jrt:%u, maxjrt:%u\r\n", JoinRequestTrials, MaxJoinRequestTrials);
dudmuck 0:8f0d0ae0a077 1526 if( JoinRequestTrials >= MaxJoinRequestTrials )
dudmuck 0:8f0d0ae0a077 1527 {
dudmuck 0:8f0d0ae0a077 1528 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 1:53c30224eda8 1529 isr_printf("tx-run-D\r\n");
dudmuck 0:8f0d0ae0a077 1530 }
dudmuck 0:8f0d0ae0a077 1531 else
dudmuck 0:8f0d0ae0a077 1532 {
dudmuck 0:8f0d0ae0a077 1533 LoRaMacFlags.Bits.MacDone = 0;
dudmuck 0:8f0d0ae0a077 1534 // Sends the same frame again
dudmuck 0:8f0d0ae0a077 1535 OnTxDelayedTimerEvent( );
dudmuck 0:8f0d0ae0a077 1536 }
dudmuck 0:8f0d0ae0a077 1537 }
dudmuck 0:8f0d0ae0a077 1538 }
dudmuck 0:8f0d0ae0a077 1539 } // ..if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) )
dudmuck 0:8f0d0ae0a077 1540 } // ..if( ( NodeAckRequested == false ) && ( txTimeout == false ) )
dudmuck 0:8f0d0ae0a077 1541
dudmuck 0:8f0d0ae0a077 1542 if( LoRaMacFlags.Bits.McpsInd == 1 )
dudmuck 0:8f0d0ae0a077 1543 {// Procedure if we received a frame
dudmuck 0:8f0d0ae0a077 1544 if( ( McpsConfirm.AckReceived == true ) /*|| ( AckTimeoutRetriesCounter > AckTimeoutRetries )*/ )
dudmuck 0:8f0d0ae0a077 1545 {
dudmuck 0:8f0d0ae0a077 1546 NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 1547 if( IsUpLinkCounterFixed == false )
dudmuck 0:8f0d0ae0a077 1548 {
dudmuck 0:8f0d0ae0a077 1549 UpLinkCounter++;
dudmuck 0:8f0d0ae0a077 1550 }
dudmuck 0:8f0d0ae0a077 1551
dudmuck 0:8f0d0ae0a077 1552 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 1553 }
dudmuck 0:8f0d0ae0a077 1554 }
dudmuck 0:8f0d0ae0a077 1555
dudmuck 0:8f0d0ae0a077 1556 } // ...if( LoRaMacFlags.Bits.MacDone == 1 )
dudmuck 0:8f0d0ae0a077 1557
dudmuck 0:8f0d0ae0a077 1558 // Handle reception for Class B and Class C
dudmuck 0:8f0d0ae0a077 1559 if( ( LoRaMacState & LORAMAC_RX ) == LORAMAC_RX )
dudmuck 0:8f0d0ae0a077 1560 {
dudmuck 0:8f0d0ae0a077 1561 LoRaMacState &= ~LORAMAC_RX;
dudmuck 0:8f0d0ae0a077 1562 }
dudmuck 0:8f0d0ae0a077 1563 if( LoRaMacState == LORAMAC_IDLE )
dudmuck 0:8f0d0ae0a077 1564 {
dudmuck 0:8f0d0ae0a077 1565 if( LoRaMacFlags.Bits.McpsReq == 1 )
dudmuck 0:8f0d0ae0a077 1566 {
dudmuck 0:8f0d0ae0a077 1567 LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm );
dudmuck 0:8f0d0ae0a077 1568 LoRaMacFlags.Bits.McpsReq = 0;
dudmuck 0:8f0d0ae0a077 1569 }
dudmuck 0:8f0d0ae0a077 1570
dudmuck 0:8f0d0ae0a077 1571 if( LoRaMacFlags.Bits.MlmeReq == 1 )
dudmuck 0:8f0d0ae0a077 1572 {
dudmuck 0:8f0d0ae0a077 1573 LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
dudmuck 0:8f0d0ae0a077 1574 LoRaMacFlags.Bits.MlmeReq = 0;
dudmuck 0:8f0d0ae0a077 1575 }
dudmuck 0:8f0d0ae0a077 1576
dudmuck 0:8f0d0ae0a077 1577 if( LoRaMacFlags.Bits.MlmeInd == 1 )
dudmuck 0:8f0d0ae0a077 1578 {
dudmuck 0:8f0d0ae0a077 1579 LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
dudmuck 0:8f0d0ae0a077 1580 LoRaMacFlags.Bits.MlmeInd = 0;
dudmuck 0:8f0d0ae0a077 1581 }
dudmuck 0:8f0d0ae0a077 1582
dudmuck 0:8f0d0ae0a077 1583 // Procedure done. Reset variables.
dudmuck 0:8f0d0ae0a077 1584 LoRaMacFlags.Bits.MacDone = 0;
dudmuck 0:8f0d0ae0a077 1585 }
dudmuck 0:8f0d0ae0a077 1586 else
dudmuck 0:8f0d0ae0a077 1587 {
dudmuck 0:8f0d0ae0a077 1588 // Operation not finished restart timer
dudmuck 0:8f0d0ae0a077 1589 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
dudmuck 0:8f0d0ae0a077 1590 }
dudmuck 0:8f0d0ae0a077 1591
dudmuck 0:8f0d0ae0a077 1592 if( LoRaMacFlags.Bits.McpsInd == 1 )
dudmuck 0:8f0d0ae0a077 1593 {
dudmuck 0:8f0d0ae0a077 1594 if( LoRaMacFlags.Bits.McpsIndSkip == 0 )
dudmuck 0:8f0d0ae0a077 1595 {
dudmuck 0:8f0d0ae0a077 1596 LoRaMacPrimitives->MacMcpsIndication( &McpsIndication );
dudmuck 0:8f0d0ae0a077 1597 }
dudmuck 0:8f0d0ae0a077 1598 LoRaMacFlags.Bits.McpsIndSkip = 0;
dudmuck 0:8f0d0ae0a077 1599 LoRaMacFlags.Bits.McpsInd = 0;
dudmuck 0:8f0d0ae0a077 1600 }
dudmuck 0:8f0d0ae0a077 1601 } // ..OnMacStateCheckTimerEvent( void )
dudmuck 0:8f0d0ae0a077 1602
dudmuck 0:8f0d0ae0a077 1603 static void OnTxDelayedTimerEvent( void )
dudmuck 0:8f0d0ae0a077 1604 {
dudmuck 0:8f0d0ae0a077 1605 LoRaMacHeader_t macHdr;
dudmuck 0:8f0d0ae0a077 1606 LoRaMacFrameCtrl_t fCtrl;
dudmuck 0:8f0d0ae0a077 1607
dudmuck 0:8f0d0ae0a077 1608 LoRaMacState &= ~LORAMAC_TX_DELAYED;
dudmuck 0:8f0d0ae0a077 1609
dudmuck 0:8f0d0ae0a077 1610 if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) && ( MlmeConfirm.MlmeRequest == MLME_JOIN ) )
dudmuck 0:8f0d0ae0a077 1611 {
dudmuck 0:8f0d0ae0a077 1612 ResetMacParameters( );
dudmuck 0:8f0d0ae0a077 1613 if (++Channel == LORA_MAX_NB_CHANNELS)
dudmuck 0:8f0d0ae0a077 1614 Channel = 0;
dudmuck 1:53c30224eda8 1615 isr_printf("<ch%u>", Channel);
dudmuck 1:53c30224eda8 1616 isr_printf("tx-delayed join ch%u\r\n", Channel);
dudmuck 0:8f0d0ae0a077 1617
dudmuck 0:8f0d0ae0a077 1618 macHdr.Value = 0;
dudmuck 0:8f0d0ae0a077 1619 macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
dudmuck 0:8f0d0ae0a077 1620
dudmuck 0:8f0d0ae0a077 1621 fCtrl.Value = 0;
dudmuck 0:8f0d0ae0a077 1622 fCtrl.Bits.Adr = 0;
dudmuck 0:8f0d0ae0a077 1623
dudmuck 0:8f0d0ae0a077 1624 /* In case of join request retransmissions, the stack must prepare
dudmuck 0:8f0d0ae0a077 1625 * the frame again, because the network server keeps track of the random
dudmuck 0:8f0d0ae0a077 1626 * LoRaMacDevNonce values to prevent reply attacks. */
dudmuck 0:8f0d0ae0a077 1627 PrepareFrame( &macHdr, &fCtrl, 0, NULL, 0 );
dudmuck 0:8f0d0ae0a077 1628 }
dudmuck 0:8f0d0ae0a077 1629
dudmuck 0:8f0d0ae0a077 1630 ScheduleTx( );
dudmuck 0:8f0d0ae0a077 1631 }
dudmuck 0:8f0d0ae0a077 1632
dudmuck 0:8f0d0ae0a077 1633 static void OnRxWindowTimerEvent( void )
dudmuck 0:8f0d0ae0a077 1634 {
dudmuck 0:8f0d0ae0a077 1635 Radio.Standby( );
dudmuck 8:ab2f9a8d2eaa 1636 if (expecting_beacon) {
dudmuck 8:ab2f9a8d2eaa 1637 isr_printf("rxwin-during-beacon\r\n");
dudmuck 8:ab2f9a8d2eaa 1638 /* TODO: report failure */
dudmuck 8:ab2f9a8d2eaa 1639 } else {
dudmuck 8:ab2f9a8d2eaa 1640 RxWindowSetup( LORAMAC_FIRST_CHANNEL + ( Channel * LORAMAC_STEPWIDTH_CHANNEL), RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false );
dudmuck 8:ab2f9a8d2eaa 1641 }
dudmuck 0:8f0d0ae0a077 1642
dudmuck 0:8f0d0ae0a077 1643 /* no retrying TX in this class of operation */
dudmuck 0:8f0d0ae0a077 1644 LoRaMacState &= ~LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 1645 }
dudmuck 0:8f0d0ae0a077 1646
dudmuck 0:8f0d0ae0a077 1647 static void OnAckTimeoutTimerEvent( void )
dudmuck 0:8f0d0ae0a077 1648 {
dudmuck 0:8f0d0ae0a077 1649 if( NodeAckRequested == true )
dudmuck 0:8f0d0ae0a077 1650 {
dudmuck 0:8f0d0ae0a077 1651 LoRaMacState &= ~LORAMAC_ACK_REQ;
dudmuck 0:8f0d0ae0a077 1652 }
dudmuck 0:8f0d0ae0a077 1653 }
dudmuck 0:8f0d0ae0a077 1654
dudmuck 0:8f0d0ae0a077 1655 static bool RxWindowSetup( uint32_t freq, int8_t datarate, uint32_t bandwidth, uint16_t timeout, bool rxContinuous )
dudmuck 0:8f0d0ae0a077 1656 {
dudmuck 0:8f0d0ae0a077 1657 uint8_t downlinkDatarate = Datarates[datarate];
dudmuck 0:8f0d0ae0a077 1658 RadioModems_t modem;
dudmuck 0:8f0d0ae0a077 1659
dudmuck 0:8f0d0ae0a077 1660 if( Radio.GetStatus( ) == RF_IDLE )
dudmuck 0:8f0d0ae0a077 1661 {
dudmuck 0:8f0d0ae0a077 1662 Radio.SetChannel( freq );
dudmuck 0:8f0d0ae0a077 1663
dudmuck 0:8f0d0ae0a077 1664 // Store downlink datarate
dudmuck 0:8f0d0ae0a077 1665 McpsIndication.RxDatarate = ( uint8_t ) datarate;
dudmuck 0:8f0d0ae0a077 1666
dudmuck 0:8f0d0ae0a077 1667 modem = MODEM_LORA;
dudmuck 0:8f0d0ae0a077 1668 Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
dudmuck 0:8f0d0ae0a077 1669 Radio.SetMaxPayloadLength( MODEM_LORA, 255 );
dudmuck 0:8f0d0ae0a077 1670
dudmuck 0:8f0d0ae0a077 1671 if( rxContinuous == false )
dudmuck 0:8f0d0ae0a077 1672 {
dudmuck 0:8f0d0ae0a077 1673 Radio.Rx( LoRaMacParams.MaxRxWindow );
dudmuck 0:8f0d0ae0a077 1674 }
dudmuck 0:8f0d0ae0a077 1675 else
dudmuck 0:8f0d0ae0a077 1676 {
dudmuck 0:8f0d0ae0a077 1677 Radio.Rx( 0 ); // Continuous mode
dudmuck 0:8f0d0ae0a077 1678 }
dudmuck 0:8f0d0ae0a077 1679 return true;
dudmuck 0:8f0d0ae0a077 1680 }
dudmuck 0:8f0d0ae0a077 1681 return false;
dudmuck 0:8f0d0ae0a077 1682 }
dudmuck 0:8f0d0ae0a077 1683
dudmuck 0:8f0d0ae0a077 1684 static bool ValueInRange( int8_t value, int8_t min, int8_t max )
dudmuck 0:8f0d0ae0a077 1685 {
dudmuck 0:8f0d0ae0a077 1686 if( ( value >= min ) && ( value <= max ) )
dudmuck 0:8f0d0ae0a077 1687 {
dudmuck 0:8f0d0ae0a077 1688 return true;
dudmuck 0:8f0d0ae0a077 1689 }
dudmuck 0:8f0d0ae0a077 1690 return false;
dudmuck 0:8f0d0ae0a077 1691 }
dudmuck 0:8f0d0ae0a077 1692
dudmuck 0:8f0d0ae0a077 1693 static LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 )
dudmuck 0:8f0d0ae0a077 1694 {
dudmuck 0:8f0d0ae0a077 1695 LoRaMacStatus_t status = LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 1696 // The maximum buffer length must take MAC commands to re-send into account.
dudmuck 0:8f0d0ae0a077 1697 uint8_t bufLen = LORA_MAC_COMMAND_MAX_LENGTH - MacCommandsBufferToRepeatIndex;
dudmuck 0:8f0d0ae0a077 1698
dudmuck 0:8f0d0ae0a077 1699 switch( cmd )
dudmuck 0:8f0d0ae0a077 1700 {
dudmuck 0:8f0d0ae0a077 1701 case MOTE_MAC_LINK_CHECK_REQ:
dudmuck 0:8f0d0ae0a077 1702 if( MacCommandsBufferIndex < bufLen )
dudmuck 0:8f0d0ae0a077 1703 {
dudmuck 0:8f0d0ae0a077 1704 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
dudmuck 0:8f0d0ae0a077 1705 // No payload for this command
dudmuck 0:8f0d0ae0a077 1706 status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1707 }
dudmuck 0:8f0d0ae0a077 1708 break;
dudmuck 0:8f0d0ae0a077 1709 case MOTE_MAC_RX_PARAM_SETUP_ANS:
dudmuck 0:8f0d0ae0a077 1710 if( MacCommandsBufferIndex < ( bufLen - 1 ) )
dudmuck 0:8f0d0ae0a077 1711 {
dudmuck 0:8f0d0ae0a077 1712 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
dudmuck 0:8f0d0ae0a077 1713 // Status: Datarate ACK, Channel ACK
dudmuck 0:8f0d0ae0a077 1714 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
dudmuck 0:8f0d0ae0a077 1715 status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1716 }
dudmuck 0:8f0d0ae0a077 1717 break;
dudmuck 0:8f0d0ae0a077 1718 case MOTE_MAC_DEV_STATUS_ANS:
dudmuck 0:8f0d0ae0a077 1719 if( MacCommandsBufferIndex < ( bufLen - 2 ) )
dudmuck 0:8f0d0ae0a077 1720 {
dudmuck 0:8f0d0ae0a077 1721 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
dudmuck 0:8f0d0ae0a077 1722 // 1st byte Battery
dudmuck 0:8f0d0ae0a077 1723 // 2nd byte Margin
dudmuck 0:8f0d0ae0a077 1724 MacCommandsBuffer[MacCommandsBufferIndex++] = p1;
dudmuck 0:8f0d0ae0a077 1725 MacCommandsBuffer[MacCommandsBufferIndex++] = p2;
dudmuck 0:8f0d0ae0a077 1726 status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1727 }
dudmuck 0:8f0d0ae0a077 1728 break;
dudmuck 0:8f0d0ae0a077 1729 case MOTE_MAC_RX_TIMING_SETUP_ANS:
dudmuck 0:8f0d0ae0a077 1730 if( MacCommandsBufferIndex < bufLen )
dudmuck 0:8f0d0ae0a077 1731 {
dudmuck 0:8f0d0ae0a077 1732 MacCommandsBuffer[MacCommandsBufferIndex++] = cmd;
dudmuck 0:8f0d0ae0a077 1733 // No payload for this answer
dudmuck 0:8f0d0ae0a077 1734 status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1735 }
dudmuck 0:8f0d0ae0a077 1736 break;
dudmuck 0:8f0d0ae0a077 1737 default:
dudmuck 0:8f0d0ae0a077 1738 return LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 1739 }
dudmuck 0:8f0d0ae0a077 1740 if( status == LORAMAC_STATUS_OK )
dudmuck 0:8f0d0ae0a077 1741 {
dudmuck 0:8f0d0ae0a077 1742 MacCommandsInNextTx = true;
dudmuck 0:8f0d0ae0a077 1743 }
dudmuck 0:8f0d0ae0a077 1744 return status;
dudmuck 0:8f0d0ae0a077 1745 }
dudmuck 0:8f0d0ae0a077 1746
dudmuck 0:8f0d0ae0a077 1747 static uint8_t ParseMacCommandsToRepeat( uint8_t* cmdBufIn, uint8_t length, uint8_t* cmdBufOut )
dudmuck 0:8f0d0ae0a077 1748 {
dudmuck 0:8f0d0ae0a077 1749 uint8_t i = 0;
dudmuck 0:8f0d0ae0a077 1750 uint8_t cmdCount = 0;
dudmuck 0:8f0d0ae0a077 1751
dudmuck 0:8f0d0ae0a077 1752 if( ( cmdBufIn == NULL ) || ( cmdBufOut == NULL ) )
dudmuck 0:8f0d0ae0a077 1753 {
dudmuck 0:8f0d0ae0a077 1754 return 0;
dudmuck 0:8f0d0ae0a077 1755 }
dudmuck 0:8f0d0ae0a077 1756
dudmuck 0:8f0d0ae0a077 1757 for( i = 0; i < length; i++ )
dudmuck 0:8f0d0ae0a077 1758 {
dudmuck 0:8f0d0ae0a077 1759 switch( cmdBufIn[i] )
dudmuck 0:8f0d0ae0a077 1760 {
dudmuck 0:8f0d0ae0a077 1761 // STICKY
dudmuck 0:8f0d0ae0a077 1762 case MOTE_MAC_RX_PARAM_SETUP_ANS:
dudmuck 0:8f0d0ae0a077 1763 {
dudmuck 0:8f0d0ae0a077 1764 cmdBufOut[cmdCount++] = cmdBufIn[i++];
dudmuck 0:8f0d0ae0a077 1765 cmdBufOut[cmdCount++] = cmdBufIn[i];
dudmuck 0:8f0d0ae0a077 1766 break;
dudmuck 0:8f0d0ae0a077 1767 }
dudmuck 0:8f0d0ae0a077 1768 case MOTE_MAC_RX_TIMING_SETUP_ANS:
dudmuck 0:8f0d0ae0a077 1769 {
dudmuck 0:8f0d0ae0a077 1770 cmdBufOut[cmdCount++] = cmdBufIn[i];
dudmuck 0:8f0d0ae0a077 1771 break;
dudmuck 0:8f0d0ae0a077 1772 }
dudmuck 0:8f0d0ae0a077 1773 // NON-STICKY
dudmuck 0:8f0d0ae0a077 1774 case MOTE_MAC_DEV_STATUS_ANS:
dudmuck 0:8f0d0ae0a077 1775 { // 2 bytes payload
dudmuck 0:8f0d0ae0a077 1776 i += 2;
dudmuck 0:8f0d0ae0a077 1777 break;
dudmuck 0:8f0d0ae0a077 1778 }
dudmuck 0:8f0d0ae0a077 1779 case MOTE_MAC_LINK_CHECK_REQ:
dudmuck 0:8f0d0ae0a077 1780 { // 0 byte payload
dudmuck 0:8f0d0ae0a077 1781 break;
dudmuck 0:8f0d0ae0a077 1782 }
dudmuck 0:8f0d0ae0a077 1783 default:
dudmuck 0:8f0d0ae0a077 1784 break;
dudmuck 0:8f0d0ae0a077 1785 }
dudmuck 0:8f0d0ae0a077 1786 }
dudmuck 0:8f0d0ae0a077 1787
dudmuck 0:8f0d0ae0a077 1788 return cmdCount;
dudmuck 0:8f0d0ae0a077 1789 }
dudmuck 0:8f0d0ae0a077 1790
dudmuck 0:8f0d0ae0a077 1791 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr )
dudmuck 0:8f0d0ae0a077 1792 {
dudmuck 0:8f0d0ae0a077 1793 while( macIndex < commandsSize )
dudmuck 0:8f0d0ae0a077 1794 {
dudmuck 0:8f0d0ae0a077 1795 // Decode Frame MAC commands
dudmuck 0:8f0d0ae0a077 1796 switch( payload[macIndex++] )
dudmuck 0:8f0d0ae0a077 1797 {
dudmuck 0:8f0d0ae0a077 1798 case SRV_MAC_LINK_CHECK_ANS:
dudmuck 0:8f0d0ae0a077 1799 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
dudmuck 0:8f0d0ae0a077 1800 MlmeConfirm.DemodMargin = payload[macIndex++];
dudmuck 0:8f0d0ae0a077 1801 MlmeConfirm.NbGateways = payload[macIndex++];
dudmuck 0:8f0d0ae0a077 1802 break;
dudmuck 0:8f0d0ae0a077 1803 case SRV_MAC_DEV_STATUS_REQ:
dudmuck 0:8f0d0ae0a077 1804 {
dudmuck 0:8f0d0ae0a077 1805 uint8_t batteryLevel = BAT_LEVEL_NO_MEASURE;
dudmuck 0:8f0d0ae0a077 1806 if( ( LoRaMacCallbacks != NULL ) && ( LoRaMacCallbacks->GetBatteryLevel != NULL ) )
dudmuck 0:8f0d0ae0a077 1807 {
dudmuck 0:8f0d0ae0a077 1808 batteryLevel = LoRaMacCallbacks->GetBatteryLevel( );
dudmuck 0:8f0d0ae0a077 1809 }
dudmuck 0:8f0d0ae0a077 1810 AddMacCommand( MOTE_MAC_DEV_STATUS_ANS, batteryLevel, snr );
dudmuck 0:8f0d0ae0a077 1811 break;
dudmuck 0:8f0d0ae0a077 1812 }
dudmuck 0:8f0d0ae0a077 1813 case SRV_MAC_RX_TIMING_SETUP_REQ:
dudmuck 0:8f0d0ae0a077 1814 {
dudmuck 0:8f0d0ae0a077 1815 uint8_t delay = payload[macIndex++] & 0x0F;
dudmuck 0:8f0d0ae0a077 1816
dudmuck 0:8f0d0ae0a077 1817 if( delay == 0 )
dudmuck 0:8f0d0ae0a077 1818 {
dudmuck 0:8f0d0ae0a077 1819 delay++;
dudmuck 0:8f0d0ae0a077 1820 }
dudmuck 0:8f0d0ae0a077 1821 LoRaMacParams.ReceiveDelay_us = delay * 1e6;
dudmuck 0:8f0d0ae0a077 1822 AddMacCommand( MOTE_MAC_RX_TIMING_SETUP_ANS, 0, 0 );
dudmuck 0:8f0d0ae0a077 1823 }
dudmuck 0:8f0d0ae0a077 1824 break;
dudmuck 0:8f0d0ae0a077 1825 default:
dudmuck 0:8f0d0ae0a077 1826 // Unknown command. ABORT MAC commands processing
dudmuck 0:8f0d0ae0a077 1827 return;
dudmuck 0:8f0d0ae0a077 1828 }
dudmuck 0:8f0d0ae0a077 1829 }
dudmuck 0:8f0d0ae0a077 1830 }
dudmuck 0:8f0d0ae0a077 1831
dudmuck 0:8f0d0ae0a077 1832 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize )
dudmuck 0:8f0d0ae0a077 1833 {
dudmuck 0:8f0d0ae0a077 1834 LoRaMacFrameCtrl_t fCtrl;
dudmuck 0:8f0d0ae0a077 1835 LoRaMacStatus_t status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 1836
dudmuck 0:8f0d0ae0a077 1837 fCtrl.Value = 0;
dudmuck 0:8f0d0ae0a077 1838 fCtrl.Bits.FOptsLen = 0;
dudmuck 0:8f0d0ae0a077 1839 fCtrl.Bits.FPending = 0;
dudmuck 0:8f0d0ae0a077 1840 fCtrl.Bits.Ack = false;
dudmuck 0:8f0d0ae0a077 1841 fCtrl.Bits.AdrAckReq = false;
dudmuck 0:8f0d0ae0a077 1842 fCtrl.Bits.Adr = false;
dudmuck 0:8f0d0ae0a077 1843
dudmuck 0:8f0d0ae0a077 1844 // Prepare the frame
dudmuck 0:8f0d0ae0a077 1845 status = PrepareFrame( macHdr, &fCtrl, fPort, fBuffer, fBufferSize );
dudmuck 0:8f0d0ae0a077 1846
dudmuck 0:8f0d0ae0a077 1847 // Validate status
dudmuck 0:8f0d0ae0a077 1848 if( status != LORAMAC_STATUS_OK )
dudmuck 0:8f0d0ae0a077 1849 {
dudmuck 0:8f0d0ae0a077 1850 return status;
dudmuck 0:8f0d0ae0a077 1851 }
dudmuck 0:8f0d0ae0a077 1852
dudmuck 0:8f0d0ae0a077 1853 // Reset confirm parameters
dudmuck 0:8f0d0ae0a077 1854 McpsConfirm.AckReceived = false;
dudmuck 0:8f0d0ae0a077 1855 McpsConfirm.UpLinkCounter = UpLinkCounter;
dudmuck 0:8f0d0ae0a077 1856
dudmuck 0:8f0d0ae0a077 1857 status = ScheduleTx( );
dudmuck 0:8f0d0ae0a077 1858
dudmuck 0:8f0d0ae0a077 1859 return status;
dudmuck 0:8f0d0ae0a077 1860 }
dudmuck 0:8f0d0ae0a077 1861
dudmuck 0:8f0d0ae0a077 1862 static LoRaMacStatus_t ScheduleTx( void )
dudmuck 0:8f0d0ae0a077 1863 {
dudmuck 0:8f0d0ae0a077 1864 // Compute Rx1 windows parameters
dudmuck 0:8f0d0ae0a077 1865 if( IsLoRaMacNetworkJoined == false )
dudmuck 0:8f0d0ae0a077 1866 {
dudmuck 0:8f0d0ae0a077 1867 RxWindowDelay_us = LoRaMacParams.JoinAcceptDelay_us + RxWindowsParam.RxOffset; // dont care
dudmuck 0:8f0d0ae0a077 1868 }
dudmuck 0:8f0d0ae0a077 1869 else
dudmuck 0:8f0d0ae0a077 1870 {
dudmuck 0:8f0d0ae0a077 1871 RxWindowDelay_us = LoRaMacParams.ReceiveDelay_us + RxWindowsParam.RxOffset;
dudmuck 0:8f0d0ae0a077 1872 }
dudmuck 0:8f0d0ae0a077 1873
dudmuck 0:8f0d0ae0a077 1874 // Schedule transmission of frame
dudmuck 0:8f0d0ae0a077 1875
dudmuck 0:8f0d0ae0a077 1876 // Try to send now
dudmuck 0:8f0d0ae0a077 1877 return SendFrameOnChannel( Channels[Channel] );
dudmuck 0:8f0d0ae0a077 1878 }
dudmuck 0:8f0d0ae0a077 1879
dudmuck 0:8f0d0ae0a077 1880 static void ResetMacParameters( void )
dudmuck 0:8f0d0ae0a077 1881 {
dudmuck 0:8f0d0ae0a077 1882 IsLoRaMacNetworkJoined = false;
dudmuck 0:8f0d0ae0a077 1883
dudmuck 0:8f0d0ae0a077 1884 // Counters
dudmuck 0:8f0d0ae0a077 1885 UpLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1886 DownLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1887
dudmuck 0:8f0d0ae0a077 1888 ChannelsNbRepCounter = 0;
dudmuck 0:8f0d0ae0a077 1889
dudmuck 0:8f0d0ae0a077 1890 MacCommandsBufferIndex = 0;
dudmuck 0:8f0d0ae0a077 1891 MacCommandsBufferToRepeatIndex = 0;
dudmuck 0:8f0d0ae0a077 1892
dudmuck 0:8f0d0ae0a077 1893 IsRxWindowsEnabled = true;
dudmuck 0:8f0d0ae0a077 1894
dudmuck 0:8f0d0ae0a077 1895 LoRaMacParams.ChannelsTxPower = LoRaMacParamsDefaults.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 1896 LoRaMacParams.ChannelsDatarate_fixed = LoRaMacParamsDefaults.ChannelsDatarate_fixed;
dudmuck 0:8f0d0ae0a077 1897
dudmuck 0:8f0d0ae0a077 1898 LoRaMacParams.Rx1DrOffset = LoRaMacParamsDefaults.Rx1DrOffset;
dudmuck 0:8f0d0ae0a077 1899
dudmuck 0:8f0d0ae0a077 1900 NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 1901 SrvAckRequested = false;
dudmuck 0:8f0d0ae0a077 1902 MacCommandsInNextTx = false;
dudmuck 0:8f0d0ae0a077 1903
dudmuck 0:8f0d0ae0a077 1904 // Reset Multicast downlink counters
dudmuck 0:8f0d0ae0a077 1905 MulticastParams_t *cur = MulticastChannels;
dudmuck 0:8f0d0ae0a077 1906 while( cur != NULL )
dudmuck 0:8f0d0ae0a077 1907 {
dudmuck 0:8f0d0ae0a077 1908 cur->DownLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 1909 cur = cur->Next;
dudmuck 0:8f0d0ae0a077 1910 }
dudmuck 0:8f0d0ae0a077 1911
dudmuck 0:8f0d0ae0a077 1912 }
dudmuck 0:8f0d0ae0a077 1913
dudmuck 0:8f0d0ae0a077 1914 LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t *macHdr, LoRaMacFrameCtrl_t *fCtrl, uint8_t fPort, void *fBuffer, uint16_t fBufferSize )
dudmuck 0:8f0d0ae0a077 1915 {
dudmuck 0:8f0d0ae0a077 1916 uint16_t i;
dudmuck 0:8f0d0ae0a077 1917 uint8_t pktHeaderLen = 0;
dudmuck 0:8f0d0ae0a077 1918 uint32_t mic = 0;
dudmuck 0:8f0d0ae0a077 1919 const void* payload = fBuffer;
dudmuck 0:8f0d0ae0a077 1920 uint8_t framePort = fPort;
dudmuck 0:8f0d0ae0a077 1921
dudmuck 0:8f0d0ae0a077 1922 LoRaMacBufferPktLen = 0;
dudmuck 0:8f0d0ae0a077 1923
dudmuck 0:8f0d0ae0a077 1924 NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 1925
dudmuck 0:8f0d0ae0a077 1926 if( fBuffer == NULL )
dudmuck 0:8f0d0ae0a077 1927 {
dudmuck 0:8f0d0ae0a077 1928 fBufferSize = 0;
dudmuck 0:8f0d0ae0a077 1929 }
dudmuck 0:8f0d0ae0a077 1930
dudmuck 0:8f0d0ae0a077 1931 LoRaMacTxPayloadLen = fBufferSize;
dudmuck 0:8f0d0ae0a077 1932
dudmuck 0:8f0d0ae0a077 1933 LoRaMacBuffer[pktHeaderLen++] = macHdr->Value;
dudmuck 0:8f0d0ae0a077 1934
dudmuck 0:8f0d0ae0a077 1935 switch( macHdr->Bits.MType )
dudmuck 0:8f0d0ae0a077 1936 {
dudmuck 0:8f0d0ae0a077 1937 case FRAME_TYPE_JOIN_REQ:
dudmuck 0:8f0d0ae0a077 1938 LoRaMacBufferPktLen = pktHeaderLen;
dudmuck 0:8f0d0ae0a077 1939
dudmuck 0:8f0d0ae0a077 1940 memcpyr( LoRaMacBuffer + LoRaMacBufferPktLen, LoRaMacAppEui, 8 );
dudmuck 0:8f0d0ae0a077 1941 LoRaMacBufferPktLen += 8;
dudmuck 0:8f0d0ae0a077 1942 memcpyr( LoRaMacBuffer + LoRaMacBufferPktLen, LoRaMacDevEui, 8 );
dudmuck 0:8f0d0ae0a077 1943 LoRaMacBufferPktLen += 8;
dudmuck 0:8f0d0ae0a077 1944
dudmuck 0:8f0d0ae0a077 1945 LoRaMacDevNonce = Radio.Random( );
dudmuck 0:8f0d0ae0a077 1946
dudmuck 0:8f0d0ae0a077 1947 LoRaMacBuffer[LoRaMacBufferPktLen++] = LoRaMacDevNonce & 0xFF;
dudmuck 0:8f0d0ae0a077 1948 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( LoRaMacDevNonce >> 8 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1949
dudmuck 0:8f0d0ae0a077 1950 LoRaMacJoinComputeMic( LoRaMacBuffer, LoRaMacBufferPktLen & 0xFF, LoRaMacAppKey, &mic );
dudmuck 0:8f0d0ae0a077 1951
dudmuck 0:8f0d0ae0a077 1952 LoRaMacBuffer[LoRaMacBufferPktLen++] = mic & 0xFF;
dudmuck 0:8f0d0ae0a077 1953 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 8 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1954 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 16 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1955 LoRaMacBuffer[LoRaMacBufferPktLen++] = ( mic >> 24 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1956
dudmuck 0:8f0d0ae0a077 1957 break;
dudmuck 0:8f0d0ae0a077 1958 case FRAME_TYPE_DATA_CONFIRMED_UP:
dudmuck 0:8f0d0ae0a077 1959 NodeAckRequested = true;
dudmuck 0:8f0d0ae0a077 1960 //Intentional fallthrough
dudmuck 0:8f0d0ae0a077 1961 case FRAME_TYPE_DATA_UNCONFIRMED_UP:
dudmuck 0:8f0d0ae0a077 1962 if( IsLoRaMacNetworkJoined == false )
dudmuck 0:8f0d0ae0a077 1963 {
dudmuck 0:8f0d0ae0a077 1964 return LORAMAC_STATUS_NO_NETWORK_JOINED; // No network has been joined yet
dudmuck 0:8f0d0ae0a077 1965 }
dudmuck 0:8f0d0ae0a077 1966
dudmuck 0:8f0d0ae0a077 1967 fCtrl->Bits.AdrAckReq = 0;
dudmuck 0:8f0d0ae0a077 1968
dudmuck 0:8f0d0ae0a077 1969 if( SrvAckRequested == true )
dudmuck 0:8f0d0ae0a077 1970 {
dudmuck 0:8f0d0ae0a077 1971 SrvAckRequested = false;
dudmuck 0:8f0d0ae0a077 1972 fCtrl->Bits.Ack = 1;
dudmuck 0:8f0d0ae0a077 1973 }
dudmuck 0:8f0d0ae0a077 1974
dudmuck 0:8f0d0ae0a077 1975 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1976 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 8 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1977 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 16 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1978 LoRaMacBuffer[pktHeaderLen++] = ( LoRaMacDevAddr >> 24 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1979
dudmuck 0:8f0d0ae0a077 1980 LoRaMacBuffer[pktHeaderLen++] = fCtrl->Value;
dudmuck 0:8f0d0ae0a077 1981
dudmuck 0:8f0d0ae0a077 1982 LoRaMacBuffer[pktHeaderLen++] = UpLinkCounter & 0xFF;
dudmuck 0:8f0d0ae0a077 1983 LoRaMacBuffer[pktHeaderLen++] = ( UpLinkCounter >> 8 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 1984
dudmuck 0:8f0d0ae0a077 1985 // Copy the MAC commands which must be re-send into the MAC command buffer
dudmuck 0:8f0d0ae0a077 1986 memcpy1( &MacCommandsBuffer[MacCommandsBufferIndex], MacCommandsBufferToRepeat, MacCommandsBufferToRepeatIndex );
dudmuck 0:8f0d0ae0a077 1987 MacCommandsBufferIndex += MacCommandsBufferToRepeatIndex;
dudmuck 0:8f0d0ae0a077 1988
dudmuck 0:8f0d0ae0a077 1989 if( ( payload != NULL ) && ( LoRaMacTxPayloadLen > 0 ) )
dudmuck 0:8f0d0ae0a077 1990 {
dudmuck 0:8f0d0ae0a077 1991 if( ( MacCommandsBufferIndex <= LORA_MAC_COMMAND_MAX_LENGTH ) && ( MacCommandsInNextTx == true ) )
dudmuck 0:8f0d0ae0a077 1992 {
dudmuck 0:8f0d0ae0a077 1993 fCtrl->Bits.FOptsLen += MacCommandsBufferIndex;
dudmuck 0:8f0d0ae0a077 1994
dudmuck 0:8f0d0ae0a077 1995 // Update FCtrl field with new value of OptionsLength
dudmuck 0:8f0d0ae0a077 1996 LoRaMacBuffer[0x05] = fCtrl->Value;
dudmuck 0:8f0d0ae0a077 1997 for( i = 0; i < MacCommandsBufferIndex; i++ )
dudmuck 0:8f0d0ae0a077 1998 {
dudmuck 0:8f0d0ae0a077 1999 LoRaMacBuffer[pktHeaderLen++] = MacCommandsBuffer[i];
dudmuck 0:8f0d0ae0a077 2000 }
dudmuck 0:8f0d0ae0a077 2001 }
dudmuck 0:8f0d0ae0a077 2002 }
dudmuck 0:8f0d0ae0a077 2003 else
dudmuck 0:8f0d0ae0a077 2004 {
dudmuck 0:8f0d0ae0a077 2005 if( ( MacCommandsBufferIndex > 0 ) && ( MacCommandsInNextTx ) )
dudmuck 0:8f0d0ae0a077 2006 {
dudmuck 0:8f0d0ae0a077 2007 LoRaMacTxPayloadLen = MacCommandsBufferIndex;
dudmuck 0:8f0d0ae0a077 2008 payload = MacCommandsBuffer;
dudmuck 0:8f0d0ae0a077 2009 framePort = 0;
dudmuck 0:8f0d0ae0a077 2010 }
dudmuck 0:8f0d0ae0a077 2011 }
dudmuck 0:8f0d0ae0a077 2012 MacCommandsInNextTx = false;
dudmuck 0:8f0d0ae0a077 2013 // Store MAC commands which must be re-send in case the device does not receive a downlink anymore
dudmuck 0:8f0d0ae0a077 2014 MacCommandsBufferToRepeatIndex = ParseMacCommandsToRepeat( MacCommandsBuffer, MacCommandsBufferIndex, MacCommandsBufferToRepeat );
dudmuck 0:8f0d0ae0a077 2015 if( MacCommandsBufferToRepeatIndex > 0 )
dudmuck 0:8f0d0ae0a077 2016 {
dudmuck 0:8f0d0ae0a077 2017 MacCommandsInNextTx = true;
dudmuck 0:8f0d0ae0a077 2018 }
dudmuck 0:8f0d0ae0a077 2019
dudmuck 0:8f0d0ae0a077 2020 if( ( payload != NULL ) && ( LoRaMacTxPayloadLen > 0 ) )
dudmuck 0:8f0d0ae0a077 2021 {
dudmuck 0:8f0d0ae0a077 2022 LoRaMacBuffer[pktHeaderLen++] = framePort;
dudmuck 0:8f0d0ae0a077 2023
dudmuck 0:8f0d0ae0a077 2024 if( framePort == 0 )
dudmuck 0:8f0d0ae0a077 2025 {
dudmuck 0:8f0d0ae0a077 2026 LoRaMacPayloadEncrypt( (uint8_t* ) payload, LoRaMacTxPayloadLen, LoRaMacNwkSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, &LoRaMacBuffer[pktHeaderLen] );
dudmuck 0:8f0d0ae0a077 2027 }
dudmuck 0:8f0d0ae0a077 2028 else
dudmuck 0:8f0d0ae0a077 2029 {
dudmuck 0:8f0d0ae0a077 2030 LoRaMacPayloadEncrypt( (uint8_t* ) payload, LoRaMacTxPayloadLen, LoRaMacAppSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, &LoRaMacBuffer[pktHeaderLen] );
dudmuck 0:8f0d0ae0a077 2031 }
dudmuck 0:8f0d0ae0a077 2032 }
dudmuck 0:8f0d0ae0a077 2033 LoRaMacBufferPktLen = pktHeaderLen + LoRaMacTxPayloadLen;
dudmuck 0:8f0d0ae0a077 2034
dudmuck 0:8f0d0ae0a077 2035 LoRaMacComputeMic( LoRaMacBuffer, LoRaMacBufferPktLen, LoRaMacNwkSKey, LoRaMacDevAddr, UP_LINK, UpLinkCounter, &mic );
dudmuck 0:8f0d0ae0a077 2036
dudmuck 0:8f0d0ae0a077 2037 LoRaMacBuffer[LoRaMacBufferPktLen + 0] = mic & 0xFF;
dudmuck 0:8f0d0ae0a077 2038 LoRaMacBuffer[LoRaMacBufferPktLen + 1] = ( mic >> 8 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 2039 LoRaMacBuffer[LoRaMacBufferPktLen + 2] = ( mic >> 16 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 2040 LoRaMacBuffer[LoRaMacBufferPktLen + 3] = ( mic >> 24 ) & 0xFF;
dudmuck 0:8f0d0ae0a077 2041
dudmuck 0:8f0d0ae0a077 2042 LoRaMacBufferPktLen += LORAMAC_MFR_LEN;
dudmuck 0:8f0d0ae0a077 2043
dudmuck 0:8f0d0ae0a077 2044 break;
dudmuck 0:8f0d0ae0a077 2045 case FRAME_TYPE_PROPRIETARY:
dudmuck 0:8f0d0ae0a077 2046 if( ( fBuffer != NULL ) && ( LoRaMacTxPayloadLen > 0 ) )
dudmuck 0:8f0d0ae0a077 2047 {
dudmuck 0:8f0d0ae0a077 2048 memcpy1( LoRaMacBuffer + pktHeaderLen, ( uint8_t* ) fBuffer, LoRaMacTxPayloadLen );
dudmuck 0:8f0d0ae0a077 2049 LoRaMacBufferPktLen = pktHeaderLen + LoRaMacTxPayloadLen;
dudmuck 0:8f0d0ae0a077 2050 }
dudmuck 0:8f0d0ae0a077 2051 break;
dudmuck 0:8f0d0ae0a077 2052 default:
dudmuck 0:8f0d0ae0a077 2053 return LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 2054 }
dudmuck 0:8f0d0ae0a077 2055
dudmuck 0:8f0d0ae0a077 2056 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2057 }
dudmuck 0:8f0d0ae0a077 2058
dudmuck 0:8f0d0ae0a077 2059
dudmuck 5:c108560af4c3 2060 //TxPowers[LoRaMacParams.ChannelsTxPower]
dudmuck 0:8f0d0ae0a077 2061 LoRaMacStatus_t SendFrameOnChannel( ChannelParams_t channel )
dudmuck 0:8f0d0ae0a077 2062 {
dudmuck 0:8f0d0ae0a077 2063 int8_t datarate = Datarates[LoRaMacParams.ChannelsDatarate_fixed];
dudmuck 0:8f0d0ae0a077 2064 int8_t txPowerIndex = 0;
dudmuck 0:8f0d0ae0a077 2065 int8_t txPower = 0;
dudmuck 0:8f0d0ae0a077 2066
dudmuck 0:8f0d0ae0a077 2067 if (LoRaMacState & LORAMAC_TX_SCHED) {
dudmuck 0:8f0d0ae0a077 2068 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2069 }
dudmuck 0:8f0d0ae0a077 2070
dudmuck 0:8f0d0ae0a077 2071 txPowerIndex = LoRaMacParams.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 2072 txPower = TxPowers[txPowerIndex];
dudmuck 0:8f0d0ae0a077 2073
dudmuck 2:f2d9aa163652 2074 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
dudmuck 2:f2d9aa163652 2075 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_SEND;
dudmuck 0:8f0d0ae0a077 2076 McpsConfirm.TxPower = txPowerIndex;
dudmuck 0:8f0d0ae0a077 2077 McpsConfirm.UpLinkFrequency = channel.Frequency;
dudmuck 0:8f0d0ae0a077 2078
dudmuck 0:8f0d0ae0a077 2079 Radio.SetChannel( channel.Frequency );
dudmuck 0:8f0d0ae0a077 2080
dudmuck 0:8f0d0ae0a077 2081 if( LoRaMacParams.ChannelsDatarate_fixed >= DR_8 )
dudmuck 0:8f0d0ae0a077 2082 { // High speed LoRa channel BW500 kHz
dudmuck 5:c108560af4c3 2083 //Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
dudmuck 0:8f0d0ae0a077 2084 TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
dudmuck 0:8f0d0ae0a077 2085 } else
dudmuck 0:8f0d0ae0a077 2086 return LORAMAC_STATUS_DATARATE_INVALID;
dudmuck 0:8f0d0ae0a077 2087
dudmuck 0:8f0d0ae0a077 2088 // Store the time on air
dudmuck 0:8f0d0ae0a077 2089 McpsConfirm.TxTimeOnAir = TxTimeOnAir;
dudmuck 0:8f0d0ae0a077 2090 MlmeConfirm.TxTimeOnAir = TxTimeOnAir;
dudmuck 0:8f0d0ae0a077 2091
dudmuck 0:8f0d0ae0a077 2092
dudmuck 0:8f0d0ae0a077 2093 if( IsLoRaMacNetworkJoined == false )
dudmuck 0:8f0d0ae0a077 2094 {
dudmuck 0:8f0d0ae0a077 2095 JoinRequestTrials++;
dudmuck 7:e238827f0e47 2096 isr_printf("join %luhz try%u DR%u\r\n", channel.Frequency, JoinRequestTrials, LoRaMacParams.ChannelsDatarate_fixed);
dudmuck 0:8f0d0ae0a077 2097 }
dudmuck 0:8f0d0ae0a077 2098
dudmuck 0:8f0d0ae0a077 2099 /* anything not join request is sent at permitted time slot */
dudmuck 0:8f0d0ae0a077 2100 LoRaMacHeader_t* macHdr = (LoRaMacHeader_t*)&LoRaMacBuffer[0];
dudmuck 0:8f0d0ae0a077 2101 if (macHdr->Bits.MType == FRAME_TYPE_JOIN_REQ) {
dudmuck 0:8f0d0ae0a077 2102 // Send now
dudmuck 5:c108560af4c3 2103 Radio.SetTxConfig( MODEM_LORA, txPower, 0, 2, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
dudmuck 0:8f0d0ae0a077 2104 Radio.Send( LoRaMacBuffer, LoRaMacBufferPktLen );
dudmuck 0:8f0d0ae0a077 2105
dudmuck 0:8f0d0ae0a077 2106 LoRaMacState |= LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 2107
dudmuck 0:8f0d0ae0a077 2108 // Starts the MAC layer status check timer
dudmuck 0:8f0d0ae0a077 2109 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
dudmuck 0:8f0d0ae0a077 2110 } else if (BeaconCtx.have_beacon) {
dudmuck 0:8f0d0ae0a077 2111 /* find now ping slot */
dudmuck 0:8f0d0ae0a077 2112 unsigned int target_us, now_us = lp_timer.read_us();
dudmuck 6:240fd4938d51 2113 int us_to_guard, us_since_beacon_start = now_us - BeaconCtx.LastBeaconStart_us;
dudmuck 0:8f0d0ae0a077 2114 int use_slot = BeaconCtx.tx_slot_offset;
dudmuck 0:8f0d0ae0a077 2115 int now_slot = (us_since_beacon_start - BEACON_RESERVED_us) / 30000;
dudmuck 8:ab2f9a8d2eaa 2116 int use_slot_us, txing_in_us;
dudmuck 0:8f0d0ae0a077 2117
dudmuck 0:8f0d0ae0a077 2118 while (use_slot < now_slot)
dudmuck 0:8f0d0ae0a077 2119 use_slot += BeaconCtx.periodicity_slots;
dudmuck 0:8f0d0ae0a077 2120
dudmuck 0:8f0d0ae0a077 2121 use_slot_us = (use_slot * 30000);
dudmuck 6:240fd4938d51 2122 us_to_guard = BeaconCtx.next_beacon_expected_us - use_slot_us;
dudmuck 8:ab2f9a8d2eaa 2123 target_us = (use_slot * 30000) + BEACON_RESERVED_us;
dudmuck 8:ab2f9a8d2eaa 2124 txing_in_us = target_us - us_since_beacon_start;
dudmuck 0:8f0d0ae0a077 2125 #ifdef TX_DEBUG
dudmuck 8:ab2f9a8d2eaa 2126 isr_printf("now_slot:%d, to-guard:%d use_slot:%u (txing_in %d) ", now_slot, us_to_guard, use_slot, txing_in_us);
dudmuck 0:8f0d0ae0a077 2127 #endif
dudmuck 8:ab2f9a8d2eaa 2128 if ((us_to_guard - txing_in_us) > BEACON_GUARD_us) {
dudmuck 8:ab2f9a8d2eaa 2129 us_at_sched = us_to_guard - txing_in_us;
dudmuck 8:ab2f9a8d2eaa 2130 tx_timeout.attach_us(&send_callback, txing_in_us);
dudmuck 0:8f0d0ae0a077 2131 #ifdef TX_DEBUG
dudmuck 1:53c30224eda8 2132 isr_printf(" : tx in %uus\r\n", target_us - us_since_beacon_start);
dudmuck 0:8f0d0ae0a077 2133 #endif
dudmuck 0:8f0d0ae0a077 2134 } else {
dudmuck 6:240fd4938d51 2135 #ifdef TX_DEBUG
dudmuck 5:c108560af4c3 2136 isr_printf("sf-busy\r\n");
dudmuck 6:240fd4938d51 2137 #endif
dudmuck 0:8f0d0ae0a077 2138 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2139 }
dudmuck 0:8f0d0ae0a077 2140
dudmuck 0:8f0d0ae0a077 2141 LoRaMacState |= LORAMAC_TX_SCHED;
dudmuck 0:8f0d0ae0a077 2142 } else {
dudmuck 1:53c30224eda8 2143 isr_printf("dropped tx\r\n");
dudmuck 0:8f0d0ae0a077 2144 return LORAMAC_STATUS_SERVICE_UNKNOWN; // TODO use correct failure
dudmuck 0:8f0d0ae0a077 2145 }
dudmuck 0:8f0d0ae0a077 2146
dudmuck 0:8f0d0ae0a077 2147 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2148 }
dudmuck 0:8f0d0ae0a077 2149
dudmuck 0:8f0d0ae0a077 2150 LoRaMacStatus_t SetTxContinuousWave( uint16_t timeout )
dudmuck 0:8f0d0ae0a077 2151 {
dudmuck 0:8f0d0ae0a077 2152 int8_t txPowerIndex = 0;
dudmuck 0:8f0d0ae0a077 2153 int8_t txPower = 0;
dudmuck 0:8f0d0ae0a077 2154
dudmuck 0:8f0d0ae0a077 2155 txPowerIndex = LoRaMacParams.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 2156 txPower = TxPowers[txPowerIndex];
dudmuck 0:8f0d0ae0a077 2157
dudmuck 0:8f0d0ae0a077 2158 // Starts the MAC layer status check timer
dudmuck 0:8f0d0ae0a077 2159 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
dudmuck 0:8f0d0ae0a077 2160
dudmuck 0:8f0d0ae0a077 2161 Radio.SetTxContinuousWave( Channels[Channel].Frequency, txPower, timeout );
dudmuck 0:8f0d0ae0a077 2162
dudmuck 0:8f0d0ae0a077 2163 LoRaMacState |= LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 2164
dudmuck 0:8f0d0ae0a077 2165 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2166 }
dudmuck 0:8f0d0ae0a077 2167
dudmuck 0:8f0d0ae0a077 2168 LoRaMacStatus_t SetTxContinuousWave1( uint16_t timeout, uint32_t frequency, uint8_t power )
dudmuck 0:8f0d0ae0a077 2169 {
dudmuck 0:8f0d0ae0a077 2170 Radio.SetTxContinuousWave( frequency, power, timeout );
dudmuck 0:8f0d0ae0a077 2171
dudmuck 0:8f0d0ae0a077 2172 // Starts the MAC layer status check timer
dudmuck 0:8f0d0ae0a077 2173 MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us);
dudmuck 0:8f0d0ae0a077 2174
dudmuck 0:8f0d0ae0a077 2175 LoRaMacState |= LORAMAC_TX_RUNNING;
dudmuck 0:8f0d0ae0a077 2176
dudmuck 0:8f0d0ae0a077 2177 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2178 }
dudmuck 0:8f0d0ae0a077 2179
dudmuck 0:8f0d0ae0a077 2180 void seconds()
dudmuck 0:8f0d0ae0a077 2181 {
dudmuck 1:53c30224eda8 2182 isr_printf("second\r\n");
dudmuck 0:8f0d0ae0a077 2183 }
dudmuck 0:8f0d0ae0a077 2184
dudmuck 0:8f0d0ae0a077 2185 LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t *primitives, LoRaMacCallback_t *callbacks )
dudmuck 0:8f0d0ae0a077 2186 {
dudmuck 0:8f0d0ae0a077 2187 if( primitives == NULL )
dudmuck 0:8f0d0ae0a077 2188 {
dudmuck 0:8f0d0ae0a077 2189 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2190 }
dudmuck 0:8f0d0ae0a077 2191
dudmuck 0:8f0d0ae0a077 2192 if( ( primitives->MacMcpsConfirm == NULL ) ||
dudmuck 0:8f0d0ae0a077 2193 ( primitives->MacMcpsIndication == NULL ) ||
dudmuck 0:8f0d0ae0a077 2194 ( primitives->MacMlmeConfirm == NULL ) )
dudmuck 0:8f0d0ae0a077 2195 {
dudmuck 0:8f0d0ae0a077 2196 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2197 }
dudmuck 0:8f0d0ae0a077 2198
dudmuck 0:8f0d0ae0a077 2199 LoRaMacPrimitives = primitives;
dudmuck 0:8f0d0ae0a077 2200 LoRaMacCallbacks = callbacks;
dudmuck 0:8f0d0ae0a077 2201
dudmuck 0:8f0d0ae0a077 2202 LoRaMacFlags.Value = 0;
dudmuck 0:8f0d0ae0a077 2203
dudmuck 0:8f0d0ae0a077 2204 LoRaMacDeviceClass = CLASS_A;
dudmuck 0:8f0d0ae0a077 2205 LoRaMacState = LORAMAC_IDLE;
dudmuck 0:8f0d0ae0a077 2206
dudmuck 0:8f0d0ae0a077 2207 JoinRequestTrials = 0;
dudmuck 0:8f0d0ae0a077 2208 MaxJoinRequestTrials = 255;
dudmuck 0:8f0d0ae0a077 2209
dudmuck 0:8f0d0ae0a077 2210
dudmuck 0:8f0d0ae0a077 2211 // Reset to defaults
dudmuck 0:8f0d0ae0a077 2212 LoRaMacParamsDefaults.ChannelsTxPower = LORAMAC_DEFAULT_TX_POWER;
dudmuck 0:8f0d0ae0a077 2213 LoRaMacParamsDefaults.ChannelsDatarate_fixed = LORAMAC_DEFAULT_DATARATE;
dudmuck 0:8f0d0ae0a077 2214
dudmuck 0:8f0d0ae0a077 2215 LoRaMacParamsDefaults.SystemMaxRxError = 10;
dudmuck 0:8f0d0ae0a077 2216 LoRaMacParamsDefaults.MinRxSymbols = 6; // TODO XXX increase
dudmuck 0:8f0d0ae0a077 2217 LoRaMacParamsDefaults.MaxRxWindow = MAX_RX_WINDOW;
dudmuck 0:8f0d0ae0a077 2218
dudmuck 0:8f0d0ae0a077 2219 LoRaMacParamsDefaults.ReceiveDelay_us = RECEIVE_DELAY_us;
dudmuck 0:8f0d0ae0a077 2220 LoRaMacParamsDefaults.JoinAcceptDelay_us = JOIN_ACCEPT_DELAY_us;
dudmuck 0:8f0d0ae0a077 2221
dudmuck 0:8f0d0ae0a077 2222 LoRaMacParamsDefaults.ChannelsNbRep = 1;
dudmuck 0:8f0d0ae0a077 2223 LoRaMacParamsDefaults.Rx1DrOffset = 0;
dudmuck 0:8f0d0ae0a077 2224
dudmuck 0:8f0d0ae0a077 2225
dudmuck 0:8f0d0ae0a077 2226 #if defined(USE_BAND_915_SINGLE)
dudmuck 0:8f0d0ae0a077 2227 // 500 kHz channels
dudmuck 0:8f0d0ae0a077 2228 for( uint8_t i = 0; i < LORA_MAX_NB_CHANNELS; i++ )
dudmuck 0:8f0d0ae0a077 2229 {
dudmuck 0:8f0d0ae0a077 2230 Channels[i].Frequency = LORAMAC_FIRST_CHANNEL + (i * LORAMAC_STEPWIDTH_CHANNEL);
dudmuck 0:8f0d0ae0a077 2231 Channels[i].DrRange.Value = ( DR_13 << 4 ) | DR_8;
dudmuck 0:8f0d0ae0a077 2232 Channels[i].Band = 0;
dudmuck 0:8f0d0ae0a077 2233 }
dudmuck 0:8f0d0ae0a077 2234 #endif
dudmuck 0:8f0d0ae0a077 2235
dudmuck 0:8f0d0ae0a077 2236 // Init parameters which are not set in function ResetMacParameters
dudmuck 0:8f0d0ae0a077 2237 LoRaMacParams.SystemMaxRxError = LoRaMacParamsDefaults.SystemMaxRxError;
dudmuck 0:8f0d0ae0a077 2238 LoRaMacParams.MinRxSymbols = LoRaMacParamsDefaults.MinRxSymbols;
dudmuck 0:8f0d0ae0a077 2239 LoRaMacParams.MaxRxWindow = LoRaMacParamsDefaults.MaxRxWindow;
dudmuck 0:8f0d0ae0a077 2240 LoRaMacParams.ReceiveDelay_us = LoRaMacParamsDefaults.ReceiveDelay_us;
dudmuck 0:8f0d0ae0a077 2241 LoRaMacParams.JoinAcceptDelay_us = LoRaMacParamsDefaults.JoinAcceptDelay_us;
dudmuck 0:8f0d0ae0a077 2242 LoRaMacParams.ChannelsNbRep = LoRaMacParamsDefaults.ChannelsNbRep;
dudmuck 0:8f0d0ae0a077 2243
dudmuck 0:8f0d0ae0a077 2244 ResetMacParameters( );
dudmuck 0:8f0d0ae0a077 2245
dudmuck 0:8f0d0ae0a077 2246 // Initialize Radio driver
dudmuck 0:8f0d0ae0a077 2247 RadioEvents.TxDone = OnRadioTxDone;
dudmuck 0:8f0d0ae0a077 2248 RadioEvents.RxDone = OnRadioRxDone;
dudmuck 0:8f0d0ae0a077 2249 RadioEvents.RxError = OnRadioRxError;
dudmuck 0:8f0d0ae0a077 2250 RadioEvents.TxTimeout = OnRadioTxTimeout;
dudmuck 0:8f0d0ae0a077 2251 RadioEvents.RxTimeout = OnRadioRxTimeout;
dudmuck 0:8f0d0ae0a077 2252 Radio.Init( &RadioEvents );
dudmuck 0:8f0d0ae0a077 2253
dudmuck 0:8f0d0ae0a077 2254 // Random seed initialization
dudmuck 0:8f0d0ae0a077 2255 srand1( Radio.Random( ) );
dudmuck 0:8f0d0ae0a077 2256
dudmuck 0:8f0d0ae0a077 2257 PublicNetwork = true;
dudmuck 0:8f0d0ae0a077 2258 Radio.SetPublicNetwork( PublicNetwork );
dudmuck 0:8f0d0ae0a077 2259 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 2260
dudmuck 0:8f0d0ae0a077 2261 lp_timer.start();
dudmuck 0:8f0d0ae0a077 2262
dudmuck 0:8f0d0ae0a077 2263 RxWindowsParam = ComputeRxWindowParameters(LORAMAC_DEFAULT_DATARATE, LoRaMacParams.SystemMaxRxError);
dudmuck 0:8f0d0ae0a077 2264
dudmuck 0:8f0d0ae0a077 2265 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2266 }
dudmuck 0:8f0d0ae0a077 2267
dudmuck 0:8f0d0ae0a077 2268 LoRaMacStatus_t LoRaMacQueryTxPossible( uint8_t size, LoRaMacTxInfo_t* txInfo )
dudmuck 0:8f0d0ae0a077 2269 {
dudmuck 0:8f0d0ae0a077 2270 uint8_t fOptLen = MacCommandsBufferIndex + MacCommandsBufferToRepeatIndex;
dudmuck 0:8f0d0ae0a077 2271
dudmuck 0:8f0d0ae0a077 2272 if( txInfo == NULL )
dudmuck 0:8f0d0ae0a077 2273 {
dudmuck 0:8f0d0ae0a077 2274 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2275 }
dudmuck 0:8f0d0ae0a077 2276
dudmuck 0:8f0d0ae0a077 2277 txInfo->CurrentPayloadSize = 255;
dudmuck 0:8f0d0ae0a077 2278
dudmuck 0:8f0d0ae0a077 2279 if( txInfo->CurrentPayloadSize >= fOptLen )
dudmuck 0:8f0d0ae0a077 2280 {
dudmuck 0:8f0d0ae0a077 2281 txInfo->MaxPossiblePayload = txInfo->CurrentPayloadSize - fOptLen;
dudmuck 0:8f0d0ae0a077 2282 }
dudmuck 0:8f0d0ae0a077 2283 else
dudmuck 0:8f0d0ae0a077 2284 {
dudmuck 0:8f0d0ae0a077 2285 return LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR;
dudmuck 0:8f0d0ae0a077 2286 }
dudmuck 0:8f0d0ae0a077 2287
dudmuck 0:8f0d0ae0a077 2288 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2289 }
dudmuck 0:8f0d0ae0a077 2290
dudmuck 0:8f0d0ae0a077 2291 LoRaMacStatus_t LoRaMacMibGetRequestConfirm( MibRequestConfirm_t *mibGet )
dudmuck 0:8f0d0ae0a077 2292 {
dudmuck 0:8f0d0ae0a077 2293 LoRaMacStatus_t status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2294
dudmuck 0:8f0d0ae0a077 2295 if( mibGet == NULL )
dudmuck 0:8f0d0ae0a077 2296 {
dudmuck 0:8f0d0ae0a077 2297 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2298 }
dudmuck 0:8f0d0ae0a077 2299
dudmuck 0:8f0d0ae0a077 2300 switch( mibGet->Type )
dudmuck 0:8f0d0ae0a077 2301 {
dudmuck 0:8f0d0ae0a077 2302 case MIB_DEVICE_CLASS:
dudmuck 0:8f0d0ae0a077 2303 {
dudmuck 0:8f0d0ae0a077 2304 mibGet->Param.Class = LoRaMacDeviceClass;
dudmuck 0:8f0d0ae0a077 2305 break;
dudmuck 0:8f0d0ae0a077 2306 }
dudmuck 0:8f0d0ae0a077 2307 case MIB_NETWORK_JOINED:
dudmuck 0:8f0d0ae0a077 2308 {
dudmuck 0:8f0d0ae0a077 2309 mibGet->Param.IsNetworkJoined = IsLoRaMacNetworkJoined;
dudmuck 0:8f0d0ae0a077 2310 break;
dudmuck 0:8f0d0ae0a077 2311 }
dudmuck 0:8f0d0ae0a077 2312 case MIB_NET_ID:
dudmuck 0:8f0d0ae0a077 2313 {
dudmuck 0:8f0d0ae0a077 2314 mibGet->Param.NetID = LoRaMacNetID;
dudmuck 0:8f0d0ae0a077 2315 break;
dudmuck 0:8f0d0ae0a077 2316 }
dudmuck 0:8f0d0ae0a077 2317 case MIB_DEV_ADDR:
dudmuck 0:8f0d0ae0a077 2318 {
dudmuck 0:8f0d0ae0a077 2319 mibGet->Param.DevAddr = LoRaMacDevAddr;
dudmuck 0:8f0d0ae0a077 2320 break;
dudmuck 0:8f0d0ae0a077 2321 }
dudmuck 0:8f0d0ae0a077 2322 case MIB_NWK_SKEY:
dudmuck 0:8f0d0ae0a077 2323 {
dudmuck 0:8f0d0ae0a077 2324 mibGet->Param.NwkSKey = LoRaMacNwkSKey;
dudmuck 0:8f0d0ae0a077 2325 break;
dudmuck 0:8f0d0ae0a077 2326 }
dudmuck 0:8f0d0ae0a077 2327 case MIB_APP_SKEY:
dudmuck 0:8f0d0ae0a077 2328 {
dudmuck 0:8f0d0ae0a077 2329 mibGet->Param.AppSKey = LoRaMacAppSKey;
dudmuck 0:8f0d0ae0a077 2330 break;
dudmuck 0:8f0d0ae0a077 2331 }
dudmuck 0:8f0d0ae0a077 2332 case MIB_PUBLIC_NETWORK:
dudmuck 0:8f0d0ae0a077 2333 {
dudmuck 0:8f0d0ae0a077 2334 mibGet->Param.EnablePublicNetwork = PublicNetwork;
dudmuck 0:8f0d0ae0a077 2335 break;
dudmuck 0:8f0d0ae0a077 2336 }
dudmuck 0:8f0d0ae0a077 2337 case MIB_CHANNELS_NB_REP:
dudmuck 0:8f0d0ae0a077 2338 {
dudmuck 0:8f0d0ae0a077 2339 mibGet->Param.ChannelNbRep = LoRaMacParams.ChannelsNbRep;
dudmuck 0:8f0d0ae0a077 2340 break;
dudmuck 0:8f0d0ae0a077 2341 }
dudmuck 0:8f0d0ae0a077 2342 case MIB_MAX_RX_WINDOW_DURATION:
dudmuck 0:8f0d0ae0a077 2343 {
dudmuck 0:8f0d0ae0a077 2344 mibGet->Param.MaxRxWindow = LoRaMacParams.MaxRxWindow;
dudmuck 0:8f0d0ae0a077 2345 break;
dudmuck 0:8f0d0ae0a077 2346 }
dudmuck 0:8f0d0ae0a077 2347 case MIB_CHANNELS_DEFAULT_TX_POWER:
dudmuck 0:8f0d0ae0a077 2348 {
dudmuck 0:8f0d0ae0a077 2349 mibGet->Param.ChannelsDefaultTxPower = LoRaMacParamsDefaults.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 2350 break;
dudmuck 0:8f0d0ae0a077 2351 }
dudmuck 0:8f0d0ae0a077 2352 case MIB_CHANNELS_TX_POWER:
dudmuck 0:8f0d0ae0a077 2353 {
dudmuck 0:8f0d0ae0a077 2354 mibGet->Param.ChannelsTxPower = LoRaMacParams.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 2355 break;
dudmuck 0:8f0d0ae0a077 2356 }
dudmuck 0:8f0d0ae0a077 2357 case MIB_UPLINK_COUNTER:
dudmuck 0:8f0d0ae0a077 2358 {
dudmuck 0:8f0d0ae0a077 2359 mibGet->Param.UpLinkCounter = UpLinkCounter;
dudmuck 0:8f0d0ae0a077 2360 break;
dudmuck 0:8f0d0ae0a077 2361 }
dudmuck 0:8f0d0ae0a077 2362 case MIB_DOWNLINK_COUNTER:
dudmuck 0:8f0d0ae0a077 2363 {
dudmuck 0:8f0d0ae0a077 2364 mibGet->Param.DownLinkCounter = DownLinkCounter;
dudmuck 0:8f0d0ae0a077 2365 break;
dudmuck 0:8f0d0ae0a077 2366 }
dudmuck 0:8f0d0ae0a077 2367 case MIB_MULTICAST_CHANNEL:
dudmuck 0:8f0d0ae0a077 2368 {
dudmuck 0:8f0d0ae0a077 2369 mibGet->Param.MulticastList = MulticastChannels;
dudmuck 0:8f0d0ae0a077 2370 break;
dudmuck 0:8f0d0ae0a077 2371 }
dudmuck 0:8f0d0ae0a077 2372 case MIB_SYSTEM_MAX_RX_ERROR:
dudmuck 0:8f0d0ae0a077 2373 {
dudmuck 0:8f0d0ae0a077 2374 mibGet->Param.SystemMaxRxError = LoRaMacParams.SystemMaxRxError;
dudmuck 0:8f0d0ae0a077 2375 break;
dudmuck 0:8f0d0ae0a077 2376 }
dudmuck 0:8f0d0ae0a077 2377 case MIB_MIN_RX_SYMBOLS:
dudmuck 0:8f0d0ae0a077 2378 {
dudmuck 0:8f0d0ae0a077 2379 mibGet->Param.MinRxSymbols = LoRaMacParams.MinRxSymbols;
dudmuck 0:8f0d0ae0a077 2380 break;
dudmuck 0:8f0d0ae0a077 2381 }
dudmuck 0:8f0d0ae0a077 2382 default:
dudmuck 0:8f0d0ae0a077 2383 status = LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 2384 break;
dudmuck 0:8f0d0ae0a077 2385 }
dudmuck 0:8f0d0ae0a077 2386
dudmuck 0:8f0d0ae0a077 2387 return status;
dudmuck 0:8f0d0ae0a077 2388 }
dudmuck 0:8f0d0ae0a077 2389
dudmuck 0:8f0d0ae0a077 2390 LoRaMacStatus_t LoRaMacMibSetRequestConfirm( MibRequestConfirm_t *mibSet )
dudmuck 0:8f0d0ae0a077 2391 {
dudmuck 0:8f0d0ae0a077 2392 LoRaMacStatus_t status = LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2393
dudmuck 0:8f0d0ae0a077 2394 if( mibSet == NULL )
dudmuck 0:8f0d0ae0a077 2395 {
dudmuck 0:8f0d0ae0a077 2396 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2397 }
dudmuck 0:8f0d0ae0a077 2398 if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
dudmuck 0:8f0d0ae0a077 2399 {
dudmuck 0:8f0d0ae0a077 2400 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2401 }
dudmuck 0:8f0d0ae0a077 2402
dudmuck 0:8f0d0ae0a077 2403 switch( mibSet->Type )
dudmuck 0:8f0d0ae0a077 2404 {
dudmuck 0:8f0d0ae0a077 2405 case MIB_DEVICE_CLASS:
dudmuck 0:8f0d0ae0a077 2406 {
dudmuck 0:8f0d0ae0a077 2407 LoRaMacDeviceClass = mibSet->Param.Class;
dudmuck 0:8f0d0ae0a077 2408 switch( LoRaMacDeviceClass )
dudmuck 0:8f0d0ae0a077 2409 {
dudmuck 0:8f0d0ae0a077 2410 case CLASS_A:
dudmuck 0:8f0d0ae0a077 2411 {
dudmuck 0:8f0d0ae0a077 2412 // Set the radio into sleep to setup a defined state
dudmuck 0:8f0d0ae0a077 2413 Radio.Sleep( );
dudmuck 0:8f0d0ae0a077 2414 break;
dudmuck 0:8f0d0ae0a077 2415 }
dudmuck 0:8f0d0ae0a077 2416 case CLASS_B:
dudmuck 0:8f0d0ae0a077 2417 {
dudmuck 0:8f0d0ae0a077 2418 break;
dudmuck 0:8f0d0ae0a077 2419 }
dudmuck 0:8f0d0ae0a077 2420 case CLASS_D:
dudmuck 1:53c30224eda8 2421 isr_printf("TODO MIB_DEVICE_CLASS:D\r\n");
dudmuck 0:8f0d0ae0a077 2422 break;
dudmuck 0:8f0d0ae0a077 2423 }
dudmuck 0:8f0d0ae0a077 2424 break;
dudmuck 0:8f0d0ae0a077 2425 }
dudmuck 0:8f0d0ae0a077 2426 case MIB_NETWORK_JOINED:
dudmuck 0:8f0d0ae0a077 2427 {
dudmuck 0:8f0d0ae0a077 2428 IsLoRaMacNetworkJoined = mibSet->Param.IsNetworkJoined;
dudmuck 0:8f0d0ae0a077 2429 break;
dudmuck 0:8f0d0ae0a077 2430 }
dudmuck 0:8f0d0ae0a077 2431 case MIB_NET_ID:
dudmuck 0:8f0d0ae0a077 2432 {
dudmuck 0:8f0d0ae0a077 2433 LoRaMacNetID = mibSet->Param.NetID;
dudmuck 0:8f0d0ae0a077 2434 break;
dudmuck 0:8f0d0ae0a077 2435 }
dudmuck 0:8f0d0ae0a077 2436 case MIB_DEV_ADDR:
dudmuck 0:8f0d0ae0a077 2437 {
dudmuck 0:8f0d0ae0a077 2438 LoRaMacDevAddr = mibSet->Param.DevAddr;
dudmuck 0:8f0d0ae0a077 2439 break;
dudmuck 0:8f0d0ae0a077 2440 }
dudmuck 0:8f0d0ae0a077 2441 case MIB_NWK_SKEY:
dudmuck 0:8f0d0ae0a077 2442 {
dudmuck 0:8f0d0ae0a077 2443 if( mibSet->Param.NwkSKey != NULL )
dudmuck 0:8f0d0ae0a077 2444 {
dudmuck 0:8f0d0ae0a077 2445 memcpy1( LoRaMacNwkSKey, mibSet->Param.NwkSKey,
dudmuck 0:8f0d0ae0a077 2446 sizeof( LoRaMacNwkSKey ) );
dudmuck 0:8f0d0ae0a077 2447 }
dudmuck 0:8f0d0ae0a077 2448 else
dudmuck 0:8f0d0ae0a077 2449 {
dudmuck 0:8f0d0ae0a077 2450 status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2451 }
dudmuck 0:8f0d0ae0a077 2452 break;
dudmuck 0:8f0d0ae0a077 2453 }
dudmuck 0:8f0d0ae0a077 2454 case MIB_APP_SKEY:
dudmuck 0:8f0d0ae0a077 2455 {
dudmuck 0:8f0d0ae0a077 2456 if( mibSet->Param.AppSKey != NULL )
dudmuck 0:8f0d0ae0a077 2457 {
dudmuck 0:8f0d0ae0a077 2458 memcpy1( LoRaMacAppSKey, mibSet->Param.AppSKey,
dudmuck 0:8f0d0ae0a077 2459 sizeof( LoRaMacAppSKey ) );
dudmuck 0:8f0d0ae0a077 2460 }
dudmuck 0:8f0d0ae0a077 2461 else
dudmuck 0:8f0d0ae0a077 2462 {
dudmuck 0:8f0d0ae0a077 2463 status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2464 }
dudmuck 0:8f0d0ae0a077 2465 break;
dudmuck 0:8f0d0ae0a077 2466 }
dudmuck 0:8f0d0ae0a077 2467 case MIB_PUBLIC_NETWORK:
dudmuck 0:8f0d0ae0a077 2468 {
dudmuck 0:8f0d0ae0a077 2469 PublicNetwork = mibSet->Param.EnablePublicNetwork;
dudmuck 0:8f0d0ae0a077 2470 Radio.SetPublicNetwork( PublicNetwork );
dudmuck 0:8f0d0ae0a077 2471 break;
dudmuck 0:8f0d0ae0a077 2472 }
dudmuck 0:8f0d0ae0a077 2473 case MIB_CHANNELS_NB_REP:
dudmuck 0:8f0d0ae0a077 2474 {
dudmuck 0:8f0d0ae0a077 2475 if( ( mibSet->Param.ChannelNbRep >= 1 ) &&
dudmuck 0:8f0d0ae0a077 2476 ( mibSet->Param.ChannelNbRep <= 15 ) )
dudmuck 0:8f0d0ae0a077 2477 {
dudmuck 0:8f0d0ae0a077 2478 LoRaMacParams.ChannelsNbRep = mibSet->Param.ChannelNbRep;
dudmuck 0:8f0d0ae0a077 2479 }
dudmuck 0:8f0d0ae0a077 2480 else
dudmuck 0:8f0d0ae0a077 2481 {
dudmuck 0:8f0d0ae0a077 2482 status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2483 }
dudmuck 0:8f0d0ae0a077 2484 break;
dudmuck 0:8f0d0ae0a077 2485 }
dudmuck 0:8f0d0ae0a077 2486 case MIB_MAX_RX_WINDOW_DURATION:
dudmuck 0:8f0d0ae0a077 2487 {
dudmuck 0:8f0d0ae0a077 2488 LoRaMacParams.MaxRxWindow = mibSet->Param.MaxRxWindow;
dudmuck 0:8f0d0ae0a077 2489 break;
dudmuck 0:8f0d0ae0a077 2490 }
dudmuck 0:8f0d0ae0a077 2491 case MIB_CHANNELS_DEFAULT_TX_POWER:
dudmuck 0:8f0d0ae0a077 2492 {
dudmuck 0:8f0d0ae0a077 2493 if( ValueInRange( mibSet->Param.ChannelsDefaultTxPower,
dudmuck 0:8f0d0ae0a077 2494 LORAMAC_MAX_TX_POWER, LORAMAC_MIN_TX_POWER ) )
dudmuck 0:8f0d0ae0a077 2495 {
dudmuck 0:8f0d0ae0a077 2496 LoRaMacParamsDefaults.ChannelsTxPower = mibSet->Param.ChannelsDefaultTxPower;
dudmuck 0:8f0d0ae0a077 2497 }
dudmuck 0:8f0d0ae0a077 2498 else
dudmuck 0:8f0d0ae0a077 2499 {
dudmuck 0:8f0d0ae0a077 2500 status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2501 }
dudmuck 0:8f0d0ae0a077 2502 break;
dudmuck 0:8f0d0ae0a077 2503 }
dudmuck 0:8f0d0ae0a077 2504 case MIB_CHANNELS_TX_POWER:
dudmuck 0:8f0d0ae0a077 2505 {
dudmuck 0:8f0d0ae0a077 2506 if( ValueInRange( mibSet->Param.ChannelsTxPower,
dudmuck 0:8f0d0ae0a077 2507 LORAMAC_MAX_TX_POWER, LORAMAC_MIN_TX_POWER ) )
dudmuck 0:8f0d0ae0a077 2508 {
dudmuck 0:8f0d0ae0a077 2509 LoRaMacParams.ChannelsTxPower = mibSet->Param.ChannelsTxPower;
dudmuck 0:8f0d0ae0a077 2510 }
dudmuck 0:8f0d0ae0a077 2511 else
dudmuck 0:8f0d0ae0a077 2512 {
dudmuck 0:8f0d0ae0a077 2513 status = LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2514 }
dudmuck 0:8f0d0ae0a077 2515 break;
dudmuck 0:8f0d0ae0a077 2516 }
dudmuck 0:8f0d0ae0a077 2517 case MIB_UPLINK_COUNTER:
dudmuck 0:8f0d0ae0a077 2518 {
dudmuck 0:8f0d0ae0a077 2519 UpLinkCounter = mibSet->Param.UpLinkCounter;
dudmuck 0:8f0d0ae0a077 2520 break;
dudmuck 0:8f0d0ae0a077 2521 }
dudmuck 0:8f0d0ae0a077 2522 case MIB_DOWNLINK_COUNTER:
dudmuck 0:8f0d0ae0a077 2523 {
dudmuck 0:8f0d0ae0a077 2524 DownLinkCounter = mibSet->Param.DownLinkCounter;
dudmuck 0:8f0d0ae0a077 2525 break;
dudmuck 0:8f0d0ae0a077 2526 }
dudmuck 0:8f0d0ae0a077 2527 case MIB_SYSTEM_MAX_RX_ERROR:
dudmuck 0:8f0d0ae0a077 2528 {
dudmuck 0:8f0d0ae0a077 2529 LoRaMacParams.SystemMaxRxError = LoRaMacParamsDefaults.SystemMaxRxError = mibSet->Param.SystemMaxRxError;
dudmuck 0:8f0d0ae0a077 2530 break;
dudmuck 0:8f0d0ae0a077 2531 }
dudmuck 0:8f0d0ae0a077 2532 case MIB_MIN_RX_SYMBOLS:
dudmuck 0:8f0d0ae0a077 2533 {
dudmuck 0:8f0d0ae0a077 2534 LoRaMacParams.MinRxSymbols = LoRaMacParamsDefaults.MinRxSymbols = mibSet->Param.MinRxSymbols;
dudmuck 0:8f0d0ae0a077 2535 break;
dudmuck 0:8f0d0ae0a077 2536 }
dudmuck 0:8f0d0ae0a077 2537 default:
dudmuck 0:8f0d0ae0a077 2538 status = LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 2539 break;
dudmuck 0:8f0d0ae0a077 2540 }
dudmuck 0:8f0d0ae0a077 2541
dudmuck 0:8f0d0ae0a077 2542 return status;
dudmuck 0:8f0d0ae0a077 2543 }
dudmuck 0:8f0d0ae0a077 2544
dudmuck 0:8f0d0ae0a077 2545 LoRaMacStatus_t LoRaMacMulticastChannelLink( MulticastParams_t *channelParam )
dudmuck 0:8f0d0ae0a077 2546 {
dudmuck 0:8f0d0ae0a077 2547 if( channelParam == NULL )
dudmuck 0:8f0d0ae0a077 2548 {
dudmuck 0:8f0d0ae0a077 2549 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2550 }
dudmuck 0:8f0d0ae0a077 2551 if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
dudmuck 0:8f0d0ae0a077 2552 {
dudmuck 0:8f0d0ae0a077 2553 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2554 }
dudmuck 0:8f0d0ae0a077 2555
dudmuck 0:8f0d0ae0a077 2556 // Reset downlink counter
dudmuck 0:8f0d0ae0a077 2557 channelParam->DownLinkCounter = 0;
dudmuck 0:8f0d0ae0a077 2558
dudmuck 0:8f0d0ae0a077 2559 if( MulticastChannels == NULL )
dudmuck 0:8f0d0ae0a077 2560 {
dudmuck 0:8f0d0ae0a077 2561 // New node is the fist element
dudmuck 0:8f0d0ae0a077 2562 MulticastChannels = channelParam;
dudmuck 0:8f0d0ae0a077 2563 }
dudmuck 0:8f0d0ae0a077 2564 else
dudmuck 0:8f0d0ae0a077 2565 {
dudmuck 0:8f0d0ae0a077 2566 MulticastParams_t *cur = MulticastChannels;
dudmuck 0:8f0d0ae0a077 2567
dudmuck 0:8f0d0ae0a077 2568 // Search the last node in the list
dudmuck 0:8f0d0ae0a077 2569 while( cur->Next != NULL )
dudmuck 0:8f0d0ae0a077 2570 {
dudmuck 0:8f0d0ae0a077 2571 cur = cur->Next;
dudmuck 0:8f0d0ae0a077 2572 }
dudmuck 0:8f0d0ae0a077 2573 // This function always finds the last node
dudmuck 0:8f0d0ae0a077 2574 cur->Next = channelParam;
dudmuck 0:8f0d0ae0a077 2575 }
dudmuck 0:8f0d0ae0a077 2576
dudmuck 0:8f0d0ae0a077 2577 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2578 }
dudmuck 0:8f0d0ae0a077 2579
dudmuck 0:8f0d0ae0a077 2580 LoRaMacStatus_t LoRaMacMulticastChannelUnlink( MulticastParams_t *channelParam )
dudmuck 0:8f0d0ae0a077 2581 {
dudmuck 0:8f0d0ae0a077 2582 if( channelParam == NULL )
dudmuck 0:8f0d0ae0a077 2583 {
dudmuck 0:8f0d0ae0a077 2584 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2585 }
dudmuck 0:8f0d0ae0a077 2586 if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
dudmuck 0:8f0d0ae0a077 2587 {
dudmuck 0:8f0d0ae0a077 2588 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2589 }
dudmuck 0:8f0d0ae0a077 2590
dudmuck 0:8f0d0ae0a077 2591 if( MulticastChannels != NULL )
dudmuck 0:8f0d0ae0a077 2592 {
dudmuck 0:8f0d0ae0a077 2593 if( MulticastChannels == channelParam )
dudmuck 0:8f0d0ae0a077 2594 {
dudmuck 0:8f0d0ae0a077 2595 // First element
dudmuck 0:8f0d0ae0a077 2596 MulticastChannels = channelParam->Next;
dudmuck 0:8f0d0ae0a077 2597 }
dudmuck 0:8f0d0ae0a077 2598 else
dudmuck 0:8f0d0ae0a077 2599 {
dudmuck 0:8f0d0ae0a077 2600 MulticastParams_t *cur = MulticastChannels;
dudmuck 0:8f0d0ae0a077 2601
dudmuck 0:8f0d0ae0a077 2602 // Search the node in the list
dudmuck 0:8f0d0ae0a077 2603 while( cur->Next && cur->Next != channelParam )
dudmuck 0:8f0d0ae0a077 2604 {
dudmuck 0:8f0d0ae0a077 2605 cur = cur->Next;
dudmuck 0:8f0d0ae0a077 2606 }
dudmuck 0:8f0d0ae0a077 2607 // If we found the node, remove it
dudmuck 0:8f0d0ae0a077 2608 if( cur->Next )
dudmuck 0:8f0d0ae0a077 2609 {
dudmuck 0:8f0d0ae0a077 2610 cur->Next = channelParam->Next;
dudmuck 0:8f0d0ae0a077 2611 }
dudmuck 0:8f0d0ae0a077 2612 }
dudmuck 0:8f0d0ae0a077 2613 channelParam->Next = NULL;
dudmuck 0:8f0d0ae0a077 2614 }
dudmuck 0:8f0d0ae0a077 2615
dudmuck 0:8f0d0ae0a077 2616 return LORAMAC_STATUS_OK;
dudmuck 0:8f0d0ae0a077 2617 }
dudmuck 0:8f0d0ae0a077 2618
dudmuck 0:8f0d0ae0a077 2619 LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t *mlmeRequest )
dudmuck 0:8f0d0ae0a077 2620 {
dudmuck 0:8f0d0ae0a077 2621 LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 2622 LoRaMacHeader_t macHdr;
dudmuck 0:8f0d0ae0a077 2623
dudmuck 0:8f0d0ae0a077 2624 if( mlmeRequest == NULL )
dudmuck 0:8f0d0ae0a077 2625 {
dudmuck 0:8f0d0ae0a077 2626 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2627 }
dudmuck 0:8f0d0ae0a077 2628 if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
dudmuck 0:8f0d0ae0a077 2629 {
dudmuck 0:8f0d0ae0a077 2630 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2631 }
dudmuck 0:8f0d0ae0a077 2632
dudmuck 0:8f0d0ae0a077 2633 memset1( ( uint8_t* ) &MlmeConfirm, 0, sizeof( MlmeConfirm ) );
dudmuck 0:8f0d0ae0a077 2634
dudmuck 2:f2d9aa163652 2635 MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_MLMEREQ;
dudmuck 0:8f0d0ae0a077 2636
dudmuck 0:8f0d0ae0a077 2637 switch( mlmeRequest->Type )
dudmuck 0:8f0d0ae0a077 2638 {
dudmuck 0:8f0d0ae0a077 2639 case MLME_JOIN:
dudmuck 0:8f0d0ae0a077 2640 {
dudmuck 0:8f0d0ae0a077 2641 if( ( LoRaMacState & LORAMAC_TX_DELAYED ) == LORAMAC_TX_DELAYED )
dudmuck 0:8f0d0ae0a077 2642 {
dudmuck 0:8f0d0ae0a077 2643 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2644 }
dudmuck 0:8f0d0ae0a077 2645
dudmuck 0:8f0d0ae0a077 2646 if( ( mlmeRequest->Req.Join.DevEui == NULL ) ||
dudmuck 0:8f0d0ae0a077 2647 ( mlmeRequest->Req.Join.AppEui == NULL ) ||
dudmuck 0:8f0d0ae0a077 2648 ( mlmeRequest->Req.Join.AppKey == NULL ) ||
dudmuck 0:8f0d0ae0a077 2649 ( mlmeRequest->Req.Join.NbTrials == 0 ) )
dudmuck 0:8f0d0ae0a077 2650 {
dudmuck 0:8f0d0ae0a077 2651 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2652 }
dudmuck 0:8f0d0ae0a077 2653
dudmuck 0:8f0d0ae0a077 2654 // Enables at least the usage of all datarates.
dudmuck 0:8f0d0ae0a077 2655 if( mlmeRequest->Req.Join.NbTrials < 48 )
dudmuck 0:8f0d0ae0a077 2656 {
dudmuck 0:8f0d0ae0a077 2657 mlmeRequest->Req.Join.NbTrials = 48;
dudmuck 0:8f0d0ae0a077 2658 }
dudmuck 0:8f0d0ae0a077 2659
dudmuck 0:8f0d0ae0a077 2660 LoRaMacFlags.Bits.MlmeReq = 1;
dudmuck 0:8f0d0ae0a077 2661 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
dudmuck 0:8f0d0ae0a077 2662
dudmuck 0:8f0d0ae0a077 2663 LoRaMacDevEui = mlmeRequest->Req.Join.DevEui;
dudmuck 0:8f0d0ae0a077 2664 LoRaMacAppEui = mlmeRequest->Req.Join.AppEui;
dudmuck 0:8f0d0ae0a077 2665 LoRaMacAppKey = mlmeRequest->Req.Join.AppKey;
dudmuck 0:8f0d0ae0a077 2666 MaxJoinRequestTrials = mlmeRequest->Req.Join.NbTrials;
dudmuck 0:8f0d0ae0a077 2667
dudmuck 0:8f0d0ae0a077 2668 // Reset variable JoinRequestTrials
dudmuck 0:8f0d0ae0a077 2669 JoinRequestTrials = 0;
dudmuck 0:8f0d0ae0a077 2670
dudmuck 0:8f0d0ae0a077 2671 // Setup header information
dudmuck 0:8f0d0ae0a077 2672 macHdr.Value = 0;
dudmuck 0:8f0d0ae0a077 2673 macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ;
dudmuck 0:8f0d0ae0a077 2674
dudmuck 0:8f0d0ae0a077 2675 ResetMacParameters( );
dudmuck 0:8f0d0ae0a077 2676
dudmuck 0:8f0d0ae0a077 2677 Channel = 0; // start with first channel
dudmuck 1:53c30224eda8 2678 isr_printf("<ch0>");
dudmuck 1:53c30224eda8 2679
dudmuck 1:53c30224eda8 2680 isr_printf("mlme-join-send ch%u\r\n", Channel);
dudmuck 0:8f0d0ae0a077 2681 status = Send( &macHdr, 0, NULL, 0 );
dudmuck 0:8f0d0ae0a077 2682 break;
dudmuck 0:8f0d0ae0a077 2683 }
dudmuck 0:8f0d0ae0a077 2684 case MLME_LINK_CHECK:
dudmuck 0:8f0d0ae0a077 2685 {
dudmuck 0:8f0d0ae0a077 2686 LoRaMacFlags.Bits.MlmeReq = 1;
dudmuck 0:8f0d0ae0a077 2687 // LoRaMac will send this command piggy-pack
dudmuck 0:8f0d0ae0a077 2688 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
dudmuck 0:8f0d0ae0a077 2689
dudmuck 0:8f0d0ae0a077 2690 status = AddMacCommand( MOTE_MAC_LINK_CHECK_REQ, 0, 0 );
dudmuck 0:8f0d0ae0a077 2691 break;
dudmuck 0:8f0d0ae0a077 2692 }
dudmuck 0:8f0d0ae0a077 2693 case MLME_TXCW:
dudmuck 0:8f0d0ae0a077 2694 {
dudmuck 0:8f0d0ae0a077 2695 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
dudmuck 0:8f0d0ae0a077 2696 LoRaMacFlags.Bits.MlmeReq = 1;
dudmuck 0:8f0d0ae0a077 2697 status = SetTxContinuousWave( mlmeRequest->Req.TxCw.Timeout );
dudmuck 0:8f0d0ae0a077 2698 break;
dudmuck 0:8f0d0ae0a077 2699 }
dudmuck 0:8f0d0ae0a077 2700 case MLME_TXCW_1:
dudmuck 0:8f0d0ae0a077 2701 {
dudmuck 0:8f0d0ae0a077 2702 MlmeConfirm.MlmeRequest = mlmeRequest->Type;
dudmuck 0:8f0d0ae0a077 2703 LoRaMacFlags.Bits.MlmeReq = 1;
dudmuck 0:8f0d0ae0a077 2704 status = SetTxContinuousWave1( mlmeRequest->Req.TxCw.Timeout, mlmeRequest->Req.TxCw.Frequency, mlmeRequest->Req.TxCw.Power );
dudmuck 0:8f0d0ae0a077 2705 break;
dudmuck 0:8f0d0ae0a077 2706 }
dudmuck 0:8f0d0ae0a077 2707 default:
dudmuck 0:8f0d0ae0a077 2708 break;
dudmuck 0:8f0d0ae0a077 2709 }
dudmuck 0:8f0d0ae0a077 2710
dudmuck 0:8f0d0ae0a077 2711 if( status != LORAMAC_STATUS_OK )
dudmuck 0:8f0d0ae0a077 2712 {
dudmuck 0:8f0d0ae0a077 2713 NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 2714 LoRaMacFlags.Bits.MlmeReq = 0;
dudmuck 0:8f0d0ae0a077 2715 }
dudmuck 0:8f0d0ae0a077 2716
dudmuck 0:8f0d0ae0a077 2717 return status;
dudmuck 0:8f0d0ae0a077 2718 }
dudmuck 0:8f0d0ae0a077 2719
dudmuck 0:8f0d0ae0a077 2720 LoRaMacStatus_t LoRaMacMcpsRequest( McpsReq_t *mcpsRequest )
dudmuck 0:8f0d0ae0a077 2721 {
dudmuck 0:8f0d0ae0a077 2722 LoRaMacStatus_t status = LORAMAC_STATUS_SERVICE_UNKNOWN;
dudmuck 0:8f0d0ae0a077 2723 LoRaMacHeader_t macHdr;
dudmuck 0:8f0d0ae0a077 2724 uint8_t fPort = 0;
dudmuck 0:8f0d0ae0a077 2725 void *fBuffer;
dudmuck 0:8f0d0ae0a077 2726 uint16_t fBufferSize;
dudmuck 0:8f0d0ae0a077 2727 bool readyToSend = false;
dudmuck 0:8f0d0ae0a077 2728
dudmuck 0:8f0d0ae0a077 2729 if( mcpsRequest == NULL )
dudmuck 0:8f0d0ae0a077 2730 {
dudmuck 0:8f0d0ae0a077 2731 return LORAMAC_STATUS_PARAMETER_INVALID;
dudmuck 0:8f0d0ae0a077 2732 }
dudmuck 0:8f0d0ae0a077 2733 if( ( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING ) ||
dudmuck 0:8f0d0ae0a077 2734 ( ( LoRaMacState & LORAMAC_TX_DELAYED ) == LORAMAC_TX_DELAYED ) )
dudmuck 0:8f0d0ae0a077 2735 {
dudmuck 0:8f0d0ae0a077 2736 return LORAMAC_STATUS_BUSY;
dudmuck 0:8f0d0ae0a077 2737 }
dudmuck 0:8f0d0ae0a077 2738
dudmuck 0:8f0d0ae0a077 2739 macHdr.Value = 0;
dudmuck 0:8f0d0ae0a077 2740 memset1 ( ( uint8_t* ) &McpsConfirm, 0, sizeof( McpsConfirm ) );
dudmuck 2:f2d9aa163652 2741 McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_MCPSREQ;
dudmuck 0:8f0d0ae0a077 2742
dudmuck 0:8f0d0ae0a077 2743 switch( mcpsRequest->Type )
dudmuck 0:8f0d0ae0a077 2744 {
dudmuck 0:8f0d0ae0a077 2745 case MCPS_UNCONFIRMED:
dudmuck 0:8f0d0ae0a077 2746 {
dudmuck 0:8f0d0ae0a077 2747 readyToSend = true;
dudmuck 0:8f0d0ae0a077 2748
dudmuck 0:8f0d0ae0a077 2749 macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP;
dudmuck 0:8f0d0ae0a077 2750 fPort = mcpsRequest->Req.Unconfirmed.fPort;
dudmuck 0:8f0d0ae0a077 2751 fBuffer = mcpsRequest->Req.Unconfirmed.fBuffer;
dudmuck 0:8f0d0ae0a077 2752 fBufferSize = mcpsRequest->Req.Unconfirmed.fBufferSize;
dudmuck 0:8f0d0ae0a077 2753 break;
dudmuck 0:8f0d0ae0a077 2754 }
dudmuck 0:8f0d0ae0a077 2755 case MCPS_CONFIRMED:
dudmuck 0:8f0d0ae0a077 2756 {
dudmuck 0:8f0d0ae0a077 2757 readyToSend = true;
dudmuck 0:8f0d0ae0a077 2758
dudmuck 0:8f0d0ae0a077 2759 macHdr.Bits.MType = FRAME_TYPE_DATA_CONFIRMED_UP;
dudmuck 0:8f0d0ae0a077 2760 fPort = mcpsRequest->Req.Confirmed.fPort;
dudmuck 0:8f0d0ae0a077 2761 fBuffer = mcpsRequest->Req.Confirmed.fBuffer;
dudmuck 0:8f0d0ae0a077 2762 fBufferSize = mcpsRequest->Req.Confirmed.fBufferSize;
dudmuck 0:8f0d0ae0a077 2763 break;
dudmuck 0:8f0d0ae0a077 2764 }
dudmuck 0:8f0d0ae0a077 2765 case MCPS_PROPRIETARY:
dudmuck 0:8f0d0ae0a077 2766 {
dudmuck 0:8f0d0ae0a077 2767 readyToSend = true;
dudmuck 0:8f0d0ae0a077 2768
dudmuck 0:8f0d0ae0a077 2769 macHdr.Bits.MType = FRAME_TYPE_PROPRIETARY;
dudmuck 0:8f0d0ae0a077 2770 fBuffer = mcpsRequest->Req.Proprietary.fBuffer;
dudmuck 0:8f0d0ae0a077 2771 fBufferSize = mcpsRequest->Req.Proprietary.fBufferSize;
dudmuck 0:8f0d0ae0a077 2772 break;
dudmuck 0:8f0d0ae0a077 2773 }
dudmuck 0:8f0d0ae0a077 2774 default:
dudmuck 0:8f0d0ae0a077 2775 break;
dudmuck 0:8f0d0ae0a077 2776 }
dudmuck 0:8f0d0ae0a077 2777
dudmuck 0:8f0d0ae0a077 2778 if( readyToSend == true )
dudmuck 0:8f0d0ae0a077 2779 {
dudmuck 0:8f0d0ae0a077 2780 status = Send( &macHdr, fPort, fBuffer, fBufferSize );
dudmuck 0:8f0d0ae0a077 2781 if( status == LORAMAC_STATUS_OK )
dudmuck 0:8f0d0ae0a077 2782 {
dudmuck 0:8f0d0ae0a077 2783 McpsConfirm.McpsRequest = mcpsRequest->Type;
dudmuck 0:8f0d0ae0a077 2784 LoRaMacFlags.Bits.McpsReq = 1;
dudmuck 0:8f0d0ae0a077 2785 }
dudmuck 0:8f0d0ae0a077 2786 else
dudmuck 0:8f0d0ae0a077 2787 {
dudmuck 0:8f0d0ae0a077 2788 NodeAckRequested = false;
dudmuck 0:8f0d0ae0a077 2789 }
dudmuck 0:8f0d0ae0a077 2790 }
dudmuck 0:8f0d0ae0a077 2791
dudmuck 0:8f0d0ae0a077 2792 return status;
dudmuck 0:8f0d0ae0a077 2793 }
dudmuck 0:8f0d0ae0a077 2794
dudmuck 0:8f0d0ae0a077 2795 void LoRaMacTestRxWindowsOn( bool enable )
dudmuck 0:8f0d0ae0a077 2796 {
dudmuck 0:8f0d0ae0a077 2797 IsRxWindowsEnabled = enable;
dudmuck 0:8f0d0ae0a077 2798 }
dudmuck 0:8f0d0ae0a077 2799
dudmuck 0:8f0d0ae0a077 2800 void LoRaMacTestSetMic( uint16_t txPacketCounter )
dudmuck 0:8f0d0ae0a077 2801 {
dudmuck 0:8f0d0ae0a077 2802 UpLinkCounter = txPacketCounter;
dudmuck 0:8f0d0ae0a077 2803 IsUpLinkCounterFixed = true;
dudmuck 0:8f0d0ae0a077 2804 }
dudmuck 0:8f0d0ae0a077 2805
dudmuck 0:8f0d0ae0a077 2806 void LoRaMacTestSetChannel( uint8_t channel )
dudmuck 0:8f0d0ae0a077 2807 {
dudmuck 1:53c30224eda8 2808 isr_printf("set-testch%u\r\n", channel);
dudmuck 0:8f0d0ae0a077 2809 Channel = channel;
dudmuck 0:8f0d0ae0a077 2810 }
dudmuck 0:8f0d0ae0a077 2811
dudmuck 0:8f0d0ae0a077 2812
dudmuck 0:8f0d0ae0a077 2813 static RxConfigParams_t ComputeRxWindowParameters( int8_t datarate, uint32_t rxError )
dudmuck 0:8f0d0ae0a077 2814 {
dudmuck 0:8f0d0ae0a077 2815 RxConfigParams_t rxConfigParams = { 0, 0, 0, 0 };
dudmuck 0:8f0d0ae0a077 2816 double tSymbol = 0.0;
dudmuck 0:8f0d0ae0a077 2817
dudmuck 0:8f0d0ae0a077 2818 rxConfigParams.Datarate = datarate;
dudmuck 0:8f0d0ae0a077 2819 switch( Bandwidths[datarate] )
dudmuck 0:8f0d0ae0a077 2820 {
dudmuck 0:8f0d0ae0a077 2821 default:
dudmuck 0:8f0d0ae0a077 2822 case 125000:
dudmuck 0:8f0d0ae0a077 2823 rxConfigParams.Bandwidth = 0;
dudmuck 0:8f0d0ae0a077 2824 break;
dudmuck 0:8f0d0ae0a077 2825 case 250000:
dudmuck 0:8f0d0ae0a077 2826 rxConfigParams.Bandwidth = 1;
dudmuck 0:8f0d0ae0a077 2827 break;
dudmuck 0:8f0d0ae0a077 2828 case 500000:
dudmuck 0:8f0d0ae0a077 2829 rxConfigParams.Bandwidth = 2;
dudmuck 0:8f0d0ae0a077 2830 break;
dudmuck 0:8f0d0ae0a077 2831 }
dudmuck 0:8f0d0ae0a077 2832
dudmuck 0:8f0d0ae0a077 2833 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
dudmuck 0:8f0d0ae0a077 2834 if( datarate == DR_7 )
dudmuck 0:8f0d0ae0a077 2835 { // FSK
dudmuck 0:8f0d0ae0a077 2836 tSymbol = ( 1.0 / ( double )Datarates[datarate] ) * 8.0; // 1 symbol equals 1 byte
dudmuck 0:8f0d0ae0a077 2837 }
dudmuck 0:8f0d0ae0a077 2838 else
dudmuck 0:8f0d0ae0a077 2839 #endif
dudmuck 0:8f0d0ae0a077 2840 { // LoRa
dudmuck 0:8f0d0ae0a077 2841 tSymbol = ( ( double )( 1 << Datarates[datarate] ) / ( double )Bandwidths[datarate] ) * 1e3;
dudmuck 0:8f0d0ae0a077 2842 }
dudmuck 0:8f0d0ae0a077 2843
dudmuck 0:8f0d0ae0a077 2844 rxConfigParams.RxWindowTimeout = MAX( ( uint32_t )ceil( ( ( 2 * LoRaMacParams.MinRxSymbols - 8 ) * tSymbol + 2 * rxError ) / tSymbol ), LoRaMacParams.MinRxSymbols ); // Computed number of symbols
dudmuck 0:8f0d0ae0a077 2845
dudmuck 0:8f0d0ae0a077 2846 rxConfigParams.RxOffset = ( int32_t )ceil( ( 4.0 * tSymbol ) - ( ( rxConfigParams.RxWindowTimeout * tSymbol ) / 2.0 ) - RADIO_WAKEUP_TIME );
dudmuck 0:8f0d0ae0a077 2847
dudmuck 0:8f0d0ae0a077 2848 return rxConfigParams;
dudmuck 0:8f0d0ae0a077 2849 }
dudmuck 0:8f0d0ae0a077 2850