LoRaWAN VIT

Dependencies:   mbed DigitDisplay Chainable_RGB_LED LoRaWAN-lib SX1276Lib

Committer:
spcores
Date:
Tue Apr 12 05:21:37 2016 +0000
Revision:
1:c365eaf833bb
Parent:
0:cb80564f40e1
Child:
2:66f94cef0765
Version 1.0

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 */
spcores 1:c365eaf833bb 55 #define LORAWAN_ADR_ON 0
mluis 0:cb80564f40e1 56
spcores 1:c365eaf833bb 57 #if defined( USE_BAND_868 ) || defined( USE_BAND_865 )
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 */
spcores 1:c365eaf833bb 73 #define LORAWAN_APP_PORT 5
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 )
spcores 1:c365eaf833bb 79 #define LORAWAN_APP_DATA_SIZE 7 //6
mluis 0:cb80564f40e1 80
mluis 0:cb80564f40e1 81 #else
spcores 1:c365eaf833bb 82 #define LORAWAN_APP_DATA_SIZE 6 //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 */
spcores 1:c365eaf833bb 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 */
spcores 1:c365eaf833bb 194 AnalogIn LightSensor( 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
spcores 1:c365eaf833bb 201 /* LED For Customer */
spcores 1:c365eaf833bb 202 DigitalOut myLed ( D8 );
spcores 1:c365eaf833bb 203
spcores 1:c365eaf833bb 204 /**
spcores 1:c365eaf833bb 205 Interrupt Event by SP
spcores 1:c365eaf833bb 206 **/
spcores 1:c365eaf833bb 207 InterruptIn buttonPressed(D7);
spcores 1:c365eaf833bb 208 uint8_t buttonCount = 0;
spcores 1:c365eaf833bb 209 bool buttonStatus = false; //OFF = false & ON = true
spcores 1:c365eaf833bb 210 bool ledStatus = false; //OFF = false & ON = true
spcores 1:c365eaf833bb 211
spcores 1:c365eaf833bb 212 // Interrupt Handler for User Button
spcores 1:c365eaf833bb 213 void incButtonCount()
spcores 1:c365eaf833bb 214 {
spcores 1:c365eaf833bb 215 buttonCount++;
spcores 1:c365eaf833bb 216 buttonStatus=!buttonStatus;
spcores 1:c365eaf833bb 217 if (buttonStatus == 1)
spcores 1:c365eaf833bb 218 {
spcores 1:c365eaf833bb 219 ledStatus=1;
spcores 1:c365eaf833bb 220 myLed=1;
spcores 1:c365eaf833bb 221 }
spcores 1:c365eaf833bb 222 else
spcores 1:c365eaf833bb 223 {
spcores 1:c365eaf833bb 224 ledStatus = 0;
spcores 1:c365eaf833bb 225 myLed = 0;
spcores 1:c365eaf833bb 226 }
spcores 1:c365eaf833bb 227
spcores 1:c365eaf833bb 228 }
spcores 1:c365eaf833bb 229
mluis 0:cb80564f40e1 230 /*!
mluis 0:cb80564f40e1 231 * Device states
mluis 0:cb80564f40e1 232 */
mluis 0:cb80564f40e1 233 static enum eDevicState
mluis 0:cb80564f40e1 234 {
mluis 0:cb80564f40e1 235 DEVICE_STATE_INIT,
mluis 0:cb80564f40e1 236 DEVICE_STATE_JOIN,
mluis 0:cb80564f40e1 237 DEVICE_STATE_SEND,
mluis 0:cb80564f40e1 238 DEVICE_STATE_CYCLE,
mluis 0:cb80564f40e1 239 DEVICE_STATE_SLEEP
mluis 0:cb80564f40e1 240 }DeviceState;
mluis 0:cb80564f40e1 241
mluis 0:cb80564f40e1 242 /*!
mluis 0:cb80564f40e1 243 * LoRaWAN compliance tests support data
mluis 0:cb80564f40e1 244 */
mluis 0:cb80564f40e1 245 struct ComplianceTest_s
mluis 0:cb80564f40e1 246 {
mluis 0:cb80564f40e1 247 bool Running;
mluis 0:cb80564f40e1 248 uint8_t State;
mluis 0:cb80564f40e1 249 bool IsTxConfirmed;
mluis 0:cb80564f40e1 250 uint8_t AppPort;
mluis 0:cb80564f40e1 251 uint8_t AppDataSize;
mluis 0:cb80564f40e1 252 uint8_t *AppDataBuffer;
mluis 0:cb80564f40e1 253 uint16_t DownLinkCounter;
mluis 0:cb80564f40e1 254 bool LinkCheck;
mluis 0:cb80564f40e1 255 uint8_t DemodMargin;
mluis 0:cb80564f40e1 256 uint8_t NbGateways;
mluis 0:cb80564f40e1 257 }ComplianceTest;
mluis 0:cb80564f40e1 258
mluis 0:cb80564f40e1 259 /*
mluis 0:cb80564f40e1 260 * SerialDisplay managment variables
mluis 0:cb80564f40e1 261 */
mluis 0:cb80564f40e1 262
mluis 0:cb80564f40e1 263 /*!
mluis 0:cb80564f40e1 264 * Indicates if the MAC layer network join status has changed.
mluis 0:cb80564f40e1 265 */
mluis 0:cb80564f40e1 266 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:cb80564f40e1 267
mluis 0:cb80564f40e1 268 /*!
mluis 0:cb80564f40e1 269 * Strucure containing the Uplink status
mluis 0:cb80564f40e1 270 */
mluis 0:cb80564f40e1 271 struct sLoRaMacUplinkStatus
mluis 0:cb80564f40e1 272 {
mluis 0:cb80564f40e1 273 uint8_t Acked;
mluis 0:cb80564f40e1 274 int8_t Datarate;
mluis 0:cb80564f40e1 275 uint16_t UplinkCounter;
mluis 0:cb80564f40e1 276 uint8_t Port;
mluis 0:cb80564f40e1 277 uint8_t *Buffer;
mluis 0:cb80564f40e1 278 uint8_t BufferSize;
mluis 0:cb80564f40e1 279 }LoRaMacUplinkStatus;
mluis 0:cb80564f40e1 280 volatile bool UplinkStatusUpdated = false;
mluis 0:cb80564f40e1 281
mluis 0:cb80564f40e1 282 /*!
mluis 0:cb80564f40e1 283 * Strucure containing the Downlink status
mluis 0:cb80564f40e1 284 */
mluis 0:cb80564f40e1 285 struct sLoRaMacDownlinkStatus
mluis 0:cb80564f40e1 286 {
mluis 0:cb80564f40e1 287 int16_t Rssi;
mluis 0:cb80564f40e1 288 int8_t Snr;
mluis 0:cb80564f40e1 289 uint16_t DownlinkCounter;
mluis 0:cb80564f40e1 290 bool RxData;
mluis 0:cb80564f40e1 291 uint8_t Port;
mluis 0:cb80564f40e1 292 uint8_t *Buffer;
mluis 0:cb80564f40e1 293 uint8_t BufferSize;
mluis 0:cb80564f40e1 294 }LoRaMacDownlinkStatus;
mluis 0:cb80564f40e1 295 volatile bool DownlinkStatusUpdated = false;
mluis 0:cb80564f40e1 296
mluis 0:cb80564f40e1 297 void SerialDisplayRefresh( void )
mluis 0:cb80564f40e1 298 {
mluis 0:cb80564f40e1 299 MibRequestConfirm_t mibReq;
mluis 0:cb80564f40e1 300
mluis 0:cb80564f40e1 301 SerialDisplayInit( );
mluis 0:cb80564f40e1 302 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:cb80564f40e1 303
mluis 0:cb80564f40e1 304 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:cb80564f40e1 305 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:cb80564f40e1 306 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:cb80564f40e1 307 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:cb80564f40e1 308 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:cb80564f40e1 309 #else
mluis 0:cb80564f40e1 310 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:cb80564f40e1 311 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:cb80564f40e1 312 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:cb80564f40e1 313 #endif
mluis 0:cb80564f40e1 314
mluis 0:cb80564f40e1 315 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:cb80564f40e1 316 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 317 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:cb80564f40e1 318
mluis 0:cb80564f40e1 319 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
spcores 1:c365eaf833bb 320 #if defined( USE_BAND_868 ) || defined ( USE_BAND_865 )
mluis 0:cb80564f40e1 321 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:cb80564f40e1 322 #else
mluis 0:cb80564f40e1 323 SerialDisplayUpdateDutyCycle( false );
mluis 0:cb80564f40e1 324 #endif
mluis 0:cb80564f40e1 325 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:cb80564f40e1 326
mluis 0:cb80564f40e1 327 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:cb80564f40e1 328 }
mluis 0:cb80564f40e1 329
mluis 0:cb80564f40e1 330 void SerialRxProcess( void )
mluis 0:cb80564f40e1 331 {
mluis 0:cb80564f40e1 332 if( SerialDisplayReadable( ) == true )
mluis 0:cb80564f40e1 333 {
mluis 0:cb80564f40e1 334 switch( SerialDisplayGetChar( ) )
mluis 0:cb80564f40e1 335 {
mluis 0:cb80564f40e1 336 case 'R':
mluis 0:cb80564f40e1 337 case 'r':
mluis 0:cb80564f40e1 338 // Refresh Serial screen
mluis 0:cb80564f40e1 339 SerialDisplayRefresh( );
mluis 0:cb80564f40e1 340 break;
mluis 0:cb80564f40e1 341 default:
mluis 0:cb80564f40e1 342 break;
mluis 0:cb80564f40e1 343 }
mluis 0:cb80564f40e1 344 }
mluis 0:cb80564f40e1 345 }
mluis 0:cb80564f40e1 346
mluis 0:cb80564f40e1 347 /*!
mluis 0:cb80564f40e1 348 * \brief Prepares the payload of the frame
mluis 0:cb80564f40e1 349 */
mluis 0:cb80564f40e1 350 static void PrepareTxFrame( uint8_t port )
mluis 0:cb80564f40e1 351 {
mluis 0:cb80564f40e1 352 uint32_t tempValue;
mluis 0:cb80564f40e1 353
mluis 0:cb80564f40e1 354 switch( port )
mluis 0:cb80564f40e1 355 {
spcores 1:c365eaf833bb 356 case 5:
spcores 1:c365eaf833bb 357 tempValue = LightSensor.read_u16();
spcores 1:c365eaf833bb 358 AppData[0] = (( tempValue & 0xFF00 ) >> 8 ) & 0xFF;
spcores 1:c365eaf833bb 359 AppData[1] = ( tempValue & 0x00FF );
spcores 1:c365eaf833bb 360 AppData[2] = 44; // Sending ','
spcores 1:c365eaf833bb 361 AppData[3] = ledStatus + 48; //Send LED status ON/OFF
spcores 1:c365eaf833bb 362
spcores 1:c365eaf833bb 363 //printf("Light Sensor = %d , %x", tempValue, tempValue);
spcores 1:c365eaf833bb 364 break;
spcores 1:c365eaf833bb 365
mluis 0:cb80564f40e1 366 case 10:
mluis 0:cb80564f40e1 367 {
spcores 1:c365eaf833bb 368 AppData[0] = 72;
spcores 1:c365eaf833bb 369 AppData[1] = 69;
spcores 1:c365eaf833bb 370 AppData[2] = 76;
spcores 1:c365eaf833bb 371 AppData[3] = 76;
spcores 1:c365eaf833bb 372 AppData[4] = 79;
spcores 1:c365eaf833bb 373
spcores 1:c365eaf833bb 374 tempValue = buttonStatus + 48;
spcores 1:c365eaf833bb 375 AppData[5] = tempValue;
spcores 1:c365eaf833bb 376
spcores 1:c365eaf833bb 377 buttonCount = 0;
spcores 1:c365eaf833bb 378 /*
mluis 0:cb80564f40e1 379 tempValue = ( uint32_t )( LightValue * 1000000.0 );
mluis 0:cb80564f40e1 380 AppData[0] = LightMode;
mluis 0:cb80564f40e1 381 AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF;
mluis 0:cb80564f40e1 382 AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF;
mluis 0:cb80564f40e1 383 AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF;
mluis 0:cb80564f40e1 384 AppData[4] = ( tempValue & 0x000000FF );
spcores 1:c365eaf833bb 385 */
mluis 0:cb80564f40e1 386 }
mluis 0:cb80564f40e1 387 break;
mluis 0:cb80564f40e1 388 case 15:
mluis 0:cb80564f40e1 389 {
mluis 0:cb80564f40e1 390 AppData[0] = AppLedStateOn;
mluis 0:cb80564f40e1 391 if( IsTxConfirmed == true )
mluis 0:cb80564f40e1 392 {
mluis 0:cb80564f40e1 393 AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
mluis 0:cb80564f40e1 394 AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter;
mluis 0:cb80564f40e1 395 AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8;
mluis 0:cb80564f40e1 396 AppData[4] = LoRaMacDownlinkStatus.Rssi;
mluis 0:cb80564f40e1 397 AppData[5] = LoRaMacDownlinkStatus.Snr;
mluis 0:cb80564f40e1 398 }
mluis 0:cb80564f40e1 399 }
mluis 0:cb80564f40e1 400 break;
mluis 0:cb80564f40e1 401 case 224:
mluis 0:cb80564f40e1 402 if( ComplianceTest.LinkCheck == true )
mluis 0:cb80564f40e1 403 {
mluis 0:cb80564f40e1 404 ComplianceTest.LinkCheck = false;
mluis 0:cb80564f40e1 405 AppDataSize = 3;
mluis 0:cb80564f40e1 406 AppData[0] = 5;
mluis 0:cb80564f40e1 407 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:cb80564f40e1 408 AppData[2] = ComplianceTest.NbGateways;
mluis 0:cb80564f40e1 409 ComplianceTest.State = 1;
mluis 0:cb80564f40e1 410 }
mluis 0:cb80564f40e1 411 else
mluis 0:cb80564f40e1 412 {
mluis 0:cb80564f40e1 413 switch( ComplianceTest.State )
mluis 0:cb80564f40e1 414 {
mluis 0:cb80564f40e1 415 case 4:
mluis 0:cb80564f40e1 416 ComplianceTest.State = 1;
mluis 0:cb80564f40e1 417 break;
mluis 0:cb80564f40e1 418 case 1:
mluis 0:cb80564f40e1 419 AppDataSize = 2;
mluis 0:cb80564f40e1 420 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:cb80564f40e1 421 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:cb80564f40e1 422 break;
mluis 0:cb80564f40e1 423 }
mluis 0:cb80564f40e1 424 }
mluis 0:cb80564f40e1 425 break;
mluis 0:cb80564f40e1 426 default:
mluis 0:cb80564f40e1 427 break;
mluis 0:cb80564f40e1 428 }
mluis 0:cb80564f40e1 429 }
mluis 0:cb80564f40e1 430
mluis 0:cb80564f40e1 431 /*!
mluis 0:cb80564f40e1 432 * \brief Prepares the payload of the frame
mluis 0:cb80564f40e1 433 *
mluis 0:cb80564f40e1 434 * \retval [0: frame could be send, 1: error]
mluis 0:cb80564f40e1 435 */
mluis 0:cb80564f40e1 436 static bool SendFrame( void )
mluis 0:cb80564f40e1 437 {
mluis 0:cb80564f40e1 438 McpsReq_t mcpsReq;
mluis 0:cb80564f40e1 439 LoRaMacTxInfo_t txInfo;
mluis 0:cb80564f40e1 440
mluis 0:cb80564f40e1 441 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:cb80564f40e1 442 {
mluis 0:cb80564f40e1 443 // Send empty frame in order to flush MAC commands
mluis 0:cb80564f40e1 444 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:cb80564f40e1 445 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:cb80564f40e1 446 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:cb80564f40e1 447 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:cb80564f40e1 448
mluis 0:cb80564f40e1 449 LoRaMacUplinkStatus.Acked = false;
mluis 0:cb80564f40e1 450 LoRaMacUplinkStatus.Port = 0;
mluis 0:cb80564f40e1 451 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:cb80564f40e1 452 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:cb80564f40e1 453 SerialDisplayUpdateFrameType( false );
mluis 0:cb80564f40e1 454 }
mluis 0:cb80564f40e1 455 else
mluis 0:cb80564f40e1 456 {
mluis 0:cb80564f40e1 457 LoRaMacUplinkStatus.Acked = false;
mluis 0:cb80564f40e1 458 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:cb80564f40e1 459 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:cb80564f40e1 460 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:cb80564f40e1 461 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:cb80564f40e1 462
mluis 0:cb80564f40e1 463 if( IsTxConfirmed == false )
mluis 0:cb80564f40e1 464 {
mluis 0:cb80564f40e1 465 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:cb80564f40e1 466 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:cb80564f40e1 467 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:cb80564f40e1 468 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:cb80564f40e1 469 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:cb80564f40e1 470 }
mluis 0:cb80564f40e1 471 else
mluis 0:cb80564f40e1 472 {
mluis 0:cb80564f40e1 473 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:cb80564f40e1 474 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:cb80564f40e1 475 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:cb80564f40e1 476 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 0:cb80564f40e1 477 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:cb80564f40e1 478 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:cb80564f40e1 479 }
mluis 0:cb80564f40e1 480 }
mluis 0:cb80564f40e1 481
mluis 0:cb80564f40e1 482 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:cb80564f40e1 483 {
mluis 0:cb80564f40e1 484 return false;
mluis 0:cb80564f40e1 485 }
mluis 0:cb80564f40e1 486 return true;
mluis 0:cb80564f40e1 487 }
mluis 0:cb80564f40e1 488
mluis 0:cb80564f40e1 489 /*!
mluis 0:cb80564f40e1 490 * \brief Function executed on TxNextPacket Timeout event
mluis 0:cb80564f40e1 491 */
mluis 0:cb80564f40e1 492 static void OnTxNextPacketTimerEvent( void )
mluis 0:cb80564f40e1 493 {
mluis 0:cb80564f40e1 494 MibRequestConfirm_t mibReq;
mluis 0:cb80564f40e1 495 LoRaMacStatus_t status;
mluis 0:cb80564f40e1 496
mluis 0:cb80564f40e1 497 TimerStop( &TxNextPacketTimer );
mluis 0:cb80564f40e1 498
mluis 0:cb80564f40e1 499 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:cb80564f40e1 500 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 501
mluis 0:cb80564f40e1 502 if( status == LORAMAC_STATUS_OK )
mluis 0:cb80564f40e1 503 {
mluis 0:cb80564f40e1 504 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:cb80564f40e1 505 {
mluis 0:cb80564f40e1 506 DeviceState = DEVICE_STATE_SEND;
mluis 0:cb80564f40e1 507 NextTx = true;
mluis 0:cb80564f40e1 508 }
mluis 0:cb80564f40e1 509 else
mluis 0:cb80564f40e1 510 {
mluis 0:cb80564f40e1 511 DeviceState = DEVICE_STATE_JOIN;
mluis 0:cb80564f40e1 512 }
mluis 0:cb80564f40e1 513 }
mluis 0:cb80564f40e1 514 }
mluis 0:cb80564f40e1 515
mluis 0:cb80564f40e1 516 /*!
mluis 0:cb80564f40e1 517 * \brief Function executed on Led 1 Timeout event
mluis 0:cb80564f40e1 518 */
mluis 0:cb80564f40e1 519 static void OnLed1TimerEvent( void )
mluis 0:cb80564f40e1 520 {
mluis 0:cb80564f40e1 521 TimerStop( &Led1Timer );
mluis 0:cb80564f40e1 522 // Switch LED 1 OFF
mluis 0:cb80564f40e1 523 Led1State = false;
mluis 0:cb80564f40e1 524 Led1StateChanged = true;
mluis 0:cb80564f40e1 525 }
mluis 0:cb80564f40e1 526
mluis 0:cb80564f40e1 527 /*!
mluis 0:cb80564f40e1 528 * \brief Function executed on Led 2 Timeout event
mluis 0:cb80564f40e1 529 */
mluis 0:cb80564f40e1 530 static void OnLed2TimerEvent( void )
mluis 0:cb80564f40e1 531 {
mluis 0:cb80564f40e1 532 TimerStop( &Led2Timer );
mluis 0:cb80564f40e1 533 // Switch LED 2 OFF
mluis 0:cb80564f40e1 534 Led2State = false;
mluis 0:cb80564f40e1 535 Led2StateChanged = true;
mluis 0:cb80564f40e1 536 }
mluis 0:cb80564f40e1 537
mluis 0:cb80564f40e1 538 /*!
mluis 0:cb80564f40e1 539 * \brief Function executed on Buzzer Timeout event
mluis 0:cb80564f40e1 540 */
mluis 0:cb80564f40e1 541 static void OnBuzTimerEvent( void )
mluis 0:cb80564f40e1 542 {
mluis 0:cb80564f40e1 543 buzzer = 0;
mluis 0:cb80564f40e1 544 BuzTimer.detach( );
mluis 0:cb80564f40e1 545 }
mluis 0:cb80564f40e1 546
mluis 0:cb80564f40e1 547 /*!
mluis 0:cb80564f40e1 548 * \brief MCPS-Confirm event function
mluis 0:cb80564f40e1 549 *
mluis 0:cb80564f40e1 550 * \param [IN] McpsConfirm - Pointer to the confirm structure,
mluis 0:cb80564f40e1 551 * containing confirm attributes.
mluis 0:cb80564f40e1 552 */
mluis 0:cb80564f40e1 553 static void McpsConfirm( McpsConfirm_t *McpsConfirm )
mluis 0:cb80564f40e1 554 {
mluis 0:cb80564f40e1 555 if( McpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:cb80564f40e1 556 {
mluis 0:cb80564f40e1 557 switch( McpsConfirm->McpsRequest )
mluis 0:cb80564f40e1 558 {
mluis 0:cb80564f40e1 559 case MCPS_UNCONFIRMED:
mluis 0:cb80564f40e1 560 {
mluis 0:cb80564f40e1 561 // Check Datarate
mluis 0:cb80564f40e1 562 // Check TxPower
mluis 0:cb80564f40e1 563 break;
mluis 0:cb80564f40e1 564 }
mluis 0:cb80564f40e1 565 case MCPS_CONFIRMED:
mluis 0:cb80564f40e1 566 {
mluis 0:cb80564f40e1 567 // Check Datarate
mluis 0:cb80564f40e1 568 // Check TxPower
mluis 0:cb80564f40e1 569 // Check AckReceived
mluis 0:cb80564f40e1 570 // Check NbRetries
mluis 0:cb80564f40e1 571 LoRaMacUplinkStatus.Acked = McpsConfirm->AckReceived;
mluis 0:cb80564f40e1 572 break;
mluis 0:cb80564f40e1 573 }
mluis 0:cb80564f40e1 574 case MCPS_PROPRIETARY:
mluis 0:cb80564f40e1 575 {
mluis 0:cb80564f40e1 576 break;
mluis 0:cb80564f40e1 577 }
mluis 0:cb80564f40e1 578 default:
mluis 0:cb80564f40e1 579 break;
mluis 0:cb80564f40e1 580 }
mluis 0:cb80564f40e1 581 LoRaMacUplinkStatus.Datarate = McpsConfirm->Datarate;
mluis 0:cb80564f40e1 582 LoRaMacUplinkStatus.UplinkCounter = McpsConfirm->UpLinkCounter;
mluis 0:cb80564f40e1 583
mluis 0:cb80564f40e1 584 UplinkStatusUpdated = true;
mluis 0:cb80564f40e1 585 }
mluis 0:cb80564f40e1 586 NextTx = true;
mluis 0:cb80564f40e1 587 }
mluis 0:cb80564f40e1 588
mluis 0:cb80564f40e1 589 /*!
mluis 0:cb80564f40e1 590 * \brief MCPS-Indication event function
mluis 0:cb80564f40e1 591 *
mluis 0:cb80564f40e1 592 * \param [IN] McpsIndication - Pointer to the indication structure,
mluis 0:cb80564f40e1 593 * containing indication attributes.
mluis 0:cb80564f40e1 594 */
mluis 0:cb80564f40e1 595 static void McpsIndication( McpsIndication_t *McpsIndication )
mluis 0:cb80564f40e1 596 {
mluis 0:cb80564f40e1 597 if( McpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:cb80564f40e1 598 {
mluis 0:cb80564f40e1 599 return;
mluis 0:cb80564f40e1 600 }
mluis 0:cb80564f40e1 601
mluis 0:cb80564f40e1 602 switch( McpsIndication->McpsIndication )
mluis 0:cb80564f40e1 603 {
mluis 0:cb80564f40e1 604 case MCPS_UNCONFIRMED:
mluis 0:cb80564f40e1 605 {
mluis 0:cb80564f40e1 606 break;
mluis 0:cb80564f40e1 607 }
mluis 0:cb80564f40e1 608 case MCPS_CONFIRMED:
mluis 0:cb80564f40e1 609 {
mluis 0:cb80564f40e1 610 break;
mluis 0:cb80564f40e1 611 }
mluis 0:cb80564f40e1 612 case MCPS_PROPRIETARY:
mluis 0:cb80564f40e1 613 {
mluis 0:cb80564f40e1 614 break;
mluis 0:cb80564f40e1 615 }
mluis 0:cb80564f40e1 616 case MCPS_MULTICAST:
mluis 0:cb80564f40e1 617 {
mluis 0:cb80564f40e1 618 break;
mluis 0:cb80564f40e1 619 }
mluis 0:cb80564f40e1 620 default:
mluis 0:cb80564f40e1 621 break;
mluis 0:cb80564f40e1 622 }
mluis 0:cb80564f40e1 623
mluis 0:cb80564f40e1 624 // Check Multicast
mluis 0:cb80564f40e1 625 // Check Port
mluis 0:cb80564f40e1 626 // Check Datarate
mluis 0:cb80564f40e1 627 // Check FramePending
mluis 0:cb80564f40e1 628 // Check Buffer
mluis 0:cb80564f40e1 629 // Check BufferSize
mluis 0:cb80564f40e1 630 // Check Rssi
mluis 0:cb80564f40e1 631 // Check Snr
mluis 0:cb80564f40e1 632 // Check RxSlot
mluis 0:cb80564f40e1 633 LoRaMacDownlinkStatus.Rssi = McpsIndication->Rssi;
mluis 0:cb80564f40e1 634 if( McpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:cb80564f40e1 635 {
mluis 0:cb80564f40e1 636 // Invert and divide by 4
mluis 0:cb80564f40e1 637 LoRaMacDownlinkStatus.Snr = ( ( ~McpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:cb80564f40e1 638 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:cb80564f40e1 639 }
mluis 0:cb80564f40e1 640 else
mluis 0:cb80564f40e1 641 {
mluis 0:cb80564f40e1 642 // Divide by 4
mluis 0:cb80564f40e1 643 LoRaMacDownlinkStatus.Snr = ( McpsIndication->Snr & 0xFF ) >> 2;
mluis 0:cb80564f40e1 644 }
mluis 0:cb80564f40e1 645 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 0:cb80564f40e1 646 LoRaMacDownlinkStatus.RxData = McpsIndication->RxData;
mluis 0:cb80564f40e1 647 LoRaMacDownlinkStatus.Port = McpsIndication->Port;
mluis 0:cb80564f40e1 648 LoRaMacDownlinkStatus.Buffer = McpsIndication->Buffer;
mluis 0:cb80564f40e1 649 LoRaMacDownlinkStatus.BufferSize = McpsIndication->BufferSize;
mluis 0:cb80564f40e1 650
mluis 0:cb80564f40e1 651 if( ComplianceTest.Running == true )
mluis 0:cb80564f40e1 652 {
mluis 0:cb80564f40e1 653 ComplianceTest.DownLinkCounter++;
mluis 0:cb80564f40e1 654 }
mluis 0:cb80564f40e1 655
mluis 0:cb80564f40e1 656 if( McpsIndication->RxData == true )
mluis 0:cb80564f40e1 657 {
mluis 0:cb80564f40e1 658 switch( McpsIndication->Port )
mluis 0:cb80564f40e1 659 {
mluis 0:cb80564f40e1 660 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:cb80564f40e1 661 case 2:
mluis 0:cb80564f40e1 662 if( McpsIndication->BufferSize == 1 )
mluis 0:cb80564f40e1 663 {
mluis 0:cb80564f40e1 664 AppLedStateOn = McpsIndication->Buffer[0] & 0x01;
mluis 0:cb80564f40e1 665 Led3StateChanged = true;
mluis 0:cb80564f40e1 666 }
mluis 0:cb80564f40e1 667 break;
mluis 0:cb80564f40e1 668
mluis 0:cb80564f40e1 669 case 10:
mluis 0:cb80564f40e1 670 display.write( 0, McpsIndication->Buffer[0] );
mluis 0:cb80564f40e1 671 display.write( 1, McpsIndication->Buffer[1] );
mluis 0:cb80564f40e1 672 display.write( 2, McpsIndication->Buffer[2] );
mluis 0:cb80564f40e1 673 display.write( 3, McpsIndication->Buffer[3] );
mluis 0:cb80564f40e1 674 break;
mluis 0:cb80564f40e1 675
mluis 0:cb80564f40e1 676 case 20:
mluis 0:cb80564f40e1 677 LightMode = McpsIndication->Buffer[0];
mluis 0:cb80564f40e1 678 if( LightMode )
mluis 0:cb80564f40e1 679 {
mluis 0:cb80564f40e1 680 color_led.setColorRGB(0, McpsIndication->Buffer[1], McpsIndication->Buffer[2], McpsIndication->Buffer[3] );
mluis 0:cb80564f40e1 681 }
mluis 0:cb80564f40e1 682 break;
mluis 0:cb80564f40e1 683
mluis 0:cb80564f40e1 684 case 30:
mluis 0:cb80564f40e1 685 BuzTimer.attach_us( &OnBuzTimerEvent, 200000 );
mluis 0:cb80564f40e1 686 buzzer = 1;
mluis 0:cb80564f40e1 687 break;
mluis 0:cb80564f40e1 688
mluis 0:cb80564f40e1 689 case 224:
mluis 0:cb80564f40e1 690 if( ComplianceTest.Running == false )
mluis 0:cb80564f40e1 691 {
mluis 0:cb80564f40e1 692 // Check compliance test enable command (i)
mluis 0:cb80564f40e1 693 if( ( McpsIndication->BufferSize == 4 ) &&
mluis 0:cb80564f40e1 694 ( McpsIndication->Buffer[0] == 0x01 ) &&
mluis 0:cb80564f40e1 695 ( McpsIndication->Buffer[1] == 0x01 ) &&
mluis 0:cb80564f40e1 696 ( McpsIndication->Buffer[2] == 0x01 ) &&
mluis 0:cb80564f40e1 697 ( McpsIndication->Buffer[3] == 0x01 ) )
mluis 0:cb80564f40e1 698 {
mluis 0:cb80564f40e1 699 IsTxConfirmed = false;
mluis 0:cb80564f40e1 700 AppPort = 224;
mluis 0:cb80564f40e1 701 AppDataSize = 2;
mluis 0:cb80564f40e1 702 ComplianceTest.DownLinkCounter = 0;
mluis 0:cb80564f40e1 703 ComplianceTest.LinkCheck = false;
mluis 0:cb80564f40e1 704 ComplianceTest.DemodMargin = 0;
mluis 0:cb80564f40e1 705 ComplianceTest.NbGateways = 0;
mluis 0:cb80564f40e1 706 ComplianceTest.Running = true;
mluis 0:cb80564f40e1 707 ComplianceTest.State = 1;
mluis 0:cb80564f40e1 708
mluis 0:cb80564f40e1 709 MibRequestConfirm_t mibReq;
mluis 0:cb80564f40e1 710 mibReq.Type = MIB_ADR;
mluis 0:cb80564f40e1 711 mibReq.Param.AdrEnable = true;
mluis 0:cb80564f40e1 712 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 713
spcores 1:c365eaf833bb 714 #if defined( USE_BAND_868 ) || defined( USE_BAND_865 )
mluis 0:cb80564f40e1 715 LoRaMacTestSetDutyCycleOn( false );
mluis 0:cb80564f40e1 716 #endif
mluis 0:cb80564f40e1 717 }
mluis 0:cb80564f40e1 718 }
mluis 0:cb80564f40e1 719 else
mluis 0:cb80564f40e1 720 {
mluis 0:cb80564f40e1 721 ComplianceTest.State = McpsIndication->Buffer[0];
mluis 0:cb80564f40e1 722 switch( ComplianceTest.State )
mluis 0:cb80564f40e1 723 {
mluis 0:cb80564f40e1 724 case 0: // Check compliance test disable command (ii)
mluis 0:cb80564f40e1 725 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:cb80564f40e1 726 AppPort = LORAWAN_APP_PORT;
mluis 0:cb80564f40e1 727 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:cb80564f40e1 728 ComplianceTest.DownLinkCounter = 0;
mluis 0:cb80564f40e1 729 ComplianceTest.Running = false;
mluis 0:cb80564f40e1 730
mluis 0:cb80564f40e1 731 MibRequestConfirm_t mibReq;
mluis 0:cb80564f40e1 732 mibReq.Type = MIB_ADR;
mluis 0:cb80564f40e1 733 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:cb80564f40e1 734 LoRaMacMibSetRequestConfirm( &mibReq );
spcores 1:c365eaf833bb 735 #if defined( USE_BAND_868 ) || defined( USE_BAND_865 )
mluis 0:cb80564f40e1 736 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:cb80564f40e1 737 #endif
mluis 0:cb80564f40e1 738 break;
mluis 0:cb80564f40e1 739 case 1: // (iii, iv)
mluis 0:cb80564f40e1 740 AppDataSize = 2;
mluis 0:cb80564f40e1 741 break;
mluis 0:cb80564f40e1 742 case 2: // Enable confirmed messages (v)
mluis 0:cb80564f40e1 743 IsTxConfirmed = true;
mluis 0:cb80564f40e1 744 ComplianceTest.State = 1;
mluis 0:cb80564f40e1 745 break;
mluis 0:cb80564f40e1 746 case 3: // Disable confirmed messages (vi)
mluis 0:cb80564f40e1 747 IsTxConfirmed = false;
mluis 0:cb80564f40e1 748 ComplianceTest.State = 1;
mluis 0:cb80564f40e1 749 break;
mluis 0:cb80564f40e1 750 case 4: // (vii)
mluis 0:cb80564f40e1 751 AppDataSize = McpsIndication->BufferSize;
mluis 0:cb80564f40e1 752
mluis 0:cb80564f40e1 753 AppData[0] = 4;
mluis 0:cb80564f40e1 754 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:cb80564f40e1 755 {
mluis 0:cb80564f40e1 756 AppData[i] = McpsIndication->Buffer[i] + 1;
mluis 0:cb80564f40e1 757 }
mluis 0:cb80564f40e1 758 break;
mluis 0:cb80564f40e1 759 case 5: // (viii)
mluis 0:cb80564f40e1 760 {
mluis 0:cb80564f40e1 761 MlmeReq_t mlmeReq;
mluis 0:cb80564f40e1 762 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:cb80564f40e1 763 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:cb80564f40e1 764 }
mluis 0:cb80564f40e1 765 break;
mluis 0:cb80564f40e1 766 default:
mluis 0:cb80564f40e1 767 break;
mluis 0:cb80564f40e1 768 }
mluis 0:cb80564f40e1 769 }
mluis 0:cb80564f40e1 770 break;
mluis 0:cb80564f40e1 771 default:
mluis 0:cb80564f40e1 772 break;
mluis 0:cb80564f40e1 773 }
mluis 0:cb80564f40e1 774 }
mluis 0:cb80564f40e1 775
mluis 0:cb80564f40e1 776 // Switch LED 2 ON for each received downlink
mluis 0:cb80564f40e1 777 Led2State = true;
mluis 0:cb80564f40e1 778 Led2StateChanged = true;
mluis 0:cb80564f40e1 779 TimerStart( &Led2Timer );
mluis 0:cb80564f40e1 780 DownlinkStatusUpdated = true;
mluis 0:cb80564f40e1 781 }
mluis 0:cb80564f40e1 782
mluis 0:cb80564f40e1 783 /*!
mluis 0:cb80564f40e1 784 * \brief MLME-Confirm event function
mluis 0:cb80564f40e1 785 *
mluis 0:cb80564f40e1 786 * \param [IN] MlmeConfirm - Pointer to the confirm structure,
mluis 0:cb80564f40e1 787 * containing confirm attributes.
mluis 0:cb80564f40e1 788 */
mluis 0:cb80564f40e1 789 static void MlmeConfirm( MlmeConfirm_t *MlmeConfirm )
mluis 0:cb80564f40e1 790 {
mluis 0:cb80564f40e1 791 if( MlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:cb80564f40e1 792 {
mluis 0:cb80564f40e1 793 switch( MlmeConfirm->MlmeRequest )
mluis 0:cb80564f40e1 794 {
mluis 0:cb80564f40e1 795 case MLME_JOIN:
mluis 0:cb80564f40e1 796 {
mluis 0:cb80564f40e1 797 // Status is OK, node has joined the network
mluis 0:cb80564f40e1 798 IsNetworkJoinedStatusUpdate = true;
mluis 0:cb80564f40e1 799 break;
mluis 0:cb80564f40e1 800 }
mluis 0:cb80564f40e1 801 case MLME_LINK_CHECK:
mluis 0:cb80564f40e1 802 {
mluis 0:cb80564f40e1 803 // Check DemodMargin
mluis 0:cb80564f40e1 804 // Check NbGateways
mluis 0:cb80564f40e1 805 if( ComplianceTest.Running == true )
mluis 0:cb80564f40e1 806 {
mluis 0:cb80564f40e1 807 ComplianceTest.LinkCheck = true;
mluis 0:cb80564f40e1 808 ComplianceTest.DemodMargin = MlmeConfirm->DemodMargin;
mluis 0:cb80564f40e1 809 ComplianceTest.NbGateways = MlmeConfirm->NbGateways;
mluis 0:cb80564f40e1 810 }
mluis 0:cb80564f40e1 811 break;
mluis 0:cb80564f40e1 812 }
mluis 0:cb80564f40e1 813 default:
mluis 0:cb80564f40e1 814 break;
mluis 0:cb80564f40e1 815 }
mluis 0:cb80564f40e1 816 }
mluis 0:cb80564f40e1 817 NextTx = true;
mluis 0:cb80564f40e1 818 UplinkStatusUpdated = true;
mluis 0:cb80564f40e1 819 }
mluis 0:cb80564f40e1 820
mluis 0:cb80564f40e1 821 /**
mluis 0:cb80564f40e1 822 * Main application entry point.
mluis 0:cb80564f40e1 823 */
mluis 0:cb80564f40e1 824 int main( void )
mluis 0:cb80564f40e1 825 {
spcores 1:c365eaf833bb 826 //float tempLightValue = 0.0;
mluis 0:cb80564f40e1 827
mluis 0:cb80564f40e1 828 LightMode = 0; // 0: manual, 1: automatic
mluis 0:cb80564f40e1 829 buzzer = 0; // 0: OFF, 1: ON
spcores 1:c365eaf833bb 830 myLed = 0;
mluis 0:cb80564f40e1 831
mluis 0:cb80564f40e1 832 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:cb80564f40e1 833 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:cb80564f40e1 834 MibRequestConfirm_t mibReq;
mluis 0:cb80564f40e1 835
mluis 0:cb80564f40e1 836 BoardInit( );
mluis 0:cb80564f40e1 837 SerialDisplayInit( );
spcores 1:c365eaf833bb 838 buttonPressed.rise(&incButtonCount);
spcores 1:c365eaf833bb 839
mluis 0:cb80564f40e1 840 DeviceState = DEVICE_STATE_INIT;
mluis 0:cb80564f40e1 841
mluis 0:cb80564f40e1 842 while( 1 )
mluis 0:cb80564f40e1 843 {
mluis 0:cb80564f40e1 844 SerialRxProcess( );
mluis 0:cb80564f40e1 845 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:cb80564f40e1 846 {
mluis 0:cb80564f40e1 847 IsNetworkJoinedStatusUpdate = false;
mluis 0:cb80564f40e1 848 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:cb80564f40e1 849 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 850 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:cb80564f40e1 851 }
mluis 0:cb80564f40e1 852 if( Led1StateChanged == true )
mluis 0:cb80564f40e1 853 {
mluis 0:cb80564f40e1 854 Led1StateChanged = false;
mluis 0:cb80564f40e1 855 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:cb80564f40e1 856 }
mluis 0:cb80564f40e1 857 if( Led2StateChanged == true )
mluis 0:cb80564f40e1 858 {
mluis 0:cb80564f40e1 859 Led2StateChanged = false;
mluis 0:cb80564f40e1 860 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:cb80564f40e1 861 }
mluis 0:cb80564f40e1 862 if( Led3StateChanged == true )
mluis 0:cb80564f40e1 863 {
mluis 0:cb80564f40e1 864 Led3StateChanged = false;
mluis 0:cb80564f40e1 865 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:cb80564f40e1 866 }
mluis 0:cb80564f40e1 867 if( UplinkStatusUpdated == true )
mluis 0:cb80564f40e1 868 {
mluis 0:cb80564f40e1 869 UplinkStatusUpdated = false;
mluis 0:cb80564f40e1 870 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 0:cb80564f40e1 871 }
mluis 0:cb80564f40e1 872 if( DownlinkStatusUpdated == true )
mluis 0:cb80564f40e1 873 {
mluis 0:cb80564f40e1 874 DownlinkStatusUpdated = false;
mluis 0:cb80564f40e1 875 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:cb80564f40e1 876 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:cb80564f40e1 877 }
mluis 0:cb80564f40e1 878
mluis 0:cb80564f40e1 879 switch( DeviceState )
mluis 0:cb80564f40e1 880 {
mluis 0:cb80564f40e1 881 case DEVICE_STATE_INIT:
mluis 0:cb80564f40e1 882 {
mluis 0:cb80564f40e1 883 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 0:cb80564f40e1 884 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 0:cb80564f40e1 885 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 0:cb80564f40e1 886 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 0:cb80564f40e1 887 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 0:cb80564f40e1 888
mluis 0:cb80564f40e1 889 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 0:cb80564f40e1 890
mluis 0:cb80564f40e1 891 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 0:cb80564f40e1 892 TimerSetValue( &Led1Timer, 25000 );
mluis 0:cb80564f40e1 893
mluis 0:cb80564f40e1 894 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 0:cb80564f40e1 895 TimerSetValue( &Led2Timer, 25000 );
mluis 0:cb80564f40e1 896
mluis 0:cb80564f40e1 897 mibReq.Type = MIB_ADR;
mluis 0:cb80564f40e1 898 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:cb80564f40e1 899 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 900
mluis 0:cb80564f40e1 901 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 0:cb80564f40e1 902 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 0:cb80564f40e1 903 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 904
spcores 1:c365eaf833bb 905 #if defined( USE_BAND_868 ) || defined( USE_BAND_865 )
mluis 0:cb80564f40e1 906 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 0:cb80564f40e1 907 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 0:cb80564f40e1 908 #endif
mluis 0:cb80564f40e1 909 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:cb80564f40e1 910 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:cb80564f40e1 911 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:cb80564f40e1 912
mluis 0:cb80564f40e1 913 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:cb80564f40e1 914
mluis 0:cb80564f40e1 915 DeviceState = DEVICE_STATE_JOIN;
mluis 0:cb80564f40e1 916 break;
mluis 0:cb80564f40e1 917 }
mluis 0:cb80564f40e1 918 case DEVICE_STATE_JOIN:
mluis 0:cb80564f40e1 919 {
mluis 0:cb80564f40e1 920 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 0:cb80564f40e1 921 MlmeReq_t mlmeReq;
mluis 0:cb80564f40e1 922
mluis 0:cb80564f40e1 923 mlmeReq.Type = MLME_JOIN;
mluis 0:cb80564f40e1 924
mluis 0:cb80564f40e1 925 mlmeReq.Req.Join.DevEui = DevEui;
mluis 0:cb80564f40e1 926 mlmeReq.Req.Join.AppEui = AppEui;
mluis 0:cb80564f40e1 927 mlmeReq.Req.Join.AppKey = AppKey;
mluis 0:cb80564f40e1 928
mluis 0:cb80564f40e1 929 if( NextTx == true )
mluis 0:cb80564f40e1 930 {
mluis 0:cb80564f40e1 931 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:cb80564f40e1 932 }
mluis 0:cb80564f40e1 933
mluis 0:cb80564f40e1 934 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:cb80564f40e1 935 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:cb80564f40e1 936 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:cb80564f40e1 937
mluis 0:cb80564f40e1 938 // Schedule next packet transmission
mluis 0:cb80564f40e1 939 TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE;
mluis 0:cb80564f40e1 940 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:cb80564f40e1 941
mluis 0:cb80564f40e1 942 #else
mluis 0:cb80564f40e1 943 mibReq.Type = MIB_NET_ID;
mluis 0:cb80564f40e1 944 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 0:cb80564f40e1 945 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 946
mluis 0:cb80564f40e1 947 mibReq.Type = MIB_DEV_ADDR;
mluis 0:cb80564f40e1 948 mibReq.Param.DevAddr = DevAddr;
mluis 0:cb80564f40e1 949 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 950
mluis 0:cb80564f40e1 951 mibReq.Type = MIB_NWK_SKEY;
mluis 0:cb80564f40e1 952 mibReq.Param.NwkSKey = NwkSKey;
mluis 0:cb80564f40e1 953 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 954
mluis 0:cb80564f40e1 955 mibReq.Type = MIB_APP_SKEY;
mluis 0:cb80564f40e1 956 mibReq.Param.AppSKey = AppSKey;
mluis 0:cb80564f40e1 957 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 958
mluis 0:cb80564f40e1 959 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:cb80564f40e1 960 mibReq.Param.IsNetworkJoined = true;
mluis 0:cb80564f40e1 961 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:cb80564f40e1 962
mluis 0:cb80564f40e1 963 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:cb80564f40e1 964 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:cb80564f40e1 965 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:cb80564f40e1 966 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:cb80564f40e1 967
mluis 0:cb80564f40e1 968 DeviceState = DEVICE_STATE_SEND;
mluis 0:cb80564f40e1 969 #endif
mluis 0:cb80564f40e1 970 IsNetworkJoinedStatusUpdate = true;
mluis 0:cb80564f40e1 971 break;
mluis 0:cb80564f40e1 972 }
mluis 0:cb80564f40e1 973 case DEVICE_STATE_SEND:
mluis 0:cb80564f40e1 974 {
mluis 0:cb80564f40e1 975 if( NextTx == true )
mluis 0:cb80564f40e1 976 {
mluis 0:cb80564f40e1 977 SerialDisplayUpdateUplinkAcked( false );
mluis 0:cb80564f40e1 978 SerialDisplayUpdateDonwlinkRxData( false );
mluis 0:cb80564f40e1 979 PrepareTxFrame( AppPort );
mluis 0:cb80564f40e1 980
mluis 0:cb80564f40e1 981 NextTx = SendFrame( );
mluis 0:cb80564f40e1 982
mluis 0:cb80564f40e1 983 // Switch LED 1 ON
mluis 0:cb80564f40e1 984 Led1State = true;
mluis 0:cb80564f40e1 985 Led1StateChanged = true;
mluis 0:cb80564f40e1 986 TimerStart( &Led1Timer );
mluis 0:cb80564f40e1 987 }
mluis 0:cb80564f40e1 988 if( ComplianceTest.Running == true )
mluis 0:cb80564f40e1 989 {
mluis 0:cb80564f40e1 990 // Schedule next packet transmission as soon as possible
mluis 0:cb80564f40e1 991 TxDutyCycleTime = 1000; // 1 ms
mluis 0:cb80564f40e1 992 }
mluis 0:cb80564f40e1 993 else
mluis 0:cb80564f40e1 994 {
mluis 0:cb80564f40e1 995 // Schedule next packet transmission
mluis 0:cb80564f40e1 996 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 0:cb80564f40e1 997 }
mluis 0:cb80564f40e1 998 DeviceState = DEVICE_STATE_CYCLE;
mluis 0:cb80564f40e1 999 break;
mluis 0:cb80564f40e1 1000 }
mluis 0:cb80564f40e1 1001 case DEVICE_STATE_CYCLE:
mluis 0:cb80564f40e1 1002 {
mluis 0:cb80564f40e1 1003 // Schedule next packet transmission
mluis 0:cb80564f40e1 1004 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
mluis 0:cb80564f40e1 1005 TimerStart( &TxNextPacketTimer );
mluis 0:cb80564f40e1 1006
mluis 0:cb80564f40e1 1007 DeviceState = DEVICE_STATE_SLEEP;
mluis 0:cb80564f40e1 1008 break;
mluis 0:cb80564f40e1 1009 }
mluis 0:cb80564f40e1 1010 case DEVICE_STATE_SLEEP:
mluis 0:cb80564f40e1 1011 {
mluis 0:cb80564f40e1 1012 // Wake up through events
mluis 0:cb80564f40e1 1013 break;
mluis 0:cb80564f40e1 1014 }
mluis 0:cb80564f40e1 1015 default:
mluis 0:cb80564f40e1 1016 {
mluis 0:cb80564f40e1 1017 DeviceState = DEVICE_STATE_INIT;
mluis 0:cb80564f40e1 1018 break;
mluis 0:cb80564f40e1 1019 }
mluis 0:cb80564f40e1 1020
mluis 0:cb80564f40e1 1021 }
mluis 0:cb80564f40e1 1022
spcores 1:c365eaf833bb 1023 /*
mluis 0:cb80564f40e1 1024 // Read light sensor
mluis 0:cb80564f40e1 1025 tempLightValue = LightSens.read( ) * 1.65;
mluis 0:cb80564f40e1 1026
mluis 0:cb80564f40e1 1027 LightValue = ( 1 - tempLightValue );
mluis 0:cb80564f40e1 1028
mluis 0:cb80564f40e1 1029 // Set automatic RGB from light sensor
mluis 0:cb80564f40e1 1030 if( LightMode == 0 )
mluis 0:cb80564f40e1 1031 {
mluis 0:cb80564f40e1 1032 color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
mluis 0:cb80564f40e1 1033 }
spcores 1:c365eaf833bb 1034 */
mluis 0:cb80564f40e1 1035 }
mluis 0:cb80564f40e1 1036 }