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
+32 -1
View File
@@ -79,4 +79,35 @@ Before writing any new code, surface any dirty working tree and deal with it fir
### What does NOT need a catalog entry
- Pure refactors with no behaviour change
- Log message tweaks
- Doc-only commits
- Doc-only commits
**6. Console API Reference is mandatory.**
Any new command/API endpoint, or a change to an existing one's contents/response/errors, must be reflected in the console's live API Reference page — not just in `docs/reference/api-reference.md`. The two are separate files in separate repos and neither auto-syncs from the other; both must be updated by hand in the same change.
### Console API Reference location
`C:\development\bellsystems-cp\frontend\src\pages\engineering\developer\ApiReferencePage.jsx`
This is a different repo (`bellsystems-cp`, the backend/console repo) from this firmware repo (`project-vesper`) — open it directly by path, it will not show up in a search scoped to this repo.
### How to update it
Commands are grouped into a `NAMESPACES` array. Each entry is `{ id, label, description, commands: [...] }` — one entry per namespace (e.g. `system`, `clock`, `relay`, `ota`, `firmware`, `network`, or `root` for commands with no prefix). Find the matching namespace (or add a new one) and add/edit a command object in its `commands` array with these fields:
| Field | Required | Meaning |
|---|---|---|
| `cmd` | yes | Full command name, e.g. `"clock.set_time"` |
| `handler` | yes | The C++ handler class that services it, e.g. `"ClockHandler"` |
| `transports` | yes | Array/label of which transports accept it — usually `['All']`; narrow it if the command is transport-restricted (e.g. `identify` is WebSocket-only, `ping` is unavailable on RTC-less variants) |
| `description` | yes | What the command does, plus any caveats (rejection conditions, variant restrictions, blocking behaviour) |
| `contents` | yes (`null` if none) | Array of request fields, or `null` if the command takes no `contents` body |
| `contents[].field` | yes | Field name as it appears in the JSON `contents` object |
| `contents[].type` | yes | Wire type: `string`, `int`, `bool`, `int[]`, `object`, etc. |
| `contents[].required` | yes | `true`/`false` — whether the field is mandatory. If a command requires "at least one of" a set of optional fields, say so in `notes` on each and add an error entry for the all-missing case |
| `contents[].notes` | yes | What the field means, valid ranges/values, 0-based vs 1-based indexing (call this out explicitly — this API mixes both), defaults, and interactions with other fields |
| `response` | yes | Exact success wire-format JSON as a string, matching the real `{ "status": "SUCCESS", "type": "<cmd>", "message"/"data": ... }` envelope the firmware actually sends |
| `errors` | yes (`[]` if none) | Array of `{ message, condition }` — `message` is the exact error string the firmware emits, `condition` is when it fires. Should cover every `CommandResult::err()` path in the handler |
| `example` | yes | One or more realistic full request JSON payloads (`{ "v": 2, "cmd": "...", "contents": {...} }`), as a string |
| `warning` | yes (`null` if none) | Set for anything destructive or irreversible (reboot, factory reset, rollback) — short caller-facing warning string |
When adding a **new namespace**, also check `UART_WHITELIST` near the top of the file — if the command should be reachable over UART, add its name there too (UART only accepts whitelisted commands).
Keep `docs/reference/api-reference.md` (this repo) and the console page in sync — update both in the same change whenever a command is added or changed.
+19
View File
@@ -69,6 +69,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-057 | QA-to-shipping stats reset — `telemetry.reset_boot_data` (lifetime boot count + SD boot log) and `logs.clear` (SD debug logs) | v2.1.6 | Stable |
| F-058 | Fix: NTP sync RTC corruption on flaky internet — de-dup guard, retry cooldown, large-jump consensus check | v2.1.7 | Stable |
| F-059 | Fix: partition table mismatch blocking safe OTA to pre-restructure field devices | v2.1.7 | Stable |
| F-060 | `system.set_identity` — sysadmin override of factory-set device identity (serial/hw_family/hw_revision) | v2.1.7 | Stable |
---
@@ -976,3 +977,21 @@ With this in place, OTA to the existing field devices is safe going forward —
Verified: `pio run` (all four environments) + manual decode of `partitions.bin` against the field-device `partitions.csv`.
Subsystem: `platformio.ini`, `no_fs.csv` (new)
---
### F-060 — `system.set_identity`: sysadmin override of factory-set device identity
Device identity (`serial`, `hw_family`, `hw_revision`) has been factory-set-and-read-only since the NVS provisioning work — production firmware loads it once at boot and previously had no write path at all (`ConfigManager::saveDeviceIdentityToNVS()` had been deliberately removed). That's still the right default: `serial` derives the MQTT password (`HMAC-SHA256(MQTT_SECRET, serial)`), and `hw_family`/`hw_revision` select the OTA channel/binary, so an uncontrolled identity change can desync MQTT auth or pull firmware built for the wrong board.
This adds a narrow, deliberate exception: a new `system.set_identity` command lets a sysadmin overwrite identity on an already-deployed device — e.g. correcting a mis-provisioned unit, or reassigning a serial after a board swap — without a USB/serial reflash.
**Design constraints, by intent:**
- **MQTT-only.** Rejected over WebSocket/HTTP with an explicit transport check in the handler (same pattern as `identify`'s WebSocket-only restriction), and not added to `UARTTransport::WHITELIST`, so UART drops it by default. This means the command is only reachable by publishing to the device's MQTT command topic — i.e. only from the backend/console, not from a device on the local network probing the WebSocket/UART surface.
- **All three fields required together.** No partial update. Matches the existing boot-time invariant (`loadDeviceIdentityFromNVS()` already fails if any of the three is empty) — there's no valid state with only some of them set.
- **Forces a reboot after a successful write.** MQTT credentials, hostname, and AP SSID are all derived from `serial` once at boot (`ConfigManager::begin()`); they will not recompute live, so the device would otherwise keep authenticating with the *old* derived password after its stored identity already changed. Reply is sent first, then reboot after 2s (same pattern as `system.restart`).
- **Firmware does not update the backend.** This command only writes the device's own NVS + in-RAM copy. The backend device record (and re-derived MQTT password) must be updated in the same operation by whatever triggers this command — documented as a caller responsibility in both the handler and `ConfigManager::saveDeviceIdentityToNVS()`.
Not gated behind an additional `confirm` field in the command body — the console UI is expected to require its own confirmation step before ever publishing this command, so no device-side friction was added on top of that.
Subsystem: `src/ConfigManager/ConfigManager.hpp`, `src/ConfigManager/ConfigManager.cpp`, `src/Communication/Handlers/SystemHandler/SystemHandler.hpp`, `src/Communication/Handlers/SystemHandler/SystemHandler.cpp`
+27
View File
@@ -449,6 +449,33 @@ Reboot the device. Response is sent before reboot (2s delay).
---
### `system.set_identity`
Overwrite the device's factory-set identity (`serial`, `hw_family`, `hw_revision`) in NVS and reboot to apply it. Intended for sysadmin use only — e.g. reassigning a serial after a board swap, or correcting a mis-provisioned unit — via the backend console, which is responsible for updating its own device record (including re-deriving the MQTT password from the new serial) in the same operation. Firmware does not touch the backend record itself.
> **Warning:** Non-reversible from the device's perspective, and dangerous if the backend device record isn't updated to match — a serial mismatch will break MQTT auth (password is derived from serial), and a wrong `hw_family`/`hw_revision` can pull an OTA build for the wrong hardware. Device restarts 2s after a successful response to apply the new identity.
| | |
|---|---|
| Handler | SystemHandler |
| Transports | MQTT only |
| Contents | `serial` (required), `hw_family` (required), `hw_revision` (required) |
```json
{ "v": 2, "cmd": "system.set_identity", "contents": { "serial": "PV000000000001", "hw_family": "VS", "hw_revision": "01" } }
```
**Errors:**
| Message | Condition |
|---|---|
| `system.set_identity is only available via MQTT` | Sent over WebSocket, HTTP, or UART |
| `Missing required fields: serial, hw_family, hw_revision (all required)` | One or more fields absent or not a string |
| `serial, hw_family and hw_revision must be non-empty` | One or more fields present but empty |
| `Failed to persist new identity to NVS — device identity unchanged` | NVS write/commit failed |
---
## Firmware Namespace — `FirmwareHandler`
---