Implemented LED Indicator Patterns

Dependencies:   mbed

Committer:
uss1994
Date:
Thu Jan 03 16:12:43 2019 +0000
Revision:
12:c1cf0e717684
Parent:
11:e433cbbfd436
Child:
13:831ffc0ea56c
fixes to running test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:92bca02df485 1 /*
mluis 0:92bca02df485 2 / _____) _ | |
mluis 0:92bca02df485 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:92bca02df485 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:92bca02df485 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:92bca02df485 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:92bca02df485 7 (C)2015 Semtech
mluis 0:92bca02df485 8
mluis 0:92bca02df485 9 Description: LoRaMac classA device implementation
mluis 0:92bca02df485 10
mluis 0:92bca02df485 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:92bca02df485 12
mluis 0:92bca02df485 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:92bca02df485 14 */
mluis 0:92bca02df485 15 #include "mbed.h"
mluis 0:92bca02df485 16 #include "board.h"
mluis 0:92bca02df485 17 #include "radio.h"
mluis 0:92bca02df485 18
mluis 0:92bca02df485 19 #include "LoRaMac.h"
mluis 9:ee9dcbb9708d 20 #include "Commissioning.h"
mluis 0:92bca02df485 21 #include "SerialDisplay.h"
mluis 0:92bca02df485 22
mluis 0:92bca02df485 23 /*!
mluis 9:ee9dcbb9708d 24 * Defines the application data transmission duty cycle. 5s, value in [ms].
mluis 0:92bca02df485 25 */
mluis 9:ee9dcbb9708d 26 #define APP_TX_DUTYCYCLE 5000
mluis 1:352f608c3337 27
mluis 1:352f608c3337 28 /*!
mluis 1:352f608c3337 29 * Defines a random delay for application data transmission duty cycle. 1s,
mluis 9:ee9dcbb9708d 30 * value in [ms].
mluis 1:352f608c3337 31 */
mluis 9:ee9dcbb9708d 32 #define APP_TX_DUTYCYCLE_RND 1000
mluis 0:92bca02df485 33
mluis 0:92bca02df485 34 /*!
mluis 5:1e9f6a365854 35 * Default datarate
mluis 3:9c6f7f082151 36 */
mluis 3:9c6f7f082151 37 #define LORAWAN_DEFAULT_DATARATE DR_0
mluis 3:9c6f7f082151 38
mluis 3:9c6f7f082151 39 /*!
mluis 0:92bca02df485 40 * LoRaWAN confirmed messages
mluis 0:92bca02df485 41 */
mluis 0:92bca02df485 42 #define LORAWAN_CONFIRMED_MSG_ON true
mluis 0:92bca02df485 43
mluis 0:92bca02df485 44 /*!
mluis 3:9c6f7f082151 45 * LoRaWAN Adaptive Data Rate
mluis 0:92bca02df485 46 *
mluis 0:92bca02df485 47 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:92bca02df485 48 */
mluis 0:92bca02df485 49 #define LORAWAN_ADR_ON 1
mluis 0:92bca02df485 50
mluis 1:352f608c3337 51 #if defined( USE_BAND_868 )
mluis 1:352f608c3337 52
mluis 3:9c6f7f082151 53 #include "LoRaMacTest.h"
mluis 3:9c6f7f082151 54
mluis 0:92bca02df485 55 /*!
mluis 0:92bca02df485 56 * LoRaWAN ETSI duty cycle control enable/disable
mluis 0:92bca02df485 57 *
mluis 0:92bca02df485 58 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:92bca02df485 59 */
mluis 9:ee9dcbb9708d 60 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:92bca02df485 61
mluis 5:1e9f6a365854 62 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 5:1e9f6a365854 63
mluis 9:ee9dcbb9708d 64 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 65
mluis 5:1e9f6a365854 66 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 67 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 68 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 69 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 70 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 5:1e9f6a365854 71 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 5:1e9f6a365854 72 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 5:1e9f6a365854 73
mluis 5:1e9f6a365854 74 #endif
mluis 5:1e9f6a365854 75
mluis 1:352f608c3337 76 #endif
mluis 1:352f608c3337 77
mluis 0:92bca02df485 78 /*!
mluis 0:92bca02df485 79 * LoRaWAN application port
mluis 0:92bca02df485 80 */
mluis 0:92bca02df485 81 #define LORAWAN_APP_PORT 15
mluis 0:92bca02df485 82
mluis 0:92bca02df485 83 /*!
mluis 0:92bca02df485 84 * User application data buffer size
mluis 0:92bca02df485 85 */
uss1994 10:9a4efdd07a77 86 #define LORAWAN_APP_DATA_SIZE 41
mluis 0:92bca02df485 87
mluis 0:92bca02df485 88 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:92bca02df485 89 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:92bca02df485 90 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:92bca02df485 91
mluis 7:3173f0508a98 92 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 93
mluis 0:92bca02df485 94 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:92bca02df485 95 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:92bca02df485 96
mluis 3:9c6f7f082151 97 /*!
mluis 3:9c6f7f082151 98 * Device address
mluis 3:9c6f7f082151 99 */
mluis 3:9c6f7f082151 100 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:92bca02df485 101
mluis 3:9c6f7f082151 102 #endif
mluis 0:92bca02df485 103
mluis 0:92bca02df485 104 /*!
mluis 1:352f608c3337 105 * Application port
mluis 1:352f608c3337 106 */
mluis 1:352f608c3337 107 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 1:352f608c3337 108
mluis 1:352f608c3337 109 /*!
mluis 1:352f608c3337 110 * User application data size
mluis 1:352f608c3337 111 */
mluis 1:352f608c3337 112 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 1:352f608c3337 113
mluis 1:352f608c3337 114 /*!
mluis 1:352f608c3337 115 * User application data buffer size
mluis 1:352f608c3337 116 */
mluis 1:352f608c3337 117 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 1:352f608c3337 118
mluis 1:352f608c3337 119 /*!
mluis 0:92bca02df485 120 * User application data
mluis 0:92bca02df485 121 */
mluis 1:352f608c3337 122 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 1:352f608c3337 123
mluis 1:352f608c3337 124 /*!
mluis 1:352f608c3337 125 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 1:352f608c3337 126 */
mluis 1:352f608c3337 127 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:92bca02df485 128
mluis 0:92bca02df485 129 /*!
mluis 0:92bca02df485 130 * Defines the application data transmission duty cycle
mluis 0:92bca02df485 131 */
mluis 0:92bca02df485 132 static uint32_t TxDutyCycleTime;
mluis 0:92bca02df485 133
mluis 1:352f608c3337 134 /*!
mluis 1:352f608c3337 135 * Timer to handle the application data transmission duty cycle
mluis 1:352f608c3337 136 */
mluis 1:352f608c3337 137 static TimerEvent_t TxNextPacketTimer;
mluis 0:92bca02df485 138
mluis 3:9c6f7f082151 139 /*!
uss1994 10:9a4efdd07a77 140 * SOME APPLICATION PARAMETERS
uss1994 10:9a4efdd07a77 141 */
uss1994 10:9a4efdd07a77 142
uss1994 10:9a4efdd07a77 143 DigitalOut myled(D7);
uss1994 10:9a4efdd07a77 144
uss1994 10:9a4efdd07a77 145 DigitalOut relayPin(D6);
uss1994 10:9a4efdd07a77 146 AnalogIn BAT_PIN(A1);
uss1994 10:9a4efdd07a77 147 AnalogIn LIGHT_1_PIN(A2);
uss1994 10:9a4efdd07a77 148 AnalogIn LIGHT_2_PIN(A5);
uss1994 10:9a4efdd07a77 149 AnalogIn VCE_PIN(PB_1);
uss1994 10:9a4efdd07a77 150
uss1994 11:e433cbbfd436 151 InterruptIn button1(USER_BUTTON);
uss1994 11:e433cbbfd436 152 volatile bool button1_pressed = false; // Used in the main loop
uss1994 11:e433cbbfd436 153 volatile bool button1_enabled = true; // Used for debouncing
uss1994 11:e433cbbfd436 154 Timeout button1_timeout; // Used for debouncing
uss1994 11:e433cbbfd436 155 int press_count;
uss1994 11:e433cbbfd436 156 Timeout buttonOptions_timeout; // Used for determining number of presses
uss1994 11:e433cbbfd436 157
uss1994 10:9a4efdd07a77 158 unsigned int time_window = 5; // Default 3600 seconds in an hour
uss1994 10:9a4efdd07a77 159 unsigned long long_interval = 604800; // Default 31557600 seconds in a year
uss1994 10:9a4efdd07a77 160 unsigned int hb_interval = 3600; // Default 86400 seconds in a day
uss1994 10:9a4efdd07a77 161 unsigned long test_interval = 86400; // Default 2592000 seconds in a month
uss1994 10:9a4efdd07a77 162
uss1994 10:9a4efdd07a77 163 time_t next_stest;
uss1994 10:9a4efdd07a77 164 time_t next_ltest;
uss1994 10:9a4efdd07a77 165
uss1994 10:9a4efdd07a77 166 uint8_t hb_data[5];
uss1994 10:9a4efdd07a77 167 uint8_t short_tdata[14];
uss1994 10:9a4efdd07a77 168 uint8_t long_tdata[41];
uss1994 10:9a4efdd07a77 169
uss1994 10:9a4efdd07a77 170 uint8_t received_data[28];
uss1994 10:9a4efdd07a77 171
uss1994 10:9a4efdd07a77 172 uint8_t *data;
uss1994 10:9a4efdd07a77 173 uint8_t data_len;
uss1994 10:9a4efdd07a77 174
uss1994 10:9a4efdd07a77 175 bool running_test;
uss1994 10:9a4efdd07a77 176 bool joining;
uss1994 10:9a4efdd07a77 177 bool received_downlink;
uss1994 10:9a4efdd07a77 178
uss1994 10:9a4efdd07a77 179 time_t current_time;
uss1994 10:9a4efdd07a77 180
uss1994 10:9a4efdd07a77 181 unsigned long last_measurement;
uss1994 10:9a4efdd07a77 182 unsigned long last_hb;
uss1994 10:9a4efdd07a77 183 unsigned long test_start;
uss1994 10:9a4efdd07a77 184
uss1994 10:9a4efdd07a77 185 int measureCount;
uss1994 10:9a4efdd07a77 186 uint8_t s_measurements[4];
uss1994 10:9a4efdd07a77 187 uint8_t l_measurements[22];
uss1994 10:9a4efdd07a77 188 uint8_t *measurements;
uss1994 10:9a4efdd07a77 189 int available_slots = 9;
uss1994 10:9a4efdd07a77 190
uss1994 10:9a4efdd07a77 191 unsigned long measurement_interval;
uss1994 10:9a4efdd07a77 192 unsigned long test_duration;
uss1994 10:9a4efdd07a77 193
uss1994 10:9a4efdd07a77 194 /*!
mluis 3:9c6f7f082151 195 * Specifies the state of the application LED
mluis 3:9c6f7f082151 196 */
mluis 3:9c6f7f082151 197 static bool AppLedStateOn = false;
mluis 3:9c6f7f082151 198 volatile bool Led3StateChanged = false;
mluis 0:92bca02df485 199 /*!
mluis 3:9c6f7f082151 200 * Timer to handle the state of LED1
mluis 0:92bca02df485 201 */
mluis 3:9c6f7f082151 202 static TimerEvent_t Led1Timer;
mluis 3:9c6f7f082151 203 volatile bool Led1State = false;
mluis 3:9c6f7f082151 204 volatile bool Led1StateChanged = false;
mluis 3:9c6f7f082151 205 /*!
mluis 3:9c6f7f082151 206 * Timer to handle the state of LED2
mluis 3:9c6f7f082151 207 */
mluis 3:9c6f7f082151 208 static TimerEvent_t Led2Timer;
mluis 3:9c6f7f082151 209 volatile bool Led2State = false;
mluis 3:9c6f7f082151 210 volatile bool Led2StateChanged = false;
mluis 0:92bca02df485 211
mluis 0:92bca02df485 212 /*!
mluis 0:92bca02df485 213 * Indicates if a new packet can be sent
mluis 0:92bca02df485 214 */
mluis 3:9c6f7f082151 215 static bool NextTx = true;
mluis 0:92bca02df485 216
mluis 3:9c6f7f082151 217 /*!
mluis 3:9c6f7f082151 218 * Device states
mluis 3:9c6f7f082151 219 */
mluis 9:ee9dcbb9708d 220 static enum eDeviceState
mluis 3:9c6f7f082151 221 {
mluis 3:9c6f7f082151 222 DEVICE_STATE_INIT,
mluis 3:9c6f7f082151 223 DEVICE_STATE_JOIN,
mluis 3:9c6f7f082151 224 DEVICE_STATE_SEND,
mluis 3:9c6f7f082151 225 DEVICE_STATE_CYCLE,
mluis 3:9c6f7f082151 226 DEVICE_STATE_SLEEP
mluis 3:9c6f7f082151 227 }DeviceState;
mluis 0:92bca02df485 228
uss1994 10:9a4efdd07a77 229 static enum eMessageType
uss1994 10:9a4efdd07a77 230 {
uss1994 10:9a4efdd07a77 231 MESSAGE_TYPE_HB,
uss1994 10:9a4efdd07a77 232 MESSAGE_TYPE_SHORT_TEST,
uss1994 10:9a4efdd07a77 233 MESSAGE_TYPE_LONG_TEST
uss1994 10:9a4efdd07a77 234 }MessageType;
uss1994 10:9a4efdd07a77 235
mluis 3:9c6f7f082151 236 /*!
mluis 3:9c6f7f082151 237 * LoRaWAN compliance tests support data
mluis 3:9c6f7f082151 238 */
mluis 3:9c6f7f082151 239 struct ComplianceTest_s
mluis 3:9c6f7f082151 240 {
mluis 3:9c6f7f082151 241 bool Running;
mluis 3:9c6f7f082151 242 uint8_t State;
mluis 3:9c6f7f082151 243 bool IsTxConfirmed;
mluis 3:9c6f7f082151 244 uint8_t AppPort;
mluis 3:9c6f7f082151 245 uint8_t AppDataSize;
mluis 3:9c6f7f082151 246 uint8_t *AppDataBuffer;
mluis 3:9c6f7f082151 247 uint16_t DownLinkCounter;
mluis 3:9c6f7f082151 248 bool LinkCheck;
mluis 3:9c6f7f082151 249 uint8_t DemodMargin;
mluis 3:9c6f7f082151 250 uint8_t NbGateways;
mluis 3:9c6f7f082151 251 }ComplianceTest;
mluis 0:92bca02df485 252
mluis 3:9c6f7f082151 253 /*
mluis 3:9c6f7f082151 254 * SerialDisplay managment variables
mluis 3:9c6f7f082151 255 */
mluis 1:352f608c3337 256
mluis 3:9c6f7f082151 257 /*!
mluis 3:9c6f7f082151 258 * Indicates if the MAC layer network join status has changed.
mluis 3:9c6f7f082151 259 */
mluis 3:9c6f7f082151 260 static bool IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 261
mluis 3:9c6f7f082151 262 /*!
mluis 3:9c6f7f082151 263 * Strucure containing the Uplink status
mluis 3:9c6f7f082151 264 */
mluis 0:92bca02df485 265 struct sLoRaMacUplinkStatus
mluis 0:92bca02df485 266 {
mluis 0:92bca02df485 267 uint8_t Acked;
mluis 0:92bca02df485 268 int8_t Datarate;
mluis 0:92bca02df485 269 uint16_t UplinkCounter;
mluis 0:92bca02df485 270 uint8_t Port;
mluis 0:92bca02df485 271 uint8_t *Buffer;
mluis 0:92bca02df485 272 uint8_t BufferSize;
mluis 0:92bca02df485 273 }LoRaMacUplinkStatus;
mluis 3:9c6f7f082151 274 volatile bool UplinkStatusUpdated = false;
mluis 0:92bca02df485 275
mluis 3:9c6f7f082151 276 /*!
mluis 3:9c6f7f082151 277 * Strucure containing the Downlink status
mluis 3:9c6f7f082151 278 */
mluis 0:92bca02df485 279 struct sLoRaMacDownlinkStatus
mluis 0:92bca02df485 280 {
mluis 0:92bca02df485 281 int16_t Rssi;
mluis 0:92bca02df485 282 int8_t Snr;
mluis 0:92bca02df485 283 uint16_t DownlinkCounter;
mluis 0:92bca02df485 284 bool RxData;
mluis 0:92bca02df485 285 uint8_t Port;
mluis 0:92bca02df485 286 uint8_t *Buffer;
mluis 0:92bca02df485 287 uint8_t BufferSize;
mluis 0:92bca02df485 288 }LoRaMacDownlinkStatus;
mluis 3:9c6f7f082151 289 volatile bool DownlinkStatusUpdated = false;
mluis 0:92bca02df485 290
mluis 0:92bca02df485 291 void SerialDisplayRefresh( void )
mluis 0:92bca02df485 292 {
mluis 3:9c6f7f082151 293 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 294
mluis 0:92bca02df485 295 SerialDisplayInit( );
mluis 0:92bca02df485 296 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:92bca02df485 297
mluis 0:92bca02df485 298 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 0:92bca02df485 299 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 3:9c6f7f082151 300 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:92bca02df485 301 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:92bca02df485 302 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 303 #endif
mluis 0:92bca02df485 304 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:92bca02df485 305 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:92bca02df485 306 SerialDisplayUpdateKey( 7, AppKey );
mluis 3:9c6f7f082151 307
mluis 3:9c6f7f082151 308 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 309 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 310 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 311
mluis 0:92bca02df485 312 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 1:352f608c3337 313 #if defined( USE_BAND_868 )
mluis 0:92bca02df485 314 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 1:352f608c3337 315 #else
mluis 1:352f608c3337 316 SerialDisplayUpdateDutyCycle( false );
mluis 1:352f608c3337 317 #endif
mluis 0:92bca02df485 318 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 0:92bca02df485 319
mluis 0:92bca02df485 320 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 321 }
mluis 0:92bca02df485 322
mluis 0:92bca02df485 323 void SerialRxProcess( void )
mluis 0:92bca02df485 324 {
mluis 0:92bca02df485 325 if( SerialDisplayReadable( ) == true )
mluis 0:92bca02df485 326 {
mluis 0:92bca02df485 327 switch( SerialDisplayGetChar( ) )
mluis 0:92bca02df485 328 {
mluis 0:92bca02df485 329 case 'R':
mluis 0:92bca02df485 330 case 'r':
mluis 0:92bca02df485 331 // Refresh Serial screen
mluis 0:92bca02df485 332 SerialDisplayRefresh( );
mluis 0:92bca02df485 333 break;
mluis 0:92bca02df485 334 default:
mluis 0:92bca02df485 335 break;
mluis 0:92bca02df485 336 }
mluis 0:92bca02df485 337 }
mluis 0:92bca02df485 338 }
mluis 0:92bca02df485 339
uss1994 11:e433cbbfd436 340 // Enables button when bouncing is over
uss1994 11:e433cbbfd436 341 void button1_enabled_cb(void)
uss1994 11:e433cbbfd436 342 {
uss1994 11:e433cbbfd436 343 button1_enabled = true;
uss1994 11:e433cbbfd436 344 }
uss1994 11:e433cbbfd436 345
uss1994 11:e433cbbfd436 346 // ISR handling button pressed event
uss1994 11:e433cbbfd436 347 void button1_onpressed_cb(void)
uss1994 11:e433cbbfd436 348 {
uss1994 11:e433cbbfd436 349 if (button1_enabled) { // Disabled while the button is bouncing
uss1994 11:e433cbbfd436 350 button1_enabled = false;
uss1994 11:e433cbbfd436 351 button1_pressed = true; // To be read by the main loop
uss1994 11:e433cbbfd436 352 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
uss1994 11:e433cbbfd436 353 }
uss1994 11:e433cbbfd436 354 }
uss1994 11:e433cbbfd436 355
uss1994 10:9a4efdd07a77 356 // **************************** COMMUNICATION PACKET DEFINITION METHODS ******************************** //
uss1994 10:9a4efdd07a77 357
uss1994 10:9a4efdd07a77 358 void heartbeat_message(uint8_t *hb_data, uint8_t *current_date) {
uss1994 10:9a4efdd07a77 359 hb_data[0] = 0xD2;
uss1994 10:9a4efdd07a77 360 memcpy(hb_data+1, current_date, 4);
uss1994 10:9a4efdd07a77 361 }
uss1994 10:9a4efdd07a77 362
uss1994 10:9a4efdd07a77 363 void short_test_result(uint8_t *short_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) {
uss1994 10:9a4efdd07a77 364 short_tdata[0] = 0xD7;
uss1994 10:9a4efdd07a77 365 memcpy(short_tdata+1, test_start_date, 4);
uss1994 10:9a4efdd07a77 366 memcpy(short_tdata+5, next_test_date, 4);
uss1994 10:9a4efdd07a77 367 short_tdata[9] = (uint8_t) 2;
uss1994 10:9a4efdd07a77 368 memcpy(short_tdata+10, measurements, 4);
uss1994 10:9a4efdd07a77 369 }
uss1994 10:9a4efdd07a77 370
uss1994 10:9a4efdd07a77 371 void long_test_result(uint8_t *long_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) {
uss1994 10:9a4efdd07a77 372 long_tdata[0] = 0xD8;
uss1994 10:9a4efdd07a77 373 memcpy(long_tdata+1, test_start_date, 4);
uss1994 10:9a4efdd07a77 374 memcpy(long_tdata+5, next_test_date, 4);
uss1994 10:9a4efdd07a77 375 long_tdata[9] = (uint8_t) 2;
uss1994 10:9a4efdd07a77 376 memcpy(long_tdata+10, measurements, 31);
uss1994 10:9a4efdd07a77 377 }
uss1994 10:9a4efdd07a77 378
mluis 0:92bca02df485 379 /*!
mluis 3:9c6f7f082151 380 * \brief Prepares the payload of the frame
mluis 0:92bca02df485 381 */
mluis 0:92bca02df485 382 static void PrepareTxFrame( uint8_t port )
mluis 0:92bca02df485 383 {
mluis 1:352f608c3337 384 switch( port )
mluis 1:352f608c3337 385 {
mluis 1:352f608c3337 386 case 15:
uss1994 10:9a4efdd07a77 387 {
uss1994 10:9a4efdd07a77 388 switch ( MessageType )
mluis 1:352f608c3337 389 {
uss1994 10:9a4efdd07a77 390 case MESSAGE_TYPE_HB:
uss1994 10:9a4efdd07a77 391 {
uss1994 10:9a4efdd07a77 392 AppDataSize = 5;
uss1994 10:9a4efdd07a77 393 heartbeat_message(AppData, (uint8_t *)&current_time);
uss1994 10:9a4efdd07a77 394 break;
uss1994 10:9a4efdd07a77 395 }
uss1994 10:9a4efdd07a77 396 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 397 {
uss1994 10:9a4efdd07a77 398 AppDataSize = 14;
uss1994 10:9a4efdd07a77 399 short_test_result(AppData, (uint8_t *)&current_time, (uint8_t *)&next_stest, s_measurements);
uss1994 10:9a4efdd07a77 400 break;
uss1994 10:9a4efdd07a77 401 }
uss1994 10:9a4efdd07a77 402 case MESSAGE_TYPE_LONG_TEST:
uss1994 10:9a4efdd07a77 403 {
uss1994 10:9a4efdd07a77 404 AppDataSize = 32;
uss1994 10:9a4efdd07a77 405 long_test_result(AppData, (uint8_t *)&current_time, (uint8_t *)&next_ltest, l_measurements);
uss1994 10:9a4efdd07a77 406 break;
uss1994 10:9a4efdd07a77 407 }
uss1994 10:9a4efdd07a77 408 default:
uss1994 10:9a4efdd07a77 409 {
uss1994 10:9a4efdd07a77 410 AppDataSize = 5;
uss1994 10:9a4efdd07a77 411 heartbeat_message(AppData, (uint8_t *)&current_time);
uss1994 10:9a4efdd07a77 412 break;
uss1994 10:9a4efdd07a77 413 }
mluis 1:352f608c3337 414 }
mluis 1:352f608c3337 415 }
mluis 1:352f608c3337 416 break;
mluis 1:352f608c3337 417 case 224:
mluis 3:9c6f7f082151 418 if( ComplianceTest.LinkCheck == true )
mluis 1:352f608c3337 419 {
mluis 3:9c6f7f082151 420 ComplianceTest.LinkCheck = false;
mluis 1:352f608c3337 421 AppDataSize = 3;
mluis 1:352f608c3337 422 AppData[0] = 5;
mluis 3:9c6f7f082151 423 AppData[1] = ComplianceTest.DemodMargin;
mluis 3:9c6f7f082151 424 AppData[2] = ComplianceTest.NbGateways;
mluis 3:9c6f7f082151 425 ComplianceTest.State = 1;
mluis 1:352f608c3337 426 }
mluis 1:352f608c3337 427 else
mluis 1:352f608c3337 428 {
mluis 3:9c6f7f082151 429 switch( ComplianceTest.State )
mluis 1:352f608c3337 430 {
mluis 1:352f608c3337 431 case 4:
mluis 3:9c6f7f082151 432 ComplianceTest.State = 1;
mluis 1:352f608c3337 433 break;
mluis 1:352f608c3337 434 case 1:
mluis 1:352f608c3337 435 AppDataSize = 2;
mluis 3:9c6f7f082151 436 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 3:9c6f7f082151 437 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 1:352f608c3337 438 break;
mluis 1:352f608c3337 439 }
mluis 1:352f608c3337 440 }
mluis 1:352f608c3337 441 break;
mluis 3:9c6f7f082151 442 default:
mluis 3:9c6f7f082151 443 break;
mluis 1:352f608c3337 444 }
mluis 0:92bca02df485 445 }
mluis 0:92bca02df485 446
mluis 3:9c6f7f082151 447 /*!
mluis 3:9c6f7f082151 448 * \brief Prepares the payload of the frame
mluis 3:9c6f7f082151 449 *
mluis 3:9c6f7f082151 450 * \retval [0: frame could be send, 1: error]
mluis 3:9c6f7f082151 451 */
mluis 0:92bca02df485 452 static bool SendFrame( void )
mluis 0:92bca02df485 453 {
mluis 3:9c6f7f082151 454 McpsReq_t mcpsReq;
mluis 3:9c6f7f082151 455 LoRaMacTxInfo_t txInfo;
mluis 9:ee9dcbb9708d 456
mluis 3:9c6f7f082151 457 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 1:352f608c3337 458 {
mluis 3:9c6f7f082151 459 // Send empty frame in order to flush MAC commands
mluis 3:9c6f7f082151 460 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 461 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 3:9c6f7f082151 462 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 3:9c6f7f082151 463 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 5:1e9f6a365854 464
mluis 3:9c6f7f082151 465 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 466 LoRaMacUplinkStatus.Port = 0;
mluis 3:9c6f7f082151 467 LoRaMacUplinkStatus.Buffer = NULL;
mluis 3:9c6f7f082151 468 LoRaMacUplinkStatus.BufferSize = 0;
mluis 3:9c6f7f082151 469 SerialDisplayUpdateFrameType( false );
mluis 1:352f608c3337 470 }
mluis 1:352f608c3337 471 else
mluis 1:352f608c3337 472 {
mluis 3:9c6f7f082151 473 LoRaMacUplinkStatus.Acked = false;
mluis 3:9c6f7f082151 474 LoRaMacUplinkStatus.Port = AppPort;
mluis 3:9c6f7f082151 475 LoRaMacUplinkStatus.Buffer = AppData;
mluis 3:9c6f7f082151 476 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 3:9c6f7f082151 477 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 3:9c6f7f082151 478
mluis 3:9c6f7f082151 479 if( IsTxConfirmed == false )
mluis 3:9c6f7f082151 480 {
mluis 3:9c6f7f082151 481 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 3:9c6f7f082151 482 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 3:9c6f7f082151 483 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 484 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 3:9c6f7f082151 485 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 486 }
mluis 3:9c6f7f082151 487 else
mluis 3:9c6f7f082151 488 {
mluis 3:9c6f7f082151 489 mcpsReq.Type = MCPS_CONFIRMED;
mluis 3:9c6f7f082151 490 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 3:9c6f7f082151 491 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 3:9c6f7f082151 492 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 4:00cf2370c99d 493 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 3:9c6f7f082151 494 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:9c6f7f082151 495 }
mluis 1:352f608c3337 496 }
mluis 1:352f608c3337 497
mluis 3:9c6f7f082151 498 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:92bca02df485 499 {
mluis 0:92bca02df485 500 return false;
mluis 0:92bca02df485 501 }
mluis 3:9c6f7f082151 502 return true;
mluis 0:92bca02df485 503 }
mluis 0:92bca02df485 504
mluis 0:92bca02df485 505 /*!
mluis 0:92bca02df485 506 * \brief Function executed on TxNextPacket Timeout event
mluis 0:92bca02df485 507 */
mluis 0:92bca02df485 508 static void OnTxNextPacketTimerEvent( void )
mluis 0:92bca02df485 509 {
mluis 3:9c6f7f082151 510 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 511 LoRaMacStatus_t status;
mluis 3:9c6f7f082151 512
mluis 0:92bca02df485 513 TimerStop( &TxNextPacketTimer );
mluis 3:9c6f7f082151 514
mluis 3:9c6f7f082151 515 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 516 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 517
mluis 3:9c6f7f082151 518 if( status == LORAMAC_STATUS_OK )
mluis 3:9c6f7f082151 519 {
mluis 3:9c6f7f082151 520 if( mibReq.Param.IsNetworkJoined == true )
mluis 3:9c6f7f082151 521 {
mluis 3:9c6f7f082151 522 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 523 NextTx = true;
mluis 3:9c6f7f082151 524 }
mluis 3:9c6f7f082151 525 else
mluis 3:9c6f7f082151 526 {
mluis 3:9c6f7f082151 527 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 528 }
mluis 3:9c6f7f082151 529 }
mluis 0:92bca02df485 530 }
mluis 0:92bca02df485 531
mluis 0:92bca02df485 532 /*!
mluis 0:92bca02df485 533 * \brief Function executed on Led 1 Timeout event
mluis 0:92bca02df485 534 */
mluis 0:92bca02df485 535 static void OnLed1TimerEvent( void )
mluis 0:92bca02df485 536 {
mluis 0:92bca02df485 537 TimerStop( &Led1Timer );
mluis 3:9c6f7f082151 538 // Switch LED 1 OFF
mluis 1:352f608c3337 539 Led1State = false;
mluis 0:92bca02df485 540 Led1StateChanged = true;
mluis 0:92bca02df485 541 }
mluis 0:92bca02df485 542
mluis 0:92bca02df485 543 /*!
mluis 0:92bca02df485 544 * \brief Function executed on Led 2 Timeout event
mluis 0:92bca02df485 545 */
mluis 0:92bca02df485 546 static void OnLed2TimerEvent( void )
mluis 0:92bca02df485 547 {
mluis 0:92bca02df485 548 TimerStop( &Led2Timer );
mluis 3:9c6f7f082151 549 // Switch LED 2 OFF
mluis 1:352f608c3337 550 Led2State = false;
mluis 0:92bca02df485 551 Led2StateChanged = true;
mluis 0:92bca02df485 552 }
mluis 0:92bca02df485 553
mluis 0:92bca02df485 554 /*!
mluis 3:9c6f7f082151 555 * \brief MCPS-Confirm event function
mluis 3:9c6f7f082151 556 *
mluis 5:1e9f6a365854 557 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 558 * containing confirm attributes.
mluis 0:92bca02df485 559 */
mluis 5:1e9f6a365854 560 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:92bca02df485 561 {
mluis 5:1e9f6a365854 562 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:92bca02df485 563 {
mluis 5:1e9f6a365854 564 switch( mcpsConfirm->McpsRequest )
mluis 3:9c6f7f082151 565 {
mluis 3:9c6f7f082151 566 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 567 {
mluis 3:9c6f7f082151 568 // Check Datarate
mluis 3:9c6f7f082151 569 // Check TxPower
mluis 3:9c6f7f082151 570 break;
mluis 3:9c6f7f082151 571 }
mluis 3:9c6f7f082151 572 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 573 {
mluis 3:9c6f7f082151 574 // Check Datarate
mluis 3:9c6f7f082151 575 // Check TxPower
mluis 3:9c6f7f082151 576 // Check AckReceived
mluis 5:1e9f6a365854 577 // Check NbTrials
mluis 5:1e9f6a365854 578 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 3:9c6f7f082151 579 break;
mluis 3:9c6f7f082151 580 }
mluis 3:9c6f7f082151 581 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 582 {
mluis 3:9c6f7f082151 583 break;
mluis 3:9c6f7f082151 584 }
mluis 3:9c6f7f082151 585 default:
mluis 3:9c6f7f082151 586 break;
mluis 3:9c6f7f082151 587 }
mluis 5:1e9f6a365854 588 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 5:1e9f6a365854 589 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 5:1e9f6a365854 590
mluis 7:3173f0508a98 591 // Switch LED 1 ON
mluis 7:3173f0508a98 592 Led1State = true;
mluis 7:3173f0508a98 593 Led1StateChanged = true;
mluis 7:3173f0508a98 594 TimerStart( &Led1Timer );
mluis 7:3173f0508a98 595
mluis 3:9c6f7f082151 596 UplinkStatusUpdated = true;
mluis 3:9c6f7f082151 597 }
mluis 3:9c6f7f082151 598 NextTx = true;
mluis 3:9c6f7f082151 599 }
mluis 3:9c6f7f082151 600
mluis 3:9c6f7f082151 601 /*!
mluis 3:9c6f7f082151 602 * \brief MCPS-Indication event function
mluis 3:9c6f7f082151 603 *
mluis 5:1e9f6a365854 604 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 3:9c6f7f082151 605 * containing indication attributes.
mluis 3:9c6f7f082151 606 */
mluis 5:1e9f6a365854 607 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 3:9c6f7f082151 608 {
mluis 5:1e9f6a365854 609 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 610 {
mluis 3:9c6f7f082151 611 return;
mluis 3:9c6f7f082151 612 }
mluis 3:9c6f7f082151 613
mluis 5:1e9f6a365854 614 switch( mcpsIndication->McpsIndication )
mluis 3:9c6f7f082151 615 {
mluis 3:9c6f7f082151 616 case MCPS_UNCONFIRMED:
mluis 3:9c6f7f082151 617 {
mluis 3:9c6f7f082151 618 break;
mluis 3:9c6f7f082151 619 }
mluis 3:9c6f7f082151 620 case MCPS_CONFIRMED:
mluis 3:9c6f7f082151 621 {
mluis 3:9c6f7f082151 622 break;
mluis 3:9c6f7f082151 623 }
mluis 3:9c6f7f082151 624 case MCPS_PROPRIETARY:
mluis 3:9c6f7f082151 625 {
mluis 3:9c6f7f082151 626 break;
mluis 3:9c6f7f082151 627 }
mluis 3:9c6f7f082151 628 case MCPS_MULTICAST:
mluis 3:9c6f7f082151 629 {
mluis 3:9c6f7f082151 630 break;
mluis 3:9c6f7f082151 631 }
mluis 3:9c6f7f082151 632 default:
mluis 3:9c6f7f082151 633 break;
mluis 3:9c6f7f082151 634 }
mluis 3:9c6f7f082151 635
mluis 3:9c6f7f082151 636 // Check Multicast
mluis 3:9c6f7f082151 637 // Check Port
mluis 3:9c6f7f082151 638 // Check Datarate
mluis 3:9c6f7f082151 639 // Check FramePending
mluis 3:9c6f7f082151 640 // Check Buffer
mluis 3:9c6f7f082151 641 // Check BufferSize
mluis 3:9c6f7f082151 642 // Check Rssi
mluis 3:9c6f7f082151 643 // Check Snr
mluis 3:9c6f7f082151 644 // Check RxSlot
mluis 5:1e9f6a365854 645 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 5:1e9f6a365854 646 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 3:9c6f7f082151 647 {
mluis 3:9c6f7f082151 648 // Invert and divide by 4
mluis 5:1e9f6a365854 649 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 3:9c6f7f082151 650 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:92bca02df485 651 }
mluis 0:92bca02df485 652 else
mluis 0:92bca02df485 653 {
mluis 3:9c6f7f082151 654 // Divide by 4
mluis 5:1e9f6a365854 655 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 3:9c6f7f082151 656 }
mluis 3:9c6f7f082151 657 LoRaMacDownlinkStatus.DownlinkCounter++;
mluis 5:1e9f6a365854 658 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 5:1e9f6a365854 659 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 5:1e9f6a365854 660 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 5:1e9f6a365854 661 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 662
mluis 3:9c6f7f082151 663 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 664 {
mluis 3:9c6f7f082151 665 ComplianceTest.DownLinkCounter++;
mluis 3:9c6f7f082151 666 }
mluis 0:92bca02df485 667
mluis 5:1e9f6a365854 668 if( mcpsIndication->RxData == true )
mluis 3:9c6f7f082151 669 {
mluis 5:1e9f6a365854 670 switch( mcpsIndication->Port )
mluis 0:92bca02df485 671 {
mluis 3:9c6f7f082151 672 case 1: // The application LED can be controlled on port 1 or 2
mluis 3:9c6f7f082151 673 case 2:
mluis 5:1e9f6a365854 674 if( mcpsIndication->BufferSize == 1 )
mluis 1:352f608c3337 675 {
mluis 5:1e9f6a365854 676 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 3:9c6f7f082151 677 Led3StateChanged = true;
mluis 3:9c6f7f082151 678 }
mluis 3:9c6f7f082151 679 break;
mluis 3:9c6f7f082151 680 case 224:
mluis 3:9c6f7f082151 681 if( ComplianceTest.Running == false )
mluis 3:9c6f7f082151 682 {
mluis 3:9c6f7f082151 683 // Check compliance test enable command (i)
mluis 5:1e9f6a365854 684 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 5:1e9f6a365854 685 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 5:1e9f6a365854 686 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 5:1e9f6a365854 687 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 5:1e9f6a365854 688 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 1:352f608c3337 689 {
mluis 3:9c6f7f082151 690 IsTxConfirmed = false;
mluis 3:9c6f7f082151 691 AppPort = 224;
mluis 3:9c6f7f082151 692 AppDataSize = 2;
mluis 3:9c6f7f082151 693 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 694 ComplianceTest.LinkCheck = false;
mluis 3:9c6f7f082151 695 ComplianceTest.DemodMargin = 0;
mluis 3:9c6f7f082151 696 ComplianceTest.NbGateways = 0;
mluis 3:9c6f7f082151 697 ComplianceTest.Running = true;
mluis 3:9c6f7f082151 698 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 699
mluis 3:9c6f7f082151 700 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 701 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 702 mibReq.Param.AdrEnable = true;
mluis 3:9c6f7f082151 703 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 704
mluis 3:9c6f7f082151 705 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 706 LoRaMacTestSetDutyCycleOn( false );
mluis 3:9c6f7f082151 707 #endif
mluis 1:352f608c3337 708 }
mluis 1:352f608c3337 709 }
mluis 0:92bca02df485 710 else
mluis 0:92bca02df485 711 {
mluis 5:1e9f6a365854 712 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 3:9c6f7f082151 713 switch( ComplianceTest.State )
mluis 3:9c6f7f082151 714 {
mluis 3:9c6f7f082151 715 case 0: // Check compliance test disable command (ii)
mluis 3:9c6f7f082151 716 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 3:9c6f7f082151 717 AppPort = LORAWAN_APP_PORT;
mluis 3:9c6f7f082151 718 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 3:9c6f7f082151 719 ComplianceTest.DownLinkCounter = 0;
mluis 3:9c6f7f082151 720 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 721
mluis 3:9c6f7f082151 722 MibRequestConfirm_t mibReq;
mluis 3:9c6f7f082151 723 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 724 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 725 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 726 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 727 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 728 #endif
mluis 3:9c6f7f082151 729 break;
mluis 3:9c6f7f082151 730 case 1: // (iii, iv)
mluis 3:9c6f7f082151 731 AppDataSize = 2;
mluis 3:9c6f7f082151 732 break;
mluis 3:9c6f7f082151 733 case 2: // Enable confirmed messages (v)
mluis 3:9c6f7f082151 734 IsTxConfirmed = true;
mluis 3:9c6f7f082151 735 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 736 break;
mluis 3:9c6f7f082151 737 case 3: // Disable confirmed messages (vi)
mluis 3:9c6f7f082151 738 IsTxConfirmed = false;
mluis 3:9c6f7f082151 739 ComplianceTest.State = 1;
mluis 3:9c6f7f082151 740 break;
mluis 3:9c6f7f082151 741 case 4: // (vii)
mluis 5:1e9f6a365854 742 AppDataSize = mcpsIndication->BufferSize;
mluis 3:9c6f7f082151 743
mluis 3:9c6f7f082151 744 AppData[0] = 4;
mluis 3:9c6f7f082151 745 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 3:9c6f7f082151 746 {
mluis 5:1e9f6a365854 747 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 3:9c6f7f082151 748 }
mluis 3:9c6f7f082151 749 break;
mluis 3:9c6f7f082151 750 case 5: // (viii)
mluis 3:9c6f7f082151 751 {
mluis 3:9c6f7f082151 752 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 753 mlmeReq.Type = MLME_LINK_CHECK;
mluis 3:9c6f7f082151 754 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 755 }
mluis 3:9c6f7f082151 756 break;
mluis 7:3173f0508a98 757 case 6: // (ix)
mluis 7:3173f0508a98 758 {
mluis 7:3173f0508a98 759 MlmeReq_t mlmeReq;
mluis 7:3173f0508a98 760
mluis 9:ee9dcbb9708d 761 // Disable TestMode and revert back to normal operation
mluis 9:ee9dcbb9708d 762 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 9:ee9dcbb9708d 763 AppPort = LORAWAN_APP_PORT;
mluis 9:ee9dcbb9708d 764 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 9:ee9dcbb9708d 765 ComplianceTest.DownLinkCounter = 0;
mluis 9:ee9dcbb9708d 766 ComplianceTest.Running = false;
mluis 9:ee9dcbb9708d 767
mluis 9:ee9dcbb9708d 768 MibRequestConfirm_t mibReq;
mluis 9:ee9dcbb9708d 769 mibReq.Type = MIB_ADR;
mluis 9:ee9dcbb9708d 770 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 9:ee9dcbb9708d 771 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 772 #if defined( USE_BAND_868 )
mluis 9:ee9dcbb9708d 773 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 9:ee9dcbb9708d 774 #endif
mluis 9:ee9dcbb9708d 775
mluis 7:3173f0508a98 776 mlmeReq.Type = MLME_JOIN;
mluis 7:3173f0508a98 777
mluis 7:3173f0508a98 778 mlmeReq.Req.Join.DevEui = DevEui;
mluis 7:3173f0508a98 779 mlmeReq.Req.Join.AppEui = AppEui;
mluis 7:3173f0508a98 780 mlmeReq.Req.Join.AppKey = AppKey;
mluis 9:ee9dcbb9708d 781 mlmeReq.Req.Join.NbTrials = 3;
mluis 7:3173f0508a98 782
mluis 7:3173f0508a98 783 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:3173f0508a98 784 DeviceState = DEVICE_STATE_SLEEP;
mluis 7:3173f0508a98 785 }
mluis 7:3173f0508a98 786 break;
mluis 9:ee9dcbb9708d 787 case 7: // (x)
mluis 9:ee9dcbb9708d 788 {
mluis 9:ee9dcbb9708d 789 if( mcpsIndication->BufferSize == 3 )
mluis 9:ee9dcbb9708d 790 {
mluis 9:ee9dcbb9708d 791 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 792 mlmeReq.Type = MLME_TXCW;
mluis 9:ee9dcbb9708d 793 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 794 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 795 }
mluis 9:ee9dcbb9708d 796 else if( mcpsIndication->BufferSize == 7 )
mluis 9:ee9dcbb9708d 797 {
mluis 9:ee9dcbb9708d 798 MlmeReq_t mlmeReq;
mluis 9:ee9dcbb9708d 799 mlmeReq.Type = MLME_TXCW_1;
mluis 9:ee9dcbb9708d 800 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 9:ee9dcbb9708d 801 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 9:ee9dcbb9708d 802 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 9:ee9dcbb9708d 803 LoRaMacMlmeRequest( &mlmeReq );
mluis 9:ee9dcbb9708d 804 }
mluis 9:ee9dcbb9708d 805 ComplianceTest.State = 1;
mluis 9:ee9dcbb9708d 806 }
mluis 9:ee9dcbb9708d 807 break;
mluis 3:9c6f7f082151 808 default:
mluis 3:9c6f7f082151 809 break;
mluis 3:9c6f7f082151 810 }
mluis 0:92bca02df485 811 }
mluis 3:9c6f7f082151 812 break;
mluis 3:9c6f7f082151 813 default:
mluis 3:9c6f7f082151 814 break;
mluis 0:92bca02df485 815 }
mluis 0:92bca02df485 816 }
mluis 1:352f608c3337 817
mluis 3:9c6f7f082151 818 // Switch LED 2 ON for each received downlink
mluis 3:9c6f7f082151 819 Led2State = true;
mluis 3:9c6f7f082151 820 Led2StateChanged = true;
mluis 3:9c6f7f082151 821 TimerStart( &Led2Timer );
mluis 3:9c6f7f082151 822 DownlinkStatusUpdated = true;
mluis 3:9c6f7f082151 823 }
mluis 3:9c6f7f082151 824
mluis 3:9c6f7f082151 825 /*!
mluis 3:9c6f7f082151 826 * \brief MLME-Confirm event function
mluis 3:9c6f7f082151 827 *
mluis 5:1e9f6a365854 828 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 3:9c6f7f082151 829 * containing confirm attributes.
mluis 3:9c6f7f082151 830 */
mluis 5:1e9f6a365854 831 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 3:9c6f7f082151 832 {
mluis 9:ee9dcbb9708d 833 switch( mlmeConfirm->MlmeRequest )
mluis 3:9c6f7f082151 834 {
mluis 9:ee9dcbb9708d 835 case MLME_JOIN:
mluis 3:9c6f7f082151 836 {
mluis 9:ee9dcbb9708d 837 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 838 {
mluis 3:9c6f7f082151 839 // Status is OK, node has joined the network
mluis 3:9c6f7f082151 840 IsNetworkJoinedStatusUpdate = true;
mluis 7:3173f0508a98 841 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 842
uss1994 10:9a4efdd07a77 843
uss1994 10:9a4efdd07a77 844 MessageType = MESSAGE_TYPE_HB;
uss1994 10:9a4efdd07a77 845 joining = false;
uss1994 10:9a4efdd07a77 846 next_stest = current_time + 5;
uss1994 10:9a4efdd07a77 847 next_ltest = current_time + long_interval;
mluis 9:ee9dcbb9708d 848 }
mluis 9:ee9dcbb9708d 849 else
mluis 9:ee9dcbb9708d 850 {
mluis 9:ee9dcbb9708d 851 // Join was not successful. Try to join again
mluis 9:ee9dcbb9708d 852 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 853 }
mluis 9:ee9dcbb9708d 854 break;
mluis 9:ee9dcbb9708d 855 }
mluis 9:ee9dcbb9708d 856 case MLME_LINK_CHECK:
mluis 9:ee9dcbb9708d 857 {
mluis 9:ee9dcbb9708d 858 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 3:9c6f7f082151 859 {
mluis 3:9c6f7f082151 860 // Check DemodMargin
mluis 3:9c6f7f082151 861 // Check NbGateways
mluis 3:9c6f7f082151 862 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 863 {
mluis 3:9c6f7f082151 864 ComplianceTest.LinkCheck = true;
mluis 5:1e9f6a365854 865 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 5:1e9f6a365854 866 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 3:9c6f7f082151 867 }
mluis 3:9c6f7f082151 868 }
mluis 9:ee9dcbb9708d 869 break;
mluis 3:9c6f7f082151 870 }
mluis 9:ee9dcbb9708d 871 default:
mluis 9:ee9dcbb9708d 872 break;
mluis 3:9c6f7f082151 873 }
mluis 3:9c6f7f082151 874 NextTx = true;
mluis 3:9c6f7f082151 875 UplinkStatusUpdated = true;
mluis 0:92bca02df485 876 }
mluis 0:92bca02df485 877
uss1994 10:9a4efdd07a77 878
uss1994 10:9a4efdd07a77 879 // **************************** TEST METHODS ******************************** //
uss1994 10:9a4efdd07a77 880
uss1994 10:9a4efdd07a77 881 int takeMeasurement(uint8_t *measurements, int count) {
uss1994 10:9a4efdd07a77 882 // This method should only run for long tests
uss1994 10:9a4efdd07a77 883 int battery_reading = BAT_PIN.read()*1000;
uss1994 10:9a4efdd07a77 884 int vce_reading = VCE_PIN.read()*1000;
uss1994 10:9a4efdd07a77 885 int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 886 int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 887
uss1994 10:9a4efdd07a77 888 int index = count;
uss1994 10:9a4efdd07a77 889
uss1994 10:9a4efdd07a77 890 measurements[(uint8_t)index] = (uint8_t) (battery_reading/10);
uss1994 10:9a4efdd07a77 891 measurements[(uint8_t)index+10] = (uint8_t) light_1_reading;
uss1994 10:9a4efdd07a77 892 measurements[(uint8_t)index+20] = (uint8_t) light_2_reading;
uss1994 10:9a4efdd07a77 893 return index+1;
uss1994 10:9a4efdd07a77 894 }
uss1994 10:9a4efdd07a77 895
uss1994 10:9a4efdd07a77 896 void startTest(uint8_t *measurements) {
uss1994 10:9a4efdd07a77 897
uss1994 10:9a4efdd07a77 898 // Determine test length & measurement interval
uss1994 10:9a4efdd07a77 899 switch (MessageType)
uss1994 10:9a4efdd07a77 900 {
uss1994 10:9a4efdd07a77 901 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 902 test_duration = 30;
uss1994 10:9a4efdd07a77 903 // set interval to twice the test length so that it never happens
uss1994 10:9a4efdd07a77 904 measurement_interval = 2*test_duration;
uss1994 10:9a4efdd07a77 905 break;
uss1994 10:9a4efdd07a77 906 case MESSAGE_TYPE_LONG_TEST:
uss1994 10:9a4efdd07a77 907 test_duration = 5400;
uss1994 10:9a4efdd07a77 908 // interval is divided by two longer than available measurement
uss1994 10:9a4efdd07a77 909 // slots so that it completes before the end of the test
uss1994 10:9a4efdd07a77 910 measurement_interval = test_duration/(available_slots + 2);
uss1994 10:9a4efdd07a77 911 break;
uss1994 10:9a4efdd07a77 912 default:
uss1994 10:9a4efdd07a77 913 test_duration = 0;
uss1994 10:9a4efdd07a77 914 break;
uss1994 10:9a4efdd07a77 915 }
uss1994 10:9a4efdd07a77 916
uss1994 10:9a4efdd07a77 917 // Measure voltage preval
uss1994 10:9a4efdd07a77 918 measurements[0] = (uint8_t) (BAT_PIN.read()*1000)/10;
uss1994 10:9a4efdd07a77 919
uss1994 10:9a4efdd07a77 920 // Set measurement count to 1
uss1994 10:9a4efdd07a77 921 measureCount = 1;
uss1994 10:9a4efdd07a77 922
uss1994 10:9a4efdd07a77 923 // Start test
uss1994 10:9a4efdd07a77 924 relayPin = 1;
uss1994 12:c1cf0e717684 925 AppLedStateOn = 1;
uss1994 12:c1cf0e717684 926 Led3StateChanged = true;
uss1994 10:9a4efdd07a77 927 }
uss1994 10:9a4efdd07a77 928
uss1994 10:9a4efdd07a77 929 void endTest(uint8_t *measurements) {
uss1994 10:9a4efdd07a77 930 // Measure endvals
uss1994 10:9a4efdd07a77 931 int battery_reading = BAT_PIN.read()*1000;
uss1994 10:9a4efdd07a77 932 int vce_reading = VCE_PIN.read()*1000;
uss1994 10:9a4efdd07a77 933 int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 934 int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading;
uss1994 10:9a4efdd07a77 935
uss1994 10:9a4efdd07a77 936 switch (MessageType)
uss1994 10:9a4efdd07a77 937 {
uss1994 10:9a4efdd07a77 938 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 939 measurements[1] = (uint8_t) (battery_reading/10);
uss1994 10:9a4efdd07a77 940 measurements[2] = (uint8_t) light_1_reading;
uss1994 10:9a4efdd07a77 941 measurements[3] = (uint8_t) light_2_reading;
uss1994 10:9a4efdd07a77 942 break;
uss1994 10:9a4efdd07a77 943 default:
uss1994 10:9a4efdd07a77 944 measurements[10] = (uint8_t) (battery_reading/10);
uss1994 10:9a4efdd07a77 945 measurements[20] = (uint8_t) light_1_reading;
uss1994 10:9a4efdd07a77 946 measurements[30] = (uint8_t) light_2_reading;
uss1994 10:9a4efdd07a77 947 break;
uss1994 10:9a4efdd07a77 948 }
uss1994 10:9a4efdd07a77 949
uss1994 10:9a4efdd07a77 950 // End test
uss1994 10:9a4efdd07a77 951 relayPin = 0;
uss1994 12:c1cf0e717684 952 AppLedStateOn = 0;
uss1994 12:c1cf0e717684 953 Led3StateChanged = true;
uss1994 10:9a4efdd07a77 954 }
uss1994 10:9a4efdd07a77 955
uss1994 11:e433cbbfd436 956 void button_options_handler(void)
uss1994 11:e433cbbfd436 957 {
uss1994 11:e433cbbfd436 958 if (press_count >= 3) {
uss1994 11:e433cbbfd436 959 // Run long test
uss1994 11:e433cbbfd436 960 MessageType = MESSAGE_TYPE_LONG_TEST;
uss1994 11:e433cbbfd436 961 measurements = l_measurements;
uss1994 11:e433cbbfd436 962
uss1994 11:e433cbbfd436 963 startTest(measurements);
uss1994 11:e433cbbfd436 964
uss1994 11:e433cbbfd436 965 running_test = true;
uss1994 11:e433cbbfd436 966 test_start = current_time;
uss1994 11:e433cbbfd436 967 last_measurement = current_time;
uss1994 11:e433cbbfd436 968 }
uss1994 11:e433cbbfd436 969 if (press_count == 2) {
uss1994 11:e433cbbfd436 970 // Run short test
uss1994 11:e433cbbfd436 971 MessageType = MESSAGE_TYPE_SHORT_TEST;
uss1994 11:e433cbbfd436 972 measurements = s_measurements;
uss1994 11:e433cbbfd436 973
uss1994 11:e433cbbfd436 974 startTest(measurements);
uss1994 11:e433cbbfd436 975
uss1994 11:e433cbbfd436 976 running_test = true;
uss1994 11:e433cbbfd436 977 test_start = current_time;
uss1994 11:e433cbbfd436 978 last_measurement = current_time;
uss1994 11:e433cbbfd436 979 }
uss1994 11:e433cbbfd436 980
uss1994 11:e433cbbfd436 981 // Reset count
uss1994 11:e433cbbfd436 982 press_count = 0;
uss1994 11:e433cbbfd436 983 }
uss1994 11:e433cbbfd436 984
uss1994 10:9a4efdd07a77 985
mluis 0:92bca02df485 986 /**
mluis 0:92bca02df485 987 * Main application entry point.
mluis 0:92bca02df485 988 */
mluis 0:92bca02df485 989 int main( void )
mluis 0:92bca02df485 990 {
mluis 3:9c6f7f082151 991 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 3:9c6f7f082151 992 LoRaMacCallback_t LoRaMacCallbacks;
mluis 3:9c6f7f082151 993 MibRequestConfirm_t mibReq;
mluis 0:92bca02df485 994
mluis 0:92bca02df485 995 BoardInit( );
mluis 3:9c6f7f082151 996 SerialDisplayInit( );
mluis 0:92bca02df485 997
mluis 7:3173f0508a98 998 SerialDisplayUpdateEui( 5, DevEui );
mluis 7:3173f0508a98 999 SerialDisplayUpdateEui( 6, AppEui );
mluis 7:3173f0508a98 1000 SerialDisplayUpdateKey( 7, AppKey );
mluis 7:3173f0508a98 1001
mluis 7:3173f0508a98 1002 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 7:3173f0508a98 1003 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 7:3173f0508a98 1004 SerialDisplayUpdateDevAddr( DevAddr );
mluis 7:3173f0508a98 1005 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 7:3173f0508a98 1006 SerialDisplayUpdateKey( 13, AppSKey );
mluis 7:3173f0508a98 1007 #endif
mluis 7:3173f0508a98 1008
mluis 3:9c6f7f082151 1009 DeviceState = DEVICE_STATE_INIT;
uss1994 10:9a4efdd07a77 1010 set_time(1514764800);
uss1994 10:9a4efdd07a77 1011 relayPin = 0;
uss1994 10:9a4efdd07a77 1012
uss1994 10:9a4efdd07a77 1013 running_test = false;
uss1994 10:9a4efdd07a77 1014 joining = true;
uss1994 10:9a4efdd07a77 1015 received_downlink = false;
uss1994 10:9a4efdd07a77 1016 time_t start_time = time(NULL);
uss1994 10:9a4efdd07a77 1017
uss1994 10:9a4efdd07a77 1018 last_hb = start_time;
uss1994 10:9a4efdd07a77 1019 last_measurement = start_time;
uss1994 11:e433cbbfd436 1020
uss1994 11:e433cbbfd436 1021 //button1.mode(PullUp); // Activate pull-up
uss1994 11:e433cbbfd436 1022 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
uss1994 12:c1cf0e717684 1023 AppLedStateOn = 0;
uss1994 12:c1cf0e717684 1024 Led3StateChanged = true;
mluis 0:92bca02df485 1025
mluis 0:92bca02df485 1026 while( 1 )
mluis 0:92bca02df485 1027 {
uss1994 10:9a4efdd07a77 1028 current_time = time(NULL);
mluis 0:92bca02df485 1029 SerialRxProcess( );
mluis 0:92bca02df485 1030 if( IsNetworkJoinedStatusUpdate == true )
mluis 0:92bca02df485 1031 {
mluis 0:92bca02df485 1032 IsNetworkJoinedStatusUpdate = false;
mluis 3:9c6f7f082151 1033 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 1034 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1035 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:92bca02df485 1036 }
mluis 0:92bca02df485 1037 if( Led1StateChanged == true )
mluis 0:92bca02df485 1038 {
mluis 0:92bca02df485 1039 Led1StateChanged = false;
mluis 1:352f608c3337 1040 SerialDisplayUpdateLedState( 1, Led1State );
mluis 0:92bca02df485 1041 }
mluis 0:92bca02df485 1042 if( Led2StateChanged == true )
mluis 0:92bca02df485 1043 {
mluis 0:92bca02df485 1044 Led2StateChanged = false;
mluis 1:352f608c3337 1045 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 1046 }
mluis 0:92bca02df485 1047 if( Led3StateChanged == true )
mluis 0:92bca02df485 1048 {
mluis 0:92bca02df485 1049 Led3StateChanged = false;
mluis 0:92bca02df485 1050 SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:92bca02df485 1051 }
mluis 3:9c6f7f082151 1052 if( UplinkStatusUpdated == true )
mluis 0:92bca02df485 1053 {
mluis 3:9c6f7f082151 1054 UplinkStatusUpdated = false;
mluis 3:9c6f7f082151 1055 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
mluis 3:9c6f7f082151 1056 }
mluis 3:9c6f7f082151 1057 if( DownlinkStatusUpdated == true )
mluis 3:9c6f7f082151 1058 {
mluis 3:9c6f7f082151 1059 DownlinkStatusUpdated = false;
mluis 1:352f608c3337 1060 SerialDisplayUpdateLedState( 2, Led2State );
mluis 0:92bca02df485 1061 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
mluis 0:92bca02df485 1062 }
mluis 3:9c6f7f082151 1063
mluis 3:9c6f7f082151 1064 switch( DeviceState )
mluis 0:92bca02df485 1065 {
mluis 3:9c6f7f082151 1066 case DEVICE_STATE_INIT:
mluis 3:9c6f7f082151 1067 {
mluis 3:9c6f7f082151 1068 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
mluis 3:9c6f7f082151 1069 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
mluis 3:9c6f7f082151 1070 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
mluis 3:9c6f7f082151 1071 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mluis 3:9c6f7f082151 1072 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
mluis 3:9c6f7f082151 1073
mluis 3:9c6f7f082151 1074 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 3:9c6f7f082151 1075
mluis 3:9c6f7f082151 1076 TimerInit( &Led1Timer, OnLed1TimerEvent );
mluis 9:ee9dcbb9708d 1077 TimerSetValue( &Led1Timer, 25 );
mluis 3:9c6f7f082151 1078
mluis 3:9c6f7f082151 1079 TimerInit( &Led2Timer, OnLed2TimerEvent );
mluis 9:ee9dcbb9708d 1080 TimerSetValue( &Led2Timer, 25 );
mluis 3:9c6f7f082151 1081
mluis 3:9c6f7f082151 1082 mibReq.Type = MIB_ADR;
mluis 3:9c6f7f082151 1083 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 3:9c6f7f082151 1084 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1085
mluis 3:9c6f7f082151 1086 mibReq.Type = MIB_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1087 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
mluis 3:9c6f7f082151 1088 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1089
mluis 3:9c6f7f082151 1090 #if defined( USE_BAND_868 )
mluis 3:9c6f7f082151 1091 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 3:9c6f7f082151 1092 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
mluis 5:1e9f6a365854 1093
mluis 9:ee9dcbb9708d 1094 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 )
mluis 5:1e9f6a365854 1095 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
mluis 5:1e9f6a365854 1096 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
mluis 5:1e9f6a365854 1097 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
mluis 5:1e9f6a365854 1098 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
mluis 5:1e9f6a365854 1099 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
mluis 5:1e9f6a365854 1100 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
mluis 5:1e9f6a365854 1101 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:3173f0508a98 1102
mluis 9:ee9dcbb9708d 1103 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
mluis 9:ee9dcbb9708d 1104 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 9:ee9dcbb9708d 1105 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 9:ee9dcbb9708d 1106
mluis 7:3173f0508a98 1107 mibReq.Type = MIB_RX2_CHANNEL;
mluis 7:3173f0508a98 1108 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
mluis 7:3173f0508a98 1109 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 5:1e9f6a365854 1110 #endif
mluis 5:1e9f6a365854 1111
mluis 3:9c6f7f082151 1112 #endif
mluis 3:9c6f7f082151 1113 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 3:9c6f7f082151 1114 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 3:9c6f7f082151 1115 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
mluis 3:9c6f7f082151 1116
mluis 3:9c6f7f082151 1117 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 3:9c6f7f082151 1118
mluis 3:9c6f7f082151 1119 DeviceState = DEVICE_STATE_JOIN;
mluis 3:9c6f7f082151 1120 break;
mluis 3:9c6f7f082151 1121 }
mluis 3:9c6f7f082151 1122 case DEVICE_STATE_JOIN:
mluis 3:9c6f7f082151 1123 {
mluis 3:9c6f7f082151 1124 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 3:9c6f7f082151 1125 MlmeReq_t mlmeReq;
mluis 3:9c6f7f082151 1126
mluis 3:9c6f7f082151 1127 mlmeReq.Type = MLME_JOIN;
mluis 3:9c6f7f082151 1128
mluis 3:9c6f7f082151 1129 mlmeReq.Req.Join.DevEui = DevEui;
mluis 3:9c6f7f082151 1130 mlmeReq.Req.Join.AppEui = AppEui;
mluis 3:9c6f7f082151 1131 mlmeReq.Req.Join.AppKey = AppKey;
mluis 3:9c6f7f082151 1132
mluis 3:9c6f7f082151 1133 if( NextTx == true )
mluis 3:9c6f7f082151 1134 {
mluis 3:9c6f7f082151 1135 LoRaMacMlmeRequest( &mlmeReq );
mluis 3:9c6f7f082151 1136 }
mluis 7:3173f0508a98 1137 DeviceState = DEVICE_STATE_SLEEP;
mluis 3:9c6f7f082151 1138 #else
mluis 3:9c6f7f082151 1139 mibReq.Type = MIB_NET_ID;
mluis 3:9c6f7f082151 1140 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
mluis 3:9c6f7f082151 1141 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1142
mluis 3:9c6f7f082151 1143 mibReq.Type = MIB_DEV_ADDR;
mluis 3:9c6f7f082151 1144 mibReq.Param.DevAddr = DevAddr;
mluis 3:9c6f7f082151 1145 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1146
mluis 3:9c6f7f082151 1147 mibReq.Type = MIB_NWK_SKEY;
mluis 3:9c6f7f082151 1148 mibReq.Param.NwkSKey = NwkSKey;
mluis 3:9c6f7f082151 1149 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1150
mluis 3:9c6f7f082151 1151 mibReq.Type = MIB_APP_SKEY;
mluis 3:9c6f7f082151 1152 mibReq.Param.AppSKey = AppSKey;
mluis 3:9c6f7f082151 1153 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1154
mluis 3:9c6f7f082151 1155 mibReq.Type = MIB_NETWORK_JOINED;
mluis 3:9c6f7f082151 1156 mibReq.Param.IsNetworkJoined = true;
mluis 3:9c6f7f082151 1157 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:9c6f7f082151 1158
mluis 3:9c6f7f082151 1159 DeviceState = DEVICE_STATE_SEND;
mluis 3:9c6f7f082151 1160 #endif
mluis 3:9c6f7f082151 1161 IsNetworkJoinedStatusUpdate = true;
mluis 3:9c6f7f082151 1162 break;
mluis 3:9c6f7f082151 1163 }
mluis 3:9c6f7f082151 1164 case DEVICE_STATE_SEND:
mluis 3:9c6f7f082151 1165 {
mluis 3:9c6f7f082151 1166 if( NextTx == true )
mluis 3:9c6f7f082151 1167 {
mluis 3:9c6f7f082151 1168 SerialDisplayUpdateUplinkAcked( false );
mluis 3:9c6f7f082151 1169 SerialDisplayUpdateDonwlinkRxData( false );
mluis 3:9c6f7f082151 1170 PrepareTxFrame( AppPort );
mluis 3:9c6f7f082151 1171
mluis 3:9c6f7f082151 1172 NextTx = SendFrame( );
mluis 3:9c6f7f082151 1173 }
mluis 3:9c6f7f082151 1174 if( ComplianceTest.Running == true )
mluis 3:9c6f7f082151 1175 {
mluis 5:1e9f6a365854 1176 // Schedule next packet transmission
mluis 9:ee9dcbb9708d 1177 TxDutyCycleTime = 5000; // 5000 ms
mluis 3:9c6f7f082151 1178 }
mluis 3:9c6f7f082151 1179 else
mluis 3:9c6f7f082151 1180 {
mluis 3:9c6f7f082151 1181 // Schedule next packet transmission
mluis 3:9c6f7f082151 1182 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mluis 3:9c6f7f082151 1183 }
mluis 3:9c6f7f082151 1184 DeviceState = DEVICE_STATE_CYCLE;
mluis 3:9c6f7f082151 1185 break;
mluis 3:9c6f7f082151 1186 }
mluis 3:9c6f7f082151 1187 case DEVICE_STATE_CYCLE:
mluis 3:9c6f7f082151 1188 {
uss1994 10:9a4efdd07a77 1189 // DeviceState = DEVICE_STATE_SLEEP;
mluis 5:1e9f6a365854 1190
mluis 3:9c6f7f082151 1191 // Schedule next packet transmission
uss1994 10:9a4efdd07a77 1192 // TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
uss1994 10:9a4efdd07a77 1193 // TimerStart( &TxNextPacketTimer );
uss1994 10:9a4efdd07a77 1194
uss1994 10:9a4efdd07a77 1195
uss1994 10:9a4efdd07a77 1196
uss1994 10:9a4efdd07a77 1197 // Is a test is running
uss1994 10:9a4efdd07a77 1198 if (running_test) {
uss1994 10:9a4efdd07a77 1199 // If it is
uss1994 10:9a4efdd07a77 1200 //check if it's time to take the next interval measurement
uss1994 10:9a4efdd07a77 1201 if (current_time - last_measurement >= measurement_interval) {
uss1994 10:9a4efdd07a77 1202 if (measureCount <= available_slots) {
uss1994 10:9a4efdd07a77 1203 measureCount = takeMeasurement(measurements, measureCount);
uss1994 10:9a4efdd07a77 1204 }
uss1994 10:9a4efdd07a77 1205 last_measurement = current_time;
uss1994 10:9a4efdd07a77 1206 }
uss1994 10:9a4efdd07a77 1207
uss1994 10:9a4efdd07a77 1208 //check if it's time to end the test
uss1994 10:9a4efdd07a77 1209 if (current_time - test_start >= test_duration) {
uss1994 10:9a4efdd07a77 1210 endTest(measurements);
uss1994 10:9a4efdd07a77 1211 // sendResult(measurements, ttype);
uss1994 10:9a4efdd07a77 1212 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 1213 last_hb = current_time;
uss1994 10:9a4efdd07a77 1214
uss1994 10:9a4efdd07a77 1215 // Set the times for the next tests
uss1994 10:9a4efdd07a77 1216 switch ( MessageType )
uss1994 10:9a4efdd07a77 1217 {
uss1994 10:9a4efdd07a77 1218 case MESSAGE_TYPE_SHORT_TEST:
uss1994 10:9a4efdd07a77 1219 {
uss1994 10:9a4efdd07a77 1220 next_stest = test_start + test_interval;
uss1994 10:9a4efdd07a77 1221 break;
uss1994 10:9a4efdd07a77 1222 }
uss1994 10:9a4efdd07a77 1223 default:
uss1994 10:9a4efdd07a77 1224 {
uss1994 10:9a4efdd07a77 1225 // If long test, reset both test types,
uss1994 10:9a4efdd07a77 1226 // to prevent two tests at the same time
uss1994 10:9a4efdd07a77 1227 next_stest = test_start + test_interval;
uss1994 10:9a4efdd07a77 1228 next_ltest = test_start + long_interval;
uss1994 10:9a4efdd07a77 1229 break;
uss1994 10:9a4efdd07a77 1230 }
uss1994 10:9a4efdd07a77 1231 }
uss1994 10:9a4efdd07a77 1232
uss1994 10:9a4efdd07a77 1233 running_test = false;
uss1994 10:9a4efdd07a77 1234 }
uss1994 10:9a4efdd07a77 1235 } else {
uss1994 10:9a4efdd07a77 1236 // If it isn't
uss1994 10:9a4efdd07a77 1237
uss1994 10:9a4efdd07a77 1238 // check if downlink action is required
uss1994 10:9a4efdd07a77 1239
uss1994 10:9a4efdd07a77 1240 //if (received_downlink == true) {
uss1994 10:9a4efdd07a77 1241 // dl_handler(received_data);
uss1994 10:9a4efdd07a77 1242 //}
uss1994 10:9a4efdd07a77 1243
uss1994 11:e433cbbfd436 1244 //check if button is pressed
uss1994 11:e433cbbfd436 1245 if (button1_pressed) { // Set when button is pressed
uss1994 11:e433cbbfd436 1246 if (press_count == 0) {
uss1994 11:e433cbbfd436 1247 buttonOptions_timeout.attach(callback(button_options_handler), 2); // Debounce time 300 ms
uss1994 11:e433cbbfd436 1248 }
uss1994 11:e433cbbfd436 1249 button1_pressed = false;
uss1994 11:e433cbbfd436 1250 press_count++;
uss1994 11:e433cbbfd436 1251 }
uss1994 11:e433cbbfd436 1252
uss1994 10:9a4efdd07a77 1253 //check if it's time to run a test
uss1994 10:9a4efdd07a77 1254 if (current_time >= next_stest || current_time >= next_ltest) {
uss1994 10:9a4efdd07a77 1255
uss1994 10:9a4efdd07a77 1256 // Check what kind of test to run
uss1994 10:9a4efdd07a77 1257 if (current_time >= next_ltest) {
uss1994 10:9a4efdd07a77 1258 MessageType = MESSAGE_TYPE_LONG_TEST;
uss1994 10:9a4efdd07a77 1259 measurements = l_measurements;
uss1994 10:9a4efdd07a77 1260 } else {
uss1994 10:9a4efdd07a77 1261 MessageType = MESSAGE_TYPE_SHORT_TEST;
uss1994 10:9a4efdd07a77 1262 measurements = s_measurements;
uss1994 10:9a4efdd07a77 1263 }
uss1994 10:9a4efdd07a77 1264
uss1994 10:9a4efdd07a77 1265 startTest(measurements);
uss1994 10:9a4efdd07a77 1266
uss1994 10:9a4efdd07a77 1267 running_test = true;
uss1994 10:9a4efdd07a77 1268 test_start = current_time;
uss1994 10:9a4efdd07a77 1269 last_measurement = current_time;
uss1994 10:9a4efdd07a77 1270
uss1994 10:9a4efdd07a77 1271
uss1994 10:9a4efdd07a77 1272 //check if it's time for a heartbeat
uss1994 10:9a4efdd07a77 1273 } else if (current_time - last_hb >= hb_interval) {
uss1994 10:9a4efdd07a77 1274 // sendHb();
uss1994 10:9a4efdd07a77 1275 DeviceState = DEVICE_STATE_SEND;
uss1994 10:9a4efdd07a77 1276 MessageType = MESSAGE_TYPE_HB;
uss1994 10:9a4efdd07a77 1277 last_hb = current_time;
uss1994 10:9a4efdd07a77 1278 }
uss1994 10:9a4efdd07a77 1279 }
uss1994 10:9a4efdd07a77 1280
uss1994 10:9a4efdd07a77 1281
mluis 3:9c6f7f082151 1282 break;
mluis 3:9c6f7f082151 1283 }
mluis 3:9c6f7f082151 1284 case DEVICE_STATE_SLEEP:
mluis 3:9c6f7f082151 1285 {
mluis 3:9c6f7f082151 1286 // Wake up through events
mluis 3:9c6f7f082151 1287 break;
mluis 3:9c6f7f082151 1288 }
mluis 3:9c6f7f082151 1289 default:
mluis 3:9c6f7f082151 1290 {
mluis 3:9c6f7f082151 1291 DeviceState = DEVICE_STATE_INIT;
mluis 3:9c6f7f082151 1292 break;
mluis 3:9c6f7f082151 1293 }
mluis 0:92bca02df485 1294 }
mluis 0:92bca02df485 1295 }
mluis 0:92bca02df485 1296 }