LoRaWAN demo 76 bootcamp

Dependencies:   mbed DigitDisplay Chainable_RGB_LED LoRaWAN-lib SX1276Lib

Committer:
mluis
Date:
Thu Mar 17 09:32:46 2016 +0000
Revision:
0:cb80564f40e1
Child:
1:21e3eef8200f
Project creation based on LoRaWAN-demo-76

Who changed what in which revision?

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