feat: 新增接收dispatch反向路由
CI / host (pull_request) Successful in 2s
CI / cortex-m (pull_request) Successful in 5s

This commit is contained in:
2026-07-30 10:49:44 +08:00
parent fa1f547e81
commit 1c3e48d59f
10 changed files with 373 additions and 664 deletions
+22 -5
View File
@@ -6,14 +6,17 @@
*
* - **发送能力**:编译期声明的可发送报文类型,生成 dr_send_<id>(data, len)
* - **接收能力**:编译期声明的可接收报文类型,生成 dr_on_recv_<id>(port, data, len)
* - **端口**:运行期创建的连接点,包含 send_type + handler 列表
* - **端口**:运行期连接点,包含 send_type + recv_type + handler 列表
*
* ## 路由
* ## 发送路由
*
* 调用 dr_send_<id>(data, len) 时,库遍历所有端口,
* 找到 send_type 匹配的端口,逐一调用其全部 handler。无需指定从哪个端口发送。
* dr_send_<id>(data, len) 遍历所有端口,找到 send_type 匹配的端口,
* 逐一调用其全部 handler,无需指定从哪个端口发送。
*
* 接收侧由用户自行调用 dr_on_recv_<id>(port, data, len) 处理外设收到的数据。
* ## 接收路由
*
* 外设(UART / SPI / CAN)收到数据后,调用 dr_port_dispatch(port, data, len),
* 库根据端口的 recv_type 自动查表调用对应的 dr_on_recv_<id>(),无需手动 if/else。
*/
#ifndef DR_H
@@ -89,12 +92,26 @@ void dr_port_delete(dr_port_t port);
void dr_port_set_send_type(dr_port_t port, int cap_id);
int dr_port_get_send_type(dr_port_t port);
void dr_port_set_recv_type(dr_port_t port, int cap_id);
int dr_port_get_recv_type(dr_port_t port);
int dr_port_add_handler(dr_port_t port, dr_send_handler_t handler);
int dr_port_remove_handler(dr_port_t port, dr_send_handler_t handler);
void dr_port_clear_handlers(dr_port_t port);
int dr_port_handler_count(dr_port_t port);
/**
* @brief 按端口绑定的 recv_type 自动路由到对应的 dr_on_recv_<id>() 回调
*
* 外设中断中收到数据后调用此函数,无需自己写 if/else 判断报文类型。
* 库通过 X-macro 生成函数指针表,直接查表调用,零分支预测开销。
*
* @param[in] port 接收端口 ID
* @param[in] data 数据缓冲区
* @param[in] len 数据长度
*/
void dr_port_dispatch(dr_port_t port, const void *data, size_t len);
/* ========== 能力元数据查询 ========== */
int dr_send_cap_count(void);