feat: 变更架构为广播,发送到所有绑定的端口
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
# DataRouter
|
||||
|
||||
轻量级 C 语言数据路由库。**发送能力和接收能力分开声明**,端口独立设置 send_type 和 recv_type,支持多外设一对多广播路由。
|
||||
轻量级 C 语言数据路由库。**发送能力和接收能力分开声明**,端口绑定 `send_type` 和外设 handler 列表。调用 `datarouter_send_<id>(data, len)` 时自动广播到所有匹配端口。
|
||||
|
||||
## 概念
|
||||
|
||||
| 术语 | 说明 |
|
||||
|------|------|
|
||||
| **发送能力 (Send Capability)** | 编译期声明的可发送报文类型,生成 `datarouter_send_<id>()` |
|
||||
| **接收能力 (Recv Capability)** | 编译期声明的可接收报文类型,生成 `datarouter_on_recv_<id>()` |
|
||||
| **端口 (Port)** | 运行期连接点,独立设置 send_type + recv_type + handler 列表 |
|
||||
| **发送能力 (Send Capability)** | 编译期声明的可发送报文类型,生成 `datarouter_send_<id>(data, len)` |
|
||||
| **接收能力 (Recv Capability)** | 编译期声明的可接收报文类型,生成 `datarouter_on_recv_<id>(port, data, len)` |
|
||||
| **端口 (Port)** | 运行期创建的连接点,包含 `send_type` + handler 列表 |
|
||||
| **Handler** | 用户实现的物理外设发送函数,一个端口可注册多个(默认上限 4) |
|
||||
|
||||
## 快速开始
|
||||
@@ -27,14 +27,11 @@
|
||||
DATAROUTER_RECV(command, "Remote control command")
|
||||
```
|
||||
|
||||
> 库默认自带一份示例配置。将你的 `datarouter_config.h` 放到项目目录,
|
||||
> 编译时添加 `-I /path/to/your/config` 即可覆盖。无需修改库源代码。
|
||||
> 库自带默认配置。将你的 `datarouter_config.h` 放到项目目录,
|
||||
> 编译时添加 `-I /path/to/your/config` 即可覆盖。无需修改库源码。
|
||||
>
|
||||
> **注意**:确保你的配置目录在包含路径中出现在库目录之前,否则编译 `datarouter.c`
|
||||
> 时仍会使用库的默认配置。在 CMake 中可参考:
|
||||
> ```cmake
|
||||
> # 你的 CMakeLists.txt
|
||||
> include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}) # 全局优先
|
||||
> include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
> add_subdirectory(path/to/DataRouter)
|
||||
> target_link_libraries(my_app DataRouter)
|
||||
> ```
|
||||
@@ -56,26 +53,17 @@ static void tcp_send(uint32_t port_id, const void *data, size_t len) {
|
||||
```c
|
||||
datarouter_init();
|
||||
|
||||
/* —— 只发送:传感器上报 —— */
|
||||
/* —— 传感器上报:sensor 报文走 UART + TCP —— */
|
||||
datarouter_port_t tx = datarouter_port_create();
|
||||
datarouter_port_set_send_type(tx, DATAROUTER_SEND_sensor);
|
||||
datarouter_port_add_handler(tx, uart_send);
|
||||
datarouter_port_add_handler(tx, tcp_send);
|
||||
datarouter_send_sensor(tx, "temperature:25.3", 16); // → UART + TCP
|
||||
|
||||
/* —— 只接收:RTCM 修正 —— */
|
||||
datarouter_port_t rx = datarouter_port_create();
|
||||
datarouter_port_set_recv_type(rx, DATAROUTER_RECV_rtcm);
|
||||
// 外设收到数据后由用户调回调:
|
||||
datarouter_on_recv_rtcm(rx, data, len);
|
||||
/* 不指定端口,库自动广播到所有 send_type=sensor 的端口 */
|
||||
datarouter_send_sensor("temperature:25.3", 16); // → UART + TCP
|
||||
|
||||
/* —— 同时收发:状态上报 + 命令接收 —— */
|
||||
datarouter_port_t bi = datarouter_port_create();
|
||||
datarouter_port_set_send_type(bi, DATAROUTER_SEND_status);
|
||||
datarouter_port_set_recv_type(bi, DATAROUTER_RECV_command);
|
||||
datarouter_port_add_handler(bi, uart_send);
|
||||
datarouter_send_status(bi, "status:OK", 9);
|
||||
datarouter_on_recv_command(bi, "reboot", 6);
|
||||
/* —— 接收:实现回调,外设数据到达后自行调用 —— */
|
||||
datarouter_on_recv_rtcm(rx_port, data, len);
|
||||
```
|
||||
|
||||
### 4. 实现接收回调
|
||||
@@ -84,10 +72,6 @@ datarouter_on_recv_command(bi, "reboot", 6);
|
||||
void datarouter_on_recv_rtcm(uint32_t port_id, const void *data, size_t len) {
|
||||
apply_rtcm_correction(data, len);
|
||||
}
|
||||
|
||||
void datarouter_on_recv_command(uint32_t port_id, const void *data, size_t len) {
|
||||
execute_command(data, len);
|
||||
}
|
||||
```
|
||||
|
||||
## API 一览
|
||||
@@ -107,8 +91,6 @@ void datarouter_port_delete(datarouter_port_t port); // 删除
|
||||
|
||||
void datarouter_port_set_send_type(port, int cap_id); // 设置发送类型
|
||||
int datarouter_port_get_send_type(port); // -1 = 未设置
|
||||
void datarouter_port_set_recv_type(port, int cap_id); // 设置接收类型
|
||||
int datarouter_port_get_recv_type(port); // -1 = 未设置
|
||||
|
||||
int datarouter_port_add_handler(port, handler); // 注册外设 handler
|
||||
int datarouter_port_remove_handler(port, handler); // 移除
|
||||
@@ -119,12 +101,13 @@ int datarouter_port_handler_count(port); // 当前数量
|
||||
### 发送触发(每个 SEND 能力生成一个)
|
||||
|
||||
```c
|
||||
int datarouter_send_sensor(port, data, len); // 0 成功 / -1 未初始化
|
||||
int datarouter_send_status(port, data, len); // -2 端口无效
|
||||
// -3 send_type 不匹配
|
||||
// -4 无 handler
|
||||
int datarouter_send_sensor(const void *data, size_t len); // 0 成功
|
||||
int datarouter_send_status(const void *data, size_t len); // -1 未初始化
|
||||
// -2 无匹配端口
|
||||
```
|
||||
|
||||
不指定端口,库遍历所有端口,`send_type` 匹配的逐一调用其全部 handler。
|
||||
|
||||
### 接收回调(每个 RECV 能力生成一个声明)
|
||||
|
||||
```c
|
||||
@@ -149,17 +132,18 @@ const char *datarouter_recv_cap_desc(int cap_id);
|
||||
## 路由模型
|
||||
|
||||
```
|
||||
send_sensor(port, data, len)
|
||||
datarouter_send_sensor("temperature:25.3", 16)
|
||||
│
|
||||
├─→ handler[0] = uart_send → UART
|
||||
├─→ handler[1] = tcp_send → TCP
|
||||
└─→ handler[2] = can_send → CAN
|
||||
├─→ port 0 (send_type=sensor) handlers[0]=uart_send → UART
|
||||
│ handlers[1]=tcp_send → TCP
|
||||
└─→ port 1 (send_type=sensor) handlers[0]=can_send → CAN
|
||||
```
|
||||
|
||||
- 一个端口可注册多个 handler(一对多广播)
|
||||
- 调用 `send_<id>(data, len)` 时,库遍历所有端口,自动匹配 `send_type`
|
||||
- 每个端口可注册多个 handler(一对多广播)
|
||||
- 同一 handler 可注册到多个端口(多路复用)
|
||||
- 运行时通过 `add_handler` / `remove_handler` 动态改变路由
|
||||
- 清空 handler 列表后发送返回 -4
|
||||
- 运行时通过 `add_handler` / `remove_handler` / `port_delete` 动态改变路由
|
||||
- 无任何端口匹配该 `send_type` 时返回 -2
|
||||
|
||||
## 编译
|
||||
|
||||
@@ -183,18 +167,19 @@ ctest --test-dir build
|
||||
./build/test_datarouter
|
||||
```
|
||||
|
||||
当前 27 个测试用例,覆盖端口管理、send_type/recv_type 独立设置、handler 增删、一对多广播、接收回调、能力元数据、错误边界。
|
||||
当前 22 个测试用例,覆盖端口管理、send_type 与 handler 设置、自动广播路由、接收回调、能力元数据。
|
||||
|
||||
## 文件结构
|
||||
|
||||
```
|
||||
datarouter_config.h 发送 + 接收能力配置(用户编辑)
|
||||
datarouter.h 公共 API + X-macro 展开
|
||||
datarouter.c 实现(端口表 + handler 遍历)
|
||||
example.c 收发分离使用示例
|
||||
tests/test_datarouter.c 单元测试(27 用例)
|
||||
DESIGN.md 详细设计文档(含 PlantUML 图)
|
||||
TEST_CASES.md 测试用例文档
|
||||
datarouter_config.h 发送 + 接收能力配置(用户可覆盖)
|
||||
datarouter.h 公共 API + X-macro 展开
|
||||
datarouter.c 实现(端口表 + send 遍历)
|
||||
datarouter_config_template.h 外部配置模板
|
||||
example.c 使用示例
|
||||
tests/test_datarouter.c 单元测试(22 用例)
|
||||
DESIGN.md 详细设计文档(含 PlantUML 图)
|
||||
TEST_CASES.md 测试用例文档
|
||||
```
|
||||
|
||||
## 依赖
|
||||
|
||||
Reference in New Issue
Block a user