Release:5.20灰测
This commit is contained in:
97
Core/User/Network/udp_manager.c
Normal file
97
Core/User/Network/udp_manager.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* @file udp_manager.c
|
||||
* @brief UDP管理模块
|
||||
* @details 处理充电桩与服务器之间的UDP通信,提供数据发送功能。
|
||||
* 支持向指定充电桩发送数据以及向上位机服务器发送数据。
|
||||
* @date 2025-01-09 14:32:15
|
||||
* @version 1.0.0
|
||||
* @copyright Copyright (c) 2026
|
||||
*/
|
||||
|
||||
#include "udp_manager.h"
|
||||
|
||||
struct netconn *datalink_conn; // 数据链路句柄
|
||||
|
||||
/**
|
||||
* @brief 桩IP地址
|
||||
* @note 桩IP地址从10.12.19.101开始递增
|
||||
*/
|
||||
static ip4_addr_t s_point_ip[6] = {
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 101), /* 桩1 */
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 102), /* 桩2 */
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 103), /* 桩3 */
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 104), /* 桩4 */
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 105), /* 桩5 */
|
||||
IPADDR4_INIT_BYTES(10, 12, 19, 106), /* 桩6 */
|
||||
};
|
||||
|
||||
static ip4_addr_t s_server_ip = IPADDR4_INIT_BYTES(10, 12, 19, 107); /* 上位机 */
|
||||
|
||||
/**
|
||||
* @brief UDP发送
|
||||
* @note 发送数据到指定桩
|
||||
* @param point_id 目标桩ID
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
* @return err_t 错误码
|
||||
*/
|
||||
err_t udp_send_to_point(uint8_t point_id, uint8_t *data, u16_t len)
|
||||
{
|
||||
if (!data || len == 0)
|
||||
{
|
||||
return ERR_VAL;
|
||||
}
|
||||
|
||||
if (!datalink_conn)
|
||||
{
|
||||
return ERR_CLSD;
|
||||
}
|
||||
|
||||
if (point_id < 1 || point_id > 6)
|
||||
{
|
||||
return ERR_VAL;
|
||||
}
|
||||
|
||||
struct netbuf *buf = netbuf_new();
|
||||
if (!buf)
|
||||
{
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
void *buf_data = netbuf_alloc(buf, len);
|
||||
if (!buf_data)
|
||||
{
|
||||
netbuf_delete(buf);
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
memcpy(buf_data, data, len);
|
||||
err_t err = netconn_sendto(datalink_conn, buf, &s_point_ip[point_id - 1], LINK_STAKE_PORT);
|
||||
netbuf_delete(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
err_t udp_send_to_server(uint8_t *data, uint16_t len)
|
||||
{
|
||||
struct netbuf *buf = netbuf_new();
|
||||
if (!buf)
|
||||
{
|
||||
return ERR_MEM;
|
||||
}
|
||||
if (!datalink_conn)
|
||||
{
|
||||
return ERR_CLSD;
|
||||
}
|
||||
|
||||
void *buf_data = netbuf_alloc(buf, len); // ← 在 netbuf 内部分配内存
|
||||
if (!buf_data)
|
||||
{
|
||||
netbuf_delete(buf);
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
memcpy(buf_data, data, len); // ← 拷贝数据,不再依赖外部指针
|
||||
err_t err = netconn_sendto(datalink_conn, buf, &s_server_ip, LINK_APP_PORT);
|
||||
netbuf_delete(buf);
|
||||
return err;
|
||||
}
|
||||
41
Core/User/Network/udp_manager.h
Normal file
41
Core/User/Network/udp_manager.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file user\network\udp_manager.h
|
||||
* @author luhuaishuai
|
||||
* @version v0.1
|
||||
* @date 2023-10-11
|
||||
* @brief Briefly describe the function of your function
|
||||
******************************************************************************
|
||||
*/
|
||||
#ifndef __UDP_MANAGER_H
|
||||
#define __UDP_MANAGER_H
|
||||
|
||||
/* includes ----------------------------------------------------------------------------------------------*/
|
||||
#include "global.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/api.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "board_config.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
extern struct netconn *datalink_conn; // 数据链路句柄
|
||||
|
||||
/**
|
||||
* @brief 发送数据到充电桩
|
||||
* @param point_index 桩编号 1~6
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
*/
|
||||
err_t udp_send_to_point(uint8_t point_index, uint8_t *data, u16_t len);
|
||||
|
||||
/**
|
||||
* @brief 发送数据到上位机
|
||||
* @param data 数据指针
|
||||
* @param len 数据长度
|
||||
*/
|
||||
err_t udp_send_to_server(uint8_t *data, uint16_t len);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* __UDP_MANAGER_H */
|
||||
56
Core/User/Network/udp_router.c
Normal file
56
Core/User/Network/udp_router.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file udp_router.c
|
||||
* @brief UDP路由分发模块
|
||||
* @details 处理充电桩与服务器之间的UDP通信路由分发,根据指令字符串匹配对应的处理函数。
|
||||
* 支持充电桩南向通信和上位机控制两类路由,通过静态路由表实现高效的指令分发。
|
||||
* @date 2025-01-09 14:32:15
|
||||
* @version 1.0.0
|
||||
* @copyright Copyright (c) 2026
|
||||
*/
|
||||
#include "udp_router.h"
|
||||
|
||||
/**
|
||||
* @brief UDP 路由表
|
||||
*/
|
||||
static const route_entry_t ROUTE_TABLE[] = {
|
||||
/* ── 充电桩南向路由 ── */
|
||||
{"online", point_callback_power_on, " 上电 "},
|
||||
{"heartbeat", point_callback_heartbeat, " 心跳 "},
|
||||
{"start charging", point_callback_start_charging, " 开始充电反馈 "},
|
||||
{"end charging", point_callback_end_charging, " 停止充电反馈 "},
|
||||
{"realtime data", point_callback_realtime_data, " 实时数据 "},
|
||||
{"settlement bill", point_callback_settlement_bill, " 结算账单 "},
|
||||
{"proactive end charging", point_callback_proactive_end_charging, " 桩主动停止充电反馈 "},
|
||||
{"charge process real", point_callback_charge_process, " BMS实时需求 "},
|
||||
{"bms info real", point_callback_bms_info, " BMS实时信息 "},
|
||||
/* ── 上位机路由 ── */
|
||||
{"server_login", (cmd_handler_t)host_computer_on_login, " 上位机登录 "},
|
||||
{"server_get_status", (cmd_handler_t)host_computer_on_get_status, " 获取状态 "},
|
||||
{"server_reboot", (cmd_handler_t)host_computer_on_reboot, " 重启设备 "},
|
||||
};
|
||||
|
||||
#define ROUTE_TABLE_SIZE (sizeof(ROUTE_TABLE) / sizeof(ROUTE_TABLE[0]))
|
||||
|
||||
/**
|
||||
* @brief 路由分发入口
|
||||
* @param id 指令发送的充电桩ID
|
||||
* @param cmd 指令字符串
|
||||
* @param json_pack JSON格式的指令参数
|
||||
*/
|
||||
void udp_route_dispatch(uint8_t id, const char *cmd, cJSON *json_pack)
|
||||
{
|
||||
if (!cmd)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < ROUTE_TABLE_SIZE; i++)
|
||||
{
|
||||
if (strcmp(cmd, ROUTE_TABLE[i].cmd) == 0)
|
||||
{
|
||||
printf("[ UDP 接收路由 ] 指令: %-26s │ 桩ID: %d │ %s \r\n",cmd,id, ROUTE_TABLE[i].desc);
|
||||
ROUTE_TABLE[i].handler(id, json_pack);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("[ UDP 接收路由 ] = 未知指令: '%s' 桩ID: %d \r\n", cmd, id);
|
||||
}
|
||||
23
Core/User/Network/udp_router.h
Normal file
23
Core/User/Network/udp_router.h
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#ifndef __UDP_ROUTER_H
|
||||
#define __UDP_ROUTER_H
|
||||
|
||||
/* includes ----------------------------------------------------------------------------------------------*/
|
||||
#include "global.h"
|
||||
#include "board_config.h"
|
||||
#include "point_protocol.h"
|
||||
#include "host_computer_protocol.h"
|
||||
|
||||
typedef void (*cmd_handler_t)(uint8_t id, cJSON *json);
|
||||
typedef struct
|
||||
{
|
||||
const char *cmd;
|
||||
cmd_handler_t handler;
|
||||
const char *desc;
|
||||
} route_entry_t;
|
||||
|
||||
|
||||
void udp_route_dispatch(uint8_t id, const char *cmd, cJSON *json_pack);//UDP路由分发入口
|
||||
|
||||
|
||||
#endif /* __UDP_ROUTER_H */
|
||||
Reference in New Issue
Block a user