CLI: factory reset

This commit is contained in:
DrZlo13 2021-12-03 10:33:06 +10:00
parent 3a89455938
commit 23fb0b3862
3 changed files with 50 additions and 6 deletions

View File

@ -1,14 +1,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "cli.h" #include "cli.h"
#include "nvs.h"
#include "helpers.h"
#include "cli-args.h" #include "cli-args.h"
#include "cli-commands.h" #include "cli-commands.h"
#include "helpers.h"
void cli_help(Cli* cli, mstring_t* args); void cli_help(Cli* cli, mstring_t* args);
void cli_gpio_set(Cli* cli, mstring_t* args); void cli_gpio_set(Cli* cli, mstring_t* args);
void cli_gpio_get(Cli* cli, mstring_t* args); void cli_gpio_get(Cli* cli, mstring_t* args);
void cli_device_info(Cli* cli, mstring_t* args); void cli_device_info(Cli* cli, mstring_t* args);
void cli_factory_reset(Cli* cli, mstring_t* args);
void cli_wifi_scan(Cli* cli, mstring_t* args); void cli_wifi_scan(Cli* cli, mstring_t* args);
const CliItem cli_items[] = { const CliItem cli_items[] = {
@ -40,6 +42,10 @@ const CliItem cli_items[] = {
.name = "device_info", .name = "device_info",
.callback = cli_device_info, .callback = cli_device_info,
}, },
{
.name = "factory_reset",
.callback = cli_factory_reset,
},
}; };
size_t cli_items_count = COUNT_OF(cli_items); size_t cli_items_count = COUNT_OF(cli_items);
@ -53,3 +59,10 @@ void cli_help(Cli* cli, mstring_t* args) {
} }
} }
} }
void cli_factory_reset(Cli* cli, mstring_t* args) {
cli_write_str(cli, "Erasing NVS");
cli_write_eol(cli);
nvs_erase();
cli_write_str(cli, "OK");
}

View File

@ -6,17 +6,39 @@
#define NVS_STORE "nvs_storage" #define NVS_STORE "nvs_storage"
void nvs_init(void) { void nvs_init(void) {
ESP_LOGI(TAG, "init"); ESP_LOGI(TAG, "init " NVS_DEFAULT_PART_NAME);
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init_partition(NVS_DEFAULT_PART_NAME);
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGI(TAG, "erasing"); ESP_LOGI(TAG, "erasing " NVS_DEFAULT_PART_NAME);
ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_DEFAULT_PART_NAME));
ret = nvs_flash_init(); ret = nvs_flash_init_partition(NVS_DEFAULT_PART_NAME);
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "init " NVS_STORE);
ret = nvs_flash_init_partition(NVS_STORE);
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGI(TAG, "erasing " NVS_STORE);
ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_STORE));
ret = nvs_flash_init_partition(NVS_STORE);
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "init done"); ESP_LOGI(TAG, "init done");
} }
void nvs_erase(void) {
ESP_LOGI(TAG, "erasing " NVS_DEFAULT_PART_NAME);
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
ESP_LOGI(TAG, "erasing " NVS_STORE);
ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_STORE));
ESP_ERROR_CHECK(nvs_flash_init_partition(NVS_STORE));
ESP_LOGI(TAG, "erasing done");
}
esp_err_t nvs_save_string(const char* key, const mstring_t* value) { esp_err_t nvs_save_string(const char* key, const mstring_t* value) {
nvs_handle_t nvs_handle; nvs_handle_t nvs_handle;
esp_err_t err; esp_err_t err;

View File

@ -1,7 +1,16 @@
/**
* @file nvs.h
* @author Sergey Gavrilov (who.just.the.doctor@gmail.com)
* @version 1.0
* @date 2021-12-03
*
* NVS management
*/
#pragma once #pragma once
#include <m-string.h> #include <m-string.h>
#include <esp_err.h> #include <esp_err.h>
void nvs_init(void); void nvs_init(void);
void nvs_erase(void);
esp_err_t nvs_save_string(const char* key, const mstring_t* value); esp_err_t nvs_save_string(const char* key, const mstring_t* value);
esp_err_t nvs_load_string(const char* key, mstring_t* value); esp_err_t nvs_load_string(const char* key, mstring_t* value);