feat: update led-driver app files over ESP-NOW
tools/driver_ota.py (make ota-drivers) pushes the driver's .py files through the controller, one file per POST /drivers/ota/file. Drivers skip files they already have, and the rest are committed together. - one target: acked unicast, two chunks in flight - several targets: each file is broadcast once, then each driver is asked for its missing chunks and repaired by unicast - rel: rel_send_seq, rel_wait and rel_send_wait, plus a callback for the drivers' 0x26 status replies - commands are retried, because status replies are not acked - broadcast chunks are paced 50 ms apart: faster broadcast left C3 drivers unable to receive until reset - bench results in docs/BENCH_TEST.md, endpoints in docs/API.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1c222771a5
commit
32c0ecfa76
13 files changed
+862
-9
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c" "drv_ota.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio
|
||||
|
||||
+465
@@ -0,0 +1,465 @@
|
||||
#include "drv_ota.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "cJSON.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/task.h"
|
||||
#include "rel.h"
|
||||
#include "store.h"
|
||||
|
||||
static const char *TAG = "drv_ota";
|
||||
|
||||
#define WIRE_MAGIC 0x4C
|
||||
#define OP_BEGIN 0x07
|
||||
#define OP_DATA 0x08
|
||||
#define OP_END 0x09
|
||||
#define OP_COMMIT 0x0A
|
||||
#define OP_ABORT 0x0B
|
||||
#define OP_MISSING 0x0C
|
||||
|
||||
// Status codes from the driver
|
||||
enum { C_OK, C_READY, C_SAME, C_BADHASH, C_INCOMPLETE, C_BADPATH, C_IOERR, C_NOSESS, C_TOOBIG, C_NCODES };
|
||||
static const char *const CODE_NAMES[] = { "ok", "ready", "same", "badhash", "incomplete",
|
||||
"badpath", "ioerr", "nosess", "toobig" };
|
||||
|
||||
#define CHUNK 944 // 4 link fragments; must match espnow_ota.CHUNK
|
||||
#define MAX_FILE 65536
|
||||
#define MAX_CHUNKS ((MAX_FILE + CHUNK - 1) / CHUNK)
|
||||
#define MAX_MACS 32
|
||||
#define MAX_PATH 128
|
||||
#define ACK_MS 3000 // rel gives up after about 1 s; this only guards against a lost outcome
|
||||
#define BEGIN_MS 1000 // per try: the driver hashes its current copy first
|
||||
#define END_MS 2500 // per try: the driver hashes the staged copy (64 KB takes well under 1 s on a C3)
|
||||
#define CMD_TRIES 5 // status replies are not acked, so a lost one costs a resend
|
||||
#define REPAIR_ROUNDS 4
|
||||
#define BCAST_GAP_MS 50 // between broadcast chunks: a C3 writing flash cannot take them faster
|
||||
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
uint16_t sess;
|
||||
uint8_t op, code;
|
||||
uint8_t arg_len;
|
||||
uint8_t arg[24];
|
||||
} status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
char hex[13];
|
||||
int state; // C_READY while in the transfer, C_SAME, C_OK when written, else the error
|
||||
const char *err; // link-level failure text, when no driver code applies
|
||||
int repaired;
|
||||
} target_t;
|
||||
|
||||
static QueueHandle_t s_status_q;
|
||||
static SemaphoreHandle_t s_busy;
|
||||
|
||||
static void on_status(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
{
|
||||
if (len < 6) {
|
||||
return;
|
||||
}
|
||||
status_t st = { .sess = pkt[2] | (pkt[3] << 8), .op = pkt[4], .code = pkt[5] };
|
||||
memcpy(st.mac, mac, 6);
|
||||
st.arg_len = len - 6 > (int)sizeof(st.arg) ? sizeof(st.arg) : len - 6;
|
||||
memcpy(st.arg, pkt + 6, st.arg_len);
|
||||
xQueueSend(s_status_q, &st, 0);
|
||||
}
|
||||
|
||||
// Wait for this driver's reply to op; replies left over from earlier requests are dropped
|
||||
static bool wait_status(const uint8_t *mac, uint16_t sess, uint8_t op, int timeout_ms, status_t *out)
|
||||
{
|
||||
TickType_t until = xTaskGetTickCount() + pdMS_TO_TICKS(timeout_ms);
|
||||
for (;;) {
|
||||
TickType_t now = xTaskGetTickCount();
|
||||
if ((int32_t)(until - now) <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (xQueueReceive(s_status_q, out, until - now) != pdTRUE) {
|
||||
return false;
|
||||
}
|
||||
if (out->sess == sess && out->op == op && memcmp(out->mac, mac, 6) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void drain_status(void)
|
||||
{
|
||||
status_t st;
|
||||
while (xQueueReceive(s_status_q, &st, 0) == pdTRUE) {
|
||||
}
|
||||
}
|
||||
|
||||
// Acked command to one driver, then its status reply. Returns the code, or -1 if the driver never answered.
|
||||
static int command(const uint8_t *mac, const uint8_t *pkt, size_t len, uint16_t sess, uint8_t op, int timeout_ms,
|
||||
status_t *st)
|
||||
{
|
||||
status_t local;
|
||||
st = st ? st : &local;
|
||||
// Neither the reply nor a given-up ack proves the driver missed it: every command is safe to repeat
|
||||
for (int t = 0; t < CMD_TRIES; t++) {
|
||||
rel_send_wait(mac, pkt, len, ACK_MS);
|
||||
if (wait_status(mac, sess, op, timeout_ms, st)) {
|
||||
return st->code;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static size_t data_pkt(uint8_t *pkt, uint16_t sess, int idx, const uint8_t *body, size_t size)
|
||||
{
|
||||
size_t off = (size_t)idx * CHUNK, n = size - off > CHUNK ? CHUNK : size - off;
|
||||
pkt[0] = WIRE_MAGIC;
|
||||
pkt[1] = OP_DATA;
|
||||
pkt[2] = sess & 255;
|
||||
pkt[3] = sess >> 8;
|
||||
pkt[4] = idx & 255;
|
||||
pkt[5] = idx >> 8;
|
||||
memcpy(pkt + 6, body + off, n);
|
||||
return 6 + n;
|
||||
}
|
||||
|
||||
// Acked unicast of the chunks marked in want (NULL = all), two messages in flight.
|
||||
// rel_send copies each message, so one packet buffer is enough. A chunk whose ack never came
|
||||
// is left to the END/MISSING repair; only a run of them means the driver is gone.
|
||||
#define DEAD_AFTER 8
|
||||
|
||||
static bool chunk_done(const uint8_t *mac, uint16_t seq, int *dead)
|
||||
{
|
||||
if (rel_wait(mac, seq, ACK_MS) == ESP_OK) {
|
||||
*dead = 0;
|
||||
return true;
|
||||
}
|
||||
return ++*dead < DEAD_AFTER;
|
||||
}
|
||||
|
||||
static bool send_chunks(const uint8_t *mac, uint16_t sess, const uint8_t *body, size_t size, const uint8_t *want,
|
||||
int *sent)
|
||||
{
|
||||
static uint8_t pkt[6 + CHUNK];
|
||||
int cnt = (size + CHUNK - 1) / CHUNK;
|
||||
uint16_t fifo[2];
|
||||
int head = 0, inflight = 0, dead = 0;
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
if (want && !(want[i >> 3] & (1 << (i & 7)))) {
|
||||
continue;
|
||||
}
|
||||
if (inflight == 2) {
|
||||
if (!chunk_done(mac, fifo[head], &dead)) {
|
||||
return false;
|
||||
}
|
||||
head ^= 1;
|
||||
inflight--;
|
||||
}
|
||||
size_t n = data_pkt(pkt, sess, i, body, size);
|
||||
uint16_t seq;
|
||||
esp_err_t err = ESP_ERR_NO_MEM;
|
||||
for (int t = 0; t < 50 && err == ESP_ERR_NO_MEM; t++) {
|
||||
err = rel_send_seq(mac, pkt, n, false, 1, &seq);
|
||||
if (err == ESP_ERR_NO_MEM) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
fifo[(head + inflight) & 1] = seq;
|
||||
inflight++;
|
||||
if (sent) {
|
||||
(*sent)++;
|
||||
}
|
||||
}
|
||||
for (; inflight > 0; inflight--, head ^= 1) {
|
||||
if (!chunk_done(mac, fifo[head], &dead)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void broadcast_chunks(uint16_t sess, const uint8_t *body, size_t size)
|
||||
{
|
||||
static uint8_t pkt[6 + CHUNK];
|
||||
int cnt = (size + CHUNK - 1) / CHUNK;
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
size_t n = data_pkt(pkt, sess, i, body, size);
|
||||
for (int t = 0; t < 50 && rel_send(NULL, pkt, n, false, 1) == ESP_ERR_NO_MEM; t++) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(BCAST_GAP_MS));
|
||||
}
|
||||
}
|
||||
|
||||
// END until the driver has every chunk: ask what is missing and resend it by unicast
|
||||
static void finish(target_t *t, uint16_t sess, const uint8_t *body, size_t size)
|
||||
{
|
||||
uint8_t end[4] = { WIRE_MAGIC, OP_END, sess & 255, sess >> 8 };
|
||||
uint8_t miss[4] = { WIRE_MAGIC, OP_MISSING, sess & 255, sess >> 8 };
|
||||
for (int round = 0; round < REPAIR_ROUNDS; round++) {
|
||||
int code = command(t->mac, end, sizeof(end), sess, OP_END, END_MS, NULL);
|
||||
if (code != C_INCOMPLETE) {
|
||||
t->state = code;
|
||||
t->err = code < 0 ? "no reply to end" : NULL;
|
||||
return;
|
||||
}
|
||||
status_t st;
|
||||
if (command(t->mac, miss, sizeof(miss), sess, OP_MISSING, BEGIN_MS, &st) != C_OK || st.arg_len < 2) {
|
||||
t->state = -1;
|
||||
t->err = "no reply to missing";
|
||||
return;
|
||||
}
|
||||
int cnt = st.arg[0] | (st.arg[1] << 8);
|
||||
if (cnt != (int)((size + CHUNK - 1) / CHUNK) || st.arg_len < 2 + (cnt + 7) / 8) {
|
||||
t->state = -1;
|
||||
t->err = "bad missing reply";
|
||||
return;
|
||||
}
|
||||
if (!send_chunks(t->mac, sess, body, size, st.arg + 2, &t->repaired)) {
|
||||
t->state = -1;
|
||||
t->err = "link lost";
|
||||
return;
|
||||
}
|
||||
}
|
||||
t->state = C_INCOMPLETE;
|
||||
}
|
||||
|
||||
static void transfer(target_t *ts, int n, uint16_t sess, const char *path, const uint8_t sha[32], const uint8_t *body,
|
||||
size_t size)
|
||||
{
|
||||
uint8_t begin[41 + MAX_PATH];
|
||||
size_t plen = strlen(path);
|
||||
begin[0] = WIRE_MAGIC;
|
||||
begin[1] = OP_BEGIN;
|
||||
begin[2] = sess & 255;
|
||||
begin[3] = sess >> 8;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
begin[4 + i] = (size >> (8 * i)) & 255;
|
||||
}
|
||||
memcpy(begin + 8, sha, 32);
|
||||
begin[40] = plen;
|
||||
memcpy(begin + 41, path, plen);
|
||||
|
||||
int ready = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int code = command(ts[i].mac, begin, 41 + plen, sess, OP_BEGIN, BEGIN_MS, NULL);
|
||||
ts[i].state = code;
|
||||
ts[i].err = code < 0 ? "no reply to begin" : NULL;
|
||||
ready += code == C_READY;
|
||||
}
|
||||
if (ready == 0) {
|
||||
return;
|
||||
}
|
||||
if (ready > 1) {
|
||||
broadcast_chunks(sess, body, size);
|
||||
} else {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ts[i].state == C_READY && !send_chunks(ts[i].mac, sess, body, size, NULL, NULL)) {
|
||||
ts[i].state = -1;
|
||||
ts[i].err = "link lost";
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ts[i].state == C_READY) {
|
||||
finish(&ts[i], sess, body, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_hex(const char *s, uint8_t *out, int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++) {
|
||||
unsigned v;
|
||||
if (sscanf(s + 2 * i, "%2x", &v) != 1) {
|
||||
return -1;
|
||||
}
|
||||
out[i] = v;
|
||||
}
|
||||
return s[2 * n] == '\0' || s[2 * n] == ',' ? 0 : -1;
|
||||
}
|
||||
|
||||
// mac=all, or a comma-separated list of 12-hex addresses
|
||||
static int parse_targets(const char *list, target_t *ts)
|
||||
{
|
||||
int n = 0;
|
||||
if (!strcmp(list, "all")) {
|
||||
store_lock();
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
const cJSON *tr = cJSON_GetObjectItem(d, "transport"), *addr = cJSON_GetObjectItem(d, "address");
|
||||
if (n < MAX_MACS && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") && cJSON_IsString(addr) &&
|
||||
strlen(addr->valuestring) == 12 && parse_hex(addr->valuestring, ts[n].mac, 6) == 0) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
store_unlock();
|
||||
} else {
|
||||
for (const char *p = list; *p && n < MAX_MACS;) {
|
||||
if (parse_hex(p, ts[n].mac, 6) != 0) {
|
||||
return -1;
|
||||
}
|
||||
n++;
|
||||
p += 12;
|
||||
if (*p == ',') {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
uint8_t *m = ts[i].mac;
|
||||
snprintf(ts[i].hex, sizeof(ts[i].hex), "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], m[4], m[5]);
|
||||
ts[i].state = -1;
|
||||
ts[i].err = NULL;
|
||||
ts[i].repaired = 0;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static esp_err_t send_json(httpd_req_t *req, const char *status, cJSON *obj)
|
||||
{
|
||||
char *s = cJSON_PrintUnformatted(obj);
|
||||
cJSON_Delete(obj);
|
||||
if (!s) {
|
||||
return httpd_resp_send_500(req);
|
||||
}
|
||||
httpd_resp_set_status(req, status);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
esp_err_t r = httpd_resp_sendstr(req, s);
|
||||
free(s);
|
||||
return r;
|
||||
}
|
||||
|
||||
static esp_err_t send_error(httpd_req_t *req, const char *status, const char *msg)
|
||||
{
|
||||
ESP_LOGW(TAG, "%s", msg);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "error", msg);
|
||||
return send_json(req, status, o);
|
||||
}
|
||||
|
||||
static cJSON *results(const target_t *ts, int n, bool file)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON *e = cJSON_AddObjectToObject(o, ts[i].hex);
|
||||
int s = ts[i].state;
|
||||
const char *result = s == C_OK ? (file ? "written" : "ok") : s == C_SAME ? "same" : "error";
|
||||
cJSON_AddStringToObject(e, "result", result);
|
||||
if (s < 0) {
|
||||
cJSON_AddStringToObject(e, "code", ts[i].err ? ts[i].err : "no reply");
|
||||
} else if (s != C_OK && s != C_SAME) {
|
||||
cJSON_AddStringToObject(e, "code", s < C_NCODES ? CODE_NAMES[s] : "unknown");
|
||||
}
|
||||
if (file) {
|
||||
cJSON_AddNumberToObject(e, "repaired", ts[i].repaired);
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
static esp_err_t handle(httpd_req_t *req, const char *action, const char *query, target_t *ts, int n, uint16_t sess)
|
||||
{
|
||||
if (!strcmp(action, "commit") || !strcmp(action, "abort")) {
|
||||
bool commit = action[0] == 'c';
|
||||
uint8_t op = commit ? OP_COMMIT : OP_ABORT;
|
||||
uint8_t pkt[4] = { WIRE_MAGIC, op, sess & 255, sess >> 8 };
|
||||
for (int i = 0; i < n; i++) {
|
||||
ts[i].state = command(ts[i].mac, pkt, sizeof(pkt), sess, op, BEGIN_MS, NULL);
|
||||
}
|
||||
return send_json(req, "200 OK", results(ts, n, false));
|
||||
}
|
||||
if (strcmp(action, "file")) {
|
||||
return send_error(req, "404 Not Found", "unknown action");
|
||||
}
|
||||
|
||||
char path[MAX_PATH + 1], shahex[65];
|
||||
uint8_t sha[32];
|
||||
if (httpd_query_key_value(query, "path", path, sizeof(path)) != ESP_OK || !path[0] ||
|
||||
httpd_query_key_value(query, "sha", shahex, sizeof(shahex)) != ESP_OK || strlen(shahex) != 64 ||
|
||||
parse_hex(shahex, sha, 32) != 0) {
|
||||
return send_error(req, "400 Bad Request", "path and sha (64 hex) are required");
|
||||
}
|
||||
for (const char *c = path; *c; c++) { // query values arrive undecoded: keep paths to plain names
|
||||
if (!(isalnum((unsigned char)*c) || *c == '_' || *c == '.' || *c == '/' || *c == '-')) {
|
||||
return send_error(req, "400 Bad Request", "path may only hold letters, digits and _ . / -");
|
||||
}
|
||||
}
|
||||
if (req->content_len > MAX_FILE) {
|
||||
return send_error(req, "413 Payload Too Large", "file must be at most 65536 bytes");
|
||||
}
|
||||
uint8_t *body = malloc(req->content_len + 1); // empty files (__init__.py) are fine
|
||||
if (!body) {
|
||||
return send_error(req, "500 Internal Server Error", "out of memory");
|
||||
}
|
||||
size_t got = 0;
|
||||
while (got < req->content_len) {
|
||||
int r = httpd_req_recv(req, (char *)body + got, req->content_len - got);
|
||||
if (r == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||
continue;
|
||||
}
|
||||
if (r <= 0) {
|
||||
free(body);
|
||||
return send_error(req, "400 Bad Request", "receive failed");
|
||||
}
|
||||
got += r;
|
||||
}
|
||||
ESP_LOGI(TAG, "%s (%u B) to %d driver(s), session %u", path, (unsigned)got, n, sess);
|
||||
transfer(ts, n, sess, path, sha, body, got);
|
||||
free(body);
|
||||
return send_json(req, "200 OK", results(ts, n, true));
|
||||
}
|
||||
|
||||
static esp_err_t ota_post_handler(httpd_req_t *req)
|
||||
{
|
||||
const char *action = req->uri + strlen("/drivers/ota/");
|
||||
size_t alen = strcspn(action, "?");
|
||||
char act[16] = "";
|
||||
if (alen < sizeof(act)) {
|
||||
memcpy(act, action, alen);
|
||||
act[alen] = '\0';
|
||||
}
|
||||
|
||||
char query[480], macs[400], sessbuf[8];
|
||||
if (httpd_req_get_url_query_str(req, query, sizeof(query)) != ESP_OK ||
|
||||
httpd_query_key_value(query, "mac", macs, sizeof(macs)) != ESP_OK ||
|
||||
httpd_query_key_value(query, "sess", sessbuf, sizeof(sessbuf)) != ESP_OK) {
|
||||
return send_error(req, "400 Bad Request", "mac and sess are required");
|
||||
}
|
||||
long sess = strtol(sessbuf, NULL, 10);
|
||||
if (sess < 1 || sess > 0xFFFF) {
|
||||
return send_error(req, "400 Bad Request", "sess must be 1..65535");
|
||||
}
|
||||
static target_t ts[MAX_MACS];
|
||||
if (xSemaphoreTake(s_busy, 0) != pdTRUE) {
|
||||
return send_error(req, "409 Conflict", "a driver update is already running");
|
||||
}
|
||||
esp_err_t r;
|
||||
int n = parse_targets(macs, ts);
|
||||
if (n <= 0) {
|
||||
r = send_error(req, "400 Bad Request", n < 0 ? "bad mac list" : "no ESP-NOW drivers");
|
||||
} else {
|
||||
drain_status();
|
||||
r = handle(req, act, query, ts, n, (uint16_t)sess);
|
||||
}
|
||||
xSemaphoreGive(s_busy);
|
||||
return r;
|
||||
}
|
||||
|
||||
esp_err_t drv_ota_register(httpd_handle_t server)
|
||||
{
|
||||
s_status_q = xQueueCreate(16, sizeof(status_t));
|
||||
s_busy = xSemaphoreCreateMutex();
|
||||
if (!s_status_q || !s_busy) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
rel_set_ota_status_cb(on_status);
|
||||
const httpd_uri_t u = { .uri = "/drivers/ota/*", .method = HTTP_POST, .handler = ota_post_handler };
|
||||
return httpd_register_uri_handler(server, &u);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
// Update led-driver app files (.py) over ESP-NOW. Wire format is documented in
|
||||
// led-driver/src/espnow_ota.py; tools/driver_ota.py drives a session:
|
||||
//
|
||||
// POST /drivers/ota/file?mac=<12hex>[,<12hex>...]|all&sess=<1..65535>&path=<p>&sha=<64hex>
|
||||
// body: the raw file (0..64 KB). One driver gets the chunks by acked unicast; several get
|
||||
// one broadcast, then each is asked for its missing chunks and repaired by unicast.
|
||||
// Reply: {"<mac>":{"result":"same|written|error","code":"...","repaired":n}, ...}
|
||||
// POST /drivers/ota/commit?mac=...&sess= drivers move their staged files into place and reset
|
||||
// POST /drivers/ota/abort?mac=...&sess= drivers drop their staged files
|
||||
//
|
||||
// mac=all means every ESP-NOW device in the devices collection. One request runs at a time.
|
||||
esp_err_t drv_ota_register(httpd_handle_t server);
|
||||
+3
-1
@@ -34,6 +34,7 @@
|
||||
#include "web.h"
|
||||
#include "audio.h"
|
||||
#include "ota.h"
|
||||
#include "drv_ota.h"
|
||||
|
||||
static const char *TAG = "eth";
|
||||
|
||||
@@ -205,7 +206,7 @@ static void start_webserver(void)
|
||||
}
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.max_uri_handlers = 64;
|
||||
config.max_uri_handlers = 72;
|
||||
config.stack_size = 8192;
|
||||
config.lru_purge_enable = true;
|
||||
if (httpd_start(&s_server, &config) != ESP_OK) {
|
||||
@@ -217,6 +218,7 @@ static void start_webserver(void)
|
||||
web_register(s_server);
|
||||
audio_register(s_server);
|
||||
ota_register(s_server);
|
||||
drv_ota_register(s_server);
|
||||
api_register(s_server);
|
||||
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
||||
}
|
||||
|
||||
+80
-1
@@ -22,6 +22,7 @@
|
||||
#define M_DIGEST 0x22
|
||||
#define M_RESYNC 0x23
|
||||
#define M_ECHO 0x24
|
||||
#define M_OTA_STATUS 0x26
|
||||
#define F_ACK 1
|
||||
#define F_COMMIT 2
|
||||
#define HDR 9
|
||||
@@ -31,6 +32,7 @@
|
||||
#define FIRST_RETRY_MS 60
|
||||
#define DIGEST_PERIOD_US 1500000
|
||||
#define MAX_PEERS 32
|
||||
#define DONE_RING 16
|
||||
|
||||
static const char *TAG = "rel";
|
||||
|
||||
@@ -59,8 +61,16 @@ typedef struct {
|
||||
int fails;
|
||||
} peer_t;
|
||||
|
||||
typedef struct {
|
||||
bool used, ok;
|
||||
uint8_t mac[6];
|
||||
uint16_t seq;
|
||||
} done_t;
|
||||
|
||||
static pend_t s_pend[MAX_PENDING];
|
||||
static peer_t s_peer[MAX_PEERS];
|
||||
static done_t s_done[DONE_RING]; // recent unicast outcomes, for rel_wait
|
||||
static int s_done_next;
|
||||
static SemaphoreHandle_t s_lock;
|
||||
static QueueHandle_t s_rx;
|
||||
static uint16_t s_seq;
|
||||
@@ -68,6 +78,17 @@ static volatile uint16_t s_ver;
|
||||
static rel_stats_t s_stats;
|
||||
static rel_resync_cb_t s_resync_cb;
|
||||
|
||||
// Caller holds s_lock
|
||||
static void record_done(const pend_t *p, bool ok)
|
||||
{
|
||||
done_t *d = &s_done[s_done_next];
|
||||
s_done_next = (s_done_next + 1) % DONE_RING;
|
||||
d->used = true;
|
||||
d->ok = ok;
|
||||
d->seq = p->seq;
|
||||
memcpy(d->mac, p->mac, 6);
|
||||
}
|
||||
|
||||
static uint32_t full_mask(int cnt)
|
||||
{
|
||||
return cnt >= 32 ? 0xFFFFFFFFu : ((1u << cnt) - 1);
|
||||
@@ -113,6 +134,11 @@ static esp_err_t send_frag(const uint8_t *mac, uint8_t flags, uint16_t seq, uint
|
||||
}
|
||||
|
||||
esp_err_t rel_send(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat)
|
||||
{
|
||||
return rel_send_seq(mac, data, len, commit, repeat, NULL);
|
||||
}
|
||||
|
||||
esp_err_t rel_send_seq(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat, uint16_t *seq_out)
|
||||
{
|
||||
if (len == 0) {
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
@@ -126,6 +152,9 @@ esp_err_t rel_send(const uint8_t *mac, const void *data, size_t len, bool commit
|
||||
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
uint16_t seq = ++s_seq;
|
||||
if (seq_out) {
|
||||
*seq_out = seq;
|
||||
}
|
||||
pend_t *slot = NULL;
|
||||
if (mac) {
|
||||
for (int i = 0; i < MAX_PENDING && !slot; i++) {
|
||||
@@ -169,6 +198,41 @@ esp_err_t rel_send(const uint8_t *mac, const void *data, size_t len, bool commit
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t rel_wait(const uint8_t *mac, uint16_t seq, int timeout_ms)
|
||||
{
|
||||
int64_t until = esp_timer_get_time() + (int64_t)timeout_ms * 1000;
|
||||
for (;;) {
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
for (int i = 0; i < DONE_RING; i++) {
|
||||
const done_t *d = &s_done[i];
|
||||
if (d->used && d->seq == seq && memcmp(d->mac, mac, 6) == 0) {
|
||||
bool ok = d->ok;
|
||||
xSemaphoreGive(s_lock);
|
||||
return ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(s_lock);
|
||||
if (esp_timer_get_time() >= until) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t rel_send_wait(const uint8_t *mac, const void *data, size_t len, int timeout_ms)
|
||||
{
|
||||
uint16_t seq;
|
||||
esp_err_t err;
|
||||
for (int t = 0; t < 50; t++) { // the radio queue is shared with everything else: wait for room
|
||||
err = rel_send_seq(mac, data, len, false, 1, &seq);
|
||||
if (err != ESP_ERR_NO_MEM) {
|
||||
break;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
return err == ESP_OK ? rel_wait(mac, seq, timeout_ms) : err;
|
||||
}
|
||||
|
||||
void rel_bump_ver(void)
|
||||
{
|
||||
uint16_t v = s_ver + 1;
|
||||
@@ -208,6 +272,12 @@ int rel_peer_status(const uint8_t mac[6], int *fails)
|
||||
}
|
||||
|
||||
static rel_announce_cb_t s_announce_cb;
|
||||
static rel_ota_status_cb_t s_ota_status_cb;
|
||||
|
||||
void rel_set_ota_status_cb(rel_ota_status_cb_t cb)
|
||||
{
|
||||
s_ota_status_cb = cb;
|
||||
}
|
||||
|
||||
void rel_set_announce_cb(rel_announce_cb_t cb)
|
||||
{
|
||||
@@ -223,7 +293,8 @@ static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int le
|
||||
// A driver's boot hello is either a 0x4C ANNOUNCE or a small JSON object; everything else we
|
||||
// care about is a link reply inside 0x4C
|
||||
bool announce = (data[0] == WIRE_MAGIC && data[1] == M_ANNOUNCE) || data[0] == '{';
|
||||
bool reply = data[0] == WIRE_MAGIC && (data[1] == M_ACK || data[1] == M_RESYNC || data[1] == M_ECHO);
|
||||
bool reply = data[0] == WIRE_MAGIC &&
|
||||
(data[1] == M_ACK || data[1] == M_RESYNC || data[1] == M_ECHO || data[1] == M_OTA_STATUS);
|
||||
if (!announce && !reply) {
|
||||
return;
|
||||
}
|
||||
@@ -255,6 +326,12 @@ static void handle_rx(const rx_t *rx)
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (rx->data[1] == M_OTA_STATUS) {
|
||||
if (s_ota_status_cb) {
|
||||
s_ota_status_cb(rx->mac, rx->data, rx->len);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (rx->len < 10) { // [4C][21][0][seq u16][cnt][mask u32]
|
||||
return;
|
||||
}
|
||||
@@ -272,6 +349,7 @@ static void handle_rx(const rx_t *rx)
|
||||
pe->fails = 0;
|
||||
}
|
||||
s_stats.acked++;
|
||||
record_done(p, true);
|
||||
free(p->msg);
|
||||
p->used = false;
|
||||
}
|
||||
@@ -296,6 +374,7 @@ static void retry_pending(void)
|
||||
pe->fails++;
|
||||
}
|
||||
s_stats.ack_timeouts++;
|
||||
record_done(p, false);
|
||||
free(p->msg);
|
||||
p->used = false;
|
||||
continue;
|
||||
|
||||
+15
@@ -13,6 +13,7 @@
|
||||
// digest [4C][22][ver u16] (broadcast)
|
||||
// resync [4C][23][ver u16] (driver -> us)
|
||||
// echo [4C][24][beat_idx u16] (driver -> us, bench)
|
||||
// ota [4C][26][sess u16][op][code] + arg (driver -> us, see drv_ota.h)
|
||||
#define REL_FRAG_MAX 238 // inner packet bytes per frame; 4C 03/04 wrappers plus an envelope fit in one
|
||||
#define REL_MAX_FRAGS 32
|
||||
|
||||
@@ -34,6 +35,16 @@ esp_err_t rel_init(void);
|
||||
// acked or given up on. commit makes the driver adopt the current state version.
|
||||
esp_err_t rel_send(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat);
|
||||
|
||||
// rel_send that also returns the sequence number, for rel_wait
|
||||
esp_err_t rel_send_seq(const uint8_t *mac, const void *data, size_t len, bool commit, int repeat, uint16_t *seq);
|
||||
|
||||
// Block until the unicast message (mac, seq) is fully acked (ESP_OK), given up on (ESP_FAIL)
|
||||
// or timeout_ms passes (ESP_ERR_TIMEOUT). Only the last few outcomes are kept, so wait soon after sending.
|
||||
esp_err_t rel_wait(const uint8_t *mac, uint16_t seq, int timeout_ms);
|
||||
|
||||
// Unicast send, retried while the radio queue is full, then rel_wait
|
||||
esp_err_t rel_send_wait(const uint8_t *mac, const void *data, size_t len, int timeout_ms);
|
||||
|
||||
// A broadcast state change happened: advance the version drivers compare in digests
|
||||
void rel_bump_ver(void);
|
||||
uint16_t rel_ver(void);
|
||||
@@ -43,6 +54,10 @@ void rel_set_resync_cb(rel_resync_cb_t cb);
|
||||
// A driver announced itself at boot: a 0x4C ANNOUNCE packet or its JSON hello (called from the rel task)
|
||||
typedef void (*rel_announce_cb_t)(const uint8_t mac[6], const uint8_t *pkt, int len);
|
||||
void rel_set_announce_cb(rel_announce_cb_t cb);
|
||||
|
||||
// A driver's OTA status reply (0x26), called from the rel task
|
||||
typedef void (*rel_ota_status_cb_t)(const uint8_t mac[6], const uint8_t *pkt, int len);
|
||||
void rel_set_ota_status_cb(rel_ota_status_cb_t cb);
|
||||
void rel_stats(rel_stats_t *out);
|
||||
|
||||
// Milliseconds since the driver last acked; -1 if never. fails = consecutive unacked messages
|
||||
|
||||
Reference in new issue
Block a user