201 lines
6.9 KiB
C++
201 lines
6.9 KiB
C++
#include "commands.h"
|
|
|
|
#include "api_client.h"
|
|
#include "app_config.h"
|
|
#include "camera_mgr.h"
|
|
#include "ota_mgr.h"
|
|
#include "power_mgr.h"
|
|
#include "stream_session.h"
|
|
|
|
// main.cpp owns the capture pipeline; this is how a command reaches it.
|
|
extern void requestManualCapture();
|
|
|
|
// A deferred command, kept as a fixed-size record so the queue needs no heap.
|
|
struct Deferred {
|
|
bool used;
|
|
uint32_t id;
|
|
char type[20];
|
|
char s_arg[64]; // session id, or firmware url
|
|
char s_arg2[65]; // sha256
|
|
uint32_t n_arg; // seconds, size
|
|
uint8_t u_arg[3]; // mode, framesize, spare
|
|
};
|
|
|
|
static const size_t kQueueDepth = 4;
|
|
static Deferred s_queue[kQueueDepth] = {};
|
|
|
|
static bool enqueue(const Deferred& d) {
|
|
for (size_t i = 0; i < kQueueDepth; i++) {
|
|
if (!s_queue[i].used) { s_queue[i] = d; s_queue[i].used = true; return true; }
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void commandOnReceived(JsonObjectConst cmd) {
|
|
const uint32_t id = cmd["id"] | (uint32_t)0;
|
|
const char* type = cmd["type"] | "";
|
|
JsonObjectConst p = cmd["payload"];
|
|
|
|
log_i("cmd #%u: %s", (unsigned)id, type);
|
|
powerNoteActivity();
|
|
|
|
// --- immediate, cheap ---------------------------------------------------
|
|
|
|
if (strcmp(type, "apply_config") == 0) {
|
|
const bool was_sleeping = g_cfg.deep_sleep_enabled;
|
|
const AppConfig before = g_cfg;
|
|
|
|
bool changed = false;
|
|
if (!p.isNull()) {
|
|
changed = configApplyJson(p);
|
|
} else {
|
|
// No inline payload: the endpoint wants us to pull the full doc.
|
|
changed = apiFetchConfig();
|
|
}
|
|
|
|
if (changed) {
|
|
configSave();
|
|
cameraApplyConfig();
|
|
powerOnConfigChanged(was_sleeping);
|
|
if (configNeedsCameraRestart(before, g_cfg)) {
|
|
log_i("cmd: frame size changed, restarting camera");
|
|
cameraRestart();
|
|
}
|
|
}
|
|
apiAckCommand(id, "done", changed ? "config applied" : "no change");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "capture_now") == 0) {
|
|
requestManualCapture();
|
|
apiAckCommand(id, "done", "capture queued");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "report_status") == 0) {
|
|
// Deferred: we are currently inside the heartbeat's response handler,
|
|
// and opening a second connection from here would nest HTTP clients.
|
|
Deferred d = {};
|
|
d.id = id;
|
|
strncpy(d.type, "report_status", sizeof(d.type) - 1);
|
|
if (!enqueue(d)) apiAckCommand(id, "failed", "command queue full");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "wake_and_hold") == 0) {
|
|
uint32_t secs = p["seconds"] | (uint32_t)60;
|
|
if (secs > 900) secs = 900; // bounded, per SPEC §4.3
|
|
powerHoldAwake(secs);
|
|
apiAckCommand(id, "done", "holding awake");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "sleep_now") == 0) {
|
|
// Deferred: we want the ack to reach the Pi before the radio goes off.
|
|
Deferred d = {};
|
|
d.id = id;
|
|
d.n_arg = p["seconds"] | (uint32_t)0;
|
|
strncpy(d.type, "sleep_now", sizeof(d.type) - 1);
|
|
if (!enqueue(d)) apiAckCommand(id, "failed", "command queue full");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "stream_stop") == 0) {
|
|
streamStop("stopped by command");
|
|
apiAckCommand(id, "done", "stream stopped");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "reboot") == 0) {
|
|
apiAckCommand(id, "done", "rebooting");
|
|
delay(200);
|
|
ESP.restart();
|
|
return;
|
|
}
|
|
|
|
// --- deferred: these block, so they run from the main loop ---------------
|
|
|
|
if (strcmp(type, "stream_start") == 0) {
|
|
Deferred d = {};
|
|
d.id = id;
|
|
strncpy(d.type, "stream_start", sizeof(d.type) - 1);
|
|
strncpy(d.s_arg, p["session_id"] | "", sizeof(d.s_arg) - 1);
|
|
d.n_arg = p["duration_s"] | (uint32_t)0;
|
|
d.u_arg[0] = p["mode"] | (uint8_t)0xFF;
|
|
d.u_arg[1] = p["frame_size"] | (uint8_t)0;
|
|
if (!d.s_arg[0]) { apiAckCommand(id, "rejected", "no session_id"); return; }
|
|
if (!enqueue(d)) apiAckCommand(id, "failed", "command queue full");
|
|
return;
|
|
}
|
|
|
|
if (strcmp(type, "ota_update") == 0) {
|
|
Deferred d = {};
|
|
d.id = id;
|
|
strncpy(d.type, "ota_update", sizeof(d.type) - 1);
|
|
strncpy(d.s_arg, p["url"] | "", sizeof(d.s_arg) - 1);
|
|
strncpy(d.s_arg2, p["sha256"] | "", sizeof(d.s_arg2) - 1);
|
|
d.n_arg = p["size"] | (uint32_t)0;
|
|
// The version lands in s_arg's tail via the firmware envelope instead;
|
|
// an ota_update command without a url means "use the target from the
|
|
// envelope", which main.cpp already tracks.
|
|
if (!enqueue(d)) apiAckCommand(id, "failed", "command queue full");
|
|
return;
|
|
}
|
|
|
|
log_w("cmd #%u: unknown type '%s'", (unsigned)id, type);
|
|
apiAckCommand(id, "rejected", "unknown command type");
|
|
}
|
|
|
|
// The firmware target from the most recent response envelope, so an
|
|
// `ota_update` command with no explicit url still knows what to fetch.
|
|
extern OtaTarget g_fw_target;
|
|
|
|
void commandRunDeferred() {
|
|
for (size_t i = 0; i < kQueueDepth; i++) {
|
|
if (!s_queue[i].used) continue;
|
|
|
|
Deferred d = s_queue[i];
|
|
s_queue[i].used = false; // clear first: a reboot must not replay it
|
|
|
|
if (strcmp(d.type, "stream_start") == 0) {
|
|
bool ok = streamStart(d.s_arg, d.u_arg[0], d.u_arg[1], (uint16_t)d.n_arg);
|
|
apiAckCommand(d.id, ok ? "done" : "failed",
|
|
ok ? "streaming" : "could not start stream");
|
|
|
|
} else if (strcmp(d.type, "ota_update") == 0) {
|
|
OtaTarget t = g_fw_target;
|
|
if (d.s_arg[0]) strncpy(t.url, d.s_arg, sizeof(t.url) - 1);
|
|
if (d.s_arg2[0]) strncpy(t.sha256, d.s_arg2, sizeof(t.sha256) - 1);
|
|
if (d.n_arg) t.size = d.n_arg;
|
|
|
|
if (!t.url[0]) {
|
|
apiAckCommand(d.id, "rejected", "no firmware url");
|
|
continue;
|
|
}
|
|
// Ack before flashing: otaApply() reboots on success and would
|
|
// never get the chance to report it.
|
|
apiAckCommand(d.id, "done", "flashing");
|
|
if (!otaApply(t)) {
|
|
apiAckCommand(d.id, "failed", "ota failed, still on old image");
|
|
}
|
|
|
|
} else if (strcmp(d.type, "report_status") == 0) {
|
|
bool ok = apiPostStatus("on_request");
|
|
apiAckCommand(d.id, ok ? "done" : "failed", ok ? "status posted" : "status post failed");
|
|
|
|
} else if (strcmp(d.type, "sleep_now") == 0) {
|
|
apiAckCommand(d.id, "done", "sleeping");
|
|
powerSleepNow(d.n_arg);
|
|
// Returns only if sleep is switched off or OTA is in flight.
|
|
apiAckCommand(d.id, "rejected", "deep sleep is off");
|
|
}
|
|
}
|
|
}
|
|
|
|
bool commandHasPending() {
|
|
for (size_t i = 0; i < kQueueDepth; i++) {
|
|
if (s_queue[i].used) return true;
|
|
}
|
|
return false;
|
|
}
|