From 1fa7ab282240bb9342945143f1fa841448f1f1b8 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Wed, 12 Jun 2024 10:44:04 +0300 Subject: [PATCH] Added multiple attempts to send a message --- include/zh_network.h | 4 +++- zh_network.c | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/zh_network.h b/include/zh_network.h index f825f21..470ba5b 100644 --- a/include/zh_network.h +++ b/include/zh_network.h @@ -52,7 +52,8 @@ .id_vector_size = 100, \ .route_vector_size = 100, \ .wifi_interface = WIFI_IF_STA, \ - .wifi_channel = 1 \ + .wifi_channel = 1, \ + .attempts = 3 \ } #ifdef __cplusplus @@ -78,6 +79,7 @@ extern "C" uint16_t route_vector_size; ///< The maximum size of the routing table. @note If the size is exceeded, the first route will be deleted. Minimum recommended value: number of planned nodes in the network + 10%. wifi_interface_t wifi_interface; ///< WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface. uint8_t wifi_channel; ///< Wi-Fi channel uses to send/receive ESPNOW data. @note Values from 1 to 14. + uint8_t attempts; ///< Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5. } zh_network_init_config_t; /// \cond diff --git a/zh_network.c b/zh_network.c index df0bbd4..fbb2e7a 100644 --- a/zh_network.c +++ b/zh_network.c @@ -32,6 +32,7 @@ static zh_vector_t _response_vector = {0}; static uint8_t _self_mac[6] = {0}; static const uint8_t _broadcast_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; static bool _is_initialized = false; +static uint8_t _attempts = 0; /// \cond typedef struct @@ -401,6 +402,8 @@ static void _processing(void *pvParameter) } memset(on_send, 0, sizeof(zh_network_event_on_send_t)); memcpy(on_send->mac_addr, queue.data.original_target_mac, 6); + SEND: + ++_attempts; if (esp_now_send((uint8_t *)peer->peer_addr, (uint8_t *)&queue.data, sizeof(_queue_t) - 12) != ESP_OK) { ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__); @@ -411,6 +414,7 @@ static void _processing(void *pvParameter) EventBits_t bit = xEventGroupWaitBits(_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS); if ((bit & DATA_SEND_SUCCESS) != 0) { + _attempts = 0; if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0) { if (queue.data.message_type == BROADCAST) @@ -482,6 +486,11 @@ static void _processing(void *pvParameter) } else { + if (_attempts < _init_config.attempts) + { + goto SEND; + } + _attempts = 0; if (memcmp(queue.data.original_target_mac, _broadcast_mac, 6) != 0) { ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X is incorrect.", MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));