diff --git a/main/engine.c b/main/engine.c index 2db8f7b..72a22d3 100644 --- a/main/engine.c +++ b/main/engine.c @@ -846,6 +846,43 @@ static bool hex_mac(const char *s, uint8_t mac[6]) return parse_mac(s, mac); } +/* Drivers that stopped acking (switched off, out of range). Every unicast to one holds a pending slot + for its whole retry run, so a few zone edits would fill the link and starve the next zone play. + Group updates go to such a driver at most once per DEAD_RETRY_US; a driver that comes back gets its + groups when it announces (boot) or resyncs (missed state). */ +#define DEAD_FAILS 3 +#define DEAD_RETRY_US (30LL * 1000000) +static struct { + uint8_t mac[6]; + int64_t tried_us; +} s_dead[16]; + +static bool skip_unresponsive(const uint8_t mac[6]) +{ + int fails; + rel_peer_status(mac, &fails); + if (fails < DEAD_FAILS) { + return false; + } + int slot = 0; + for (int i = 0; i < 16; i++) { + if (!memcmp(s_dead[i].mac, mac, 6)) { + slot = i; + break; + } + if (s_dead[i].tried_us < s_dead[slot].tried_us) { + slot = i; // oldest entry, if this driver has none + } + } + int64_t now = esp_timer_get_time(); + if (!memcmp(s_dead[slot].mac, mac, 6) && now - s_dead[slot].tried_us < DEAD_RETRY_US) { + return true; + } + memcpy(s_dead[slot].mac, mac, 6); + s_dead[slot].tried_us = now; + return false; +} + // Re-send group membership to every registered ESP-NOW driver (zones or devices changed) void engine_groups_changed(void) { @@ -857,7 +894,8 @@ void engine_groups_changed(void) 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)) { + if (cJSON_IsString(name) && cJSON_IsString(tr) && !strcmp(tr->valuestring, "espnow") && hex_mac(hex, mac) && + !skip_unresponsive(mac)) { send_groups(mac, name->valuestring, true); } }