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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user