Separating users from tasks + fixing weird ota bug
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// STD Lib
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// SDK
|
||||
#include "esp_log.h"
|
||||
@@ -23,7 +24,12 @@
|
||||
#include "http_server.cpp"
|
||||
// clang-format on
|
||||
|
||||
internal constexpr bool kBlockUntilEthernetEstablished = false;
|
||||
// Global Application State Definitions
|
||||
bool g_Ethernet_Initialized = false;
|
||||
bool g_Wifi_Initialized = false;
|
||||
uint8_t g_Active_WWW_Partition = 0;
|
||||
|
||||
constexpr bool kBlockUntilEthernetEstablished = false;
|
||||
|
||||
extern "C" void app_main()
|
||||
{
|
||||
@@ -46,6 +52,10 @@ extern "C" void app_main()
|
||||
{
|
||||
// Read active www partition from NVS
|
||||
err = nvs_get_u8(my_handle, "www_part", &g_Active_WWW_Partition);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
printf("NVS: Found active www partition: %d\n", g_Active_WWW_Partition);
|
||||
}
|
||||
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND)
|
||||
{
|
||||
@@ -74,6 +84,76 @@ extern "C" void app_main()
|
||||
printf("Error opening NVS handle!\n");
|
||||
}
|
||||
|
||||
// Detect if this is the first boot after a new flash (Firmware or Frontend)
|
||||
bool is_new_flash = false;
|
||||
if (nvs_open("storage", NVS_READWRITE, &my_handle) == ESP_OK)
|
||||
{
|
||||
// 1. Check Firmware Compile Time
|
||||
char last_time[64] = {0};
|
||||
size_t time_size = sizeof(last_time);
|
||||
const char *current_time = __DATE__ " " __TIME__;
|
||||
if (nvs_get_str(my_handle, "last_fw_time", last_time, &time_size) !=
|
||||
ESP_OK ||
|
||||
strcmp(last_time, current_time) != 0)
|
||||
{
|
||||
printf("New firmware detected! (Last: %s, Current: %s)\n",
|
||||
last_time[0] ? last_time : "None", current_time);
|
||||
is_new_flash = true;
|
||||
nvs_set_str(my_handle, "last_fw_time", current_time);
|
||||
}
|
||||
|
||||
// 2. Check Frontend Partition Fingerprint (www_0)
|
||||
const esp_partition_t *www0_p = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, "www_0");
|
||||
if (www0_p)
|
||||
{
|
||||
uint8_t current_sha[32];
|
||||
if (esp_partition_get_sha256(www0_p, current_sha) == ESP_OK)
|
||||
{
|
||||
uint8_t last_sha[32] = {0};
|
||||
size_t sha_size = sizeof(last_sha);
|
||||
if (nvs_get_blob(my_handle, "www0_sha", last_sha, &sha_size) !=
|
||||
ESP_OK ||
|
||||
memcmp(last_sha, current_sha, 32) != 0)
|
||||
{
|
||||
printf("New frontend partition detected via SHA256!\n");
|
||||
is_new_flash = true;
|
||||
nvs_set_blob(my_handle, "www0_sha", current_sha, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_new_flash)
|
||||
{
|
||||
nvs_commit(my_handle);
|
||||
}
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
|
||||
// If we are running from FACTORY and a new flash was detected, override to
|
||||
// www_0
|
||||
{
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
if (running != NULL &&
|
||||
running->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY)
|
||||
{
|
||||
if (is_new_flash && g_Active_WWW_Partition != 0)
|
||||
{
|
||||
printf("FACTORY APP + NEW FLASH: Overriding www_part to 0 (was %d)\n",
|
||||
g_Active_WWW_Partition);
|
||||
g_Active_WWW_Partition = 0;
|
||||
|
||||
// Persist the override
|
||||
if (nvs_open("storage", NVS_READWRITE, &my_handle) == ESP_OK)
|
||||
{
|
||||
nvs_set_u8(my_handle, "www_part", 0);
|
||||
nvs_commit(my_handle);
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
setup_led();
|
||||
@@ -168,6 +248,9 @@ extern "C" void app_main()
|
||||
|
||||
printf("Connected!\n");
|
||||
|
||||
// Start the webserver
|
||||
web_server = start_webserver();
|
||||
|
||||
// Mark the current app as valid to cancel rollback, only if it's an OTA app
|
||||
{
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
@@ -179,9 +262,6 @@ extern "C" void app_main()
|
||||
}
|
||||
}
|
||||
|
||||
// Start the webserver
|
||||
web_server = start_webserver();
|
||||
|
||||
// Keep the main task alive indefinitely
|
||||
while (true)
|
||||
{
|
||||
@@ -193,7 +273,7 @@ shutdown:
|
||||
|
||||
if (web_server)
|
||||
{
|
||||
stop_webserver(web_server);
|
||||
stop_webserver(web_server, g_Active_WWW_Partition);
|
||||
web_server = NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user