inital draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72_tjm by Timothy Mulrooney

Committer:
tmulrooney
Date:
Wed Mar 09 20:31:37 2016 +0000
Revision:
4:35c4373393c3
Parent:
3:b93a0cb03083
Child:
5:df5350d5e2fc
first draft

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