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:
2026-07-06 09:10:37 +02:00
co-authored by Cursor
parent 9f04c4bb0c
commit ddbee70c23
63 changed files with 3771 additions and 19 deletions
@@ -0,0 +1,122 @@
/**
* @file NetBootstrap.hpp
* @brief Owns network resources for setup or STA mode for app lifetime.
*
* 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
*/
#pragma once
#include "core/ISecureStore.hpp"
#include "net/NetError.hpp"
#include "net/NetState.hpp"
#include "net/SetupWebServer.hpp"
#include "net/SoftApHost.hpp"
#include "net/StaClient.hpp"
#include <expected>
#include <optional>
namespace net {
/**
* @brief NetBootstrap — brings up SoftAP or STA plus the HTTP server.
*
* @dname NetBootstrap
* @return n/a (type)
* @pubstate Owns optional softAp_, optional sta_, and webServer_. Must
* outlive app_main; keep one instance alive for process lifetime.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class NetBootstrap {
public:
/**
* @brief start — init platform and join stored Wi-Fi or open SoftAP.
*
* @dname start
* @param store Secure store consulted for saved STA credentials.
* @return NetBootstrap on success, or a NetError.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static std::expected<NetBootstrap, NetError>
start(core::ISecureStore& store);
NetBootstrap(const NetBootstrap&) = delete;
NetBootstrap& operator=(const NetBootstrap&) = delete;
/**
* @brief NetBootstrap — move-construct from a started bootstrap.
*
* @dname NetBootstrap
* @param other Source instance; left empty after the move.
* @pubstate transfers softAp_, sta_, and webServer_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
NetBootstrap(NetBootstrap&& other) noexcept = default;
/**
* @brief operator= — move-assign from a started bootstrap.
*
* @dname operator=
* @param other Source instance; left empty after the move.
* @return Reference to this instance.
* @pubstate transfers softAp_, sta_, and webServer_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
NetBootstrap& operator=(NetBootstrap&& other) noexcept = default;
/**
* @brief ~NetBootstrap — tear down network subsystems.
*
* @dname ~NetBootstrap
* @pubstate destroys softAp_, sta_, and webServer_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
~NetBootstrap() = default;
/**
* @brief state — read the active network phase.
*
* @dname state
* @return Current NetState.
* @pubstate reads state_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] NetState state() const noexcept;
private:
NetBootstrap(std::optional<SoftApHost> softAp,
std::optional<StaClient> sta,
SetupWebServer webServer,
NetState state);
std::optional<SoftApHost> softAp_;
std::optional<StaClient> sta_;
SetupWebServer webServer_;
NetState state_;
};
} // namespace net
@@ -0,0 +1,46 @@
/**
* @file NetError.hpp
* @brief Typed errors for network bootstrap operations.
*
* 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
*/
#pragma once
namespace net {
/**
* @brief NetError — failure causes for network bring-up.
*
* @dname NetError
* @return n/a (type)
* @pubstate n/a
*
* @author Michele Bigi
* @date 2026-07-06
*/
enum class NetError {
NvsInitFailed,
NetifInitFailed,
EventLoopFailed,
WifiInitFailed,
WifiConfigFailed,
WifiStartFailed,
HttpServerStartFailed,
StaConnectTimeout,
StaConnectFailed,
StoreSaveFailed,
CredentialsNotFound,
};
} // namespace net
@@ -0,0 +1,41 @@
/**
* @file NetState.hpp
* @brief Explicit network provisioning state machine.
*
* 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
*/
#pragma once
namespace net {
/**
* @brief NetState — coarse network provisioning phase.
*
* @dname NetState
* @return n/a (type)
* @pubstate n/a
*
* Slice 1 starts in SoftApSetup; Slice 2 adds STA join after provisioning.
*
* @author Michele Bigi
* @date 2026-07-06
*/
enum class NetState {
Uninitialized,
SoftApSetup,
StaConnecting,
StaConnected,
};
} // namespace net
@@ -0,0 +1,114 @@
/**
* @file SetupWebServer.hpp
* @brief HTTP server for setup UI, health, and Wi-Fi provisioning API.
*
* 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
*/
#pragma once
#include "core/ISecureStore.hpp"
#include "net/NetError.hpp"
#include "net/NetState.hpp"
#include <expected>
struct httpd_handle;
namespace net {
/**
* @brief SetupWebServer — setup UI, health, and Wi-Fi provisioning API.
*
* @dname SetupWebServer
* @return n/a (type)
* @pubstate Owns server_ and borrows store_ while running. Routes delegate
* JSON work to the pure core.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class SetupWebServer {
public:
/**
* @brief SetupWebServer — construct an unstarted server.
*
* @dname SetupWebServer
* @pubstate clears server_, store_, and netState_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
SetupWebServer();
/**
* @brief ~SetupWebServer — stop the HTTP server if running.
*
* @dname ~SetupWebServer
* @pubstate stops server_ when non-null.
*
* @author Michele Bigi
* @date 2026-07-06
*/
~SetupWebServer();
SetupWebServer(const SetupWebServer&) = delete;
SetupWebServer& operator=(const SetupWebServer&) = delete;
/**
* @brief SetupWebServer — move-construct, transferring the handle.
*
* @dname SetupWebServer
* @param other Source server; left stopped after the move.
* @pubstate takes ownership of other.server_ and copies store pointer.
*
* @author Michele Bigi
* @date 2026-07-06
*/
SetupWebServer(SetupWebServer&& other) noexcept;
/**
* @brief operator= — move-assign, transferring the handle.
*
* @dname operator=
* @param other Source server; left stopped after the move.
* @return Reference to this instance.
* @pubstate takes ownership of other.server_ and copies store pointer.
*
* @author Michele Bigi
* @date 2026-07-06
*/
SetupWebServer& operator=(SetupWebServer&& other) noexcept;
/**
* @brief start — register routes and listen on port 80.
*
* @dname start
* @param store Secure store for POST /api/wifi persistence.
* @param netState Active network phase exposed to handlers.
* @return Ok on success, or NetError::HttpServerStartFailed.
* @pubstate writes server_, store_, and netState_ on success.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::expected<void, NetError> start(core::ISecureStore& store,
NetState netState);
private:
httpd_handle* server_;
core::ISecureStore* store_;
NetState netState_;
};
} // namespace net
@@ -0,0 +1,98 @@
/**
* @file SoftApConfig.hpp
* @brief Configuration value type for the setup SoftAP.
*
* 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
*/
#pragma once
#include <cstdint>
#include <string_view>
namespace net {
/**
* @brief SoftApConfig — immutable SoftAP parameters for first-time setup.
*
* @dname SoftApConfig
* @param ssid Broadcast SSID (non-empty).
* @param channel Wi-Fi channel (113).
* @param maxConnections Maximum associated stations.
* @return n/a (type)
* @pubstate Owns no handles; pure configuration snapshot.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class SoftApConfig {
public:
/**
* @brief setupDefault — factory for the Slice 1 setup SoftAP.
*
* @dname setupDefault
* @return SoftApConfig with SSID DigiRadio-setup.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static SoftApConfig setupDefault();
/**
* @brief ssid — read the broadcast SSID.
*
* @dname ssid
* @return SSID string view.
* @pubstate reads ssid_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::string_view ssid() const noexcept;
/**
* @brief channel — read the Wi-Fi channel.
*
* @dname channel
* @return Channel number.
* @pubstate reads channel_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::uint8_t channel() const noexcept;
/**
* @brief maxConnections — read the station limit.
*
* @dname maxConnections
* @return Maximum associated clients.
* @pubstate reads maxConnections_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::uint8_t maxConnections() const noexcept;
private:
SoftApConfig(std::string_view ssid,
std::uint8_t channel,
std::uint8_t maxConnections);
std::string_view ssid_;
std::uint8_t channel_;
std::uint8_t maxConnections_;
};
} // namespace net
@@ -0,0 +1,111 @@
/**
* @file SoftApHost.hpp
* @brief RAII wrapper that brings up an ESP32 SoftAP.
*
* 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
*/
#pragma once
#include "net/NetError.hpp"
#include "net/SoftApConfig.hpp"
#include <expected>
namespace net {
/**
* @brief SoftApHost — owns SoftAP lifecycle on the ESP32 Wi-Fi stack.
*
* @dname SoftApHost
* @param config SoftAP parameters applied on start().
* @return n/a (type)
* @pubstate Owns started_ (whether Wi-Fi AP is running). Stops AP in the
* destructor when started.
*
* Imperative shell: wraps ESP-IDF Wi-Fi calls; no business logic.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class SoftApHost {
public:
/**
* @brief SoftApHost — construct with configuration to apply on start.
*
* @dname SoftApHost
* @param config SoftAP parameters.
* @pubstate writes config_, clears started_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
explicit SoftApHost(SoftApConfig config);
/**
* @brief ~SoftApHost — stop the SoftAP if running.
*
* @dname ~SoftApHost
* @pubstate stops AP when started_ is true.
*
* @author Michele Bigi
* @date 2026-07-06
*/
~SoftApHost();
SoftApHost(const SoftApHost&) = delete;
SoftApHost& operator=(const SoftApHost&) = delete;
/**
* @brief SoftApHost — move-construct, transferring started state.
*
* @dname SoftApHost
* @param other Source host; left stopped after the move.
* @pubstate transfers config_ and started_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
SoftApHost(SoftApHost&& other) noexcept;
/**
* @brief operator= — move-assign, transferring started state.
*
* @dname operator=
* @param other Source host; left stopped after the move.
* @return Reference to this instance.
* @pubstate transfers config_ and started_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
SoftApHost& operator=(SoftApHost&& other) noexcept;
/**
* @brief start — bring up the configured SoftAP.
*
* @dname start
* @return Ok on success, or a NetError describing the failure.
* @pubstate writes started_ on success; uses config_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::expected<void, NetError> start();
private:
SoftApConfig config_;
bool started_;
};
} // namespace net
@@ -0,0 +1,109 @@
/**
* @file StaClient.hpp
* @brief RAII helper that joins a Wi-Fi network in STA mode.
*
* 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
*/
#pragma once
#include "core/WifiCredentials.hpp"
#include "net/NetError.hpp"
#include <expected>
namespace net {
/**
* @brief StaClient — connects the ESP32 to a stored Wi-Fi network.
*
* @dname StaClient
* @return n/a (type)
* @pubstate Owns connected_ (whether STA link + IP are up). Assumes
* esp_wifi_init() was already called by NetBootstrap.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class StaClient {
public:
/**
* @brief StaClient — construct an unconnected STA client.
*
* @dname StaClient
* @pubstate clears connected_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
StaClient();
/**
* @brief ~StaClient — disconnect STA if connected.
*
* @dname ~StaClient
* @pubstate stops Wi-Fi when connected_ is true.
*
* @author Michele Bigi
* @date 2026-07-06
*/
~StaClient();
StaClient(const StaClient&) = delete;
StaClient& operator=(const StaClient&) = delete;
/**
* @brief StaClient — move-construct, transferring connection state.
*
* @dname StaClient
* @param other Source client; left disconnected after the move.
* @pubstate transfers connected_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
StaClient(StaClient&& other) noexcept;
/**
* @brief operator= — move-assign, transferring connection state.
*
* @dname operator=
* @param other Source client; left disconnected after the move.
* @return Reference to this instance.
* @pubstate transfers connected_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
StaClient& operator=(StaClient&& other) noexcept;
/**
* @brief connect — join the network described by creds.
*
* @dname connect
* @param creds Validated domain credentials from ISecureStore.
* @return Ok on success, or NetError::StaConnectTimeout /
* NetError::StaConnectFailed.
* @pubstate writes connected_ on success; uses creds via Secret.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::expected<void, NetError>
connect(const core::WifiCredentials& creds);
private:
bool connected_;
};
} // namespace net