CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc::ui::WifiHandlers Class Reference

#include <WifiHandlers.h>

Public Member Functions

void loadConfig ()
 Loads Wi-Fi configuration from NVS.
void saveConfig ()
 Saves current wizard Wi-Fi configuration to NVS.
WifiConfigconfig ()
WifiWizardwizard ()
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.

Detailed Description

Definition at line 42 of file WifiHandlers.h.

Member Function Documentation

◆ acquire()

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.

Returns
true if WiFi is connected on return.

Definition at line 445 of file WifiHandlers.cpp.

References ensureConnected().

Referenced by host_wifi_request().

◆ clearConfig()

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.

◆ config()

WifiConfig & cdc::ui::WifiHandlers::config ( )
inline

Definition at line 49 of file WifiHandlers.h.

◆ connect()

bool cdc::ui::WifiHandlers::connect ( )

◆ disconnect()

void cdc::ui::WifiHandlers::disconnect ( )

Disconnects and disables Wi-Fi if active.

Returns
void

Definition at line 258 of file WifiHandlers.cpp.

References cdc::hal::getWifiControllerInstance().

◆ ensureConnected()

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.

Returns
true if WiFi is connected on return.

Connection stays open after return. The caller must call disconnect() when done. Triggers an NTP sync that reuses the connection (no tear-down).

Returns
true on a usable connection.

Definition at line 369 of file WifiHandlers.cpp.

References connect(), cdc::hal::getRtcInstance(), cdc::hal::getWifiControllerInstance(), loadConfig(), and syncNtp().

Referenced by acquire().

◆ getConnectTimeoutMs()

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.

Returns
Timeout in ms, clamped to [WIFI_CONNECT_TIMEOUT_MIN_MS, WIFI_CONNECT_TIMEOUT_MAX_MS]; default WIFI_CONNECT_TIMEOUT_DEFAULT_MS if unset.

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.

Referenced by connect(), and syncNtp().

◆ getLastError()

const char * cdc::ui::WifiHandlers::getLastError ( ) const
inline

Definition at line 187 of file WifiHandlers.h.

◆ instance()

◆ isConnected()

bool cdc::ui::WifiHandlers::isConnected ( ) const

Returns whether Wi-Fi is currently connected.

Returns
true if station is 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().

◆ isUserEnabled()

bool cdc::ui::WifiHandlers::isUserEnabled ( ) const
inline

Returns the persisted user/system WiFi intent.

Returns
true if WiFi was last turned on by the user/system.

Definition at line 123 of file WifiHandlers.h.

◆ isValidIpAddress()

bool cdc::ui::WifiHandlers::isValidIpAddress ( const char * ip)
static

Validates dotted IPv4 address string.

Parameters
ipIPv4 address string.
Returns
true if address is syntactically and numerically valid.

Definition at line 50 of file WifiHandlers.cpp.

Referenced by cdc::ui::onWifiGatewayEntered(), cdc::ui::onWifiNetmaskEntered(), and cdc::ui::onWifiStaticIpEntered().

◆ loadConfig()

void cdc::ui::WifiHandlers::loadConfig ( )

Loads Wi-Fi configuration from NVS.

Returns
void

Definition at line 136 of file WifiHandlers.cpp.

Referenced by ensureConnected(), restoreOnBoot(), saveConfig(), setUserEnabled(), and cdc::ui::showWifiMainMenu().

◆ persistUserIntent()

void cdc::ui::WifiHandlers::persistUserIntent ( bool enabled)
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.

Parameters
enabledIntent to persist.

Definition at line 136 of file WifiHandlers.h.

◆ release()

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().

◆ restoreOnBoot()

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().

◆ saveConfig()

void cdc::ui::WifiHandlers::saveConfig ( )

Saves current wizard Wi-Fi configuration to NVS.

Returns
void

Definition at line 175 of file WifiHandlers.cpp.

References loadConfig().

Referenced by saveCredentials(), and cdc::ui::wifiFinishSetup().

◆ saveCredentials()

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.

Parameters
ssidNull-terminated SSID (max 32 chars, truncated if longer).
passwordNull-terminated password (nullptr or "" for open networks).

Definition at line 104 of file WifiHandlers.cpp.

References saveConfig(), and cdc::hal::WPA2_PSK.

◆ setConnectTimeoutMs()

bool cdc::ui::WifiHandlers::setConnectTimeoutMs ( uint32_t ms)

Persists the connect timeout to NVS.

Persists the connect timeout if the value is in range.

Parameters
msTimeout in ms; must lie within [WIFI_CONNECT_TIMEOUT_MIN_MS, WIFI_CONNECT_TIMEOUT_MAX_MS].
Returns
true on success, false if the value is out of range or NVS could not be opened.

Definition at line 89 of file WifiHandlers.cpp.

References cdc::ui::WIFI_CONNECT_TIMEOUT_MAX_MS.

◆ setUserEnabled()

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.

Parameters
enabledtrue to turn WiFi on, false to turn it off.
Returns
When enabling, true if WiFi is connected on return; when disabling, always true.

Definition at line 424 of file WifiHandlers.cpp.

References connect(), isConnected(), and loadConfig().

Referenced by cdc::serial::cmdWifiOff(), and cdc::ui::wifiDisconnect().

◆ syncNtp()

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.

Returns
true if the system time is valid after the call.

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().

◆ wizard()


The documentation for this class was generated from the following files: