157 lines
4.8 KiB
C
157 lines
4.8 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"
|
|
#include "task_config.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_EV_STATE_CHANGE,
|
|
} service_wifi_event_t;
|
|
|
|
typedef enum {
|
|
SERVICE_WIFI_STATE_DISABLED,
|
|
SERVICE_WIFI_STATE_DPP,
|
|
SERVICE_WIFI_STATE_CONNECTING,
|
|
SERVICE_WIFI_STATE_CONNECTED,
|
|
SERVICE_WIFI_STATE_IP_READY,
|
|
} service_wifi_state_t;
|
|
|
|
typedef struct {
|
|
service_wifi_event_t ev;
|
|
void *payload;
|
|
} service_wifi_request_t;
|
|
|
|
static esp_err_t service_wifi_update_state(service_wifi_state_t new_state) {
|
|
service_wifi_request_t req = {
|
|
.ev = SERVICE_WIFI_EV_STATE_CHANGE,
|
|
.payload = (void *)new_state,
|
|
};
|
|
|
|
if(xQueueSend(s_service_wifi_event_queue, &req, 0) != pdPASS) {
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static void service_wifi_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) {
|
|
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_wifi_update_state(SERVICE_WIFI_STATE_IP_READY);
|
|
}
|
|
}
|
|
|
|
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_request_t request;
|
|
for (;;) {
|
|
if (xQueueReceive(s_service_wifi_event_queue, &request, portMAX_DELAY) == pdPASS) {
|
|
if (request.ev == SERVICE_WIFI_EV_STATE_CHANGE) {
|
|
service_wifi_state_t state = (service_wifi_state_t)request.payload;
|
|
switch (state) {
|
|
case SERVICE_WIFI_STATE_IP_READY:
|
|
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_request_t));
|
|
if (s_service_wifi_event_queue == NULL) {
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
if (xTaskCreate(service_wifi_task, "SVC_WIFI", TASK_SVC_WIFI_STACK_SIZE, NULL, TASK_SVC_WIFI_PRIORITY,
|
|
&s_service_wifi_task_handle) != pdPASS) {
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t service_wifi_notify() {
|
|
return ESP_OK;
|
|
}
|