2929#include "gpio.h"
3030#include "LoRaMac.h"
3131#include "Commissioning.h"
32+ #include "NvmCtxMgmt.h"
3233
3334#ifndef ACTIVE_REGION
3435
@@ -164,7 +165,7 @@ static bool NextTx = true;
164165 */
165166static enum eDeviceState
166167{
167- DEVICE_STATE_INIT ,
168+ DEVICE_STATE_RESTORE ,
168169 DEVICE_STATE_START ,
169170 DEVICE_STATE_JOIN ,
170171 DEVICE_STATE_SEND ,
@@ -540,8 +541,7 @@ static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
540541 printf ("CHANNEL MASK: " );
541542#if defined( REGION_AS923 ) || defined( REGION_CN779 ) || \
542543 defined( REGION_EU868 ) || defined( REGION_IN865 ) || \
543- defined( REGION_KR920 ) || defined( REGION_EU433 ) || \
544- defined( REGION_RU864 )
544+ defined( REGION_KR920 ) || defined( REGION_EU433 )
545545
546546 for ( uint8_t i = 0 ; i < 1 ; i ++ )
547547
@@ -912,10 +912,11 @@ int main( void )
912912 macPrimitives .MacMlmeIndication = MlmeIndication ;
913913 macCallbacks .GetBatteryLevel = BoardGetBatteryLevel ;
914914 macCallbacks .GetTemperatureLevel = NULL ;
915+ macCallbacks .NvmContextChange = NvmCtxMgmtEvent ;
915916
916917 LoRaMacInitialization ( & macPrimitives , & macCallbacks , ACTIVE_REGION );
917918
918- DeviceState = DEVICE_STATE_INIT ;
919+ DeviceState = DEVICE_STATE_RESTORE ;
919920
920921 printf ( "###### ===== ClassA demo application v1.0.RC1 ==== ######\r\n\r\n" );
921922
@@ -931,8 +932,15 @@ int main( void )
931932
932933 switch ( DeviceState )
933934 {
934- case DEVICE_STATE_INIT :
935+ case DEVICE_STATE_RESTORE :
935936 {
937+ // Try to restore from NVM and query the mac if possible.
938+ if ( NvmCtxMgmtRestore ( ) == NVMCTXMGMT_STATUS_SUCCESS )
939+ {
940+ printf ( "\r\n###### ===== CTXS RESTORED ==== ######\r\n\r\n" );
941+ }
942+ else
943+ {
936944 mibReq .Type = MIB_APP_KEY ;
937945 mibReq .Param .AppKey = AppKey ;
938946 LoRaMacMibSetRequestConfirm ( & mibReq );
@@ -985,6 +993,7 @@ int main( void )
985993 mibReq .Param .AppSKey = AppSKey ;
986994 LoRaMacMibSetRequestConfirm ( & mibReq );
987995#endif
996+ }
988997 DeviceState = DEVICE_STATE_START ;
989998 break ;
990999 }
@@ -1117,13 +1126,18 @@ int main( void )
11171126 }
11181127 case DEVICE_STATE_SLEEP :
11191128 {
1129+ if ( NvmCtxMgmtStore ( ) == NVMCTXMGMT_STATUS_SUCCESS )
1130+ {
1131+ printf ( "\r\n###### ===== CTXS STORED ==== ######\r\n" );
1132+ }
1133+
11201134 // Wake up through events
11211135 BoardLowPowerHandler ( );
11221136 break ;
11231137 }
11241138 default :
11251139 {
1126- DeviceState = DEVICE_STATE_INIT ;
1140+ DeviceState = DEVICE_STATE_START ;
11271141 break ;
11281142 }
11291143 }
0 commit comments