esp32c3/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-C3'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-08 13:21:02 -03:00 committed by Xiang Xiao
parent 3e07477c85
commit e8e50900d0
5 changed files with 12 additions and 326 deletions

View File

@ -774,50 +774,6 @@ config ESP32C3_WIFI_SCAN_RESULT_SIZE
---help--- ---help---
Maximum scan result buffer size. Maximum scan result buffer size.
config ESP32C3_WIFI_SAVE_PARAM
bool "Save Wi-Fi Parameters"
default n
depends on !DISABLE_MOUNTPOINT
---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 ESP32C3_WIFI_FS_MOUNTPT
string "Wi-Fi parameters mount point"
default "/mnt/esp/wifi"
depends on ESP32C3_WIFI_SAVE_PARAM
---help---
Mount point of Wi-Fi storage file system.
config ESP32C3_WIFI_MTD_ENCRYPT
bool "Encrypt Wi-Fi MTD partition"
default y
depends on ESP32C3_SECURE_FLASH_ENC_ENABLED
config ESP32C3_WIFI_MTD_OFFSET
hex "Wi-Fi MTD partition offset"
default 0x280000 if !ESP32C3_HAVE_OTA_PARTITION
default 0x350000 if ESP32C3_HAVE_OTA_PARTITION
depends on ESP32C3_WIFI_SAVE_PARAM
---help---
This is the base address of the Wi-Fi MTD partition.
config ESP32C3_WIFI_MTD_SIZE
hex "Wi-Fi MTD partition size"
default 0xb0000
depends on ESP32C3_WIFI_SAVE_PARAM
---help---
This is the size of the Wi-Fi MTD partition.
config ESP32C3_WIFI_STA_DISCONNECT_PM config ESP32C3_WIFI_STA_DISCONNECT_PM
bool "Power Management for station when disconnected" bool "Power Management for station when disconnected"
default n default n

View File

