feat: sequences v2, driver frame sync and submodule updates
- Sequences v2, bridges and sim-controller changes from the working tree. - Records the led-driver, led-tool and led-simulator commits above. - Includes dmx-driver/ and graphify-out/. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f8f67823b
commit
50ca46fee6
657 files changed
+15818
-1107
No files matched your search
+2
-2
@@ -1,13 +1,13 @@
|
||||
if(IDF_TARGET STREQUAL "linux")
|
||||
# Host build for led-simulator (sim-controller/): no Ethernet, Wi-Fi, mic or OTA. The sim
|
||||
# component has app_main and stands in for main.c, mic.c and ota.c.
|
||||
idf_component_register(SRCS "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "drv_ota.c"
|
||||
idf_component_register(SRCS "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c"
|
||||
INCLUDE_DIRS "." "../sim-controller/components/sim/include"
|
||||
REQUIRES esp_http_server esp_timer esp_event json
|
||||
PRIV_REQUIRES mbedtls lwip esp_app_format)
|
||||
else()
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c" "drv_ota.c"
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
|
||||
@@ -41,6 +41,10 @@ bool api_mac(const cJSON *v, char out[13]);
|
||||
cJSON *api_driver_config(const cJSON *name, const cJSON *num_leds, const cJSON *color_order, const cJSON *startup);
|
||||
// Combined 0..255 brightness for a driver: global x its groups x device [x zone_b when >= 0]
|
||||
int api_effective_brightness(const char *mac, int zone_b);
|
||||
|
||||
// Merge settings a driver reported (num_leds, color_order, startup_mode, max_brightness, reversed)
|
||||
// into its device record. cfg is the JSON object, len bytes (not terminated). True when the record changed.
|
||||
bool api_apply_driver_cfg(cJSON *rec, const char *cfg, int len);
|
||||
// Blink every member of a group, then "off"
|
||||
esp_err_t api_identify_group(const char *group_id);
|
||||
|
||||
|
||||
-504
@@ -1,504 +0,0 @@
|
||||
#include "drv_ota.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "cJSON.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_random.h"
|
||||
#include "mbedtls/sha256.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;
|
||||
}
|
||||
|
||||
cJSON *drv_ota_send(const char *macs, const char *path, const uint8_t *data, size_t len, bool commit, esp_err_t *err)
|
||||
{
|
||||
static target_t ts[MAX_MACS];
|
||||
if (len > MAX_FILE || strlen(path) > MAX_PATH) {
|
||||
*err = ESP_ERR_INVALID_SIZE;
|
||||
return NULL;
|
||||
}
|
||||
if (!s_busy || xSemaphoreTake(s_busy, 0) != pdTRUE) {
|
||||
*err = ESP_ERR_INVALID_STATE;
|
||||
return NULL;
|
||||
}
|
||||
cJSON *out = NULL;
|
||||
int n = parse_targets(macs, ts);
|
||||
if (n <= 0) {
|
||||
*err = n < 0 ? ESP_ERR_INVALID_ARG : ESP_ERR_NOT_FOUND;
|
||||
} else {
|
||||
uint8_t sha[32];
|
||||
mbedtls_sha256(data, len, sha, 0);
|
||||
uint16_t sess = (esp_random() % 0xFFFF) + 1;
|
||||
drain_status();
|
||||
ESP_LOGI(TAG, "%s (%u B) to %d driver(s), session %u", path, (unsigned)len, n, sess);
|
||||
transfer(ts, n, sess, path, sha, data, len);
|
||||
out = results(ts, n, true);
|
||||
if (commit) {
|
||||
uint8_t pkt[4] = { WIRE_MAGIC, OP_COMMIT, sess & 255, sess >> 8 };
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (ts[i].state == C_OK) {
|
||||
command(ts[i].mac, pkt, sizeof(pkt), sess, OP_COMMIT, BEGIN_MS, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
*err = ESP_OK;
|
||||
}
|
||||
xSemaphoreGive(s_busy);
|
||||
return out;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "cJSON.h"
|
||||
#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);
|
||||
|
||||
// Send one file the same way from C (macs as above). With commit, drivers that wrote it switch to it
|
||||
// and reset. Returns the per-driver results (caller owns), or NULL with *err: ESP_ERR_INVALID_STATE
|
||||
// when an update is running, ESP_ERR_NOT_FOUND with no drivers, ESP_ERR_INVALID_ARG for a bad list,
|
||||
// ESP_ERR_INVALID_SIZE for a file over 64 KB.
|
||||
cJSON *drv_ota_send(const char *macs, const char *path, const uint8_t *data, size_t len, bool commit, esp_err_t *err);
|
||||
+32
-3
@@ -5,6 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "api_util.h"
|
||||
#include "envelope.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
@@ -959,17 +960,32 @@ void engine_groups_push_all(int *total, int *sent, int *failed)
|
||||
static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
{
|
||||
char name[48] = "";
|
||||
char *cfg = NULL; // the driver's settings as JSON
|
||||
int cfg_len = 0;
|
||||
if (pkt[0] == '{') {
|
||||
cJSON *j = cJSON_ParseWithLength((const char *)pkt, len);
|
||||
const cJSON *n = cJSON_GetObjectItem(j, "name");
|
||||
if (cJSON_IsString(n)) {
|
||||
snprintf(name, sizeof(name), "%s", n->valuestring);
|
||||
}
|
||||
const cJSON *c = cJSON_GetObjectItem(j, "cfg");
|
||||
if (cJSON_IsObject(c)) {
|
||||
cfg = cJSON_PrintUnformatted(c);
|
||||
cfg_len = cfg ? (int)strlen(cfg) : 0;
|
||||
}
|
||||
cJSON_Delete(j);
|
||||
} else if (len > 3 && pkt[2] > 0 && 3 + pkt[2] <= len) { // 4C 01 <len><name> ...
|
||||
snprintf(name, sizeof(name), "%.*s", pkt[2] < sizeof(name) - 1 ? pkt[2] : (int)sizeof(name) - 1, pkt + 3);
|
||||
int co = 3 + pkt[2] + 6; // after u16 num_leds, colour order, startup mode, brightness, type: [len][json]
|
||||
if (co < len && pkt[co] > 0 && co + 1 + pkt[co] <= len) {
|
||||
cfg = (char *)pkt + co + 1;
|
||||
cfg_len = pkt[co];
|
||||
}
|
||||
}
|
||||
if (!name[0]) {
|
||||
if (pkt[0] == '{') {
|
||||
free(cfg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
char hex[13];
|
||||
@@ -985,11 +1001,24 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
cJSON_AddStringToObject(rec, "type", "led");
|
||||
cJSON_AddStringToObject(rec, "transport", "espnow");
|
||||
cJSON_AddStringToObject(rec, "address", hex);
|
||||
api_apply_driver_cfg(rec, cfg, cfg_len);
|
||||
store_save("device");
|
||||
ESP_LOGI(TAG, "new driver %s (%s)", name, hex);
|
||||
} else if (!cJSON_IsString(old) || strcmp(old->valuestring, name) != 0) {
|
||||
cJSON_ReplaceItemInObject(rec, "name", cJSON_CreateString(name));
|
||||
store_save("device");
|
||||
} else {
|
||||
bool changed = false;
|
||||
if (!cJSON_IsString(old) || strcmp(old->valuestring, name) != 0) {
|
||||
cJSON_ReplaceItemInObject(rec, "name", cJSON_CreateString(name));
|
||||
changed = true;
|
||||
}
|
||||
if (api_apply_driver_cfg(rec, cfg, cfg_len)) {
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
store_save("device");
|
||||
}
|
||||
}
|
||||
if (pkt[0] == '{') {
|
||||
free(cfg);
|
||||
}
|
||||
send_groups(mac, name, false);
|
||||
store_unlock();
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
#include "web.h"
|
||||
#include "audio.h"
|
||||
#include "ota.h"
|
||||
#include "drv_ota.h"
|
||||
|
||||
static const char *TAG = "eth";
|
||||
|
||||
@@ -310,7 +309,6 @@ static void start_webserver(void)
|
||||
audio_register(s_server);
|
||||
audio_api_register(s_server);
|
||||
ota_register(s_server);
|
||||
drv_ota_register(s_server);
|
||||
api_register(s_server);
|
||||
ws_register(s_server);
|
||||
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
||||
|
||||
@@ -10,12 +10,10 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include "api_util.h"
|
||||
#include "drv_ota.h"
|
||||
#include "esp_log.h"
|
||||
#include "store.h"
|
||||
#include "web.h"
|
||||
|
||||
static const char *TAG = "patterns";
|
||||
|
||||
#define PAT_DIR "/db/patterns"
|
||||
#define CODE_MAX (64 * 1024)
|
||||
@@ -410,76 +408,6 @@ static esp_err_t create_driver_pattern(httpd_req_t *req)
|
||||
return api_json(req, "201 Created", o, true);
|
||||
}
|
||||
|
||||
// POST /patterns/<name>/send {device_id?}: push the source to one driver or every ESP-NOW driver
|
||||
// (patterns/<name>.py on the driver), which then resets to load it
|
||||
static esp_err_t send_pattern(httpd_req_t *req, const char *name)
|
||||
{
|
||||
char base[NAME_MAX_LEN + 1];
|
||||
if (!base_name(name, base)) {
|
||||
return api_error(req, "400 Bad Request", "Invalid pattern name");
|
||||
}
|
||||
if (builtin(base)) {
|
||||
return api_error(req, "400 Bad Request", "on and off are built into the driver firmware");
|
||||
}
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
char macs[16] = "all";
|
||||
const cJSON *did = cJSON_GetObjectItem(body, "device_id");
|
||||
if (cJSON_IsString(did) && did->valuestring[0]) {
|
||||
const cJSON *dev = cJSON_GetObjectItem(store_coll("device"), did->valuestring);
|
||||
const cJSON *tr = cJSON_GetObjectItem(dev, "transport"), *addr = cJSON_GetObjectItem(dev, "address");
|
||||
if (!dev) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "404 Not Found", "Device not found");
|
||||
}
|
||||
if (!cJSON_IsString(tr) || strcmp(tr->valuestring, "espnow") || !cJSON_IsString(addr) ||
|
||||
strlen(addr->valuestring) != 12) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", "Device is not an ESP-NOW driver");
|
||||
}
|
||||
snprintf(macs, sizeof(macs), "%s", addr->valuestring);
|
||||
}
|
||||
cJSON_Delete(body);
|
||||
const uint8_t *data;
|
||||
size_t len;
|
||||
bool heap;
|
||||
if (!load(base, &data, &len, &heap)) {
|
||||
return api_error(req, "404 Not Found", "Pattern file not found");
|
||||
}
|
||||
char path[NAME_MAX_LEN + 16];
|
||||
snprintf(path, sizeof(path), "patterns/%s.py", base);
|
||||
esp_err_t err;
|
||||
store_unlock(); // the transfer takes seconds; the sequencer must keep running
|
||||
cJSON *results = drv_ota_send(macs, path, data, len, true, &err);
|
||||
store_lock();
|
||||
if (heap) {
|
||||
free((void *)data);
|
||||
}
|
||||
if (!results) {
|
||||
return err == ESP_ERR_INVALID_STATE ? api_error(req, "409 Conflict", "a driver update is already running")
|
||||
: err == ESP_ERR_NOT_FOUND ? api_error(req, "404 Not Found", "No ESP-NOW drivers")
|
||||
: api_error(req, "400 Bad Request", "Pattern cannot be sent");
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject(), *ids = cJSON_CreateArray(), *it;
|
||||
cJSON_ArrayForEach(it, results)
|
||||
{
|
||||
const cJSON *res = cJSON_GetObjectItem(it, "result");
|
||||
if (cJSON_IsString(res) && strcmp(res->valuestring, "error")) {
|
||||
cJSON_AddItemToArray(ids, cJSON_CreateString(it->string));
|
||||
}
|
||||
}
|
||||
int sent = cJSON_GetArraySize(ids);
|
||||
ESP_LOGI(TAG, "%s sent to %d driver(s)", path, sent);
|
||||
cJSON_AddStringToObject(o, sent ? "message" : "error", sent ? "Pattern sent" : "No driver accepted the pattern");
|
||||
cJSON_AddStringToObject(o, "pattern", path + strlen("patterns/"));
|
||||
cJSON_AddItemToObject(o, "device_ids", ids);
|
||||
cJSON_AddNumberToObject(o, "sent_count", sent);
|
||||
cJSON_AddItemToObject(o, "results", results);
|
||||
return api_json(req, sent ? "200 OK" : "503 Service Unavailable", o, true);
|
||||
}
|
||||
|
||||
bool patterns_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
{
|
||||
static const char FILE_PREFIX[] = "/patterns/ota/file/";
|
||||
@@ -522,8 +450,6 @@ bool patterns_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
*ret = upload(req);
|
||||
} else if (n == 2 && !strcmp(seg[1], "driver")) {
|
||||
*ret = create_driver_pattern(req);
|
||||
} else if (n == 3 && !strcmp(seg[2], "send")) {
|
||||
*ret = send_pattern(req, seg[1]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
+2
-16
@@ -21,7 +21,6 @@
|
||||
#define M_DIGEST 0x22
|
||||
#define M_RESYNC 0x23
|
||||
#define M_ECHO 0x24
|
||||
#define M_OTA_STATUS 0x26
|
||||
#define M_PING_RSP 0x06
|
||||
#define F_ACK 1
|
||||
#define F_COMMIT 2
|
||||
@@ -51,7 +50,7 @@ typedef struct {
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
uint8_t len;
|
||||
uint8_t data[96];
|
||||
uint8_t data[250]; // ESP-NOW maximum: announces and ping replies carry the driver's settings
|
||||
} rx_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -272,12 +271,6 @@ 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;
|
||||
}
|
||||
|
||||
static rel_ping_cb_t s_ping_cb;
|
||||
|
||||
@@ -301,8 +294,7 @@ static void recv_cb(const uint8_t *src_mac, const uint8_t *data, int len)
|
||||
// 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 || data[1] == M_OTA_STATUS ||
|
||||
data[1] == M_PING_RSP);
|
||||
(data[1] == M_ACK || data[1] == M_RESYNC || data[1] == M_ECHO || data[1] == M_PING_RSP);
|
||||
if (!announce && !reply) {
|
||||
return;
|
||||
}
|
||||
@@ -334,12 +326,6 @@ 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->data[1] == M_PING_RSP) {
|
||||
// The driver is there: count it as heard, so group updates go to it again
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
// 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)
|
||||
// ping [4C][06][ping_id u32][len][name] (driver -> us, reply to [4C][05][ping_id])
|
||||
#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
|
||||
@@ -56,10 +55,6 @@ void rel_set_resync_cb(rel_resync_cb_t cb);
|
||||
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);
|
||||
|
||||
// A driver's PING_RSP ([4C][06][ping_id u32][name len][name]), called from the rel task. Drivers
|
||||
// answer a broadcast PING_REQ ([4C][05][ping_id u32]) after a random 50-500 ms.
|
||||
typedef void (*rel_ping_cb_t)(const uint8_t mac[6], const uint8_t *pkt, int len);
|
||||
|
||||
+73
-10
@@ -7,6 +7,7 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include "api_util.h"
|
||||
#include "esp_log.h"
|
||||
#include "engine.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -171,6 +172,26 @@ static cJSON *device_config_from(const cJSON *body)
|
||||
return dc;
|
||||
}
|
||||
|
||||
bool api_apply_driver_cfg(cJSON *rec, const char *cfg, int len)
|
||||
{
|
||||
cJSON *j = cfg && len > 0 ? cJSON_ParseWithLength(cfg, len) : NULL;
|
||||
bool changed = false;
|
||||
// "name" is handled by the callers
|
||||
for (size_t i = 1; j && rec && i < sizeof(DEVICE_CONFIG_KEYS) / sizeof(DEVICE_CONFIG_KEYS[0]); i++) {
|
||||
const cJSON *v = cJSON_GetObjectItem(j, DEVICE_CONFIG_KEYS[i]);
|
||||
if (!v || !(cJSON_IsNumber(v) || cJSON_IsString(v) || cJSON_IsBool(v))) {
|
||||
continue;
|
||||
}
|
||||
const cJSON *old = cJSON_GetObjectItem(rec, DEVICE_CONFIG_KEYS[i]);
|
||||
if (!old || !cJSON_Compare(old, v, true)) {
|
||||
api_set(rec, DEVICE_CONFIG_KEYS[i], cJSON_Duplicate(v, true));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(j);
|
||||
return changed;
|
||||
}
|
||||
|
||||
// v1 messages to one driver by its MAC
|
||||
static esp_err_t push_to(const char *mac, cJSON *msg)
|
||||
{
|
||||
@@ -314,10 +335,12 @@ static int clamp255(const cJSON *v, int dflt)
|
||||
}
|
||||
|
||||
// global x each group listing the MAC (none: no factor) x device output_brightness [x zone], each a
|
||||
// 0..255 factor: round(f1/255 * f2/255 * ... * 255). zone_b < 0 leaves the zone out.
|
||||
// 0..255 factor: round(f1/255 * f2/255 * ... * 255), capped at the lowest group max_brightness.
|
||||
// zone_b < 0 leaves the zone out.
|
||||
int api_effective_brightness(const char *mac, int zone_b)
|
||||
{
|
||||
double v = clamp255(cJSON_GetObjectItem(store_settings(), "global_brightness"), 255) / 255.0;
|
||||
double cap = 1.0; // lowest max_brightness of the groups listing the MAC
|
||||
const cJSON *g;
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
@@ -327,6 +350,10 @@ int api_effective_brightness(const char *mac, int zone_b)
|
||||
char m[13];
|
||||
if (api_mac(it, m) && !strcmp(m, mac)) {
|
||||
v *= clamp255(cJSON_GetObjectItem(g, "output_brightness"), 255) / 255.0;
|
||||
double c = clamp255(cJSON_GetObjectItem(g, "max_brightness"), 255) / 255.0;
|
||||
if (c < cap) {
|
||||
cap = c;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -336,6 +363,9 @@ int api_effective_brightness(const char *mac, int zone_b)
|
||||
if (zone_b >= 0) {
|
||||
v *= (zone_b > 255 ? 255 : zone_b) / 255.0;
|
||||
}
|
||||
if (v > cap) {
|
||||
v = cap;
|
||||
}
|
||||
int b = (int)(v * 255 + 0.5);
|
||||
return b < 0 ? 0 : (b > 255 ? 255 : b);
|
||||
}
|
||||
@@ -440,6 +470,7 @@ static esp_err_t device_driver_config(httpd_req_t *req, const char *mac)
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
char name[33];
|
||||
char cfg[200]; // the driver's settings as JSON, "" when it sent none
|
||||
int rtt_ms;
|
||||
} ping_rsp_t;
|
||||
|
||||
@@ -472,6 +503,12 @@ static void on_ping_rsp(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
nl = nl < (int)sizeof(r->name) - 1 ? nl : (int)sizeof(r->name) - 1;
|
||||
memcpy(r->name, pkt + 7, nl);
|
||||
r->name[nl] = 0;
|
||||
r->cfg[0] = 0;
|
||||
int co = 7 + pkt[6]; // then [len][json]
|
||||
if (co < len && pkt[co] > 0 && co + 1 + pkt[co] <= len && pkt[co] < sizeof(r->cfg)) {
|
||||
memcpy(r->cfg, pkt + co + 1, pkt[co]);
|
||||
r->cfg[pkt[co]] = 0;
|
||||
}
|
||||
r->rtt_ms = (int)((now - s_ping.sent_us) / 1000);
|
||||
}
|
||||
}
|
||||
@@ -479,7 +516,7 @@ static void on_ping_rsp(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
}
|
||||
|
||||
// Add a driver that answered but is not registered yet, or take its new name. True when stored.
|
||||
static bool register_from_ping(const char *hex, const char *name)
|
||||
static bool register_from_ping(const char *hex, const char *name, const char *cfg)
|
||||
{
|
||||
char label[48];
|
||||
snprintf(label, sizeof(label), "%s", name);
|
||||
@@ -495,17 +532,22 @@ static bool register_from_ping(const char *hex, const char *name)
|
||||
cJSON_AddStringToObject(rec, "type", "led");
|
||||
cJSON_AddStringToObject(rec, "transport", "espnow");
|
||||
cJSON_AddStringToObject(rec, "address", hex);
|
||||
api_apply_driver_cfg(rec, cfg, strlen(cfg));
|
||||
return true;
|
||||
}
|
||||
bool changed = false;
|
||||
const cJSON *old = cJSON_GetObjectItem(rec, "name");
|
||||
if (is_espnow(rec) && (!cJSON_IsString(old) || strcmp(old->valuestring, label))) {
|
||||
api_set(rec, "name", cJSON_CreateString(label));
|
||||
return true;
|
||||
changed = true;
|
||||
}
|
||||
return false;
|
||||
if (is_espnow(rec) && api_apply_driver_cfg(rec, cfg, strlen(cfg))) {
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
// POST /devices/ping {timeout_s}: broadcast PING_REQ and collect replies for timeout_s (0.5-30, default 3)
|
||||
// POST /devices/ping {timeout_s, broadcast}: PING_REQ to each known device, or a broadcast when broadcast is true or none are known and collect replies for timeout_s (0.5-30, default 3)
|
||||
static esp_err_t ping_devices(httpd_req_t *req)
|
||||
{
|
||||
static bool hooked; // replies only matter while a ping runs, so the callback is set on first use
|
||||
@@ -524,6 +566,7 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
}
|
||||
double timeout_s = cJSON_IsNumber(t) ? t->valuedouble : 3.0;
|
||||
timeout_s = timeout_s < 0.5 ? 0.5 : (timeout_s > 30 ? 30 : timeout_s);
|
||||
bool unicast = !cJSON_IsTrue(cJSON_GetObjectItem(body, "broadcast")) && store_coll("device")->child; // no known devices: broadcast to discover them
|
||||
cJSON_Delete(body);
|
||||
|
||||
portENTER_CRITICAL(&s_ping_mux);
|
||||
@@ -539,9 +582,23 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
return api_error(req, "409 Conflict", "a ping is already running");
|
||||
}
|
||||
uint8_t pkt[6] = { 0x4C, 0x05, id & 255, (id >> 8) & 255, (id >> 16) & 255, id >> 24 };
|
||||
esp_err_t err = espnow_tx_send(NULL, pkt, sizeof(pkt));
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
espnow_tx_send(NULL, pkt, sizeof(pkt)); // a second copy against a lost broadcast; replies are deduplicated
|
||||
esp_err_t err = ESP_OK;
|
||||
if (unicast) { // one acked frame to each known device
|
||||
cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
unsigned m[6];
|
||||
if (d->string && sscanf(d->string, "%2x%2x%2x%2x%2x%2x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) == 6) {
|
||||
uint8_t mac[6] = { m[0], m[1], m[2], m[3], m[4], m[5] };
|
||||
esp_err_t e = espnow_tx_send(mac, pkt, sizeof(pkt));
|
||||
err = e != ESP_OK ? e : err;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = espnow_tx_send(NULL, pkt, sizeof(pkt));
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
espnow_tx_send(NULL, pkt, sizeof(pkt)); // a second copy against a lost broadcast; replies are deduplicated
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
store_unlock(); // replies take up to timeout_s; HTTP and the sequencer keep going
|
||||
vTaskDelay(pdMS_TO_TICKS((int)(timeout_s * 1000)));
|
||||
@@ -571,7 +628,13 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
cJSON_AddStringToObject(r, "mac", hex);
|
||||
cJSON_AddStringToObject(r, "name", got[i].name);
|
||||
cJSON_AddNumberToObject(r, "rtt_ms", got[i].rtt_ms);
|
||||
registered += register_from_ping(hex, got[i].name);
|
||||
if (got[i].cfg[0]) {
|
||||
cJSON *cj = cJSON_Parse(got[i].cfg);
|
||||
if (cj) {
|
||||
cJSON_AddItemToObject(r, "config", cj);
|
||||
}
|
||||
}
|
||||
registered += register_from_ping(hex, got[i].name, got[i].cfg);
|
||||
}
|
||||
cJSON_AddNumberToObject(o, "registered", registered);
|
||||
if (registered) {
|
||||
@@ -807,7 +870,7 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "driver-config")) {
|
||||
*ret = device_driver_config(req, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "patterns")) {
|
||||
*ret = api_error(req, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices; use POST /patterns/<name>/send");
|
||||
*ret = api_error(req, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices");
|
||||
} else {
|
||||
*ret = api_error(req, "404 Not Found", "unknown action");
|
||||
}
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@
|
||||
|
||||
static const char *const DEFAULTS =
|
||||
"{\"name\":\"\",\"devices\":[],\"wifi_driver_display_name\":null,\"wifi_driver_num_leds\":null,"
|
||||
"\"wifi_color_order\":null,\"wifi_startup_mode\":null,\"output_brightness\":255,\"bridge_id\":null,"
|
||||
"\"wifi_color_order\":null,\"wifi_startup_mode\":null,\"output_brightness\":255,\"max_brightness\":255,\"bridge_id\":null,"
|
||||
"\"pattern\":\"on\",\"colors\":[\"000000\",\"FF0000\"],\"brightness\":100,\"delay\":100,\"step_offset\":0,"
|
||||
"\"step_increment\":1,\"n1\":0,\"n2\":0,\"n3\":0,\"n4\":0,\"n5\":0,\"n6\":0,\"n7\":0,\"n8\":0}";
|
||||
|
||||
@@ -310,7 +310,7 @@ void api_migrate_groups(void)
|
||||
bool changed = false;
|
||||
cJSON *defs = cJSON_Parse(DEFAULTS), *g;
|
||||
static const char *const KEYS[] = { "devices", "wifi_driver_display_name", "wifi_driver_num_leds", "wifi_color_order",
|
||||
"wifi_startup_mode", "output_brightness", "bridge_id" };
|
||||
"wifi_startup_mode", "output_brightness", "max_brightness", "bridge_id" };
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
if (!cJSON_IsObject(g)) {
|
||||
|
||||
Reference in new issue
Block a user