From 65def379fc4375a04e80569300705b040e953bef Mon Sep 17 00:00:00 2001 From: Pawel Jalocha Date: Tue, 20 Oct 2020 00:35:45 +0100 Subject: [PATCH] Reset the LoRaWAN state when new AppKey supplied --- main/main.cpp | 14 ++++++++------ main/parameters.h | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 16de427..e5b440a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -63,12 +63,14 @@ void app_main(void) } #endif #ifdef WITH_LORAWAN - WANdev.Reset(getUniqueID(), Parameters.AppKey); // set default LoRaWAN config. - if(WANdev.ReadFromNVS()!=ESP_OK) // if can't read the LoRaWAN setup from NVS - { WANdev.WriteToNVS(); } // then store the default - if( /* WANdev.State<2 && */ memcmp(WANdev.AppKey, Parameters.AppKey, 16)) // if LoRaWAN key different from the one in Parameters - { WANdev.Reset(getUniqueID(), Parameters.AppKey); // then reset LoRaWAN to this key - WANdev.WriteToNVS(); } // and save LoRaWAN config. to NVS + WANdev.Reset(getUniqueID(), Parameters.AppKey); // set default LoRaWAN config. + if(WANdev.ReadFromNVS()!=ESP_OK) // if can't read the LoRaWAN setup from NVS + { WANdev.WriteToNVS(); } // then store the default + if(Parameters.hasAppKey()) + { if(!Parameters.sameAppKey(WANdev.AppKey)) // if LoRaWAN key different from the one in Parameters + { WANdev.Reset(getUniqueID(), Parameters.AppKey); // then reset LoRaWAN to this key + WANdev.WriteToNVS(); } // and save LoRaWAN config. to NVS + Parameters.clrAppKey(); } #endif CONS_UART_SetBaudrate(Parameters.CONbaud); diff --git a/main/parameters.h b/main/parameters.h index 6bef14e..275e7c4 100644 --- a/main/parameters.h +++ b/main/parameters.h @@ -156,6 +156,19 @@ class FlashParameters static const uint32_t CheckInit = 0x89ABCDEF; +#ifdef WITH_LORAWAN + bool hasAppKey(void) const + { uint8_t Sum=AppKey[0]; + for(int Idx=1; Idx<16; Idx++) + { if(Sum!=0) break; + Sum|=AppKey[Idx]; } + return Sum!=0; } + + void clrAppKey(void) { for(int Idx=0; Idx<16; Idx++) AppKey[Idx]=0; } + void cpyAppKey(uint8_t *Key) { memcpy(Key, AppKey, 16); } + bool sameAppKey(const uint8_t *RefKey) const { return memcmp(AppKey, RefKey, 16)==0; } +#endif + uint32_t static calcCheckSum(volatile uint32_t *Word, uint32_t Words) // calculate check-sum of pointed data { uint32_t Check=CheckInit; for(uint32_t Idx=0; Idx