All configuration is now done exclusively through mimi_secrets.h at build time. Removed NVS read/write logic, CLI config commands (wifi_set, set_tg_token, set_api_key, set_model, set_proxy, clear_proxy, set_search_key), and setter functions from all modules. CLI retains debug/maintenance commands only. Updated all documentation to reflect the change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
117 lines
3.7 KiB
C
117 lines
3.7 KiB
C
#include "wifi_manager.h"
|
|
#include "mimi_config.h"
|
|
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
#include "esp_log.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_netif.h"
|
|
|
|
static const char *TAG = "wifi";
|
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
|
static int s_retry_count = 0;
|
|
static char s_ip_str[16] = "0.0.0.0";
|
|
static bool s_connected = false;
|
|
|
|
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) {
|
|
esp_wifi_connect();
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
s_connected = false;
|
|
if (s_retry_count < MIMI_WIFI_MAX_RETRY) {
|
|
/* Exponential backoff: 1s, 2s, 4s, 8s, ... capped at 30s */
|
|
uint32_t delay_ms = MIMI_WIFI_RETRY_BASE_MS << s_retry_count;
|
|
if (delay_ms > MIMI_WIFI_RETRY_MAX_MS) {
|
|
delay_ms = MIMI_WIFI_RETRY_MAX_MS;
|
|
}
|
|
ESP_LOGW(TAG, "Disconnected, retry %d/%d in %" PRIu32 "ms",
|
|
s_retry_count + 1, MIMI_WIFI_MAX_RETRY, delay_ms);
|
|
vTaskDelay(pdMS_TO_TICKS(delay_ms));
|
|
esp_wifi_connect();
|
|
s_retry_count++;
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to connect after %d retries", MIMI_WIFI_MAX_RETRY);
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
|
}
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
snprintf(s_ip_str, sizeof(s_ip_str), IPSTR, IP2STR(&event->ip_info.ip));
|
|
ESP_LOGI(TAG, "Connected! IP: %s", s_ip_str);
|
|
s_retry_count = 0;
|
|
s_connected = true;
|
|
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
esp_err_t wifi_manager_init(void)
|
|
{
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
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));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_LOGI(TAG, "WiFi manager initialized");
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t wifi_manager_start(void)
|
|
{
|
|
if (MIMI_SECRET_WIFI_SSID[0] == '\0') {
|
|
ESP_LOGW(TAG, "No WiFi credentials. Set MIMI_SECRET_WIFI_SSID in mimi_secrets.h");
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
|
|
wifi_config_t wifi_cfg = {0};
|
|
strncpy((char *)wifi_cfg.sta.ssid, MIMI_SECRET_WIFI_SSID, sizeof(wifi_cfg.sta.ssid) - 1);
|
|
strncpy((char *)wifi_cfg.sta.password, MIMI_SECRET_WIFI_PASS, sizeof(wifi_cfg.sta.password) - 1);
|
|
|
|
ESP_LOGI(TAG, "Connecting to SSID: %s", wifi_cfg.sta.ssid);
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t wifi_manager_wait_connected(uint32_t timeout_ms)
|
|
{
|
|
TickType_t ticks = (timeout_ms == UINT32_MAX) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
|
|
EventBits_t bits = xEventGroupWaitBits(
|
|
s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
|
pdFALSE, pdFALSE, ticks);
|
|
|
|
if (bits & WIFI_CONNECTED_BIT) {
|
|
return ESP_OK;
|
|
}
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
|
|
bool wifi_manager_is_connected(void)
|
|
{
|
|
return s_connected;
|
|
}
|
|
|
|
const char *wifi_manager_get_ip(void)
|
|
{
|
|
return s_ip_str;
|
|
}
|
|
|
|
EventGroupHandle_t wifi_manager_get_event_group(void)
|
|
{
|
|
return s_wifi_event_group;
|
|
}
|