/* * nvm.c * * Created on: Aug 14, 2023 * Author: Sword */ #include #include "nvs_flash.h" #include "nvs.h" #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include #include "hmi.h" #include "nvm.h" #include "main.h" static const char* TAG = "NVM"; #define LOG_LOCAL_LEVEL ESP_LOG_INFO #include "esp_log.h" nvs_handle_t my_handle; RTC_DATA_ATTR static uint8_t last_posted_sector; extern uint8_t onBoarded; /**/ void nvm_init(void) { /* Initialize the dedicated NVS partition */ esp_err_t ret = nvs_flash_init(); /* Check if NVS partition doesn't contain any empty pages or contains data in new format and cannot be recognized by this version of code */ if((ret == ESP_ERR_NVS_NO_FREE_PAGES) || (ret == ESP_ERR_NVS_NEW_VERSION_FOUND)) { /* If so ----> then erase the NVS partition reinitialize it one more time*/ ESP_ERROR_CHECK(nvs_flash_erase()); /* reinitialize NVS partition one more time */ ret = nvs_flash_init(); } //Check that there is no problem with initializing NVS partition ESP_ERROR_CHECK(ret); } /**/ void nvm_clear(void) { /* erase the NVS partition */ ESP_ERROR_CHECK(nvs_flash_erase()); } /**/ uint8_t nvm_read_onboarding_flag(onboarding_type_t flag_key) { uint8_t onBoarding; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { //ESP_LOGI(TAG,"Done\nReading on-boarding value from NVS ... "); if(WIFI_ONBOARDING_KEY == flag_key) { err = nvs_get_u8(my_handle, NVM_WIFI_ONBOARDING_KEY, &onBoarding); } else { err = nvs_get_u8(my_handle, NVM_CELL_ONBOARDING_KEY, &onBoarding); } switch (err) { case ESP_OK: ESP_LOGI(TAG,"On-Boarding = %d\n",onBoarding); // Close the opened NVS nvs_close(my_handle); return onBoarding; break; case ESP_ERR_NVS_NOT_FOUND: ESP_LOGI(TAG,"The on-boarding value is not initialized yet!\n"); break; default: ESP_LOGI(TAG,"Error (%s) reading!\n", esp_err_to_name(err)); } } // Close the opened NVS nvs_close(my_handle); return 0; } /**/ void nvm_write_onboarding_flag(onboarding_type_t flag_key, uint8_t flag_value) { esp_err_t err; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { //ESP_LOGI(TAG,"Done\nUpdating on-boarding value in NVS ... "); if(WIFI_ONBOARDING_KEY == flag_key) { err = nvs_set_u8(my_handle, NVM_WIFI_ONBOARDING_KEY, flag_value); } else { err = nvs_set_u8(my_handle, NVM_CELL_ONBOARDING_KEY, flag_value); } if (err != ESP_OK) { ESP_LOGI(TAG,"Failed"); } else { //ESP_LOGI(TAG,"Done"); } // Commit written value. // After setting any values, nvs_commit() must be called to ensure changes are written // to flash storage. Implementations may write to storage at other times, // but this is not guaranteed. hmi_stop_red_flashing(); //ESP_LOGI(TAG,"Committing updates in NVS ... "); err = nvs_commit(my_handle); if (err != ESP_OK) { ESP_LOGI(TAG,"Failed Updating on-boarding value in NVS"); } else { ESP_LOGI(TAG,"Done Updating on-boarding value in NVS"); } } // Close the opened NVS nvs_close(my_handle); } /**/ uint8_t nvm_read_comms_mode(void) { uint8_t commsMode; esp_err_t err; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { //ESP_LOGI(TAG,"Done\nReading Communication-Mode value from NVS ... "); err = nvs_get_u8(my_handle, NVM_COMMS_MODE_KEY, &commsMode); switch (err) { case ESP_OK: ESP_LOGI(TAG,"Communication Mode = %d\n",commsMode); // Close the opened NVS nvs_close(my_handle); return commsMode; break; case ESP_ERR_NVS_NOT_FOUND: ESP_LOGI(TAG,"The Communication-Mode value is not initialized yet!\n"); break; default: ESP_LOGI(TAG,"Error (%s) reading!\n", esp_err_to_name(err)); } } // Close the opened NVS nvs_close(my_handle); return DEFAULT_COMMS_MODE; } /**/ void nvm_write_comms_mode(uint8_t commsMode) { esp_err_t err; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { //ESP_LOGI(TAG,"Done\nUpdating Communication-Mode value in NVS ... "); err = nvs_set_u8(my_handle, NVM_COMMS_MODE_KEY, commsMode); if (err != ESP_OK) { ESP_LOGI(TAG,"Failed"); } else { //ESP_LOGI(TAG,"Done"); } //ESP_LOGI(TAG,"Committing updates in NVS ... "); err = nvs_commit(my_handle); if (err != ESP_OK) { ESP_LOGI(TAG,"Failed Updating Communication-Mode value in NVS"); } else { ESP_LOGI(TAG,"Done Updating Communication-Mode value in NVS"); } } // Close the opened NVS nvs_close(my_handle); } /**/ esp_err_t nvm_read_wifi_credentials(char* ssid, char* pswd) { esp_err_t err; size_t ssid_len; size_t pswd_len; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } /* Make sure that NVS storage has been opened successfully */ if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { /* Get the length of the WIFI-SSID */ nvs_get_str(my_handle, NVM_WIFI_SSID_KEY, NULL, (size_t*)&ssid_len); /* Get the length of the WIFI-PSWD */ nvs_get_str(my_handle, NVM_WIFI_PSWD_KEY, NULL, (size_t*)&pswd_len); //ESP_LOGI(TAG,"NVM-SSID len = %d, and NVM-PWD len = %d",ssid_len,pswd_len); //char* sid = malloc(ssid_len); //char* pwd = malloc(pswd_len); /* Retrieve the WIFI-SSID */ if(ESP_OK == err) err = nvs_get_str(my_handle, (const char*)NVM_WIFI_SSID_KEY, ssid, (size_t*)&ssid_len); /* Retrieve the WIFI-PSWD */ if(ESP_OK == err) err = nvs_get_str(my_handle, (const char*)NVM_WIFI_PSWD_KEY, pswd, (size_t*)&pswd_len); /* Check the result of the NVS reading */ switch (err) { case ESP_OK: //strcpy(ssid,sid); //strcpy(pswd,pwd); ESP_LOGI(TAG,"WIFI_SSID is: %s and WIFI_PSWD is: %s",ssid,pswd); break; case ESP_ERR_NVS_NOT_FOUND: ESP_LOGI(TAG,"The WIFI_Credentials not initialized yet!\n"); break; default: ESP_LOGI(TAG,"Error (%s) reading!\n", esp_err_to_name(err)); } //free(sid); //free(pwd); } // Close the opened NVS nvs_close(my_handle); return err; } /**/ void nvm_write_wifi_credentials(char* ssid, uint8_t ssid_len, char* pswd, uint8_t pswd_len) { esp_err_t err; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGE(TAG,"Failed to reinitialized The NVS partition"); } } /* Make sure that NVS storage has been opened successfully */ if (err != ESP_OK) { ESP_LOGE(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { //ESP_LOGI(TAG,"Done\nUpdating WIFI-Credentials in NVS ... "); /* Store the WIFI-SSID string in NVS */ err = nvs_set_str(my_handle, (const char*)NVM_WIFI_SSID_KEY, (const char*)ssid); /* If WIFI-SSID stored successfully in NVS then ---> Store the WIFI-PSWD string in NVS */ if(ESP_OK == err) err = nvs_set_str(my_handle, (const char*)NVM_WIFI_PSWD_KEY, (const char*)pswd); /* Inform the user with one of these logs to indicate failing or succeeding to store wifi-credentials */ if (err != ESP_OK) { ESP_LOGE(TAG,"Failed"); } else { //ESP_LOGI(TAG,"Done"); } // Commit written value. // After setting any values, nvs_commit() must be called to ensure changes are written // to flash storage. Implementations may write to storage at other times, // but this is not guaranteed. //ESP_LOGI(TAG,"Committing updates in NVS ... "); err = nvs_commit(my_handle); if (err != ESP_OK) { ESP_LOGE(TAG,"Failed Updating WIFI-Credentials in NVS"); } else { ESP_LOGI(TAG,"Done Updating WIFI-Credentials in NVS"); } } // Close the opened NVS nvs_close(my_handle); } #if 0 /**/ void nvm_read_wifi_backup_credentials(char* ssid, char* pswd, char* key1, char* key2) { esp_err_t err; size_t ssid_len; size_t pswd_len; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } /* Make sure that NVS storage has been opened successfully */ if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { /* Get the length of the WIFI-SSID */ nvs_get_str(my_handle, key1, NULL, (size_t*)&ssid_len); /* Get the length of the WIFI-PSWD */ nvs_get_str(my_handle, key2, NULL, (size_t*)&pswd_len); ESP_LOGI(TAG,"NVM-SSID len = %d, and NVM-PWD len = %d",ssid_len,pswd_len); //char* sid = malloc(ssid_len); //char* pwd = malloc(pswd_len); /* Retrieve the WIFI-SSID */ if(ESP_OK == err) err = nvs_get_str(my_handle, (const char*)key1, ssid, (size_t*)&ssid_len); /* Retrieve the WIFI-PSWD */ if(ESP_OK == err) err = nvs_get_str(my_handle, (const char*)key2, pswd, (size_t*)&pswd_len); /* Check the result of the NVS reading */ switch (err) { case ESP_OK: //strcpy(ssid,sid); //strcpy(pswd,pwd); ESP_LOGI(TAG,"WIFI_SSID is: %s and WIFI_PSWD is: %s",ssid,pswd); break; case ESP_ERR_NVS_NOT_FOUND: ESP_LOGI(TAG,"The WIFI_Credentials not initialized yet!\n"); break; default: ESP_LOGI(TAG,"Error (%s) reading!\n", esp_err_to_name(err)); } //free(sid); //free(pwd); } // Close the opened NVS nvs_close(my_handle); } /**/ void nvm_write_wifi_backup_credentials(char* ssid1, char* pswd1, char* ssid2, char* pswd2) { esp_err_t err; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGE(TAG,"Failed to reinitialized The NVS partition"); } } /* Make sure that NVS storage has been opened successfully */ if (err != ESP_OK) { ESP_LOGE(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { ESP_LOGI(TAG,"Done\nUpdating WIFI-Credentials in NVS ... "); /* Store the WIFI-SSID string in NVS */ err = nvs_set_str(my_handle, (const char*)NVM_WIFI_SSID_KEY, (const char*)ssid); /* If WIFI-SSID stored successfully in NVS then ---> Store the WIFI-PSWD string in NVS */ if(ESP_OK == err) err = nvs_set_str(my_handle, (const char*)NVM_WIFI_PSWD_KEY, (const char*)pswd); /* Inform the user with one of these logs to indicate failing or succeeding to store wifi-credentials */ if (err != ESP_OK) { ESP_LOGE(TAG,"Failed"); } else { ESP_LOGI(TAG,"Done"); } // Commit written value. // After setting any values, nvs_commit() must be called to ensure changes are written // to flash storage. Implementations may write to storage at other times, // but this is not guaranteed. ESP_LOGI(TAG,"Committing updates in NVS ... "); err = nvs_commit(my_handle); if (err != ESP_OK) { ESP_LOGE(TAG,"Failed"); } else { ESP_LOGI(TAG,"Done"); } } // Close the opened NVS nvs_close(my_handle); } #endif /**/ void nvm_write_history_data(historyLog_t* history_data) { esp_err_t err; size_t required_size = 0; // value will default to 0, if not set yet in NVS char key_str[18]; uint8_t sector_counter = 1; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGE(TAG,"Failed to reinitialized The NVS partition"); } } /* Make sure that NVS storage has been opened successfully */ if (err != ESP_OK) { ESP_LOGE(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { do { /* Setup the NVS sector to write to */ sprintf(key_str,NVM_HISTORY_DATA_KEY,sector_counter); /* get the size of the history space */ err = nvs_get_blob(my_handle, key_str, NULL, &required_size); /* Check if we can store data at that sector or not*/ if(required_size >= (NVM_MAX_NUMBER_OF_IN_ONE_SECTOR*sizeof(historyLog_t))) { /* Reset the required_size again to 0 */ required_size = 0; /* if not then check the next sector */ sector_counter++; } else { /* if yes then increase the size of data to be stored by one element */ required_size += sizeof(historyLog_t); break; } }while(sector_counter <= NVM_NUMBER_OF_SECTORS); /* Make sure that there are some free sectors to store data in */ if(sector_counter <= NVM_NUMBER_OF_SECTORS) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, key_str, history_data, required_size); if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) ESP_LOGI(TAG," ---> History space not there"); if (required_size == 0) { ESP_LOGI(TAG," ---> Nothing saved yet"); } else { // Commit err = nvs_commit(my_handle); if (err == ESP_OK) ESP_LOGI(TAG," ---> the new history data has been committed successfully to nvm-sector %d\nand its size now is %d (max allowed size is %d)",sector_counter,required_size,(NVM_MAX_NUMBER_OF_IN_ONE_SECTOR*sizeof(historyLog_t))); } } else { ESP_LOGE(TAG," ----> All the NVM sectors are full. can't store additional data"); } } // Close nvs_close(my_handle); } /**/ size_t nvm_read_history_data(historyLog_t* history_data, uint8_t sector_number) { esp_err_t err; size_t required_size = 0; // value will default to 0, if not set yet in NVS char key_str[18]; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { /* Setup the NVS sector to write to */ sprintf(key_str,NVM_HISTORY_DATA_KEY,sector_number); // obtain required memory space to store blob being read from NVS err = nvs_get_blob(my_handle, key_str, NULL, &required_size); if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) ESP_LOGI(TAG," ---> History not found yet"); //ESP_LOGI(TAG,"History data are:"); if (required_size == 0) { ESP_LOGI(TAG," ---> Nothing saved yet!\n"); } else { err = nvs_get_blob(my_handle, key_str, history_data, &required_size); if (err != ESP_OK) { ESP_LOGI(TAG," ---> ERROR in retrieving history data for NVM sector %d",sector_number); } } } // Close nvs_close(my_handle); return required_size; } /**/ void nvm_clear_history_sector(uint8_t sector_num) { esp_err_t wifi_err; uint8_t wifi_onboarding; uint8_t cell_onboarding; uint8_t comms_mode; char ssid[32] = {0}; char pswd[64] = {0}; ESP_LOGI(TAG,"Clearing NVM History Sectors After successful POST process"); /* If there are some written data in the first sector ---> then this means that you should clear it. otherwise no (just to save read-write cycles for nvs) */ if(nvm_read_history_data(NULL,1)) { /* 1- Firstly retrieve stored data in NVS */ wifi_onboarding = nvm_read_onboarding_flag(WIFI_ONBOARDING_KEY); cell_onboarding = nvm_read_onboarding_flag(CELL_ONBOARDING_KEY); comms_mode = nvm_read_comms_mode(); wifi_err = nvm_read_wifi_credentials(ssid,pswd); /* Reload the WDT */ vTaskDelay(10/portTICK_PERIOD_MS); /* 2- Clear NVS partition */ nvm_clear(); /* Reload the WDT */ vTaskDelay(10/portTICK_PERIOD_MS); /* 3- Store the retrieved data again in NVS */ nvm_write_onboarding_flag(WIFI_ONBOARDING_KEY,wifi_onboarding); nvm_write_onboarding_flag(CELL_ONBOARDING_KEY,cell_onboarding); nvm_write_comms_mode(comms_mode); if(wifi_err == ESP_OK) nvm_write_wifi_credentials(ssid,32,pswd,64); } #if 0 esp_err_t err; char key_str[18]; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { for(int sector_counter=1; sector_counter<=NVM_NUMBER_OF_SECTORS; sector_counter++) { /* Setup the NVS sector to clear */ sprintf(key_str,NVM_HISTORY_DATA_KEY,sector_counter); /* Check which sector number need to be cleared */ if((sector_num == sector_counter) || ((sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, key_str, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector %d",sector_counter); break; } else { /* Check if the sector_num isn't equal to the NVM_HISTORY_ALL_SECTORS then break the loop after first successful clear process */ if(sector_num != NVM_HISTORY_ALL_SECTORS) { break; } } } } #endif #if 0 if((sector_num == NVM_HISTORY_SECTOR_1) || ((sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR1, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 1"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_2) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR2, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 2"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_3) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR3, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 3"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_4) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR4, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 4"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_5) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR5, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 5"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_6) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR6, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 6"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_7) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR7, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 7"); } } if((err == ESP_OK) && ((sector_num == NVM_HISTORY_SECTOR_8) || (sector_num == NVM_HISTORY_ALL_SECTORS))) { /* set the new values for the history data */ err = nvs_set_blob(my_handle, NVM_HISTORY_SECTOR8, NULL, 0); if(err != ESP_OK) { ESP_LOGI(TAG,"Failed to clear sector 8"); } } #endif // err = nvs_commit(my_handle); // } // Close // nvs_close(my_handle); } /**/ uint8_t nvm_get_last_written_history_sector(void) { esp_err_t err; size_t required_size = 0; // value will default to 0, if not set yet in NVS char key_str[18]; uint8_t sector_counter = 1; /*Storing the on-boarding value in the NVS so that to indicate that each time power on/off the value would be the same*/ //ESP_LOGI(TAG,"Opening Non-Volatile Storage (NVS) handle... "); err = nvs_open("storage", NVS_READWRITE, &my_handle); /* Make sure that NVS is initialized */ if(ESP_ERR_NVS_NOT_INITIALIZED == err) { /* initialized it if not */ err = nvs_flash_init(); /* Check if the response is OK then OPEN it */ if(err == ESP_OK) { /* Retry to open the NVS */ err = nvs_open("storage", NVS_READWRITE, &my_handle); } else { /* Print this LOG if failed to initialize it */ ESP_LOGI(TAG,"Failed to reinitialized The NVS partition"); } } if (err != ESP_OK) { ESP_LOGI(TAG,"Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { do { /* Setup the key of NVS sector to read the size of it's written data */ sprintf(key_str,NVM_HISTORY_DATA_KEY,sector_counter); /* get the size of the history space */ err = nvs_get_blob(my_handle, key_str, NULL, &required_size); /* Check if that sector is not full */ if(required_size >= (NVM_MAX_NUMBER_OF_IN_ONE_SECTOR*sizeof(historyLog_t))) { /* Reset the required_size again to 0 */ required_size = 0; /* if full then check the next sector */ sector_counter++; } else { /* if not full then return the sector_number */ break; } }while(sector_counter <= NVM_NUMBER_OF_SECTORS); if(sector_counter > NVM_NUMBER_OF_SECTORS) { /* Setup the key of NVS sector to read it's written data */ sector_counter--; } } // Close nvs_close(my_handle); return sector_counter; } /**/ void nvm_set_last_posted_history_sector(uint8_t sector_num) { if(sector_num <= NVM_NUMBER_OF_SECTORS) last_posted_sector = sector_num; else last_posted_sector = 0; } /**/ uint8_t nvm_get_last_posted_history_sector(void) { return last_posted_sector; }