127 lines
4.0 KiB
C
127 lines
4.0 KiB
C
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_spi_flash.h"
|
|
#include "esp_system.h"
|
|
#include "esp_wifi.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/queue.h"
|
|
#include "freertos/task.h"
|
|
#include "if_standby.h"
|
|
#include "if_wifi.h"
|
|
#include "nvs_flash.h"
|
|
#include "sdkconfig.h"
|
|
|
|
#define LOG_TAG "SVC_WIFI"
|
|
|
|
#define WIFI_SERVICE_SSID CONFIG_ESP_WIFI_SSID
|
|
#define WIFI_SERVICE_PASS CONFIG_ESP_WIFI_PASSWORD
|
|
#define WIFI_SERVICE_MAXIMUM_RETRY CONFIG_ESP_WIFI_MAXIMUM_RETRY
|
|
|
|
static TaskHandle_t s_service_wifi_task_handle;
|
|
static QueueHandle_t s_service_wifi_event_queue;
|
|
|
|
static uint8_t s_retry_count = 0;
|
|
|
|
typedef enum {
|
|
SERVICE_WIFI_EVENT_CONNECTING,
|
|
SERVICE_WIFI_EVENT_DISCONNECTED,
|
|
SERVICE_WIFI_EVENT_CONNECTED,
|
|
SERVICE_WIFI_EVENT_START_DPP,
|
|
} service_wifi_event_t;
|
|
|
|
static void service_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
|
|
service_wifi_event_t service_ev;
|
|
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) {
|
|
if (s_retry_count < WIFI_SERVICE_MAXIMUM_RETRY) {
|
|
esp_wifi_connect();
|
|
s_retry_count++;
|
|
}
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
s_retry_count = 0;
|
|
service_ev = SERVICE_WIFI_EVENT_CONNECTED;
|
|
xQueueSend(s_service_wifi_event_queue, &service_ev, 0);
|
|
}
|
|
}
|
|
|
|
void service_wifi_task(void *pvParameters) {
|
|
char wifi_ssid[32];
|
|
char wifi_pass[32];
|
|
|
|
size_t wifi_length = 32;
|
|
|
|
uint8_t wifi_config_valid = 0;
|
|
|
|
nvs_handle_t n_handle;
|
|
if (nvs_open("storage", NVS_READWRITE, &n_handle) != ESP_OK) {
|
|
ESP_LOGE(LOG_TAG, "Failed to create NVS handle.");
|
|
vTaskSuspend(NULL);
|
|
}
|
|
|
|
// TODO: check WiFi storage length.
|
|
if (nvs_get_str(n_handle, "u_wifi_ssid", wifi_ssid, &wifi_length) == ESP_OK) {
|
|
if (nvs_get_str(n_handle, "u_wifi_pass", wifi_pass, &wifi_length) == ESP_OK) {
|
|
wifi_config_valid = 1;
|
|
}
|
|
}
|
|
|
|
if (!wifi_config_valid) {
|
|
if_standby_component_update(IF_STANDBY_COMPONENT_WIFI, "\U000F092D");
|
|
}
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta =
|
|
{
|
|
.ssid = WIFI_SERVICE_SSID,
|
|
.password = WIFI_SERVICE_PASS,
|
|
.threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK,
|
|
.pmf_cfg =
|
|
{
|
|
.capable = true,
|
|
.required = false,
|
|
},
|
|
},
|
|
};
|
|
|
|
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());
|
|
|
|
service_wifi_event_t queue_event;
|
|
for (;;) {
|
|
if (xQueueReceive(s_service_wifi_event_queue, &queue_event, portMAX_DELAY) == pdPASS) {
|
|
switch (queue_event) {
|
|
case SERVICE_WIFI_EVENT_CONNECTED:
|
|
if_standby_component_update(IF_STANDBY_COMPONENT_WIFI, "\U000F0928");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
esp_err_t service_wifi_init(void) {
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
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_register(WIFI_EVENT, ESP_EVENT_ANY_ID, service_wifi_event_handler, NULL));
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, service_wifi_event_handler, NULL));
|
|
|
|
s_service_wifi_event_queue = xQueueCreate(2, sizeof(service_wifi_event_t));
|
|
if (s_service_wifi_event_queue == NULL) {
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
if (xTaskCreate(service_wifi_task, "SVC_WIFI", 2048, NULL, 6, &s_service_wifi_task_handle) != pdPASS) {
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|