纳入固件源码、屏幕组件、nand_xfer 工具和文档。忽略本机构建产物、sdkconfig,以及含本机路径的依赖锁。 Co-authored-by: Cursor <[email protected]>
400 lines
15 KiB
C
400 lines
15 KiB
C
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_log.h"
|
|
#include "esp_event.h"
|
|
#include "nvs_flash.h"
|
|
#include "config.h"
|
|
#include "event.h"
|
|
#include "cjson.h"
|
|
#include "BLE/ble.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
|
|
#include "wifi.h"
|
|
#include "BLE/ble_send.h"
|
|
#include "LVGL/lvgl_init.h"
|
|
#include "OTA/ota_upgrade.h"
|
|
#include "wifi_prov_ui.h"
|
|
|
|
static bool wifi_initialized = false;
|
|
static bool netif_created = false;
|
|
static bool netif_inited = false;
|
|
|
|
static const char *WIFI_TAG = "wifi_station";
|
|
static int s_retry_num = 0;
|
|
/* 仅 BLE 下发配网指令触发:失败时执行“释放 WiFi + 恢复 UI”,成功不恢复(等待后续 OTA) */
|
|
static volatile bool s_ble_wifi_prov_session = false;
|
|
/** wifi_network_shutdown_full() 主动 stop/deinit 时置位,避免 STA_DISCONNECTED 误走重连 */
|
|
static volatile bool s_wifi_intentional_shutdown = false;
|
|
|
|
extern bool g_wifi_state;
|
|
// 定义WiFi配置变量
|
|
static char ssid[32]; // 存放WIFI 名称
|
|
static char password[64]; // 存放WIFI 密码
|
|
// 事件组和位定义
|
|
static EventGroupHandle_t s_wifi_event_group; // 定义事件组
|
|
static volatile bool s_wifi_connected_now = false;
|
|
|
|
static const char* wifi_disconnect_reason_str(int reason);
|
|
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
|
|
|
// 先新增:原因码转文字的辅助函数(放在事件回调外,比如文件开头)
|
|
static const char* wifi_disconnect_reason_str(int reason) {
|
|
switch (reason) {
|
|
case WIFI_REASON_AUTH_EXPIRE: return "认证超时(心跳包丢失/共存干扰)";
|
|
case WIFI_REASON_AUTH_FAIL: return "认证失败(密码错误/加密模式不匹配)";
|
|
case WIFI_REASON_ASSOC_FAIL: return "关联失败(路由器拒绝/MAC过滤/连接数满)";
|
|
case WIFI_REASON_BEACON_TIMEOUT: return "Beacon超时(路由器离线/信号弱/共存干扰)";
|
|
case WIFI_REASON_HANDSHAKE_TIMEOUT: return "握手超时(射频干扰/距离过远)";
|
|
case WIFI_REASON_ROAMING: return "路由器信道切换(漫游)";
|
|
case WIFI_REASON_UNSPECIFIED: return "未知原因";
|
|
default: return "未定义原因码";
|
|
}
|
|
}
|
|
// 修改 event_handler 函数
|
|
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
|
{
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) // 事件ID
|
|
{
|
|
// 收到 STA 启动信号,开始连接
|
|
esp_wifi_connect(); // 开始连接WIFI
|
|
}
|
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) // WIFI 事件 == 6
|
|
{
|
|
if (s_wifi_intentional_shutdown)
|
|
{
|
|
/* esp_wifi_stop/deinit 会派发本事件,并非链路异常,禁止 esp_wifi_connect 重连 */
|
|
s_wifi_connected_now = false;
|
|
ESP_LOGI(WIFI_TAG, "STA_DISCONNECTED:主动关闭 WiFi 过程中,跳过重连与断开上报");
|
|
return;
|
|
}
|
|
s_wifi_connected_now = false;
|
|
g_wifi_state = false;
|
|
set_system_event(WIFI_DISCONNECTED_BIT); // 任何断开都立即上报,供上层做3秒恢复判断
|
|
/********************* 新增:打印失败原因 *********************/
|
|
// 1. 提取断开事件的详细信息(核心)
|
|
wifi_event_sta_disconnected_t* disconnect_info = (wifi_event_sta_disconnected_t*)event_data;
|
|
// 2. 打印原因码 + 文字解释(一目了然)
|
|
ESP_LOGE(WIFI_TAG, "===== WiFi断开/连接失败 ======");
|
|
ESP_LOGE(WIFI_TAG, "失败原因码:%d", disconnect_info->reason);
|
|
ESP_LOGE(WIFI_TAG, "失败原因:%s", wifi_disconnect_reason_str(disconnect_info->reason));
|
|
ESP_LOGE(WIFI_TAG, "==============================");
|
|
/************************************************************/
|
|
// 断开后尝试重连
|
|
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
|
|
{
|
|
esp_wifi_connect(); // 检测到断开 开始重连
|
|
s_retry_num++;
|
|
ESP_LOGI(WIFI_TAG, "WiFi连接失败,正在尝试第 %d 次重连...", s_retry_num);
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "重试多次无效,连接失败");
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_EVENT_FAIL_BIT); // WIFI 连接失败
|
|
if (ble_is_connected())
|
|
{
|
|
ble_send_wifi_state_request(0); // 通过 BLE 反馈:WiFi 断开/未连上
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGW(WIFI_TAG, "BLE 已断开,跳过 WiFi 断开状态上报");
|
|
}
|
|
if (ota_upgrade_is_running())
|
|
{
|
|
ESP_LOGW(WIFI_TAG, "OTA 过程中 WiFi 重连失败,请求恢复 OTA 前状态");
|
|
set_system_event(OTA_RECOVER_REQUIRED_BIT);
|
|
}
|
|
|
|
/* 仅 BLE 配网会话:最终失败后释放 WiFi 并恢复 UI */
|
|
if (s_ble_wifi_prov_session)
|
|
{
|
|
s_ble_wifi_prov_session = false;
|
|
ESP_LOGW(WIFI_TAG, "BLE 配网最终失败:停止并释放 WiFi,准备恢复 UI");
|
|
(void)esp_wifi_stop();
|
|
(void)esp_wifi_deinit();
|
|
s_retry_num = 0;
|
|
set_system_event(WIFI_BLE_PROV_FAIL_RECOVER_UI_BIT);
|
|
}
|
|
|
|
wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
|
|
ESP_LOGE(WIFI_TAG, "Disconnect reason: %d", event->reason);
|
|
}
|
|
}
|
|
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) // 获取IP 事件 IP_EVENT_STA_GOT_IP
|
|
{
|
|
s_wifi_connected_now = true;
|
|
/* BLE 配网成功:结束会话,但不恢复 UI(通常紧跟 OTA) */
|
|
if (s_ble_wifi_prov_session)
|
|
{
|
|
s_ble_wifi_prov_session = false;
|
|
}
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
ESP_LOGI(WIFI_TAG, "获取到IP: " IPSTR, IP2STR(&event->ip_info.ip)); // 显示IP 地址
|
|
|
|
// ============================================================
|
|
// === 新增:强制设置 DNS 服务器 (解决 202 域名解析错误) ===
|
|
// ============================================================
|
|
esp_netif_t *netif = event->esp_netif;
|
|
esp_netif_dns_info_t dns_info;
|
|
|
|
// 1. 设置主 DNS 为阿里 DNS (223.5.5.5) - 解析国内域名最快
|
|
IP4_ADDR(&dns_info.ip.u_addr.ip4, 223, 5, 5, 5);
|
|
dns_info.ip.type = IPADDR_TYPE_V4;
|
|
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
|
|
|
// 2. 设置备用 DNS 为谷歌 DNS (8.8.8.8) - 防止阿里挂了
|
|
IP4_ADDR(&dns_info.ip.u_addr.ip4, 8, 8, 8, 8);
|
|
dns_info.ip.type = IPADDR_TYPE_V4;
|
|
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_BACKUP, &dns_info);
|
|
|
|
ESP_LOGI(WIFI_TAG, "已强制设置DNS: Main=223.5.5.5, Backup=8.8.8.8");
|
|
// ============================================================
|
|
|
|
s_retry_num = 0;
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_EVENT_CONNECTED_BIT); // 事件组
|
|
set_system_event(WIFI_CONNECTED_BIT);
|
|
/* 配网成功,主动通过 BLE 1.10_A 反馈:WiFi 已连接 (0x01) */
|
|
ESP_LOGI(WIFI_TAG, "配网成功,主动通过 BLE 1.10_A 反馈 WiFi 已连接");
|
|
ble_send_wifi_state_request(1);
|
|
g_wifi_state = true;
|
|
}
|
|
}
|
|
|
|
// 初始化 WiFi
|
|
static esp_netif_t *get_existing_wifi_netif(void) // wifi 网络接口管理函数
|
|
{
|
|
esp_netif_t *netif = NULL;
|
|
|
|
// 遍历系统当前所有的网络接口
|
|
while ((netif = esp_netif_next(netif)) != NULL) // 遍历所有网络接口
|
|
{
|
|
const char *desc = esp_netif_get_desc(netif);
|
|
const char *key = esp_netif_get_ifkey(netif);
|
|
|
|
ESP_LOGI(WIFI_TAG, "发现现有接口 - Key: %s, Desc: %s",
|
|
key ? key : "NULL",
|
|
desc ? desc : "NULL");
|
|
|
|
// 检查这个接口是否是 WiFi STA
|
|
// 通常 Key 是 "WIFI_STA_DEF" 或者 Desc 包含 "sta"
|
|
if ((key && strcmp(key, "WIFI_STA_DEF") == 0) || (desc && strstr(desc, "sta") != NULL))
|
|
{
|
|
return netif;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
WIFI_STATE wifi_init_sta(void)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "===== 开始WiFi初始化 =====");
|
|
|
|
// 1. 初始化 TCP/IP 堆栈
|
|
esp_err_t err = esp_netif_init();
|
|
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE)
|
|
{
|
|
ESP_LOGE(WIFI_TAG, "esp_netif_init 失败: %d", err);
|
|
return wifi_disconnected;
|
|
}
|
|
|
|
// 2. 【关键修复】创建默认事件循环
|
|
// 如果没有这个,创建 netif 会直接崩溃或报错
|
|
err = esp_event_loop_create_default();
|
|
if (err == ESP_OK)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "默认事件循环创建成功");
|
|
}
|
|
else if (err == ESP_ERR_INVALID_STATE)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "默认事件循环已存在,跳过");
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGE(WIFI_TAG, "事件循环创建失败: %d", err);
|
|
return wifi_disconnected;
|
|
}
|
|
|
|
// 3. 创建事件组
|
|
if (s_wifi_event_group == NULL)
|
|
{
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
}
|
|
|
|
// 4. 【强力修复】获取或创建 Network Interface
|
|
// 不再只依赖 key 查找,而是遍历查找
|
|
esp_netif_t *netif = get_existing_wifi_netif();
|
|
|
|
if (netif != NULL)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "检测到已存在 WiFi STA 接口,直接复用");
|
|
}
|
|
else
|
|
{
|
|
// 只有真的找不到时,才创建
|
|
ESP_LOGI(WIFI_TAG, "未找到现有接口,正在创建默认 WiFi STA...");
|
|
netif = esp_netif_create_default_wifi_sta();
|
|
|
|
if (netif == NULL)
|
|
{
|
|
ESP_LOGE(WIFI_TAG, "致命错误:创建 WiFi 接口失败,可能驱动已异常");
|
|
// 这里返回,防止后续操作导致崩溃
|
|
return wifi_disconnected;
|
|
}
|
|
}
|
|
|
|
// 5. 初始化 WiFi 驱动
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
esp_err_t ret = esp_wifi_init(&cfg);
|
|
|
|
if (ret == ESP_OK)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "WiFi 驱动初始化完成");
|
|
wifi_initialized = true;
|
|
}
|
|
else if (ret == ESP_ERR_INVALID_STATE)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "WiFi 驱动此前已初始化,继续配置");
|
|
wifi_initialized = true;
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGE(WIFI_TAG, "WiFi 初始化失败: %s", esp_err_to_name(ret));
|
|
return wifi_disconnected;
|
|
}
|
|
|
|
// 6. 注册事件回调 (防止重复注册)
|
|
static bool handlers_registered = false;
|
|
if (!handlers_registered)
|
|
{
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
|
ESP_EVENT_ANY_ID,
|
|
&event_handler,
|
|
NULL,
|
|
NULL));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
|
IP_EVENT_STA_GOT_IP,
|
|
&event_handler,
|
|
NULL,
|
|
NULL));
|
|
handlers_registered = true;
|
|
}
|
|
|
|
// 7. 配置 WiFi 参数
|
|
wifi_config_t wifi_config = {0};
|
|
memset(&wifi_config, 0, sizeof(wifi_config)); // 清零很重要
|
|
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid) - 1);
|
|
strncpy((char *)wifi_config.sta.password, password, sizeof(wifi_config.sta.password) - 1);
|
|
|
|
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
|
|
|
wifi_config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
|
wifi_config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
|
|
|
ESP_LOGI(WIFI_TAG, "正在设置 WiFi 模式为 STA...");
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_LOGI(WIFI_TAG, "正在应用 WiFi 配置...");
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
|
|
s_retry_num = 0;
|
|
|
|
// 8. 启动与连接
|
|
ESP_LOGI(WIFI_TAG, "启动 WiFi...");
|
|
ret = esp_wifi_start();
|
|
|
|
esp_wifi_set_ps(WIFI_PS_NONE);
|
|
|
|
if (ret == ESP_OK)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "启动成功,Event Handler 将自动发起连接");
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGW(WIFI_TAG, "WiFi 可能已在运行,手动发起连接...");
|
|
esp_wifi_connect();
|
|
}
|
|
|
|
/* 非阻塞:不在此等待连接结果,避免阻塞 BLE 任务导致无法处理 OTA 等后续指令。
|
|
* 连接成功/失败由 event_handler 在 IP_EVENT_STA_GOT_IP 或 WIFI_EVENT_STA_DISCONNECTED 中
|
|
* 通过 ble_send_wifi_state_request(1/0) 反馈给车机。 */
|
|
ESP_LOGI(WIFI_TAG, "已发起连接,结果将通过 BLE 1.10_A 反馈");
|
|
return wifi_connected; /* 仅表示已发起连接,实际结果由事件回调上报 */
|
|
}
|
|
// 主函数
|
|
void wifi_main(char *ss, char *pass)
|
|
{
|
|
ESP_LOGI(WIFI_TAG, "===== 开始WiFi配置处理 =====");
|
|
|
|
// 检查输入参数
|
|
if (ss == NULL || pass == NULL)
|
|
{
|
|
ESP_LOGE(WIFI_TAG, "WiFi配置参数无效");
|
|
return;
|
|
}
|
|
|
|
// 复制WiFi配置
|
|
strncpy((char *)ssid, ss, sizeof(ssid) - 1);
|
|
strncpy((char *)password, pass, sizeof(password) - 1);
|
|
|
|
// 输出WiFi配置(注意:实际生产环境中不应输出密码)
|
|
ESP_LOGI(WIFI_TAG, "接收到WiFi配置:");
|
|
ESP_LOGI(WIFI_TAG, "SSID: %s", ssid);
|
|
ESP_LOGI(WIFI_TAG, "PASSWORD: %s", password);
|
|
ESP_LOGI(WIFI_TAG, "开始连接WiFi...");
|
|
|
|
/* BLE 配网:保存状态并拆 UI,失败时由 main_task 恢复;成功不恢复 */
|
|
/*
|
|
s_ble_wifi_prov_session = true;
|
|
wifi_prov_capture_pre_state();
|
|
lvgl_deinit_for_wifi();
|
|
wifi_prov_mark_lvgl_deinited();
|
|
set_block_auto_sleep(true);
|
|
*/
|
|
if (wifi_init_sta() == wifi_disconnected)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
}
|
|
// 通过蓝牙通知车机 wifi状态
|
|
ESP_LOGI(WIFI_TAG, "===== WiFi配置处理完成 =====");
|
|
}
|
|
|
|
bool wifi_is_connected_now(void)
|
|
{
|
|
return s_wifi_connected_now;
|
|
}
|
|
|
|
bool wifi_sta_reconnect_exhausted(void)
|
|
{
|
|
return s_retry_num >= EXAMPLE_ESP_MAXIMUM_RETRY;
|
|
}
|
|
|
|
bool wifi_sta_still_reconnecting(void)
|
|
{
|
|
return !s_wifi_connected_now && s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY;
|
|
}
|
|
|
|
void wifi_network_shutdown_full(void)
|
|
{
|
|
ESP_LOGW(WIFI_TAG, "wifi_network_shutdown_full: stop + deinit (释放协议栈供 LVGL 重建)");
|
|
s_wifi_intentional_shutdown = true;
|
|
s_wifi_connected_now = false;
|
|
s_ble_wifi_prov_session = false;
|
|
(void)esp_wifi_stop();
|
|
esp_err_t de = esp_wifi_deinit();
|
|
if (de != ESP_OK && de != ESP_ERR_WIFI_NOT_INIT)
|
|
{
|
|
ESP_LOGW(WIFI_TAG, "esp_wifi_deinit: %s", esp_err_to_name(de));
|
|
}
|
|
wifi_initialized = false;
|
|
s_retry_num = 0;
|
|
/* 等默认事件循环处理完本次断开,再清标志,避免竞态 */
|
|
vTaskDelay(pdMS_TO_TICKS(80));
|
|
s_wifi_intentional_shutdown = false;
|
|
}
|
|
|