CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
WifiHandlers.h
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5
6namespace cdc::ui {
7
8// WiFi timeout constants (milliseconds)
9static constexpr uint32_t WIFI_CONNECT_TIMEOUT_DEFAULT_MS = 15000;
10static constexpr uint32_t WIFI_CONNECT_TIMEOUT_MIN_MS = 3000;
11static constexpr uint32_t WIFI_CONNECT_TIMEOUT_MAX_MS = 60000;
12static constexpr uint32_t WIFI_SCAN_TIMEOUT_MS = 10000;
13static constexpr uint32_t NTP_SYNC_TIMEOUT_MS = 10000;
14
15// WiFi Setup Wizard State
16struct WifiWizard {
17 char ssid[33] = {};
18 char password[65] = {};
20 bool useDhcp = true;
21 char staticIp[16] = {};
22 char gateway[16] = {};
23 char netmask[16] = "255.255.255.0";
24 bool fromScan = false;
25
26 void reset();
27};
28
29// WiFi Stored Configuration
30struct WifiConfig {
31 char ssid[33] = {};
32 char password[65] = {};
33 uint8_t security = 0;
34 bool useDhcp = true;
35 uint32_t staticIp = 0;
36 uint32_t gateway = 0;
37 uint32_t netmask = 0;
38 bool valid = false;
39};
40
41// WiFi management handler class
42class WifiHandlers {
43public:
44 static WifiHandlers& instance();
45
46 // Config persistence
47 void loadConfig();
48 void saveConfig();
49 WifiConfig& config() { return config_; }
50 WifiWizard& wizard() { return wizard_; }
51
61 void saveCredentials(const char* ssid, const char* password);
62
67 void clearConfig();
68
75 uint32_t getConnectTimeoutMs() const;
76
84 bool setConnectTimeoutMs(uint32_t ms);
85
86 // Connection management
87 bool connect();
88 void disconnect();
89 bool isConnected() const;
90
103 bool ensureConnected();
104
117 bool setUserEnabled(bool enabled);
118
123 bool isUserEnabled() const { return userEnabled_; }
124
136 void persistUserIntent(bool enabled) {
137 userEnabled_ = enabled;
138 persistUserEnabled(enabled);
139 }
140
150 bool acquire();
151
159 void release();
160
167 void restoreOnBoot();
168
181 bool syncNtp(bool disconnectAfter = true);
182
183 // Helper: IP validation
184 static bool isValidIpAddress(const char* ip);
185
186 // Get connection error message
187 const char* getLastError() const { return lastError_; }
188
189private:
190 WifiHandlers() = default;
191
192 WifiConfig config_;
193 WifiWizard wizard_;
194 const char* lastError_ = nullptr;
195
196 bool userEnabled_ = false;
197 int holdCount_ = 0;
198
199 static bool isValidIpOctet(int val);
200 uint32_t parseIpAddress(const char* ip) const;
201
202 void persistUserEnabled(bool enabled);
203 void maybeRelease();
204};
205
206} // namespace cdc::ui
bool setUserEnabled(bool enabled)
Sets the user/system WiFi intent and applies it immediately.
bool connect()
Connects to Wi-Fi using saved configuration.
static bool isValidIpAddress(const char *ip)
Validates dotted IPv4 address string.
bool syncNtp(bool disconnectAfter=true)
Synchronizes system time via NTP.
void saveCredentials(const char *ssid, const char *password)
Stores credentials directly (WPA2-PSK, DHCP) and persists them.
bool ensureConnected()
Ensures the device is connected to WiFi and optionally syncs time, leaving the connection up for the ...
void persistUserIntent(bool enabled)
Persists the user/system WiFi intent without bringing the radio up or down.
WifiWizard & wizard()
bool setConnectTimeoutMs(uint32_t ms)
Persists the connect timeout to NVS.
void release()
Releases a previously acquired WiFi hold.
WifiConfig & config()
const char * getLastError() const
void saveConfig()
Saves current wizard Wi-Fi configuration to NVS.
bool acquire()
Acquires a hold on the WiFi connection for a plugin/host caller.
void restoreOnBoot()
Restores the persisted WiFi intent at boot.
static WifiHandlers & instance()
Returns singleton Wi-Fi handlers instance.
uint32_t getConnectTimeoutMs() const
Reads the persisted connect timeout (NVS key "tout").
bool isUserEnabled() const
Returns the persisted user/system WiFi intent.
void loadConfig()
Loads Wi-Fi configuration from NVS.
void disconnect()
Disconnects and disables Wi-Fi if active.
void clearConfig()
Erases the entire "wifi" NVS namespace and invalidates the cached configuration.
bool isConnected() const
Returns whether Wi-Fi is currently connected.
Centralized key-code constants for cdc_views.
Definition IModule.h:8
static constexpr uint32_t WIFI_CONNECT_TIMEOUT_DEFAULT_MS
Definition WifiHandlers.h:9
static constexpr uint32_t WIFI_SCAN_TIMEOUT_MS
static constexpr uint32_t WIFI_CONNECT_TIMEOUT_MIN_MS
static constexpr uint32_t NTP_SYNC_TIMEOUT_MS
static constexpr uint32_t WIFI_CONNECT_TIMEOUT_MAX_MS
hal::WifiSecurity security
void reset()
Resets Wi-Fi wizard state to defaults.