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>
This commit is contained in:
2026-03-14 22:16:03 +02:00
co-authored by Claude Sonnet 4.6
parent d4bd990e53
commit d4d8b1c380
29 changed files with 2961 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
# Networking
## Summary
`Networking` manages all WiFi connectivity for the Vesper device. It owns the connection lifecycle — initial association, timeout detection, automatic reconnection, and fallback to a WiFiManager captive portal or permanent AP mode — and exposes a minimal callback interface so the rest of the firmware reacts to link-up/link-down events without polling. Ethernet hardware is present in the source but permanently disabled as of firmware v138; the code paths are commented out and will not execute.
---
## Public API
| Method | Description |
|---|---|
| `Networking(ConfigManager& configManager)` | Constructor. Allocates `WiFiManager`, registers static instance pointer. |
| `void begin()` | Initializes the FreeRTOS reconnection timer, registers the Arduino WiFi event handler, and starts the first connection attempt (or permanent AP mode if configured). |
| `bool isConnected() const` | Returns `true` if any transport (`WIFI` or `AP`) has an active connection. |
| `String getLocalIP() const` | Returns the current IP address as a string; `"0.0.0.0"` when disconnected. |
| `String getGateway() const` | Returns the gateway IP string for the active WiFi connection. |
| `ConnectionType getActiveConnection() const` | Returns the `ConnectionType` enum value: `NONE`, `WIFI`, or `AP`. |
| `NetworkState getState() const` | Returns the full `NetworkState` enum for more granular status inspection. |
| `void setNetworkCallbacks(onConnected, onDisconnected)` | Registers `std::function<void()>` callbacks fired on link-up and link-down events. Must be called before the first connection completes to avoid missing the event. |
| `void forceReconnect()` | Tears down the current WiFi connection and restarts the connection sequence. Intended for debug/testing; blocks for ~1 second during teardown. |
| `void startPermanentAPMode()` | Switches to a fixed AP at `192.168.4.1` using the SSID/password from `NetworkConfig`. Sets state to `AP_MODE_PERMANENT`. |
| `bool isInAPMode() const` | Returns `true` when state is `AP_MODE_PERMANENT`. |
| `bool isHealthy() const` | Returns `true` if connected with a valid IP and, for WiFi, RSSI >= -80 dBm. Used by `HealthMonitor`. |
### Enumerations
**`NetworkState`**
| Value | Meaning |
|---|---|
| `DISCONNECTED` | No active connection, not attempting |
| `CONNECTING_ETHERNET` | Ethernet attempt in progress (disabled, unused) |
| `CONNECTING_WIFI` | WiFi association in progress |
| `WIFI_PORTAL_MODE` | WiFiManager captive portal is active |
| `CONNECTED_ETHERNET` | Ethernet up (disabled, unused) |
| `CONNECTED_WIFI` | WiFi up and IP assigned |
| `RECONNECTING` | Transitional state during `forceReconnect()` |
| `AP_MODE_PERMANENT` | Device is running as a standalone AP |
**`ConnectionType`**: `NONE`, `ETHERNET`, `WIFI`, `AP`
---
## Initialization
`Networking` is constructed with a reference to `ConfigManager`, then initialized in `setup()` after `ConfigManager::begin()` has run (so network settings from SD are available):
```cpp
Networking networking(configManager);
// ...
networking.begin();
```
Network callbacks **must be registered after `begin()`** and before `setup()` returns, because a fast WiFi reconnect can fire the connected callback during the same `setup()` call:
```cpp
networking.setNetworkCallbacks(
[]() { communication.onNetworkConnected(); /* start server, NTP, etc. */ },
[]() { communication.onNetworkDisconnected(); }
);
// Handle the case where WiFi was already up before callbacks were registered:
if (networking.isConnected()) {
communication.onNetworkConnected();
}
```
The `AsyncWebServer` must **not** be started before the `onNetworkConnected` callback fires, because `WiFiManager` binds port 80 during portal mode. Starting the server before the portal closes causes a port conflict.
---
## Dependencies
| Dependency | Role |
|---|---|
| `ConfigManager` | Reads `NetworkConfig` (SSID, PSK, static IP, hostname, AP credentials, `permanentAPMode` flag) |
| `WiFiManager` (library) | Handles captive-portal credential entry when saved credentials are absent or fail repeatedly |
| `WiFi` (Arduino/ESP32) | Underlying WiFi driver; event handler registered via `WiFi.onEvent()` |
| FreeRTOS | One-shot `TimerHandle_t` for reconnection scheduling; reconnect logic runs in a short-lived task (`netRecon`) |
---
## Gotchas / Notes
**Ethernet is disabled.** The code contains Ethernet paths (`ARDUINO_EVENT_ETH_*` handlers, `startEthernetConnection`, `onEthernetConnected`, etc.) that are entirely commented out or guarded with early returns. Enabling Ethernet requires uncommenting the `ETH.begin(...)` call in `begin()` and restoring the disabled code blocks.
**Static `_instance` pointer.** The event handler is a static function that dispatches through `Networking::_instance`. Only one `Networking` instance should exist at runtime. Constructing a second instance logs a warning and overwrites the pointer, which will break event dispatch for the original instance.
**Reconnection timer runs in a task.** `reconnectionTimerCallback` is called from the FreeRTOS timer service task (2 KB stack). To avoid stack overflow, the callback immediately spawns a `netRecon` task (3072 bytes) which calls `handleReconnection()` and deletes itself. Do not add blocking or stack-heavy logic to the timer callback directly.
**Portal fallback threshold.** After `MAX_WIFI_FAILURES` (3) consecutive 10-second timeouts during the boot sequence, `handleReconnection()` opens the WiFiManager captive portal. After boot is complete (`_bootSequenceComplete == true`), it instead keeps retrying indefinitely without opening the portal.
**`forceReconnect()` is blocking.** It calls `delay(1000)` synchronously. Do not call it from a timer callback, ISR, or any task with a tight deadline.
**AP mode and IP addresses.** In `AP_MODE_PERMANENT`, `getLocalIP()` returns `WiFi.softAPIP()` (`192.168.4.1`). In STA mode it returns `WiFi.localIP()`. `getGateway()` always returns `"0.0.0.0"` in AP mode.
**`isHealthy()` signal threshold.** RSSI < -80 dBm is treated as unhealthy. This can cause `HealthMonitor` to flag the subsystem in marginal RF environments even when packets are flowing normally.
**`NetworkConfig` is loaded from SD card.** If the SD card is absent or unreadable at boot, `ConfigManager` falls back to compiled-in defaults (`defaultWifiSsid = "BellSystemsInfra"`). The device will still attempt to connect; it will not stall waiting for SD.