bolar_2 backup
This commit is contained in:
229
main/connectivity/wifi_credential_store.cc
Normal file
229
main/connectivity/wifi_credential_store.cc
Normal file
@@ -0,0 +1,229 @@
|
||||
#include "connectivity/wifi_credential_store.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "WifiCredentials2";
|
||||
constexpr const char* kNvsNamespace = "wifi";
|
||||
|
||||
void MakeKey(const char* prefix, size_t index, char* output, size_t size)
|
||||
{
|
||||
if (index == 0) {
|
||||
std::snprintf(output, size, "%s", prefix);
|
||||
} else {
|
||||
std::snprintf(output, size, "%s%u", prefix,
|
||||
static_cast<unsigned>(index));
|
||||
}
|
||||
}
|
||||
|
||||
bool SameSsid(const WifiCredential& credential, const char* ssid)
|
||||
{
|
||||
return ssid != nullptr &&
|
||||
std::strncmp(
|
||||
credential.ssid.data(), ssid, credential.ssid.size()) == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
size_t NvsWifiCredentialStore::Load(
|
||||
WifiCredential* output,
|
||||
size_t capacity) const
|
||||
{
|
||||
if (output == nullptr || capacity == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
nvs_handle_t handle = 0;
|
||||
const esp_err_t open_result =
|
||||
nvs_open(kNvsNamespace, NVS_READONLY, &handle);
|
||||
if (open_result == ESP_ERR_NVS_NOT_FOUND) {
|
||||
return 0;
|
||||
}
|
||||
if (open_result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "open namespace failed: %s",
|
||||
esp_err_to_name(open_result));
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
for (size_t index = 0;
|
||||
index < kMaxSavedWifiCredentials && count < capacity;
|
||||
++index) {
|
||||
char ssid_key[16]{};
|
||||
char password_key[16]{};
|
||||
MakeKey("ssid", index, ssid_key, sizeof(ssid_key));
|
||||
MakeKey("password", index, password_key, sizeof(password_key));
|
||||
|
||||
WifiCredential credential;
|
||||
size_t ssid_size = credential.ssid.size();
|
||||
size_t password_size = credential.password.size();
|
||||
if (nvs_get_str(
|
||||
handle, ssid_key, credential.ssid.data(), &ssid_size) !=
|
||||
ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
if (nvs_get_str(
|
||||
handle, password_key, credential.password.data(),
|
||||
&password_size) != ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
if (!credential.IsValid()) {
|
||||
ESP_LOGW(kTag, "ignored invalid credential slot=%u",
|
||||
static_cast<unsigned>(index));
|
||||
continue;
|
||||
}
|
||||
output[count++] = credential;
|
||||
}
|
||||
|
||||
nvs_close(handle);
|
||||
ESP_LOGI(kTag, "loaded %u saved WiFi credential(s)",
|
||||
static_cast<unsigned>(count));
|
||||
return count;
|
||||
}
|
||||
|
||||
bool NvsWifiCredentialStore::SaveAndPromote(
|
||||
const WifiCredential& credential)
|
||||
{
|
||||
if (!credential.IsValid()) {
|
||||
ESP_LOGE(kTag, "refused invalid WiFi credential");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::array<WifiCredential, kMaxSavedWifiCredentials> credentials{};
|
||||
size_t count = Load(credentials.data(), credentials.size());
|
||||
size_t existing = count;
|
||||
for (size_t index = 0; index < count; ++index) {
|
||||
if (SameSsid(credentials[index], credential.ssid.data())) {
|
||||
existing = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing < count) {
|
||||
for (size_t index = existing; index > 0; --index) {
|
||||
credentials[index] = credentials[index - 1];
|
||||
}
|
||||
} else {
|
||||
count = std::min(count + 1U, credentials.size());
|
||||
for (size_t index = count - 1; index > 0; --index) {
|
||||
credentials[index] = credentials[index - 1];
|
||||
}
|
||||
}
|
||||
credentials[0] = credential;
|
||||
|
||||
if (!SaveAll(credentials.data(), count)) {
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(kTag, "saved WiFi credential ssid=%s",
|
||||
credential.ssid.data());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NvsWifiCredentialStore::Remove(const char* ssid)
|
||||
{
|
||||
if (ssid == nullptr || ssid[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::array<WifiCredential, kMaxSavedWifiCredentials> credentials{};
|
||||
size_t count = Load(credentials.data(), credentials.size());
|
||||
size_t remove_index = count;
|
||||
for (size_t index = 0; index < count; ++index) {
|
||||
if (SameSsid(credentials[index], ssid)) {
|
||||
remove_index = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (remove_index == count) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t index = remove_index; index + 1 < count; ++index) {
|
||||
credentials[index] = credentials[index + 1];
|
||||
}
|
||||
--count;
|
||||
if (!SaveAll(credentials.data(), count)) {
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(kTag, "removed WiFi credential ssid=%s", ssid);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NvsWifiCredentialStore::Clear()
|
||||
{
|
||||
return SaveAll(nullptr, 0);
|
||||
}
|
||||
|
||||
bool NvsWifiCredentialStore::SaveAll(
|
||||
const WifiCredential* credentials,
|
||||
size_t count) const
|
||||
{
|
||||
if ((credentials == nullptr && count != 0) ||
|
||||
count > kMaxSavedWifiCredentials) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nvs_handle_t handle = 0;
|
||||
esp_err_t result =
|
||||
nvs_open(kNvsNamespace, NVS_READWRITE, &handle);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "open writable namespace failed: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < kMaxSavedWifiCredentials; ++index) {
|
||||
char ssid_key[16]{};
|
||||
char password_key[16]{};
|
||||
MakeKey("ssid", index, ssid_key, sizeof(ssid_key));
|
||||
MakeKey("password", index, password_key, sizeof(password_key));
|
||||
if (index < count) {
|
||||
result = nvs_set_str(
|
||||
handle, ssid_key, credentials[index].ssid.data());
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_set_str(
|
||||
handle, password_key,
|
||||
credentials[index].password.data());
|
||||
}
|
||||
} else {
|
||||
const esp_err_t ssid_result = nvs_erase_key(handle, ssid_key);
|
||||
const esp_err_t password_result =
|
||||
nvs_erase_key(handle, password_key);
|
||||
if (ssid_result != ESP_OK &&
|
||||
ssid_result != ESP_ERR_NVS_NOT_FOUND) {
|
||||
result = ssid_result;
|
||||
}
|
||||
if (password_result != ESP_OK &&
|
||||
password_result != ESP_ERR_NVS_NOT_FOUND) {
|
||||
result = password_result;
|
||||
}
|
||||
}
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "write credential slot=%u failed: %s",
|
||||
static_cast<unsigned>(index),
|
||||
esp_err_to_name(result));
|
||||
nvs_close(handle);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = nvs_commit(handle);
|
||||
nvs_close(handle);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "commit credentials failed: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user