L3STIC / Mbed 2 deprecated LoRaWAN-MULTI_RTC_heure-V04

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Committer:
MGstic
Date:
Wed Dec 12 08:41:45 2018 +0000
Revision:
9:07c94dd6dc51
Parent:
8:cfe39840b9fb
Child:
10:1a85ff06be1a
V02;

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