Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
WifiManager Class Reference

Glue between the pure prov::WifiPlan and the Arduino WiFi stack. More...

#include <wifi_manager.h>

Classes

struct  Outcome
 Outcome with the lockout time for Result::LockedOut. More...
 
struct  Status
 Snapshot for /api/wifi/status. More...
 

Public Types

enum class  Result { Ok , BadInput , WrongPin , LockedOut }
 Result of add()/forget(); the HTTP layer maps it to 200 / 400 / 403 / 429.
 
using AddOutcome = Outcome
 
using ForgetOutcome = Outcome
 

Public Member Functions

 WifiManager (const WifiManager &)=delete
 
WifiManager & operator= (const WifiManager &)=delete
 
void begin ()
 Loads NVS, brings up the WiFi stack and starts the connection plan. Returns at once.
 
void maintain ()
 Call from loop(): WiFi events, scan polling, plan timers, hotspot DNS and the serial wifi-reset.
 
Status status ()
 Snapshot of mode, SSID, IP, whether a PIN is set and the last failure.
 
const char * mode_name ()
 Short mode name for logs ("sta" | "setup" | "joining" | "lost").
 
bool setup_mode () const
 true while the setup hotspot is running (atomic, no lock).
 
prov::PlanState plan_state ()
 Current plan state (thread-safe; for tests and diagnostics).
 
size_t scan (prov::ScanResult *out, size_t cap)
 Copies the cached scan (strongest entry per SSID, hidden networks skipped).
 
size_t saved (char(*ssids)[prov::kSsidMax+1], size_t cap)
 Copies the saved SSIDs.
 
AddOutcome add (const char *ssid, size_t ssid_len, const char *pass, size_t pass_len, const char *pin, size_t pin_len, uint32_t now_ms)
 Adds (or replaces) a network after checking the PIN; the first add with no PIN set defines the PIN.
 
ForgetOutcome forget (const char *ssid, size_t ssid_len, const char *pin, size_t pin_len, uint32_t now_ms)
 Forgets a saved network after checking the PIN (same PinGuard as add, so lockouts are shared).
 

Static Public Attributes

static constexpr size_t kScanCap = 24
 Capacity of the scan cache (strongest networks beyond it are not guaranteed to be kept).
 

Detailed Description

Glue between the pure prov::WifiPlan and the Arduino WiFi stack.

Driven only from loop() through maintain(), which never waits for a join or scan to complete (scans are async, WiFi events (arduino_events task) only set atomic flags, and maintain() turns them into plan events). All WiFi API calls and plan calls happen in maintain()/begin(), and maintain() holds one FreeRTOS mutex for its whole pass, including WiFi driver calls that take a while (WiFi.mode/softAP when the hotspot starts or stops, WiFi.begin, starting a scan) and NVS writes (save_nets after a join).

status() (behind /api/wifi/status, which the HUD polls) never takes that mutex: maintain(), begin() and add() publish a copy of its fields into a spinlock-guarded snapshot whenever they may have changed, and status() copies that. The rarer, user-initiated accessors (scan(), saved(), add(), forget()) still share the network list, PIN guard and scan cache with maintain() under the mutex, so a call that lands during one of those slow driver calls waits for it, and the single httpd task (every /stream send and /commands reply) waits with it. add() and forget() also hold the mutex across their own NVS writes, which blocks loop() in maintain() for that time.

Member Function Documentation

◆ add()

WifiManager::AddOutcome WifiManager::add ( const char * ssid,
size_t ssid_len,
const char * pass,
size_t pass_len,
const char * pin,
size_t pin_len,
uint32_t now_ms )

Adds (or replaces) a network after checking the PIN; the first add with no PIN set defines the PIN.

Inputs are validated before any PIN attempt is counted. Persists the list (and the new PIN record) and asks maintain() to tell the plan; a full list evicts the oldest entry.

◆ begin()

void WifiManager::begin ( )

Loads NVS, brings up the WiFi stack and starts the connection plan. Returns at once.

Must run before the HTTP server starts (it sets up the TCP/IP stack).

◆ saved()

size_t WifiManager::saved ( char(*) ssids[prov::kSsidMax+1],
size_t cap )

Copies the saved SSIDs.

Returns
Number written (at most cap).

◆ scan()

size_t WifiManager::scan ( prov::ScanResult * out,
size_t cap )

Copies the cached scan (strongest entry per SSID, hidden networks skipped).

Requests a new async scan from maintain() if the cache is older than 10 s, so the first call after a long pause may return an old or empty list; call again after a few seconds.

Returns
Number of entries written to out (at most cap).

◆ status()

WifiManager::Status WifiManager::status ( )

Snapshot of mode, SSID, IP, whether a PIN is set and the last failure.

Never takes the manager mutex (one short spinlock copy), so it cannot wait behind maintain(). The copy is the one published at the end of the last maintain() pass (or by begin()/add()), so it lags the live state by at most one pass. It holds no password or PIN.


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