Channel 11 defaults, ESP-NOW driver config and rig/desktop updates

Moves the controller, bridge and led-driver default ESP-NOW channel to 11 and
updates the led-driver and led-tool submodule pointers. Includes the rest of the
working tree: web UI, desktop and sim-controller changes, API and bridge code,
graphify output, and the .gitea and fixture-driver folders.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-21 01:19:02 +13:00
1 parent 35207a868a
commit edf35e9c71
280 files changed
+24043 -1756

No files matched your search

+338 -40
View File
@@ -14,6 +14,8 @@
#include "rel.h"
#include "wifi_drv.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "store.h"
@@ -308,6 +310,33 @@ static bool wifi_in_group(const cJSON *dev, const char *id, const char *gid)
return false;
}
// The Wi-Fi driver whose record holds this MAC (12 hex digits), or NULL
static const cJSON *wifi_by_mac(const char *hex12)
{
const cJSON *d;
cJSON_ArrayForEach(d, store_coll("device"))
{
const cJSON *m = cJSON_GetObjectItem(d, "mac");
if (d->string && is_wifi(d) && cJSON_IsString(m) && !strcasecmp(m->valuestring, hex12)) {
return d;
}
}
return NULL;
}
// Whether the Wi-Fi device is in one of the zone's groups
static bool wifi_in_zone(const cJSON *dev, const char *zone_id)
{
char t[ZONE_TARGETS_MAX][GROUP_ID_MAX];
int n = engine_zone_targets(zone_id, t, ZONE_TARGETS_MAX);
for (int i = 0; i < n; i++) {
if (wifi_in_group(dev, dev->string, t[i])) {
return true;
}
}
return false;
}
// Wi-Fi drivers in the zone's groups get its preset (payload), or off when payload is NULL
static void wifi_zone(const char *zone_id, const cJSON *payload)
{
@@ -495,8 +524,10 @@ typedef struct {
char zone[16];
char preset[24];
uint16_t ver;
uint32_t seq; // order of zone_cur_set calls, to tell which zone spoke last
} zone_cur_t;
static zone_cur_t s_cur[16];
static uint32_t s_cur_seq;
static void zone_cur_set(const char *zone_id, const char *preset_id)
{
@@ -514,6 +545,7 @@ static void zone_cur_set(const char *zone_id, const char *preset_id)
snprintf(slot->zone, sizeof(slot->zone), "%s", zone_id);
snprintf(slot->preset, sizeof(slot->preset), "%s", preset_id);
slot->ver = rel_ver();
slot->seq = ++s_cur_seq;
}
}
@@ -597,6 +629,26 @@ int engine_group_macs(const char *gid, char out[][13], int max)
snprintf(out[n++], 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
}
// Wi-Fi drivers are listed by hostname; the MAC in their record names them as d<mac> like an ESP-NOW driver
cJSON_ArrayForEach(d, store_coll("device"))
{
const cJSON *m = cJSON_GetObjectItem(d, "mac");
if (n >= max || !d->string || !is_wifi(d) || !cJSON_IsString(m) || strlen(m->valuestring) != 12 ||
!wifi_in_group(d, d->string, gid)) {
continue;
}
char hex[13];
for (int i = 0; i < 13; i++) {
hex[i] = (char)tolower((unsigned char)m->valuestring[i]);
}
bool dup = false;
for (int i = 0; i < n && !dup; i++) {
dup = !strcmp(out[i], hex);
}
if (!dup) {
memcpy(out[n++], hex, 13);
}
}
return n;
}
@@ -1063,7 +1115,13 @@ static void send_groups(const uint8_t mac[6], const char *name, bool allow_empty
if (count == 0 && !allow_empty) {
return;
}
rel_send(mac, pkt, len, false, 1);
// tx waits for room: unicasts to drivers that are off hold the link's slots until they give up, and a
// dropped GROUPS leaves a driver out of a zone whose next play it then ignores (and commits, so no resync)
esp_err_t err = tx(mac, pkt, len, false, 1);
if (err != ESP_OK) {
ESP_LOGW(TAG, "groups for %02x%02x%02x%02x%02x%02x not sent: %s", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5], esp_err_to_name(err));
}
}
static bool hex_mac(const char *s, uint8_t mac[6])
@@ -1108,60 +1166,224 @@ static bool skip_unresponsive(const uint8_t mac[6])
return false;
}
// Re-send group membership to every registered ESP-NOW driver (zones or devices changed)
void engine_groups_changed(void)
/* Group pushes never hold the store lock while they wait for room on the link. Each send to a driver that
does not ack holds a link slot for its whole retry run, so with a few such drivers every send waits about
2 s for room; doing that under the lock stalled every HTTP request (the UI stopped loading) for as long
as the push ran. So the packets are built under the lock (quick) and sent after it is released. */
typedef struct {
uint8_t mac[6];
uint8_t pkt[REL_FRAG_MAX];
size_t len;
uint16_t seq;
bool queued;
} group_out_t;
// Every registered ESP-NOW driver's GROUPS packet (skip_dead: leave out drivers that stopped acking).
// Takes the store lock. Returns a malloc'd array of *n entries (NULL when there are none).
static group_out_t *groups_collect(bool skip_dead, int *n)
{
*n = 0;
store_lock();
int cap = cJSON_GetArraySize(store_coll("device"));
group_out_t *out = cap > 0 ? malloc(cap * sizeof(*out)) : NULL;
const cJSON *d;
cJSON_ArrayForEach(d, store_coll("device"))
{
const cJSON *name = cJSON_GetObjectItem(d, "name"), *addr = cJSON_GetObjectItem(d, "address");
const cJSON *tr = cJSON_GetObjectItem(d, "transport");
uint8_t mac[6];
const char *hex = cJSON_IsString(addr) ? addr->valuestring : d->string;
if (cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") && hex_mac(hex, mac) &&
!skip_unresponsive(mac)) {
send_groups(mac, name->valuestring, true);
if (out && *n < cap && cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") &&
hex_mac(hex, out[*n].mac) && !(skip_dead && skip_unresponsive(out[*n].mac))) {
int count;
out[*n].len = build_groups(out[*n].pkt, out[*n].mac, name->valuestring, &count);
out[*n].queued = false;
(*n)++;
}
}
store_unlock();
if (out && *n == 0) {
free(out);
out = NULL;
}
return out;
}
#define PUSH_MAX 32
static SemaphoreHandle_t s_groups_wake;
static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len);
static void resync_driver(const uint8_t mac[6]);
/* Announces and resync requests arrive on the ack task (rel_task), which is also the only task that frees
room on the link (it processes the acks). Handling them there meant taking the store lock and waiting for
room on the link from the very task that makes room: each one stalled ack handling for up to 2 s, and
while a group push held the store lock no ack was processed at all. So the ack task only queues them,
and the driver task below does the work. */
enum { JOB_ANNOUNCE, JOB_RESYNC };
typedef struct {
uint8_t kind;
uint8_t mac[6];
uint8_t len;
uint8_t data[250]; // an announce: rel's largest frame
} driver_job_t;
#define DRIVER_JOBS_LEN 32
static QueueHandle_t s_driver_jobs;
static void queue_driver_job(uint8_t kind, const uint8_t mac[6], const uint8_t *data, int len)
{
if (!s_driver_jobs || len < 0 || len > (int)sizeof(((driver_job_t *)0)->data)) {
return;
}
driver_job_t j = { .kind = kind, .len = (uint8_t)len };
memcpy(j.mac, mac, 6);
if (len > 0) {
memcpy(j.data, data, len);
}
if (xQueueSend(s_driver_jobs, &j, 0) != pdTRUE) {
// The driver announces or asks for a resync again, so a dropped one only costs time
ESP_LOGW(TAG, "%s from %02x%02x%02x%02x%02x%02x dropped: driver task busy",
kind == JOB_ANNOUNCE ? "announce" : "resync", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
}
static void queue_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
{
queue_driver_job(JOB_ANNOUNCE, mac, pkt, len);
}
static void queue_resync(const uint8_t mac[6])
{
queue_driver_job(JOB_RESYNC, mac, NULL, 0);
}
/* The driver task never waits for room on the link: with drivers that do not ack holding the link's slots,
each wait is up to 2 s, and announces queued behind them were dropped. A GROUPS that finds no room is
kept here and sent again (rebuilt, in case membership changed) every DEFER_RETRY_MS. Only the driver
task touches this. */
#define DEFER_MAX 64
#define DEFER_RETRY_MS 100
static struct {
uint8_t mac[6];
bool allow_empty; // from a group push (send even with no groups); else from an announce (see build_groups)
} s_defer[DEFER_MAX];
static int s_defer_n;
static int64_t s_defer_next_us;
static void defer_groups(const uint8_t mac[6], bool allow_empty)
{
for (int i = 0; i < s_defer_n; i++) {
if (!memcmp(s_defer[i].mac, mac, 6)) {
s_defer[i].allow_empty |= allow_empty;
return;
}
}
if (s_defer_n == DEFER_MAX) {
ESP_LOGW(TAG, "groups for %02x%02x%02x%02x%02x%02x not sent: link busy", mac[0], mac[1], mac[2], mac[3],
mac[4], mac[5]);
return;
}
memcpy(s_defer[s_defer_n].mac, mac, 6);
s_defer[s_defer_n].allow_empty = allow_empty;
s_defer_n++;
}
// Send a driver its GROUPS now if the link has room, else later (see s_defer). Never waits.
static void send_groups_or_defer(const uint8_t mac[6], const uint8_t *pkt, size_t len, bool allow_empty)
{
esp_err_t err = rel_send(mac, pkt, len, false, 1);
if (err == ESP_ERR_NO_MEM) {
defer_groups(mac, allow_empty);
} else if (err != ESP_OK) {
ESP_LOGW(TAG, "groups for %02x%02x%02x%02x%02x%02x not sent: %s", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5], esp_err_to_name(err));
}
}
static void retry_deferred_groups(void)
{
int64_t now = esp_timer_get_time();
if (!s_defer_n || now < s_defer_next_us) {
return;
}
s_defer_next_us = now + DEFER_RETRY_MS * 1000LL;
int n = s_defer_n;
static struct {
uint8_t mac[6];
bool allow_empty;
} todo[DEFER_MAX];
memcpy(todo, s_defer, n * sizeof(todo[0]));
s_defer_n = 0; // those that still find no room come back
for (int i = 0; i < n; i++) {
uint8_t pkt[REL_FRAG_MAX];
int count = 0;
size_t len = 0;
store_lock();
const char *name = device_name_for(todo[i].mac); // gone (deleted): nothing to send
if (name) {
len = build_groups(pkt, todo[i].mac, name, &count);
}
store_unlock();
if (len && (count > 0 || todo[i].allow_empty)) {
send_groups_or_defer(todo[i].mac, pkt, len, todo[i].allow_empty);
}
}
}
// One group push to every driver (see groups_collect)
static void groups_push_pass(void)
{
int n;
group_out_t *out = groups_collect(true, &n); // skip_unresponsive is only called from this task
for (int i = 0; i < n; i++) {
send_groups_or_defer(out[i].mac, out[i].pkt, out[i].len, true);
}
free(out);
}
// The driver task: queued announces and resyncs, and group pushes. A push runs once per wake: edits made
// during a pass give the semaphore again, so they get a pass of their own; edits made while it waits share one.
#define DRIVER_TASK_POLL_MS 20
static void driver_task(void *arg)
{
(void)arg;
for (;;) {
driver_job_t j;
if (xQueueReceive(s_driver_jobs, &j, pdMS_TO_TICKS(DRIVER_TASK_POLL_MS)) == pdTRUE) {
if (j.kind == JOB_ANNOUNCE) {
on_announce(j.mac, j.data, j.len);
} else {
resync_driver(j.mac);
}
}
if (xSemaphoreTake(s_groups_wake, 0) == pdTRUE) {
groups_push_pass();
}
retry_deferred_groups();
}
}
// Re-send group membership to every registered ESP-NOW driver (zones or devices changed). Returns at once:
// the group pusher sends it, so an HTTP handler never waits on the drivers.
void engine_groups_changed(void)
{
if (s_groups_wake) {
xSemaphoreGive(s_groups_wake);
}
}
void engine_groups_push_all(int *total, int *sent, int *failed)
{
struct {
uint8_t mac[6];
uint16_t seq;
bool queued;
} out[PUSH_MAX];
int n = 0;
store_lock();
const cJSON *d;
cJSON_ArrayForEach(d, store_coll("device"))
{
const cJSON *name = cJSON_GetObjectItem(d, "name"), *addr = cJSON_GetObjectItem(d, "address");
const cJSON *tr = cJSON_GetObjectItem(d, "transport");
const char *hex = cJSON_IsString(addr) ? addr->valuestring : d->string;
if (n < PUSH_MAX && cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") &&
hex_mac(hex, out[n].mac)) {
uint8_t pkt[REL_FRAG_MAX];
int count;
size_t len = build_groups(pkt, out[n].mac, name->valuestring, &count);
out[n].queued = false;
for (int i = 0; i < 400 && !out[n].queued; i++) { // wait for room, as tx() does
esp_err_t err = rel_send_seq(out[n].mac, pkt, len, false, 1, &out[n].seq);
out[n].queued = err == ESP_OK;
if (err != ESP_ERR_NO_MEM) {
break;
}
vTaskDelay(pdMS_TO_TICKS(5));
int n;
group_out_t *out = groups_collect(false, &n);
for (int k = 0; k < n; k++) {
for (int i = 0; i < 400 && !out[k].queued; i++) { // wait for room, as tx() does
esp_err_t err = rel_send_seq(out[k].mac, out[k].pkt, out[k].len, false, 1, &out[k].seq);
out[k].queued = err == ESP_OK;
if (err != ESP_ERR_NO_MEM) {
break;
}
n++;
vTaskDelay(pdMS_TO_TICKS(5));
}
}
store_unlock();
// All are in flight at once; then collect each outcome (rel gives up after about 2 s)
*total = n;
*sent = *failed = 0;
@@ -1173,6 +1395,7 @@ void engine_groups_push_all(int *total, int *sent, int *failed)
(*failed)++;
}
}
free(out);
}
// A driver said hello at boot: register it, then tell it which zones it belongs to
@@ -1320,6 +1543,9 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
// A driver heard on the browser bridge is one of the rig page's simulated drivers: "sim": true (a real
// board has no "sim" key)
const bool sim = bridge_mac_is_browser(mac);
// A fixture of a DMX bridge: "dmx_fixture": true and the bridge's address, kept once set
char dmx_addr[BRIDGE_ADDR_MAX];
const bool dmx = bridge_mac_is_dmx(mac, dmx_addr, sizeof(dmx_addr));
if (!rec) {
rec = cJSON_AddObjectToObject(devs, hex);
cJSON_AddStringToObject(rec, "id", hex);
@@ -1331,6 +1557,10 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
if (sim) {
cJSON_AddTrueToObject(rec, "sim");
}
if (dmx) {
cJSON_AddTrueToObject(rec, "dmx_fixture");
cJSON_AddStringToObject(rec, "bridge", dmx_addr);
}
store_save("device");
ESP_LOGI(TAG, "new %sdriver %s (%s)", sim ? "simulated " : "", name, hex);
} else {
@@ -1342,6 +1572,13 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
if (api_apply_driver_cfg(rec, cfg, cfg_len)) {
changed = true;
}
const cJSON *was = cJSON_GetObjectItem(rec, "bridge");
if (dmx && (!cJSON_IsTrue(cJSON_GetObjectItem(rec, "dmx_fixture")) || !cJSON_IsString(was) ||
strcmp(was->valuestring, dmx_addr) != 0)) {
api_set(rec, "dmx_fixture", cJSON_CreateTrue());
api_set(rec, "bridge", cJSON_CreateString(dmx_addr));
changed = true;
}
if (sim != cJSON_IsTrue(cJSON_GetObjectItem(rec, "sim"))) {
cJSON_DeleteItemFromObject(rec, "sim");
if (sim) {
@@ -1356,8 +1593,14 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
if (pkt[0] == '{') {
free(cfg);
}
send_groups(mac, name, false);
// Built under the lock, sent after it (see groups_collect): HTTP handlers wait on that lock
uint8_t gpkt[REL_FRAG_MAX];
int count;
size_t glen = build_groups(gpkt, mac, name, &count);
store_unlock();
if (count > 0) { // a driver in no zone keeps the groups it has (see build_groups)
send_groups_or_defer(mac, gpkt, glen, false);
}
}
/* A driver reported a stale state version: replay what its zone is playing, then commit the version */
@@ -1402,8 +1645,16 @@ static void resync_driver(const uint8_t mac[6])
esp_err_t engine_init(void)
{
wifi_drv_init();
rel_set_resync_cb(resync_driver);
rel_set_announce_cb(on_announce);
s_groups_wake = xSemaphoreCreateBinary();
s_driver_jobs = xQueueCreate(DRIVER_JOBS_LEN, sizeof(driver_job_t));
// 8 KB: it runs what rel_task used to (announce parsing, resync's frames) as well as the group pushes
if (!s_groups_wake || !s_driver_jobs || xTaskCreate(driver_task, "drivers", 8192, NULL, 3, NULL) != pdPASS) {
ESP_LOGE(TAG, "driver task not started");
return ESP_ERR_NO_MEM;
}
// The ack task only queues these (see driver_job_t)
rel_set_resync_cb(queue_resync);
rel_set_announce_cb(queue_announce);
return ESP_OK;
}
@@ -1413,5 +1664,52 @@ bool engine_mac_in_zone(const char *hex12, const char *zone_id)
if (!hex_mac(hex12, mac)) {
return false;
}
return driver_in_zone(mac, device_name_for(mac), zone_id);
const cJSON *w = wifi_by_mac(hex12);
return w ? wifi_in_zone(w, zone_id) : driver_in_zone(mac, device_name_for(mac), zone_id);
}
bool engine_driver_current(const char *hex12, char *preset, size_t len, uint32_t *seq)
{
uint8_t mac[6];
if (!hex_mac(hex12, mac)) {
return false;
}
const char *name = device_name_for(mac);
const cJSON *w = wifi_by_mac(hex12);
const zone_cur_t *best = NULL;
for (int i = 0; i < 16; i++) {
if (s_cur[i].zone[0] && (!best || s_cur[i].seq > best->seq) &&
(w ? wifi_in_zone(w, s_cur[i].zone) : driver_in_zone(mac, name, s_cur[i].zone))) {
best = &s_cur[i];
}
}
if (!best) {
return false;
}
snprintf(preset, len, "%s", best->preset);
*seq = best->seq;
return true;
}
bool engine_mac_is_sim(const char *hex12)
{
const cJSON *rec = cJSON_GetObjectItem(store_coll("device"), hex12);
return cJSON_IsTrue(cJSON_GetObjectItem(rec ? rec : wifi_by_mac(hex12), "sim"));
}
bool engine_mac_is_wifi(const char *hex12)
{
return wifi_by_mac(hex12) != NULL;
}
bool engine_group_has_sim(const char *gid)
{
char macs[64][13];
int n = engine_group_macs(gid, macs, 64);
for (int i = 0; i < n; i++) {
if (engine_mac_is_sim(macs[i])) {
return true;
}
}
return false;
}