diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index db4e69de644ebaedab6ae1ecc4bcc215bac99b35..af25396fb4e9f4d82cd8169b914373894ec541f9 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -61,8 +61,14 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { - esp_log_write(ESP_LOG_INFO, TAG_ESPNOW, "Message received\n"); + response response = {}; + // send a host change msg if client is new, simplifies client + response.type = esp_now_is_peer_exist(mac) ? dataAck : hostChange; + esp_read_mac(response.mac, ESP_MAC_WIFI_STA); + response.time = rtc.getEpoch(); + // this block needs to happen before we send the message, as it's not possible to + // send a message to an unregistered peer (i.e. new client) if (!esp_now_is_peer_exist(mac)) { esp_now_peer_info_t client = {}; memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6); @@ -75,11 +81,6 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { } } - // TODO: Think if response to client may contain error messages - response response = {}; - response.type = dataAck; - esp_read_mac(response.mac, ESP_MAC_WIFI_STA); - response.time = rtc.getEpoch(); esp_err_t success = esp_now_send(mac, (uint8_t *)&response, sizeof(response)); esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW, (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n");