e523398fa8
- apps/device-agent-embedded/: ESP32 WireGuard agent (C, ESP-IDF, mbedTLS) - reference/namespace-vrf/: Linux namespace/VRF network isolation docs - apps/android-agent: VPN permission check, StartResult sealed class
148 lines
4.1 KiB
C
148 lines
4.1 KiB
C
/**
|
|
* @file wifi.c
|
|
* @brief WiFi STA Management
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include "wifi.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_netif.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "freertos/task.h"
|
|
|
|
static const char *TAG = "nexus_wifi";
|
|
|
|
/* Event group bits */
|
|
#define WIFI_CONNECTED_BIT BIT0
|
|
#define WIFI_FAIL_BIT BIT1
|
|
|
|
/* Static state */
|
|
static EventGroupHandle_t s_wifi_event_group = NULL;
|
|
static esp_netif_t *s_sta_netif = NULL;
|
|
static esp_event_handler_instance_t s_any_id_instance = NULL;
|
|
static esp_event_handler_instance_t s_got_ip_instance = NULL;
|
|
static bool s_initialized = false;
|
|
|
|
/**
|
|
* @brief WiFi event handler
|
|
*/
|
|
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data)
|
|
{
|
|
if (event_base == WIFI_EVENT) {
|
|
switch (event_id) {
|
|
case WIFI_EVENT_STA_START:
|
|
ESP_LOGI(TAG, "WiFi STA started, connecting...");
|
|
esp_wifi_connect();
|
|
break;
|
|
|
|
case WIFI_EVENT_STA_DISCONNECTED:
|
|
ESP_LOGW(TAG, "WiFi disconnected");
|
|
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
/* Auto-reconnect is handled by esp_wifi_connect() below */
|
|
esp_wifi_connect();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
} 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;
|
|
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
esp_err_t wifi_init_sta(const char *ssid, const char *password)
|
|
{
|
|
if (ssid == NULL || password == NULL) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
|
|
if (s_initialized) {
|
|
ESP_LOGW(TAG, "WiFi already initialized");
|
|
return ESP_OK;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Initializing WiFi STA...");
|
|
|
|
/* Create event group */
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
|
|
/* Initialize network interface */
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
s_sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
/* Initialize WiFi */
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
/* Register event handlers */
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
WIFI_EVENT, ESP_EVENT_ANY_ID,
|
|
&wifi_event_handler, NULL, &s_any_id_instance));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
IP_EVENT, IP_EVENT_STA_GOT_IP,
|
|
&wifi_event_handler, NULL, &s_got_ip_instance));
|
|
|
|
/* Configure WiFi */
|
|
wifi_config_t wifi_config = {
|
|
.sta = {
|
|
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
|
.pmf_cfg = {
|
|
.capable = true,
|
|
.required = false,
|
|
},
|
|
},
|
|
};
|
|
|
|
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);
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
s_initialized = true;
|
|
ESP_LOGI(TAG, "WiFi STA initialized, will connect to: %s", ssid);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t wifi_connect(void)
|
|
{
|
|
if (!s_initialized) {
|
|
ESP_LOGE(TAG, "WiFi not initialized, call wifi_init_sta first");
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Connecting to WiFi...");
|
|
esp_err_t err = esp_wifi_connect();
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "WiFi connect failed: %s", esp_err_to_name(err));
|
|
}
|
|
return err;
|
|
}
|
|
|
|
esp_err_t wifi_disconnect(void)
|
|
{
|
|
if (!s_initialized) {
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Disconnecting from WiFi...");
|
|
return esp_wifi_disconnect();
|
|
}
|
|
|
|
bool wifi_is_connected(void)
|
|
{
|
|
if (s_wifi_event_group == NULL) {
|
|
return false;
|
|
}
|
|
return (xEventGroupGetBits(s_wifi_event_group) & WIFI_CONNECTED_BIT) != 0;
|
|
}
|