new release
This commit is contained in:
@@ -1,155 +1,267 @@
|
||||
// =========================
|
||||
// wifi.c (ESP-IDF v5.4.2)
|
||||
// =========================
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_mac.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "mdns.h"
|
||||
|
||||
#include "network_events.h"
|
||||
#include "network.h"
|
||||
|
||||
|
||||
#define AP_SSID "plx-%02x%02x%02x"
|
||||
|
||||
#define MDNS_SSID "plx%02x"
|
||||
// -----------------------------------------------------------------------------
|
||||
// Config
|
||||
// -----------------------------------------------------------------------------
|
||||
#define AP_SSID "plx-%02x%02x%02x" // SSID do AP (usa 3 bytes do MAC)
|
||||
#define MDNS_SSID "plx%02x" // hostname mDNS (usa 2 bytes do MAC)
|
||||
|
||||
#define NVS_NAMESPACE "wifi"
|
||||
#define NVS_ENABLED "enabled"
|
||||
#define NVS_SSID "ssid"
|
||||
#define NVS_PASSWORD "password"
|
||||
|
||||
// Comprimentos com terminador
|
||||
#define SSID_MAX_LEN 32
|
||||
#define PASS_MAX_LEN 64 // 63 chars + '\0'
|
||||
#define SSID_BUF_SZ (SSID_MAX_LEN + 1) // 33
|
||||
#define PASS_BUF_SZ (PASS_MAX_LEN + 1) // 65
|
||||
|
||||
static const char *TAG = "wifi";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Estado global
|
||||
// -----------------------------------------------------------------------------
|
||||
static nvs_handle_t nvs;
|
||||
|
||||
static esp_netif_t *sta_netif;
|
||||
|
||||
static esp_netif_t *ap_netif;
|
||||
|
||||
EventGroupHandle_t wifi_event_group;
|
||||
|
||||
// Backoff simples para reconexão STA (agora sem delay no event handler)
|
||||
static int s_retry_count = 0;
|
||||
static const int s_retry_max = 7;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Lê string do NVS com segurança (trunca se necessário)
|
||||
static esp_err_t nvs_get_str_safe(nvs_handle_t h, const char *key, char *out, size_t out_sz)
|
||||
{
|
||||
if (!out || out_sz == 0)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
out[0] = '\0';
|
||||
|
||||
size_t need = 0;
|
||||
esp_err_t err = nvs_get_str(h, key, NULL, &need);
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND)
|
||||
return ESP_OK;
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
|
||||
if (need == 0)
|
||||
return ESP_OK; // vazio
|
||||
|
||||
if (need > out_sz)
|
||||
{
|
||||
// Truncar de forma segura
|
||||
char *tmp = (char *)malloc(need);
|
||||
if (!tmp)
|
||||
return ESP_ERR_NO_MEM;
|
||||
err = nvs_get_str(h, key, tmp, &need);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
snprintf(out, out_sz, "%s", tmp);
|
||||
}
|
||||
free(tmp);
|
||||
return err;
|
||||
}
|
||||
return nvs_get_str(h, key, out, &need);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Eventos
|
||||
// -----------------------------------------------------------------------------
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "event_handler");
|
||||
|
||||
if (event_base == WIFI_EVENT)
|
||||
{
|
||||
if (event_id == WIFI_EVENT_AP_STACONNECTED)
|
||||
switch (event_id)
|
||||
{
|
||||
ESP_LOGI(TAG, "STA connected");
|
||||
case WIFI_EVENT_AP_STACONNECTED:
|
||||
{
|
||||
ESP_LOGI(TAG, "AP: STA connected");
|
||||
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi AP " MACSTR " join, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_AP_CONNECTED_BIT);
|
||||
break;
|
||||
}
|
||||
if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
|
||||
case WIFI_EVENT_AP_STADISCONNECTED:
|
||||
{
|
||||
ESP_LOGI(TAG, "AP STA disconnected");
|
||||
ESP_LOGI(TAG, "AP: STA disconnected");
|
||||
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi AP " MACSTR " leave, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_AP_CONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT);
|
||||
break;
|
||||
}
|
||||
if (event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
ESP_LOGI(TAG, "STA disconnected");
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_STA_CONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT);
|
||||
esp_wifi_connect();
|
||||
}
|
||||
if (event_id == WIFI_EVENT_STA_START)
|
||||
case WIFI_EVENT_STA_START:
|
||||
{
|
||||
ESP_LOGI(TAG, "STA start");
|
||||
s_retry_count = 0;
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
}
|
||||
case WIFI_EVENT_STA_DISCONNECTED:
|
||||
{
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_STA_CONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT);
|
||||
|
||||
wifi_event_sta_disconnected_t *ev = (wifi_event_sta_disconnected_t *)event_data;
|
||||
ESP_LOGW(TAG, "STA disconnected, reason=%d", ev ? ev->reason : -1);
|
||||
|
||||
// NÃO bloquear o event loop com vTaskDelay
|
||||
if (s_retry_count < s_retry_max)
|
||||
{
|
||||
esp_err_t err = esp_wifi_connect();
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
s_retry_count++;
|
||||
ESP_LOGI(TAG, "Retrying connection (%d/%d)", s_retry_count, s_retry_max);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_connect failed (%d)", err);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Max retries reached");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (event_base == IP_EVENT)
|
||||
{
|
||||
ESP_LOGI(TAG, "event_base == IP_EVENT");
|
||||
|
||||
if (event_id == IP_EVENT_STA_GOT_IP || event_id == IP_EVENT_GOT_IP6)
|
||||
if (event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
if (event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi STA got ip: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
}
|
||||
else
|
||||
{
|
||||
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi STA got ip6: " IPV6STR, IPV62STR(event->ip6_info.ip));
|
||||
}
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi STA got ip: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_STA_CONNECTED_BIT);
|
||||
s_retry_count = 0;
|
||||
}
|
||||
else if (event_id == IP_EVENT_GOT_IP6)
|
||||
{
|
||||
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
||||
ESP_LOGI(TAG, "WiFi STA got ip6: " IPV6STR, IPV62STR(event->ip6_info.ip));
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT);
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_STA_CONNECTED_BIT);
|
||||
s_retry_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Config STA/AP
|
||||
// -----------------------------------------------------------------------------
|
||||
static void sta_set_config(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "sta_set_config");
|
||||
|
||||
if (wifi_get_enabled())
|
||||
{
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.pmf_cfg = {
|
||||
.capable = true,
|
||||
.required = false}}};
|
||||
wifi_get_ssid((char *)wifi_config.sta.ssid);
|
||||
wifi_get_password((char *)wifi_config.sta.password);
|
||||
if (!wifi_get_enabled())
|
||||
return;
|
||||
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
|
||||
wifi_config_t wifi_config = {0};
|
||||
wifi_config.sta.pmf_cfg.capable = true;
|
||||
wifi_config.sta.pmf_cfg.required = false;
|
||||
|
||||
// buffers "seguros" vindos da NVS (estes sim têm terminador)
|
||||
char ssid_buf[SSID_BUF_SZ] = {0}; // 33 (32 + '\0')
|
||||
char pass_buf[PASS_BUF_SZ] = {0}; // 65 (64 + '\0')
|
||||
wifi_get_ssid(ssid_buf);
|
||||
wifi_get_password(pass_buf);
|
||||
|
||||
// Copiar **sem** terminador para os campos do esp_wifi (32/64 bytes)
|
||||
// SSID: max 32
|
||||
size_t ssid_len = strnlen(ssid_buf, SSID_MAX_LEN); // até 32
|
||||
memcpy(wifi_config.sta.ssid, ssid_buf, ssid_len);
|
||||
|
||||
// Password WPA/WPA2: 8..63 chars (campo tem 64 bytes)
|
||||
// (se usares rede aberta, pass_len pode ser 0)
|
||||
size_t pass_len = strnlen(pass_buf, 63); // até 63
|
||||
memcpy(wifi_config.sta.password, pass_buf, pass_len);
|
||||
if (pass_len <= 63)
|
||||
{
|
||||
wifi_config.sta.password[pass_len] = '\0';
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); // v5.x
|
||||
}
|
||||
|
||||
static void ap_set_config(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "ap_set_config");
|
||||
|
||||
wifi_config_t wifi_ap_config = {
|
||||
.ap = {
|
||||
.max_connection = 1,
|
||||
.authmode = WIFI_AUTH_OPEN}};
|
||||
wifi_config_t wifi_ap_config = {0};
|
||||
wifi_ap_config.ap.max_connection = 1;
|
||||
wifi_ap_config.ap.authmode = WIFI_AUTH_OPEN; // para portal cativo, por exemplo
|
||||
|
||||
uint8_t mac[6];
|
||||
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);
|
||||
sprintf((char *)wifi_ap_config.ap.ssid, AP_SSID, mac[3], mac[4], mac[5]);
|
||||
esp_wifi_get_mac(WIFI_IF_AP, mac);
|
||||
snprintf((char *)wifi_ap_config.ap.ssid, sizeof(wifi_ap_config.ap.ssid),
|
||||
AP_SSID, mac[3], mac[4], mac[5]); // "plx-XXXXXX"
|
||||
|
||||
wifi_config_t wifi_sta_config = {0};
|
||||
|
||||
esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_ap_config);
|
||||
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config);
|
||||
// Só AP (não mexer na config STA aqui para não a limpar)
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Arranque STA
|
||||
// -----------------------------------------------------------------------------
|
||||
static void sta_try_start(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "sta_try_start");
|
||||
|
||||
sta_set_config();
|
||||
if (wifi_get_enabled())
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting STA");
|
||||
esp_wifi_start();
|
||||
esp_err_t e = esp_wifi_start();
|
||||
if (e != ESP_OK && e != ESP_ERR_WIFI_CONN)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_start returned %d", e);
|
||||
}
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_STA_MODE_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// API pública
|
||||
// -----------------------------------------------------------------------------
|
||||
void wifi_ini(void)
|
||||
{
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "Wifi init");
|
||||
|
||||
// Abre NVS (assume que nvs_flash_init() já foi chamado no boot geral)
|
||||
ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs));
|
||||
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
@@ -160,108 +272,125 @@ void wifi_ini(void)
|
||||
sta_netif = esp_netif_create_default_wifi_sta();
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
||||
|
||||
char chargeid[6];
|
||||
// mDNS: usa dois bytes do MAC para identificar
|
||||
uint8_t mac[6];
|
||||
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);
|
||||
sprintf((char *)chargeid, MDNS_SSID, 0);
|
||||
esp_wifi_get_mac(WIFI_IF_STA, mac);
|
||||
char chargeid[16];
|
||||
// ex.: "plx00"
|
||||
snprintf(chargeid, sizeof(chargeid), MDNS_SSID, 0);
|
||||
|
||||
// Hostname das interfaces alinhado com o mDNS
|
||||
ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, chargeid));
|
||||
ESP_ERROR_CHECK(esp_netif_set_hostname(ap_netif, chargeid));
|
||||
|
||||
ESP_ERROR_CHECK(mdns_init());
|
||||
ESP_ERROR_CHECK(mdns_hostname_set(chargeid));
|
||||
ESP_ERROR_CHECK(mdns_instance_name_set("EVSE controller"));
|
||||
|
||||
sta_try_start();
|
||||
|
||||
}
|
||||
|
||||
esp_netif_t *wifi_get_sta_netif(void)
|
||||
{
|
||||
return sta_netif;
|
||||
}
|
||||
|
||||
esp_netif_t *wifi_get_ap_netif(void)
|
||||
{
|
||||
return ap_netif;
|
||||
}
|
||||
esp_netif_t *wifi_get_sta_netif(void) { return sta_netif; }
|
||||
esp_netif_t *wifi_get_ap_netif(void) { return ap_netif; }
|
||||
|
||||
esp_err_t wifi_set_config(bool enabled, const char *ssid, const char *password)
|
||||
{
|
||||
ESP_LOGI(TAG, "wifi_set_config(enabled=%d)", enabled);
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "wifi_set_config(enabled=%d, ssid=\"%s\")", enabled, ssid?:"<nil>");
|
||||
|
||||
|
||||
// Validação (quando habilitar STA)
|
||||
if (enabled)
|
||||
{
|
||||
if (ssid == NULL || strlen(ssid) == 0)
|
||||
// SSID 1..32
|
||||
if (ssid && (strlen(ssid) == 0 || strlen(ssid) > SSID_MAX_LEN))
|
||||
{
|
||||
ESP_LOGE(TAG, "SSID out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (!ssid)
|
||||
{
|
||||
size_t len = 0;
|
||||
nvs_get_str(nvs, NVS_SSID, NULL, &len);
|
||||
if (len <= 1)
|
||||
esp_err_t e = nvs_get_str(nvs, NVS_SSID, NULL, &len);
|
||||
if (e != ESP_OK || len <= 1)
|
||||
{
|
||||
ESP_LOGE(TAG, "Required SSID");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
// Password: 8..63 (se não for vazia). Aceita "" para rede open (caso uses).
|
||||
if (password)
|
||||
{
|
||||
size_t lp = strlen(password);
|
||||
if (lp != 0 && (lp < 8 || lp > 63))
|
||||
{
|
||||
ESP_LOGE(TAG, "Password length must be 8..63");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ssid != NULL && strlen(ssid) > 32)
|
||||
{
|
||||
ESP_LOGE(TAG, "SSID out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (password != NULL && strlen(password) > 32)
|
||||
{
|
||||
ESP_LOGE(TAG, "Password out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
nvs_set_u8(nvs, NVS_ENABLED, enabled);
|
||||
if (ssid != NULL)
|
||||
{
|
||||
nvs_set_str(nvs, NVS_SSID, ssid);
|
||||
}
|
||||
if (password != NULL)
|
||||
{
|
||||
nvs_set_str(nvs, NVS_PASSWORD, password);
|
||||
}
|
||||
nvs_commit(nvs);
|
||||
// Persiste no NVS
|
||||
ESP_ERROR_CHECK(nvs_set_u8(nvs, NVS_ENABLED, enabled));
|
||||
if (ssid)
|
||||
ESP_ERROR_CHECK(nvs_set_str(nvs, NVS_SSID, ssid));
|
||||
if (password)
|
||||
ESP_ERROR_CHECK(nvs_set_str(nvs, NVS_PASSWORD, password));
|
||||
ESP_ERROR_CHECK(nvs_commit(nvs));
|
||||
|
||||
// Reinicia modo
|
||||
ESP_LOGI(TAG, "Stopping AP/STA");
|
||||
xEventGroupClearBits(wifi_event_group, WIFI_AP_MODE_BIT | WIFI_STA_MODE_BIT);
|
||||
esp_wifi_stop();
|
||||
esp_err_t e = esp_wifi_stop();
|
||||
if (e != ESP_OK && e != ESP_ERR_WIFI_NOT_INIT && e != ESP_ERR_WIFI_STOP_STATE)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_stop returned %d", e);
|
||||
}
|
||||
|
||||
sta_try_start();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
uint16_t wifi_scan(wifi_scan_ap_t *scan_aps)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "wifi_scan");
|
||||
|
||||
if (!scan_aps)
|
||||
return 0;
|
||||
|
||||
uint16_t number = WIFI_SCAN_SCAN_LIST_SIZE;
|
||||
wifi_ap_record_t ap_info[WIFI_SCAN_SCAN_LIST_SIZE];
|
||||
uint16_t ap_count = 0;
|
||||
memset(ap_info, 0, sizeof(ap_info));
|
||||
|
||||
esp_wifi_scan_start(NULL, true);
|
||||
esp_wifi_scan_get_ap_records(&number, ap_info);
|
||||
esp_wifi_scan_get_ap_num(&ap_count);
|
||||
esp_err_t err = esp_wifi_scan_start(NULL, true);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_scan_start failed (%d)", err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = esp_wifi_scan_get_ap_records(&number, ap_info);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_scan_get_ap_records failed (%d)", err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = esp_wifi_scan_get_ap_num(&ap_count);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_scan_get_ap_num failed (%d)", err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "wifi_scan --- %d", ap_count);
|
||||
|
||||
for (int i = 0; (i < WIFI_SCAN_SCAN_LIST_SIZE) && (i < ap_count); i++)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "wifi_scan ---");
|
||||
|
||||
strcpy(scan_aps[i].ssid, (const char *)ap_info[i].ssid);
|
||||
// garante que scan_aps[i].ssid tenha pelo menos 33 bytes
|
||||
snprintf(scan_aps[i].ssid, SSID_BUF_SZ, "%s", (const char *)ap_info[i].ssid);
|
||||
scan_aps[i].rssi = ap_info[i].rssi;
|
||||
scan_aps[i].auth = ap_info[i].authmode != WIFI_AUTH_OPEN;
|
||||
}
|
||||
@@ -271,23 +400,30 @@ uint16_t wifi_scan(wifi_scan_ap_t *scan_aps)
|
||||
|
||||
bool wifi_get_enabled(void)
|
||||
{
|
||||
uint8_t value = false;
|
||||
nvs_get_u8(nvs, NVS_ENABLED, &value);
|
||||
uint8_t value = 0;
|
||||
esp_err_t e = nvs_get_u8(nvs, NVS_ENABLED, &value);
|
||||
if (e == ESP_ERR_NVS_NOT_FOUND)
|
||||
return false;
|
||||
if (e != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "nvs_get_u8(NVS_ENABLED) failed (%d), assuming disabled", e);
|
||||
return false;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void wifi_get_ssid(char *value)
|
||||
{
|
||||
size_t len = 32;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_SSID, value, &len);
|
||||
if (!value)
|
||||
return;
|
||||
nvs_get_str_safe(nvs, NVS_SSID, value, SSID_BUF_SZ); // 33
|
||||
}
|
||||
|
||||
void wifi_get_password(char *value)
|
||||
{
|
||||
size_t len = 64;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_PASSWORD, value, &len);
|
||||
if (!value)
|
||||
return;
|
||||
nvs_get_str_safe(nvs, NVS_PASSWORD, value, PASS_BUF_SZ); // 65
|
||||
}
|
||||
|
||||
void wifi_ap_start(void)
|
||||
@@ -300,7 +436,11 @@ void wifi_ap_start(void)
|
||||
esp_wifi_stop();
|
||||
|
||||
ap_set_config();
|
||||
esp_wifi_start();
|
||||
esp_err_t e = esp_wifi_start();
|
||||
if (e != ESP_OK && e != ESP_ERR_WIFI_CONN)
|
||||
{
|
||||
ESP_LOGW(TAG, "esp_wifi_start (AP) returned %d", e);
|
||||
}
|
||||
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_AP_MODE_BIT);
|
||||
}
|
||||
@@ -319,7 +459,9 @@ void wifi_ap_stop(void)
|
||||
|
||||
bool wifi_is_ap(void)
|
||||
{
|
||||
wifi_mode_t mode;
|
||||
esp_wifi_get_mode(&mode);
|
||||
return mode == WIFI_MODE_APSTA;
|
||||
if (!wifi_event_group)
|
||||
return false;
|
||||
|
||||
EventBits_t bits = xEventGroupGetBits(wifi_event_group);
|
||||
return (bits & WIFI_AP_MODE_BIT) != 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user