test TDOA

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Semtech

Committer:
nicocarv
Date:
Fri Jun 02 13:01:13 2017 +0000
Revision:
11:cc085c2e81a1
Parent:
10:cac88ae7d9af
ADR ON; Semtech channels OFF

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45496a70a8a5 1 /*
mluis 0:45496a70a8a5 2 / _____) _ | |
mluis 0:45496a70a8a5 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45496a70a8a5 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45496a70a8a5 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45496a70a8a5 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45496a70a8a5 7 (C)2015 Semtech
mluis 0:45496a70a8a5 8
mluis 0:45496a70a8a5 9 Description: LoRaMac classA device implementation
mluis 0:45496a70a8a5 10
mluis 0:45496a70a8a5 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45496a70a8a5 12
mluis 0:45496a70a8a5 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:45496a70a8a5 14 */
mluis 0:45496a70a8a5 15 #include "mbed.h"
mluis 0:45496a70a8a5 16 #include "board.h"
mluis 0:45496a70a8a5 17 #include "radio.h"
mluis 0:45496a70a8a5 18
mluis 0:45496a70a8a5 19 #include "LoRaMac.h"
mluis 7:5077515c163b 20 #include "Commissioning.h"
mluis 0:45496a70a8a5 21 #include "SerialDisplay.h"
mluis 0:45496a70a8a5 22
mluis 0:45496a70a8a5 23 /*!
nicocarv 8:0c5f1dac7b74 24 * ********************* *********************
nicocarv 8:0c5f1dac7b74 25 * * N unconfirmed TXs * - - time between periods of transmit - - * N unconfirmed TXs *
nicocarv 8:0c5f1dac7b74 26 * ********************* *********************
nicocarv 8:0c5f1dac7b74 27 * APP_N_PERIOD_TX PERIOD_TX_DUTYCYCLE APP_N_PERIOD_TX
nicocarv 8:0c5f1dac7b74 28 *
nicocarv 8:0c5f1dac7b74 29 *
nicocarv 8:0c5f1dac7b74 30 * Defines the application data transmission duty cycle inside a period of transmission.
nicocarv 8:0c5f1dac7b74 31 * value in [ms].
mluis 0:45496a70a8a5 32 */
mluis 7:5077515c163b 33 #define APP_TX_DUTYCYCLE 5000
mluis 0:45496a70a8a5 34
mluis 0:45496a70a8a5 35 /*!
nicocarv 8:0c5f1dac7b74 36 * Defines a random delay for application data transmission duty cycle.
mluis 7:5077515c163b 37 * value in [ms].
mluis 0:45496a70a8a5 38 */
nicocarv 8:0c5f1dac7b74 39 #define APP_TX_DUTYCYCLE_RND 10
nicocarv 8:0c5f1dac7b74 40
nicocarv 8:0c5f1dac7b74 41 /*!
nicocarv 8:0c5f1dac7b74 42 * Defines the Number of Unconfirmed Uplinks to send in each period of transmission
nicocarv 8:0c5f1dac7b74 43 */
nicocarv 8:0c5f1dac7b74 44 #define APP_N_PERIOD_TX 10
nicocarv 8:0c5f1dac7b74 45
nicocarv 8:0c5f1dac7b74 46 /*!
nicocarv 8:0c5f1dac7b74 47 * Defines the delay between periods of transmissions
nicocarv 8:0c5f1dac7b74 48 * value in [ms].
nicocarv 8:0c5f1dac7b74 49 */
nicocarv 8:0c5f1dac7b74 50 #define PERIOD_TX_DUTYCYCLE 60000
mluis 0:45496a70a8a5 51
mluis 0:45496a70a8a5 52 /*!
mluis 3:3152aa75c58d 53 * Default datarate
mluis 0:45496a70a8a5 54 */
nicocarv 11:cc085c2e81a1 55 #define LORAWAN_DEFAULT_DATARATE DR_5
mluis 0:45496a70a8a5 56
mluis 0:45496a70a8a5 57 /*!
mluis 0:45496a70a8a5 58 * LoRaWAN confirmed messages
mluis 0:45496a70a8a5 59 */
nicocarv 8:0c5f1dac7b74 60 #define LORAWAN_CONFIRMED_MSG_ON false
mluis 0:45496a70a8a5 61
mluis 0:45496a70a8a5 62 /*!
mluis 0:45496a70a8a5 63 * LoRaWAN Adaptive Data Rate
mluis 0:45496a70a8a5 64 *
mluis 0:45496a70a8a5 65 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:45496a70a8a5 66 */
nicocarv 11:cc085c2e81a1 67 #define LORAWAN_ADR_ON 1
mluis 0:45496a70a8a5 68
mluis 0:45496a70a8a5 69 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 70
mluis 0:45496a70a8a5 71 #include "LoRaMacTest.h"
mluis 0:45496a70a8a5 72
mluis 0:45496a70a8a5 73 /*!
mluis 0:45496a70a8a5 74 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:45496a70a8a5 75 *
mluis 0:45496a70a8a5 76 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:45496a70a8a5 77 */
mluis 7:5077515c163b 78 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:45496a70a8a5 79
nicocarv 11:cc085c2e81a1 80 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 0
mluis 3:3152aa75c58d 81
mluis 7:5077515c163b 82 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 3:3152aa75c58d 83
mluis 3:3152aa75c58d 84 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 85 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 86 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 87 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 88 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 89 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 3:3152aa75c58d 90 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 3:3152aa75c58d 91
mluis 3:3152aa75c58d 92 #endif
mluis 3:3152aa75c58d 93
mluis 0:45496a70a8a5 94 #endif
mluis 0:45496a70a8a5 95
mluis 0:45496a70a8a5 96 /*!
mluis 0:45496a70a8a5 97 * LoRaWAN application port
mluis 0:45496a70a8a5 98 */
mluis 0:45496a70a8a5 99 #define LORAWAN_APP_PORT 15
mluis 0:45496a70a8a5 100
mluis 0:45496a70a8a5 101 /*!
mluis 0:45496a70a8a5 102 * User application data buffer size
mluis 0:45496a70a8a5 103 */
mluis 0:45496a70a8a5 104 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 0:45496a70a8a5 105 #define LORAWAN_APP_DATA_SIZE 6
mluis 0:45496a70a8a5 106
mluis 0:45496a70a8a5 107 #else
mluis 0:45496a70a8a5 108 #define LORAWAN_APP_DATA_SIZE 1
mluis 0:45496a70a8a5 109
mluis 0:45496a70a8a5 110 #endif
mluis 0:45496a70a8a5 111
mluis 0:45496a70a8a5 112 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:45496a70a8a5 113 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:45496a70a8a5 114 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:45496a70a8a5 115
mluis 5:62862ef9480b 116 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 117
mluis 0:45496a70a8a5 118 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:45496a70a8a5 119 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:45496a70a8a5 120
mluis 0:45496a70a8a5 121 /*!
mluis 0:45496a70a8a5 122 * Device address
mluis 0:45496a70a8a5 123 */
mluis 0:45496a70a8a5 124 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:45496a70a8a5 125
mluis 0:45496a70a8a5 126 #endif
mluis 0:45496a70a8a5 127
nicocarv 8:0c5f1dac7b74 128
nicocarv 8:0c5f1dac7b74 129 /*!
nicocarv 8:0c5f1dac7b74 130 * Count the number of TX in a TX period slot
nicocarv 8:0c5f1dac7b74 131 */
nicocarv 8:0c5f1dac7b74 132 int PERIODS_COUNT = 0;
nicocarv 8:0c5f1dac7b74 133
nicocarv 8:0c5f1dac7b74 134
mluis 0:45496a70a8a5 135 /*!
mluis 0:45496a70a8a5 136 * Application port
mluis 0:45496a70a8a5 137 */
mluis 0:45496a70a8a5 138 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 139
mluis 0:45496a70a8a5 140 /*!
mluis 0:45496a70a8a5 141 * User application data size
mluis 0:45496a70a8a5 142 */
mluis 0:45496a70a8a5 143 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 144
mluis 0:45496a70a8a5 145 /*!
mluis 0:45496a70a8a5 146 * User application data buffer size
mluis 0:45496a70a8a5 147 */
mluis 0:45496a70a8a5 148 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 0:45496a70a8a5 149
mluis 0:45496a70a8a5 150 /*!
mluis 0:45496a70a8a5 151 * User application data
mluis 0:45496a70a8a5 152 */
mluis 0:45496a70a8a5 153 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 0:45496a70a8a5 154
mluis 0:45496a70a8a5 155 /*!
mluis 0:45496a70a8a5 156 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 0:45496a70a8a5 157 */
mluis 0:45496a70a8a5 158 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 159
mluis 0:45496a70a8a5 160 /*!
mluis 0:45496a70a8a5 161 * Defines the application data transmission duty cycle
mluis 0:45496a70a8a5 162 */
mluis 0:45496a70a8a5 163 static uint32_t TxDutyCycleTime;
mluis 0:45496a70a8a5 164
mluis 0:45496a70a8a5 165 /*!
mluis 0:45496a70a8a5 166 * Timer to handle the application data transmission duty cycle
mluis 0:45496a70a8a5 167 */
mluis 0:45496a70a8a5 168 static TimerEvent_t TxNextPacketTimer;
mluis 0:45496a70a8a5 169
mluis 0:45496a70a8a5 170 /*!
mluis 0:45496a70a8a5 171 * Specifies the state of the application LED
mluis 0:45496a70a8a5 172 */
mluis 0:45496a70a8a5 173 static bool AppLedStateOn = false;
mluis 0:45496a70a8a5 174 volatile bool Led3StateChanged = false;
mluis 0:45496a70a8a5 175 /*!
mluis 0:45496a70a8a5 176 * Timer to handle the state of LED1
mluis 0:45496a70a8a5 177 */
mluis 0:45496a70a8a5 178 static TimerEvent_t Led1Timer;
mluis 0:45496a70a8a5 179 volatile bool Led1State = false;
mluis 0:45496a70a8a5 180 volatile bool Led1StateChanged = false;
mluis 0:45496a70a8a5 181 /*!
mluis 0:45496a70a8a5 182 * Timer to handle the state of LED2
mluis 0:45496a70a8a5 183 */
mluis 0:45496a70a8a5 184 static TimerEvent_t Led2Timer;
mluis 0:45496a70a8a5 185 volatile bool Led2State = false;
mluis 0:45496a70a8a5 186 volatile bool Led2StateChanged = false;
mluis 0:45496a70a8a5 187
mluis 0:45496a70a8a5 188 /*!
mluis 0:45496a70a8a5 189 * Indicates if a new packet can be sent
mluis 0:45496a70a8a5 190 */
mluis 0:45496a70a8a5 191 static bool NextTx = true;
mluis 0:45496a70a8a5 192
mluis 0:45496a70a8a5 193 /*!
mluis 0:45496a70a8a5 194 * Device states
mluis 0:45496a70a8a5 195 */
mluis 7:5077515c163b 196 static enum eDeviceState
mluis 0:45496a70a8a5 197 {
mluis 0:45496a70a8a5 198 DEVICE_STATE_INIT,
mluis 0:45496a70a8a5 199 DEVICE_STATE_JOIN,
mluis 0:45496a70a8a5 200 DEVICE_STATE_SEND,
mluis 0:45496a70a8a5 201 DEVICE_STATE_CYCLE,
mluis 0:45496a70a8a5 202 DEVICE_STATE_SLEEP
mluis 0:45496a70a8a5 203 }DeviceState;
mluis 0:45496a70a8a5 204
mluis 0:45496a70a8a5 205 /*!
mluis 0:45496a70a8a5 206 * LoRaWAN compliance tests support data
mluis 0:45496a70a8a5 207 */
mluis 0:45496a70a8a5 208 struct ComplianceTest_s
mluis 0:45496a70a8a5 209 {
mluis 0:45496a70a8a5 210 bool Running;
mluis 0:45496a70a8a5 211 uint8_t State;
mluis 0:45496a70a8a5 212 bool IsTxConfirmed;
mluis 0:45496a70a8a5 213 uint8_t AppPort;
mluis 0:45496a70a8a5 214 uint8_t AppDataSize;
mluis 0:45496a70a8a5 215 uint8_t *AppDataBuffer;
mluis 0:45496a70a8a5 216 uint16_t DownLinkCounter;
mluis 0:45496a70a8a5 217 bool LinkCheck;
mluis 0:45496a70a8a5 218 uint8_t DemodMargin;
mluis 0:45496a70a8a5 219 uint8_t NbGateways;
mluis 0:45496a70a8a5 220 }ComplianceTest;
mluis 0:45496a70a8a5 221
mluis 0:45496a70a8a5 222 /*
mluis 0:45496a70a8a5 223 * SerialDisplay managment variables
mluis 0:45496a70a8a5 224 */
mluis 0:45496a70a8a5 225
mluis 0:45496a70a8a5 226 /*!
mluis 0:45496a70a8a5 227 * Indicates if the MAC layer network join status has changed.
mluis 0:45496a70a8a5 228 */
mluis 0:45496a70a8a5 229 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 230
mluis 0:45496a70a8a5 231 /*!
mluis 0:45496a70a8a5 232 * Strucure containing the Uplink status
mluis 0:45496a70a8a5 233 */
mluis 0:45496a70a8a5 234 struct sLoRaMacUplinkStatus
mluis 0:45496a70a8a5 235 {
mluis 0:45496a70a8a5 236 uint8_t Acked;
mluis 0:45496a70a8a5 237 int8_t Datarate;
mluis 0:45496a70a8a5 238 uint16_t UplinkCounter;
mluis 0:45496a70a8a5 239 uint8_t Port;
mluis 0:45496a70a8a5 240 uint8_t *Buffer;
mluis 0:45496a70a8a5 241 uint8_t BufferSize;
mluis 0:45496a70a8a5 242 }LoRaMacUplinkStatus;
mluis 0:45496a70a8a5 243 volatile bool UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 244
mluis 0:45496a70a8a5 245 /*!
mluis 0:45496a70a8a5 246 * Strucure containing the Downlink status
mluis 0:45496a70a8a5 247 */
mluis 0:45496a70a8a5 248 struct sLoRaMacDownlinkStatus
mluis 0:45496a70a8a5 249 {
mluis 0:45496a70a8a5 250 int16_t Rssi;
mluis 0:45496a70a8a5 251 int8_t Snr;
mluis 0:45496a70a8a5 252 uint16_t DownlinkCounter;
mluis 0:45496a70a8a5 253 bool RxData;
mluis 0:45496a70a8a5 254 uint8_t Port;
mluis 0:45496a70a8a5 255 uint8_t *Buffer;
mluis 0:45496a70a8a5 256 uint8_t BufferSize;
mluis 0:45496a70a8a5 257 }LoRaMacDownlinkStatus;
mluis 0:45496a70a8a5 258 volatile bool DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 259
mluis 0:45496a70a8a5 260 void SerialDisplayRefresh( void )
mluis 0:45496a70a8a5 261 {
mluis 0:45496a70a8a5 262 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 263
mluis 0:45496a70a8a5 264 SerialDisplayInit( );
mluis 0:45496a70a8a5 265 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 266
mluis 0:45496a70a8a5 267 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:45496a70a8a5 268 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 269 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 270 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 271 SerialDisplayUpdateKey( 13, AppSKey );
mluis 5:62862ef9480b 272 #endif
mluis 0:45496a70a8a5 273 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 274 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 275 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 276
mluis 0:45496a70a8a5 277 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 278 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 279 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 280
mluis 0:45496a70a8a5 281 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 282 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 283 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 284 #else
mluis 0:45496a70a8a5 285 SerialDisplayUpdateDutyCycle( false );
mluis 0:45496a70a8a5 286 #endif
mluis 0:45496a70a8a5 287 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 288
mluis 0:45496a70a8a5 289 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 290 }
mluis 0:45496a70a8a5 291
mluis 0:45496a70a8a5 292 void SerialRxProcess( void )
mluis 0:45496a70a8a5 293 {
mluis 0:45496a70a8a5 294 if( SerialDisplayReadable( ) == true )
mluis 0:45496a70a8a5 295 {
mluis 0:45496a70a8a5 296 switch( SerialDisplayGetChar( ) )
mluis 0:45496a70a8a5 297 {
mluis 0:45496a70a8a5 298 case 'R':
mluis 0:45496a70a8a5 299 case 'r':
mluis 0:45496a70a8a5 300 // Refresh Serial screen
mluis 0:45496a70a8a5 301 SerialDisplayRefresh( );
mluis 0:45496a70a8a5 302 break;
mluis 0:45496a70a8a5 303 default:
mluis 0:45496a70a8a5 304 break;
mluis 0:45496a70a8a5 305 }
mluis 0:45496a70a8a5 306 }
mluis 0:45496a70a8a5 307 }
mluis 0:45496a70a8a5 308
mluis 0:45496a70a8a5 309 /*!
mluis 0:45496a70a8a5 310 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 311 */
mluis 0:45496a70a8a5 312 static void PrepareTxFrame( uint8_t port )
mluis 0:45496a70a8a5 313 {
mluis 0:45496a70a8a5 314 switch( port )
mluis 0:45496a70a8a5 315 {
mluis 0:45496a70a8a5 316 case 15:
mluis 0:45496a70a8a5 317 {
mluis 0:45496a70a8a5 318 AppData[0] = AppLedStateOn;
mluis 0:45496a70a8a5 319 if( IsTxConfirmed == true )
mluis 0:45496a70a8a5 320 {
mluis 0:45496a70a8a5 321 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:45496a70a8a5 322 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:45496a70a8a5 323 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:45496a70a8a5 324 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:45496a70a8a5 325 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 326 }
mluis 0:45496a70a8a5 327 }
mluis 0:45496a70a8a5 328 break;
mluis 0:45496a70a8a5 329 case 224:
mluis 0:45496a70a8a5 330 if( ComplianceTest.LinkCheck == true )
mluis 0:45496a70a8a5 331 {
mluis 0:45496a70a8a5 332 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 333 AppDataSize = 3;
mluis 0:45496a70a8a5 334 AppData[0] = 5;
mluis 0:45496a70a8a5 335 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:45496a70a8a5 336 AppData[2] = ComplianceTest.NbGateways;
mluis 0:45496a70a8a5 337 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 338 }
mluis 0:45496a70a8a5 339 else
mluis 0:45496a70a8a5 340 {
mluis 0:45496a70a8a5 341 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 342 {
mluis 0:45496a70a8a5 343 case 4:
mluis 0:45496a70a8a5 344 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 345 break;
mluis 0:45496a70a8a5 346 case 1:
mluis 0:45496a70a8a5 347 AppDataSize = 2;
mluis 0:45496a70a8a5 348 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:45496a70a8a5 349 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:45496a70a8a5 350 break;
mluis 0:45496a70a8a5 351 }
mluis 0:45496a70a8a5 352 }
mluis 0:45496a70a8a5 353 break;
mluis 0:45496a70a8a5 354 default:
mluis 0:45496a70a8a5 355 break;
mluis 0:45496a70a8a5 356 }
mluis 0:45496a70a8a5 357 }
mluis 0:45496a70a8a5 358
mluis 0:45496a70a8a5 359 /*!
mluis 0:45496a70a8a5 360 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 361 *
mluis 0:45496a70a8a5 362 * \retval [0: frame could be send, 1: error]
mluis 0:45496a70a8a5 363 */
mluis 0:45496a70a8a5 364 static bool SendFrame( void )
mluis 0:45496a70a8a5 365 {
mluis 0:45496a70a8a5 366 McpsReq_t mcpsReq;
mluis 0:45496a70a8a5 367 LoRaMacTxInfo_t txInfo;
mluis 7:5077515c163b 368
mluis 0:45496a70a8a5 369 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 370 {
mluis 0:45496a70a8a5 371 // Send empty frame in order to flush MAC commands
mluis 0:45496a70a8a5 372 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 373 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:45496a70a8a5 374 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:45496a70a8a5 375 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:3152aa75c58d 376
mluis 0:45496a70a8a5 377 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 378 LoRaMacUplinkStatus.Port = 0;
mluis 0:45496a70a8a5 379 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:45496a70a8a5 380 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:45496a70a8a5 381 SerialDisplayUpdateFrameType( false );
mluis 0:45496a70a8a5 382 }
mluis 0:45496a70a8a5 383 else
mluis 0:45496a70a8a5 384 {
mluis 0:45496a70a8a5 385 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 386 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:45496a70a8a5 387 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:45496a70a8a5 388 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:45496a70a8a5 389 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:45496a70a8a5 390
mluis 0:45496a70a8a5 391 if( IsTxConfirmed == false )
mluis 0:45496a70a8a5 392 {
mluis 0:45496a70a8a5 393 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 394 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:45496a70a8a5 395 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 396 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:45496a70a8a5 397 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 398 }
mluis 0:45496a70a8a5 399 else
mluis 0:45496a70a8a5 400 {
mluis 0:45496a70a8a5 401 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:45496a70a8a5 402 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:45496a70a8a5 403 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 404 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 2:2eeaeae2c725 405 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:45496a70a8a5 406 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 407 }
mluis 0:45496a70a8a5 408 }
mluis 0:45496a70a8a5 409
mluis 0:45496a70a8a5 410 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 411 {
mluis 0:45496a70a8a5 412 return false;
mluis 0:45496a70a8a5 413 }
mluis 0:45496a70a8a5 414 return true;
mluis 0:45496a70a8a5 415 }
mluis 0:45496a70a8a5 416
mluis 0:45496a70a8a5 417 /*!
mluis 0:45496a70a8a5 418 * \brief Function executed on TxNextPacket Timeout event
mluis 0:45496a70a8a5 419 */
mluis 0:45496a70a8a5 420 static void OnTxNextPacketTimerEvent( void )
mluis 0:45496a70a8a5 421 {
mluis 0:45496a70a8a5 422 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 423 LoRaMacStatus_t status;
mluis 0:45496a70a8a5 424
mluis 0:45496a70a8a5 425 TimerStop( &TxNextPacketTimer );
mluis 0:45496a70a8a5 426
mluis 0:45496a70a8a5 427 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 428 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 429
mluis 0:45496a70a8a5 430 if( status == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 431 {
mluis 0:45496a70a8a5 432 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:45496a70a8a5 433 {
mluis 0:45496a70a8a5 434 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 435 NextTx = true;
mluis 0:45496a70a8a5 436 }
mluis 0:45496a70a8a5 437 else
mluis 0:45496a70a8a5 438 {
mluis 0:45496a70a8a5 439 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 440 }
mluis 0:45496a70a8a5 441 }
mluis 0:45496a70a8a5 442 }
mluis 0:45496a70a8a5 443
mluis 0:45496a70a8a5 444 /*!
mluis 0:45496a70a8a5 445 * \brief Function executed on Led 1 Timeout event
mluis 0:45496a70a8a5 446 */
mluis 0:45496a70a8a5 447 static void OnLed1TimerEvent( void )
mluis 0:45496a70a8a5 448 {
mluis 0:45496a70a8a5 449 TimerStop( &Led1Timer );
mluis 0:45496a70a8a5 450 // Switch LED 1 OFF
mluis 0:45496a70a8a5 451 Led1State = false;
mluis 0:45496a70a8a5 452 Led1StateChanged = true;
mluis 0:45496a70a8a5 453 }
mluis 0:45496a70a8a5 454
mluis 0:45496a70a8a5 455 /*!
mluis 0:45496a70a8a5 456 * \brief Function executed on Led 2 Timeout event
mluis 0:45496a70a8a5 457 */
mluis 0:45496a70a8a5 458 static void OnLed2TimerEvent( void )
mluis 0:45496a70a8a5 459 {
mluis 0:45496a70a8a5 460 TimerStop( &Led2Timer );
mluis 0:45496a70a8a5 461 // Switch LED 2 OFF
mluis 0:45496a70a8a5 462 Led2State = false;
mluis 0:45496a70a8a5 463 Led2StateChanged = true;
mluis 0:45496a70a8a5 464 }
mluis 0:45496a70a8a5 465
mluis 0:45496a70a8a5 466 /*!
mluis 0:45496a70a8a5 467 * \brief MCPS-Confirm event function
mluis 0:45496a70a8a5 468 *
mluis 3:3152aa75c58d 469 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 470 * containing confirm attributes.
mluis 0:45496a70a8a5 471 */
mluis 3:3152aa75c58d 472 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:45496a70a8a5 473 {
mluis 3:3152aa75c58d 474 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 475 {
mluis 3:3152aa75c58d 476 switch( mcpsConfirm->McpsRequest )
mluis 0:45496a70a8a5 477 {
mluis 0:45496a70a8a5 478 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 479 {
mluis 0:45496a70a8a5 480 // Check Datarate
mluis 0:45496a70a8a5 481 // Check TxPower
mluis 0:45496a70a8a5 482 break;
mluis 0:45496a70a8a5 483 }
mluis 0:45496a70a8a5 484 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 485 {
mluis 0:45496a70a8a5 486 // Check Datarate
mluis 0:45496a70a8a5 487 // Check TxPower
mluis 0:45496a70a8a5 488 // Check AckReceived
mluis 3:3152aa75c58d 489 // Check NbTrials
mluis 3:3152aa75c58d 490 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 0:45496a70a8a5 491 break;
mluis 0:45496a70a8a5 492 }
mluis 0:45496a70a8a5 493 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 494 {
mluis 0:45496a70a8a5 495 break;
mluis 0:45496a70a8a5 496 }
mluis 0:45496a70a8a5 497 default:
mluis 0:45496a70a8a5 498 break;
mluis 0:45496a70a8a5 499 }
mluis 3:3152aa75c58d 500 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 3:3152aa75c58d 501 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 3:3152aa75c58d 502
mluis 5:62862ef9480b 503 // Switch LED 1 ON
mluis 5:62862ef9480b 504 Led1State = true;
mluis 5:62862ef9480b 505 Led1StateChanged = true;
mluis 5:62862ef9480b 506 TimerStart( &Led1Timer );
mluis 5:62862ef9480b 507
mluis 0:45496a70a8a5 508 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 509 }
mluis 0:45496a70a8a5 510 NextTx = true;
mluis 0:45496a70a8a5 511 }
mluis 0:45496a70a8a5 512
mluis 0:45496a70a8a5 513 /*!
mluis 0:45496a70a8a5 514 * \brief MCPS-Indication event function
mluis 0:45496a70a8a5 515 *
mluis 3:3152aa75c58d 516 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 0:45496a70a8a5 517 * containing indication attributes.
mluis 0:45496a70a8a5 518 */
mluis 3:3152aa75c58d 519 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:45496a70a8a5 520 {
mluis 3:3152aa75c58d 521 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 522 {
mluis 0:45496a70a8a5 523 return;
mluis 0:45496a70a8a5 524 }
mluis 0:45496a70a8a5 525
mluis 3:3152aa75c58d 526 switch( mcpsIndication->McpsIndication )
mluis 0:45496a70a8a5 527 {
mluis 0:45496a70a8a5 528 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 529 {
mluis 0:45496a70a8a5 530 break;
mluis 0:45496a70a8a5 531 }
mluis 0:45496a70a8a5 532 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 533 {
mluis 0:45496a70a8a5 534 break;
mluis 0:45496a70a8a5 535 }
mluis 0:45496a70a8a5 536 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 537 {
mluis 0:45496a70a8a5 538 break;
mluis 0:45496a70a8a5 539 }
mluis 0:45496a70a8a5 540 case MCPS_MULTICAST:
mluis 0:45496a70a8a5 541 {
mluis 0:45496a70a8a5 542 break;
mluis 0:45496a70a8a5 543 }
mluis 0:45496a70a8a5 544 default:
mluis 0:45496a70a8a5 545 break;
mluis 0:45496a70a8a5 546 }
mluis 0:45496a70a8a5 547
mluis 0:45496a70a8a5 548 // Check Multicast
mluis 0:45496a70a8a5 549 // Check Port
mluis 0:45496a70a8a5 550 // Check Datarate
mluis 0:45496a70a8a5 551 // Check FramePending
mluis 0:45496a70a8a5 552 // Check Buffer
mluis 0:45496a70a8a5 553 // Check BufferSize
mluis 0:45496a70a8a5 554 // Check Rssi
mluis 0:45496a70a8a5 555 // Check Snr
mluis 0:45496a70a8a5 556 // Check RxSlot
mluis 3:3152aa75c58d 557 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 3:3152aa75c58d 558 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:45496a70a8a5 559 {
mluis 0:45496a70a8a5 560 // Invert and divide by 4
mluis 3:3152aa75c58d 561 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:45496a70a8a5 562 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 563 }
mluis 0:45496a70a8a5 564 else
mluis 0:45496a70a8a5 565 {
mluis 0:45496a70a8a5 566 // Divide by 4
mluis 3:3152aa75c58d 567 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 0:45496a70a8a5 568 }
mluis 0:45496a70a8a5 569 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 3:3152aa75c58d 570 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 3:3152aa75c58d 571 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 3:3152aa75c58d 572 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 3:3152aa75c58d 573 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 574
mluis 0:45496a70a8a5 575 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 576 {
mluis 0:45496a70a8a5 577 ComplianceTest.DownLinkCounter++;
mluis 0:45496a70a8a5 578 }
mluis 0:45496a70a8a5 579
mluis 3:3152aa75c58d 580 if( mcpsIndication->RxData == true )
mluis 0:45496a70a8a5 581 {
mluis 3:3152aa75c58d 582 switch( mcpsIndication->Port )
mluis 0:45496a70a8a5 583 {
mluis 0:45496a70a8a5 584 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:45496a70a8a5 585 case 2:
mluis 3:3152aa75c58d 586 if( mcpsIndication->BufferSize == 1 )
mluis 0:45496a70a8a5 587 {
mluis 3:3152aa75c58d 588 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 0:45496a70a8a5 589 Led3StateChanged = true;
mluis 0:45496a70a8a5 590 }
mluis 0:45496a70a8a5 591 break;
mluis 0:45496a70a8a5 592 case 224:
mluis 0:45496a70a8a5 593 if( ComplianceTest.Running == false )
mluis 0:45496a70a8a5 594 {
mluis 0:45496a70a8a5 595 // Check compliance test enable command (i)
mluis 3:3152aa75c58d 596 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 3:3152aa75c58d 597 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 3:3152aa75c58d 598 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 3:3152aa75c58d 599 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 3:3152aa75c58d 600 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 0:45496a70a8a5 601 {
mluis 0:45496a70a8a5 602 IsTxConfirmed = false;
mluis 0:45496a70a8a5 603 AppPort = 224;
mluis 0:45496a70a8a5 604 AppDataSize = 2;
mluis 0:45496a70a8a5 605 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 606 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 607 ComplianceTest.DemodMargin = 0;
mluis 0:45496a70a8a5 608 ComplianceTest.NbGateways = 0;
mluis 0:45496a70a8a5 609 ComplianceTest.Running = true;
mluis 0:45496a70a8a5 610 ComplianceTest.State = 1;
mluis 7:5077515c163b 611
mluis 0:45496a70a8a5 612 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 613 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 614 mibReq.Param.AdrEnable = true;
mluis 0:45496a70a8a5 615 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 616
mluis 0:45496a70a8a5 617 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 618 LoRaMacTestSetDutyCycleOn( false );
mluis 0:45496a70a8a5 619 #endif
mluis 0:45496a70a8a5 620 }
mluis 0:45496a70a8a5 621 }
mluis 0:45496a70a8a5 622 else
mluis 0:45496a70a8a5 623 {
mluis 3:3152aa75c58d 624 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 0:45496a70a8a5 625 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 626 {
mluis 0:45496a70a8a5 627 case 0: // Check compliance test disable command (ii)
mluis 0:45496a70a8a5 628 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 629 AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 630 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 631 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 632 ComplianceTest.Running = false;
mluis 7:5077515c163b 633
mluis 0:45496a70a8a5 634 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 635 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 636 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 637 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 638 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 639 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 640 #endif
mluis 0:45496a70a8a5 641 break;
mluis 0:45496a70a8a5 642 case 1: // (iii, iv)
mluis 0:45496a70a8a5 643 AppDataSize = 2;
mluis 0:45496a70a8a5 644 break;
mluis 0:45496a70a8a5 645 case 2: // Enable confirmed messages (v)
mluis 0:45496a70a8a5 646 IsTxConfirmed = true;
mluis 0:45496a70a8a5 647 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 648 break;
mluis 0:45496a70a8a5 649 case 3: // Disable confirmed messages (vi)
mluis 0:45496a70a8a5 650 IsTxConfirmed = false;
mluis 0:45496a70a8a5 651 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 652 break;
mluis 0:45496a70a8a5 653 case 4: // (vii)
mluis 3:3152aa75c58d 654 AppDataSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 655
mluis 0:45496a70a8a5 656 AppData[0] = 4;
mluis 0:45496a70a8a5 657 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:45496a70a8a5 658 {
mluis 3:3152aa75c58d 659 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 0:45496a70a8a5 660 }
mluis 0:45496a70a8a5 661 break;
mluis 0:45496a70a8a5 662 case 5: // (viii)
mluis 0:45496a70a8a5 663 {
mluis 0:45496a70a8a5 664 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 665 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:45496a70a8a5 666 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 667 }
mluis 0:45496a70a8a5 668 break;
mluis 5:62862ef9480b 669 case 6: // (ix)
mluis 5:62862ef9480b 670 {
mluis 5:62862ef9480b 671 MlmeReq_t mlmeReq;
mluis 5:62862ef9480b 672
mluis 7:5077515c163b 673 // Disable TestMode and revert back to normal operation
mluis 7:5077515c163b 674 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 7:5077515c163b 675 AppPort = LORAWAN_APP_PORT;
mluis 7:5077515c163b 676 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 7:5077515c163b 677 ComplianceTest.DownLinkCounter = 0;
mluis 7:5077515c163b 678 ComplianceTest.Running = false;
mluis 7:5077515c163b 679
mluis 7:5077515c163b 680 MibRequestConfirm_t mibReq;
mluis 7:5077515c163b 681 mibReq.Type = MIB_ADR;
mluis 7:5077515c163b 682 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 7:5077515c163b 683 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 7:5077515c163b 684 #if defined( USE_BAND_868 )
mluis 7:5077515c163b 685 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 7:5077515c163b 686 #endif
mluis 7:5077515c163b 687
mluis 5:62862ef9480b 688 mlmeReq.Type = MLME_JOIN;
mluis 5:62862ef9480b 689
mluis 5:62862ef9480b 690 mlmeReq.Req.Join.DevEui = DevEui;
mluis 5:62862ef9480b 691 mlmeReq.Req.Join.AppEui = AppEui;
mluis 5:62862ef9480b 692 mlmeReq.Req.Join.AppKey = AppKey;
mluis 7:5077515c163b 693 mlmeReq.Req.Join.NbTrials = 3;
mluis 5:62862ef9480b 694
mluis 5:62862ef9480b 695 LoRaMacMlmeRequest( &mlmeReq );
mluis 5:62862ef9480b 696 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:62862ef9480b 697 }
mluis 5:62862ef9480b 698 break;
mluis 7:5077515c163b 699 case 7: // (x)
mluis 7:5077515c163b 700 {
mluis 7:5077515c163b 701 if( mcpsIndication->BufferSize == 3 )
mluis 7:5077515c163b 702 {
mluis 7:5077515c163b 703 MlmeReq_t mlmeReq;
mluis 7:5077515c163b 704 mlmeReq.Type = MLME_TXCW;
mluis 7:5077515c163b 705 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 7:5077515c163b 706 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:5077515c163b 707 }
mluis 7:5077515c163b 708 else if( mcpsIndication->BufferSize == 7 )
mluis 7:5077515c163b 709 {
mluis 7:5077515c163b 710 MlmeReq_t mlmeReq;
mluis 7:5077515c163b 711 mlmeReq.Type = MLME_TXCW_1;
mluis 7:5077515c163b 712 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 7:5077515c163b 713 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 7:5077515c163b 714 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 7:5077515c163b 715 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:5077515c163b 716 }
mluis 7:5077515c163b 717 ComplianceTest.State = 1;
mluis 7:5077515c163b 718 }
mluis 7:5077515c163b 719 break;
mluis 0:45496a70a8a5 720 default:
mluis 0:45496a70a8a5 721 break;
mluis 0:45496a70a8a5 722 }
mluis 0:45496a70a8a5 723 }
mluis 0:45496a70a8a5 724 break;
mluis 0:45496a70a8a5 725 default:
mluis 0:45496a70a8a5 726 break;
mluis 0:45496a70a8a5 727 }
mluis 0:45496a70a8a5 728 }
mluis 0:45496a70a8a5 729
mluis 0:45496a70a8a5 730 // Switch LED 2 ON for each received downlink
mluis 0:45496a70a8a5 731 Led2State = true;
mluis 0:45496a70a8a5 732 Led2StateChanged = true;
mluis 0:45496a70a8a5 733 TimerStart( &Led2Timer );
mluis 0:45496a70a8a5 734 DownlinkStatusUpdated = true;
mluis 0:45496a70a8a5 735 }
mluis 0:45496a70a8a5 736
mluis 0:45496a70a8a5 737 /*!
mluis 0:45496a70a8a5 738 * \brief MLME-Confirm event function
mluis 0:45496a70a8a5 739 *
mluis 3:3152aa75c58d 740 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 741 * containing confirm attributes.
mluis 0:45496a70a8a5 742 */
mluis 3:3152aa75c58d 743 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 0:45496a70a8a5 744 {
mluis 7:5077515c163b 745 switch( mlmeConfirm->MlmeRequest )
mluis 0:45496a70a8a5 746 {
mluis 7:5077515c163b 747 case MLME_JOIN:
mluis 0:45496a70a8a5 748 {
mluis 7:5077515c163b 749 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 750 {
mluis 0:45496a70a8a5 751 // Status is OK, node has joined the network
mluis 0:45496a70a8a5 752 IsNetworkJoinedStatusUpdate = true;
mluis 5:62862ef9480b 753 DeviceState = DEVICE_STATE_SEND;
mluis 7:5077515c163b 754 }
mluis 7:5077515c163b 755 else
mluis 7:5077515c163b 756 {
mluis 7:5077515c163b 757 // Join was not successful. Try to join again
mluis 7:5077515c163b 758 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 759 }
mluis 7:5077515c163b 760 break;
mluis 7:5077515c163b 761 }
mluis 7:5077515c163b 762 case MLME_LINK_CHECK:
mluis 7:5077515c163b 763 {
mluis 7:5077515c163b 764 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 765 {
mluis 0:45496a70a8a5 766 // Check DemodMargin
mluis 0:45496a70a8a5 767 // Check NbGateways
mluis 0:45496a70a8a5 768 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 769 {
mluis 0:45496a70a8a5 770 ComplianceTest.LinkCheck = true;
mluis 3:3152aa75c58d 771 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 3:3152aa75c58d 772 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 0:45496a70a8a5 773 }
mluis 0:45496a70a8a5 774 }
mluis 7:5077515c163b 775 break;
mluis 0:45496a70a8a5 776 }
mluis 7:5077515c163b 777 default:
mluis 7:5077515c163b 778 break;
mluis 0:45496a70a8a5 779 }
mluis 0:45496a70a8a5 780 NextTx = true;
mluis 0:45496a70a8a5 781 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 782 }
mluis 0:45496a70a8a5 783
mluis 0:45496a70a8a5 784 /**
mluis 0:45496a70a8a5 785 * Main application entry point.
mluis 0:45496a70a8a5 786 */
mluis 0:45496a70a8a5 787 int main( void )
mluis 0:45496a70a8a5 788 {
nicocarv 8:0c5f1dac7b74 789
mluis 0:45496a70a8a5 790 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:45496a70a8a5 791 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:45496a70a8a5 792 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 793
mluis 0:45496a70a8a5 794 BoardInit( );
mluis 0:45496a70a8a5 795 SerialDisplayInit( );
mluis 0:45496a70a8a5 796
mluis 5:62862ef9480b 797 SerialDisplayUpdateEui( 5, DevEui );
mluis 5:62862ef9480b 798 SerialDisplayUpdateEui( 6, AppEui );
mluis 5:62862ef9480b 799 SerialDisplayUpdateKey( 7, AppKey );
mluis 5:62862ef9480b 800
mluis 5:62862ef9480b 801 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 5:62862ef9480b 802 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 5:62862ef9480b 803 SerialDisplayUpdateDevAddr( DevAddr );
mluis 5:62862ef9480b 804 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 5:62862ef9480b 805 SerialDisplayUpdateKey( 13, AppSKey );
mluis 5:62862ef9480b 806 #endif
mluis 5:62862ef9480b 807
mluis 0:45496a70a8a5 808 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 809
mluis 0:45496a70a8a5 810 while( 1 )
mluis 0:45496a70a8a5 811 {
mluis 0:45496a70a8a5 812 SerialRxProcess( );
mluis 0:45496a70a8a5 813 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:45496a70a8a5 814 {
mluis 0:45496a70a8a5 815 IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 816 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 817 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 818 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 819 }
mluis 0:45496a70a8a5 820 if( Led1StateChanged == true )
mluis 0:45496a70a8a5 821 {
mluis 0:45496a70a8a5 822 Led1StateChanged = false;
mluis 0:45496a70a8a5 823 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:45496a70a8a5 824 }
mluis 0:45496a70a8a5 825 if( Led2StateChanged == true )
mluis 0:45496a70a8a5 826 {
mluis 0:45496a70a8a5 827 Led2StateChanged = false;
mluis 0:45496a70a8a5 828 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 829 }
mluis 0:45496a70a8a5 830 if( Led3StateChanged == true )
mluis 0:45496a70a8a5 831 {
mluis 0:45496a70a8a5 832 Led3StateChanged = false;
mluis 0:45496a70a8a5 833 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 834 }
mluis 0:45496a70a8a5 835 if( UplinkStatusUpdated == true )
mluis 0:45496a70a8a5 836 {
mluis 0:45496a70a8a5 837 UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 838 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:45496a70a8a5 839 }
mluis 0:45496a70a8a5 840 if( DownlinkStatusUpdated == true )
mluis 0:45496a70a8a5 841 {
mluis 0:45496a70a8a5 842 DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 843 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:45496a70a8a5 844 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:45496a70a8a5 845 }
mluis 0:45496a70a8a5 846
mluis 0:45496a70a8a5 847 switch( DeviceState )
mluis 0:45496a70a8a5 848 {
mluis 0:45496a70a8a5 849 case DEVICE_STATE_INIT:
mluis 0:45496a70a8a5 850 {
mluis 0:45496a70a8a5 851 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 0:45496a70a8a5 852 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 0:45496a70a8a5 853 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 0:45496a70a8a5 854 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 0:45496a70a8a5 855 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 0:45496a70a8a5 856
mluis 0:45496a70a8a5 857 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:45496a70a8a5 858
mluis 0:45496a70a8a5 859 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 7:5077515c163b 860 TimerSetValue( &Led1Timer, 25 );
mluis 0:45496a70a8a5 861
mluis 0:45496a70a8a5 862 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 7:5077515c163b 863 TimerSetValue( &Led2Timer, 25 );
mluis 0:45496a70a8a5 864
mluis 0:45496a70a8a5 865 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 866 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 867 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 868
mluis 0:45496a70a8a5 869 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 870 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 0:45496a70a8a5 871 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 872
mluis 0:45496a70a8a5 873 #if defined( USE_BAND_868 )
mluis 0:45496a70a8a5 874 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:45496a70a8a5 875 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 3:3152aa75c58d 876
mluis 7:5077515c163b 877 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 3:3152aa75c58d 878 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 3:3152aa75c58d 879 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 3:3152aa75c58d 880 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 3:3152aa75c58d 881 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 3:3152aa75c58d 882 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
nicocarv 10:cac88ae7d9af 883 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
nicocarv 10:cac88ae7d9af 884 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 5:62862ef9480b 885
nicocarv 10:cac88ae7d9af 886 //mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
nicocarv 10:cac88ae7d9af 887 //mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_0 };
nicocarv 10:cac88ae7d9af 888 //LoRaMacMibSetRequestConfirm( &mibReq );
mluis 7:5077515c163b 889
nicocarv 10:cac88ae7d9af 890 //mibReq.Type = MIB_RX2_CHANNEL;
nicocarv 10:cac88ae7d9af 891 //mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_0 };
nicocarv 10:cac88ae7d9af 892 //LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:3152aa75c58d 893 #endif
mluis 3:3152aa75c58d 894
mluis 0:45496a70a8a5 895 #endif
mluis 0:45496a70a8a5 896 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 897 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 898 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:45496a70a8a5 899
mluis 0:45496a70a8a5 900 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:45496a70a8a5 901
mluis 0:45496a70a8a5 902 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 903 break;
mluis 0:45496a70a8a5 904 }
mluis 0:45496a70a8a5 905 case DEVICE_STATE_JOIN:
mluis 0:45496a70a8a5 906 {
mluis 0:45496a70a8a5 907 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:45496a70a8a5 908 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 909
mluis 0:45496a70a8a5 910 mlmeReq.Type = MLME_JOIN;
mluis 0:45496a70a8a5 911
mluis 0:45496a70a8a5 912 mlmeReq.Req.Join.DevEui = DevEui;
mluis 0:45496a70a8a5 913 mlmeReq.Req.Join.AppEui = AppEui;
mluis 0:45496a70a8a5 914 mlmeReq.Req.Join.AppKey = AppKey;
mluis 0:45496a70a8a5 915
mluis 0:45496a70a8a5 916 if( NextTx == true )
mluis 0:45496a70a8a5 917 {
mluis 0:45496a70a8a5 918 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 919 }
mluis 5:62862ef9480b 920 DeviceState = DEVICE_STATE_SLEEP;
mluis 0:45496a70a8a5 921 #else
mluis 0:45496a70a8a5 922 mibReq.Type = MIB_NET_ID;
mluis 0:45496a70a8a5 923 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 0:45496a70a8a5 924 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 925
mluis 0:45496a70a8a5 926 mibReq.Type = MIB_DEV_ADDR;
mluis 0:45496a70a8a5 927 mibReq.Param.DevAddr = DevAddr;
mluis 0:45496a70a8a5 928 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 929
mluis 0:45496a70a8a5 930 mibReq.Type = MIB_NWK_SKEY;
mluis 0:45496a70a8a5 931 mibReq.Param.NwkSKey = NwkSKey;
mluis 0:45496a70a8a5 932 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 933
mluis 0:45496a70a8a5 934 mibReq.Type = MIB_APP_SKEY;
mluis 0:45496a70a8a5 935 mibReq.Param.AppSKey = AppSKey;
mluis 0:45496a70a8a5 936 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 937
mluis 0:45496a70a8a5 938 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 939 mibReq.Param.IsNetworkJoined = true;
mluis 0:45496a70a8a5 940 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 941
mluis 0:45496a70a8a5 942 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 943 #endif
mluis 0:45496a70a8a5 944 IsNetworkJoinedStatusUpdate = true;
mluis 0:45496a70a8a5 945 break;
mluis 0:45496a70a8a5 946 }
mluis 0:45496a70a8a5 947 case DEVICE_STATE_SEND:
mluis 0:45496a70a8a5 948 {
mluis 0:45496a70a8a5 949 if( NextTx == true )
mluis 0:45496a70a8a5 950 {
mluis 0:45496a70a8a5 951 SerialDisplayUpdateUplinkAcked( false );
mluis 0:45496a70a8a5 952 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:45496a70a8a5 953 PrepareTxFrame( AppPort );
nicocarv 8:0c5f1dac7b74 954 PERIODS_COUNT = PERIODS_COUNT + 1;
nicocarv 8:0c5f1dac7b74 955
mluis 0:45496a70a8a5 956 NextTx = SendFrame( );
mluis 0:45496a70a8a5 957 }
mluis 0:45496a70a8a5 958 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 959 {
mluis 3:3152aa75c58d 960 // Schedule next packet transmission
mluis 7:5077515c163b 961 TxDutyCycleTime = 5000; // 5000 ms
mluis 0:45496a70a8a5 962 }
mluis 0:45496a70a8a5 963 else
mluis 0:45496a70a8a5 964 {
nicocarv 8:0c5f1dac7b74 965 if ( PERIODS_COUNT > APP_N_PERIOD_TX - 1)
nicocarv 8:0c5f1dac7b74 966 {
nicocarv 8:0c5f1dac7b74 967 // Last Tx sent so period of tx finisehd. We wait time between perdiods
nicocarv 8:0c5f1dac7b74 968 PERIODS_COUNT = 0;
nicocarv 8:0c5f1dac7b74 969 TxDutyCycleTime = PERIOD_TX_DUTYCYCLE;
nicocarv 8:0c5f1dac7b74 970 }
nicocarv 8:0c5f1dac7b74 971 else
nicocarv 8:0c5f1dac7b74 972 {
nicocarv 8:0c5f1dac7b74 973 // Schedule next packet transmission
nicocarv 8:0c5f1dac7b74 974 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
nicocarv 8:0c5f1dac7b74 975 }
mluis 0:45496a70a8a5 976 }
mluis 0:45496a70a8a5 977 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:45496a70a8a5 978 break;
mluis 0:45496a70a8a5 979 }
mluis 0:45496a70a8a5 980 case DEVICE_STATE_CYCLE:
mluis 0:45496a70a8a5 981 {
mluis 3:3152aa75c58d 982 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:3152aa75c58d 983
mluis 0:45496a70a8a5 984 // Schedule next packet transmission
mluis 0:45496a70a8a5 985 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:45496a70a8a5 986 TimerStart( &TxNextPacketTimer );
mluis 0:45496a70a8a5 987 break;
mluis 0:45496a70a8a5 988 }
mluis 0:45496a70a8a5 989 case DEVICE_STATE_SLEEP:
mluis 0:45496a70a8a5 990 {
mluis 0:45496a70a8a5 991 // Wake up through events
mluis 0:45496a70a8a5 992 break;
mluis 0:45496a70a8a5 993 }
mluis 0:45496a70a8a5 994 default:
mluis 0:45496a70a8a5 995 {
mluis 0:45496a70a8a5 996 DeviceState = DEVICE_STATE_INIT;
mluis 0:45496a70a8a5 997 break;
mluis 0:45496a70a8a5 998 }
mluis 0:45496a70a8a5 999 }
mluis 0:45496a70a8a5 1000 }
mluis 0:45496a70a8a5 1001 }