inital draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72_tjm by Timothy Mulrooney

Committer:
tmulrooney
Date:
Tue Aug 09 14:38:07 2016 +0000
Revision:
9:b70f8b97176a
Parent:
8:7af52843c8a9
production board

Who changed what in which revision?

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