Files
BR_YKC/Core/User/App/task_udp.c
2026-05-21 12:19:01 +08:00

110 lines
3.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
******************************************************************************
* @file User\App\task_udp.c
* @author 路淮
* @version v0.1
* @date 2026-05-21
* @brief UDP通信任务处理模块
******************************************************************************
*/
#include "task_udp.h"
/**
* @brief udp_recv_task_functionUDP 数据接受任务
* @note none
* @param none
* @retval none
*/
void udp_recv_task_function(void const *argument)
{
err_t recv_err;
struct netbuf *datalink_buf = NULL;
datalink_conn = netconn_new(NETCONN_UDP);
netconn_bind(datalink_conn, IP_ADDR_ANY, LINK_SERVER_PORT);
UDP_Message_Queue_Init(); // 初始化UDP接收队列
/*桩UDP通讯初始化完成 发送云快充任务通知*/
xTaskNotifyGive(YkcTaskHandle);
xTaskNotifyGive(Air724_ParseTaskHandle);
xTaskNotifyGive(UDP_ParseTaskHandle);
while (1)
{
recv_err = netconn_recv(datalink_conn, &datalink_buf);
if (recv_err == ERR_OK && datalink_buf != NULL)
{
uint8_t *playload;
uint16_t playload_len;
netbuf_data(datalink_buf, (void *)&playload, &playload_len);
if (playload_len > 0)
{
UdpMsg_t msg;
ip_addr_copy(msg.src_ip, *netbuf_fromaddr(datalink_buf)); // 获取UDP源IP
msg.src_port = netbuf_fromport(datalink_buf); // 获取UDP源端口
msg.len = playload_len;
msg.data = (char *)pvPortMalloc(playload_len + 1);
if (msg.data != NULL)
{
memcpy(msg.data, playload, playload_len);
msg.data[playload_len] = '\0';
// 队列满,释放数据内存
if (xQueueSend(UDP_Message_Queue, &msg, 0) != pdPASS)
{
vPortFree(msg.data);
}
}
}
netbuf_delete(datalink_buf); // 释放UDP网络缓冲区
}
else
{
if (recv_err != ERR_TIMEOUT && recv_err != ERR_WOULDBLOCK)
{
printf("datalink netconn_recv err: %d\r\n", recv_err);
}
}
}
}
/**
* @brief udp_parse_task_functionUDP 数据解析任务
* @note none
* @param none
* @retval none
*/
void udp_parse_task_function(void const *argument)
{
UdpMsg_t msg;
cJSON *root = NULL, *cmd = NULL, *id = NULL;
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); /* 等待桩通讯协议层完成*/
while (1)
{
if (xQueueReceive(UDP_Message_Queue, &msg, portMAX_DELAY) == pdPASS)
{
root = cJSON_Parse((const char *)msg.data);
if (root == NULL)
{
printf("JSON Parse Failed: %s\r\n", msg.data);
vPortFree(msg.data);
continue;
}
id = cJSON_GetObjectItem(root, "id");
cmd = cJSON_GetObjectItem(root, "cmd");
if (cmd != NULL && id != NULL)
{
const char *cmd_str = cmd->valuestring;
udp_route_dispatch((uint8_t)id->valueint, cmd->valuestring, root); //送入路由处理
cJSON_Delete(root);
}
else
{
printf("Missing 'code' field from \r\n");
cJSON_Delete(root);
}
vPortFree(msg.data);
msg.data = NULL;
}
}
}