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:
+16
-1
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user