Files
DataRouter/example.c
7iwind 1c3e48d59f
CI / host (pull_request) Successful in 2s
CI / cortex-m (pull_request) Successful in 5s
feat: 新增接收dispatch反向路由
2026-07-30 10:49:44 +08:00

77 lines
2.1 KiB
C

/**
* @file example.c
*
* 展示:
* 1. send_type + 多 handler 发送广播
* 2. recv_type + dr_port_dispatch 自动路由接收
* 3. 外设中断中只需一行 dr_port_dispatch()
*/
#include "datarouter.h"
#include <stdio.h>
/* ---- 发送 handler ---- */
static void uart_write(uint32_t port_id, const void *data, size_t len)
{
printf("[UART port %u] %.*s\n", port_id, (int)len, (const char *)data);
}
static void tcp_write(uint32_t port_id, const void *data, size_t len)
{
printf("[TCP port %u] %.*s\n", port_id, (int)len, (const char *)data);
}
/* ---- 接收回调 ---- */
void dr_on_recv_rtcm(uint32_t port_id, const void *data, size_t len)
{
printf("[RX:rtcm port %u] %.*s\n", port_id, (int)len, (const char *)data);
}
void dr_on_recv_command(uint32_t port_id, const void *data, size_t len)
{
printf("[RX:cmd port %u] %.*s\n", port_id, (int)len, (const char *)data);
}
/* ---- main ---- */
int main(void)
{
dr_init();
/*
* 创建端口,配置发送和接收能力。
* 一个端口可以同时有 send_type 和 recv_type。
*/
dr_port_t uart1 = dr_port_create();
dr_port_set_send_type(uart1, DR_SEND_sensor);
dr_port_set_recv_type(uart1, DR_RECV_rtcm);
dr_port_add_handler(uart1, uart_write);
dr_port_add_handler(uart1, tcp_write);
dr_port_t uart2 = dr_port_create();
dr_port_set_send_type(uart2, DR_SEND_status);
dr_port_set_recv_type(uart2, DR_RECV_command);
dr_port_add_handler(uart2, uart_write);
/* ---- 发送 ---- */
printf("--- send sensor (auto-route to uart1) ---\n");
dr_send_sensor("temperature:25.3", 16);
printf("\n--- send status (auto-route to uart2) ---\n");
dr_send_status("status:OK", 9);
/* ---- 接收(模拟外设中断) ---- */
printf("\n--- UART1 received data → dispatch to rtcm ---\n");
dr_port_dispatch(uart1, "rtcm_data_v3.2", 14);
printf("\n--- UART2 received data → dispatch to command ---\n");
dr_port_dispatch(uart2, "reboot", 6);
/* 清理 */
dr_port_delete(uart1);
dr_port_delete(uart2);
return 0;
}