From 20c8c98b38e38c1c328169ae48a97a2d333ede01 Mon Sep 17 00:00:00 2001 From: Terrence Date: Tue, 10 Sep 2024 00:45:13 +0800 Subject: [PATCH] add reset to factory --- main/CMakeLists.txt | 1 + main/SystemReset.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++ main/SystemReset.h | 17 ++++++++++++ main/main.cc | 19 ++++--------- 4 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 main/SystemReset.cc create mode 100644 main/SystemReset.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e6a45c08..e11275de 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,6 @@ set(SOURCES "AudioDevice.cc" "SystemInfo.cc" + "SystemReset.cc" "WebSocketClient.cc" "OpusEncoder.cc" "BuiltinLed.cc" diff --git a/main/SystemReset.cc b/main/SystemReset.cc new file mode 100644 index 00000000..038915cb --- /dev/null +++ b/main/SystemReset.cc @@ -0,0 +1,67 @@ +#include "SystemReset.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "driver/gpio.h" +#include "esp_partition.h" +#include "esp_system.h" +#include "freertos/FreeRTOS.h" + + +#define TAG "SystemReset" + + +SystemReset::SystemReset() { + // Configure GPIO1, GPIO2 as INPUT, reset NVS flash if the button is pressed + gpio_config_t io_conf; + io_conf.intr_type = GPIO_INTR_DISABLE; + io_conf.mode = GPIO_MODE_INPUT; + io_conf.pin_bit_mask = (1ULL << GPIO_NUM_1) | (1ULL << GPIO_NUM_2); + io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; + gpio_config(&io_conf); +} + + +void SystemReset::CheckButtons() { + if (gpio_get_level(GPIO_NUM_2) == 0) { + ESP_LOGI(TAG, "Button is pressed, reset to factory"); + ResetNvsFlash(); + ResetToFactory(); + } + + if (gpio_get_level(GPIO_NUM_1) == 0) { + ESP_LOGI(TAG, "Button is pressed, reset NVS flash"); + ResetNvsFlash(); + } +} + +void SystemReset::ResetNvsFlash() { + ESP_LOGI(TAG, "Resetting NVS flash"); + esp_err_t ret = nvs_flash_erase(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to erase NVS flash"); + } +} + +void SystemReset::ResetToFactory() { + ESP_LOGI(TAG, "Resetting to factory"); + // Erase otadata partition + const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL); + if (partition == NULL) { + ESP_LOGE(TAG, "Failed to find otadata partition"); + return; + } + esp_partition_erase_range(partition, 0, partition->size); + ESP_LOGI(TAG, "Erased otadata partition"); + + // Reboot in 3 seconds + RestartInSeconds(3); +} + +void SystemReset::RestartInSeconds(int seconds) { + for (int i = seconds; i > 0; i--) { + ESP_LOGI(TAG, "Resetting in %d seconds", i); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + esp_restart(); +} diff --git a/main/SystemReset.h b/main/SystemReset.h new file mode 100644 index 00000000..0c34a771 --- /dev/null +++ b/main/SystemReset.h @@ -0,0 +1,17 @@ +#ifndef _SYSTEM_RESET_H +#define _SYSTEM_RESET_H + +class SystemReset { +public: + SystemReset(); + + void CheckButtons(); + +private: + void ResetNvsFlash(); + void ResetToFactory(); + void RestartInSeconds(int seconds); +}; + + +#endif diff --git a/main/main.cc b/main/main.cc index 610e429a..7b038bae 100755 --- a/main/main.cc +++ b/main/main.cc @@ -9,29 +9,20 @@ #include "WifiConfigurationAp.h" #include "Application.h" #include "SystemInfo.h" +#include "SystemReset.h" #define TAG "main" #define STATS_TICKS pdMS_TO_TICKS(1000) extern "C" void app_main(void) { + // Check if the reset button is pressed + SystemReset system_reset; + system_reset.CheckButtons(); + // Initialize the default event loop ESP_ERROR_CHECK(esp_event_loop_create_default()); - // Configure GPIO1 as INPUT, reset NVS flash if the button is pressed - gpio_config_t io_conf; - io_conf.intr_type = GPIO_INTR_DISABLE; - io_conf.mode = GPIO_MODE_INPUT; - io_conf.pin_bit_mask = 1ULL << 1; - io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; - io_conf.pull_up_en = GPIO_PULLUP_ENABLE; - gpio_config(&io_conf); - - if (gpio_get_level(GPIO_NUM_1) == 0) { - ESP_LOGI(TAG, "Button is pressed, reset NVS flash"); - nvs_flash_erase(); - } - // Initialize NVS flash for WiFi configuration esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {