0
0
Fork 0

fix(nvs) use wt_nvs instead of the default 'nvs' partition

This commit is contained in:
kerms 2024-04-08 10:39:17 +08:00
parent 70b026228a
commit e547a83938
8 changed files with 25 additions and 10 deletions

2
.gitignore vendored
View File

@ -3,8 +3,10 @@ build/
tmp/
.history/
sdkconfig.old
sdkconfig
.idea/
dependencies.lock
package-lock.json
**/priv_note.md
_priv_tools/
project_components/wifi_manager/wifi_configuration.h

View File

@ -9,6 +9,8 @@
#define TAG __FILE_NAME__
void event_on_connected(ip_event_got_ip_t *event);
void ip_event_handler(void *handler_arg __attribute__((unused)),
esp_event_base_t event_base __attribute__((unused)),
int32_t event_id,
@ -19,6 +21,7 @@ void ip_event_handler(void *handler_arg __attribute__((unused)),
ip_event_got_ip_t *event = event_data;
printf("STA GOT IP : %s\n",
ip4addr_ntoa((const ip4_addr_t *) &event->ip_info.ip));
event_on_connected(event);
break;
}
case IP_EVENT_STA_LOST_IP:
@ -130,8 +133,6 @@ int wifi_event_trigger_scan(uint8_t channel, wifi_event_scan_done_cb cb, uint16_
static void reconnect_after_disco();
void event_on_connected(wifi_event_sta_connected_t *event);
void wifi_event_handler(void *handler_arg __attribute__((unused)),
esp_event_base_t event_base __attribute__((unused)),
int32_t event_id,
@ -163,7 +164,6 @@ void wifi_event_handler(void *handler_arg __attribute__((unused)),
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
#endif
event_ctx.is_connected = 1;
event_on_connected(event);
break;
}
case WIFI_EVENT_STA_DISCONNECTED: {
@ -219,7 +219,7 @@ int wifi_event_trigger_connect(uint8_t attempt, wifi_event_connect_done_cb cb, v
return err;
}
void event_on_connected(wifi_event_sta_connected_t *event)
void event_on_connected(ip_event_got_ip_t *event)
{
if (event_ctx.conn.cb) {
event_ctx.conn.cb(event_ctx.conn.arg, event);

View File

@ -4,6 +4,7 @@
#include <stdint.h>
#include <esp_event_base.h>
#include <esp_wifi_types.h>
#include <esp_netif_types.h>
void ip_event_handler(void *handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
@ -18,7 +19,7 @@ void wifi_event_set_disco_handler(void (*disconn_handler)(void));
* @brief
* @param connect_status 0: SUCCESS, OTHER: ERROR
*/
typedef void (*wifi_event_connect_done_cb)(void *arg, wifi_event_sta_connected_t *event);
typedef void (*wifi_event_connect_done_cb)(void *arg, ip_event_got_ip_t *event);
int wifi_event_trigger_connect(uint8_t attempt, wifi_event_connect_done_cb cb, void *arg);

View File

@ -30,7 +30,7 @@ typedef struct wifi_ctx_t {
uint8_t max_ap;
} scan;
struct {
wifi_event_sta_connected_t *event;
ip_event_got_ip_t *event;
uint8_t need_unlock; /* used when trigger connection from wifi_manager instead of wifi_api */
} conn;
};
@ -84,6 +84,7 @@ void wifi_manager_init(void)
}
if (set_default_sta_cred() == 0) {
ESP_LOGI(TAG, "STA connect to saved cred");
do_connect = 1;
ctx.do_fast_connect = 1;
}
@ -214,7 +215,7 @@ void *wifi_manager_get_sta_netif()
}
static void try_connect_done(void *arg, wifi_event_sta_connected_t *event)
static void try_connect_done(void *arg, ip_event_got_ip_t *event)
{
ctx.conn.event = event;
if (ctx.task) {

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#define NVS_NAMESPACE "wifi"
#define NVS_NAMESPACE "wt_wifi"
int wifi_data_get_last_conn_cred(wifi_credential_t *ap_credential)
{
@ -67,7 +67,7 @@ int wifi_save_ap_credential(wifi_credential_t *ap_credential)
}
}
err |= wt_nvs_set(handle, KEY_WIFI_STA_LAST_AP_CRED, ap_credential, sizeof(wifi_credential_t));
err = wt_nvs_set(handle, KEY_WIFI_STA_LAST_AP_CRED, ap_credential, sizeof(wifi_credential_t));
if (err) {
return err;
}

View File

@ -2,6 +2,8 @@
#include <nvs_flash.h>
#define WT_PARTITION_NAME "wt_nvs"
void wt_nvs_init()
{
// Initialize default NVS
@ -13,11 +15,20 @@ void wt_nvs_init()
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
err = nvs_flash_init_partition(WT_PARTITION_NAME);
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init_partition(WT_PARTITION_NAME);
}
ESP_ERROR_CHECK(err);
}
int wt_nvs_open(const char* namespace, nvs_handle_t *out_handle)
{
return nvs_open(namespace, NVS_READWRITE, out_handle);
return nvs_open_from_partition(WT_PARTITION_NAME, namespace, NVS_READWRITE, out_handle);
}
void wt_nvs_close(nvs_handle_t handle)