diff --git a/Provider/main/CMakeLists.txt b/Provider/main/CMakeLists.txt index 8e06816..f7d65df 100644 --- a/Provider/main/CMakeLists.txt +++ b/Provider/main/CMakeLists.txt @@ -8,7 +8,7 @@ idf_component_register(SRCS "main.cpp" if(CONFIG_CALENDINK_DEPLOY_WEB_PAGES) set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../frontend") if(EXISTS ${WEB_SRC_DIR}/dist) - littlefs_create_partition_image(www ${WEB_SRC_DIR}/dist FLASH_IN_PROJECT) + littlefs_create_partition_image(www_0 ${WEB_SRC_DIR}/dist FLASH_IN_PROJECT) else() message(FATAL_ERROR "'${WEB_SRC_DIR}/dist' doesn't exist. Run 'npm run build' in frontend/ first.") endif() diff --git a/Provider/main/appstate.hpp b/Provider/main/appstate.hpp index 4e007b9..5fb8584 100644 --- a/Provider/main/appstate.hpp +++ b/Provider/main/appstate.hpp @@ -5,3 +5,4 @@ // Shared Application State (Unity Build) internal bool g_Ethernet_Initialized = false; internal bool g_Wifi_Initialized = false; +internal uint8_t g_Active_WWW_Partition = 0; diff --git a/Provider/main/http_server.cpp b/Provider/main/http_server.cpp index 73ccd3b..8589b58 100644 --- a/Provider/main/http_server.cpp +++ b/Provider/main/http_server.cpp @@ -178,7 +178,7 @@ internal httpd_handle_t start_webserver(void) #ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES esp_vfs_littlefs_conf_t conf = {}; conf.base_path = "/www"; - conf.partition_label = "www"; + conf.partition_label = g_Active_WWW_Partition == 0 ? "www_0" : "www_1"; conf.format_if_mount_failed = false; conf.dont_mount = false; esp_err_t ret = esp_vfs_littlefs_register(&conf); @@ -253,7 +253,8 @@ internal void stop_webserver(httpd_handle_t server) { httpd_stop(server); #ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES - esp_vfs_littlefs_unregister("www"); + esp_vfs_littlefs_unregister(g_Active_WWW_Partition == 0 ? "www_0" + : "www_1"); #endif } } diff --git a/Provider/main/main.cpp b/Provider/main/main.cpp index 7e24118..31d033b 100644 --- a/Provider/main/main.cpp +++ b/Provider/main/main.cpp @@ -5,10 +5,12 @@ #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "nvs.h" #include "nvs_flash.h" #include "sdkconfig.h" #include "soc/gpio_num.h" + // Project headers #include "appstate.hpp" #include "types.hpp" @@ -28,7 +30,33 @@ extern "C" void app_main() httpd_handle_t web_server = NULL; - ESP_ERROR_CHECK(nvs_flash_init()); + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); + + nvs_handle_t my_handle; + if (nvs_open("storage", NVS_READWRITE, &my_handle) == ESP_OK) + { + err = nvs_get_u8(my_handle, "www_part", &g_Active_WWW_Partition); + if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) + { + printf("Error reading www_part from NVS: %s\n", esp_err_to_name(err)); + } + if (g_Active_WWW_Partition > 1) + { + g_Active_WWW_Partition = 0; + } + nvs_close(my_handle); + } + else + { + printf("Error opening NVS handle!\n"); + } + ESP_ERROR_CHECK(esp_event_loop_create_default()); setup_led(); diff --git a/Provider/partitions.csv b/Provider/partitions.csv index 707d6df..9c44e65 100644 --- a/Provider/partitions.csv +++ b/Provider/partitions.csv @@ -2,4 +2,5 @@ nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, 0x10000, 1M, -www, data, littlefs, , 128K, +www_0, data, littlefs, , 1M, +www_1, data, littlefs, , 1M,