Forked (hacked) for Legacy Gateway testing

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWAN-demo-72 by Miguel Luis

Committer:
mick_ccc
Date:
Thu Apr 20 14:30:02 2017 +0000
Revision:
9:e395d85afe81
Parent:
6:f9224695ed01
Hacked for Gateway Legacy testing

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