|
CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
|
#include <WifiHandlers.h>
Public Member Functions | |
| void | loadConfig () |
| Loads Wi-Fi configuration from NVS. | |
| void | saveConfig () |
| Saves current wizard Wi-Fi configuration to NVS. | |
| WifiConfig & | config () |
| WifiWizard & | wizard () |
| void | saveCredentials (const char *ssid, const char *password) |
| Stores credentials directly (WPA2-PSK, DHCP) and persists them. | |
| void | clearConfig () |
| Erases the entire "wifi" NVS namespace and invalidates the cached configuration. | |
| uint32_t | getConnectTimeoutMs () const |
| Reads the persisted connect timeout (NVS key "tout"). | |
| bool | setConnectTimeoutMs (uint32_t ms) |
| Persists the connect timeout to NVS. | |
| bool | connect () |
| Connects to Wi-Fi using saved configuration. | |
| void | disconnect () |
| Disconnects and disables Wi-Fi if active. | |
| bool | isConnected () const |
| Returns whether Wi-Fi is currently connected. | |
| bool | ensureConnected () |
| Ensures the device is connected to WiFi and optionally syncs time, leaving the connection up for the caller to use. | |
| bool | setUserEnabled (bool enabled) |
| Sets the user/system WiFi intent and applies it immediately. | |
| bool | isUserEnabled () const |
| Returns the persisted user/system WiFi intent. | |
| void | persistUserIntent (bool enabled) |
| Persists the user/system WiFi intent without bringing the radio up or down. | |
| bool | acquire () |
| Acquires a hold on the WiFi connection for a plugin/host caller. | |
| void | release () |
| Releases a previously acquired WiFi hold. | |
| void | restoreOnBoot () |
| Restores the persisted WiFi intent at boot. | |
| bool | syncNtp (bool disconnectAfter=true) |
| Synchronizes system time via NTP. | |
| const char * | getLastError () const |
Static Public Member Functions | |
| static WifiHandlers & | instance () |
| Returns singleton Wi-Fi handlers instance. | |
| static bool | isValidIpAddress (const char *ip) |
| Validates dotted IPv4 address string. | |
Definition at line 42 of file WifiHandlers.h.
| bool cdc::ui::WifiHandlers::acquire | ( | ) |
Acquires a hold on the WiFi connection for a plugin/host caller.
Acquires a plugin/host hold and ensures WiFi is connected.
Increments the holder count and ensures WiFi is connected. While at least one holder is active, release and setUserEnabled(false) will not tear the connection down. Pairs with release.
Definition at line 445 of file WifiHandlers.cpp.
References ensureConnected().
Referenced by host_wifi_request().
| void cdc::ui::WifiHandlers::clearConfig | ( | ) |
Erases the entire "wifi" NVS namespace and invalidates the cached configuration.
Erases the WiFi NVS namespace and invalidates the cached config.
Definition at line 120 of file WifiHandlers.cpp.
|
inline |
Definition at line 49 of file WifiHandlers.h.
| bool cdc::ui::WifiHandlers::connect | ( | ) |
Connects to Wi-Fi using saved configuration.
Definition at line 211 of file WifiHandlers.cpp.
References cdc::hal::CONNECTION_FAILED, cdc::hal::DISCONNECTED, getConnectTimeoutMs(), cdc::hal::getWifiControllerInstance(), cdc::ui::ViewStack::hideModal(), cdc::ui::ViewStack::instance(), cdc::ui::ViewStack::render(), cdc::ui::showToastTask(), cdc::hal::STA, and cdc::ui::tr().
Referenced by ensureConnected(), restoreOnBoot(), and setUserEnabled().
| void cdc::ui::WifiHandlers::disconnect | ( | ) |
Disconnects and disables Wi-Fi if active.
Definition at line 258 of file WifiHandlers.cpp.
References cdc::hal::getWifiControllerInstance().
| bool cdc::ui::WifiHandlers::ensureConnected | ( | ) |
Ensures the device is connected to WiFi and optionally syncs time, leaving the connection up for the caller to use.
Ensures WiFi is connected; runs NTP sync only when the time is unset.
Idempotent: if already connected, returns true after triggering an NTP sync only when the system clock has not been set yet.
The caller owns the lifetime of the connection and must call disconnect() when done. There is no auto-shutdown.
Connection stays open after return. The caller must call disconnect() when done. Triggers an NTP sync that reuses the connection (no tear-down).
Definition at line 369 of file WifiHandlers.cpp.
References connect(), cdc::hal::getRtcInstance(), cdc::hal::getWifiControllerInstance(), loadConfig(), and syncNtp().
Referenced by acquire().
| uint32_t cdc::ui::WifiHandlers::getConnectTimeoutMs | ( | ) | const |
Reads the persisted connect timeout (NVS key "tout").
Returns the persisted connect timeout, clamped to the valid range.
Definition at line 73 of file WifiHandlers.cpp.
References cdc::ui::WIFI_CONNECT_TIMEOUT_DEFAULT_MS, cdc::ui::WIFI_CONNECT_TIMEOUT_MAX_MS, and cdc::ui::WIFI_CONNECT_TIMEOUT_MIN_MS.
|
inline |
Definition at line 187 of file WifiHandlers.h.
|
static |
Returns singleton Wi-Fi handlers instance.
Definition at line 31 of file WifiHandlers.cpp.
Referenced by app_main(), cdc::serial::cmdWifiConnect(), cdc::serial::cmdWifiForget(), cdc::serial::cmdWifiOff(), cdc::serial::cmdWifiOn(), cdc::serial::cmdWifiStatus(), cdc::serial::cmdWifiTimeout(), cdc::os_ui::SystemSettingsBackup::exportSystemSettings(), host_wifi_is_connected(), host_wifi_release(), host_wifi_request(), cdc::os_ui::SystemSettingsBackup::importSystemSettings(), cdc::ui::LockScreenView::onKey(), cdc::ui::onWifiAuthSelect(), cdc::ui::onWifiGatewayEntered(), cdc::ui::onWifiIpModeSelect(), cdc::ui::onWifiNetmaskEntered(), cdc::ui::onWifiPasswordEntered(), cdc::ui::onWifiScanSelect(), cdc::ui::onWifiStaticIpEntered(), cdc::ui::onWifiToggleCallback(), cdc::ui::rebuildWifiMainMenu(), cdc::ui::showWifiMainMenu(), cdc::ui::wifiConnect(), cdc::ui::wifiDisconnect(), cdc::ui::wifiFinishSetup(), cdc::ui::wifiNtpSync(), cdc::ui::wifiSetup(), and cdc::ui::wifiShowDetails().
| bool cdc::ui::WifiHandlers::isConnected | ( | ) | const |
Returns whether Wi-Fi is currently connected.
Definition at line 202 of file WifiHandlers.cpp.
References cdc::hal::getWifiControllerInstance().
Referenced by host_wifi_is_connected(), cdc::ui::LockScreenView::onKey(), and setUserEnabled().
|
inline |
Returns the persisted user/system WiFi intent.
Definition at line 123 of file WifiHandlers.h.
|
static |
Validates dotted IPv4 address string.
| ip | IPv4 address string. |
Definition at line 50 of file WifiHandlers.cpp.
Referenced by cdc::ui::onWifiGatewayEntered(), cdc::ui::onWifiNetmaskEntered(), and cdc::ui::onWifiStaticIpEntered().
| void cdc::ui::WifiHandlers::loadConfig | ( | ) |
Loads Wi-Fi configuration from NVS.
Definition at line 136 of file WifiHandlers.cpp.
Referenced by ensureConnected(), restoreOnBoot(), saveConfig(), setUserEnabled(), and cdc::ui::showWifiMainMenu().
|
inline |
Persists the user/system WiFi intent without bringing the radio up or down.
Unlike setUserEnabled, this only stores the intent flag and updates the cached value; the persisted state takes effect at the next restoreOnBoot. Used during settings restore to avoid a blocking connect.
| enabled | Intent to persist. |
Definition at line 136 of file WifiHandlers.h.
| void cdc::ui::WifiHandlers::release | ( | ) |
Releases a previously acquired WiFi hold.
Releases a plugin/host hold and tears WiFi down if appropriate.
Decrements the holder count and tears the connection down only when no holder remains and the user intent is off. Safe to call when the count is already zero.
Definition at line 455 of file WifiHandlers.cpp.
Referenced by host_wifi_release().
| void cdc::ui::WifiHandlers::restoreOnBoot | ( | ) |
Restores the persisted WiFi intent at boot.
Reconnects at boot if WiFi intent was persisted as enabled.
Reloads the configuration and, if the user had WiFi enabled before the last reboot and a valid configuration exists, reconnects.
Definition at line 463 of file WifiHandlers.cpp.
References connect(), and loadConfig().
Referenced by app_main().
| void cdc::ui::WifiHandlers::saveConfig | ( | ) |
Saves current wizard Wi-Fi configuration to NVS.
Definition at line 175 of file WifiHandlers.cpp.
References loadConfig().
Referenced by saveCredentials(), and cdc::ui::wifiFinishSetup().
| void cdc::ui::WifiHandlers::saveCredentials | ( | const char * | ssid, |
| const char * | password ) |
Stores credentials directly (WPA2-PSK, DHCP) and persists them.
Stores credentials (WPA2/DHCP defaults) and persists them.
Resets the wizard, fills SSID/password, marks DHCP+WPA2, then writes to NVS via saveConfig() and reloads the cached config.
| ssid | Null-terminated SSID (max 32 chars, truncated if longer). |
| password | Null-terminated password (nullptr or "" for open networks). |
Definition at line 104 of file WifiHandlers.cpp.
References saveConfig(), and cdc::hal::WPA2_PSK.
| bool cdc::ui::WifiHandlers::setConnectTimeoutMs | ( | uint32_t | ms | ) |
Persists the connect timeout to NVS.
Persists the connect timeout if the value is in range.
| ms | Timeout in ms; must lie within [WIFI_CONNECT_TIMEOUT_MIN_MS, WIFI_CONNECT_TIMEOUT_MAX_MS]. |
Definition at line 89 of file WifiHandlers.cpp.
References cdc::ui::WIFI_CONNECT_TIMEOUT_MAX_MS.
| bool cdc::ui::WifiHandlers::setUserEnabled | ( | bool | enabled | ) |
Sets the user/system WiFi intent and applies it immediately.
Applies and persists the user/system WiFi intent.
Persists the intent flag (NVS key "ena") and either brings WiFi up (using the saved configuration) or tears it down. Tear-down is deferred while plugin holders are still active (see acquire); the radio only goes down once no holder remains and the user intent is off.
| enabled | true to turn WiFi on, false to turn it off. |
Definition at line 424 of file WifiHandlers.cpp.
References connect(), isConnected(), and loadConfig().
Referenced by cdc::serial::cmdWifiOff(), and cdc::ui::wifiDisconnect().
| bool cdc::ui::WifiHandlers::syncNtp | ( | bool | disconnectAfter = true | ) |
Synchronizes system time via NTP.
When disconnectAfter is true (default), the function reproduces the legacy "connect, sync, disconnect" pattern used for one-shot time fetches. When false, the caller is expected to manage the WiFi lifetime; the function will reuse an active connection or open one without tearing it down on return.
If the RTC reports an already-valid time, the function returns true without contacting any NTP server.
Always contacts an NTP server; callers that only want a sync when the time is currently unset should check IRtc::isTimeSet() themselves before calling. The disconnectAfter flag controls whether a connection opened by this function is also torn down on return; an existing connection (opened by the caller) is never disconnected here.
Definition at line 286 of file WifiHandlers.cpp.
References getConnectTimeoutMs(), cdc::hal::getRtcInstance(), cdc::hal::getWifiControllerInstance(), cdc::ui::NTP_SYNC_TIMEOUT_MS, and cdc::hal::STA.
Referenced by ensureConnected().
|
inline |
Definition at line 50 of file WifiHandlers.h.
Referenced by cdc::ui::onWifiAuthSelect(), cdc::ui::onWifiGatewayEntered(), cdc::ui::onWifiIpModeSelect(), cdc::ui::onWifiNetmaskEntered(), cdc::ui::onWifiPasswordEntered(), cdc::ui::onWifiScanSelect(), cdc::ui::onWifiStaticIpEntered(), and cdc::ui::wifiSetup().