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,36 @@
|
||||
/**
|
||||
* @file FirmwareVersion.cpp
|
||||
* @brief FirmwareVersion 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 "core/FirmwareVersion.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace core {
|
||||
|
||||
FirmwareVersion::FirmwareVersion(std::string_view version)
|
||||
: version_(version)
|
||||
{
|
||||
assert(!version.empty());
|
||||
}
|
||||
|
||||
std::string_view FirmwareVersion::value() const noexcept
|
||||
{
|
||||
return version_;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @file HealthStatus.cpp
|
||||
* @brief HealthStatus 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 "core/HealthStatus.hpp"
|
||||
|
||||
namespace core {
|
||||
|
||||
HealthStatus HealthStatus::ok(FirmwareVersion firmware)
|
||||
{
|
||||
return HealthStatus(HealthState::Ok, std::move(firmware));
|
||||
}
|
||||
|
||||
HealthStatus::HealthStatus(HealthState state, FirmwareVersion firmware)
|
||||
: state_(state)
|
||||
, firmware_(std::move(firmware))
|
||||
{
|
||||
}
|
||||
|
||||
HealthState HealthStatus::state() const noexcept
|
||||
{
|
||||
return state_;
|
||||
}
|
||||
|
||||
const FirmwareVersion& HealthStatus::firmware() const noexcept
|
||||
{
|
||||
return firmware_;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @file HealthStatusJson.cpp
|
||||
* @brief JSON serialisation for HealthStatus.
|
||||
*
|
||||
* 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 "core/HealthStatusJson.hpp"
|
||||
|
||||
namespace core {
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* @brief healthStateToken — map HealthState to the API status string.
|
||||
*
|
||||
* @dname healthStateToken
|
||||
* @param state Health indicator to encode.
|
||||
* @return JSON string token without quotes (e.g. ok).
|
||||
* @pubstate none
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] const char* healthStateToken(HealthState state) noexcept
|
||||
{
|
||||
switch (state) {
|
||||
case HealthState::Ok:
|
||||
return "ok";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string serializeHealthStatusJson(const HealthStatus& status)
|
||||
{
|
||||
return std::string("{\"status\":\"") + healthStateToken(status.state())
|
||||
+ "\",\"fw\":\"" + std::string(status.firmware().value()) + "\"}";
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @file Secret.cpp
|
||||
* @brief Secret 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 "core/Secret.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace core {
|
||||
|
||||
Secret::Secret(std::string value)
|
||||
: bytes_(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
Secret::Secret(Secret&& other) noexcept
|
||||
: bytes_(std::move(other.bytes_))
|
||||
{
|
||||
other.zeroize();
|
||||
}
|
||||
|
||||
Secret& Secret::operator=(Secret&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
zeroize();
|
||||
bytes_ = std::move(other.bytes_);
|
||||
other.zeroize();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Secret::~Secret()
|
||||
{
|
||||
zeroize();
|
||||
}
|
||||
|
||||
std::size_t Secret::length() const noexcept
|
||||
{
|
||||
return bytes_.size();
|
||||
}
|
||||
|
||||
void Secret::zeroize() noexcept
|
||||
{
|
||||
if (!bytes_.empty()) {
|
||||
std::memset(bytes_.data(), 0, bytes_.size());
|
||||
bytes_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @file WifiCredentials.cpp
|
||||
* @brief WifiCredentials 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 "core/WifiCredentials.hpp"
|
||||
|
||||
namespace core {
|
||||
|
||||
bool WifiCredentials::isPasswordValid(std::string_view raw) noexcept
|
||||
{
|
||||
if (raw.empty()) {
|
||||
return true;
|
||||
}
|
||||
return raw.size() >= 8 && raw.size() <= kMaxPasswordLength;
|
||||
}
|
||||
|
||||
WifiCredentials::WifiCredentials(WifiSsid ssid, Secret password)
|
||||
: ssid_(std::move(ssid))
|
||||
, password_(std::move(password))
|
||||
{
|
||||
}
|
||||
|
||||
const WifiSsid& WifiCredentials::ssid() const noexcept
|
||||
{
|
||||
return ssid_;
|
||||
}
|
||||
|
||||
const Secret& WifiCredentials::password() const noexcept
|
||||
{
|
||||
return password_;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* @file WifiProvisionJson.cpp
|
||||
* @brief Wi-Fi provisioning JSON parse/serialise 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 "core/WifiProvisionJson.hpp"
|
||||
|
||||
namespace core {
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* @brief extractJsonString — read a quoted string value for a key.
|
||||
*
|
||||
* @dname extractJsonString
|
||||
* @param json Full JSON object text.
|
||||
* @param key Field name without quotes (e.g. ssid).
|
||||
* @return Decoded string view into json, or empty on failure.
|
||||
* @pubstate none
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::string_view extractJsonString(std::string_view json,
|
||||
std::string_view key)
|
||||
{
|
||||
const std::string needle = std::string("\"") + std::string(key) + "\":\"";
|
||||
const std::size_t start = json.find(needle);
|
||||
if (start == std::string_view::npos) {
|
||||
return {};
|
||||
}
|
||||
const std::size_t valueStart = start + needle.size();
|
||||
const std::size_t valueEnd = json.find('"', valueStart);
|
||||
if (valueEnd == std::string_view::npos) {
|
||||
return {};
|
||||
}
|
||||
return json.substr(valueStart, valueEnd - valueStart);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::expected<WifiCredentials, ParseError>
|
||||
parseWifiProvisionJson(std::string_view json)
|
||||
{
|
||||
if (json.find('{') == std::string_view::npos) {
|
||||
return std::unexpected(ParseError::InvalidJson);
|
||||
}
|
||||
|
||||
const std::string_view ssidRaw = extractJsonString(json, "ssid");
|
||||
if (ssidRaw.empty() && json.find("\"ssid\"") == std::string_view::npos) {
|
||||
return std::unexpected(ParseError::MissingField);
|
||||
}
|
||||
if (!WifiSsid::isValid(ssidRaw)) {
|
||||
return std::unexpected(ParseError::InvalidSsid);
|
||||
}
|
||||
|
||||
std::string_view passwordRaw;
|
||||
if (json.find("\"password\"") != std::string_view::npos) {
|
||||
passwordRaw = extractJsonString(json, "password");
|
||||
}
|
||||
if (!WifiCredentials::isPasswordValid(passwordRaw)) {
|
||||
return std::unexpected(ParseError::InvalidPassword);
|
||||
}
|
||||
|
||||
return WifiCredentials(WifiSsid(ssidRaw), Secret(std::string(passwordRaw)));
|
||||
}
|
||||
|
||||
std::string serializeWifiProvisionSavedJson(unsigned rebootInSec)
|
||||
{
|
||||
return std::string("{\"status\":\"saved\",\"reboot_in_sec\":")
|
||||
+ std::to_string(rebootInSec) + "}";
|
||||
}
|
||||
|
||||
std::string serializeWifiProvisionErrorJson(std::string_view reason)
|
||||
{
|
||||
return std::string("{\"status\":\"error\",\"reason\":\"") + std::string(reason)
|
||||
+ "\"}";
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file WifiSsid.cpp
|
||||
* @brief WifiSsid 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 "core/WifiSsid.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace core {
|
||||
|
||||
bool WifiSsid::isValid(std::string_view raw) noexcept
|
||||
{
|
||||
return !raw.empty() && raw.size() <= kMaxLength;
|
||||
}
|
||||
|
||||
WifiSsid::WifiSsid(std::string_view raw)
|
||||
: ssid_(raw)
|
||||
{
|
||||
assert(isValid(raw));
|
||||
}
|
||||
|
||||
std::string_view WifiSsid::value() const noexcept
|
||||
{
|
||||
return ssid_;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
Reference in New Issue
Block a user