feat: HAL variant system, OTA improvements, NVS migration scaffold

- Add variants/vesper.hpp and variants/agnus.hpp with HAS_SD_CARD, HAS_RTC,
  HAS_PCF8574, OUTPUT_COUNT_MAX, HW_FAMILY capability flags
- Add agnus-v1 and agnus-mini-v1 PlatformIO environments with -include injection
- Add src/NvsMigration/ scaffold for future NVS schema versioning
- FirmwareHandler: OTA flow improvements and header updates
- OTAManager: FastAPI integration, semver handling improvements
- ConfigManager: refactoring groundwork for NVS-only variant support
- CommunicationRouter: minor fix
- provisioning/nvs_lab_device.csv: fix hw_type to VS, update device id fields
- docs/dev/ota-telemetry-api.md: update API spec

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 11:39:11 +02:00
co-authored by Claude Sonnet 4.6
parent d4d8b1c380
commit 37b65cc15a
14 changed files with 561 additions and 96 deletions
+35 -29
View File
@@ -10,10 +10,12 @@
// NVS namespace for device identity storage
const char* ConfigManager::NVS_NAMESPACE = "device_id";
// NVS keys for device identity
static const char* NVS_DEVICE_UID_KEY = "device_uid";
static const char* NVS_HW_TYPE_KEY = "hw_type";
static const char* NVS_HW_VERSION_KEY = "hw_version";
// NVS keys for device identity (new schema — written by backend provisioning tool)
// Old keys (device_uid, hw_type, hw_version) are migrated to these on first boot
// by NvsMigration::run() which runs before ConfigManager::begin().
static const char* NVS_SERIAL_KEY = "serial";
static const char* NVS_HW_FAMILY_KEY = "hw_family";
static const char* NVS_HW_REVISION_KEY = "hw_revision";
ConfigManager::ConfigManager() {
// Initialize with empty defaults - everything will be loaded/generated in begin()
@@ -28,12 +30,12 @@ void ConfigManager::initializeCleanDefaults() {
generateNetworkIdentifiers();
// Set MQTT user to deviceUID for unique identification
mqttConfig.user = deviceConfig.deviceUID;
mqttConfig.user = deviceConfig.serial;
// Derive MQTT password from deviceUID using HMAC-SHA256
// MQTT_SECRET is compiled in via build flag -DMQTT_SECRET="..."
// Must match the MQTT_SECRET in the backend .env exactly.
mqttConfig.password = computeMqttPassword(deviceConfig.deviceUID);
mqttConfig.password = computeMqttPassword(deviceConfig.serial);
LOG_INFO(TAG, "🔑 MQTT auth — user: %s, password: %s", mqttConfig.user.c_str(), mqttConfig.password.c_str());
LOG_INFO(TAG, "🔑 MQTT_SECRET prefix: %.8s...", MQTT_SECRET);
@@ -43,8 +45,8 @@ void ConfigManager::initializeCleanDefaults() {
void ConfigManager::generateNetworkIdentifiers() {
networkConfig.hostname = "BellSystems-" + deviceConfig.deviceUID;
networkConfig.apSsid = "BellSystems-Setup-" + deviceConfig.deviceUID;
networkConfig.hostname = "BellSystems-" + deviceConfig.serial;
networkConfig.apSsid = "BellSystems-Setup-" + deviceConfig.serial;
LOG_DEBUG(TAG, "ConfigManager - Generated hostname: %s, AP SSID: %s",
@@ -66,7 +68,7 @@ bool ConfigManager::begin() {
if (!initializeNVS()) {
LOG_ERROR(TAG, "ConfigManager - ❌ NVS initialization failed, using empty defaults");
} else {
// Load device identity from NVS (deviceUID, hwType, hwVersion)
// Load device identity from NVS (serial, hwFamily, hwRevision)
loadDeviceIdentityFromNVS();
}
@@ -126,8 +128,8 @@ bool ConfigManager::begin() {
saveGeneralConfig();
}
LOG_INFO(TAG, "ConfigManager - ✅ Initialization Complete ! UID: %s, Hostname: %s",
deviceConfig.deviceUID.c_str(), networkConfig.hostname.c_str());
LOG_INFO(TAG, "ConfigManager - ✅ Initialization Complete ! Serial: %s, Hostname: %s",
deviceConfig.serial.c_str(), networkConfig.hostname.c_str());
return true;
}
@@ -165,12 +167,12 @@ bool ConfigManager::loadDeviceIdentityFromNVS() {
}
// Read factory-set device identity from NVS (READ-ONLY)
deviceConfig.deviceUID = readNVSString(NVS_DEVICE_UID_KEY, "");
deviceConfig.hwType = readNVSString(NVS_HW_TYPE_KEY, "");
deviceConfig.hwVersion = readNVSString(NVS_HW_VERSION_KEY, "");
deviceConfig.serial = readNVSString(NVS_SERIAL_KEY, "");
deviceConfig.hwFamily = readNVSString(NVS_HW_FAMILY_KEY, "");
deviceConfig.hwRevision = readNVSString(NVS_HW_REVISION_KEY, "");
// Validate that factory identity exists
if (deviceConfig.deviceUID.isEmpty() || deviceConfig.hwType.isEmpty() || deviceConfig.hwVersion.isEmpty()) {
if (deviceConfig.serial.isEmpty() || deviceConfig.hwFamily.isEmpty() || deviceConfig.hwRevision.isEmpty()) {
LOG_ERROR(TAG, "═══════════════════════════════════════════════════════════════════════════");
LOG_ERROR(TAG, " ⚠️ CRITICAL: DEVICE IDENTITY NOT FOUND IN NVS");
LOG_ERROR(TAG, " ⚠️ This device has NOT been factory-programmed!");
@@ -181,9 +183,9 @@ bool ConfigManager::loadDeviceIdentityFromNVS() {
LOG_INFO(TAG, "═══════════════════════════════════════════════════════════════════════════");
LOG_INFO(TAG, " 🏭 FACTORY DEVICE IDENTITY LOADED FROM NVS (READ-ONLY)");
LOG_INFO(TAG, " 🆔 Device UID: %s", deviceConfig.deviceUID.c_str());
LOG_INFO(TAG, " 🔧 Hardware Type: %s", deviceConfig.hwType.c_str());
LOG_INFO(TAG, " 📐 Hardware Version: %s", deviceConfig.hwVersion.c_str());
LOG_INFO(TAG, " 🆔 Serial: %s", deviceConfig.serial.c_str());
LOG_INFO(TAG, " 🔧 HW Family: %s", deviceConfig.hwFamily.c_str());
LOG_INFO(TAG, " 📐 HW Revision: %s", deviceConfig.hwRevision.c_str());
LOG_INFO(TAG, " 🔒 These values are PERMANENT and cannot be changed by production firmware");
LOG_INFO(TAG, "═══════════════════════════════════════════════════════════════════════════");
@@ -191,7 +193,7 @@ bool ConfigManager::loadDeviceIdentityFromNVS() {
}
// REMOVED: saveDeviceIdentityToNVS() - Production firmware MUST NOT write device identity
// Device identity (UID, hwType, hwVersion) is factory-set ONLY and stored in NVS by factory firmware
// Device identity (serial, hwFamily, hwRevision) is factory-set ONLY and stored in NVS by backend provisioning
// Production firmware reads these values once at boot and keeps them in RAM
String ConfigManager::readNVSString(const char* key, const String& defaultValue) {
@@ -272,13 +274,13 @@ bool ConfigManager::isHealthy() const {
return false;
}
if (deviceConfig.deviceUID.isEmpty()) {
LOG_VERBOSE(TAG, "ConfigManager - ⚠️ Unhealthy - Device UID not set (factory configuration required)");
if (deviceConfig.serial.isEmpty()) {
LOG_VERBOSE(TAG, "ConfigManager - ⚠️ Unhealthy - Serial not set (factory configuration required)");
return false;
}
if (deviceConfig.hwType.isEmpty()) {
LOG_VERBOSE(TAG, "ConfigManager - ⚠️ Unhealthy - Hardware type not set (factory configuration required)");
if (deviceConfig.hwFamily.isEmpty()) {
LOG_VERBOSE(TAG, "ConfigManager - ⚠️ Unhealthy - HW family not set (factory configuration required)");
return false;
}
@@ -1099,9 +1101,9 @@ String ConfigManager::getAllSettingsAsJson() const {
// Device info
JsonObject device = doc.createNestedObject("device");
device["uid"] = deviceConfig.deviceUID;
device["hwType"] = deviceConfig.hwType;
device["hwVersion"] = deviceConfig.hwVersion;
device["serial"] = deviceConfig.serial;
device["hw_family"] = deviceConfig.hwFamily;
device["hw_revision"] = deviceConfig.hwRevision;
device["fwVersion"] = FW_VERSION;
// Network config
@@ -1246,10 +1248,13 @@ bool ConfigManager::loadGeneralConfig() {
generalConfig.mqttEnabled = doc["mqttEnabled"].as<bool>();
mqttConfig.enabled = generalConfig.mqttEnabled; // Sync with mqttConfig
}
if (doc.containsKey("otaChannel")) {
generalConfig.otaChannel = doc["otaChannel"].as<String>();
}
LOG_DEBUG(TAG, "ConfigManager - General config loaded - Serial log level: %d, SD log level: %d, MQTT log level: %d, MQTT enabled: %s",
LOG_DEBUG(TAG, "ConfigManager - General config loaded - Serial log level: %d, SD log level: %d, MQTT log level: %d, MQTT enabled: %s, OTA channel: %s",
generalConfig.serialLogLevel, generalConfig.sdLogLevel, generalConfig.mqttLogLevel,
generalConfig.mqttEnabled ? "true" : "false");
generalConfig.mqttEnabled ? "true" : "false", generalConfig.otaChannel.c_str());
return true;
}
@@ -1261,6 +1266,7 @@ bool ConfigManager::saveGeneralConfig() {
doc["sdLogLevel"] = generalConfig.sdLogLevel;
doc["mqttLogLevel"] = generalConfig.mqttLogLevel;
doc["mqttEnabled"] = generalConfig.mqttEnabled;
doc["otaChannel"] = generalConfig.otaChannel;
char buffer[256];
size_t len = serializeJson(doc, buffer, sizeof(buffer));