@ -87,12 +87,6 @@
#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) #define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE))
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
# define NVS_FS_PREFIX CONFIG_ESP32C3_WIFI_FS_MOUNTPT
# define NVS_DIR_BASE NVS_FS_PREFIX"/wifi."
# define NVS_FILE_MODE 0777
#endif
#define WIFI_CONNECT_TIMEOUT CONFIG_ESP32C3_WIFI_CONNECT_TIMEOUT #define WIFI_CONNECT_TIMEOUT CONFIG_ESP32C3_WIFI_CONNECT_TIMEOUT
#define WIFI_RECONNECT_COUNT (10) #define WIFI_RECONNECT_COUNT (10)
@ -2945,13 +2939,9 @@ static int esp_nvs_set_i8(uint32_t handle,
const char *key, const char *key,
int8_t value) int8_t value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(int8_t));
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -2974,15 +2964,9 @@ static int esp_nvs_get_i8(uint32_t handle,
const char *key, const char *key,
int8_t *out_value) int8_t *out_value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
size_t len = sizeof(int8_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3005,13 +2989,9 @@ static int esp_nvs_set_u8(uint32_t handle,
const char *key, const char *key,
uint8_t value) uint8_t value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(uint8_t));
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3034,15 +3014,9 @@ static int esp_nvs_get_u8(uint32_t handle,
const char *key, const char *key,
uint8_t *out_value) uint8_t *out_value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
size_t len = sizeof(uint8_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3065,13 +3039,9 @@ static int esp_nvs_set_u16(uint32_t handle,
const char *key, const char *key,
uint16_t value) uint16_t value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
return esp_nvs_set_blob(handle, key, &value, sizeof(uint16_t));
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3094,15 +3064,9 @@ static int esp_nvs_get_u16(uint32_t handle,
const char *key, const char *key,
uint16_t *out_value) uint16_t *out_value)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
size_t len = sizeof(uint16_t);
return esp_nvs_get_blob(handle, key, out_value, &len);
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3125,37 +3089,9 @@ static int esp_nvs_open(const char *name,
uint32_t open_mode, uint32_t open_mode,
uint32_t *out_handle) uint32_t *out_handle)
{ {
#ifdef CONFIG_ESP32C3_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("ERROR: Failed to alloc %d memory\n", tmp);
return -1;
}
ret = asprintf(&index_name, "%s", name);
if (ret < 0)
{
wlerr("ERROR: 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(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3174,14 +3110,7 @@ static int esp_nvs_open(const char *name,
static void esp_nvs_close(uint32_t handle) static void esp_nvs_close(uint32_t handle)
{ {
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
struct nvs_adpt *nvs_adpt = (struct nvs_adpt *)handle;
kmm_free(nvs_adpt->index_name);
kmm_free(nvs_adpt);
#else
DEBUGPANIC(); DEBUGPANIC();
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3219,57 +3148,9 @@ static int esp_nvs_set_blob(uint32_t handle,
const void *value, const void *value,
size_t length) size_t length)
{ {
#ifdef CONFIG_ESP32C3_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("ERROR: Failed to create NVS dir string\n");
return -1;
}
ret = nx_unlink(dir);
if (ret)
{
if (ret != -ENOENT)
{
wlerr("ERROR: Failed to unlink %s error=%d\n", dir, ret);
kmm_free(dir);
return -1;
}
}
ret = file_open(&file, dir, O_WRONLY | O_CREAT, NVS_FILE_MODE);
if (ret < 0)
{
wlerr("ERROR: Failed to set open %s\n", dir);
kmm_free(dir);
return -1;
}
ret = file_write(&file, value, length);
if (ret < 0)
{
wlerr("ERROR: Failed to write to %s\n", dir);
kmm_free(dir);
file_close(&file);
return -1;
}
kmm_free(dir);
file_close(&file);
return 0;
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3294,56 +3175,9 @@ static int esp_nvs_get_blob(uint32_t handle,
void *out_value, void *out_value,
size_t *length) size_t *length)
{ {
#ifdef CONFIG_ESP32C3_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("ERROR: Failed to create NVS dir string\n");
return -1;
}
ret = file_open(&file, dir, O_RDONLY);
if (ret < 0)
{
if (ret == -ENOENT)
{
wlinfo("INFO: No file %s\n", dir);
kmm_free(dir);
return ESP_ERR_NVS_NOT_FOUND;
}
wlerr("ERROR: Failed to get open %s\n", dir);
kmm_free(dir);
return -1;
}
ret = file_read(&file, out_value, *length);
if (ret <= 0)
{
wlerr("ERROR: Failed to write to %s\n", dir);
kmm_free(dir);
file_close(&file);
return -1;
}
else
{
*length = ret;
}
kmm_free(dir);
file_close(&file);
return 0;
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -3363,35 +3197,9 @@ static int esp_nvs_get_blob(uint32_t handle,
static int esp_nvs_erase_key(uint32_t handle, const char *key) static int esp_nvs_erase_key(uint32_t handle, const char *key)
{ {
#ifdef CONFIG_ESP32C3_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("ERROR: Failed to create NVS dir string\n");
return -1;
}
ret = nx_unlink(dir);
if (ret < 0)
{
wlerr("ERROR: Failed to delete NVS file %s\n", dir);
kmm_free(dir);
return -1;
}
kmm_free(dir);
return 0;
#else
DEBUGPANIC(); DEBUGPANIC();
return -1; return -1;
#endif
} }
/**************************************************************************** /****************************************************************************
@ -5040,11 +4848,7 @@ int esp_wifi_adapter_init(void)
sq_init(&g_wifi_evt_queue); sq_init(&g_wifi_evt_queue);
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
wifi_cfg.nvs_enable = 1;
#else
wifi_cfg.nvs_enable = 0; wifi_cfg.nvs_enable = 0;
#endif
#ifdef CONFIG_ESP32C3_WIFI_TX_AMPDU #ifdef CONFIG_ESP32C3_WIFI_TX_AMPDU
wifi_cfg.ampdu_tx_enable = 1; wifi_cfg.ampdu_tx_enable = 1;

View File

@ -60,12 +60,6 @@
# define OTA_ENCRYPT false # define OTA_ENCRYPT false
#endif #endif
#ifdef CONFIG_ESP32C3_WIFI_MTD_ENCRYPT
# define WIFI_ENCRYPT true
#else
# define WIFI_ENCRYPT false
#endif
#ifdef CONFIG_ESP32C3_STORAGE_MTD_ENCRYPT #ifdef CONFIG_ESP32C3_STORAGE_MTD_ENCRYPT
# define STORAGE_ENCRYPT true # define STORAGE_ENCRYPT true
#else #else
@ -373,72 +367,6 @@ static int setup_nxffs(struct mtd_dev_s *mtd, const char *mnt_pt)
} }
#endif #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.
*
****************************************************************************/
#if defined (CONFIG_ESP32C3_WIFI_SAVE_PARAM)
static int init_wifi_partition(void)
{
int ret = OK;
struct mtd_dev_s *mtd;
mtd = esp32c3_spiflash_alloc_mtdpart(CONFIG_ESP32C3_WIFI_MTD_OFFSET,
CONFIG_ESP32C3_WIFI_MTD_SIZE,
WIFI_ENCRYPT);
if (!mtd)
{
ferr("ERROR: Failed to alloc MTD partition of SPI Flash\n");
return ERROR;
}
#if defined (CONFIG_ESP32C3_SPIFLASH_SMARTFS)
ret = setup_smartfs(1, mtd, CONFIG_ESP32C3_WIFI_FS_MOUNTPT);
if (ret < 0)
{
ferr("ERROR: Failed to setup smartfs\n");
return ret;
}
#elif defined (CONFIG_ESP32C3_SPIFLASH_LITTLEFS)
const char *path = "/dev/mtdblock1";
ret = setup_littlefs(path, mtd, CONFIG_ESP32C3_WIFI_FS_MOUNTPT, 0777);
if (ret < 0)
{
ferr("ERROR: Failed to setup littlefs\n");
return ret;
}
#elif defined (CONFIG_ESP32C3_SPIFLASH_SPIFFS)
const char *path = "/dev/mtdblock1";
ret = setup_spiffs(path, mtd, CONFIG_ESP32C3_WIFI_FS_MOUNTPT, 0777);
if (ret < 0)
{
ferr("ERROR: Failed to setup spiffs\n");
return ret;
}
#else
ferr("ERROR: No supported FS selected. Wi-Fi partition "
"should be mounted before Wi-Fi initialization\n");
#endif
return ret;
}
#endif
/**************************************************************************** /****************************************************************************
* Name: init_storage_partition * Name: init_storage_partition
* *
@ -466,7 +394,7 @@ static int init_storage_partition(void)
#if defined (CONFIG_ESP32C3_SPIFLASH_SMARTFS) #if defined (CONFIG_ESP32C3_SPIFLASH_SMARTFS)
ret = setup_smartfs(0, mtd, NULL); ret = setup_smartfs(0, mtd, "/data");
if (ret < 0) if (ret < 0)
{ {
ferr("ERROR: Failed to setup smartfs\n"); ferr("ERROR: Failed to setup smartfs\n");
@ -475,7 +403,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32C3_SPIFLASH_NXFFS) #elif defined (CONFIG_ESP32C3_SPIFLASH_NXFFS)
ret = setup_nxffs(mtd, "/mnt"); ret = setup_nxffs(mtd, "/data");
if (ret < 0) if (ret < 0)
{ {
ferr("ERROR: Failed to setup nxffs\n"); ferr("ERROR: Failed to setup nxffs\n");
@ -485,7 +413,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32C3_SPIFLASH_LITTLEFS) #elif defined (CONFIG_ESP32C3_SPIFLASH_LITTLEFS)
const char *path = "/dev/esp32c3flash"; const char *path = "/dev/esp32c3flash";
ret = setup_littlefs(path, mtd, NULL, 0755); ret = setup_littlefs(path, mtd, "/data", 0755);
if (ret < 0) if (ret < 0)
{ {
ferr("ERROR: Failed to setup littlefs\n"); ferr("ERROR: Failed to setup littlefs\n");
@ -495,7 +423,7 @@ static int init_storage_partition(void)
#elif defined (CONFIG_ESP32C3_SPIFLASH_SPIFFS) #elif defined (CONFIG_ESP32C3_SPIFLASH_SPIFFS)
const char *path = "/dev/esp32c3flash"; const char *path = "/dev/esp32c3flash";
ret = setup_spiffs(path, mtd, NULL, 0755); ret = setup_spiffs(path, mtd, "/data", 0755);
if (ret < 0) if (ret < 0)
{ {
ferr("ERROR: Failed to setup spiffs\n"); ferr("ERROR: Failed to setup spiffs\n");
@ -579,14 +507,6 @@ int board_spiflash_init(void)
} }
#endif #endif
#ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM
ret = init_wifi_partition();
if (ret < 0)
{
return ret;
}
#endif
ret = init_storage_partition(); ret = init_storage_partition();
if (ret < 0) if (ret < 0)
{ {

View File

@ -18,6 +18,7 @@ CONFIG_ARCH_CHIP_ESP32C3WROOM02=y
CONFIG_ARCH_INTERRUPTSTACK=1536 CONFIG_ARCH_INTERRUPTSTACK=1536
CONFIG_ARCH_RISCV=y CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=16717 CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y CONFIG_BUILTIN=y
CONFIG_DEBUG_ASSERTIONS=y CONFIG_DEBUG_ASSERTIONS=y
@ -33,7 +34,6 @@ CONFIG_ESP32C3_SPIFLASH_SPIFFS=y
CONFIG_ESP32C3_STORAGE_MTD_OFFSET=0x110000 CONFIG_ESP32C3_STORAGE_MTD_OFFSET=0x110000
CONFIG_ESP32C3_STORAGE_MTD_SIZE=0xf0000 CONFIG_ESP32C3_STORAGE_MTD_SIZE=0xf0000
CONFIG_ESP32C3_WIFI=y CONFIG_ESP32C3_WIFI=y
CONFIG_ESP32C3_WIFI_SAVE_PARAM=y
CONFIG_ESP32C3_WIFI_STATION_SOFTAP=y CONFIG_ESP32C3_WIFI_STATION_SOFTAP=y
CONFIG_EXAMPLES_DHCPD=y CONFIG_EXAMPLES_DHCPD=y
CONFIG_EXPERIMENTAL=y CONFIG_EXPERIMENTAL=y
@ -43,13 +43,13 @@ CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072 CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_FLOATINGPOINT=y
CONFIG_LIBC_MAX_EXITFUNS=1 CONFIG_LIBC_MAX_EXITFUNS=1
CONFIG_MM_REGIONS=3 CONFIG_MM_REGIONS=3
CONFIG_NETDB_DNSCLIENT=y CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDEV_LATEINIT=y CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_PHY_IOCTL=y CONFIG_NETDEV_PHY_IOCTL=y
CONFIG_NETDEV_WIRELESS_IOCTL=y CONFIG_NETDEV_WIRELESS_IOCTL=y
CONFIG_NETUTILS_CJSON=y
CONFIG_NETUTILS_DHCPD=y CONFIG_NETUTILS_DHCPD=y
CONFIG_NET_BROADCAST=y CONFIG_NET_BROADCAST=y
CONFIG_NET_ETH_PKTSIZE=1514 CONFIG_NET_ETH_PKTSIZE=1514
@ -79,4 +79,5 @@ CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_WIRELESS=y CONFIG_WIRELESS=y
CONFIG_WIRELESS_WAPI=y CONFIG_WIRELESS_WAPI=y
CONFIG_WIRELESS_WAPI_CMDTOOL=y CONFIG_WIRELESS_WAPI_CMDTOOL=y
CONFIG_WIRELESS_WAPI_INITCONF=y
CONFIG_WIRELESS_WAPI_STACKSIZE=4096 CONFIG_WIRELESS_WAPI_STACKSIZE=4096

View File

@ -7,6 +7,7 @@
# #
# CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set # CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
CONFIG_ARCH="risc-v" CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32c3-devkit" CONFIG_ARCH_BOARD="esp32c3-devkit"
CONFIG_ARCH_BOARD_COMMON=y CONFIG_ARCH_BOARD_COMMON=y
@ -21,6 +22,8 @@ CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y CONFIG_BUILTIN=y
CONFIG_DRIVERS_IEEE80211=y CONFIG_DRIVERS_IEEE80211=y
CONFIG_DRIVERS_WIRELESS=y CONFIG_DRIVERS_WIRELESS=y
CONFIG_ESP32C3_SPIFLASH=y
CONFIG_ESP32C3_SPIFLASH_SPIFFS=y
CONFIG_ESP32C3_WIFI=y CONFIG_ESP32C3_WIFI=y
CONFIG_FS_PROCFS=y CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y CONFIG_HAVE_CXX=y
@ -36,6 +39,7 @@ CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDEV_LATEINIT=y CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_PHY_IOCTL=y CONFIG_NETDEV_PHY_IOCTL=y
CONFIG_NETDEV_WIRELESS_IOCTL=y CONFIG_NETDEV_WIRELESS_IOCTL=y
CONFIG_NETUTILS_CJSON=y
CONFIG_NETUTILS_IPERF=y CONFIG_NETUTILS_IPERF=y
CONFIG_NET_BROADCAST=y CONFIG_NET_BROADCAST=y
CONFIG_NET_ETH_PKTSIZE=1514 CONFIG_NET_ETH_PKTSIZE=1514
@ -68,4 +72,5 @@ CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_WIRELESS=y CONFIG_WIRELESS=y
CONFIG_WIRELESS_WAPI=y CONFIG_WIRELESS_WAPI=y
CONFIG_WIRELESS_WAPI_CMDTOOL=y CONFIG_WIRELESS_WAPI_CMDTOOL=y
CONFIG_WIRELESS_WAPI_INITCONF=y
CONFIG_WIRELESS_WAPI_STACKSIZE=4096 CONFIG_WIRELESS_WAPI_STACKSIZE=4096