inital draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72_tjm by Timothy Mulrooney

Committer:
tmulrooney
Date:
Fri Mar 11 20:01:15 2016 +0000
Revision:
6:ee58dcb61940
Parent:
5:df5350d5e2fc
Child:
8:7af52843c8a9
comments added

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