93 lines
2.9 KiB
C++
93 lines
2.9 KiB
C++
/*
|
|
* WiFi Manager Module
|
|
*
|
|
* Joins one remembered network (for OTA updates) and scans on request. Owns no
|
|
* LVGL objects: the Settings screen polls getStatus()/getNetworks() and calls
|
|
* the request functions, so screen lifetimes can never leave dangling pointers
|
|
* here.
|
|
*
|
|
* Radio rules (the ESP-NOW link to Inst. Cluster shares the radio):
|
|
* - the radio stays in STA mode forever; "off" only stops association/scans.
|
|
* Never WiFi.disconnect(true) / WiFi.mode(WIFI_OFF) - that kills ESP-NOW.
|
|
* - scans only when asked (each one hops every channel);
|
|
* - no endless auto-reconnect: one attempt at boot, one after a drop, then
|
|
* idle on ESPNOW_CHANNEL until the user asks again.
|
|
*
|
|
* The network is remembered in NVS (namespace "wifi") after a successful join.
|
|
* The password is never printed.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
|
|
namespace WiFiManager {
|
|
|
|
#define WIFI_SCAN_MAX_NETWORKS 20
|
|
#define WIFI_CONNECT_TIMEOUT_MS 15000
|
|
#define WIFI_TASK_STACK_SIZE 4096
|
|
#define WIFI_TASK_PRIORITY 1
|
|
#define WIFI_TASK_CORE 0
|
|
|
|
typedef enum {
|
|
LINK_OFF = 0, // Wi-Fi switched off
|
|
LINK_IDLE, // on, not connected, not trying
|
|
LINK_CONNECTING,
|
|
LINK_CONNECTED,
|
|
LINK_FAILED // last attempt failed; see status_t::error
|
|
} link_t;
|
|
|
|
typedef struct {
|
|
link_t link;
|
|
char ssid[33]; // network being joined / joined / that failed
|
|
char ip[16];
|
|
int rssi; // dBm, when connected
|
|
char error[48]; // human-readable reason when LINK_FAILED
|
|
char saved_ssid[33]; // remembered network ("" = none)
|
|
bool scanning;
|
|
uint32_t scan_gen; // bumps each time a scan completes
|
|
} status_t;
|
|
|
|
typedef struct {
|
|
char ssid[33];
|
|
int8_t rssi;
|
|
bool secured;
|
|
} network_t;
|
|
|
|
/** Start the Wi-Fi task. If enabled and a network is saved, joins it. */
|
|
void init(bool enabled);
|
|
|
|
/** Wi-Fi switch: persisted. On joins the saved network; off disconnects. */
|
|
void setEnabled(bool on);
|
|
bool isEnabled();
|
|
|
|
/** Scan once (ignored while off or already scanning). */
|
|
void requestScan();
|
|
|
|
/** Join a network; remembered if it works. pass may be NULL/"" for open networks. */
|
|
void connect(const char* ssid, const char* pass);
|
|
|
|
/** Join the remembered network again. */
|
|
void connectSaved();
|
|
|
|
/** Drop the remembered network and disconnect. */
|
|
void forget();
|
|
|
|
status_t getStatus();
|
|
|
|
/** Copy of the last scan (strongest first, one row per SSID). Returns the count. */
|
|
uint8_t getNetworks(network_t* out, uint8_t max);
|
|
|
|
/** Console: `wifi status | join <ssid> [pass] | forget | on | off`. */
|
|
bool handleCommand(const char* line);
|
|
|
|
/**
|
|
* The remembered network, password included - for LinkPeer, which hands it to
|
|
* Inst. Cluster inside an encrypted frame. Never print the password.
|
|
* @return false if no network is saved
|
|
*/
|
|
bool getSavedCredentials(char* ssid, size_t ssid_n, char* pass, size_t pass_n);
|
|
|
|
} // namespace WiFiManager
|