Files
project-vesper/src/SettingsWebServer
bonaminandClaude Sonnet 4.6 d4d8b1c380 docs: add per-subsystem README files
Add inline documentation for all src/ subsystems covering purpose,
responsibilities, public API, and usage notes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-14 22:16:03 +02:00
..
2026-03-14 22:16:03 +02:00

SettingsWebServer

Summary

SettingsWebServer is a lightweight web interface that lets a user switch the device between AP mode and Station (router) mode, view basic device status, and trigger a reboot. It registers three HTTP endpoints on a shared AsyncWebServer instance, serves a single-page HTML UI (defined in SettingsPage.h and stored in PROGMEM), and persists mode changes through ConfigManager before issuing ESP.restart().

Status — superseded. The ongoing comms refactor (Stage 6) replaces this class with WebServerTransport, which registers the same three endpoints but routes form actions through the CommandBus instead of calling ConfigManager directly. SettingsPage.h remains in this directory and is still #included by WebServerTransport until the old directory is removed.


Public API

Method Signature Description
Constructor SettingsWebServer(AsyncWebServer& server, ConfigManager& configManager, Networking& networking) Stores references to dependencies. Does not register any routes.
begin void begin() Registers the three HTTP endpoints on server. Must be called before server.begin().

All other methods are private.


HTTP Endpoints

Registered by begin():

Method Path Behaviour
GET /settings Serves the settings HTML page with runtime values substituted into PROGMEM template.
POST /api/set-mode Accepts form body mode=ap|station. Calls ConfigManager::setPermanentAPMode() + saveNetworkConfig(), then delay(3000) + ESP.restart().
POST /api/reboot Responds with a plain-text ack, then delay(2000) + ESP.restart().

The page is reachable at:

  • http://192.168.4.1/settings when the device is in AP mode
  • http://<device-ip>/settings when in Station mode

Initialization

SettingsWebServer does not own the AsyncWebServer — it receives it by reference. The expected setup order in the caller is:

// 1. Construct dependencies first
ConfigManager configManager;
Networking    networking;
AsyncWebServer server(80);

// 2. Construct and register routes (before server.begin())
SettingsWebServer settingsWebServer(server, configManager, networking);
settingsWebServer.begin();

// 3. Start the server
server.begin();

ConfigManager must already be initialised (NVS loaded) before begin() is called, because handleSetMode writes to it immediately on receipt of a POST. Networking must be initialised before any request arrives, because generateSettingsHTML calls networking.isInAPMode() and networking.getLocalIP() on every page load.


Dependencies

Dependency Header Purpose
ESPAsyncWebServer <ESPAsyncWebServer.h> Non-blocking HTTP server
ConfigManager ConfigManager/ConfigManager.hpp Read device UID / fw version; write and persist AP mode flag
Networking Networking/Networking.hpp Read current IP and AP/station state for status display
Logging Logging/Logging.hpp LOG_INFO / LOG_DEBUG macros
SettingsPage.h (same directory) PROGMEM HTML template with %PLACEHOLDER% tokens

No SD card, I2C, or SPIFFS access. No FreeRTOS tasks are created.


Gotchas / Notes

Blocking delay() inside async callbacks. handleSetMode and handleReboot call delay() (3 s and 2 s respectively) on the AsyncWebServer callback task before calling ESP.restart(). This blocks the entire async task during that window. All in-flight requests will stall. This is intentional — the device is about to reboot — but be aware if adding new endpoints that share the same server.

ESP.restart() inside an async callback. ESP.restart() is called from within an AsyncWebServer request handler lambda. This works in practice on ESP32 because the preceding delay() gives the HTTP response time to flush, but it is not guaranteed by the library. If the response is not received by the browser, the user will see a network error before the reboot confirmation alert fires.

Single public method. begin() is the only call site ever needs to make after construction. There is no loop() method — AsyncWebServer is fully event-driven.

HTML loaded from PROGMEM on every request. generateSettingsHTML() allocates a String from FPSTR(SETTINGS_PAGE_HTML) on every GET /settings. The template is roughly 2.5 KB. On a heavily loaded device this could cause heap fragmentation; in normal usage (infrequent human access) it is fine.

No authentication. The settings page is unauthenticated. Anyone on the same network can trigger a mode change or reboot. This is by design for the provisioning/setup use case but should be noted for deployments where the device IP is reachable by untrusted clients.

Superseded by WebServerTransport. New code should not instantiate SettingsWebServer directly. WebServerTransport (in src/Communication/Transport/WebServerTransport/) provides identical endpoints and routes actions through the CommandBus. The only remaining shared artifact is SettingsPage.h, which WebServerTransport includes by path until this directory is cleaned up.