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:
jimmyandClaude Opus 5.5 committed 2026-11-02 23:45:38 +13:00
1 parent 1c222771a5
commit 32c0ecfa76
13 files changed
+862 -9

No files matched your search

+80 -1
View File
@@ -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;