1 /*!
2 * \file main.c
3 *
4 * \brief Performs a periodic uplink
5 *
6 * \copyright Revised BSD License, see section \ref LICENSE.
7 *
8 * \code
9 * ______ _
10 * / _____) _ | |
11 * ( (____ _____ ____ _| |_ _____ ____| |__
12 * \____ \| ___ | (_ _) ___ |/ ___) _ \
13 * _____) ) ____| | | || |_| ____( (___| | | |
14 * (______/|_____)_|_|_| \__)_____)\____)_| |_|
15 * (C)2013-2018 Semtech
16 *
17 * \endcode
18 *
19 * \author Miguel Luis ( Semtech )
20 */
21
22 /*! \file periodic-uplink/NucleoL073/main.c */
23
24 #include <stdio.h>
25 #include "../firmwareVersion.h"
26 #include "../../common/githubVersion.h"
27 #include "utilities.h"
28 #include "board.h"
29 #include "gpio.h"
30 #include "uart.h"
31 #include "RegionCommon.h"
32
33 #include "cli.h"
34 #include "Commissioning.h"
35 #include "LmHandler.h"
36 #include "LmhpCompliance.h"
37 #include "CayenneLpp.h"
38 #include "LmHandlerMsgDisplay.h"
39
40 #ifndef ACTIVE_REGION
41
42 #warning "No active region defined, LORAMAC_REGION_EU868 will be used as default."
43
44 #define ACTIVE_REGION LORAMAC_REGION_EU868
45
46 #endif
47
48 /*!
49 * LoRaWAN default end-device class
50 */
51 #ifndef LORAWAN_DEFAULT_CLASS
52 #define LORAWAN_DEFAULT_CLASS CLASS_A
53 #endif
54
55 /*!
56 * Defines the application data transmission duty cycle. 5s, value in [ms].
57 */
58 #define APP_TX_DUTYCYCLE 5000
59
60 /*!
61 * Defines a random delay for application data transmission duty cycle. 1s,
62 * value in [ms].
63 */
64 #define APP_TX_DUTYCYCLE_RND 1000
65
66 /*!
67 * LoRaWAN Adaptive Data Rate
68 *
69 * \remark Please note that when ADR is enabled the end-device should be static
70 */
71 #define LORAWAN_ADR_STATE LORAMAC_HANDLER_ADR_ON
72
73 /*!
74 * Default datarate
75 *
76 * \remark Please note that LORAWAN_DEFAULT_DATARATE is used only when ADR is disabled
77 */
78 #define LORAWAN_DEFAULT_DATARATE DR_0
79
80 /*!
81 * LoRaWAN confirmed messages
82 */
83 #define LORAWAN_DEFAULT_CONFIRMED_MSG_STATE LORAMAC_HANDLER_UNCONFIRMED_MSG
84
85 /*!
86 * User application data buffer size
87 */
88 #define LORAWAN_APP_DATA_BUFFER_MAX_SIZE 242
89
90 /*!
91 * LoRaWAN ETSI duty cycle control enable/disable
92 *
93 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
94 */
95 #define LORAWAN_DUTYCYCLE_ON true
96
97 /*!
98 * LoRaWAN application port
99 * @remark The allowed port range is from 1 up to 223. Other values are reserved.
100 */
101 #define LORAWAN_APP_PORT 2
102
103 /*!
104 *
105 */
106 typedef enum
107 {
108 LORAMAC_HANDLER_TX_ON_TIMER,
109 LORAMAC_HANDLER_TX_ON_EVENT,
110 }LmHandlerTxEvents_t;
111
112 /*!
113 * User application data
114 */
115 static uint8_t AppDataBuffer[LORAWAN_APP_DATA_BUFFER_MAX_SIZE];
116
117 /*!
118 * User application data structure
119 */
120 static LmHandlerAppData_t AppData =
121 {
122 .Buffer = AppDataBuffer,
123 .BufferSize = 0,
124 .Port = 0,
125 };
126
127 /*!
128 * Specifies the state of the application LED
129 */
130 static bool AppLedStateOn = false;
131
132 /*!
133 * Timer to handle the application data transmission duty cycle
134 */
135 static TimerEvent_t TxTimer;
136
137 /*!
138 * Timer to handle the state of LED1
139 */
140 static TimerEvent_t Led1Timer;
141
142 /*!
143 * Timer to handle the state of LED2
144 */
145 static TimerEvent_t Led2Timer;
146
147 /*!
148 * Timer to handle the state of LED beacon indicator
149 */
150 static TimerEvent_t LedBeaconTimer;
151
152 static void OnMacProcessNotify( void );
153 static void OnNvmDataChange( LmHandlerNvmContextStates_t state, uint16_t size );
154 static void OnNetworkParametersChange( CommissioningParams_t* params );
155 static void OnMacMcpsRequest( LoRaMacStatus_t status, McpsReq_t *mcpsReq, TimerTime_t nextTxIn );
156 static void OnMacMlmeRequest( LoRaMacStatus_t status, MlmeReq_t *mlmeReq, TimerTime_t nextTxIn );
157 static void OnJoinRequest( LmHandlerJoinParams_t* params );
158 static void OnTxData( LmHandlerTxParams_t* params );
159 static void OnRxData( LmHandlerAppData_t* appData, LmHandlerRxParams_t* params );
160 static void OnClassChange( DeviceClass_t deviceClass );
161 static void OnBeaconStatusChange( LoRaMacHandlerBeaconParams_t* params );
162 #if( LMH_SYS_TIME_UPDATE_NEW_API == 1 )
163 static void OnSysTimeUpdate( bool isSynchronized, int32_t timeCorrection );
164 #else
165 static void OnSysTimeUpdate( void );
166 #endif
167 static void PrepareTxFrame( void );
168 static void StartTxProcess( LmHandlerTxEvents_t txEvent );
169 static void UplinkProcess( void );
170
171 static void OnTxPeriodicityChanged( uint32_t periodicity );
172 static void OnTxFrameCtrlChanged( LmHandlerMsgTypes_t isTxConfirmed );
173 static void OnPingSlotPeriodicityChanged( uint8_t pingSlotPeriodicity );
174
175 /*!
176 * Function executed on TxTimer event
177 */
178 static void OnTxTimerEvent( void* context );
179
180 /*!
181 * Function executed on Led 1 Timeout event
182 */
183 static void OnLed1TimerEvent( void* context );
184
185 /*!
186 * Function executed on Led 2 Timeout event
187 */
188 static void OnLed2TimerEvent( void* context );
189
190 /*!
191 * \brief Function executed on Beacon timer Timeout event
192 */
193 static void OnLedBeaconTimerEvent( void* context );
194
195 static LmHandlerCallbacks_t LmHandlerCallbacks =
196 {
197 .GetBatteryLevel = BoardGetBatteryLevel,
198 .GetTemperature = NULL,
199 .GetRandomSeed = BoardGetRandomSeed,
200 .OnMacProcess = OnMacProcessNotify,
201 .OnNvmDataChange = OnNvmDataChange,
202 .OnNetworkParametersChange = OnNetworkParametersChange,
203 .OnMacMcpsRequest = OnMacMcpsRequest,
204 .OnMacMlmeRequest = OnMacMlmeRequest,
205 .OnJoinRequest = OnJoinRequest,
206 .OnTxData = OnTxData,
207 .OnRxData = OnRxData,
208 .OnClassChange= OnClassChange,
209 .OnBeaconStatusChange = OnBeaconStatusChange,
210 .OnSysTimeUpdate = OnSysTimeUpdate,
211 };
212
213 static LmHandlerParams_t LmHandlerParams =
214 {
215 .Region = ACTIVE_REGION,
216 .AdrEnable = LORAWAN_ADR_STATE,
217 .IsTxConfirmed = LORAWAN_DEFAULT_CONFIRMED_MSG_STATE,
218 .TxDatarate = LORAWAN_DEFAULT_DATARATE,
219 .PublicNetworkEnable = LORAWAN_PUBLIC_NETWORK,
220 .DutyCycleEnabled = LORAWAN_DUTYCYCLE_ON,
221 .DataBufferMaxSize = LORAWAN_APP_DATA_BUFFER_MAX_SIZE,
222 .DataBuffer = AppDataBuffer,
223 .PingSlotPeriodicity = REGION_COMMON_DEFAULT_PING_SLOT_PERIODICITY,
224 };
225
226 static LmhpComplianceParams_t LmhpComplianceParams =
227 {
228 .FwVersion.Value = FIRMWARE_VERSION,
229 .OnTxPeriodicityChanged = OnTxPeriodicityChanged,
230 .OnTxFrameCtrlChanged = OnTxFrameCtrlChanged,
231 .OnPingSlotPeriodicityChanged = OnPingSlotPeriodicityChanged,
232 };
233
234 /*!
235 * Indicates if LoRaMacProcess call is pending.
236 *
237 * \warning If variable is equal to 0 then the MCU can be set in low power mode
238 */
239 static volatile uint8_t IsMacProcessPending = 0;
240
241 static volatile uint8_t IsTxFramePending = 0;
242
243 static volatile uint32_t TxPeriodicity = 0;
244
245 /*!
246 * LED GPIO pins objects
247 */
248 extern Gpio_t Led1; // Tx
249 extern Gpio_t Led2; // Rx
250
251 /*!
252 * UART object used for command line interface handling
253 */
254 extern Uart_t Uart2;
255
256 /*!
257 * Main application entry point.
258 */
main(void)259 int main( void )
260 {
261 BoardInitMcu( );
262 BoardInitPeriph( );
263
264 TimerInit( &Led1Timer, OnLed1TimerEvent );
265 TimerSetValue( &Led1Timer, 25 );
266
267 TimerInit( &Led2Timer, OnLed2TimerEvent );
268 TimerSetValue( &Led2Timer, 25 );
269
270 TimerInit( &LedBeaconTimer, OnLedBeaconTimerEvent );
271 TimerSetValue( &LedBeaconTimer, 5000 );
272
273 // Initialize transmission periodicity variable
274 TxPeriodicity = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
275
276 const Version_t appVersion = { .Value = FIRMWARE_VERSION };
277 const Version_t gitHubVersion = { .Value = GITHUB_VERSION };
278 DisplayAppInfo( "periodic-uplink-lpp",
279 &appVersion,
280 &gitHubVersion );
281
282 if ( LmHandlerInit( &LmHandlerCallbacks, &LmHandlerParams ) != LORAMAC_HANDLER_SUCCESS )
283 {
284 printf( "LoRaMac wasn't properly initialized\n" );
285 // Fatal error, endless loop.
286 while ( 1 )
287 {
288 }
289 }
290
291 // Set system maximum tolerated rx error in milliseconds
292 LmHandlerSetSystemMaxRxError( 20 );
293
294 // The LoRa-Alliance Compliance protocol package should always be
295 // initialized and activated.
296 LmHandlerPackageRegister( PACKAGE_ID_COMPLIANCE, &LmhpComplianceParams );
297
298 LmHandlerJoin( );
299
300 StartTxProcess( LORAMAC_HANDLER_TX_ON_TIMER );
301
302 while( 1 )
303 {
304 // Process characters sent over the command line interface
305 CliProcess( &Uart2 );
306
307 // Processes the LoRaMac events
308 LmHandlerProcess( );
309
310 // Process application uplinks management
311 UplinkProcess( );
312
313 CRITICAL_SECTION_BEGIN( );
314 if( IsMacProcessPending == 1 )
315 {
316 // Clear flag and prevent MCU to go into low power modes.
317 IsMacProcessPending = 0;
318 }
319 else
320 {
321 // The MCU wakes up through events
322 BoardLowPowerHandler( );
323 }
324 CRITICAL_SECTION_END( );
325 }
326 }
327
OnMacProcessNotify(void)328 static void OnMacProcessNotify( void )
329 {
330 IsMacProcessPending = 1;
331 }
332
OnNvmDataChange(LmHandlerNvmContextStates_t state,uint16_t size)333 static void OnNvmDataChange( LmHandlerNvmContextStates_t state, uint16_t size )
334 {
335 DisplayNvmDataChange( state, size );
336 }
337
OnNetworkParametersChange(CommissioningParams_t * params)338 static void OnNetworkParametersChange( CommissioningParams_t* params )
339 {
340 DisplayNetworkParametersUpdate( params );
341 }
342
OnMacMcpsRequest(LoRaMacStatus_t status,McpsReq_t * mcpsReq,TimerTime_t nextTxIn)343 static void OnMacMcpsRequest( LoRaMacStatus_t status, McpsReq_t *mcpsReq, TimerTime_t nextTxIn )
344 {
345 DisplayMacMcpsRequestUpdate( status, mcpsReq, nextTxIn );
346 }
347
OnMacMlmeRequest(LoRaMacStatus_t status,MlmeReq_t * mlmeReq,TimerTime_t nextTxIn)348 static void OnMacMlmeRequest( LoRaMacStatus_t status, MlmeReq_t *mlmeReq, TimerTime_t nextTxIn )
349 {
350 DisplayMacMlmeRequestUpdate( status, mlmeReq, nextTxIn );
351 }
352
OnJoinRequest(LmHandlerJoinParams_t * params)353 static void OnJoinRequest( LmHandlerJoinParams_t* params )
354 {
355 DisplayJoinRequestUpdate( params );
356 if( params->Status == LORAMAC_HANDLER_ERROR )
357 {
358 LmHandlerJoin( );
359 }
360 else
361 {
362 LmHandlerRequestClass( LORAWAN_DEFAULT_CLASS );
363 }
364 }
365
OnTxData(LmHandlerTxParams_t * params)366 static void OnTxData( LmHandlerTxParams_t* params )
367 {
368 DisplayTxUpdate( params );
369 }
370
OnRxData(LmHandlerAppData_t * appData,LmHandlerRxParams_t * params)371 static void OnRxData( LmHandlerAppData_t* appData, LmHandlerRxParams_t* params )
372 {
373 DisplayRxUpdate( appData, params );
374
375 switch( appData->Port )
376 {
377 case 1: // The application LED can be controlled on port 1 or 2
378 case LORAWAN_APP_PORT:
379 {
380 AppLedStateOn = appData->Buffer[0] & 0x01;
381 }
382 break;
383 default:
384 break;
385 }
386
387 // Switch LED 2 ON for each received downlink
388 GpioWrite( &Led2, 1 );
389 TimerStart( &Led2Timer );
390 }
391
OnClassChange(DeviceClass_t deviceClass)392 static void OnClassChange( DeviceClass_t deviceClass )
393 {
394 DisplayClassUpdate( deviceClass );
395
396 // Inform the server as soon as possible that the end-device has switched to ClassB
397 LmHandlerAppData_t appData =
398 {
399 .Buffer = NULL,
400 .BufferSize = 0,
401 .Port = 0,
402 };
403 LmHandlerSend( &appData, LORAMAC_HANDLER_UNCONFIRMED_MSG );
404 }
405
OnBeaconStatusChange(LoRaMacHandlerBeaconParams_t * params)406 static void OnBeaconStatusChange( LoRaMacHandlerBeaconParams_t* params )
407 {
408 switch( params->State )
409 {
410 case LORAMAC_HANDLER_BEACON_RX:
411 {
412 TimerStart( &LedBeaconTimer );
413 break;
414 }
415 case LORAMAC_HANDLER_BEACON_LOST:
416 case LORAMAC_HANDLER_BEACON_NRX:
417 {
418 TimerStop( &LedBeaconTimer );
419 break;
420 }
421 default:
422 {
423 break;
424 }
425 }
426
427 DisplayBeaconUpdate( params );
428 }
429
430 #if( LMH_SYS_TIME_UPDATE_NEW_API == 1 )
OnSysTimeUpdate(bool isSynchronized,int32_t timeCorrection)431 static void OnSysTimeUpdate( bool isSynchronized, int32_t timeCorrection )
432 {
433
434 }
435 #else
OnSysTimeUpdate(void)436 static void OnSysTimeUpdate( void )
437 {
438
439 }
440 #endif
441
442 /*!
443 * Prepares the payload of the frame and transmits it.
444 */
PrepareTxFrame(void)445 static void PrepareTxFrame( void )
446 {
447 if( LmHandlerIsBusy( ) == true )
448 {
449 return;
450 }
451
452 uint8_t channel = 0;
453
454 AppData.Port = LORAWAN_APP_PORT;
455
456 CayenneLppReset( );
457 CayenneLppAddDigitalInput( channel++, AppLedStateOn );
458 CayenneLppAddAnalogInput( channel++, BoardGetBatteryLevel( ) * 100 / 254 );
459
460 CayenneLppCopy( AppData.Buffer );
461 AppData.BufferSize = CayenneLppGetSize( );
462
463 if( LmHandlerSend( &AppData, LmHandlerParams.IsTxConfirmed ) == LORAMAC_HANDLER_SUCCESS )
464 {
465 // Switch LED 1 ON
466 GpioWrite( &Led1, 1 );
467 TimerStart( &Led1Timer );
468 }
469 }
470
StartTxProcess(LmHandlerTxEvents_t txEvent)471 static void StartTxProcess( LmHandlerTxEvents_t txEvent )
472 {
473 switch( txEvent )
474 {
475 default:
476 // Intentional fall through
477 case LORAMAC_HANDLER_TX_ON_TIMER:
478 {
479 // Schedule 1st packet transmission
480 TimerInit( &TxTimer, OnTxTimerEvent );
481 TimerSetValue( &TxTimer, TxPeriodicity );
482 OnTxTimerEvent( NULL );
483 }
484 break;
485 case LORAMAC_HANDLER_TX_ON_EVENT:
486 {
487 }
488 break;
489 }
490 }
491
UplinkProcess(void)492 static void UplinkProcess( void )
493 {
494 uint8_t isPending = 0;
495 CRITICAL_SECTION_BEGIN( );
496 isPending = IsTxFramePending;
497 IsTxFramePending = 0;
498 CRITICAL_SECTION_END( );
499 if( isPending == 1 )
500 {
501 PrepareTxFrame( );
502 }
503 }
504
OnTxPeriodicityChanged(uint32_t periodicity)505 static void OnTxPeriodicityChanged( uint32_t periodicity )
506 {
507 TxPeriodicity = periodicity;
508
509 if( TxPeriodicity == 0 )
510 { // Revert to application default periodicity
511 TxPeriodicity = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
512 }
513
514 // Update timer periodicity
515 TimerStop( &TxTimer );
516 TimerSetValue( &TxTimer, TxPeriodicity );
517 TimerStart( &TxTimer );
518 }
519
OnTxFrameCtrlChanged(LmHandlerMsgTypes_t isTxConfirmed)520 static void OnTxFrameCtrlChanged( LmHandlerMsgTypes_t isTxConfirmed )
521 {
522 LmHandlerParams.IsTxConfirmed = isTxConfirmed;
523 }
524
OnPingSlotPeriodicityChanged(uint8_t pingSlotPeriodicity)525 static void OnPingSlotPeriodicityChanged( uint8_t pingSlotPeriodicity )
526 {
527 LmHandlerParams.PingSlotPeriodicity = pingSlotPeriodicity;
528 }
529
530 /*!
531 * Function executed on TxTimer event
532 */
OnTxTimerEvent(void * context)533 static void OnTxTimerEvent( void* context )
534 {
535 TimerStop( &TxTimer );
536
537 IsTxFramePending = 1;
538
539 // Schedule next transmission
540 TimerSetValue( &TxTimer, TxPeriodicity );
541 TimerStart( &TxTimer );
542 }
543
544 /*!
545 * Function executed on Led 1 Timeout event
546 */
OnLed1TimerEvent(void * context)547 static void OnLed1TimerEvent( void* context )
548 {
549 TimerStop( &Led1Timer );
550 // Switch LED 1 OFF
551 GpioWrite( &Led1, 0 );
552 }
553
554 /*!
555 * Function executed on Led 2 Timeout event
556 */
OnLed2TimerEvent(void * context)557 static void OnLed2TimerEvent( void* context )
558 {
559 TimerStop( &Led2Timer );
560 // Switch LED 2 OFF
561 GpioWrite( &Led2, 0 );
562 }
563
564 /*!
565 * \brief Function executed on Beacon timer Timeout event
566 */
OnLedBeaconTimerEvent(void * context)567 static void OnLedBeaconTimerEvent( void* context )
568 {
569 GpioWrite( &Led2, 1 );
570 TimerStart( &Led2Timer );
571
572 TimerStart( &LedBeaconTimer );
573 }
574