Compare commits

...

5 Commits
v1.0.0 ... main

Author SHA1 Message Date
ebe0b8cbf5 Version 1.0.2
Update README.
2024-07-24 13:49:22 +03:00
10fefb2912 Update README.md 2024-07-12 15:08:54 +03:00
783b44fbca Minor changes 2024-06-18 18:20:35 +03:00
78c3712368 Updated some comments 2024-06-18 18:12:00 +03:00
76281e85be Version 1.0.1
Added multiple attempts to send a message.
Fixed bug with ESP-NOW channel setting when device is connected to router.
Changed internal error message.
Added support of ESP-IDF v4.x.x.
Medium main code refactoring.
Added WiFi channel selection.
2024-06-12 22:54:28 +03:00
5 changed files with 249 additions and 289 deletions

View File

@ -21,8 +21,9 @@
## Attention
1. The definition of ZH_NETWORK_MAX_MESSAGE_SIZE in the zh_network.h can be changed between 1 and 218. Smaller size - higher transmission speed. All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE.
2. For correct work at ESP-NOW + STA mode your WiFi router must be set on channel 1.
3. The ZHNetwork and the zh_network are incompatible.
2. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
3. All devices on the network must have the same WiFi channel.
4. The ZHNetwork and the zh_network are incompatible.
## Testing
@ -40,8 +41,6 @@
1. [zh_vector](https://github.com/aZholtikov/zh_vector.git)
## [Function description](http://zh-network.zh.com.ru)
## Using
In an existing project, run the following command to install the component:
@ -93,8 +92,8 @@ void app_main(void)
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
esp_wifi_set_max_tx_power(8); // Power reduction is for example and testing purposes only. Do not use in your own programs!
zh_network_init_config_t zh_network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
zh_network_init(&zh_network_init_config);
zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
zh_network_init(&network_init_config);
#ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL);
#else
@ -126,7 +125,7 @@ void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
printf("Int %d\n", recv_message->int_value);
printf("Float %f\n", recv_message->float_value);
printf("Bool %d\n", recv_message->bool_value);
free(recv_data->data); // Do not delete to avoid memory leaks!
heap_caps_free(recv_data->data); // Do not delete to avoid memory leaks!
break;
case ZH_NETWORK_ON_SEND_EVENT:;
zh_network_event_on_send_t *send_data = event_data;

View File

@ -1,9 +1,3 @@
/**
* @file
* Header file for the zh_network component.
*
*/
#pragma once
#include "string.h"
@ -23,25 +17,8 @@
#include "esp_mac.h"
#endif
/**
* @brief Unique identifier of ESP-NOW interface events base. Used when registering the event handler.
*
*/
#define ESP_EVENT_BASE ZH_NETWORK
#define ZH_NETWORK_MAX_MESSAGE_SIZE 218 // Maximum value of the transmitted data size. @attention All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE.
/**
* @brief Maximum value of the transmitted data size.
*
* @note Value range from 1 to 218. Smaller size - higher transmission speed.
*
* @attention All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE.
*/
#define ZH_NETWORK_MAX_MESSAGE_SIZE 218
/**
* @brief Default values for zh_network_init_config_t structure for initial initialization of ESP-NOW interface.
*
*/
#define ZH_NETWORK_INIT_CONFIG_DEFAULT() \
{ \
.network_id = 0xFAFBFCFD, \
@ -51,7 +28,9 @@
.max_waiting_time = 1000, \
.id_vector_size = 100, \
.route_vector_size = 100, \
.wifi_interface = WIFI_IF_STA \
.wifi_interface = WIFI_IF_STA, \
.wifi_channel = 1, \
.attempts = 3 \
}
#ifdef __cplusplus
@ -59,70 +38,45 @@ extern "C"
{
#endif
/**
* @brief Structure for initial initialization of ESP-NOW interface.
*
* @note Before initialize ESP-NOW interface recommend initialize zh_network_init_config_t structure with default values.
*
* @code zh_network_init_config_t config = ZH_NETWORK_INIT_CONFIG_DEFAULT() @endcode
*/
typedef struct
typedef struct // Structure for initial initialization of ESP-NOW interface.
{
uint32_t network_id; ///< A unique ID for the mesh network. @attention The ID must be the same for all nodes in the network.
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 3072 bytes.
uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 32.
uint16_t max_waiting_time; ///< Maximum time to wait a response message from target node (in milliseconds). @note If a response message from the target node is not received within this time, the status of the sent message will be "sent fail".
uint16_t id_vector_size; ///< Maximum size of unique ID of received messages. @note If the size is exceeded, the first value will be deleted. Minimum recommended value: number of planned nodes in the network + 10%.
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.
uint32_t network_id; // A unique ID for the mesh network. @attention The ID must be the same for all nodes in the network.
uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
uint16_t stack_size; // Stack size for task for the ESP-NOW messages processing. @note The minimum size is 3072 bytes.
uint8_t queue_size; // Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 32.
uint16_t max_waiting_time; // Maximum time to wait a response message from target node (in milliseconds). @note If a response message from the target node is not received within this time, the status of the sent message will be "sent fail".
uint16_t id_vector_size; // Maximum size of unique ID of received messages. @note If the size is exceeded, the first value will be deleted. Minimum recommended value: number of planned nodes in the network + 10%.
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
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
/// \endcond
ESP_EVENT_DECLARE_BASE(ZH_NETWORK);
/**
* @brief Enumeration of possible ESP-NOW events.
*
*/
typedef enum
typedef enum // Enumeration of possible ESP-NOW events.
{
ZH_NETWORK_ON_RECV_EVENT, ///< The event when the ESP-NOW message was received.
ZH_NETWORK_ON_SEND_EVENT ///< The event when the ESP-NOW message was sent.
ZH_NETWORK_ON_RECV_EVENT, // The event when the ESP-NOW message was received.
ZH_NETWORK_ON_SEND_EVENT // The event when the ESP-NOW message was sent.
} zh_network_event_type_t;
/**
* @brief Enumeration of possible status of sent ESP-NOW message.
*
*/
typedef enum
typedef enum // Enumeration of possible status of sent ESP-NOW message.
{
ZH_NETWORK_SEND_SUCCESS, ///< If ESP-NOW message was sent success.
ZH_NETWORK_SEND_FAIL ///< If ESP-NOW message was sent fail.
ZH_NETWORK_SEND_SUCCESS, // If ESP-NOW message was sent success.
ZH_NETWORK_SEND_FAIL // If ESP-NOW message was sent fail.
} zh_network_on_send_event_type_t;
/**
* @brief Structure for sending data to the event handler when an ESP-NOW message was sent.
*
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_SEND_EVENT event.
*/
typedef struct
typedef struct // Structure for sending data to the event handler when an ESP-NOW message was sent. @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_SEND_EVENT event.
{
uint8_t mac_addr[6]; ///< MAC address of the device to which the ESP-NOW message was sent. @note
zh_network_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note
uint8_t mac_addr[6]; // MAC address of the device to which the ESP-NOW message was sent.
zh_network_on_send_event_type_t status; // Status of sent ESP-NOW message.
} zh_network_event_on_send_t;
/**
* @brief Structure for sending data to the event handler when an ESP-NOW message was received.
*
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_RECV_EVENT event.
*/
typedef struct
typedef struct // Structure for sending data to the event handler when an ESP-NOW message was received. @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_RECV_EVENT event.
{
uint8_t mac_addr[6]; ///< MAC address of the sender ESP-NOW message. @note
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note
uint8_t data_len; ///< Size of the received ESP-NOW message. @note
uint8_t mac_addr[6]; // MAC address of the sender ESP-NOW message.
uint8_t *data; // Pointer to the data of the received ESP-NOW message.
uint8_t data_len; // Size of the received ESP-NOW message.
} zh_network_event_on_recv_t;
/**
@ -140,7 +94,7 @@ extern "C"
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
* - ESP_FAIL if any internal error
*/
esp_err_t zh_network_init(zh_network_init_config_t *config);
esp_err_t zh_network_init(const zh_network_init_config_t *config);
/**
* @brief Deinitialize ESP-NOW interface.
@ -164,7 +118,7 @@ extern "C"
* - ESP_OK if sent was success
* - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full
* - ESP_FAIL if ESP-NOW is not initialized
* - ESP_FAIL if ESP-NOW is not initialized or any internal error
*/
esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);

View File

@ -1 +1 @@
1.0.0
1.0.2

View File

@ -1,18 +1,11 @@
/**
* @file
* The main code of the zh_network component.
*/
#include "zh_network.h"
/// \cond
#define DATA_SEND_SUCCESS BIT0
#define DATA_SEND_FAIL BIT1
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
/// \endcond
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
#ifdef CONFIG_IDF_TARGET_ESP8266
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len);
#else
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
@ -21,7 +14,7 @@ static void _processing(void *pvParameter);
static const char *TAG = "zh_network";
static EventGroupHandle_t _send_cb_status_event_group_handle = {0};
static EventGroupHandle_t _event_group_handle = {0};
static QueueHandle_t _queue_handle = {0};
static TaskHandle_t _processing_task_handle = {0};
static SemaphoreHandle_t _id_vector_mutex = {0};
@ -32,55 +25,48 @@ 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;
/// \cond
typedef enum
{
BROADCAST,
UNICAST,
DELIVERY_CONFIRM,
SEARCH_REQUEST,
SEARCH_RESPONSE
} message_type_t;
typedef struct
{
message_type_t message_type;
uint32_t network_id;
uint32_t message_id;
uint32_t confirm_id;
uint8_t original_target_mac[6];
uint8_t original_sender_mac[6];
uint8_t sender_mac[6];
uint8_t data[ZH_NETWORK_MAX_MESSAGE_SIZE];
uint8_t data_len;
} data_t;
static uint8_t _attempts = 0;
typedef struct
{
uint8_t original_target_mac[6];
uint8_t intermediate_target_mac[6];
} routing_table_t;
typedef enum
{
TO_SEND,
ON_RECV,
WAIT_ROUTE,
WAIT_RESPONSE,
} queue_id_t;
} _routing_table_t;
typedef struct
{
queue_id_t id;
uint64_t time;
data_t data;
} queue_t;
enum
{
TO_SEND,
ON_RECV,
WAIT_ROUTE,
WAIT_RESPONSE,
} id;
struct
{
enum
{
BROADCAST,
UNICAST,
DELIVERY_CONFIRM,
SEARCH_REQUEST,
SEARCH_RESPONSE
} __attribute__((packed)) message_type;
uint32_t network_id;
uint32_t message_id;
uint32_t confirm_id;
uint8_t original_target_mac[6];
uint8_t original_sender_mac[6];
uint8_t sender_mac[6];
uint8_t payload[ZH_NETWORK_MAX_MESSAGE_SIZE];
uint8_t payload_len;
} __attribute__((packed)) data;
} _queue_t;
ESP_EVENT_DEFINE_BASE(ZH_NETWORK);
/// \endcond
esp_err_t zh_network_init(zh_network_init_config_t *config)
esp_err_t zh_network_init(const zh_network_init_config_t *config)
{
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
if (config == NULL)
@ -88,12 +74,29 @@ esp_err_t zh_network_init(zh_network_init_config_t *config)
ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
}
if (esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE) == ESP_ERR_WIFI_NOT_INIT)
_init_config = *config;
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel incorrect.");
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE);
if (err == ESP_ERR_WIFI_NOT_INIT || err == ESP_ERR_WIFI_NOT_STARTED)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized.");
return ESP_ERR_WIFI_NOT_INIT;
}
if (sizeof(data_t) > ESP_NOW_MAX_DATA_LEN)
else if (err == ESP_FAIL)
{
uint8_t prim = 0;
wifi_second_chan_t sec = 0;
esp_wifi_get_channel(&prim, &sec);
if (prim != _init_config.wifi_channel)
{
ESP_LOGW(TAG, "ESP-NOW initialization warning. The device is connected to the router. Channel %d will be used for ESP-NOW.", prim);
}
}
if ((sizeof(_queue_t) - 14) > ESP_NOW_MAX_DATA_LEN)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. The maximum value of the transmitted data size is incorrect.");
return ESP_ERR_INVALID_ARG;
@ -106,11 +109,10 @@ esp_err_t zh_network_init(zh_network_init_config_t *config)
{
esp_read_mac(_self_mac, ESP_MAC_WIFI_SOFTAP);
}
_init_config = *config;
_send_cb_status_event_group_handle = xEventGroupCreate();
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(queue_t));
_event_group_handle = xEventGroupCreate();
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t));
zh_vector_init(&_id_vector, sizeof(uint32_t), false);
zh_vector_init(&_route_vector, sizeof(routing_table_t), false);
zh_vector_init(&_route_vector, sizeof(_routing_table_t), false);
zh_vector_init(&_response_vector, sizeof(uint32_t), false);
_id_vector_mutex = xSemaphoreCreateMutex();
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
@ -136,7 +138,7 @@ esp_err_t zh_network_deinit(void)
ESP_LOGE(TAG, "ESP-NOW deinitialization fail. ESP-NOW not initialized.");
return ESP_FAIL;
}
vEventGroupDelete(_send_cb_status_event_group_handle);
vEventGroupDelete(_event_group_handle);
vQueueDelete(_queue_handle);
esp_now_unregister_send_cb();
esp_now_unregister_recv_cb();
@ -175,32 +177,31 @@ esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint
ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full.");
return ESP_ERR_INVALID_STATE;
}
queue_t queue = {0};
_queue_t queue = {0};
queue.id = TO_SEND;
data_t *send_data = &queue.data;
send_data->network_id = _init_config.network_id;
send_data->message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
memcpy(send_data->original_sender_mac, &_self_mac, 6);
queue.data.network_id = _init_config.network_id;
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
memcpy(queue.data.original_sender_mac, _self_mac, 6);
if (target == NULL)
{
send_data->message_type = BROADCAST;
memcpy(send_data->original_target_mac, &_broadcast_mac, 6);
queue.data.message_type = BROADCAST;
memcpy(queue.data.original_target_mac, _broadcast_mac, 6);
}
else
{
if (memcmp(target, &_broadcast_mac, 6) != 0)
if (memcmp(target, _broadcast_mac, 6) != 0)
{
send_data->message_type = UNICAST;
memcpy(send_data->original_target_mac, target, 6);
queue.data.message_type = UNICAST;
memcpy(queue.data.original_target_mac, target, 6);
}
else
{
send_data->message_type = BROADCAST;
memcpy(send_data->original_target_mac, &_broadcast_mac, 6);
queue.data.message_type = BROADCAST;
memcpy(queue.data.original_target_mac, _broadcast_mac, 6);
}
}
memcpy(&send_data->data, data, data_len);
send_data->data_len = data_len;
memcpy(queue.data.payload, data, data_len);
queue.data.payload_len = data_len;
if (target == NULL)
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
@ -211,7 +212,8 @@ esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint
}
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
return ESP_FAIL;
}
return ESP_OK;
}
@ -220,21 +222,21 @@ static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if (status == ESP_NOW_SEND_SUCCESS)
{
xEventGroupSetBits(_send_cb_status_event_group_handle, DATA_SEND_SUCCESS);
xEventGroupSetBits(_event_group_handle, DATA_SEND_SUCCESS);
}
else
{
xEventGroupSetBits(_send_cb_status_event_group_handle, DATA_SEND_FAIL);
xEventGroupSetBits(_event_group_handle, DATA_SEND_FAIL);
}
}
#ifdef CONFIG_IDF_TARGET_ESP8266
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
#else
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len)
#endif
{
#ifdef CONFIG_IDF_TARGET_ESP8266
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(mac_addr));
#else
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(esp_now_info->src_addr));
@ -244,10 +246,12 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Queue is almost full.");
return;
}
if (data_len == sizeof(data_t))
if (data_len == sizeof(_queue_t) - 14)
{
data_t *recv_data = (data_t *)data;
if (memcmp(&recv_data->network_id, &_init_config.network_id, sizeof(recv_data->network_id)) != 0)
_queue_t queue = {0};
queue.id = ON_RECV;
memcpy(&queue.data, data, data_len);
if (memcmp(&queue.data.network_id, &_init_config.network_id, sizeof(queue.data.network_id)) != 0)
{
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Incorrect mesh network ID.");
return;
@ -255,7 +259,7 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat
for (uint16_t i = 0; i < zh_vector_get_size(&_id_vector); ++i)
{
uint32_t *message_id = zh_vector_get_item(&_id_vector, i);
if (memcmp(&recv_data->message_id, message_id, sizeof(recv_data->message_id)) == 0)
if (memcmp(&queue.data.message_id, message_id, sizeof(queue.data.message_id)) == 0)
{
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Repeat message received.");
return;
@ -263,30 +267,26 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat
}
if (xSemaphoreTake(_id_vector_mutex, portTICK_PERIOD_MS) == pdTRUE)
{
zh_vector_push_back(&_id_vector, &recv_data->message_id);
zh_vector_push_back(&_id_vector, &queue.data.message_id);
if (zh_vector_get_size(&_id_vector) > _init_config.id_vector_size)
{
zh_vector_delete_item(&_id_vector, 0);
}
xSemaphoreGive(_id_vector_mutex);
}
queue_t queue = {0};
queue.id = ON_RECV;
recv_data = &queue.data;
memcpy(recv_data, data, data_len);
#ifdef CONFIG_IDF_TARGET_ESP8266
memcpy(recv_data->sender_mac, mac_addr, 6);
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
memcpy(queue.data.sender_mac, mac_addr, 6);
#else
memcpy(recv_data->sender_mac, esp_now_info->src_addr, 6);
memcpy(queue.data.sender_mac, esp_now_info->src_addr, 6);
#endif
#ifdef CONFIG_IDF_TARGET_ESP8266
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
#else
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
#endif
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
else
@ -297,11 +297,10 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat
static void _processing(void *pvParameter)
{
queue_t queue = {0};
_queue_t queue = {0};
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
{
bool flag = false;
data_t *data = &queue.data;
switch (queue.id)
{
case TO_SEND:
@ -315,14 +314,14 @@ static void _processing(void *pvParameter)
}
memset(peer, 0, sizeof(esp_now_peer_info_t));
peer->ifidx = _init_config.wifi_interface;
if (data->message_type == BROADCAST || data->message_type == SEARCH_REQUEST || data->message_type == SEARCH_RESPONSE)
if (queue.data.message_type == BROADCAST || queue.data.message_type == SEARCH_REQUEST || queue.data.message_type == SEARCH_RESPONSE)
{
memcpy(peer->peer_addr, &_broadcast_mac, 6);
if (memcmp(data->original_sender_mac, &_self_mac, 6) == 0)
memcpy(peer->peer_addr, _broadcast_mac, 6);
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
{
if (xSemaphoreTake(_id_vector_mutex, portTICK_PERIOD_MS) == pdTRUE)
{
zh_vector_push_back(&_id_vector, &data->message_id);
zh_vector_push_back(&_id_vector, &queue.data.message_id);
if (zh_vector_get_size(&_id_vector) > _init_config.id_vector_size)
{
zh_vector_delete_item(&_id_vector, 0);
@ -336,8 +335,8 @@ static void _processing(void *pvParameter)
ESP_LOGI(TAG, "Checking routing table to MAC %02X:%02X:%02X:%02X:%02X:%02X.", MAC2STR(queue.data.original_target_mac));
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
{
routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(data->original_target_mac, routing_table->original_target_mac, 6) == 0)
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
{
memcpy(peer->peer_addr, routing_table->intermediate_target_mac, 6);
flag = true;
@ -348,7 +347,7 @@ static void _processing(void *pvParameter)
if (flag == false)
{
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X not found.", MAC2STR(queue.data.original_target_mac));
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
@ -360,19 +359,19 @@ static void _processing(void *pvParameter)
queue.time = esp_timer_get_time() / 1000;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
queue.id = TO_SEND;
data->message_type = SEARCH_REQUEST;
memcpy(data->original_sender_mac, &_self_mac, 6);
data->data_len = 0;
memset(data->data, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
data->message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
queue.data.message_type = SEARCH_REQUEST;
memcpy(queue.data.original_sender_mac, _self_mac, 6);
queue.data.payload_len = 0;
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(peer);
break;
@ -393,45 +392,48 @@ static void _processing(void *pvParameter)
break;
}
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
memcpy(on_send->mac_addr, data->original_target_mac, 6);
if (esp_now_send((uint8_t *)peer->peer_addr, (uint8_t *)data, sizeof(data_t)) != ESP_OK)
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) - 14) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(peer);
heap_caps_free(on_send);
break;
}
EventBits_t bit = xEventGroupWaitBits(_send_cb_status_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS);
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)
{
if (memcmp(data->original_sender_mac, &_self_mac, 6) == 0)
_attempts = 0;
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
{
if (data->message_type == BROADCAST)
if (queue.data.message_type == BROADCAST)
{
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
on_send->status = ZH_NETWORK_SEND_SUCCESS;
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
if (data->message_type == SEARCH_REQUEST)
if (queue.data.message_type == SEARCH_REQUEST)
{
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == SEARCH_RESPONSE)
if (queue.data.message_type == SEARCH_RESPONSE)
{
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == DELIVERY_CONFIRM)
if (queue.data.message_type == DELIVERY_CONFIRM)
{
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
@ -440,33 +442,33 @@ static void _processing(void *pvParameter)
queue.time = esp_timer_get_time() / 1000;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
}
else
{
if (data->message_type == BROADCAST)
if (queue.data.message_type == BROADCAST)
{
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == SEARCH_REQUEST)
if (queue.data.message_type == SEARCH_REQUEST)
{
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == SEARCH_RESPONSE)
if (queue.data.message_type == SEARCH_RESPONSE)
{
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == DELIVERY_CONFIRM)
if (queue.data.message_type == DELIVERY_CONFIRM)
{
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
@ -475,22 +477,27 @@ static void _processing(void *pvParameter)
}
else
{
if (memcmp(data->original_target_mac, &_broadcast_mac, 6) != 0)
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));
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
{
routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(data->original_target_mac, routing_table->original_target_mac, 6) == 0)
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
{
zh_vector_delete_item(&_route_vector, i);
}
}
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == DELIVERY_CONFIRM)
if (queue.data.message_type == DELIVERY_CONFIRM)
{
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
@ -498,19 +505,19 @@ static void _processing(void *pvParameter)
queue.time = esp_timer_get_time() / 1000;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
ESP_LOGI(TAG, "System message for routing request to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue.", MAC2STR(queue.data.original_target_mac));
queue.id = TO_SEND;
data->message_type = SEARCH_REQUEST;
memcpy(data->original_sender_mac, &_self_mac, 6);
data->data_len = 0;
memset(data->data, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
data->message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
queue.data.message_type = SEARCH_REQUEST;
memcpy(queue.data.original_sender_mac, _self_mac, 6);
queue.data.payload_len = 0;
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
}
@ -520,84 +527,84 @@ static void _processing(void *pvParameter)
break;
case ON_RECV:
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
switch (data->message_type)
switch (queue.data.message_type)
{
case BROADCAST:
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
zh_network_event_on_recv_t *on_recv = heap_caps_malloc(sizeof(zh_network_event_on_recv_t), MALLOC_CAP_8BIT);
if (on_recv == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_recv);
break;
}
memset(on_recv, 0, sizeof(zh_network_event_on_recv_t));
memcpy(on_recv->mac_addr, data->original_sender_mac, 6);
on_recv->data_len = data->data_len;
on_recv->data = heap_caps_malloc(data->data_len, MALLOC_CAP_8BIT);
memcpy(on_recv->mac_addr, queue.data.original_sender_mac, 6);
on_recv->data_len = queue.data.payload_len;
on_recv->data = heap_caps_malloc(queue.data.payload_len, MALLOC_CAP_8BIT);
if (on_recv->data == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_recv);
heap_caps_free(on_recv->data);
break;
}
memset(on_recv->data, 0, data->data_len);
memcpy(on_recv->data, data->data, data->data_len);
memset(on_recv->data, 0, queue.data.payload_len);
memcpy(on_recv->data, queue.data.payload, queue.data.payload_len);
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_RECV_EVENT, on_recv, sizeof(zh_network_event_on_recv_t) + on_recv->data_len + sizeof(on_recv->data_len), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(on_recv);
queue.id = TO_SEND;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
case UNICAST:
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (memcmp(data->original_target_mac, &_self_mac, 6) == 0)
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
{
zh_network_event_on_recv_t *on_recv = heap_caps_malloc(sizeof(zh_network_event_on_recv_t), MALLOC_CAP_8BIT);
if (on_recv == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_recv);
break;
}
memset(on_recv, 0, sizeof(zh_network_event_on_recv_t));
memcpy(on_recv->mac_addr, data->original_sender_mac, 6);
on_recv->data_len = data->data_len;
on_recv->data = heap_caps_malloc(data->data_len, MALLOC_CAP_8BIT);
memcpy(on_recv->mac_addr, queue.data.original_sender_mac, 6);
on_recv->data_len = queue.data.payload_len;
on_recv->data = heap_caps_malloc(queue.data.payload_len, MALLOC_CAP_8BIT);
if (on_recv->data == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_recv);
heap_caps_free(on_recv->data);
break;
}
memset(on_recv->data, 0, data->data_len);
memcpy(on_recv->data, data->data, data->data_len);
memset(on_recv->data, 0, queue.data.payload_len);
memcpy(on_recv->data, queue.data.payload, queue.data.payload_len);
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_RECV_EVENT, on_recv, sizeof(zh_network_event_on_recv_t) + on_recv->data_len + sizeof(on_recv->data_len), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(on_recv);
queue.id = TO_SEND;
data->message_type = DELIVERY_CONFIRM;
memcpy(data->original_target_mac, data->original_sender_mac, 6);
memcpy(data->original_sender_mac, &_self_mac, 6);
data->data_len = 0;
memset(data->data, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
data->confirm_id = data->message_id;
data->message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
queue.data.message_type = DELIVERY_CONFIRM;
memcpy(queue.data.original_target_mac, queue.data.original_sender_mac, 6);
memcpy(queue.data.original_sender_mac, _self_mac, 6);
queue.data.payload_len = 0;
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
queue.data.confirm_id = queue.data.message_id;
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
}
@ -606,14 +613,14 @@ static void _processing(void *pvParameter)
queue.id = TO_SEND;
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
case DELIVERY_CONFIRM:
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (memcmp(data->original_target_mac, &_self_mac, 6) == 0)
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
{
zh_vector_push_back(&_response_vector, &data->confirm_id);
zh_vector_push_back(&_response_vector, &queue.data.confirm_id);
if (zh_vector_get_size(&_response_vector) > _init_config.queue_size)
{
zh_vector_delete_item(&_response_vector, 0);
@ -626,43 +633,43 @@ static void _processing(void *pvParameter)
queue.id = TO_SEND;
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
case SEARCH_REQUEST:
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
{
routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(data->original_target_mac, routing_table->original_target_mac, 6) == 0)
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
{
zh_vector_delete_item(&_route_vector, i);
}
}
{ // Just to avoid the compiler warning.
routing_table_t routing_table = {0};
memcpy(&routing_table.original_target_mac, data->original_sender_mac, 6);
memcpy(&routing_table.intermediate_target_mac, data->sender_mac, 6);
_routing_table_t routing_table = {0};
memcpy(routing_table.original_target_mac, queue.data.original_sender_mac, 6);
memcpy(routing_table.intermediate_target_mac, queue.data.sender_mac, 6);
zh_vector_push_back(&_route_vector, &routing_table);
}
if (zh_vector_get_size(&_route_vector) > _init_config.route_vector_size)
{
zh_vector_delete_item(&_route_vector, 0);
}
if (memcmp(data->original_target_mac, &_self_mac, 6) == 0)
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
{
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to the queue.", MAC2STR(queue.data.original_target_mac), MAC2STR(queue.data.original_sender_mac));
queue.id = TO_SEND;
data->message_type = SEARCH_RESPONSE;
memcpy(data->original_target_mac, data->original_sender_mac, 6);
memcpy(data->original_sender_mac, &_self_mac, 6);
data->data_len = 0;
memset(data->data, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
data->message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
queue.data.message_type = SEARCH_RESPONSE;
memcpy(queue.data.original_target_mac, queue.data.original_sender_mac, 6);
memcpy(queue.data.original_sender_mac, _self_mac, 6);
queue.data.payload_len = 0;
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
}
@ -671,37 +678,37 @@ static void _processing(void *pvParameter)
queue.id = TO_SEND;
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
case SEARCH_RESPONSE:
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
{
routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(data->original_target_mac, routing_table->original_target_mac, 6) == 0)
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
{
zh_vector_delete_item(&_route_vector, i);
}
}
{ // Just to avoid the compiler warning.
routing_table_t routing_table = {0};
memcpy(&routing_table.original_target_mac, data->original_sender_mac, 6);
memcpy(&routing_table.intermediate_target_mac, data->sender_mac, 6);
_routing_table_t routing_table = {0};
memcpy(routing_table.original_target_mac, queue.data.original_sender_mac, 6);
memcpy(routing_table.intermediate_target_mac, queue.data.sender_mac, 6);
zh_vector_push_back(&_route_vector, &routing_table);
}
if (zh_vector_get_size(&_route_vector) > _init_config.route_vector_size)
{
zh_vector_delete_item(&_route_vector, 0);
}
if (memcmp(data->original_target_mac, &_self_mac, 6) != 0)
if (memcmp(queue.data.original_target_mac, _self_mac, 6) != 0)
{
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
queue.id = TO_SEND;
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
break;
}
@ -715,24 +722,24 @@ static void _processing(void *pvParameter)
for (uint16_t i = 0; i < zh_vector_get_size(&_response_vector); ++i)
{
uint32_t *message_id = zh_vector_get_item(&_response_vector, i);
if (memcmp(&data->message_id, message_id, sizeof(data->message_id)) == 0)
if (memcmp(&queue.data.message_id, message_id, sizeof(queue.data.message_id)) == 0)
{
zh_vector_delete_item(&_response_vector, i);
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
if (on_send == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_send);
break;
}
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
memcpy(on_send->mac_addr, data->original_target_mac, 6);
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
on_send->status = ZH_NETWORK_SEND_SUCCESS;
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(on_send);
flag = true;
@ -744,23 +751,23 @@ static void _processing(void *pvParameter)
if ((esp_timer_get_time() / 1000 - queue.time) > _init_config.max_waiting_time)
{
ESP_LOGW(TAG, "Time for waiting confirmation message from MAC %02X:%02X:%02X:%02X:%02X:%02X is expired.", MAC2STR(queue.data.original_target_mac));
if (memcmp(data->original_sender_mac, &_self_mac, 6) == 0)
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
{
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
if (on_send == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_send);
break;
}
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
memcpy(on_send->mac_addr, data->original_target_mac, 6);
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
on_send->status = ZH_NETWORK_SEND_FAIL;
ESP_LOGE(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(on_send);
}
@ -768,29 +775,29 @@ static void _processing(void *pvParameter)
}
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
break;
case WAIT_ROUTE:
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
{
routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(data->original_target_mac, routing_table->original_target_mac, 6) == 0)
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
{
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_target_mac));
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list and added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == DELIVERY_CONFIRM)
if (queue.data.message_type == DELIVERY_CONFIRM)
{
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list and added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
queue.id = TO_SEND;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
flag = true;
break;
@ -801,33 +808,33 @@ static void _processing(void *pvParameter)
if ((esp_timer_get_time() / 1000 - queue.time) > _init_config.max_waiting_time)
{
ESP_LOGW(TAG, "Time for waiting routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is expired.", MAC2STR(queue.data.original_target_mac));
if (memcmp(data->original_sender_mac, &_self_mac, 6) == 0)
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
{
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
if (on_send == NULL)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
heap_caps_free(on_send);
break;
}
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
memcpy(on_send->mac_addr, data->original_target_mac, 6);
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
on_send->status = ZH_NETWORK_SEND_FAIL;
ESP_LOGE(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
heap_caps_free(on_send);
}
else
{
if (data->message_type == UNICAST)
if (queue.data.message_type == UNICAST)
{
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
if (data->message_type == DELIVERY_CONFIRM)
if (queue.data.message_type == DELIVERY_CONFIRM)
{
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
}
@ -836,7 +843,7 @@ static void _processing(void *pvParameter)
}
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
break;

BIN
zh_network.zip Normal file

Binary file not shown.