123 lines
3.9 KiB
C
123 lines
3.9 KiB
C
/*
|
|
* MTG RFID Companion - Wi-Fi manager (Core 0)
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_netif.h"
|
|
#include "nvs_flash.h"
|
|
#include "wifi_manager.h"
|
|
|
|
static const char *TAG = "wifi";
|
|
|
|
#define WIFI_RETRY_MAX (UINT8_MAX)
|
|
#define WIFI_RECONNECT_DELAY_MS (3000)
|
|
|
|
static EventGroupHandle_t s_wifi_group = NULL;
|
|
#define WIFI_CONNECTED_BIT BIT0
|
|
|
|
static int s_retry_count = 0;
|
|
|
|
static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, void *data)
|
|
{
|
|
(void)arg;
|
|
|
|
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) {
|
|
ESP_LOGI(TAG, "station started, connecting...");
|
|
esp_wifi_connect();
|
|
} else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
if (s_retry_count < WIFI_RETRY_MAX) {
|
|
ESP_LOGW(TAG, "disconnected (%d), retrying in %d ms", s_retry_count, WIFI_RECONNECT_DELAY_MS);
|
|
xEventGroupClearBits(s_wifi_group, WIFI_CONNECTED_BIT);
|
|
vTaskDelay(pdMS_TO_TICKS(WIFI_RECONNECT_DELAY_MS));
|
|
esp_wifi_connect();
|
|
s_retry_count++;
|
|
}
|
|
} else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)data;
|
|
ESP_LOGI(TAG, "connected, IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
|
s_retry_count = 0;
|
|
xEventGroupSetBits(s_wifi_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
esp_err_t wifi_manager_init(void)
|
|
{
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
if (ret != ESP_OK) {
|
|
return ret;
|
|
}
|
|
|
|
s_wifi_group = xEventGroupCreate();
|
|
if (s_wifi_group == NULL) {
|
|
return ESP_ERR_NO_MEM;
|
|
}
|
|
|
|
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_instance_register(
|
|
WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
|
IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL));
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t wifi_manager_start(void)
|
|
{
|
|
#ifndef CONFIG_MTG_WIFI_SSID
|
|
#error "Missing CONFIG_MTG_WIFI_SSID - run idf.py menuconfig"
|
|
#endif
|
|
|
|
const char *ssid = CONFIG_MTG_WIFI_SSID;
|
|
const char *pass = CONFIG_MTG_WIFI_PASS;
|
|
|
|
/* Empty SSID, or an untouched sdkconfig.defaults.esp32 placeholder:
|
|
* keep station mode disabled so we don't hammer a bogus AP at boot. */
|
|
if (strlen(ssid) == 0 || strstr(ssid, "YOUR_") != NULL) {
|
|
ESP_LOGW(TAG, "no SSID configured - Wi-Fi station skipped (set CONFIG_MTG_WIFI_SSID)");
|
|
return ESP_OK;
|
|
}
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta = {
|
|
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
|
},
|
|
};
|
|
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid) - 1);
|
|
strncpy((char *)wifi_config.sta.password, pass, 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());
|
|
|
|
ESP_LOGI(TAG, "station mode started for '%s'", ssid);
|
|
return ESP_OK;
|
|
}
|
|
|
|
bool wifi_manager_connected(void)
|
|
{
|
|
return s_wifi_group != NULL && (xEventGroupGetBits(s_wifi_group) & WIFI_CONNECTED_BIT) != 0;
|
|
}
|
|
|
|
esp_err_t wifi_manager_wait_connected(TickType_t timeout_ticks)
|
|
{
|
|
if (s_wifi_group == NULL) {
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
EventBits_t bits = xEventGroupWaitBits(s_wifi_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, timeout_ticks);
|
|
return (bits & WIFI_CONNECTED_BIT) ? ESP_OK : ESP_ERR_TIMEOUT;
|
|
} |