feat: ping drivers and push groups from the devices modal

POST /devices/ping broadcasts an ESP-NOW PING_REQ, collects the drivers'
PING_RSP for timeout_s and registers or renames the ones that answer. A
reply also counts as the driver being heard, so group updates go to it
again. POST /devices/groups sends every ESP-NOW driver its groups and
reports the acks. The devices modal gets the Ping drivers and Update
groups buttons from main, with per-driver ping dots.

UI files are now served with Cache-Control: no-cache and the firmware
build hash as ETag, so a browser picks up a new UI right after an update
instead of a day later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-03 17:53:56 +13:00
1 parent e7f238aebc
commit 19fea0b0a1
10 files changed
+559 -29

No files matched your search

+180
View File
@@ -9,6 +9,9 @@
#include "engine.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_random.h"
#include "esp_timer.h"
#include "espnow_tx.h"
#include "rel.h"
#include "store.h"
@@ -289,6 +292,175 @@ static esp_err_t identify(httpd_req_t *req, const cJSON *dev, const char *mac)
return api_message(req, "200 OK", "Identify sent");
}
/* ------------------------------------------------------------------- ping */
#define PING_MAX 32
typedef struct {
uint8_t mac[6];
char name[33];
int rtt_ms;
} ping_rsp_t;
static struct {
uint32_t id; // 0 = no ping running
int64_t sent_us;
int count;
ping_rsp_t rsp[PING_MAX];
} s_ping;
static portMUX_TYPE s_ping_mux = portMUX_INITIALIZER_UNLOCKED;
// Rel task: [4C][06][ping_id u32 LE][len][name]
static void on_ping_rsp(const uint8_t mac[6], const uint8_t *pkt, int len)
{
if (len < 7) {
return;
}
uint32_t id = pkt[2] | (pkt[3] << 8) | (pkt[4] << 16) | ((uint32_t)pkt[5] << 24);
int nl = pkt[6] < len - 7 ? pkt[6] : len - 7;
int64_t now = esp_timer_get_time();
portENTER_CRITICAL(&s_ping_mux);
if (id && id == s_ping.id && s_ping.count < PING_MAX) {
bool seen = false;
for (int i = 0; i < s_ping.count && !seen; i++) {
seen = !memcmp(s_ping.rsp[i].mac, mac, 6);
}
if (!seen) {
ping_rsp_t *r = &s_ping.rsp[s_ping.count++];
memcpy(r->mac, mac, 6);
nl = nl < (int)sizeof(r->name) - 1 ? nl : (int)sizeof(r->name) - 1;
memcpy(r->name, pkt + 7, nl);
r->name[nl] = 0;
r->rtt_ms = (int)((now - s_ping.sent_us) / 1000);
}
}
portEXIT_CRITICAL(&s_ping_mux);
}
// 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)
{
char label[48];
snprintf(label, sizeof(label), "%s", name);
if (!label[0]) {
snprintf(label, sizeof(label), "led-%s", hex);
}
cJSON *devs = store_coll("device");
cJSON *rec = cJSON_GetObjectItem(devs, hex);
if (!rec) {
rec = cJSON_AddObjectToObject(devs, hex);
cJSON_AddStringToObject(rec, "id", hex);
cJSON_AddStringToObject(rec, "name", label);
cJSON_AddStringToObject(rec, "type", "led");
cJSON_AddStringToObject(rec, "transport", "espnow");
cJSON_AddStringToObject(rec, "address", hex);
return true;
}
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;
}
return false;
}
// POST /devices/ping {timeout_s}: broadcast PING_REQ 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
if (!hooked) {
rel_set_ping_cb(on_ping_rsp);
hooked = true;
}
cJSON *body = api_body_opt(req);
if (!body) {
return ESP_OK;
}
const cJSON *t = cJSON_GetObjectItem(body, "timeout_s");
if (t && !cJSON_IsNull(t) && !cJSON_IsNumber(t)) {
cJSON_Delete(body);
return api_error(req, "400 Bad Request", "Invalid timeout_s");
}
double timeout_s = cJSON_IsNumber(t) ? t->valuedouble : 3.0;
timeout_s = timeout_s < 0.5 ? 0.5 : (timeout_s > 30 ? 30 : timeout_s);
cJSON_Delete(body);
portENTER_CRITICAL(&s_ping_mux);
bool busy = s_ping.id != 0;
uint32_t id = (esp_random() | 1);
if (!busy) {
s_ping.id = id;
s_ping.count = 0;
s_ping.sent_us = esp_timer_get_time();
}
portEXIT_CRITICAL(&s_ping_mux);
if (busy) {
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
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)));
store_lock();
}
static ping_rsp_t got[PING_MAX];
portENTER_CRITICAL(&s_ping_mux);
int n = s_ping.count;
memcpy(got, s_ping.rsp, n * sizeof(got[0]));
s_ping.id = 0;
portEXIT_CRITICAL(&s_ping_mux);
if (err != ESP_OK) {
return api_error(req, "503 Service Unavailable", "Send failed");
}
cJSON *o = cJSON_CreateObject(), *responses;
cJSON_AddTrueToObject(o, "ok");
cJSON_AddNumberToObject(o, "ping_id", id);
cJSON_AddNumberToObject(o, "timeout_s", timeout_s);
responses = cJSON_AddObjectToObject(o, "responses");
int registered = 0;
for (int i = 0; i < n; i++) {
char hex[13];
const uint8_t *m = got[i].mac;
snprintf(hex, sizeof(hex), "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], m[4], m[5]);
cJSON *r = cJSON_AddObjectToObject(responses, hex);
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);
}
cJSON_AddNumberToObject(o, "registered", registered);
if (registered) {
store_save("device");
engine_groups_changed();
}
return api_json(req, "200 OK", o, true);
}
// POST /devices/groups: push group membership to every ESP-NOW driver and report the acks
static esp_err_t push_groups(httpd_req_t *req)
{
int total, sent, failed;
store_unlock(); // waits up to a few seconds for acks
engine_groups_push_all(&total, &sent, &failed);
store_lock();
if (!total) {
cJSON *o = cJSON_CreateObject();
cJSON_AddFalseToObject(o, "ok");
cJSON_AddStringToObject(o, "error", "No ESP-NOW devices in registry");
return api_json(req, "400 Bad Request", o, true);
}
cJSON *o = cJSON_CreateObject();
cJSON_AddBoolToObject(o, "ok", failed == 0);
cJSON_AddNumberToObject(o, "total", total);
cJSON_AddNumberToObject(o, "sent", sent);
cJSON_AddNumberToObject(o, "failed", failed);
return api_json(req, failed ? "503 Service Unavailable" : "200 OK", o, true);
}
// Per-driver delivery health from the acked link
static esp_err_t device_status(httpd_req_t *req)
{
@@ -321,6 +493,14 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
*ret = device_status(req);
return true;
}
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "ping")) {
*ret = ping_devices(req);
return true;
}
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "groups")) {
*ret = push_groups(req);
return true;
}
if (n == 1) {
if (method == HTTP_GET) {
cJSON *o = cJSON_CreateObject(), *d;