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
@@ -22,13 +22,15 @@ TTY := $(shell [ -t 0 ] && echo -it)
|
|||||||
OWNER := $(shell docker info --format '{{.SecurityOptions}}' 2>/dev/null | grep -q rootless && echo 0:0 || echo $$(id -u):$$(id -g))
|
OWNER := $(shell docker info --format '{{.SecurityOptions}}' 2>/dev/null | grep -q rootless && echo 0:0 || echo $$(id -u):$$(id -g))
|
||||||
FIX_OWNER = chown -R $(OWNER) /project
|
FIX_OWNER = chown -R $(OWNER) /project
|
||||||
|
|
||||||
.PHONY: help web build flash ota monitor flash-monitor log size menuconfig clean fullclean erase test test-driver test-envelope
|
.PHONY: help web build flash ota ota-drivers monitor flash-monitor log size menuconfig clean fullclean erase test test-driver test-envelope
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "LED controller (ESP32-S3):"
|
@echo "LED controller (ESP32-S3):"
|
||||||
@echo " make build minify + gzip the web UI, then build the firmware"
|
@echo " make build minify + gzip the web UI, then build the firmware"
|
||||||
@echo " make flash flash firmware and web UI to PORT ($(PORT))"
|
@echo " make flash flash firmware and web UI to PORT ($(PORT))"
|
||||||
@echo " make ota HOST=ip build, then upload the firmware (web UI included) over Ethernet (no USB)"
|
@echo " make ota HOST=ip build, then upload the firmware (web UI included) over Ethernet (no USB)"
|
||||||
|
@echo " make ota-drivers HOST=ip [MAC=aabbccddeeff] [BROADCAST=1]"
|
||||||
|
@echo " push changed led-driver .py files over ESP-NOW (all drivers without MAC)"
|
||||||
@echo " make monitor serial console on PORT (Ctrl-] to quit)"
|
@echo " make monitor serial console on PORT (Ctrl-] to quit)"
|
||||||
@echo " make flash-monitor flash, then open the console"
|
@echo " make flash-monitor flash, then open the console"
|
||||||
@echo " make log [SECS=10] print the console for SECS seconds and exit (no terminal needed)"
|
@echo " make log [SECS=10] print the console for SECS seconds and exit (no terminal needed)"
|
||||||
@@ -56,6 +58,10 @@ ota: build
|
|||||||
@test -n "$(HOST)" || { echo "usage: make ota HOST=<controller ip>"; exit 1; }
|
@test -n "$(HOST)" || { echo "usage: make ota HOST=<controller ip>"; exit 1; }
|
||||||
python3 tools/ota.py $(HOST)
|
python3 tools/ota.py $(HOST)
|
||||||
|
|
||||||
|
ota-drivers:
|
||||||
|
@test -n "$(HOST)" || { echo "usage: make ota-drivers HOST=<controller ip> [MAC=...] [BROADCAST=1]"; exit 1; }
|
||||||
|
python3 tools/driver_ota.py $(HOST) $(if $(MAC),--mac $(MAC),--all) $(if $(BROADCAST),--broadcast)
|
||||||
|
|
||||||
monitor:
|
monitor:
|
||||||
$(DOCKER_RUN) $(TTY) $(DEVICE) $(IDF_IMAGE) idf.py -p $(PORT) monitor
|
$(DOCKER_RUN) $(TTY) $(DEVICE) $(IDF_IMAGE) idf.py -p $(PORT) monitor
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ ESP32-S3 controller for LED drivers: W5500 Ethernet, web UI, I2S microphone beat
|
|||||||
make build # minify the web UI from web/, then build the firmware (ESP-IDF in Docker)
|
make build # minify the web UI from web/, then build the firmware (ESP-IDF in Docker)
|
||||||
make flash # flash over USB
|
make flash # flash over USB
|
||||||
make ota HOST=<ip> # update firmware (web UI included) over Ethernet, no USB (POST /ota)
|
make ota HOST=<ip> # update firmware (web UI included) over Ethernet, no USB (POST /ota)
|
||||||
|
make ota-drivers HOST=<ip> [MAC=..] [BROADCAST=1] # push changed led-driver .py files over ESP-NOW
|
||||||
make monitor # serial console
|
make monitor # serial console
|
||||||
make test # driver unit tests and the C/Python envelope cross-check
|
make test # driver unit tests and the C/Python envelope cross-check
|
||||||
```
|
```
|
||||||
|
|||||||
+10
@@ -64,6 +64,16 @@ Wi-Fi devices are not targeted by `/ws` directly; use **`POST /presets/send`**,
|
|||||||
|
|
||||||
Below, `<id>` values are string identifiers used by the JSON stores (numeric strings in practice).
|
Below, `<id>` values are string identifiers used by the JSON stores (numeric strings in practice).
|
||||||
|
|
||||||
|
### Driver app update over ESP-NOW — `/drivers/ota` (v2 controller)
|
||||||
|
|
||||||
|
Pushes led-driver `.py` files to ESP-NOW drivers, one file per request. `tools/driver_ota.py` (or `make ota-drivers`) runs a whole session. The wire format is described in `led-driver/src/espnow_ota.py`. Only one request runs at a time; a second one gets `409`.
|
||||||
|
|
||||||
|
| Method | Path | Description |
|
||||||
|
|--------|------|-------------|
|
||||||
|
| POST | `/drivers/ota/file?mac=<macs>&sess=<n>&path=<p>&sha=<64 hex>` | Body: the raw file (0 to 65536 bytes). `mac` is a comma-separated list of 12-hex MACs, or `all` for every ESP-NOW device. `sess` is 1–65535 and is shared by all files of one update. `path` is the device path: a root file, `patterns/…` or `lib/…`. Drivers that already have a file with that sha256 skip it. With one target the chunks go by acked unicast. With several, the file is broadcast once, then each driver is asked for its missing chunks and repaired. The file is staged as `<path>.new` until commit. Reply: `{"<mac>":{"result":"same"\|"written"\|"error","code":"…","repaired":n}}`. Error codes come from the driver (`badhash`, `badpath`, `ioerr`, `toobig`, `incomplete`, `nosess`) or the link (`no reply to begin`, `link lost`, …). |
|
||||||
|
| POST | `/drivers/ota/commit?mac=<macs>&sess=<n>` | Each driver moves its staged files into place, updates `file_hashes.json` and resets if anything changed. Reply: `{"<mac>":{"result":"ok"\|"error"}}`. |
|
||||||
|
| POST | `/drivers/ota/abort?mac=<macs>&sess=<n>` | Each driver deletes its staged files. |
|
||||||
|
|
||||||
### Settings — `/settings`
|
### Settings — `/settings`
|
||||||
|
|
||||||
| Method | Path | Description |
|
| Method | Path | Description |
|
||||||
|
|||||||
+38
-2
@@ -272,9 +272,45 @@ Groups: the controller sends a driver its group list when the driver announces i
|
|||||||
|
|
||||||
**Wi-Fi start-up order on the C3.** With the extra link code loaded before Wi-Fi start-up, the C3 failed with `Wifi Unknown Error 0x0101` on about every other cold boot (2 of 4). Starting ESP-NOW before the pattern library and the LED driver claim memory fixed it (6 of 6 boots). `led-driver/src/main.py` now does this.
|
**Wi-Fi start-up order on the C3.** With the extra link code loaded before Wi-Fi start-up, the C3 failed with `Wifi Unknown Error 0x0101` on about every other cold boot (2 of 4). Starting ESP-NOW before the pattern library and the LED driver claim memory fixed it (6 of 6 boots). `led-driver/src/main.py` now does this.
|
||||||
|
|
||||||
## 8. Troubleshooting
|
## 8. Driver app update over ESP-NOW
|
||||||
|
|
||||||
|
Pushes the led-driver `.py` files from the PC, through the controller, to the drivers. The drivers need `espnow_ota.py` first, so install the code once over USB (`led-cli -p <port> --all`). After that:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 tools/driver_ota.py $S3 --mac <mac> --dry-run # list the files and drivers, send nothing
|
||||||
|
python3 tools/driver_ota.py $S3 --mac <mac> # one driver
|
||||||
|
make ota-drivers HOST=$S3 BROADCAST=1 # every ESP-NOW driver, one broadcast and repairs
|
||||||
|
```
|
||||||
|
|
||||||
|
Each file prints `written`, or nothing when the driver already has it. Drivers that received files reset after the commit, and the script waits for them to ack the link again. `--bad-sha <device path>` sends a wrong hash for one file, to test the abort path.
|
||||||
|
|
||||||
|
| Check | Expected |
|
||||||
|
|---|---|
|
||||||
|
| Push with nothing changed | every file skipped, no reset |
|
||||||
|
| Edit one pattern, push | only that file written; the driver resets, comes back and runs the new code; `file_hashes.json` has the new hash |
|
||||||
|
| `--bad-sha patterns/<file>.py` | `ERROR badhash` and `aborted:`; no `.new` files left on the driver and the old file unchanged |
|
||||||
|
| Controller loss at 20% (`beats: false`) | still completes, slower, with `+n repaired` on some files |
|
||||||
|
| `--broadcast` with two or more drivers | each changed file is sent once to all of them; afterwards every driver still acks and fires beats |
|
||||||
|
|
||||||
|
### Results of the first run (S3 controller, two C3 drivers with 119 LEDs, channel 5)
|
||||||
|
|
||||||
|
| | result |
|
||||||
|
|---|---|
|
||||||
|
| 62 files, nothing changed | all skipped in about 7 s (about 110 ms per file, mostly the driver hashing its copy), no reset |
|
||||||
|
| One changed pattern (7.3 KB) | written and committed; driver reset and acked again; content and manifest checked over USB |
|
||||||
|
| 60 KB file, unicast | 4.6 s, 12.8 KB/s including both hash checks |
|
||||||
|
| 60 KB file, unicast, 20% controller loss | 4 of 4 runs completed, 3.8 to 8.3 KB/s |
|
||||||
|
| Wrong hash | `badhash`, aborted, nothing staged or committed |
|
||||||
|
| 60 KB file, broadcast to both | 4.2 s, about 14 KB/s per driver (29 KB/s in total), usually no repairs (once 3) |
|
||||||
|
| 60 KB file, broadcast to both, 20% loss | 2 of 2 completed in about 33 s; 33 to 58 of the 65 chunks needed a repair (each chunk is 4 frames with no radio retries) |
|
||||||
|
| Beat echo round trip after the change to `esp.irq()`, 177 beats, two drivers | 2.4 / 5.5 / 23.5 ms (min / avg / max) beyond the 30 ms lead, against 9.8 / 15.0 / 25.7 ms when the driver polled; 176 of 177 echoed |
|
||||||
|
|
||||||
|
**Broadcast pacing.** With 12 ms between broadcast chunks (about 30 KB/s) the drivers stopped receiving ESP-NOW frames altogether. Their `esp.stats()` receive count froze, with no drops counted, while the controller's unicasts were still acked by the drivers' radios. It lasted until a reset, more than 5 minutes once, and beats stopped too. The likely cause is the C3 writing to flash while the frames keep coming. At 50 ms (`BCAST_GAP_MS` in `main/drv_ota.c`) it did not happen in 5 runs.
|
||||||
|
|
||||||
|
## 9. Troubleshooting
|
||||||
|
|
||||||
- **A board on a USB-serial/JTAG port (C3, and the S3 controller) resets whenever a program opens its port,** and a C3 can land in ROM download mode (`wait usb download`). The old apt `mpremote` 0.4.0 hangs on this. Use the newer one in `~/projects/micropython/micropython/tools/mpremote/mpremote.py`, or `led-tool`, and note that `fs cp -r` fails on drivers that print timestamps (copy named files instead).
|
- **A board on a USB-serial/JTAG port (C3, and the S3 controller) resets whenever a program opens its port,** and a C3 can land in ROM download mode (`wait usb download`). The old apt `mpremote` 0.4.0 hangs on this. Use the newer one in `~/projects/micropython/micropython/tools/mpremote/mpremote.py`, or `led-tool`, and note that `fs cp -r` fails on drivers that print timestamps (copy named files instead).
|
||||||
|
- **Only one program can use a board's serial port at a time.** A serial logger left running while `led-cli` uploads corrupts the transfer silently: `led-cli` reports the files uploaded, but the old ones stay on the driver.
|
||||||
- **Running a long `mpremote exec` on the driver resets it:** the driver's watchdog (10 s) keeps running after `mpremote` interrupts `main.py`, so keep REPL experiments under about 8 s and reset the driver afterwards.
|
- **Running a long `mpremote exec` on the driver resets it:** the driver's watchdog (10 s) keeps running after `mpremote` interrupts `main.py`, so keep REPL experiments under about 8 s and reset the driver afterwards.
|
||||||
|
|
||||||
- **Driver never reacts, no acks:** channel mismatch. Check `CONFIG_ESPNOW_CHANNEL` against the driver's `wifi_channel`. Neighbouring channels can still work at bench range, so a working link does not prove they match.
|
- **Driver never reacts, no acks:** channel mismatch. Check `CONFIG_ESPNOW_CHANNEL` against the driver's `wifi_channel`. Neighbouring channels can still work at bench range, so a working link does not prove they match.
|
||||||
@@ -284,7 +320,7 @@ Groups: the controller sends a driver its group list when the driver announces i
|
|||||||
- **Resync never triggers:** the driver ignores a digest equal to its own version; confirm with `/status` that `link.ver` changed after the missed play.
|
- **Resync never triggers:** the driver ignores a digest equal to its own version; confirm with `/status` that `link.ver` changed after the missed play.
|
||||||
- **Web UI is blank:** the web UI was not packed before the build; run `tools/build_web.sh` and build again (`make build` does this).
|
- **Web UI is blank:** the web UI was not packed before the build; run `tools/build_web.sh` and build again (`make build` does this).
|
||||||
|
|
||||||
## 9. Cleanup
|
## 10. Cleanup
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -s -X POST -d '{"percent":0}' http://$S3/test/loss
|
curl -s -X POST -d '{"percent":0}' http://$S3/test/loss
|
||||||
|
|||||||
+1
-1
Submodule led-driver updated: 940d1d0a77...495da4e366.
+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 "."
|
INCLUDE_DIRS "."
|
||||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
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
|
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 "web.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "drv_ota.h"
|
||||||
|
|
||||||
static const char *TAG = "eth";
|
static const char *TAG = "eth";
|
||||||
|
|
||||||
@@ -205,7 +206,7 @@ static void start_webserver(void)
|
|||||||
}
|
}
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||||
config.max_uri_handlers = 64;
|
config.max_uri_handlers = 72;
|
||||||
config.stack_size = 8192;
|
config.stack_size = 8192;
|
||||||
config.lru_purge_enable = true;
|
config.lru_purge_enable = true;
|
||||||
if (httpd_start(&s_server, &config) != ESP_OK) {
|
if (httpd_start(&s_server, &config) != ESP_OK) {
|
||||||
@@ -217,6 +218,7 @@ static void start_webserver(void)
|
|||||||
web_register(s_server);
|
web_register(s_server);
|
||||||
audio_register(s_server);
|
audio_register(s_server);
|
||||||
ota_register(s_server);
|
ota_register(s_server);
|
||||||
|
drv_ota_register(s_server);
|
||||||
api_register(s_server);
|
api_register(s_server);
|
||||||
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
||||||
}
|
}
|
||||||
|
|||||||
+80
-1
@@ -22,6 +22,7 @@
|
|||||||
#define M_DIGEST 0x22
|
#define M_DIGEST 0x22
|
||||||
#define M_RESYNC 0x23
|
#define M_RESYNC 0x23
|
||||||
#define M_ECHO 0x24
|
#define M_ECHO 0x24
|
||||||
|
#define M_OTA_STATUS 0x26
|
||||||
#define F_ACK 1
|
#define F_ACK 1
|
||||||
#define F_COMMIT 2
|
#define F_COMMIT 2
|
||||||
#define HDR 9
|
#define HDR 9
|
||||||
@@ -31,6 +32,7 @@
|
|||||||
#define FIRST_RETRY_MS 60
|
#define FIRST_RETRY_MS 60
|
||||||
#define DIGEST_PERIOD_US 1500000
|
#define DIGEST_PERIOD_US 1500000
|
||||||
#define MAX_PEERS 32
|
#define MAX_PEERS 32
|
||||||
|
#define DONE_RING 16
|
||||||
|
|
||||||
static const char *TAG = "rel";
|
static const char *TAG = "rel";
|
||||||
|
|
||||||
@@ -59,8 +61,16 @@ typedef struct {
|
|||||||
int fails;
|
int fails;
|
||||||
} peer_t;
|
} peer_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool used, ok;
|
||||||
|
uint8_t mac[6];
|
||||||
|
uint16_t seq;
|
||||||
|
} done_t;
|
||||||
|
|
||||||
static pend_t s_pend[MAX_PENDING];
|
static pend_t s_pend[MAX_PENDING];
|
||||||
static peer_t s_peer[MAX_PEERS];
|
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 SemaphoreHandle_t s_lock;
|
||||||
static QueueHandle_t s_rx;
|
static QueueHandle_t s_rx;
|
||||||
static uint16_t s_seq;
|
static uint16_t s_seq;
|
||||||
@@ -68,6 +78,17 @@ static volatile uint16_t s_ver;
|
|||||||
static rel_stats_t s_stats;
|
static rel_stats_t s_stats;
|
||||||
static rel_resync_cb_t s_resync_cb;
|
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)
|
static uint32_t full_mask(int cnt)
|
||||||
{
|
{
|
||||||
return cnt >= 32 ? 0xFFFFFFFFu : ((1u << cnt) - 1);
|
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)
|
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) {
|
if (len == 0) {
|
||||||
return ESP_ERR_INVALID_SIZE;
|
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);
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
uint16_t seq = ++s_seq;
|
uint16_t seq = ++s_seq;
|
||||||
|
if (seq_out) {
|
||||||
|
*seq_out = seq;
|
||||||
|
}
|
||||||
pend_t *slot = NULL;
|
pend_t *slot = NULL;
|
||||||
if (mac) {
|
if (mac) {
|
||||||
for (int i = 0; i < MAX_PENDING && !slot; i++) {
|
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;
|
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)
|
void rel_bump_ver(void)
|
||||||
{
|
{
|
||||||
uint16_t v = s_ver + 1;
|
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_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)
|
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
|
// 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
|
// care about is a link reply inside 0x4C
|
||||||
bool announce = (data[0] == WIRE_MAGIC && data[1] == M_ANNOUNCE) || data[0] == '{';
|
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) {
|
if (!announce && !reply) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -255,6 +326,12 @@ static void handle_rx(const rx_t *rx)
|
|||||||
}
|
}
|
||||||
return;
|
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]
|
if (rx->len < 10) { // [4C][21][0][seq u16][cnt][mask u32]
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -272,6 +349,7 @@ static void handle_rx(const rx_t *rx)
|
|||||||
pe->fails = 0;
|
pe->fails = 0;
|
||||||
}
|
}
|
||||||
s_stats.acked++;
|
s_stats.acked++;
|
||||||
|
record_done(p, true);
|
||||||
free(p->msg);
|
free(p->msg);
|
||||||
p->used = false;
|
p->used = false;
|
||||||
}
|
}
|
||||||
@@ -296,6 +374,7 @@ static void retry_pending(void)
|
|||||||
pe->fails++;
|
pe->fails++;
|
||||||
}
|
}
|
||||||
s_stats.ack_timeouts++;
|
s_stats.ack_timeouts++;
|
||||||
|
record_done(p, false);
|
||||||
free(p->msg);
|
free(p->msg);
|
||||||
p->used = false;
|
p->used = false;
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
+15
@@ -13,6 +13,7 @@
|
|||||||
// digest [4C][22][ver u16] (broadcast)
|
// digest [4C][22][ver u16] (broadcast)
|
||||||
// resync [4C][23][ver u16] (driver -> us)
|
// resync [4C][23][ver u16] (driver -> us)
|
||||||
// echo [4C][24][beat_idx u16] (driver -> us, bench)
|
// 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_FRAG_MAX 238 // inner packet bytes per frame; 4C 03/04 wrappers plus an envelope fit in one
|
||||||
#define REL_MAX_FRAGS 32
|
#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.
|
// 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);
|
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
|
// A broadcast state change happened: advance the version drivers compare in digests
|
||||||
void rel_bump_ver(void);
|
void rel_bump_ver(void);
|
||||||
uint16_t rel_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)
|
// 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);
|
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);
|
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);
|
void rel_stats(rel_stats_t *out);
|
||||||
|
|
||||||
// Milliseconds since the driver last acked; -1 if never. fails = consecutive unacked messages
|
// Milliseconds since the driver last acked; -1 if never. fails = consecutive unacked messages
|
||||||
|
|||||||
Executable
+221
@@ -0,0 +1,221 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Update the led-driver app files (.py) over ESP-NOW, through the controller.
|
||||||
|
|
||||||
|
driver_ota.py <controller> (--mac MAC [MAC ...] | --all) [--broadcast] [--driver-dir DIR] [--dry-run]
|
||||||
|
|
||||||
|
The file set matches `led-cli --src --patterns --lib`: src/ (minus patterns/) to the root,
|
||||||
|
src/patterns/ to patterns/, lib/ to lib/. Drivers skip files they already have (sha256), stage
|
||||||
|
the rest, and only move them into place on commit, then reset.
|
||||||
|
|
||||||
|
Without --broadcast drivers are updated one after another. With it, each changed file is
|
||||||
|
broadcast once and every driver is then repaired with the chunks it missed.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import http.client
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
||||||
|
SKIP_DIRS = {"__pycache__"}
|
||||||
|
SKIP_FILES = {"file_hashes.json"}
|
||||||
|
MAX_FILE = 65536
|
||||||
|
|
||||||
|
|
||||||
|
def file_set(driver_dir):
|
||||||
|
"""[(device path, local path)] in a stable order."""
|
||||||
|
out = []
|
||||||
|
|
||||||
|
def walk(local, dest, exclude=()):
|
||||||
|
for name in sorted(os.listdir(local)):
|
||||||
|
full = os.path.join(local, name)
|
||||||
|
if os.path.isdir(full):
|
||||||
|
if name not in SKIP_DIRS and name not in exclude:
|
||||||
|
walk(full, f"{dest}/{name}" if dest else name)
|
||||||
|
elif not name.endswith((".pyc", ".pyo")) and name not in SKIP_FILES:
|
||||||
|
out.append((f"{dest}/{name}" if dest else name, full))
|
||||||
|
|
||||||
|
src = os.path.join(driver_dir, "src")
|
||||||
|
walk(src, "", exclude=("patterns",))
|
||||||
|
if os.path.isdir(os.path.join(src, "patterns")):
|
||||||
|
walk(os.path.join(src, "patterns"), "patterns")
|
||||||
|
if os.path.isdir(os.path.join(driver_dir, "lib")):
|
||||||
|
walk(os.path.join(driver_dir, "lib"), "lib")
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
class Controller:
|
||||||
|
def __init__(self, host):
|
||||||
|
self.host = host
|
||||||
|
|
||||||
|
def request(self, method, path, body=None, timeout=60):
|
||||||
|
c = http.client.HTTPConnection(self.host, 80, timeout=timeout)
|
||||||
|
headers = {"Content-Type": "application/octet-stream"} if body is not None else {}
|
||||||
|
c.request(method, path, body=body, headers=headers)
|
||||||
|
r = c.getresponse()
|
||||||
|
data = r.read().decode()
|
||||||
|
try:
|
||||||
|
doc = json.loads(data) if data else {}
|
||||||
|
except ValueError:
|
||||||
|
doc = {"error": data}
|
||||||
|
if r.status != 200:
|
||||||
|
raise RuntimeError(f"{method} {path.split('?')[0]}: {r.status} {doc.get('error', doc)}")
|
||||||
|
return doc
|
||||||
|
|
||||||
|
def drivers(self):
|
||||||
|
"""device id -> MAC of every ESP-NOW device."""
|
||||||
|
devs = self.request("GET", "/devices", timeout=10)
|
||||||
|
return {k: d["address"] for k, d in devs.items()
|
||||||
|
if d.get("transport") == "espnow" and len(d.get("address") or "") == 12}
|
||||||
|
|
||||||
|
def status(self):
|
||||||
|
try:
|
||||||
|
return self.request("GET", "/devices/status", timeout=10)
|
||||||
|
except (OSError, RuntimeError):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def ota(self, action, macs, sess, body=None, **params):
|
||||||
|
# the controller does not URL-decode, so keep path slashes and the MAC list commas as they are
|
||||||
|
q = urllib.parse.urlencode({"mac": ",".join(macs), "sess": sess, **params}, safe="/,")
|
||||||
|
return self.request("POST", f"/drivers/ota/{action}?{q}", body=body, timeout=120)
|
||||||
|
|
||||||
|
|
||||||
|
def norm_mac(m):
|
||||||
|
m = m.lower().replace(":", "").replace("-", "")
|
||||||
|
if len(m) != 12 or any(ch not in "0123456789abcdef" for ch in m):
|
||||||
|
sys.exit(f"bad MAC: {m}")
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
def push(ctrl, macs, files, bad_sha=None):
|
||||||
|
"""One session for macs. Returns (MACs that committed, MACs that will reset)."""
|
||||||
|
sess = random.randint(1, 0xFFFF)
|
||||||
|
ok = set(macs)
|
||||||
|
t0 = time.time()
|
||||||
|
sent_bytes = 0
|
||||||
|
send_time = 0.0
|
||||||
|
per_driver = 0 # bytes of the files that went to at least one driver
|
||||||
|
changed = set()
|
||||||
|
for dest, local in files:
|
||||||
|
live = [m for m in macs if m in ok]
|
||||||
|
if not live:
|
||||||
|
break
|
||||||
|
with open(local, "rb") as f:
|
||||||
|
body = f.read()
|
||||||
|
sha = hashlib.sha256(body).hexdigest()
|
||||||
|
if dest == bad_sha:
|
||||||
|
sha = "0" * 64
|
||||||
|
t1 = time.time()
|
||||||
|
try:
|
||||||
|
res = ctrl.ota("file", live, sess, body=body, path=dest, sha=sha)
|
||||||
|
except (OSError, RuntimeError) as e:
|
||||||
|
res = {m: {"result": "error", "code": str(e)} for m in live}
|
||||||
|
t1 = time.time() - t1
|
||||||
|
line = []
|
||||||
|
for m in live:
|
||||||
|
r = res.get(m, {"result": "error", "code": "missing from reply"})
|
||||||
|
if r["result"] == "error":
|
||||||
|
ok.discard(m)
|
||||||
|
line.append(f"{m} ERROR {r.get('code')}")
|
||||||
|
elif r["result"] == "written":
|
||||||
|
sent_bytes += len(body)
|
||||||
|
changed.add(m)
|
||||||
|
rep = f" (+{r['repaired']} repaired)" if r.get("repaired") else ""
|
||||||
|
line.append(f"{m} written{rep}")
|
||||||
|
writers = sum(1 for m in live if res.get(m, {}).get("result") == "written")
|
||||||
|
if writers:
|
||||||
|
send_time += t1
|
||||||
|
per_driver += len(body)
|
||||||
|
if line:
|
||||||
|
print(f" {dest:40s} {len(body):6d} B {t1:5.1f} s " + ", ".join(line))
|
||||||
|
dt = time.time() - t0
|
||||||
|
failed = [m for m in macs if m not in ok]
|
||||||
|
if failed:
|
||||||
|
ctrl.ota("abort", failed, sess)
|
||||||
|
print(" aborted:", ", ".join(failed))
|
||||||
|
good = [m for m in macs if m in ok]
|
||||||
|
if good:
|
||||||
|
res = ctrl.ota("commit", good, sess)
|
||||||
|
for m in good:
|
||||||
|
if res.get(m, {}).get("result") != "ok":
|
||||||
|
print(f" {m} commit failed: {res.get(m)}")
|
||||||
|
good = [m for m in good if res.get(m, {}).get("result") == "ok"]
|
||||||
|
if sent_bytes:
|
||||||
|
rate = per_driver / send_time / 1024 if send_time else 0
|
||||||
|
print(f" {sent_bytes} B written, {dt:.1f} s in all, {rate:.1f} KB/s while sending")
|
||||||
|
else:
|
||||||
|
print(" every file was already up to date")
|
||||||
|
return good, [m for m in good if m in changed]
|
||||||
|
|
||||||
|
|
||||||
|
def wait_back(ctrl, macs, timeout=30):
|
||||||
|
"""Drivers reset after a commit; wait until each acks the link again."""
|
||||||
|
ids = {mac: k for k, mac in ctrl.drivers().items()} # /devices/status is keyed by device id
|
||||||
|
t0 = time.time()
|
||||||
|
left = set(macs)
|
||||||
|
time.sleep(2)
|
||||||
|
while left and time.time() - t0 < timeout:
|
||||||
|
st = ctrl.status()
|
||||||
|
for m in list(left):
|
||||||
|
age = st.get(ids.get(m), {}).get("last_ack_ms_ago", -1)
|
||||||
|
if 0 <= age < (time.time() - t0) * 1000:
|
||||||
|
left.discard(m)
|
||||||
|
time.sleep(1)
|
||||||
|
return left
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
ap.add_argument("controller")
|
||||||
|
g = ap.add_mutually_exclusive_group(required=True)
|
||||||
|
g.add_argument("--mac", nargs="+")
|
||||||
|
g.add_argument("--all", action="store_true", help="every ESP-NOW device in the controller's devices")
|
||||||
|
ap.add_argument("--broadcast", action="store_true", help="one transfer for all drivers, then repair each")
|
||||||
|
ap.add_argument("--driver-dir", default=os.path.join(ROOT, "led-driver"))
|
||||||
|
ap.add_argument("--dry-run", action="store_true", help="list the files and drivers, send nothing")
|
||||||
|
ap.add_argument("--bad-sha", metavar="PATH", help="test: send a wrong hash for this device path")
|
||||||
|
a = ap.parse_args()
|
||||||
|
|
||||||
|
files = file_set(a.driver_dir)
|
||||||
|
odd = [d for d, _ in files if not all(ch.isalnum() or ch in "_./-" for ch in d)]
|
||||||
|
if odd:
|
||||||
|
sys.exit(f"file names must be letters, digits and _ . / - only: {', '.join(odd)}")
|
||||||
|
big = [d for d, p in files if os.path.getsize(p) > MAX_FILE]
|
||||||
|
if big:
|
||||||
|
sys.exit(f"files over {MAX_FILE} B cannot be sent: {', '.join(big)}")
|
||||||
|
ctrl = Controller(a.controller)
|
||||||
|
macs = sorted(ctrl.drivers().values()) if a.all else [norm_mac(m) for m in a.mac]
|
||||||
|
if not macs:
|
||||||
|
sys.exit("no ESP-NOW drivers")
|
||||||
|
total = sum(os.path.getsize(p) for _, p in files)
|
||||||
|
print(f"{len(files)} files, {total} B, to {len(macs)} driver(s)" + (" by broadcast" if a.broadcast else ""))
|
||||||
|
if a.dry_run:
|
||||||
|
for d, p in files:
|
||||||
|
print(f" {d:40s} {os.path.getsize(p):6d} B")
|
||||||
|
print(" drivers:", ", ".join(macs))
|
||||||
|
return
|
||||||
|
|
||||||
|
if a.broadcast:
|
||||||
|
done, resets = push(ctrl, macs, files, a.bad_sha)
|
||||||
|
else:
|
||||||
|
done, resets = [], []
|
||||||
|
for m in macs:
|
||||||
|
print(m)
|
||||||
|
d, r = push(ctrl, [m], files, a.bad_sha)
|
||||||
|
done += d
|
||||||
|
resets += r
|
||||||
|
failed = [m for m in macs if m not in done]
|
||||||
|
if resets:
|
||||||
|
missing = wait_back(ctrl, resets)
|
||||||
|
if missing:
|
||||||
|
print("did not ack the link again within 30 s:", ", ".join(sorted(missing)))
|
||||||
|
print(f"{len(done)} of {len(macs)} driver(s) updated")
|
||||||
|
sys.exit(1 if failed else 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -7,11 +7,12 @@ root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "led-drive
|
|||||||
sys.path[:0] = [os.path.join(root, "src"), os.path.join(root, "tests")]
|
sys.path[:0] = [os.path.join(root, "src"), os.path.join(root, "tests")]
|
||||||
|
|
||||||
import test_beat_sync # noqa: E402
|
import test_beat_sync # noqa: E402
|
||||||
|
import test_espnow_ota # noqa: E402
|
||||||
import test_espnow_rx # noqa: E402
|
import test_espnow_rx # noqa: E402
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
for mod in (test_espnow_rx, test_beat_sync):
|
for mod in (test_espnow_rx, test_beat_sync, test_espnow_ota):
|
||||||
for name, fn in vars(mod).items():
|
for name, fn in list(vars(mod).items()):
|
||||||
if name.startswith("test_") and callable(fn):
|
if name.startswith("test_") and callable(fn):
|
||||||
fn()
|
fn()
|
||||||
n += 1
|
n += 1
|
||||||
|
|||||||
Reference in new issue
Block a user