feat(SystemHandler,ConfigManager): add system.set_identity sysadmin command (F-060)

Device identity (serial/hw_family/hw_revision) has been read-only from
production firmware since NVS provisioning — serial derives the MQTT
password and hw_family/hw_revision select the OTA channel, so an
uncontrolled write path could desync MQTT auth or brick OTA to the
wrong hardware. This adds one narrow, deliberate exception: an
MQTT-only system.set_identity command for correcting a
mis-provisioned unit or reassigning a serial after a board swap,
without a USB/serial reflash.

Rejected over WebSocket/HTTP/UART by design (same pattern as
identify's WebSocket-only check); requires all three fields together
to avoid partial-identity states; forces a reboot after a successful
write since MQTT credentials/hostname/AP SSID are derived from serial
only once at boot. Firmware does not update the backend device record
itself — that's the caller's (console's) responsibility in the same
operation.

Also includes docs/README.md's rule 6 (console API reference sync
requirement) from an earlier uncommitted session, which this change
follows.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-20 20:09:39 +03:00
co-authored by Claude Sonnet 5
parent 28b175b4fc
commit 64a8bc2b88
7 changed files with 174 additions and 8 deletions
@@ -29,7 +29,8 @@ std::vector<String> SystemHandler::commands() const {
"system.get_settings", // backward-compat alias — routes to handleGetConfig
"system.get_device_info", "system.health",
"system.get_all", "system.get_telemetry",
"system.factory_reset", "system.restart"
"system.factory_reset", "system.restart",
"system.set_identity"
};
}
@@ -46,6 +47,7 @@ CommandResult SystemHandler::execute(const CommandEnvelope& envelope) {
if (envelope.command == "system.get_telemetry") return handleGetTelemetry(envelope);
if (envelope.command == "system.factory_reset") return handleFactoryReset(envelope);
if (envelope.command == "system.restart") return handleRestart(envelope);
if (envelope.command == "system.set_identity") return handleSetIdentity(envelope);
return CommandResult::err(envelope.command, "Unknown system command");
}
@@ -313,4 +315,47 @@ CommandResult SystemHandler::handleRestart(const CommandEnvelope& e) {
return CommandResult::ok("system.restart", "Device will restart in 2 seconds");
}
CommandResult SystemHandler::handleSetIdentity(const CommandEnvelope& e) {
// Sysadmin-only override of factory-set device identity. Restricted to MQTT so it can
// only be triggered from the backend/console, which is expected to update its own
// device record (and re-derive the MQTT password from the new serial) in lockstep —
// see ConfigManager::saveDeviceIdentityToNVS() for the full rationale.
if (e.transport != CommandEnvelope::Transport::MQTT) {
return CommandResult::err("system.set_identity", "system.set_identity is only available via MQTT");
}
if (!e.contents["serial"].is<const char*>() ||
!e.contents["hw_family"].is<const char*>() ||
!e.contents["hw_revision"].is<const char*>()) {
return CommandResult::err("system.set_identity",
"Missing required fields: serial, hw_family, hw_revision (all required)");
}
String serial = e.contents["serial"].as<String>();
String hwFamily = e.contents["hw_family"].as<String>();
String hwRevision = e.contents["hw_revision"].as<String>();
if (serial.isEmpty() || hwFamily.isEmpty() || hwRevision.isEmpty()) {
return CommandResult::err("system.set_identity",
"serial, hw_family and hw_revision must be non-empty");
}
LOG_WARNING(TAG, "Device identity override requested via MQTT command");
bool ok = _configManager.saveDeviceIdentityToNVS(serial, hwFamily, hwRevision);
if (!ok) {
return CommandResult::err("system.set_identity",
"Failed to persist new identity to NVS — device identity unchanged");
}
// MQTT credentials, hostname and AP SSID were derived from the OLD serial at boot
// and won't recompute without a restart, so force one after the reply goes out.
xTaskCreate([](void*) {
vTaskDelay(pdMS_TO_TICKS(2000));
ESP.restart();
}, "setIdentityRebootTask", 1024, nullptr, 1, nullptr);
return CommandResult::ok("system.set_identity",
"Device identity updated. Restarting in 2 seconds to apply.");
}
#endif // HAS_RTC
@@ -40,6 +40,7 @@ private:
CommandResult handleGetTelemetry(const CommandEnvelope& e);
CommandResult handleFactoryReset(const CommandEnvelope& e);
CommandResult handleRestart(const CommandEnvelope& e);
CommandResult handleSetIdentity(const CommandEnvelope& e);
};
#endif // HAS_RTC
+40 -3
View File
@@ -193,9 +193,46 @@ bool ConfigManager::loadDeviceIdentityFromNVS() {
return true;
}
// REMOVED: saveDeviceIdentityToNVS() - Production firmware MUST NOT write device identity
// 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
// Device identity is factory-set and normally READ-ONLY from production firmware —
// see loadDeviceIdentityFromNVS() above. This is the sole write path, reserved for
// the sysadmin-only system.set_identity command (MQTT transport only). It exists to
// let a sysadmin correct/reassign identity on already-deployed hardware; ordinary
// application code must never call this.
//
// Callers MUST update the backend device record (serial/hw_family/hw_revision, and
// re-derive the MQTT password from the new serial) in the same operation — this
// method only updates the device's own NVS + in-RAM copy, and does not touch MQTT
// credentials, hostname, or AP SSID, all of which were already derived from the OLD
// serial at boot. A reboot is required after calling this for those to be
// recomputed consistently.
bool ConfigManager::saveDeviceIdentityToNVS(const String& serial, const String& hwFamily, const String& hwRevision) {
if (serial.isEmpty() || hwFamily.isEmpty() || hwRevision.isEmpty()) {
LOG_ERROR(TAG, "ConfigManager - ❌ saveDeviceIdentityToNVS: all three fields are required");
return false;
}
LOG_WARNING(TAG, "═══════════════════════════════════════════════════════════════════════════");
LOG_WARNING(TAG, " ⚠️ DEVICE IDENTITY OVERRIDE (sysadmin command)");
LOG_WARNING(TAG, " 🆔 Serial: %s -> %s", deviceConfig.serial.c_str(), serial.c_str());
LOG_WARNING(TAG, " 🔧 HW Family: %s -> %s", deviceConfig.hwFamily.c_str(), hwFamily.c_str());
LOG_WARNING(TAG, " 📐 HW Revision: %s -> %s", deviceConfig.hwRevision.c_str(), hwRevision.c_str());
LOG_WARNING(TAG, "═══════════════════════════════════════════════════════════════════════════");
bool ok = writeNVSString(NVS_SERIAL_KEY, serial);
ok &= writeNVSString(NVS_HW_FAMILY_KEY, hwFamily);
ok &= writeNVSString(NVS_HW_REVISION_KEY, hwRevision);
ok &= commitNVS();
if (!ok) {
LOG_ERROR(TAG, "ConfigManager - ❌ Failed to persist device identity override to NVS");
return false;
}
deviceConfig.serial = serial;
deviceConfig.hwFamily = hwFamily;
deviceConfig.hwRevision = hwRevision;
return true;
}
String ConfigManager::readNVSString(const char* key, const String& defaultValue) {
if (nvsHandle == 0) {
+9 -3
View File
@@ -321,9 +321,10 @@ public:
const ClockConfig& getClockConfig() const { return clockConfig; }
const GeneralConfig& getGeneralConfig() const { return generalConfig; }
// Device identity methods (READ-ONLY - factory set via separate factory firmware)
// These values are loaded ONCE at boot from NVS and kept in RAM
// Production firmware CANNOT modify device identity
// Device identity getters — factory-set via separate factory firmware, loaded
// ONCE at boot from NVS and kept in RAM. Not writable through normal config
// flows; the only write path is saveDeviceIdentityToNVS() below, reserved for
// the sysadmin-only system.set_identity command.
String getSerial() const { return deviceConfig.serial; }
String getHwFamily() const { return deviceConfig.hwFamily; }
String getHwRevision() const { return deviceConfig.hwRevision; }
@@ -336,6 +337,11 @@ public:
String getFwVersion() const { return FW_VERSION; }
// No-op: version is compiled in, not stored. Kept for call-site compatibility.
void setFwVersion(const String&) {}
// Sysadmin override: overwrites factory-set device identity in NVS and in RAM.
// Gated to the system.set_identity command (MQTT-only). Caller is responsible
// for updating the backend device record to match — this call does not.
bool saveDeviceIdentityToNVS(const String& serial, const String& hwFamily, const String& hwRevision);
// Configuration update methods for app commands
void updateTimeConfig(long gmtOffsetSec, int daylightOffsetSec);