feat: 变更架构为广播,发送到所有绑定的端口

This commit is contained in:
2026-07-26 22:29:47 +08:00
parent e3c40c822b
commit e8ee92110b
10 changed files with 614 additions and 574 deletions
+12 -26
View File
@@ -4,20 +4,16 @@
*
* ## 概念
*
* - **发送能力 (Send Capability)**:编译期声明的可发送报文类型
* - **接收能力 (Recv Capability)**:编译期声明的可接收报文类型
* - **端口 (Port)**:运行期连接点,独立设置 send_type + recv_type + 外设 handler 列表
* - **发送能力**:编译期声明的可发送报文类型,生成 datarouter_send_<id>(data, len)
* - **接收能力**:编译期声明的可接收报文类型,生成 datarouter_on_recv_<id>(port, data, len)
* - **端口**:运行期创建的连接点,包含 send_type + handler 列表
*
* ## 使用流程
* ## 路由
*
* 1. 创建 datarouter_config.h 声明发送和接收能力(见 datarouter_config_template.h)
* 2. 编译时通过 -I 指向你的配置目录;若无,库使用内置默认配置
* 3. datarouter_init() 初始化
* 4. port_create() 创建端口
* 5. port_set_send_type() 设置发送类型 / port_set_recv_type() 设置接收类型
* 6. port_add_handler() 注册外设发送函数
* 7. 调 datarouter_send_<id>() 触发发送(库调用所有 handler)
* 8. 实现 datarouter_on_recv_<id>() 处理接收数据
* 调用 datarouter_send_<id>(data, len) 时,库遍历所有端口,
* 找到 send_type 匹配的端口,逐一调用其全部 handler。无需指定从哪个端口发送。
*
* 接收侧由用户自行调用 datarouter_on_recv_<id>(port, data, len) 处理外设收到的数据。
*/
#ifndef DATAROUTER_H
@@ -26,14 +22,6 @@
#include <stdint.h>
#include <stddef.h>
/*
* 配置加载策略:
* 1. 优先从 include path 搜索 <datarouter_config.h>(用户提供的版本)
* 2. 找不到则回退到库自带的 "datarouter_config.h"(默认配置)
*
* 用户只需将自己的 datarouter_config.h 放到项目目录,
* 编译时添加 -I /path/to/your/config 即可覆盖内置配置。
*/
#if defined(__has_include)
# if __has_include(<datarouter_config.h>)
# include <datarouter_config.h>
@@ -66,7 +54,7 @@ enum datarouter_recv_cap {
DATAROUTER_RECV_CAP_COUNT
};
/* ========== 外设发送 handler 类型 ========== */
/* ========== 外设发送 handler ========== */
typedef void (*datarouter_send_handler_t)(uint32_t port_id, const void *data, size_t len);
@@ -74,14 +62,14 @@ typedef void (*datarouter_send_handler_t)(uint32_t port_id, const void *data, si
#define DATAROUTER_MAX_HANDLERS_PER_PORT 4
#endif
/* ========== 发送触发函数(每个发送能力生成一个) ========== */
/* ========== 发送触发函数 ========== */
#define DATAROUTER_SEND(id, desc) \
int datarouter_send_##id(uint32_t port_id, const void *data, size_t len);
int datarouter_send_##id(const void *data, size_t len);
DATAROUTER_SEND_CAPABILITIES
#undef DATAROUTER_SEND
/* ========== 接收回调声明(每个接收能力生成一个,用户实现) ========== */
/* ========== 接收回调声明 ========== */
#define DATAROUTER_RECV(id, desc) \
void datarouter_on_recv_##id(uint32_t port_id, const void *data, size_t len);
@@ -101,8 +89,6 @@ void datarouter_port_delete(datarouter_port_t port);
void datarouter_port_set_send_type(datarouter_port_t port, int cap_id);
int datarouter_port_get_send_type(datarouter_port_t port);
void datarouter_port_set_recv_type(datarouter_port_t port, int cap_id);
int datarouter_port_get_recv_type(datarouter_port_t port);
int datarouter_port_add_handler(datarouter_port_t port, datarouter_send_handler_t handler);
int datarouter_port_remove_handler(datarouter_port_t port, datarouter_send_handler_t handler);