esp32s3/wifi: use wapis init config to save Wi-Fi data

Instead of using Espressif's emulated NVS to save Wi-Fi data, use
`wapi`s wireless configure initialization mechanism for saving
Wi-Fi data. It 1) avoids creating a specific storage partition
just to save Wi-Fi data (ESP32-S3's storage partition is used
instead); 2) avoids initialization problems of the emulated NVS
when SMP is enabled (the Wi-Fi driver tries to initialize it before
the actual partition is initialized); and 3) enables reconnecting
using `wapi reconnect` command and connect the device automatically
on bringup if `CONFIG_NETUTILS_NETINIT` is selected.
This commit is contained in:
Tiago Medicci Serrano 2023-05-04 15:04:19 -03:00 committed by Xiang Xiao
parent 26e99ed042
commit b680abd04b
5 changed files with 18 additions and 334 deletions

View File

@ -890,50 +890,6 @@ config ESP32S3_WIFI_SCAN_RESULT_SIZE
---help---
Maximum scan result buffer size.
config ESP32S3_WIFI_SAVE_PARAM
bool "Save Wi-Fi Parameters"
default n
depends on ESP32S3_SPIFLASH
---help---
If you enable this option, Wi-Fi adapter parameters will be saved
into the file system instead of computing them each time.
These parameters mainly contains:
- SSID
- Password
- BSSID
- PMK(compute when connecting)
- Author mode
- MAC address
- Wi-Fi hardware configuration parameters
config ESP32S3_WIFI_FS_MOUNTPT
string "Wi-Fi parameters mount point"
default "/mnt/esp/wifi"
depends on ESP32S3_WIFI_SAVE_PARAM
---help---
Mount point of Wi-Fi storage file system.
config ESP32S3_WIFI_MTD_ENCRYPT
bool "Encrypt Wi-Fi MTD partition"
default y
depends on ESP32S3_SECURE_FLASH_ENC_ENABLED
config ESP32S3_WIFI_MTD_OFFSET
hex "Wi-Fi MTD partition offset"
default 0x280000 if !ESP32S3_HAVE_OTA_PARTITION
default 0x350000 if ESP32S3_HAVE_OTA_PARTITION
depends on ESP32S3_WIFI_SAVE_PARAM
---help---
This is the base address of the Wi-Fi MTD partition.
config ESP32S3_WIFI_MTD_SIZE
hex "Wi-Fi MTD partition size"
default 0xb0000
depends on ESP32S3_WIFI_SAVE_PARAM
---help---
This is the size of the Wi-Fi MTD partition.
config ESP32S3_WIFI_STA_DISCONNECT_PM
bool "Power Management for station when disconnected"
default n

View File

