Ignore repeat master discover after slave has joined network.

Slave responds and logs only on first discover from a given master MAC;
subsequent broadcasts on the same network are silently ignored.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
simon 2026-05-18 22:21:08 +02:00
parent b592401e78
commit 81e479ecd1
2 changed files with 17 additions and 2 deletions

View File

@ -90,7 +90,7 @@ WiFi is brought up in STA mode (no AP association). Channel = `app_config.networ
**Master:** task `espnow_disc` sends `DISCOVER` every **500 ms** on the configured network. Logs `slave joined id=… mac=… ver=…` when a new slave is seen (up to 16 entries).
**Slave:** on matching `DISCOVER`, unicast `SLAVE_INFO` back to the master source MAC.
**Slave:** on first matching `DISCOVER`, logs `joined network N, master …`, sends `SLAVE_INFO` once, then ignores further discovers from that master (no repeat log or reply).
Monitor via USB-JTAG (`/dev/ttyACM0`) while using a USB-serial adapter on **GPIO2/3** (`/dev/ttyUSB0`) for UART — they are different interfaces.

View File

@ -58,6 +58,8 @@ static app_config_t s_config;
static uint8_t s_wifi_channel;
static uint8_t s_own_mac[ESP_NOW_ETH_ALEN];
static slave_entry_t s_slaves[ESPNOW_MAX_SLAVES];
static bool s_slave_joined;
static uint8_t s_master_mac[ESP_NOW_ETH_ALEN];
static uint8_t network_to_channel(uint8_t network) {
if (network < 1 || network > 13) {
@ -147,7 +149,18 @@ static void handle_discover(const uint8_t *sender_mac,
if (pkt->network != s_config.network) {
return;
}
ESP_LOGI(TAG, "discover from master on network %u", (unsigned)pkt->network);
if (s_slave_joined && mac_equal(sender_mac, s_master_mac)) {
return;
}
memcpy(s_master_mac, sender_mac, ESP_NOW_ETH_ALEN);
s_slave_joined = true;
char mac_str[18];
mac_to_str(sender_mac, mac_str, sizeof(mac_str));
ESP_LOGI(TAG, "joined network %u, master %s", (unsigned)pkt->network, mac_str);
send_slave_info(sender_mac);
}
@ -266,6 +279,8 @@ esp_err_t esp_now_comm_init(const app_config_t *config) {
memset(&s_config, 0, sizeof(s_config));
memcpy(&s_config, config, sizeof(s_config));
memset(s_slaves, 0, sizeof(s_slaves));
s_slave_joined = false;
memset(s_master_mac, 0, sizeof(s_master_mac));
s_wifi_channel = network_to_channel(config->network);
ESP_ERROR_CHECK(esp_read_mac(s_own_mac, ESP_MAC_WIFI_STA));