forked from tonyp7/esp32-wifi-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wifi_nvs.c
96 lines (68 loc) · 2.17 KB
/
wifi_nvs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <string.h>
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "wifi_nvs.h"
static const char wifi_manager_nvs_namespace[] = "espwifimgr";
static const char TAG[] = "WIFIMGRSET";
esp_err_t wifi_manager_clear_sta_config() {
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGD(TAG, "wifi_manager: clearing sta_config");
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
if (esp_err != ESP_OK) return esp_err;
esp_err = nvs_erase_all(handle);
if (esp_err != ESP_OK) return esp_err;
esp_err = nvs_commit(handle);
if (esp_err != ESP_OK) return esp_err;
nvs_close(handle);
return ESP_OK;
}
esp_err_t wifi_manager_save_sta_config(wifi_config_t* config) {
nvs_handle handle;
esp_err_t esp_err;
ESP_LOGD(TAG, "wifi_manager: About to save config to flash");
if(config){
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
if (esp_err != ESP_OK) return esp_err;
esp_err = nvs_set_blob(handle, "ssid", config->sta.ssid, 32);
if (esp_err != ESP_OK) return esp_err;
esp_err = nvs_set_blob(handle, "password", config->sta.password, 64);
if (esp_err != ESP_OK) return esp_err;
esp_err = nvs_commit(handle);
if (esp_err != ESP_OK) return esp_err;
nvs_close(handle);
ESP_LOGD(TAG, "ssid:%s password:%s", config->sta.ssid, config->sta.password);
}
return ESP_OK;
}
bool wifi_manager_load_sta_config(wifi_config_t* config) {
nvs_handle handle;
esp_err_t esp_err;
if (nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle) != ESP_OK){
return false;
}
/* allocate buffer */
size_t sz;
uint8_t *buff = malloc(sizeof(uint8_t) * 128);
memset(buff, 0x00, sizeof(sz));
/* ssid */
sz = sizeof(config->sta.ssid);
esp_err = nvs_get_blob(handle, "ssid", buff, &sz);
if(esp_err != ESP_OK){
free(buff);
return false;
}
memcpy(config->sta.ssid, buff, sz);
/* password */
sz = sizeof(config->sta.password);
esp_err = nvs_get_blob(handle, "password", buff, &sz);
if(esp_err != ESP_OK){
free(buff);
return false;
}
memcpy(config->sta.password, buff, sz);
free(buff);
nvs_close(handle);
return true;
}