wifi mesh root 节点无法接收到子节点发送到外部网络的节点
Posted: Mon Dec 25, 2023 2:05 am
工作流程如下:子节点自动发送ID给外部上位机esp_mesh_send(&addr, &sg_ToDs_data, MESH_DATA_TODS, NULL, 0);,root节点通过esp_mesh_recv_toDS函数后通过socket将数据转发给上位机。上位机通过root将控制命令转发给指定的子模块,子模块通过root转发自己的处理结果。
遇到的bug:程序启动时,root节点能正常的接收到子节点发送的数据,上位机也能正常的接收到root节点和子节点的数据。 但是当上位机发送命令由根节点转发到子节点,子节点在返回结果后。子节点的自动发送ID的数据包就不会在被root节点接收到了。root节点的 result = esp_mesh_get_rx_pending(&pending);这个函数接收的pending.toDS 值会一直等于零。但是上位机发送的命令root节点就能正常的转发给子节点,子节点的返回数据也能被root节点接收。唯独子节点自动发送ID的数据不会被接收。
root节点接收数据的代码:
void esp_mesh_p2p_rx_main(void *arg)
{
esp_err_t result;
mesh_addr_t from;
mesh_addr_t to;
mesh_data_t data;
uint32_t wifi_data = 0x02;
mesh_rx_pending_t pending ;
int flag = 0;
char temp_string[128];
data.data = rx_buf;
data.size = RX_SIZE;
while (is_mesh_connected)
{
vTaskDelay(10);
result = esp_mesh_get_rx_pending(&pending);
if(result == ESP_FAIL)
continue;
for(int i = 0; i < pending.toSelf; i++)
{ //子节点会执行这段代码,接收根节点的数据
result = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0);
if(result == ESP_OK)
{
memset(temp_string, 0, 128);
hex_to_string(temp_string, data.data, data.size);
printf("ID %d RECV TO SELF:%s\r\n",g_device_config.paper_number, temp_string);
queue_in_buffer(g_tcp_recv_data, (unsigned char *)data.data, data.size);
xQueueSendToBack( g_protocol_event, ( void * ) &wifi_data, ( TickType_t ) 10 );
}
}
for(int i = 0; i < pending.toDS; i++)
{//root节点会执行这段代码向上位机转发子节点的数据
result = esp_mesh_recv_toDS(&from, &to, &data, portMAX_DELAY, &flag, NULL, 0);
if(result == ESP_OK)
{
memset(temp_string, 0, 128);
hex_to_string(temp_string, data.data, data.size);
printf("ID %d RECV TO DS:%s\r\n",g_device_config.paper_number, temp_string);
flag = tcp_client_send_data(10000, data.data, data.size);
}
}
}
vTaskDelete(NULL);
}
root节点发送数据给子节点的函数代码:
int esp_mesh_send_data(int port, const void *data, int len)
{
mesh_data_t send_data;
mesh_addr_t addr;
char temp_string[128];
send_data.data = (uint8_t *)data;
send_data.size = len;
send_data.proto = MESH_PROTO_BIN;
send_data.tos = MESH_TOS_P2P;
memcpy(&addr, data + 10, 6);
memset(temp_string, 0, 128);
hex_to_string(temp_string, data, len);
printf("ID %d SEND TO CH:%s\r\n",g_device_config.paper_number, temp_string);
esp_mesh_send(&addr, &send_data, MESH_DATA_P2P, NULL, 0);
return 0;
}
子节点 发送数据的代码
int esp_mesh_add_send_queue(int port ,const void *data, int len)
{
queue_in_buffer(sg_send_data, (unsigned char *)data, len);
xQueueSendToBack( sg_send_len, ( void * ) &len, ( TickType_t ) 10 );
return 0;
}
static void esp_mesh_send_data_task(void *pvParameters)
{
mesh_addr_t addr;
uint32_t send_len;
unsigned char data[1024];
char temp_string[128];
while(true)
{
if(xQueueReceive(sg_send_len, &send_len, portMAX_DELAY) == pdTRUE)
{
memset(data, 0, 64);
memset(&sg_ToDs_data, 0, sizeof(mesh_data_t));
queue_out_buffer(sg_send_data, data, send_len);
sg_ToDs_data.data = (uint8_t *)data;
sg_ToDs_data.size = send_len;
sg_ToDs_data.proto = MESH_PROTO_BIN;
sg_ToDs_data.tos = MESH_TOS_P2P;
memset(temp_string, 0, 128);
hex_to_string(temp_string, data, send_len);
printf("ID %d SEND TO DS:%s\r\n",g_device_config.paper_number, temp_string);
//子节点所有的数据发送都会从这个任务中依次执行,不会出现重复调用的情况
esp_mesh_send(&addr, &sg_ToDs_data, MESH_DATA_TODS, NULL, 0);
}
}
}
请各位大佬帮忙看看这个奇怪的bug是怎么引起的。
遇到的bug:程序启动时,root节点能正常的接收到子节点发送的数据,上位机也能正常的接收到root节点和子节点的数据。 但是当上位机发送命令由根节点转发到子节点,子节点在返回结果后。子节点的自动发送ID的数据包就不会在被root节点接收到了。root节点的 result = esp_mesh_get_rx_pending(&pending);这个函数接收的pending.toDS 值会一直等于零。但是上位机发送的命令root节点就能正常的转发给子节点,子节点的返回数据也能被root节点接收。唯独子节点自动发送ID的数据不会被接收。
root节点接收数据的代码:
void esp_mesh_p2p_rx_main(void *arg)
{
esp_err_t result;
mesh_addr_t from;
mesh_addr_t to;
mesh_data_t data;
uint32_t wifi_data = 0x02;
mesh_rx_pending_t pending ;
int flag = 0;
char temp_string[128];
data.data = rx_buf;
data.size = RX_SIZE;
while (is_mesh_connected)
{
vTaskDelay(10);
result = esp_mesh_get_rx_pending(&pending);
if(result == ESP_FAIL)
continue;
for(int i = 0; i < pending.toSelf; i++)
{ //子节点会执行这段代码,接收根节点的数据
result = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0);
if(result == ESP_OK)
{
memset(temp_string, 0, 128);
hex_to_string(temp_string, data.data, data.size);
printf("ID %d RECV TO SELF:%s\r\n",g_device_config.paper_number, temp_string);
queue_in_buffer(g_tcp_recv_data, (unsigned char *)data.data, data.size);
xQueueSendToBack( g_protocol_event, ( void * ) &wifi_data, ( TickType_t ) 10 );
}
}
for(int i = 0; i < pending.toDS; i++)
{//root节点会执行这段代码向上位机转发子节点的数据
result = esp_mesh_recv_toDS(&from, &to, &data, portMAX_DELAY, &flag, NULL, 0);
if(result == ESP_OK)
{
memset(temp_string, 0, 128);
hex_to_string(temp_string, data.data, data.size);
printf("ID %d RECV TO DS:%s\r\n",g_device_config.paper_number, temp_string);
flag = tcp_client_send_data(10000, data.data, data.size);
}
}
}
vTaskDelete(NULL);
}
root节点发送数据给子节点的函数代码:
int esp_mesh_send_data(int port, const void *data, int len)
{
mesh_data_t send_data;
mesh_addr_t addr;
char temp_string[128];
send_data.data = (uint8_t *)data;
send_data.size = len;
send_data.proto = MESH_PROTO_BIN;
send_data.tos = MESH_TOS_P2P;
memcpy(&addr, data + 10, 6);
memset(temp_string, 0, 128);
hex_to_string(temp_string, data, len);
printf("ID %d SEND TO CH:%s\r\n",g_device_config.paper_number, temp_string);
esp_mesh_send(&addr, &send_data, MESH_DATA_P2P, NULL, 0);
return 0;
}
子节点 发送数据的代码
int esp_mesh_add_send_queue(int port ,const void *data, int len)
{
queue_in_buffer(sg_send_data, (unsigned char *)data, len);
xQueueSendToBack( sg_send_len, ( void * ) &len, ( TickType_t ) 10 );
return 0;
}
static void esp_mesh_send_data_task(void *pvParameters)
{
mesh_addr_t addr;
uint32_t send_len;
unsigned char data[1024];
char temp_string[128];
while(true)
{
if(xQueueReceive(sg_send_len, &send_len, portMAX_DELAY) == pdTRUE)
{
memset(data, 0, 64);
memset(&sg_ToDs_data, 0, sizeof(mesh_data_t));
queue_out_buffer(sg_send_data, data, send_len);
sg_ToDs_data.data = (uint8_t *)data;
sg_ToDs_data.size = send_len;
sg_ToDs_data.proto = MESH_PROTO_BIN;
sg_ToDs_data.tos = MESH_TOS_P2P;
memset(temp_string, 0, 128);
hex_to_string(temp_string, data, send_len);
printf("ID %d SEND TO DS:%s\r\n",g_device_config.paper_number, temp_string);
//子节点所有的数据发送都会从这个任务中依次执行,不会出现重复调用的情况
esp_mesh_send(&addr, &sg_ToDs_data, MESH_DATA_TODS, NULL, 0);
}
}
}
请各位大佬帮忙看看这个奇怪的bug是怎么引起的。