@ -78,12 +78,6 @@
#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE))
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
# define NVS_FS_PREFIX CONFIG_ESP32S3_WIFI_FS_MOUNTPT
# define NVS_DIR_BASE NVS_FS_PREFIX"/wifi."
# define NVS_FILE_MODE 0777
#endif
#define WIFI_CONNECT_TIMEOUT CONFIG_ESP32S3_WIFI_CONNECT_TIMEOUT
#define TIMER_INITIALIZED_VAL (0x5aa5a55a)
@ -2717,13 +2711,9 @@ static int32_t esp_nvs_set_i8(uint32_t handle,
const char *key,
int8_t value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(int8_t));
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2746,15 +2736,9 @@ static int32_t esp_nvs_get_i8(uint32_t handle,
const char *key,
int8_t *out_value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
size_t len = sizeof(int8_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2777,13 +2761,9 @@ static int32_t esp_nvs_set_u8(uint32_t handle,
const char *key,
uint8_t value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(uint8_t));
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2806,15 +2786,9 @@ static int32_t esp_nvs_get_u8(uint32_t handle,
const char *key,
uint8_t *out_value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
size_t len = sizeof(uint8_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2837,13 +2811,9 @@ static int32_t esp_nvs_set_u16(uint32_t handle,
const char *key,
uint16_t value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(uint16_t));
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2866,15 +2836,9 @@ static int32_t esp_nvs_get_u16(uint32_t handle,
const char *key,
uint16_t *out_value)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
size_t len = sizeof(uint16_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2897,37 +2861,9 @@ static int32_t esp_nvs_open(const char *name,
uint32_t open_mode,
uint32_t *out_handle)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
int ret;
struct nvs_adpt *nvs_adpt;
int tmp;
char *index_name;
tmp = sizeof(struct nvs_adpt);
nvs_adpt = kmm_malloc(tmp);
if (!nvs_adpt)
{
wlerr("Failed to alloc %d memory\n", tmp);
return -1;
}
ret = asprintf(&index_name, "%s", name);
if (ret < 0)
{
wlerr("Failed to create NVS index_name string\n");
kmm_free(nvs_adpt);
return -1;
}
nvs_adpt->index_name = index_name;
*out_handle = (uint32_t)nvs_adpt;
return 0;
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -2946,14 +2882,7 @@ static int32_t esp_nvs_open(const char *name,
static void esp_nvs_close(uint32_t handle)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
struct nvs_adpt *nvs_adpt = (struct nvs_adpt *)handle;
lib_free(nvs_adpt->index_name);
kmm_free(nvs_adpt);
#else
DEBUGPANIC();
#endif
}
/****************************************************************************
@ -2991,57 +2920,9 @@ static int32_t esp_nvs_set_blob(uint32_t handle,
const void *value,
size_t length)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
struct file file;
int ret;
char *dir;
struct nvs_adpt *nvs_adpt = (struct nvs_adpt *)handle;
char *index_name = nvs_adpt->index_name;
ret = asprintf(&dir, NVS_DIR_BASE"%s.%s", index_name, key);
if (ret < 0)
{
wlerr("Failed to create NVS dir string\n");
return -1;
}
ret = nx_unlink(dir);
if (ret)
{
if (ret != -ENOENT)
{
wlerr("Failed to unlink %s error=%d\n", dir, ret);
lib_free(dir);
return -1;
}
}
ret = file_open(&file, dir, O_WRONLY | O_CREAT, NVS_FILE_MODE);
if (ret < 0)
{
wlerr("Failed to set open %s\n", dir);
lib_free(dir);
return -1;
}
ret = file_write(&file, value, length);
if (ret < 0)
{
wlerr("Failed to write to %s\n", dir);
lib_free(dir);
file_close(&file);
return -1;
}
lib_free(dir);
file_close(&file);
return 0;
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -3066,56 +2947,9 @@ static int32_t esp_nvs_get_blob(uint32_t handle,
void *out_value,
size_t *length)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
struct file file;
int ret;
char *dir;
struct nvs_adpt *nvs_adpt = (struct nvs_adpt *)handle;
char *index_name = nvs_adpt->index_name;
ret = asprintf(&dir, NVS_DIR_BASE"%s.%s", index_name, key);
if (ret < 0)
{
wlerr("Failed to create NVS dir string\n");
return -1;
}
ret = file_open(&file, dir, O_RDONLY);
if (ret < 0)
{
if (ret == -ENOENT)
{
wlinfo("No file %s\n", dir);
lib_free(dir);
return ESP_ERR_NVS_NOT_FOUND;
}
wlerr("Failed to get open %s\n", dir);
lib_free(dir);
return -1;
}
ret = file_read(&file, out_value, *length);
if (ret <= 0)
{
wlerr("Failed to write to %s\n", dir);
lib_free(dir);
file_close(&file);
return -1;
}
else
{
*length = ret;
}
lib_free(dir);
file_close(&file);
return 0;
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -3135,35 +2969,9 @@ static int32_t esp_nvs_get_blob(uint32_t handle,
static int32_t esp_nvs_erase_key(uint32_t handle, const char *key)
{
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
int ret;
char *dir;
struct nvs_adpt *nvs_adpt = (struct nvs_adpt *)handle;
char *index_name = nvs_adpt->index_name;
ret = asprintf(&dir, NVS_DIR_BASE"%s.%s", index_name, key);
if (ret < 0)
{
wlerr("Failed to create NVS dir string\n");
return -1;
}
ret = nx_unlink(dir);
if (ret < 0)
{
wlerr("Failed to delete NVS file %s\n", dir);
lib_free(dir);
return -1;
}
lib_free(dir);
return 0;
#else
DEBUGPANIC();
return -1;
#endif
}
/****************************************************************************
@ -4634,11 +4442,7 @@ int esp_wifi_adapter_init(void)
sq_init(&g_wifi_evt_queue);
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
wifi_cfg.nvs_enable = 1;
#else
wifi_cfg.nvs_enable = 0;
#endif
#ifdef CONFIG_ESP32S3_WIFI_TX_AMPDU
wifi_cfg.ampdu_tx_enable = 1;
@ -4945,17 +4749,17 @@ int esp_wifi_sta_password(struct iwreq *iwr, bool set)
DEBUGASSERT(ext != NULL);
pdata = ext->key;
len = ext->key_len;
if (set && len > PWD_MAX_LEN)
{
return -EINVAL;
}
wifi_cfg = g_sta_wifi_cfg;
if (set)
{
len = ext->key_len;
if (len > PWD_MAX_LEN)
{
return -EINVAL;
}
memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN);
if (ext->alg != IW_ENCODE_ALG_NONE)
@ -4994,6 +4798,7 @@ int esp_wifi_sta_password(struct iwreq *iwr, bool set)
}
else
{
len = iwr->u.encoding.length - sizeof(*ext);
size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN);
if (len < size)
{
@ -5001,8 +4806,8 @@ int esp_wifi_sta_password(struct iwreq *iwr, bool set)
}
else
{
len = size;
memcpy(pdata, wifi_cfg.sta.password, len);
ext->key_len = size;
memcpy(pdata, wifi_cfg.sta.password, ext->key_len);
}
if (g_sta_connected)

View File

@ -50,12 +50,6 @@
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_ESP32S3_WIFI_MTD_ENCRYPT
# define WIFI_ENCRYPT true
#else
# define WIFI_ENCRYPT false
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
@ -255,72 +249,6 @@ static int setup_nxffs(struct mtd_dev_s *mtd, const char *mnt_pt)
}
#endif
/****************************************************************************
* Name: init_wifi_partition
*
* Description:
* Initialize partition that is dedicated to Wi-Fi.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
static int init_wifi_partition(void)
{
int ret = OK;
struct mtd_dev_s *mtd;
mtd = esp32s3_spiflash_alloc_mtdpart(CONFIG_ESP32S3_WIFI_MTD_OFFSET,
CONFIG_ESP32S3_WIFI_MTD_SIZE,
WIFI_ENCRYPT);
if (!mtd)
{
ferr("Failed to alloc MTD partition of SPI Flash\n");
return -ENOMEM;
}
#if defined (CONFIG_ESP32S3_SPIFLASH_SMARTFS)
ret = setup_smartfs(1, mtd, CONFIG_ESP32S3_WIFI_FS_MOUNTPT);
if (ret < 0)
{
ferr("Failed to setup smartfs\n");
return ret;
}
#elif defined(CONFIG_ESP32S3_SPIFLASH_LITTLEFS)
const char *path = "/dev/mtdblock1";
ret = setup_littlefs(path, mtd, CONFIG_ESP32S3_WIFI_FS_MOUNTPT, 0777);
if (ret < 0)
{
ferr("Failed to setup littlefs\n");
return ret;
}
#elif defined(CONFIG_ESP32S3_SPIFLASH_SPIFFS)
const char *path = "/dev/mtdblock1";
ret = setup_spiffs(path, mtd, CONFIG_ESP32S3_WIFI_FS_MOUNTPT, 0777);
if (ret < 0)
{
ferr("Failed to setup spiffs\n");
return ret;
}
#else
ferr("No supported FS selected. Wi-Fi partition "
"should be mounted before Wi-Fi initialization\n");
#endif
return ret;
}
#endif
/****************************************************************************
* Name: init_storage_partition
*
@ -348,7 +276,7 @@ static int init_storage_partition(void)
#if defined (CONFIG_ESP32S3_SPIFLASH_SMARTFS)
ret = setup_smartfs(0, mtd, NULL);
ret = setup_smartfs(0, mtd, "/data");
if (ret < 0)
{
ferr("ERROR: Failed to setup smartfs\n");
@ -357,7 +285,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32S3_SPIFLASH_NXFFS)
ret = setup_nxffs(mtd, "/mnt");
ret = setup_nxffs(mtd, "/data");
if (ret < 0)
{
ferr("ERROR: Failed to setup nxffs\n");
@ -367,7 +295,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32S3_SPIFLASH_LITTLEFS)
const char *path = "/dev/esp32s3flash";
ret = setup_littlefs(path, mtd, NULL, 0755);
ret = setup_littlefs(path, mtd, "/data", 0755);
if (ret < 0)
{
ferr("ERROR: Failed to setup littlefs\n");
@ -377,7 +305,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32S3_SPIFLASH_SPIFFS)
const char *path = "/dev/esp32s3flash";
ret = setup_spiffs(path, mtd, NULL, 0755);
ret = setup_spiffs(path, mtd, "/data", 0755);
if (ret < 0)
{
ferr("ERROR: Failed to setup spiffs\n");
@ -420,14 +348,6 @@ int board_spiflash_init(void)
return ret;
}
#ifdef CONFIG_ESP32S3_WIFI_SAVE_PARAM
ret = init_wifi_partition();
if (ret < 0)
{
return ret;
}
#endif
ret = init_storage_partition();
if (ret < 0)
{

View File

@ -20,6 +20,7 @@ CONFIG_ARCH_CHIP_ESP32S3WROOM1=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DEFAULT_TASK_STACKSIZE=4096
@ -30,7 +31,6 @@ CONFIG_ESP32S3_SPIFLASH=y
CONFIG_ESP32S3_SPIFLASH_SPIFFS=y
CONFIG_ESP32S3_UART0=y
CONFIG_ESP32S3_WIFI=y
CONFIG_ESP32S3_WIFI_SAVE_PARAM=y
CONFIG_ESP32S3_WIFI_STATION_SOFTAP=y
CONFIG_EXAMPLES_DHCPD=y
CONFIG_EXAMPLES_RANDOM=y
@ -46,6 +46,7 @@ CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_PHY_IOCTL=y
CONFIG_NETDEV_WIRELESS_IOCTL=y
CONFIG_NETUTILS_CJSON=y
CONFIG_NETUTILS_DHCPD=y
CONFIG_NETUTILS_IPERF=y
CONFIG_NET_BROADCAST=y
@ -84,4 +85,5 @@ CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_WIRELESS=y
CONFIG_WIRELESS_WAPI=y
CONFIG_WIRELESS_WAPI_CMDTOOL=y
CONFIG_WIRELESS_WAPI_INITCONF=y
CONFIG_WIRELESS_WAPI_STACKSIZE=8192

View File

@ -30,7 +30,6 @@ CONFIG_ESP32S3_SPIFLASH=y
CONFIG_ESP32S3_SPIFLASH_SPIFFS=y
CONFIG_ESP32S3_UART0=y
CONFIG_ESP32S3_WIFI=y
CONFIG_ESP32S3_WIFI_SAVE_PARAM=y
CONFIG_EXAMPLES_RANDOM=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=3072
@ -44,6 +43,7 @@ CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_PHY_IOCTL=y
CONFIG_NETDEV_WIRELESS_IOCTL=y
CONFIG_NETUTILS_CJSON=y
CONFIG_NETUTILS_IPERF=y
CONFIG_NET_BROADCAST=y
CONFIG_NET_ETH_PKTSIZE=1514
@ -81,4 +81,5 @@ CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_WIRELESS=y
CONFIG_WIRELESS_WAPI=y
CONFIG_WIRELESS_WAPI_CMDTOOL=y
CONFIG_WIRELESS_WAPI_INITCONF=y
CONFIG_WIRELESS_WAPI_STACKSIZE=8192