// api_client.h — everything the device says to the Pi. // // TRANSPORT DECISION (answers "standard REST API, I guess?") // Yes, REST over plain HTTP with a bearer token (SPEC C2 — TLS stays on the // Pi). One deviation from SPEC §4.1, and it is worth making: // // SPEC says: POST /ingest multipart/form-data: image + JSON meta // we do: POST /ingest Content-Type: image/jpeg, raw body, meta in headers // // Why: multipart means the device assembles a body with boundaries, so the // JPEG gets copied into a second buffer purely to add a wrapper the server // immediately strips. Raw body + headers hands esp_http_client the framebuffer // pointer directly — no copy, no assembly, less heap, and one fewer thing to // get wrong at 3 a.m. It is still an ordinary REST call; `curl --data-binary // @frame.jpg -H 'X-Trigger: radar'` reproduces it exactly. // // The metadata is small and flat (a dozen scalars), which is precisely the // case headers handle well. If it ever grows nested structure, switch that one // endpoint to multipart and keep the rest. // // AUTH DECISION (answers "the token comes from the endpoint, I guess?") // Yes. Two-step, and the long-lived token is never in source or in a build // flag: // 1. A *shared enrollment secret* is compiled in (or typed once). The device // POSTs /enroll with its eFuse-derived UID. // 2. The Pi creates the device row, returns a per-device token, and the // device stores it in NVS and wipes the enrollment secret. // From then on every request carries `Authorization: Bearer `. Rotating // a token in the admin UI just means the next request 401s and the device // re-enrolls. The Pi stores only the hash (SPEC §3 `devices.token_hash`). #pragma once #include #include #include "frame_ring.h" // What the endpoint sends back on ingest/heartbeat. SPEC §4.1: the upload // response carries the command queue, so a capture that was going to happen // anyway also delivers pending commands at no extra cost. struct ServerEnvelope { bool ok; int http_status; int64_t server_time_ms; // authoritative clock (SPEC C7) uint32_t config_version; // if it differs from ours, pull config bool has_firmware; char fw_version[24]; char fw_url[160]; char fw_sha256[65]; uint32_t fw_size; // Commands are handled inside the parse callback rather than copied out, // so this struct stays a fixed-size value type. uint8_t command_count; }; struct CaptureMeta { const char* trigger; // radar | timer | manual | prebuffer | stream_grab const char* burst_id; // groups the frames of one event uint8_t burst_index; uint8_t burst_count; int8_t pre_trigger; // 1 if this frame predates the trigger uint16_t radar_distance_cm; uint8_t radar_energy; uint8_t radar_state; }; // Called once per command in a response envelope. typedef void (*CommandHandler)(JsonObjectConst cmd); void apiBegin(); // Exchange the enrollment secret for a device token. No-op if already enrolled. // Returns true when g_cfg.device_token is populated and persisted. bool apiEnroll(); // POST one JPEG. `meta` rides in headers. ServerEnvelope apiIngest(const FrameCopy& frame, const CaptureMeta& meta, CommandHandler on_cmd); // POST device conditions; the response carries the same envelope as ingest. // This is the call that drains the command queue, so it is also the one that // must not be made from somewhere the device cannot then act on a command. ServerEnvelope apiHeartbeat(CommandHandler on_cmd); // POST the same conditions body to /status, which returns 204 and no command // queue. For pushes where picking up commands would be wrong or wasteful: // immediately before deep sleep (the device is about to become unable to act), // after an upload burst, or on an explicit report_status command. // Returns true on a 2xx. bool apiPostStatus(const char* reason); // GET the full device config and merge it into g_cfg. Returns true if changed. bool apiFetchConfig(); // Report a command's outcome so the WebUI can stop showing it as pending. bool apiAckCommand(uint32_t command_id, const char* status, const char* detail); // Open a chunked MJPEG push for a streaming session. See stream_session.cpp. bool apiStreamOpen(const char* session_id, uint8_t mode, uint16_t duration_s); bool apiStreamSendFrame(const uint8_t* jpeg, size_t len, int64_t epoch_ms); void apiStreamClose(); // True when the last request returned 401 — the caller should re-enroll. bool apiTokenRejected();