feat: Implement HTTP server with static file serving from LittleFS, system APIs, and network connectivity management.

This commit is contained in:
2026-03-03 16:17:05 -05:00
parent 80114b1d9f
commit 7c537ed4db
5 changed files with 36 additions and 5 deletions

View File

@@ -8,7 +8,7 @@ idf_component_register(SRCS "main.cpp"
if(CONFIG_CALENDINK_DEPLOY_WEB_PAGES) if(CONFIG_CALENDINK_DEPLOY_WEB_PAGES)
set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../frontend") set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../frontend")
if(EXISTS ${WEB_SRC_DIR}/dist) 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() else()
message(FATAL_ERROR "'${WEB_SRC_DIR}/dist' doesn't exist. Run 'npm run build' in frontend/ first.") message(FATAL_ERROR "'${WEB_SRC_DIR}/dist' doesn't exist. Run 'npm run build' in frontend/ first.")
endif() endif()

View File

@@ -5,3 +5,4 @@
// Shared Application State (Unity Build) // Shared Application State (Unity Build)
internal bool g_Ethernet_Initialized = false; internal bool g_Ethernet_Initialized = false;
internal bool g_Wifi_Initialized = false; internal bool g_Wifi_Initialized = false;
internal uint8_t g_Active_WWW_Partition = 0;

View File

@@ -178,7 +178,7 @@ internal httpd_handle_t start_webserver(void)
#ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES #ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES
esp_vfs_littlefs_conf_t conf = {}; esp_vfs_littlefs_conf_t conf = {};
conf.base_path = "/www"; 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.format_if_mount_failed = false;
conf.dont_mount = false; conf.dont_mount = false;
esp_err_t ret = esp_vfs_littlefs_register(&conf); esp_err_t ret = esp_vfs_littlefs_register(&conf);
@@ -253,7 +253,8 @@ internal void stop_webserver(httpd_handle_t server)
{ {
httpd_stop(server); httpd_stop(server);
#ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES #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 #endif
} }
} }

View File

@@ -5,10 +5,12 @@
#include "esp_log.h" #include "esp_log.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "soc/gpio_num.h" #include "soc/gpio_num.h"
// Project headers // Project headers
#include "appstate.hpp" #include "appstate.hpp"
#include "types.hpp" #include "types.hpp"
@@ -28,7 +30,33 @@ extern "C" void app_main()
httpd_handle_t web_server = NULL; 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()); ESP_ERROR_CHECK(esp_event_loop_create_default());
setup_led(); setup_led();

View File

@@ -2,4 +2,5 @@
nvs, data, nvs, 0x9000, 0x6000, nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000, phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M, factory, app, factory, 0x10000, 1M,
www, data, littlefs, , 128K, www_0, data, littlefs, , 1M,
www_1, data, littlefs, , 1M,
1 # Name Type SubType Offset Size Flags
2 nvs data nvs 0x9000 0x6000
3 phy_init data phy 0xf000 0x1000
4 factory app factory 0x10000 1M
5 www www_0 data littlefs 128K 1M
6 www_1 data littlefs 1M