Version 1.0.0
Initial version.
This commit is contained in:
commit
906f1e2725
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.DS_Store
|
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
if(${IDF_TARGET} STREQUAL esp8266)
|
||||
set(requires zh_vector)
|
||||
else()
|
||||
set(requires zh_vector esp_timer esp_wifi)
|
||||
endif()
|
||||
idf_component_register(SRCS "zh_network.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
73
LICENSE
Normal file
73
LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
150
README.md
Normal file
150
README.md
Normal file
@ -0,0 +1,150 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for ESP-NOW based mesh network
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. The maximum size of transmitted data is up to 218 bytes.
|
||||
2. Support of any data types.
|
||||
3. All nodes are not visible to the network scanner.
|
||||
4. Not required a pre-pairings for data transfer.
|
||||
5. Broadcast or unicast data transmissions.
|
||||
6. There are no periodic/synchronous messages on the network. All devices are in "silent mode" and do not "hum" into the air.
|
||||
7. Each node has its own independent routing table, updated only as needed.
|
||||
8. Each node will receive/send a message if it "sees" at least one device on the network.
|
||||
9. The number of devices on the network and the area of use is not limited.
|
||||
10. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW.
|
||||
|
||||
## 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.
|
||||
|
||||
## Testing
|
||||
|
||||
1. Program 2 receivers and 1 transmitter (specify the MAC of the 1st receiver in the code).
|
||||
2. Connect the 1st receiver to the computer. Switch on serial port monitor. Turn transmitter on. Receiver will start receiving data.
|
||||
3. Move transmitter as far away from receiver as possible until receiver is able to receive data (shield module if necessary).
|
||||
4. Turn on the 2nd receiver and place it between the 1st receiver and transmitter (preferably in the middle). The 1st receiver will resume data reception (with relaying through the 2nd receiver). P.S. You can use a transmitter instead of the 2nd receiver - makes no difference.
|
||||
|
||||
## Arduino library
|
||||
|
||||
1. For using zh_network component on Arduino download and copy (with extract) zh_network.zip file to your folder for Arduino libraries. See README.md in this folder for using example.
|
||||
2. Please pay attention - library tested on VSCode + PlatformIO. Not on Arduino IDE.
|
||||
|
||||
## Dependencies
|
||||
|
||||
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:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone https://github.com/aZholtikov/zh_vector.git
|
||||
git clone https://github.com/aZholtikov/zh_network.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_network.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Sending and receiving messages:
|
||||
|
||||
```c
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "zh_network.h"
|
||||
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
|
||||
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
|
||||
uint8_t target[6] = {0x58, 0xBF, 0x25, 0x18, 0xC8, 0x04};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char char_value[30];
|
||||
int int_value;
|
||||
float float_value;
|
||||
bool bool_value;
|
||||
} example_message_t;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_log_level_set("zh_vector", ESP_LOG_NONE);
|
||||
esp_log_level_set("zh_network", ESP_LOG_NONE);
|
||||
nvs_flash_init();
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&wifi_init_config);
|
||||
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);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL);
|
||||
#else
|
||||
esp_event_handler_instance_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL, NULL);
|
||||
#endif
|
||||
example_message_t send_message = {0};
|
||||
strcpy(send_message.char_value, "THIS IS A CHAR");
|
||||
send_message.float_value = 1.234;
|
||||
send_message.bool_value = false;
|
||||
for (;;)
|
||||
{
|
||||
send_message.int_value = esp_random();
|
||||
zh_network_send(NULL, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
zh_network_send(target, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id)
|
||||
{
|
||||
case ZH_NETWORK_ON_RECV_EVENT:;
|
||||
zh_network_event_on_recv_t *recv_data = event_data;
|
||||
printf("Message from MAC %02X:%02X:%02X:%02X:%02X:%02X is received. Data lenght %d bytes.\n", MAC2STR(recv_data->mac_addr), recv_data->data_len);
|
||||
example_message_t *recv_message = (example_message_t *)recv_data->data;
|
||||
printf("Char %s\n", recv_message->char_value);
|
||||
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!
|
||||
break;
|
||||
case ZH_NETWORK_ON_SEND_EVENT:;
|
||||
zh_network_event_on_send_t *send_data = event_data;
|
||||
if (send_data->status == ZH_NETWORK_SEND_SUCCESS)
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Thanks to [Marton Larrosa](mailto:marton@mail.com) for participating in the testing.
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
component.mk
Normal file
0
component.mk
Normal file
173
include/zh_network.h
Normal file
173
include/zh_network.h
Normal file
@ -0,0 +1,173 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_network component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "string.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_now.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "zh_vector.h"
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#include "esp_system.h"
|
||||
#else
|
||||
#include "esp_random.h"
|
||||
#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
|
||||
|
||||
/**
|
||||
* @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, \
|
||||
.task_priority = 4, \
|
||||
.stack_size = 3072, \
|
||||
.queue_size = 32, \
|
||||
.max_waiting_time = 1000, \
|
||||
.id_vector_size = 100, \
|
||||
.route_vector_size = 100, \
|
||||
.wifi_interface = WIFI_IF_STA \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
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
|
||||
{
|
||||
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.
|
||||
} zh_network_init_config_t;
|
||||
|
||||
/// \cond
|
||||
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
|
||||
/// \endcond
|
||||
|
||||
/**
|
||||
* @brief Enumeration of possible ESP-NOW events.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
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
|
||||
} 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
|
||||
{
|
||||
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
|
||||
} zh_network_event_on_recv_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize 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
|
||||
*
|
||||
* @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - 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);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize ESP-NOW interface.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if deinitialization was success
|
||||
* - ESP_FAIL if ESP-NOW is not initialized
|
||||
*/
|
||||
esp_err_t zh_network_deinit(void);
|
||||
|
||||
/**
|
||||
* @brief Send ESP-NOW data.
|
||||
*
|
||||
* @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast.
|
||||
* @param[in] data Pointer to a buffer containing the data for send.
|
||||
* @param[in] data_len Sending data length.
|
||||
*
|
||||
* @note The function will return an ESP_ERR_INVALID_STATE error if less than 50% of the size set at initialization remains in the message queue.
|
||||
*
|
||||
* @return
|
||||
* - 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_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
version.txt
Normal file
1
version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.0.0
|
848
zh_network.c
Normal file
848
zh_network.c
Normal file
@ -0,0 +1,848 @@
|
||||
/**
|
||||
* @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
|
||||
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
|
||||
static void _processing(void *pvParameter);
|
||||
|
||||
static const char *TAG = "zh_network";
|
||||
|
||||
static EventGroupHandle_t _send_cb_status_event_group_handle = {0};
|
||||
static QueueHandle_t _queue_handle = {0};
|
||||
static TaskHandle_t _processing_task_handle = {0};
|
||||
static SemaphoreHandle_t _id_vector_mutex = {0};
|
||||
static zh_network_init_config_t _init_config = {0};
|
||||
static zh_vector_t _id_vector = {0};
|
||||
static zh_vector_t _route_vector = {0};
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
queue_id_t id;
|
||||
uint64_t time;
|
||||
data_t data;
|
||||
} queue_t;
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(ZH_NETWORK);
|
||||
/// \endcond
|
||||
|
||||
esp_err_t zh_network_init(zh_network_init_config_t *config)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
|
||||
if (config == NULL)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. The maximum value of the transmitted data size is incorrect.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (_init_config.wifi_interface == WIFI_IF_STA)
|
||||
{
|
||||
esp_read_mac(_self_mac, ESP_MAC_WIFI_STA);
|
||||
}
|
||||
else
|
||||
{
|
||||
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));
|
||||
zh_vector_init(&_id_vector, sizeof(uint32_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)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
_is_initialized = true;
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_network_deinit(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization begin.");
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW deinitialization fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
vEventGroupDelete(_send_cb_status_event_group_handle);
|
||||
vQueueDelete(_queue_handle);
|
||||
esp_now_unregister_send_cb();
|
||||
esp_now_unregister_recv_cb();
|
||||
esp_now_deinit();
|
||||
zh_vector_free(&_id_vector);
|
||||
zh_vector_free(&_route_vector);
|
||||
zh_vector_free(&_response_vector);
|
||||
vTaskDelete(_processing_task_handle);
|
||||
_is_initialized = false;
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len)
|
||||
{
|
||||
if (target == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue begin.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(target));
|
||||
}
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (data_len == 0 || data == NULL || data_len > ZH_NETWORK_MAX_MESSAGE_SIZE)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 2)
|
||||
{
|
||||
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.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);
|
||||
if (target == NULL)
|
||||
{
|
||||
send_data->message_type = BROADCAST;
|
||||
memcpy(send_data->original_target_mac, &_broadcast_mac, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(target, &_broadcast_mac, 6) != 0)
|
||||
{
|
||||
send_data->message_type = UNICAST;
|
||||
memcpy(send_data->original_target_mac, target, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
send_data->message_type = BROADCAST;
|
||||
memcpy(send_data->original_target_mac, &_broadcast_mac, 6);
|
||||
}
|
||||
}
|
||||
memcpy(&send_data->data, data, data_len);
|
||||
send_data->data_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.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(_send_cb_status_event_group_handle, DATA_SEND_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
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
|
||||
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));
|
||||
#endif
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < (_init_config.queue_size - 2))
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Queue is almost full.");
|
||||
return;
|
||||
}
|
||||
if (data_len == sizeof(data_t))
|
||||
{
|
||||
data_t *recv_data = (data_t *)data;
|
||||
if (memcmp(&recv_data->network_id, &_init_config.network_id, sizeof(recv_data->network_id)) != 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Incorrect mesh network ID.");
|
||||
return;
|
||||
}
|
||||
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)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Repeat message received.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (xSemaphoreTake(_id_vector_mutex, portTICK_PERIOD_MS) == pdTRUE)
|
||||
{
|
||||
zh_vector_push_back(&_id_vector, &recv_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);
|
||||
#else
|
||||
memcpy(recv_data->sender_mac, esp_now_info->src_addr, 6);
|
||||
#endif
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
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.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Incorrect ESP-NOW data size.");
|
||||
}
|
||||
}
|
||||
|
||||
static void _processing(void *pvParameter)
|
||||
{
|
||||
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:
|
||||
ESP_LOGI(TAG, "Outgoing 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));
|
||||
esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
|
||||
if (peer == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
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)
|
||||
{
|
||||
memcpy(peer->peer_addr, &_broadcast_mac, 6);
|
||||
if (memcmp(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);
|
||||
if (zh_vector_get_size(&_id_vector) > _init_config.id_vector_size)
|
||||
{
|
||||
zh_vector_delete_item(&_id_vector, 0);
|
||||
}
|
||||
xSemaphoreGive(_id_vector_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
memcpy(peer->peer_addr, routing_table->intermediate_target_mac, 6);
|
||||
flag = true;
|
||||
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is found. Forwarding via MAC %02X:%02X:%02X:%02X:%02X:%02X.", MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
break;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
else
|
||||
{
|
||||
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));
|
||||
}
|
||||
queue.id = WAIT_ROUTE;
|
||||
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_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.
|
||||
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.");
|
||||
}
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (esp_now_add_peer(peer) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Internal error with adding peer.");
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
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, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(peer);
|
||||
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);
|
||||
if (esp_now_send((uint8_t *)peer->peer_addr, (uint8_t *)data, sizeof(data_t)) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
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);
|
||||
if ((bit & DATA_SEND_SUCCESS) != 0)
|
||||
{
|
||||
if (memcmp(data->original_sender_mac, &_self_mac, 6) == 0)
|
||||
{
|
||||
if (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.");
|
||||
}
|
||||
}
|
||||
if (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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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));
|
||||
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));
|
||||
queue.id = WAIT_RESPONSE;
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(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)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, i);
|
||||
}
|
||||
}
|
||||
if (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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
queue.id = WAIT_ROUTE;
|
||||
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_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.
|
||||
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_now_del_peer(peer->peer_addr);
|
||||
heap_caps_free(on_send);
|
||||
heap_caps_free(peer);
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
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);
|
||||
if (on_recv->data == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
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);
|
||||
if (on_recv->data == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
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.
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for forwarding.", 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.");
|
||||
}
|
||||
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)
|
||||
{
|
||||
zh_vector_push_back(&_response_vector, &data->confirm_id);
|
||||
if (zh_vector_get_size(&_response_vector) > _init_config.queue_size)
|
||||
{
|
||||
zh_vector_delete_item(&_response_vector, 0);
|
||||
}
|
||||
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));
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X fto MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for forwarding.", 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.");
|
||||
}
|
||||
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)
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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.
|
||||
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.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for routing request to MAC %02X:%02X:%02X:%02X:%02X:%02X from MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_target_mac), MAC2STR(queue.data.original_sender_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.");
|
||||
}
|
||||
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)
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
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));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case WAIT_RESPONSE:
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user