Espotel
/
LoRaWAN_ELMO_TxRx_Template
Example Tx Rx LoRa code for Multitech Conduit. Based on Semtech stack for ELMO - ver. 4.1.0.
RadioHandler.cpp@6:71b489e70063, 2016-04-19 (annotated)
- Committer:
- Pasi
- Date:
- Tue Apr 19 21:48:58 2016 +0000
- Revision:
- 6:71b489e70063
- Parent:
- 5:be347c6040c1
Text tweaking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pasi | 5:be347c6040c1 | 1 | /* |
Pasi | 5:be347c6040c1 | 2 | / _____) _ | | |
Pasi | 5:be347c6040c1 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
Pasi | 5:be347c6040c1 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
Pasi | 5:be347c6040c1 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
Pasi | 5:be347c6040c1 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
Pasi | 5:be347c6040c1 | 7 | (C)2013 Semtech |
Pasi | 5:be347c6040c1 | 8 | Description: LoRaMac classA device implementation |
Pasi | 5:be347c6040c1 | 9 | License: Revised BSD License, see LICENSE.TXT file include in the project |
Pasi | 5:be347c6040c1 | 10 | Maintainer: Miguel Luis and Gregory Cristian |
Pasi | 5:be347c6040c1 | 11 | */ |
Pasi | 5:be347c6040c1 | 12 | |
Pasi | 5:be347c6040c1 | 13 | /*! \file classA/LoRaMote/main.c */ |
Pasi | 5:be347c6040c1 | 14 | |
Pasi | 5:be347c6040c1 | 15 | #include <string.h> |
Pasi | 5:be347c6040c1 | 16 | #include <math.h> |
Pasi | 5:be347c6040c1 | 17 | #include "mbed.h" |
Pasi | 5:be347c6040c1 | 18 | |
Pasi | 5:be347c6040c1 | 19 | #include "utilities.h" |
Pasi | 5:be347c6040c1 | 20 | #include "LoRaMac.h" |
Pasi | 5:be347c6040c1 | 21 | #include "Comissioning.h" |
Pasi | 5:be347c6040c1 | 22 | #include "InterruptIn.h" |
Pasi | 5:be347c6040c1 | 23 | #include "RadioHandler.h" |
Pasi | 5:be347c6040c1 | 24 | |
Pasi | 5:be347c6040c1 | 25 | DigitalOut Led2(LED2); |
Pasi | 5:be347c6040c1 | 26 | |
Pasi | 5:be347c6040c1 | 27 | /*! |
Pasi | 5:be347c6040c1 | 28 | * Join requests trials duty cycle. |
Pasi | 5:be347c6040c1 | 29 | */ |
Pasi | 5:be347c6040c1 | 30 | #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us |
Pasi | 5:be347c6040c1 | 31 | |
Pasi | 5:be347c6040c1 | 32 | /*! |
Pasi | 5:be347c6040c1 | 33 | * Defines the application data transmission duty cycle. 5s, value in [us]. |
Pasi | 5:be347c6040c1 | 34 | */ |
Pasi | 5:be347c6040c1 | 35 | #define APP_TX_DUTYCYCLE 10000000 |
Pasi | 5:be347c6040c1 | 36 | |
Pasi | 5:be347c6040c1 | 37 | /*! |
Pasi | 5:be347c6040c1 | 38 | * Defines a random delay for application data transmission duty cycle. 1s, |
Pasi | 5:be347c6040c1 | 39 | * value in [us]. |
Pasi | 5:be347c6040c1 | 40 | */ |
Pasi | 5:be347c6040c1 | 41 | #define APP_TX_DUTYCYCLE_RND 1000000 |
Pasi | 5:be347c6040c1 | 42 | |
Pasi | 5:be347c6040c1 | 43 | /*! |
Pasi | 5:be347c6040c1 | 44 | * LoRaWAN confirmed messages |
Pasi | 5:be347c6040c1 | 45 | */ |
Pasi | 5:be347c6040c1 | 46 | #define LORAWAN_CONFIRMED_MSG_ON false |
Pasi | 5:be347c6040c1 | 47 | |
Pasi | 5:be347c6040c1 | 48 | /*! |
Pasi | 5:be347c6040c1 | 49 | * LoRaWAN Adaptive Data Rate |
Pasi | 5:be347c6040c1 | 50 | * |
Pasi | 5:be347c6040c1 | 51 | * \remark Please note that when ADR is enabled the end-device should be static |
Pasi | 5:be347c6040c1 | 52 | */ |
Pasi | 5:be347c6040c1 | 53 | #define LORAWAN_ADR_ON 1 |
Pasi | 5:be347c6040c1 | 54 | |
Pasi | 5:be347c6040c1 | 55 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 56 | |
Pasi | 5:be347c6040c1 | 57 | #include "LoRaMacTest.h" |
Pasi | 5:be347c6040c1 | 58 | |
Pasi | 5:be347c6040c1 | 59 | /*! |
Pasi | 5:be347c6040c1 | 60 | * LoRaWAN ETSI duty cycle control enable/disable |
Pasi | 5:be347c6040c1 | 61 | * |
Pasi | 5:be347c6040c1 | 62 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
Pasi | 5:be347c6040c1 | 63 | */ |
Pasi | 5:be347c6040c1 | 64 | #define LORAWAN_DUTYCYCLE_ON false |
Pasi | 5:be347c6040c1 | 65 | |
Pasi | 5:be347c6040c1 | 66 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 0 |
Pasi | 5:be347c6040c1 | 67 | |
Pasi | 5:be347c6040c1 | 68 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
Pasi | 5:be347c6040c1 | 69 | |
Pasi | 5:be347c6040c1 | 70 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Pasi | 5:be347c6040c1 | 71 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Pasi | 5:be347c6040c1 | 72 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Pasi | 5:be347c6040c1 | 73 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Pasi | 5:be347c6040c1 | 74 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Pasi | 5:be347c6040c1 | 75 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
Pasi | 5:be347c6040c1 | 76 | |
Pasi | 5:be347c6040c1 | 77 | #endif |
Pasi | 5:be347c6040c1 | 78 | |
Pasi | 5:be347c6040c1 | 79 | #endif |
Pasi | 5:be347c6040c1 | 80 | |
Pasi | 5:be347c6040c1 | 81 | /*! |
Pasi | 5:be347c6040c1 | 82 | * LoRaWAN application port |
Pasi | 5:be347c6040c1 | 83 | */ |
Pasi | 5:be347c6040c1 | 84 | #define LORAWAN_APP_PORT 2 |
Pasi | 5:be347c6040c1 | 85 | |
Pasi | 5:be347c6040c1 | 86 | /*! |
Pasi | 5:be347c6040c1 | 87 | * User application data buffer size |
Pasi | 5:be347c6040c1 | 88 | */ |
Pasi | 5:be347c6040c1 | 89 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 90 | |
Pasi | 5:be347c6040c1 | 91 | #define LORAWAN_APP_DATA_SIZE 16 |
Pasi | 5:be347c6040c1 | 92 | |
Pasi | 5:be347c6040c1 | 93 | #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) |
Pasi | 5:be347c6040c1 | 94 | |
Pasi | 5:be347c6040c1 | 95 | #define LORAWAN_APP_DATA_SIZE 11 |
Pasi | 5:be347c6040c1 | 96 | |
Pasi | 5:be347c6040c1 | 97 | #endif |
Pasi | 5:be347c6040c1 | 98 | |
Pasi | 5:be347c6040c1 | 99 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
Pasi | 5:be347c6040c1 | 100 | |
Pasi | 5:be347c6040c1 | 101 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
Pasi | 5:be347c6040c1 | 102 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
Pasi | 5:be347c6040c1 | 103 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
Pasi | 5:be347c6040c1 | 104 | |
Pasi | 5:be347c6040c1 | 105 | #else |
Pasi | 5:be347c6040c1 | 106 | |
Pasi | 5:be347c6040c1 | 107 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
Pasi | 5:be347c6040c1 | 108 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
Pasi | 5:be347c6040c1 | 109 | |
Pasi | 5:be347c6040c1 | 110 | /*! |
Pasi | 5:be347c6040c1 | 111 | * Device address |
Pasi | 5:be347c6040c1 | 112 | */ |
Pasi | 5:be347c6040c1 | 113 | static uint32_t DevAddr; |
Pasi | 5:be347c6040c1 | 114 | |
Pasi | 5:be347c6040c1 | 115 | #endif |
Pasi | 5:be347c6040c1 | 116 | |
Pasi | 5:be347c6040c1 | 117 | /*! |
Pasi | 5:be347c6040c1 | 118 | * Application port |
Pasi | 5:be347c6040c1 | 119 | */ |
Pasi | 5:be347c6040c1 | 120 | static uint8_t AppPort = LORAWAN_APP_PORT; |
Pasi | 5:be347c6040c1 | 121 | |
Pasi | 5:be347c6040c1 | 122 | /*! |
Pasi | 5:be347c6040c1 | 123 | * User application data size |
Pasi | 5:be347c6040c1 | 124 | */ |
Pasi | 5:be347c6040c1 | 125 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
Pasi | 5:be347c6040c1 | 126 | |
Pasi | 5:be347c6040c1 | 127 | /*! |
Pasi | 5:be347c6040c1 | 128 | * User application data buffer size |
Pasi | 5:be347c6040c1 | 129 | */ |
Pasi | 5:be347c6040c1 | 130 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
Pasi | 5:be347c6040c1 | 131 | |
Pasi | 5:be347c6040c1 | 132 | /*! |
Pasi | 5:be347c6040c1 | 133 | * User application data |
Pasi | 5:be347c6040c1 | 134 | */ |
Pasi | 5:be347c6040c1 | 135 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
Pasi | 5:be347c6040c1 | 136 | |
Pasi | 5:be347c6040c1 | 137 | /*! |
Pasi | 5:be347c6040c1 | 138 | * User application data buffer |
Pasi | 5:be347c6040c1 | 139 | */ |
Pasi | 5:be347c6040c1 | 140 | static uint8_t AppDataBuffer[LORAWAN_APP_DATA_MAX_SIZE]; |
Pasi | 5:be347c6040c1 | 141 | |
Pasi | 5:be347c6040c1 | 142 | /*! |
Pasi | 5:be347c6040c1 | 143 | * Indicates if the node is sending confirmed or unconfirmed messages |
Pasi | 5:be347c6040c1 | 144 | */ |
Pasi | 5:be347c6040c1 | 145 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
Pasi | 5:be347c6040c1 | 146 | |
Pasi | 5:be347c6040c1 | 147 | /*! |
Pasi | 5:be347c6040c1 | 148 | * Defines the application data transmission duty cycle |
Pasi | 5:be347c6040c1 | 149 | */ |
Pasi | 5:be347c6040c1 | 150 | static uint32_t TxDutyCycleTime; |
Pasi | 5:be347c6040c1 | 151 | |
Pasi | 5:be347c6040c1 | 152 | /*! |
Pasi | 5:be347c6040c1 | 153 | * Timer to handle the application data transmission duty cycle |
Pasi | 5:be347c6040c1 | 154 | */ |
Pasi | 5:be347c6040c1 | 155 | static Timeout TxNextPacketTimer; |
Pasi | 5:be347c6040c1 | 156 | |
Pasi | 5:be347c6040c1 | 157 | /*! |
Pasi | 5:be347c6040c1 | 158 | * Timer to handle the state of LED2 |
Pasi | 5:be347c6040c1 | 159 | */ |
Pasi | 5:be347c6040c1 | 160 | static Timeout Led2Timer; |
Pasi | 5:be347c6040c1 | 161 | |
Pasi | 5:be347c6040c1 | 162 | /*! |
Pasi | 5:be347c6040c1 | 163 | * Indicates if a new packet can be sent |
Pasi | 5:be347c6040c1 | 164 | */ |
Pasi | 5:be347c6040c1 | 165 | static bool NextTx = true; |
Pasi | 5:be347c6040c1 | 166 | |
Pasi | 5:be347c6040c1 | 167 | /*! |
Pasi | 5:be347c6040c1 | 168 | * Packet send request |
Pasi | 5:be347c6040c1 | 169 | */ |
Pasi | 5:be347c6040c1 | 170 | static bool PacketTxReq = false; |
Pasi | 5:be347c6040c1 | 171 | |
Pasi | 5:be347c6040c1 | 172 | /*! |
Pasi | 5:be347c6040c1 | 173 | * Periodic packets enabled |
Pasi | 5:be347c6040c1 | 174 | */ |
Pasi | 5:be347c6040c1 | 175 | static bool PeriodicPackets = false; |
Pasi | 5:be347c6040c1 | 176 | /*! |
Pasi | 5:be347c6040c1 | 177 | * Device states |
Pasi | 5:be347c6040c1 | 178 | */ |
Pasi | 5:be347c6040c1 | 179 | static enum eDevicState |
Pasi | 5:be347c6040c1 | 180 | { |
Pasi | 5:be347c6040c1 | 181 | DEVICE_STATE_INIT, |
Pasi | 5:be347c6040c1 | 182 | DEVICE_STATE_JOIN, |
Pasi | 5:be347c6040c1 | 183 | DEVICE_STATE_SEND, |
Pasi | 5:be347c6040c1 | 184 | DEVICE_STATE_CYCLE, |
Pasi | 5:be347c6040c1 | 185 | DEVICE_STATE_SLEEP |
Pasi | 5:be347c6040c1 | 186 | }DeviceState; |
Pasi | 5:be347c6040c1 | 187 | |
Pasi | 5:be347c6040c1 | 188 | /*! |
Pasi | 5:be347c6040c1 | 189 | * LoRaWAN compliance tests support data |
Pasi | 5:be347c6040c1 | 190 | */ |
Pasi | 5:be347c6040c1 | 191 | struct ComplianceTest_s |
Pasi | 5:be347c6040c1 | 192 | { |
Pasi | 5:be347c6040c1 | 193 | bool Running; |
Pasi | 5:be347c6040c1 | 194 | uint8_t State; |
Pasi | 5:be347c6040c1 | 195 | bool IsTxConfirmed; |
Pasi | 5:be347c6040c1 | 196 | uint8_t AppPort; |
Pasi | 5:be347c6040c1 | 197 | uint8_t AppDataSize; |
Pasi | 5:be347c6040c1 | 198 | uint8_t *AppDataBuffer; |
Pasi | 5:be347c6040c1 | 199 | uint16_t DownLinkCounter; |
Pasi | 5:be347c6040c1 | 200 | bool LinkCheck; |
Pasi | 5:be347c6040c1 | 201 | uint8_t DemodMargin; |
Pasi | 5:be347c6040c1 | 202 | uint8_t NbGateways; |
Pasi | 5:be347c6040c1 | 203 | }ComplianceTest; |
Pasi | 5:be347c6040c1 | 204 | |
Pasi | 5:be347c6040c1 | 205 | /*! |
Pasi | 5:be347c6040c1 | 206 | * \brief Prepares the payload of the frame |
Pasi | 5:be347c6040c1 | 207 | */ |
Pasi | 5:be347c6040c1 | 208 | static void PrepareTxFrame( uint8_t port ) |
Pasi | 5:be347c6040c1 | 209 | { |
Pasi | 5:be347c6040c1 | 210 | uint16_t i; |
Pasi | 5:be347c6040c1 | 211 | |
Pasi | 5:be347c6040c1 | 212 | switch( port ) |
Pasi | 5:be347c6040c1 | 213 | { |
Pasi | 5:be347c6040c1 | 214 | case 2: |
Pasi | 5:be347c6040c1 | 215 | { |
Pasi | 5:be347c6040c1 | 216 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 217 | |
Pasi | 5:be347c6040c1 | 218 | for (i=0; i<LORAWAN_APP_DATA_SIZE; i++) |
Pasi | 5:be347c6040c1 | 219 | { |
Pasi | 5:be347c6040c1 | 220 | AppData[i] = AppDataBuffer[i]; |
Pasi | 5:be347c6040c1 | 221 | } |
Pasi | 5:be347c6040c1 | 222 | |
Pasi | 5:be347c6040c1 | 223 | #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) |
Pasi | 5:be347c6040c1 | 224 | |
Pasi | 5:be347c6040c1 | 225 | for (i=0; i<LORAWAN_APP_DATA_SIZE; i++) |
Pasi | 5:be347c6040c1 | 226 | { |
Pasi | 5:be347c6040c1 | 227 | AppData[i] = AppDataBuffer[i]; |
Pasi | 5:be347c6040c1 | 228 | } |
Pasi | 5:be347c6040c1 | 229 | |
Pasi | 5:be347c6040c1 | 230 | #endif |
Pasi | 5:be347c6040c1 | 231 | } |
Pasi | 5:be347c6040c1 | 232 | break; |
Pasi | 5:be347c6040c1 | 233 | case 224: |
Pasi | 5:be347c6040c1 | 234 | if( ComplianceTest.LinkCheck == true ) |
Pasi | 5:be347c6040c1 | 235 | { |
Pasi | 5:be347c6040c1 | 236 | ComplianceTest.LinkCheck = false; |
Pasi | 5:be347c6040c1 | 237 | AppDataSize = 3; |
Pasi | 5:be347c6040c1 | 238 | AppData[0] = 5; |
Pasi | 5:be347c6040c1 | 239 | AppData[1] = ComplianceTest.DemodMargin; |
Pasi | 5:be347c6040c1 | 240 | AppData[2] = ComplianceTest.NbGateways; |
Pasi | 5:be347c6040c1 | 241 | ComplianceTest.State = 1; |
Pasi | 5:be347c6040c1 | 242 | } |
Pasi | 5:be347c6040c1 | 243 | else |
Pasi | 5:be347c6040c1 | 244 | { |
Pasi | 5:be347c6040c1 | 245 | switch( ComplianceTest.State ) |
Pasi | 5:be347c6040c1 | 246 | { |
Pasi | 5:be347c6040c1 | 247 | case 4: |
Pasi | 5:be347c6040c1 | 248 | ComplianceTest.State = 1; |
Pasi | 5:be347c6040c1 | 249 | break; |
Pasi | 5:be347c6040c1 | 250 | case 1: |
Pasi | 5:be347c6040c1 | 251 | AppDataSize = 2; |
Pasi | 5:be347c6040c1 | 252 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
Pasi | 5:be347c6040c1 | 253 | AppData[1] = ComplianceTest.DownLinkCounter; |
Pasi | 5:be347c6040c1 | 254 | break; |
Pasi | 5:be347c6040c1 | 255 | } |
Pasi | 5:be347c6040c1 | 256 | } |
Pasi | 5:be347c6040c1 | 257 | break; |
Pasi | 5:be347c6040c1 | 258 | default: |
Pasi | 5:be347c6040c1 | 259 | break; |
Pasi | 5:be347c6040c1 | 260 | } |
Pasi | 5:be347c6040c1 | 261 | } |
Pasi | 5:be347c6040c1 | 262 | |
Pasi | 5:be347c6040c1 | 263 | /*! |
Pasi | 5:be347c6040c1 | 264 | * \brief Prepares the payload of the frame |
Pasi | 5:be347c6040c1 | 265 | * |
Pasi | 5:be347c6040c1 | 266 | * \retval [0: frame could be send, 1: error] |
Pasi | 5:be347c6040c1 | 267 | */ |
Pasi | 5:be347c6040c1 | 268 | static bool SendFrame( void ) |
Pasi | 5:be347c6040c1 | 269 | { |
Pasi | 5:be347c6040c1 | 270 | McpsReq_t mcpsReq; |
Pasi | 5:be347c6040c1 | 271 | LoRaMacTxInfo_t txInfo; |
Pasi | 5:be347c6040c1 | 272 | |
Pasi | 5:be347c6040c1 | 273 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 274 | { |
Pasi | 5:be347c6040c1 | 275 | // Send empty frame in order to flush MAC commands |
Pasi | 5:be347c6040c1 | 276 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Pasi | 5:be347c6040c1 | 277 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
Pasi | 5:be347c6040c1 | 278 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
Pasi | 5:be347c6040c1 | 279 | mcpsReq.Req.Unconfirmed.Datarate = DR_0; |
Pasi | 5:be347c6040c1 | 280 | } |
Pasi | 5:be347c6040c1 | 281 | else |
Pasi | 5:be347c6040c1 | 282 | { |
Pasi | 5:be347c6040c1 | 283 | if( IsTxConfirmed == false ) |
Pasi | 5:be347c6040c1 | 284 | { |
Pasi | 5:be347c6040c1 | 285 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Pasi | 5:be347c6040c1 | 286 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
Pasi | 5:be347c6040c1 | 287 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
Pasi | 5:be347c6040c1 | 288 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
Pasi | 5:be347c6040c1 | 289 | mcpsReq.Req.Unconfirmed.Datarate = DR_0; |
Pasi | 5:be347c6040c1 | 290 | } |
Pasi | 5:be347c6040c1 | 291 | else |
Pasi | 5:be347c6040c1 | 292 | { |
Pasi | 5:be347c6040c1 | 293 | mcpsReq.Type = MCPS_CONFIRMED; |
Pasi | 5:be347c6040c1 | 294 | mcpsReq.Req.Confirmed.fPort = AppPort; |
Pasi | 5:be347c6040c1 | 295 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
Pasi | 5:be347c6040c1 | 296 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
Pasi | 5:be347c6040c1 | 297 | mcpsReq.Req.Confirmed.NbTrials = 8; |
Pasi | 5:be347c6040c1 | 298 | mcpsReq.Req.Confirmed.Datarate = DR_0; |
Pasi | 5:be347c6040c1 | 299 | } |
Pasi | 5:be347c6040c1 | 300 | } |
Pasi | 5:be347c6040c1 | 301 | |
Pasi | 5:be347c6040c1 | 302 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 303 | { |
Pasi | 5:be347c6040c1 | 304 | return false; |
Pasi | 5:be347c6040c1 | 305 | } |
Pasi | 5:be347c6040c1 | 306 | return true; |
Pasi | 5:be347c6040c1 | 307 | } |
Pasi | 5:be347c6040c1 | 308 | |
Pasi | 5:be347c6040c1 | 309 | /*! |
Pasi | 5:be347c6040c1 | 310 | * \brief Function executed on TxNextPacket Timeout event |
Pasi | 5:be347c6040c1 | 311 | */ |
Pasi | 5:be347c6040c1 | 312 | static void OnTxNextPacketTimerEvent( void ) |
Pasi | 5:be347c6040c1 | 313 | { |
Pasi | 5:be347c6040c1 | 314 | MibRequestConfirm_t mibReq; |
Pasi | 5:be347c6040c1 | 315 | LoRaMacStatus_t status; |
Pasi | 5:be347c6040c1 | 316 | |
Pasi | 5:be347c6040c1 | 317 | TxNextPacketTimer.detach(); |
Pasi | 5:be347c6040c1 | 318 | |
Pasi | 5:be347c6040c1 | 319 | mibReq.Type = MIB_NETWORK_JOINED; |
Pasi | 5:be347c6040c1 | 320 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 321 | |
Pasi | 5:be347c6040c1 | 322 | if( status == LORAMAC_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 323 | { |
Pasi | 5:be347c6040c1 | 324 | if( mibReq.Param.IsNetworkJoined == true ) |
Pasi | 5:be347c6040c1 | 325 | { |
Pasi | 5:be347c6040c1 | 326 | DeviceState = DEVICE_STATE_SEND; |
Pasi | 5:be347c6040c1 | 327 | NextTx = true; |
Pasi | 5:be347c6040c1 | 328 | } |
Pasi | 5:be347c6040c1 | 329 | else |
Pasi | 5:be347c6040c1 | 330 | { |
Pasi | 5:be347c6040c1 | 331 | DeviceState = DEVICE_STATE_JOIN; |
Pasi | 5:be347c6040c1 | 332 | } |
Pasi | 5:be347c6040c1 | 333 | } |
Pasi | 5:be347c6040c1 | 334 | } |
Pasi | 5:be347c6040c1 | 335 | |
Pasi | 5:be347c6040c1 | 336 | /*! |
Pasi | 5:be347c6040c1 | 337 | * \brief Function executed on Led 2 Timeout event |
Pasi | 5:be347c6040c1 | 338 | */ |
Pasi | 5:be347c6040c1 | 339 | static void OnLed2TimerEvent( void ) |
Pasi | 5:be347c6040c1 | 340 | { |
Pasi | 5:be347c6040c1 | 341 | Led2Timer.detach(); |
Pasi | 5:be347c6040c1 | 342 | // Switch LED 2 OFF |
Pasi | 5:be347c6040c1 | 343 | Led2 = 1; |
Pasi | 5:be347c6040c1 | 344 | } |
Pasi | 5:be347c6040c1 | 345 | |
Pasi | 5:be347c6040c1 | 346 | /*! |
Pasi | 5:be347c6040c1 | 347 | * \brief MCPS-Confirm event function |
Pasi | 5:be347c6040c1 | 348 | * |
Pasi | 5:be347c6040c1 | 349 | * \param [IN] McpsConfirm - Pointer to the confirm structure, |
Pasi | 5:be347c6040c1 | 350 | * containing confirm attributes. |
Pasi | 5:be347c6040c1 | 351 | */ |
Pasi | 5:be347c6040c1 | 352 | static void McpsConfirm( McpsConfirm_t *McpsConfirm ) |
Pasi | 5:be347c6040c1 | 353 | { |
Pasi | 5:be347c6040c1 | 354 | printf("MCPS Confirm\r\n"); |
Pasi | 5:be347c6040c1 | 355 | if( McpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 356 | { |
Pasi | 5:be347c6040c1 | 357 | switch( McpsConfirm->McpsRequest ) |
Pasi | 5:be347c6040c1 | 358 | { |
Pasi | 5:be347c6040c1 | 359 | case MCPS_UNCONFIRMED: |
Pasi | 5:be347c6040c1 | 360 | { |
Pasi | 5:be347c6040c1 | 361 | // Check Datarate |
Pasi | 5:be347c6040c1 | 362 | // Check TxPower |
Pasi | 5:be347c6040c1 | 363 | break; |
Pasi | 5:be347c6040c1 | 364 | } |
Pasi | 5:be347c6040c1 | 365 | case MCPS_CONFIRMED: |
Pasi | 5:be347c6040c1 | 366 | { |
Pasi | 5:be347c6040c1 | 367 | // Check Datarate |
Pasi | 5:be347c6040c1 | 368 | // Check TxPower |
Pasi | 5:be347c6040c1 | 369 | // Check AckReceived |
Pasi | 5:be347c6040c1 | 370 | // Check NbTrials |
Pasi | 5:be347c6040c1 | 371 | break; |
Pasi | 5:be347c6040c1 | 372 | } |
Pasi | 5:be347c6040c1 | 373 | case MCPS_PROPRIETARY: |
Pasi | 5:be347c6040c1 | 374 | { |
Pasi | 5:be347c6040c1 | 375 | break; |
Pasi | 5:be347c6040c1 | 376 | } |
Pasi | 5:be347c6040c1 | 377 | default: |
Pasi | 5:be347c6040c1 | 378 | break; |
Pasi | 5:be347c6040c1 | 379 | } |
Pasi | 5:be347c6040c1 | 380 | } |
Pasi | 5:be347c6040c1 | 381 | NextTx = true; |
Pasi | 5:be347c6040c1 | 382 | } |
Pasi | 5:be347c6040c1 | 383 | |
Pasi | 5:be347c6040c1 | 384 | /*! |
Pasi | 5:be347c6040c1 | 385 | * \brief MCPS-Indication event function |
Pasi | 5:be347c6040c1 | 386 | * |
Pasi | 5:be347c6040c1 | 387 | * \param [IN] McpsIndication - Pointer to the indication structure, |
Pasi | 5:be347c6040c1 | 388 | * containing indication attributes. |
Pasi | 5:be347c6040c1 | 389 | */ |
Pasi | 5:be347c6040c1 | 390 | static void McpsIndication( McpsIndication_t *McpsIndication ) |
Pasi | 5:be347c6040c1 | 391 | { |
Pasi | 5:be347c6040c1 | 392 | printf("MCPS Indication\r\n"); |
Pasi | 5:be347c6040c1 | 393 | if( McpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 394 | { |
Pasi | 5:be347c6040c1 | 395 | return; |
Pasi | 5:be347c6040c1 | 396 | } |
Pasi | 5:be347c6040c1 | 397 | |
Pasi | 5:be347c6040c1 | 398 | switch( McpsIndication->McpsIndication ) |
Pasi | 5:be347c6040c1 | 399 | { |
Pasi | 5:be347c6040c1 | 400 | case MCPS_UNCONFIRMED: |
Pasi | 5:be347c6040c1 | 401 | { |
Pasi | 5:be347c6040c1 | 402 | break; |
Pasi | 5:be347c6040c1 | 403 | } |
Pasi | 5:be347c6040c1 | 404 | case MCPS_CONFIRMED: |
Pasi | 5:be347c6040c1 | 405 | { |
Pasi | 5:be347c6040c1 | 406 | break; |
Pasi | 5:be347c6040c1 | 407 | } |
Pasi | 5:be347c6040c1 | 408 | case MCPS_PROPRIETARY: |
Pasi | 5:be347c6040c1 | 409 | { |
Pasi | 5:be347c6040c1 | 410 | break; |
Pasi | 5:be347c6040c1 | 411 | } |
Pasi | 5:be347c6040c1 | 412 | case MCPS_MULTICAST: |
Pasi | 5:be347c6040c1 | 413 | { |
Pasi | 5:be347c6040c1 | 414 | break; |
Pasi | 5:be347c6040c1 | 415 | } |
Pasi | 5:be347c6040c1 | 416 | default: |
Pasi | 5:be347c6040c1 | 417 | break; |
Pasi | 5:be347c6040c1 | 418 | } |
Pasi | 5:be347c6040c1 | 419 | |
Pasi | 5:be347c6040c1 | 420 | // Check Multicast |
Pasi | 5:be347c6040c1 | 421 | // Check Port |
Pasi | 5:be347c6040c1 | 422 | // Check Datarate |
Pasi | 5:be347c6040c1 | 423 | // Check FramePending |
Pasi | 5:be347c6040c1 | 424 | // Check Buffer |
Pasi | 5:be347c6040c1 | 425 | // Check BufferSize |
Pasi | 5:be347c6040c1 | 426 | // Check Rssi |
Pasi | 5:be347c6040c1 | 427 | // Check Snr |
Pasi | 5:be347c6040c1 | 428 | // Check RxSlot |
Pasi | 5:be347c6040c1 | 429 | |
Pasi | 5:be347c6040c1 | 430 | if( ComplianceTest.Running == true ) |
Pasi | 5:be347c6040c1 | 431 | { |
Pasi | 5:be347c6040c1 | 432 | ComplianceTest.DownLinkCounter++; |
Pasi | 5:be347c6040c1 | 433 | } |
Pasi | 5:be347c6040c1 | 434 | |
Pasi | 5:be347c6040c1 | 435 | if( McpsIndication->RxData == true ) |
Pasi | 5:be347c6040c1 | 436 | { |
Pasi | 5:be347c6040c1 | 437 | switch( McpsIndication->Port ) |
Pasi | 5:be347c6040c1 | 438 | { |
Pasi | 5:be347c6040c1 | 439 | case 1: |
Pasi | 5:be347c6040c1 | 440 | case 2: |
Pasi | 5:be347c6040c1 | 441 | printf("Receive buffer : %s\r\n", (char*)McpsIndication->Buffer); |
Pasi | 5:be347c6040c1 | 442 | break; |
Pasi | 5:be347c6040c1 | 443 | case 224: |
Pasi | 5:be347c6040c1 | 444 | if( ComplianceTest.Running == false ) |
Pasi | 5:be347c6040c1 | 445 | { |
Pasi | 5:be347c6040c1 | 446 | // Check compliance test enable command (i) |
Pasi | 5:be347c6040c1 | 447 | if( ( McpsIndication->BufferSize == 4 ) && |
Pasi | 5:be347c6040c1 | 448 | ( McpsIndication->Buffer[0] == 0x01 ) && |
Pasi | 5:be347c6040c1 | 449 | ( McpsIndication->Buffer[1] == 0x01 ) && |
Pasi | 5:be347c6040c1 | 450 | ( McpsIndication->Buffer[2] == 0x01 ) && |
Pasi | 5:be347c6040c1 | 451 | ( McpsIndication->Buffer[3] == 0x01 ) ) |
Pasi | 5:be347c6040c1 | 452 | { |
Pasi | 5:be347c6040c1 | 453 | IsTxConfirmed = false; |
Pasi | 5:be347c6040c1 | 454 | AppPort = 224; |
Pasi | 5:be347c6040c1 | 455 | AppDataSize = 2; |
Pasi | 5:be347c6040c1 | 456 | ComplianceTest.DownLinkCounter = 0; |
Pasi | 5:be347c6040c1 | 457 | ComplianceTest.LinkCheck = false; |
Pasi | 5:be347c6040c1 | 458 | ComplianceTest.DemodMargin = 0; |
Pasi | 5:be347c6040c1 | 459 | ComplianceTest.NbGateways = 0; |
Pasi | 5:be347c6040c1 | 460 | ComplianceTest.Running = true; |
Pasi | 5:be347c6040c1 | 461 | ComplianceTest.State = 1; |
Pasi | 5:be347c6040c1 | 462 | |
Pasi | 5:be347c6040c1 | 463 | MibRequestConfirm_t mibReq; |
Pasi | 5:be347c6040c1 | 464 | mibReq.Type = MIB_ADR; |
Pasi | 5:be347c6040c1 | 465 | mibReq.Param.AdrEnable = true; |
Pasi | 5:be347c6040c1 | 466 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 467 | |
Pasi | 5:be347c6040c1 | 468 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 469 | LoRaMacTestSetDutyCycleOn( false ); |
Pasi | 5:be347c6040c1 | 470 | #endif |
Pasi | 5:be347c6040c1 | 471 | } |
Pasi | 5:be347c6040c1 | 472 | } |
Pasi | 5:be347c6040c1 | 473 | else |
Pasi | 5:be347c6040c1 | 474 | { |
Pasi | 5:be347c6040c1 | 475 | ComplianceTest.State = McpsIndication->Buffer[0]; |
Pasi | 5:be347c6040c1 | 476 | switch( ComplianceTest.State ) |
Pasi | 5:be347c6040c1 | 477 | { |
Pasi | 5:be347c6040c1 | 478 | case 0: // Check compliance test disable command (ii) |
Pasi | 5:be347c6040c1 | 479 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
Pasi | 5:be347c6040c1 | 480 | AppPort = LORAWAN_APP_PORT; |
Pasi | 5:be347c6040c1 | 481 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
Pasi | 5:be347c6040c1 | 482 | ComplianceTest.DownLinkCounter = 0; |
Pasi | 5:be347c6040c1 | 483 | ComplianceTest.Running = false; |
Pasi | 5:be347c6040c1 | 484 | |
Pasi | 5:be347c6040c1 | 485 | MibRequestConfirm_t mibReq; |
Pasi | 5:be347c6040c1 | 486 | mibReq.Type = MIB_ADR; |
Pasi | 5:be347c6040c1 | 487 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
Pasi | 5:be347c6040c1 | 488 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 489 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 490 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
Pasi | 5:be347c6040c1 | 491 | #endif |
Pasi | 5:be347c6040c1 | 492 | break; |
Pasi | 5:be347c6040c1 | 493 | case 1: // (iii, iv) |
Pasi | 5:be347c6040c1 | 494 | AppDataSize = 2; |
Pasi | 5:be347c6040c1 | 495 | break; |
Pasi | 5:be347c6040c1 | 496 | case 2: // Enable confirmed messages (v) |
Pasi | 5:be347c6040c1 | 497 | IsTxConfirmed = true; |
Pasi | 5:be347c6040c1 | 498 | ComplianceTest.State = 1; |
Pasi | 5:be347c6040c1 | 499 | break; |
Pasi | 5:be347c6040c1 | 500 | case 3: // Disable confirmed messages (vi) |
Pasi | 5:be347c6040c1 | 501 | IsTxConfirmed = false; |
Pasi | 5:be347c6040c1 | 502 | ComplianceTest.State = 1; |
Pasi | 5:be347c6040c1 | 503 | break; |
Pasi | 5:be347c6040c1 | 504 | case 4: // (vii) |
Pasi | 5:be347c6040c1 | 505 | AppDataSize = McpsIndication->BufferSize; |
Pasi | 5:be347c6040c1 | 506 | |
Pasi | 5:be347c6040c1 | 507 | AppData[0] = 4; |
Pasi | 5:be347c6040c1 | 508 | for( uint8_t i = 1; i < AppDataSize; i++ ) |
Pasi | 5:be347c6040c1 | 509 | { |
Pasi | 5:be347c6040c1 | 510 | AppData[i] = McpsIndication->Buffer[i] + 1; |
Pasi | 5:be347c6040c1 | 511 | } |
Pasi | 5:be347c6040c1 | 512 | break; |
Pasi | 5:be347c6040c1 | 513 | case 5: // (viii) |
Pasi | 5:be347c6040c1 | 514 | { |
Pasi | 5:be347c6040c1 | 515 | MlmeReq_t mlmeReq; |
Pasi | 5:be347c6040c1 | 516 | mlmeReq.Type = MLME_LINK_CHECK; |
Pasi | 5:be347c6040c1 | 517 | LoRaMacMlmeRequest( &mlmeReq ); |
Pasi | 5:be347c6040c1 | 518 | } |
Pasi | 5:be347c6040c1 | 519 | break; |
Pasi | 5:be347c6040c1 | 520 | default: |
Pasi | 5:be347c6040c1 | 521 | break; |
Pasi | 5:be347c6040c1 | 522 | } |
Pasi | 5:be347c6040c1 | 523 | } |
Pasi | 5:be347c6040c1 | 524 | break; |
Pasi | 5:be347c6040c1 | 525 | default: |
Pasi | 5:be347c6040c1 | 526 | break; |
Pasi | 5:be347c6040c1 | 527 | } |
Pasi | 5:be347c6040c1 | 528 | } |
Pasi | 5:be347c6040c1 | 529 | |
Pasi | 5:be347c6040c1 | 530 | // Switch LED 2 ON for each received downlink |
Pasi | 5:be347c6040c1 | 531 | Led2 = 0; |
Pasi | 5:be347c6040c1 | 532 | Led2Timer.attach_us(OnLed2TimerEvent, 25000); |
Pasi | 5:be347c6040c1 | 533 | } |
Pasi | 5:be347c6040c1 | 534 | |
Pasi | 5:be347c6040c1 | 535 | /*! |
Pasi | 5:be347c6040c1 | 536 | * \brief MLME-Confirm event function |
Pasi | 5:be347c6040c1 | 537 | * |
Pasi | 5:be347c6040c1 | 538 | * \param [IN] MlmeConfirm - Pointer to the confirm structure, |
Pasi | 5:be347c6040c1 | 539 | * containing confirm attributes. |
Pasi | 5:be347c6040c1 | 540 | */ |
Pasi | 5:be347c6040c1 | 541 | static void MlmeConfirm( MlmeConfirm_t *MlmeConfirm ) |
Pasi | 5:be347c6040c1 | 542 | { |
Pasi | 5:be347c6040c1 | 543 | printf("MLME Confirm\r\n"); |
Pasi | 5:be347c6040c1 | 544 | if( MlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
Pasi | 5:be347c6040c1 | 545 | { |
Pasi | 5:be347c6040c1 | 546 | switch( MlmeConfirm->MlmeRequest ) |
Pasi | 5:be347c6040c1 | 547 | { |
Pasi | 5:be347c6040c1 | 548 | case MLME_JOIN: |
Pasi | 5:be347c6040c1 | 549 | { |
Pasi | 5:be347c6040c1 | 550 | // Status is OK, node has joined the network |
Pasi | 6:71b489e70063 | 551 | printf("\r\nElmo has joined the network\r\n\r\n"); |
Pasi | 5:be347c6040c1 | 552 | break; |
Pasi | 5:be347c6040c1 | 553 | } |
Pasi | 5:be347c6040c1 | 554 | case MLME_LINK_CHECK: |
Pasi | 5:be347c6040c1 | 555 | { |
Pasi | 5:be347c6040c1 | 556 | // Check DemodMargin |
Pasi | 5:be347c6040c1 | 557 | // Check NbGateways |
Pasi | 5:be347c6040c1 | 558 | if( ComplianceTest.Running == true ) |
Pasi | 5:be347c6040c1 | 559 | { |
Pasi | 5:be347c6040c1 | 560 | ComplianceTest.LinkCheck = true; |
Pasi | 5:be347c6040c1 | 561 | ComplianceTest.DemodMargin = MlmeConfirm->DemodMargin; |
Pasi | 5:be347c6040c1 | 562 | ComplianceTest.NbGateways = MlmeConfirm->NbGateways; |
Pasi | 5:be347c6040c1 | 563 | } |
Pasi | 5:be347c6040c1 | 564 | break; |
Pasi | 5:be347c6040c1 | 565 | } |
Pasi | 5:be347c6040c1 | 566 | default: |
Pasi | 5:be347c6040c1 | 567 | break; |
Pasi | 5:be347c6040c1 | 568 | } |
Pasi | 5:be347c6040c1 | 569 | } |
Pasi | 5:be347c6040c1 | 570 | NextTx = true; |
Pasi | 5:be347c6040c1 | 571 | } |
Pasi | 5:be347c6040c1 | 572 | |
Pasi | 5:be347c6040c1 | 573 | /** |
Pasi | 5:be347c6040c1 | 574 | * Radio state machine initialization |
Pasi | 5:be347c6040c1 | 575 | */ |
Pasi | 5:be347c6040c1 | 576 | void RadioInit( void ) |
Pasi | 5:be347c6040c1 | 577 | { |
Pasi | 5:be347c6040c1 | 578 | DeviceState = DEVICE_STATE_INIT; |
Pasi | 5:be347c6040c1 | 579 | } |
Pasi | 5:be347c6040c1 | 580 | |
Pasi | 5:be347c6040c1 | 581 | /** |
Pasi | 5:be347c6040c1 | 582 | * Request packet transmission |
Pasi | 5:be347c6040c1 | 583 | */ |
Pasi | 5:be347c6040c1 | 584 | void RequestPacketTx( char *DataString, bool PeriodicRequested ) |
Pasi | 5:be347c6040c1 | 585 | { |
Pasi | 5:be347c6040c1 | 586 | PacketTxReq = true; |
Pasi | 5:be347c6040c1 | 587 | PeriodicPackets = PeriodicRequested; |
Pasi | 5:be347c6040c1 | 588 | |
Pasi | 5:be347c6040c1 | 589 | // Copy string to buffer, truncated to max packet size if necessary with trailing zero |
Pasi | 5:be347c6040c1 | 590 | // Packet size is always max allowed. Padded with zeroes |
Pasi | 5:be347c6040c1 | 591 | strncpy((char*)AppDataBuffer, DataString, LORAWAN_APP_DATA_SIZE-1); |
Pasi | 5:be347c6040c1 | 592 | AppDataBuffer[LORAWAN_APP_DATA_SIZE-1] = 0; |
Pasi | 5:be347c6040c1 | 593 | } |
Pasi | 5:be347c6040c1 | 594 | /** |
Pasi | 5:be347c6040c1 | 595 | * Radio state machine. |
Pasi | 5:be347c6040c1 | 596 | */ |
Pasi | 5:be347c6040c1 | 597 | void RadioHandler( void ) |
Pasi | 5:be347c6040c1 | 598 | { |
Pasi | 5:be347c6040c1 | 599 | static LoRaMacPrimitives_t LoRaMacPrimitives; |
Pasi | 5:be347c6040c1 | 600 | static LoRaMacCallback_t LoRaMacCallbacks; |
Pasi | 5:be347c6040c1 | 601 | static MibRequestConfirm_t mibReq; |
Pasi | 5:be347c6040c1 | 602 | |
Pasi | 5:be347c6040c1 | 603 | switch( DeviceState ) |
Pasi | 5:be347c6040c1 | 604 | { |
Pasi | 5:be347c6040c1 | 605 | case DEVICE_STATE_INIT: |
Pasi | 5:be347c6040c1 | 606 | { |
Pasi | 5:be347c6040c1 | 607 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
Pasi | 5:be347c6040c1 | 608 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
Pasi | 5:be347c6040c1 | 609 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
Pasi | 5:be347c6040c1 | 610 | //LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
Pasi | 5:be347c6040c1 | 611 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
Pasi | 5:be347c6040c1 | 612 | |
Pasi | 5:be347c6040c1 | 613 | mibReq.Type = MIB_ADR; |
Pasi | 5:be347c6040c1 | 614 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
Pasi | 5:be347c6040c1 | 615 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 616 | |
Pasi | 5:be347c6040c1 | 617 | mibReq.Type = MIB_PUBLIC_NETWORK; |
Pasi | 5:be347c6040c1 | 618 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
Pasi | 5:be347c6040c1 | 619 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 620 | |
Pasi | 5:be347c6040c1 | 621 | #if defined( USE_BAND_868 ) |
Pasi | 5:be347c6040c1 | 622 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
Pasi | 5:be347c6040c1 | 623 | |
Pasi | 5:be347c6040c1 | 624 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
Pasi | 5:be347c6040c1 | 625 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
Pasi | 5:be347c6040c1 | 626 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
Pasi | 5:be347c6040c1 | 627 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
Pasi | 5:be347c6040c1 | 628 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
Pasi | 5:be347c6040c1 | 629 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
Pasi | 5:be347c6040c1 | 630 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
Pasi | 5:be347c6040c1 | 631 | #endif |
Pasi | 5:be347c6040c1 | 632 | |
Pasi | 5:be347c6040c1 | 633 | #endif |
Pasi | 5:be347c6040c1 | 634 | DeviceState = DEVICE_STATE_JOIN; |
Pasi | 5:be347c6040c1 | 635 | break; |
Pasi | 5:be347c6040c1 | 636 | } |
Pasi | 5:be347c6040c1 | 637 | case DEVICE_STATE_JOIN: |
Pasi | 5:be347c6040c1 | 638 | { |
Pasi | 5:be347c6040c1 | 639 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
Pasi | 5:be347c6040c1 | 640 | MlmeReq_t mlmeReq; |
Pasi | 5:be347c6040c1 | 641 | |
Pasi | 5:be347c6040c1 | 642 | #warning BoardGetUniqueId not needed in test software - commented out |
Pasi | 5:be347c6040c1 | 643 | // Initialize LoRaMac device unique ID |
Pasi | 5:be347c6040c1 | 644 | // BoardGetUniqueId( DevEui ); |
Pasi | 5:be347c6040c1 | 645 | |
Pasi | 5:be347c6040c1 | 646 | mlmeReq.Type = MLME_JOIN; |
Pasi | 5:be347c6040c1 | 647 | |
Pasi | 5:be347c6040c1 | 648 | mlmeReq.Req.Join.DevEui = DevEui; |
Pasi | 5:be347c6040c1 | 649 | mlmeReq.Req.Join.AppEui = AppEui; |
Pasi | 5:be347c6040c1 | 650 | mlmeReq.Req.Join.AppKey = AppKey; |
Pasi | 5:be347c6040c1 | 651 | |
Pasi | 5:be347c6040c1 | 652 | if( NextTx == true ) |
Pasi | 5:be347c6040c1 | 653 | { |
Pasi | 5:be347c6040c1 | 654 | LoRaMacMlmeRequest( &mlmeReq ); |
Pasi | 5:be347c6040c1 | 655 | } |
Pasi | 5:be347c6040c1 | 656 | |
Pasi | 5:be347c6040c1 | 657 | // Schedule next packet transmission |
Pasi | 5:be347c6040c1 | 658 | TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE; |
Pasi | 5:be347c6040c1 | 659 | DeviceState = DEVICE_STATE_CYCLE; |
Pasi | 5:be347c6040c1 | 660 | |
Pasi | 5:be347c6040c1 | 661 | #else |
Pasi | 5:be347c6040c1 | 662 | // Random seed initialization |
Pasi | 5:be347c6040c1 | 663 | srand1( BoardGetRandomSeed( ) ); |
Pasi | 5:be347c6040c1 | 664 | |
Pasi | 5:be347c6040c1 | 665 | // Choose a random device address |
Pasi | 5:be347c6040c1 | 666 | DevAddr = randr( 0, 0x01FFFFFF ); |
Pasi | 5:be347c6040c1 | 667 | |
Pasi | 5:be347c6040c1 | 668 | mibReq.Type = MIB_NET_ID; |
Pasi | 5:be347c6040c1 | 669 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
Pasi | 5:be347c6040c1 | 670 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 671 | |
Pasi | 5:be347c6040c1 | 672 | mibReq.Type = MIB_DEV_ADDR; |
Pasi | 5:be347c6040c1 | 673 | mibReq.Param.DevAddr = DevAddr; |
Pasi | 5:be347c6040c1 | 674 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 675 | |
Pasi | 5:be347c6040c1 | 676 | mibReq.Type = MIB_NWK_SKEY; |
Pasi | 5:be347c6040c1 | 677 | mibReq.Param.NwkSKey = NwkSKey; |
Pasi | 5:be347c6040c1 | 678 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 679 | |
Pasi | 5:be347c6040c1 | 680 | mibReq.Type = MIB_APP_SKEY; |
Pasi | 5:be347c6040c1 | 681 | mibReq.Param.AppSKey = AppSKey; |
Pasi | 5:be347c6040c1 | 682 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 683 | |
Pasi | 5:be347c6040c1 | 684 | mibReq.Type = MIB_NETWORK_JOINED; |
Pasi | 5:be347c6040c1 | 685 | mibReq.Param.IsNetworkJoined = true; |
Pasi | 5:be347c6040c1 | 686 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Pasi | 5:be347c6040c1 | 687 | |
Pasi | 5:be347c6040c1 | 688 | DeviceState = DEVICE_STATE_SEND; |
Pasi | 5:be347c6040c1 | 689 | #endif |
Pasi | 5:be347c6040c1 | 690 | break; |
Pasi | 5:be347c6040c1 | 691 | } |
Pasi | 5:be347c6040c1 | 692 | case DEVICE_STATE_SEND: |
Pasi | 5:be347c6040c1 | 693 | { |
Pasi | 5:be347c6040c1 | 694 | if( NextTx == true ) |
Pasi | 5:be347c6040c1 | 695 | { |
Pasi | 5:be347c6040c1 | 696 | printf("Packet sent\r\n"); |
Pasi | 5:be347c6040c1 | 697 | PrepareTxFrame( AppPort ); |
Pasi | 5:be347c6040c1 | 698 | |
Pasi | 5:be347c6040c1 | 699 | NextTx = SendFrame( ); |
Pasi | 5:be347c6040c1 | 700 | if(PacketTxReq) |
Pasi | 5:be347c6040c1 | 701 | { |
Pasi | 5:be347c6040c1 | 702 | printf("Unscheduled packet transmission\r\n"); |
Pasi | 5:be347c6040c1 | 703 | PacketTxReq = false; |
Pasi | 5:be347c6040c1 | 704 | } |
Pasi | 5:be347c6040c1 | 705 | } |
Pasi | 5:be347c6040c1 | 706 | if( ComplianceTest.Running == true ) |
Pasi | 5:be347c6040c1 | 707 | { |
Pasi | 5:be347c6040c1 | 708 | // Schedule next packet transmission as soon as possible |
Pasi | 5:be347c6040c1 | 709 | TxDutyCycleTime = 300000; // 300 ms |
Pasi | 5:be347c6040c1 | 710 | } |
Pasi | 5:be347c6040c1 | 711 | else |
Pasi | 5:be347c6040c1 | 712 | { |
Pasi | 5:be347c6040c1 | 713 | // Schedule next packet periodic transmission |
Pasi | 5:be347c6040c1 | 714 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
Pasi | 5:be347c6040c1 | 715 | } |
Pasi | 5:be347c6040c1 | 716 | DeviceState = DEVICE_STATE_CYCLE; |
Pasi | 5:be347c6040c1 | 717 | break; |
Pasi | 5:be347c6040c1 | 718 | } |
Pasi | 5:be347c6040c1 | 719 | case DEVICE_STATE_CYCLE: |
Pasi | 5:be347c6040c1 | 720 | { |
Pasi | 5:be347c6040c1 | 721 | DeviceState = DEVICE_STATE_SLEEP; |
Pasi | 5:be347c6040c1 | 722 | |
Pasi | 5:be347c6040c1 | 723 | // Schedule next packet transmission |
Pasi | 5:be347c6040c1 | 724 | if(PeriodicPackets) |
Pasi | 5:be347c6040c1 | 725 | { |
Pasi | 5:be347c6040c1 | 726 | TxNextPacketTimer.attach_us(OnTxNextPacketTimerEvent, TxDutyCycleTime); |
Pasi | 5:be347c6040c1 | 727 | } |
Pasi | 5:be347c6040c1 | 728 | break; |
Pasi | 5:be347c6040c1 | 729 | } |
Pasi | 5:be347c6040c1 | 730 | case DEVICE_STATE_SLEEP: |
Pasi | 5:be347c6040c1 | 731 | { |
Pasi | 5:be347c6040c1 | 732 | // Wake up through events |
Pasi | 5:be347c6040c1 | 733 | if(PacketTxReq) |
Pasi | 5:be347c6040c1 | 734 | { |
Pasi | 5:be347c6040c1 | 735 | DeviceState = DEVICE_STATE_SEND; |
Pasi | 5:be347c6040c1 | 736 | } |
Pasi | 5:be347c6040c1 | 737 | #warning TimerLowPowerHandler disabled |
Pasi | 5:be347c6040c1 | 738 | //TimerLowPowerHandler( ); |
Pasi | 5:be347c6040c1 | 739 | break; |
Pasi | 5:be347c6040c1 | 740 | } |
Pasi | 5:be347c6040c1 | 741 | default: |
Pasi | 5:be347c6040c1 | 742 | { |
Pasi | 5:be347c6040c1 | 743 | DeviceState = DEVICE_STATE_INIT; |
Pasi | 5:be347c6040c1 | 744 | break; |
Pasi | 5:be347c6040c1 | 745 | } |
Pasi | 5:be347c6040c1 | 746 | } |
Pasi | 5:be347c6040c1 | 747 | } |