Anish updated the code to work with L152. RTC seems to causing issue. Will have to disable LSE.

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Committer:
uss1994
Date:
Fri Dec 07 13:25:30 2018 +0000
Revision:
10:9a4efdd07a77
Parent:
9:ee9dcbb9708d
Child:
11:e433cbbfd436
Working firmware;

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