inital draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72_tjm by Timothy Mulrooney

Committer:
tmulrooney
Date:
Wed Mar 09 21:21:53 2016 +0000
Revision:
5:df5350d5e2fc
Parent:
4:35c4373393c3
Child:
6:ee58dcb61940
added changing inputs

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