Supports 2.8/3.5 boards with on-screen station list; ships flashable firmware packages. Co-authored-by: Cursor <cursoragent@cursor.com>
93 lines
2.6 KiB
C
93 lines
2.6 KiB
C
#pragma once
|
||
|
||
/**
|
||
* 通用 WiFi 配网核心(无 LVGL 依赖)
|
||
*
|
||
* 流程:
|
||
* 1. wifi_prov_begin() 读 NVS,有凭证则尝试 STA 连接
|
||
* 2. 失败 / 无凭证 → 业务层开屏上 UI 或 SoftAP 门户
|
||
* 3. wifi_prov_save() + wifi_prov_connect_to() 完成配网
|
||
* 4. loop 里调用 wifi_prov_loop() 做断线重连与门户处理
|
||
*/
|
||
|
||
#include <Arduino.h>
|
||
#include <WiFi.h>
|
||
|
||
#ifndef WIFI_PROV_AP_SSID
|
||
#define WIFI_PROV_AP_SSID "CYD-Setup"
|
||
#endif
|
||
|
||
#ifndef WIFI_PROV_CONNECT_TIMEOUT_MS
|
||
#define WIFI_PROV_CONNECT_TIMEOUT_MS 30000u
|
||
#endif
|
||
|
||
/* Ignore transient WL_NO_SSID_AVAIL / CONNECT_FAILED right after WiFi.begin(). */
|
||
#ifndef WIFI_PROV_FAIL_GRACE_MS
|
||
#define WIFI_PROV_FAIL_GRACE_MS 8000u
|
||
#endif
|
||
|
||
#ifndef WIFI_PROV_RECONNECT_MS
|
||
#define WIFI_PROV_RECONNECT_MS 8000u
|
||
#endif
|
||
|
||
static constexpr int WIFI_PROV_MAX_SCAN = 16;
|
||
static constexpr size_t WIFI_PROV_SSID_MAX = 32;
|
||
static constexpr size_t WIFI_PROV_PASS_MAX = 64;
|
||
|
||
struct WifiScanItem {
|
||
char ssid[WIFI_PROV_SSID_MAX + 1];
|
||
int32_t rssi;
|
||
wifi_auth_mode_t auth;
|
||
};
|
||
|
||
enum WifiProvState : uint8_t {
|
||
WIFI_PROV_IDLE = 0,
|
||
WIFI_PROV_CONNECTING,
|
||
WIFI_PROV_CONNECTED,
|
||
WIFI_PROV_FAILED,
|
||
WIFI_PROV_PORTAL,
|
||
};
|
||
|
||
using WifiProvStateCb = void (*)(WifiProvState state, void *user);
|
||
|
||
void wifi_prov_set_state_cb(WifiProvStateCb cb, void *user = nullptr);
|
||
|
||
/** 初始化 Preferences + WiFi STA;有凭证则异步开始连接。返回是否已有凭证。 */
|
||
bool wifi_prov_begin();
|
||
|
||
bool wifi_prov_has_credentials();
|
||
bool wifi_prov_is_connected();
|
||
WifiProvState wifi_prov_state();
|
||
|
||
String wifi_prov_ssid();
|
||
String wifi_prov_ip();
|
||
int8_t wifi_prov_rssi();
|
||
|
||
void wifi_prov_save(const char *ssid, const char *password);
|
||
void wifi_prov_clear();
|
||
|
||
/** 用已存凭证连接(异步,结果看 wifi_prov_loop / state) */
|
||
void wifi_prov_connect_saved();
|
||
|
||
/** 连接指定网络并在成功后写入 NVS */
|
||
void wifi_prov_connect_to(const char *ssid, const char *password);
|
||
|
||
/** 同步扫描(会阻塞数秒;门户 HTML 可用)。屏上 UI 请用异步接口。 */
|
||
int wifi_prov_scan(WifiScanItem *out, int max_items);
|
||
|
||
/** 异步扫描:start 后每 loop 调 poll;完成时写入 out 并返回条数,进行中返回 -1。 */
|
||
bool wifi_prov_scan_start();
|
||
bool wifi_prov_scan_busy();
|
||
int wifi_prov_scan_poll(WifiScanItem *out, int max_items);
|
||
|
||
/**
|
||
* SoftAP + 简易 Captive Portal(手机连 CYD-Setup → http://192.168.4.1)
|
||
* 与屏上配网可并行;配网成功后会自动 stop。
|
||
*/
|
||
void wifi_prov_portal_start(const char *ap_ssid = WIFI_PROV_AP_SSID);
|
||
void wifi_prov_portal_stop();
|
||
bool wifi_prov_portal_active();
|
||
|
||
/** 每 loop 调用:重连、门户 HTTP、状态回调 */
|
||
void wifi_prov_loop();
|