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,198 @@
|
||||
/**
|
||||
* @file NetBootstrap.cpp
|
||||
* @brief NetBootstrap 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 "net/NetBootstrap.hpp"
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
namespace net {
|
||||
|
||||
namespace {
|
||||
constexpr char kTag[] = "NetBootstrap";
|
||||
|
||||
/**
|
||||
* @brief initPlatform — one-time NVS and TCP/IP stack bring-up.
|
||||
*
|
||||
* @dname initPlatform
|
||||
* @return Ok on success, or a NetError describing the failure.
|
||||
* @pubstate initialises NVS, esp_netif, and the default event loop.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, NetError> initPlatform()
|
||||
{
|
||||
esp_err_t nvsErr = nvs_flash_init();
|
||||
if (nvsErr == ESP_ERR_NVS_NO_FREE_PAGES
|
||||
|| nvsErr == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
nvsErr = nvs_flash_init();
|
||||
}
|
||||
if (nvsErr != ESP_OK) {
|
||||
ESP_LOGE(kTag, "nvs_flash_init failed");
|
||||
return std::unexpected(NetError::NvsInitFailed);
|
||||
}
|
||||
|
||||
if (esp_netif_init() != ESP_OK) {
|
||||
ESP_LOGE(kTag, "esp_netif_init failed");
|
||||
return std::unexpected(NetError::NetifInitFailed);
|
||||
}
|
||||
|
||||
if (esp_event_loop_create_default() != ESP_OK) {
|
||||
ESP_LOGE(kTag, "esp_event_loop_create_default failed");
|
||||
return std::unexpected(NetError::EventLoopFailed);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief initWifiStack — initialise the Wi-Fi driver once.
|
||||
*
|
||||
* @dname initWifiStack
|
||||
* @return Ok on success, or NetError::WifiInitFailed.
|
||||
* @pubstate calls esp_wifi_init.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, NetError> initWifiStack()
|
||||
{
|
||||
wifi_init_config_t initCfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&initCfg) != ESP_OK) {
|
||||
ESP_LOGE(kTag, "esp_wifi_init failed");
|
||||
return std::unexpected(NetError::WifiInitFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief startSetupMode — SoftAP plus HTTP for first-time provisioning.
|
||||
*
|
||||
* @dname startSetupMode
|
||||
* @param store Store passed through to the web server routes.
|
||||
* @return NetBootstrap in SoftApSetup, or a NetError.
|
||||
* @pubstate creates default Wi-Fi AP netif and starts SoftAP.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<NetBootstrap, NetError>
|
||||
startSetupMode(core::ISecureStore& store)
|
||||
{
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
SoftApHost softAp(SoftApConfig::setupDefault());
|
||||
if (auto apResult = softAp.start(); !apResult) {
|
||||
return std::unexpected(apResult.error());
|
||||
}
|
||||
|
||||
SetupWebServer webServer;
|
||||
if (auto webResult = webServer.start(store, NetState::SoftApSetup);
|
||||
!webResult) {
|
||||
return std::unexpected(webResult.error());
|
||||
}
|
||||
|
||||
ESP_LOGI(kTag, "setup mode ready — SSID DigiRadio-setup");
|
||||
return NetBootstrap(std::move(softAp), std::nullopt, std::move(webServer),
|
||||
NetState::SoftApSetup);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief startStaMode — join stored Wi-Fi and serve HTTP on STA.
|
||||
*
|
||||
* @dname startStaMode
|
||||
* @param store Store supplying validated STA credentials.
|
||||
* @return NetBootstrap in StaConnected, or a NetError.
|
||||
* @pubstate creates default Wi-Fi STA netif and connects.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<NetBootstrap, NetError>
|
||||
startStaMode(core::ISecureStore& store)
|
||||
{
|
||||
auto credsResult = store.loadWifiCredentials();
|
||||
if (!credsResult) {
|
||||
ESP_LOGE(kTag, "stored credentials missing");
|
||||
return std::unexpected(NetError::CredentialsNotFound);
|
||||
}
|
||||
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
StaClient sta;
|
||||
if (auto staResult = sta.connect(credsResult.value()); !staResult) {
|
||||
return std::unexpected(staResult.error());
|
||||
}
|
||||
|
||||
SetupWebServer webServer;
|
||||
if (auto webResult = webServer.start(store, NetState::StaConnected);
|
||||
!webResult) {
|
||||
return std::unexpected(webResult.error());
|
||||
}
|
||||
|
||||
ESP_LOGI(kTag, "STA mode ready");
|
||||
return NetBootstrap(std::nullopt, std::move(sta), std::move(webServer),
|
||||
NetState::StaConnected);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::expected<NetBootstrap, NetError>
|
||||
NetBootstrap::start(core::ISecureStore& store)
|
||||
{
|
||||
if (auto platform = initPlatform(); !platform) {
|
||||
return std::unexpected(platform.error());
|
||||
}
|
||||
|
||||
if (auto wifi = initWifiStack(); !wifi) {
|
||||
return std::unexpected(wifi.error());
|
||||
}
|
||||
|
||||
if (store.hasWifiCredentials()) {
|
||||
auto staResult = startStaMode(store);
|
||||
if (staResult) {
|
||||
return staResult;
|
||||
}
|
||||
ESP_LOGW(kTag, "STA join failed — falling back to setup SoftAP");
|
||||
}
|
||||
|
||||
return startSetupMode(store);
|
||||
}
|
||||
|
||||
NetBootstrap::NetBootstrap(std::optional<SoftApHost> softAp,
|
||||
std::optional<StaClient> sta,
|
||||
SetupWebServer webServer,
|
||||
NetState state)
|
||||
: softAp_(std::move(softAp))
|
||||
, sta_(std::move(sta))
|
||||
, webServer_(std::move(webServer))
|
||||
, state_(state)
|
||||
{
|
||||
}
|
||||
|
||||
NetState NetBootstrap::state() const noexcept
|
||||
{
|
||||
return state_;
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
Reference in New Issue
Block a user