Add firmware slices 1–2: skeleton and Wi-Fi provisioning
Implement ESP-IDF walking skeleton with SoftAP, health API, and host tests, then Slice 2 ISecureStore/NvsSecureStore, STA join, POST /api/wifi, and the provisioning web UI. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
idf_component_register(
|
||||
SRCS "src/NvsSecureStore.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES core nvs_flash
|
||||
)
|
||||
|
||||
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_23)
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* @file NvsSecureStore.hpp
|
||||
* @brief NVS-backed ISecureStore for Wi-Fi credentials (Slice 2).
|
||||
*
|
||||
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
|
||||
*
|
||||
* Copyright 2026 Michele Bigi
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Production builds should enable NVS encryption (nvs_keys partition in
|
||||
* partitions.csv) per ESP-IDF security docs; this slice uses plain NVS
|
||||
* for development bring-up.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "core/ISecureStore.hpp"
|
||||
|
||||
namespace secure_store {
|
||||
|
||||
/**
|
||||
* @brief NvsSecureStore — persists credentials in an NVS namespace.
|
||||
*
|
||||
* @dname NvsSecureStore
|
||||
* @return n/a (type)
|
||||
* @pubstate Opens namespace digiradio on each operation. Passwords are
|
||||
* stored as NVS strings and never logged.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
class NvsSecureStore final : public core::ISecureStore {
|
||||
public:
|
||||
/**
|
||||
* @brief NvsSecureStore — default-construct the store accessor.
|
||||
*
|
||||
* @dname NvsSecureStore
|
||||
* @pubstate none
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
NvsSecureStore() = default;
|
||||
|
||||
/**
|
||||
* @brief hasWifiCredentials — check whether STA creds are stored.
|
||||
*
|
||||
* @dname hasWifiCredentials
|
||||
* @return true when SSID key exists in NVS.
|
||||
* @pubstate reads NVS namespace digiradio.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] bool hasWifiCredentials() const override;
|
||||
|
||||
/**
|
||||
* @brief saveWifiCredentials — persist validated STA credentials.
|
||||
*
|
||||
* @dname saveWifiCredentials
|
||||
* @param creds Validated credentials to persist.
|
||||
* @return Ok on success, or StoreError::IoFailed.
|
||||
* @pubstate writes NVS keys wifi_ssid and wifi_pwd.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::StoreError>
|
||||
saveWifiCredentials(const core::WifiCredentials& creds) override;
|
||||
|
||||
/**
|
||||
* @brief loadWifiCredentials — read stored STA credentials.
|
||||
*
|
||||
* @dname loadWifiCredentials
|
||||
* @return WifiCredentials on success, or a StoreError.
|
||||
* @pubstate reads NVS namespace digiradio.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<core::WifiCredentials, core::StoreError>
|
||||
loadWifiCredentials() const override;
|
||||
|
||||
/**
|
||||
* @brief clearWifiCredentials — erase stored STA credentials.
|
||||
*
|
||||
* @dname clearWifiCredentials
|
||||
* @return Ok on success, or StoreError::IoFailed.
|
||||
* @pubstate erases wifi_ssid and wifi_pwd from NVS.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::StoreError>
|
||||
clearWifiCredentials() override;
|
||||
};
|
||||
|
||||
} // namespace secure_store
|
||||
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* @file NvsSecureStore.cpp
|
||||
* @brief NvsSecureStore implementation.
|
||||
*
|
||||
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
|
||||
*
|
||||
* Copyright 2026 Michele Bigi
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
|
||||
#include "secure_store/NvsSecureStore.hpp"
|
||||
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace secure_store {
|
||||
|
||||
namespace {
|
||||
constexpr char kNamespace[] = "digiradio";
|
||||
constexpr char kSsidKey[] = "wifi_ssid";
|
||||
constexpr char kPasswordKey[] = "wifi_pwd";
|
||||
} // namespace
|
||||
|
||||
bool NvsSecureStore::hasWifiCredentials() const
|
||||
{
|
||||
nvs_handle_t handle = 0;
|
||||
if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::size_t ssidLen = 0;
|
||||
const esp_err_t ssidErr =
|
||||
nvs_get_str(handle, kSsidKey, nullptr, &ssidLen);
|
||||
nvs_close(handle);
|
||||
|
||||
return ssidErr == ESP_OK && ssidLen > 1;
|
||||
}
|
||||
|
||||
std::expected<void, core::StoreError>
|
||||
NvsSecureStore::saveWifiCredentials(const core::WifiCredentials& creds)
|
||||
{
|
||||
nvs_handle_t handle = 0;
|
||||
if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
|
||||
const std::string ssid(creds.ssid().value());
|
||||
std::string password;
|
||||
creds.password().usePlaintext(
|
||||
[&](std::string_view pwd) { password.assign(pwd); });
|
||||
|
||||
esp_err_t err = nvs_set_str(handle, kSsidKey, ssid.c_str());
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_set_str(handle, kPasswordKey, password.c_str());
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(handle);
|
||||
}
|
||||
nvs_close(handle);
|
||||
|
||||
for (char& ch : password) {
|
||||
ch = '\0';
|
||||
}
|
||||
|
||||
if (err != ESP_OK) {
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<core::WifiCredentials, core::StoreError>
|
||||
NvsSecureStore::loadWifiCredentials() const
|
||||
{
|
||||
nvs_handle_t handle = 0;
|
||||
if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK) {
|
||||
return std::unexpected(core::StoreError::NotFound);
|
||||
}
|
||||
|
||||
std::size_t ssidLen = 0;
|
||||
if (nvs_get_str(handle, kSsidKey, nullptr, &ssidLen) != ESP_OK
|
||||
|| ssidLen == 0) {
|
||||
nvs_close(handle);
|
||||
return std::unexpected(core::StoreError::NotFound);
|
||||
}
|
||||
|
||||
std::vector<char> ssidBuf(ssidLen);
|
||||
std::size_t pwdLen = 0;
|
||||
if (nvs_get_str(handle, kSsidKey, ssidBuf.data(), &ssidLen) != ESP_OK) {
|
||||
nvs_close(handle);
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
|
||||
if (nvs_get_str(handle, kPasswordKey, nullptr, &pwdLen) != ESP_OK) {
|
||||
pwdLen = 0;
|
||||
}
|
||||
|
||||
std::string password;
|
||||
if (pwdLen > 0) {
|
||||
std::vector<char> pwdBuf(pwdLen);
|
||||
if (nvs_get_str(handle, kPasswordKey, pwdBuf.data(), &pwdLen) != ESP_OK) {
|
||||
nvs_close(handle);
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
password.assign(pwdBuf.data());
|
||||
}
|
||||
|
||||
nvs_close(handle);
|
||||
|
||||
const std::string_view ssidView(ssidBuf.data());
|
||||
if (!core::WifiSsid::isValid(ssidView)) {
|
||||
return std::unexpected(core::StoreError::InvalidData);
|
||||
}
|
||||
|
||||
return core::WifiCredentials(core::WifiSsid(ssidView),
|
||||
core::Secret(std::move(password)));
|
||||
}
|
||||
|
||||
std::expected<void, core::StoreError> NvsSecureStore::clearWifiCredentials()
|
||||
{
|
||||
nvs_handle_t handle = 0;
|
||||
if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_erase_key(handle, kSsidKey);
|
||||
if (err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
err = nvs_erase_key(handle, kPasswordKey);
|
||||
}
|
||||
if (err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
err = nvs_commit(handle);
|
||||
}
|
||||
nvs_close(handle);
|
||||
|
||||
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) {
|
||||
return std::unexpected(core::StoreError::IoFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace secure_store
|
||||
Reference in New Issue
Block a user