Add EEPROM EUI-48 device identity across firmware and API.

Read the factory 24AA025E48 serial after ADAU I2C boot, derive SoftAP SSID, BT name, STA hostname, and expose serialNumber on GET /api/health with graceful fallbacks.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-07 09:00:38 +02:00
co-authored by Cursor
parent 11ad6b43ad
commit cd94e36a9d
35 changed files with 999 additions and 83 deletions
+2
View File
@@ -3,6 +3,8 @@ idf_component_register(
"src/FirmwareVersion.cpp"
"src/HealthStatus.cpp"
"src/HealthStatusJson.cpp"
"src/Eui48.cpp"
"src/DeviceIdentity.cpp"
"src/Secret.cpp"
"src/WifiSsid.cpp"
"src/WifiCredentials.cpp"
@@ -0,0 +1,147 @@
/**
* @file DeviceIdentity.hpp
* @brief Per-board identity derived from the factory EUI-48 or fallbacks.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include "core/Eui48.hpp"
#include <optional>
#include <string>
#include <string_view>
namespace core {
/**
* @brief DeviceIdentity — SSID, Bluetooth name, hostname, and serial strings.
*
* @dname DeviceIdentity
* @return n/a (type)
* @pubstate Immutable after construction. unknown() uses documented fallbacks
* when the EEPROM read fails.
*
* @author Michele Bigi
* @date 2026-07-07
*/
class DeviceIdentity {
public:
/**
* @brief unknown — identity when the EUI-48 read fails.
*
* @dname unknown
* @return DeviceIdentity with fallback SSID and serial "unknown".
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] static DeviceIdentity unknown() noexcept;
/**
* @brief fromEui48 — derive network-visible names from factory EUI-48.
*
* @dname fromEui48
* @param eui Factory-programmed identifier from the 24AA025E48.
* @return DeviceIdentity with DigiRadio-<suffix> strings.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] static DeviceIdentity fromEui48(Eui48 eui);
/**
* @brief isKnown — whether a factory EUI-48 was read successfully.
*
* @dname isKnown
* @return true when fromEui48 was used.
* @pubstate reads eui_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] bool isKnown() const noexcept;
/**
* @brief serialNumber — canonical serial or "unknown".
*
* @dname serialNumber
* @return Uppercase hex serial when known.
* @pubstate reads serialNumber_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string_view serialNumber() const noexcept;
/**
* @brief softApSsid — setup SoftAP SSID for this unit.
*
* @dname softApSsid
* @return DigiRadio-<suffix> or the DigiRadio-setup fallback.
* @pubstate reads softApSsid_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string_view softApSsid() const noexcept;
/**
* @brief bluetoothName — GAP friendly name for the BT1035 module.
*
* @dname bluetoothName
* @return DigiRadio-<suffix> or plain DigiRadio when unknown.
* @pubstate reads bluetoothName_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string_view bluetoothName() const noexcept;
/**
* @brief hostname — STA hostname / mDNS label (no .local suffix).
*
* @dname hostname
* @return digiradio-<suffix> or digiradio when unknown.
* @pubstate reads hostname_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string_view hostname() const noexcept;
/**
* @brief eui48 — optional factory identifier backing this identity.
*
* @dname eui48
* @return EUI-48 when known; otherwise empty.
* @pubstate reads eui_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] const std::optional<Eui48>& eui48() const noexcept;
private:
DeviceIdentity(std::optional<Eui48> eui,
std::string serialNumber,
std::string softApSsid,
std::string bluetoothName,
std::string hostname);
std::optional<Eui48> eui_;
std::string serialNumber_;
std::string softApSsid_;
std::string bluetoothName_;
std::string hostname_;
};
} // namespace core
@@ -0,0 +1,90 @@
/**
* @file Eui48.hpp
* @brief Strong type for a factory-programmed 48-bit EUI from 24AA025E48.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
namespace core {
/**
* @brief Eui48 — immutable six-byte globally unique identifier.
*
* @dname Eui48
* @return n/a (type)
* @pubstate Owns bytes_; constructed only from a full six-byte payload.
*
* @author Michele Bigi
* @date 2026-07-07
*/
class Eui48 {
public:
/**
* @brief fromBytes — wrap a factory EUI-48 read from EEPROM.
*
* @dname fromBytes
* @param bytes Six-byte EUI-48 in datasheet order (0xFA..0xFF).
* @return Eui48 value.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] static Eui48 fromBytes(
const std::array<std::uint8_t, 6>& bytes) noexcept;
/**
* @brief bytes — read the raw EUI-48 octets.
*
* @dname bytes
* @return Reference to the six stored bytes.
* @pubstate reads bytes_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] const std::array<std::uint8_t, 6>& bytes() const noexcept;
/**
* @brief serialNumber — canonical uppercase hex serial (12 digits).
*
* @dname serialNumber
* @return Serial string without separators (e.g. AABBCCDDEEFF).
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string serialNumber() const;
/**
* @brief shortSuffix — last three bytes as six uppercase hex digits.
*
* @dname shortSuffix
* @return Suffix for SSID, Bluetooth name, and hostname.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string shortSuffix() const;
private:
explicit Eui48(std::array<std::uint8_t, 6> bytes) noexcept;
std::array<std::uint8_t, 6> bytes_;
};
} // namespace core
@@ -21,6 +21,8 @@
#include "core/CompanionChipStatus.hpp"
#include <optional>
#include <string>
#include <string_view>
namespace core {
@@ -71,8 +73,9 @@ public:
* @brief ok — build health response including companion-chip flags.
*
* @dname ok
* @param firmware Active firmware version to report.
* @param chips Si4684 / ADAU1701 / BT1035 boot snapshot.
* @param firmware Active firmware version to report.
* @param chips Si4684 / ADAU1701 / BT1035 boot snapshot.
* @param serialNumber Unit serial from EEPROM, or "unknown".
* @return HealthStatus with HealthState::Ok and chips populated.
* @pubstate none
*
@@ -80,7 +83,8 @@ public:
* @date 2026-07-06
*/
[[nodiscard]] static HealthStatus ok(FirmwareVersion firmware,
CompanionChipStatus chips);
CompanionChipStatus chips,
std::string_view serialNumber);
/**
* @brief state — read the health indicator.
@@ -119,15 +123,29 @@ public:
[[nodiscard]] const std::optional<CompanionChipStatus>& chips() const
noexcept;
/**
* @brief serialNumber — board serial from EEPROM or "unknown".
*
* @dname serialNumber
* @return Canonical serial string for /api/health.
* @pubstate reads serialNumber_.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::string_view serialNumber() const noexcept;
private:
explicit HealthStatus(HealthState state, FirmwareVersion firmware);
explicit HealthStatus(HealthState state, FirmwareVersion firmware,
CompanionChipStatus chips);
CompanionChipStatus chips,
std::string serialNumber);
HealthState state_;
FirmwareVersion firmware_;
std::optional<CompanionChipStatus> chips_;
std::string serialNumber_;
};
} // namespace core
@@ -0,0 +1,50 @@
/**
* @file IDeviceIdentitySource.hpp
* @brief Port for reading per-board identity from non-volatile storage.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include "core/DeviceIdentity.hpp"
#include "core/IdentityError.hpp"
#include <expected>
namespace core {
/**
* @brief IDeviceIdentitySource — reads DeviceIdentity from board storage.
*
* @dname IDeviceIdentitySource
* @return n/a (type)
* @pubstate Implementations live in the imperative shell (EEPROM driver).
*
* @author Michele Bigi
* @date 2026-07-07
*/
class IDeviceIdentitySource {
public:
virtual ~IDeviceIdentitySource() = default;
/**
* @brief readDeviceIdentity — load identity from the backing store.
*
* @dname readDeviceIdentity
* @return DeviceIdentity on success, or IdentityError.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] virtual std::expected<DeviceIdentity, IdentityError>
readDeviceIdentity() = 0;
};
} // namespace core
@@ -0,0 +1,32 @@
/**
* @file IdentityError.hpp
* @brief Failure causes when reading board identity from EEPROM.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
namespace core {
/**
* @brief IdentityError — EEPROM / I2C identity read failures.
*
* @dname IdentityError
* @return n/a (type)
* @pubstate n/a
*
* @author Michele Bigi
* @date 2026-07-07
*/
enum class IdentityError {
I2cFailed, ///< Bus or device transaction failed.
ReadFailed, ///< Payload length or EEPROM response invalid.
};
} // namespace core
@@ -0,0 +1,95 @@
/**
* @file DeviceIdentity.cpp
* @brief DeviceIdentity implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "core/DeviceIdentity.hpp"
namespace core {
namespace {
constexpr std::string_view kSerialUnknown = "unknown";
constexpr std::string_view kSoftApFallback = "DigiRadio-setup";
constexpr std::string_view kBluetoothFallback = "DigiRadio";
constexpr std::string_view kHostnameFallback = "digiradio";
[[nodiscard]] std::string prefixed(std::string_view prefix,
std::string_view suffix)
{
return std::string(prefix) + std::string(suffix);
}
} // namespace
DeviceIdentity DeviceIdentity::unknown() noexcept
{
return DeviceIdentity(std::nullopt,
std::string(kSerialUnknown),
std::string(kSoftApFallback),
std::string(kBluetoothFallback),
std::string(kHostnameFallback));
}
DeviceIdentity DeviceIdentity::fromEui48(Eui48 eui)
{
const std::string suffix = eui.shortSuffix();
return DeviceIdentity(eui,
eui.serialNumber(),
prefixed("DigiRadio-", suffix),
prefixed("DigiRadio-", suffix),
prefixed("digiradio-", suffix));
}
bool DeviceIdentity::isKnown() const noexcept
{
return eui_.has_value();
}
std::string_view DeviceIdentity::serialNumber() const noexcept
{
return serialNumber_;
}
std::string_view DeviceIdentity::softApSsid() const noexcept
{
return softApSsid_;
}
std::string_view DeviceIdentity::bluetoothName() const noexcept
{
return bluetoothName_;
}
std::string_view DeviceIdentity::hostname() const noexcept
{
return hostname_;
}
const std::optional<Eui48>& DeviceIdentity::eui48() const noexcept
{
return eui_;
}
DeviceIdentity::DeviceIdentity(std::optional<Eui48> eui,
std::string serialNumber,
std::string softApSsid,
std::string bluetoothName,
std::string hostname)
: eui_(std::move(eui))
, serialNumber_(std::move(serialNumber))
, softApSsid_(std::move(softApSsid))
, bluetoothName_(std::move(bluetoothName))
, hostname_(std::move(hostname))
{
}
} // namespace core
+63
View File
@@ -0,0 +1,63 @@
/**
* @file Eui48.cpp
* @brief Eui48 implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "core/Eui48.hpp"
#include <iomanip>
#include <sstream>
namespace core {
namespace {
[[nodiscard]] std::string toUpperHex(std::string_view bytes)
{
std::ostringstream out;
out << std::uppercase << std::hex << std::setfill('0');
for (const unsigned char byte : bytes) {
out << std::setw(2) << static_cast<unsigned>(byte);
}
return out.str();
}
} // namespace
Eui48 Eui48::fromBytes(const std::array<std::uint8_t, 6>& bytes) noexcept
{
return Eui48(bytes);
}
Eui48::Eui48(std::array<std::uint8_t, 6> bytes) noexcept
: bytes_(bytes)
{
}
const std::array<std::uint8_t, 6>& Eui48::bytes() const noexcept
{
return bytes_;
}
std::string Eui48::serialNumber() const
{
return toUpperHex(
std::string_view(reinterpret_cast<const char*>(bytes_.data()),
bytes_.size()));
}
std::string Eui48::shortSuffix() const
{
return toUpperHex(
std::string_view(reinterpret_cast<const char*>(bytes_.data() + 3U), 3U));
}
} // namespace core
+21 -3
View File
@@ -15,29 +15,42 @@
namespace core {
namespace {
constexpr std::string_view kSerialUnknown = "unknown";
} // namespace
HealthStatus HealthStatus::ok(FirmwareVersion firmware)
{
return HealthStatus(HealthState::Ok, std::move(firmware));
}
HealthStatus HealthStatus::ok(FirmwareVersion firmware,
CompanionChipStatus chips)
CompanionChipStatus chips,
std::string_view serialNumber)
{
return HealthStatus(HealthState::Ok, std::move(firmware), chips);
return HealthStatus(HealthState::Ok,
std::move(firmware),
chips,
std::string(serialNumber));
}
HealthStatus::HealthStatus(HealthState state, FirmwareVersion firmware)
: state_(state)
, firmware_(std::move(firmware))
, chips_(std::nullopt)
, serialNumber_(kSerialUnknown)
{
}
HealthStatus::HealthStatus(HealthState state, FirmwareVersion firmware,
CompanionChipStatus chips)
CompanionChipStatus chips,
std::string serialNumber)
: state_(state)
, firmware_(std::move(firmware))
, chips_(chips)
, serialNumber_(std::move(serialNumber))
{
}
@@ -56,4 +69,9 @@ const std::optional<CompanionChipStatus>& HealthStatus::chips() const noexcept
return chips_;
}
std::string_view HealthStatus::serialNumber() const noexcept
{
return serialNumber_;
}
} // namespace core
@@ -39,7 +39,9 @@ namespace {
std::string serializeHealthStatusJson(const HealthStatus& status)
{
std::string json = std::string("{\"status\":\"") + healthStateToken(status.state())
+ "\",\"fw\":\"" + std::string(status.firmware().value()) + "\"";
+ "\",\"fw\":\"" + std::string(status.firmware().value())
+ "\",\"serialNumber\":\""
+ std::string(status.serialNumber()) + "\"";
if (const std::optional<CompanionChipStatus>& chips = status.chips(); chips) {
json += std::string(",\"chips\":{")
+ "\"si4684\":" + (chips->si4684Ready ? "true" : "false")
@@ -15,6 +15,8 @@ add_library(digiradio_core STATIC
"${CORE_SRC_DIR}/FirmwareVersion.cpp"
"${CORE_SRC_DIR}/HealthStatus.cpp"
"${CORE_SRC_DIR}/HealthStatusJson.cpp"
"${CORE_SRC_DIR}/Eui48.cpp"
"${CORE_SRC_DIR}/DeviceIdentity.cpp"
"${CORE_SRC_DIR}/Secret.cpp"
"${CORE_SRC_DIR}/WifiSsid.cpp"
"${CORE_SRC_DIR}/WifiCredentials.cpp"
@@ -49,6 +51,10 @@ target_include_directories(digiradio_core PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
)
add_executable(device_identity_test device_identity_test.cpp)
target_link_libraries(device_identity_test PRIVATE digiradio_core)
add_test(NAME device_identity_test COMMAND device_identity_test)
add_executable(health_status_test health_status_test.cpp)
target_link_libraries(health_status_test PRIVATE digiradio_core)
add_test(NAME health_status_test COMMAND health_status_test)
@@ -0,0 +1,100 @@
/**
* @file device_identity_test.cpp
* @brief Host tests for Eui48 and DeviceIdentity derivation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "core/DeviceIdentity.hpp"
#include "core/Eui48.hpp"
#include <array>
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
[[nodiscard]] bool expectEqual(std::string_view actual,
std::string_view expected)
{
if (actual == expected) {
return true;
}
std::cerr << "expected: " << expected << "\nactual: " << actual << '\n';
return false;
}
[[nodiscard]] int runKnownEuiTest()
{
const std::array<std::uint8_t, 6> bytes = {
0x00U, 0x04U, 0xA3U, 0x12U, 0x34U, 0x56U,
};
const core::Eui48 eui = core::Eui48::fromBytes(bytes);
if (!expectEqual(eui.serialNumber(), "0004A3123456")) {
return EXIT_FAILURE;
}
if (!expectEqual(eui.shortSuffix(), "123456")) {
return EXIT_FAILURE;
}
const core::DeviceIdentity identity = core::DeviceIdentity::fromEui48(eui);
if (!identity.isKnown()) {
std::cerr << "expected known identity\n";
return EXIT_FAILURE;
}
if (!expectEqual(identity.serialNumber(), "0004A3123456")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.softApSsid(), "DigiRadio-123456")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.bluetoothName(), "DigiRadio-123456")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.hostname(), "digiradio-123456")) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
[[nodiscard]] int runUnknownIdentityTest()
{
const core::DeviceIdentity identity = core::DeviceIdentity::unknown();
if (identity.isKnown()) {
std::cerr << "expected unknown identity\n";
return EXIT_FAILURE;
}
if (!expectEqual(identity.serialNumber(), "unknown")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.softApSsid(), "DigiRadio-setup")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.bluetoothName(), "DigiRadio")) {
return EXIT_FAILURE;
}
if (!expectEqual(identity.hostname(), "digiradio")) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace
int main()
{
if (runKnownEuiTest() != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
if (runUnknownIdentityTest() != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
@@ -7,11 +7,6 @@
* 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
*/
@@ -27,18 +22,6 @@
namespace {
/**
* @brief expectEqual — assert two strings match.
*
* @dname expectEqual
* @param actual Observed value.
* @param expected Expected value.
* @return true when equal.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] bool expectEqual(const std::string& actual,
const std::string& expected)
{
@@ -49,23 +32,13 @@ namespace {
return false;
}
/**
* @brief runHealthStatusJsonTest — verify nominal JSON output.
*
* @dname runHealthStatusJsonTest
* @param none
* @return EXIT_SUCCESS or EXIT_FAILURE.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] int runHealthStatusJsonTest()
{
const core::HealthStatus status =
core::HealthStatus::ok(core::FirmwareVersion("0.1.0"));
const std::string json = core::serializeHealthStatusJson(status);
if (!expectEqual(json, R"({"status":"ok","fw":"0.1.0"})")) {
if (!expectEqual(json,
R"({"status":"ok","fw":"0.1.0","serialNumber":"unknown"})")) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
@@ -73,17 +46,6 @@ namespace {
} // namespace
/**
* @brief main — host test entry point.
*
* @dname main
* @param none
* @return EXIT_SUCCESS when all tests pass.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
int main()
{
if (runHealthStatusJsonTest() != EXIT_SUCCESS) {
@@ -96,11 +58,12 @@ int main()
.si4684Ready = true,
.adau1701Ready = true,
.bt1035Ready = true,
});
},
"0004A3123456");
const std::string chipsJson = core::serializeHealthStatusJson(withChips);
if (!expectEqual(
chipsJson,
R"({"status":"ok","fw":"0.6.0","chips":{"si4684":true,"adau1701":true,"bt1035":true}})")) {
R"({"status":"ok","fw":"0.6.0","serialNumber":"0004A3123456","chips":{"si4684":true,"adau1701":true,"bt1035":true}})")) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;