Files

54 lines
2.0 KiB
C

// ota_mgr.h — OTA with SHA-256 verification and trial-boot rollback (SPEC C6).
//
// The rules from the spec, implemented here:
// - dual OTA slots exist from the first flash (partitions CSV)
// - verify SHA-256 of the downloaded image before activating the slot
// - boot the new image ONCE in a trial state
// - if the trial image cannot check in with the endpoint, roll back
// automatically on the next reset
// - never sleep mid-flash (INHIBIT_OTA)
//
// The battery gate from SPEC C6 is kept as a hook (`otaBatteryOkFn`) but has
// no default sensor: this build is mains-powered and the XIAO's battery-sense
// divider is not wired. Register a callback if that changes.
#pragma once
#include <Arduino.h>
struct OtaTarget {
char version[24];
char url[160]; // absolute, or a path relative to the API base
char sha256[65]; // lowercase hex
uint32_t size;
};
void otaBegin();
// True when this boot is the first run of a freshly flashed image and has not
// yet been confirmed. Until otaMarkValid() is called, a reset rolls back.
bool otaPendingVerify();
// Call once the device has proven the new image works — we use "a heartbeat
// was accepted by the endpoint", which exercises WiFi, HTTP, auth and config
// in one go. Anything less and a firmware that boots but cannot talk would be
// marked good.
void otaMarkValid();
// Give up on the trial image and reboot into the previous slot.
void otaRollback(const char* reason);
// Is `target.version` different from what we are running?
bool otaIsNewer(const OtaTarget& target);
// Download, verify, activate, reboot. Blocks for the duration. Returns only on
// failure — on success the device restarts into the new slot.
// Holds INHIBIT_OTA for the whole operation.
bool otaApply(const OtaTarget& target);
// Optional battery gate (SPEC C6: refuse to flash below a threshold).
typedef bool (*OtaBatteryOkFn)(uint8_t min_pct);
void otaSetBatteryCheck(OtaBatteryOkFn fn, uint8_t min_pct);
const char* otaRunningVersion();
const char* otaRunningPartition();