# OTA Telemetry API — Backend Endpoints Required The firmware POSTs two events during an OTA update. Both are **best-effort** — a failure does not abort the update. The device does not retry on failure. --- ## 1. Download Event **Trigger:** Firmware binary fully written to the staged flash partition, before `Update.end()` is called. The new firmware is not yet active. **Method:** `POST` **Path:** `/api/ota/events/download` ### Request Body (JSON) ```json { "device_uid": "PV000000000000", "hw_type": "VS", "hw_version": "01", "from_version": "1.2.3", "to_version": "1.3.0", "channel": "stable" } ``` | Field | Type | Description | |----------------|--------|--------------------------------------------------| | `device_uid` | string | Unique device identifier from NVS | | `hw_type` | string | Hardware type code (e.g. `VS`, `VP`, `VX`) | | `hw_version` | string | Hardware revision from NVS (e.g. `01`) | | `from_version` | string | Semver of the firmware currently running | | `to_version` | string | Semver of the firmware that was just downloaded | | `channel` | string | Update channel (`stable`, `beta`, etc.) | ### Expected Response Any `2xx` status is accepted. Recommended: `204 No Content`. --- ## 2. Flash Event **Trigger:** `Update.end()` succeeded — the new partition is committed and the device is about to restart. This is the most reliable signal that the update actually landed. **Method:** `POST` **Path:** `/api/ota/events/flash` ### Request Body (JSON) ```json { "device_uid": "PV000000000000", "hw_type": "VS", "hw_version": "01", "from_version": "1.2.3", "to_version": "1.3.0", "channel": "stable", "sha256": "a3f1...64hexchars...b9e2" } ``` | Field | Type | Description | |----------------|--------|-------------------------------------------------------| | `device_uid` | string | Unique device identifier from NVS | | `hw_type` | string | Hardware type code | | `hw_version` | string | Hardware revision from NVS | | `from_version` | string | Semver of the firmware that was running | | `to_version` | string | Semver of the firmware now committed to flash | | `channel` | string | Update channel | | `sha256` | string | SHA256 of the downloaded binary (from `/latest` metadata) | ### Expected Response Any `2xx` status is accepted. Recommended: `204 No Content`. --- ## Notes - Both requests use the **primary server only** (`servers[0]` from the configured list). No retry, no fallback server attempt. - Timeout is hardcoded to **5 seconds** to avoid blocking the reboot. - If WiFi is not connected at the time of the POST, the call is silently skipped. - The difference between the two events: `download` tells you the device received the binary; `flash` tells you the partition was committed. A device that posts `download` but never posts `flash` may have had a verification failure — worth flagging in the backend. --- ## Suggested Backend Behaviour - Store both events in a `firmware_events` table with a `type` column (`download` / `flash`). - Index by `device_uid` + `to_version` to detect partial failures (download without flash). - Use the `flash` event to drive the "current fleet version" dashboard — it is the ground truth. --- ## Backend Changes Required — Upgrade Chain Resolution ### Background The firmware sends its current version as a query parameter on every version check: ``` GET /api/firmware/{hw_type}/{channel}/latest?current_version=5.7.1 ``` The server must use `current_version` to resolve upgrade chains and return the **correct next hop** rather than blindly returning the absolute latest. The device trusts whatever version the server returns and installs it. **Example:** Device is on `5.7.1`. Latest is `7.2.2` but has `min_fw_version = 6.0.0`. - Without chain resolution → server returns `7.2.2` → device cannot install → update silently fails. - With chain resolution → server returns `6.0.0` → device installs it → on next boot, server returns `7.2.2` → device installs it. --- ### Changes needed in `/api/firmware/{hw_type}/{channel}/latest` 1. **Accept `current_version` as an optional query parameter** (string, semver format e.g. `5.7.1`). If absent or unparseable, fall back to current behaviour (return absolute latest). 2. **Resolve the next hop:** ``` query all published versions for (hw_type, channel), ordered ascending find the lowest version V where: V > current_version AND (V.min_fw_version is null OR current_version >= V.min_fw_version) return V ``` If no such version exists (current is already at latest, or all higher versions have unsatisfied min_fw_version), return `404` or the current version with no update flag. 3. **The response shape does not change** — same fields as today. The device doesn't need to know it received an intermediate version rather than the latest. ### Example query logic (pseudo-SQL) ```sql SELECT * FROM firmware_releases WHERE hw_type = :hw_type AND channel = :channel AND version > :current_version AND (min_fw_version IS NULL OR :current_version >= min_fw_version) ORDER BY version ASC -- semver-aware sort LIMIT 1; ``` > Note: semver ordering must be numeric (`1.10.0 > 1.9.0`), not lexicographic. Use a semver-aware sort or store major/minor/patch as separate integer columns. ### Edge cases to handle | Scenario | Expected behaviour | |---|---| | `current_version` absent / malformed | Ignore param, return absolute latest (existing behaviour) | | Device already at latest | Return `404` or `200` with a "no update" indicator — firmware handles both | | No version satisfies the chain constraint | Return `404` — firmware logs and skips | | Multiple intermediate hops needed | Server returns only the immediate next hop; device re-checks after each reboot and walks the chain naturally |