feat: 新增接收dispatch反向路由
This commit is contained in:
+101
-97
@@ -52,16 +52,20 @@ cleanup: \
|
||||
} while (0)
|
||||
|
||||
/* ================================================================
|
||||
* 测试用 handler
|
||||
* 测试用 handler / 回调
|
||||
* ================================================================ */
|
||||
|
||||
static int _h_cnt;
|
||||
static int _h_port[16];
|
||||
static int _h_pos;
|
||||
static void _h(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
|
||||
static void _h2(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
|
||||
static void _h3(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
|
||||
static void reset_h(void) { _h_cnt = 0; _h_pos = 0; }
|
||||
static void _h(uint32_t p, const void *d, size_t l) { (void)p; (void)d; (void)l; _h_cnt++; }
|
||||
static void reset_h(void) { _h_cnt = 0; }
|
||||
|
||||
static int _rx_rtcm;
|
||||
static int _rx_cmd;
|
||||
void dr_on_recv_rtcm(uint32_t p, const void *d, size_t l)
|
||||
{ (void)p; (void)d; (void)l; _rx_rtcm++; }
|
||||
void dr_on_recv_command(uint32_t p, const void *d, size_t l)
|
||||
{ (void)p; (void)d; (void)l; _rx_cmd++; }
|
||||
static void reset_rx(void) { _rx_rtcm = _rx_cmd = 0; }
|
||||
|
||||
/* ================================================================
|
||||
* 1. 未初始化
|
||||
@@ -97,14 +101,15 @@ TEST_END()
|
||||
TEST_START(port_default_state)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
CHECK(dr_port_get_send_type(p) == -1, NULL);
|
||||
CHECK(dr_port_get_recv_type(p) == -1, NULL);
|
||||
CHECK(dr_port_handler_count(p) == 0, NULL);
|
||||
TEST_END()
|
||||
|
||||
/* ================================================================
|
||||
* 3. send_type 与 handler
|
||||
* 3. send_type / recv_type 与 handler
|
||||
* ================================================================ */
|
||||
|
||||
TEST_START(send_type_set_get)
|
||||
TEST_START(send_type_set_clear)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p, DR_SEND_sensor);
|
||||
CHECK(dr_port_get_send_type(p) == DR_SEND_sensor, NULL);
|
||||
@@ -112,6 +117,22 @@ TEST_START(send_type_set_get)
|
||||
CHECK(dr_port_get_send_type(p) == -1, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(recv_type_set_clear)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_recv_type(p, DR_RECV_rtcm);
|
||||
CHECK(dr_port_get_recv_type(p) == DR_RECV_rtcm, NULL);
|
||||
dr_port_set_recv_type(p, -1);
|
||||
CHECK(dr_port_get_recv_type(p) == -1, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(types_independent)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p, DR_SEND_sensor);
|
||||
dr_port_set_recv_type(p, DR_RECV_command);
|
||||
CHECK(dr_port_get_send_type(p) == DR_SEND_sensor, NULL);
|
||||
CHECK(dr_port_get_recv_type(p) == DR_RECV_command, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(handler_add_remove)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
CHECK(dr_port_add_handler(p, _h) == 0, NULL);
|
||||
@@ -120,19 +141,11 @@ TEST_START(handler_add_remove)
|
||||
CHECK(dr_port_handler_count(p) == 0, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(handler_clear)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_add_handler(p, _h);
|
||||
dr_port_clear_handlers(p);
|
||||
CHECK(dr_port_handler_count(p) == 0, NULL);
|
||||
TEST_END()
|
||||
|
||||
/* ================================================================
|
||||
* 4. 发送 — 自动广播
|
||||
* ================================================================ */
|
||||
|
||||
TEST_START(send_no_match_port)
|
||||
/* 没有端口配置该 send_type */
|
||||
CHECK(dr_send_sensor("d", 1) == -2, "expected -2");
|
||||
TEST_END()
|
||||
|
||||
@@ -143,21 +156,9 @@ TEST_START(send_one_port_one_handler)
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 1, "cnt=%d", _h_cnt);
|
||||
CHECK(_h_port[0] == (int)p, "port=%d", _h_port[0]);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_one_port_multi_handler)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p, DR_SEND_sensor);
|
||||
dr_port_add_handler(p, _h);
|
||||
dr_port_add_handler(p, _h2);
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 2, "cnt=%d", _h_cnt);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_multi_ports_same_type)
|
||||
/* 两个端口同类型,各一个 handler */
|
||||
TEST_START(send_multi_ports)
|
||||
dr_port_t p1 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_t p2 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p1, DR_SEND_sensor);
|
||||
@@ -167,26 +168,9 @@ TEST_START(send_multi_ports_same_type)
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 2, "cnt=%d", _h_cnt);
|
||||
/* p1 和 p2 各触发一次 */
|
||||
CHECK((_h_port[0]==p1 && _h_port[1]==p2) ||
|
||||
(_h_port[0]==p2 && _h_port[1]==p1), "ports %d,%d", _h_port[0], _h_port[1]);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_multi_port_multi_handler)
|
||||
dr_port_t p1 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_t p2 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p1, DR_SEND_sensor);
|
||||
dr_port_set_send_type(p2, DR_SEND_sensor);
|
||||
dr_port_add_handler(p1, _h);
|
||||
dr_port_add_handler(p1, _h2); /* p1 两个 */
|
||||
dr_port_add_handler(p2, _h3); /* p2 一个 */
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 3, "cnt=%d", _h_cnt);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_type_filter)
|
||||
/* 两个端口不同类型,只触发匹配的 */
|
||||
dr_port_t p1 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_t p2 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p1, DR_SEND_sensor);
|
||||
@@ -196,19 +180,9 @@ TEST_START(send_type_filter)
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 1, "cnt=%d", _h_cnt);
|
||||
CHECK(_h_port[0] == (int)p1, "only p1 should fire");
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_after_remove_handler)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p, DR_SEND_sensor);
|
||||
dr_port_add_handler(p, _h);
|
||||
reset_h(); dr_send_sensor("d", 1); CHECK(_h_cnt == 1, NULL);
|
||||
dr_port_remove_handler(p, _h);
|
||||
CHECK(dr_send_sensor("d", 1) == -2, "expected -2 after remove");
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_after_delete_port)
|
||||
TEST_START(send_after_delete)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p, DR_SEND_sensor);
|
||||
dr_port_add_handler(p, _h);
|
||||
@@ -235,33 +209,65 @@ TEST_START(send_null_data)
|
||||
CHECK(_h_cnt == 1, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(send_multi_cap)
|
||||
/* 发送 status 不影响 sensor 端口 */
|
||||
dr_port_t p1 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_t p2 = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_send_type(p1, DR_SEND_sensor);
|
||||
dr_port_set_send_type(p2, DR_SEND_status);
|
||||
dr_port_add_handler(p1, _h);
|
||||
dr_port_add_handler(p2, _h);
|
||||
reset_h();
|
||||
CHECK(dr_send_sensor("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 1, "sensor: cnt=%d", _h_cnt);
|
||||
CHECK(dr_send_status("d", 1) == 0, NULL);
|
||||
CHECK(_h_cnt == 2, "status: cnt=%d", _h_cnt);
|
||||
TEST_END()
|
||||
|
||||
/* ================================================================
|
||||
* 5. 接收回调
|
||||
* 5. 接收回调 + dispatch
|
||||
* ================================================================ */
|
||||
|
||||
static int _rx_r;
|
||||
void dr_on_recv_rtcm(uint32_t p, const void *d, size_t l)
|
||||
{ (void)p; (void)d; (void)l; _rx_r++; }
|
||||
|
||||
TEST_START(recv_callback)
|
||||
_rx_r = 0;
|
||||
TEST_START(recv_callback_direct)
|
||||
/* 直接调用回调(旧方式) */
|
||||
reset_rx();
|
||||
dr_on_recv_rtcm(0, "d", 1);
|
||||
CHECK(_rx_r == 1, "cnt=%d", _rx_r);
|
||||
CHECK(_rx_rtcm == 1, "rtcm=%d", _rx_rtcm);
|
||||
dr_on_recv_command(1, "d", 1);
|
||||
CHECK(_rx_cmd == 1, "cmd=%d", _rx_cmd);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(dispatch_valid)
|
||||
/* dr_port_dispatch 自动路由到对应回调 */
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_recv_type(p, DR_RECV_rtcm);
|
||||
|
||||
reset_rx();
|
||||
dr_port_dispatch(p, "data", 4);
|
||||
CHECK(_rx_rtcm == 1, "rtcm should fire: %d", _rx_rtcm);
|
||||
CHECK(_rx_cmd == 0, "cmd should not fire: %d", _rx_cmd);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(dispatch_command)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_recv_type(p, DR_RECV_command);
|
||||
|
||||
reset_rx();
|
||||
dr_port_dispatch(p, "data", 4);
|
||||
CHECK(_rx_cmd == 1, "cmd should fire: %d", _rx_cmd);
|
||||
CHECK(_rx_rtcm == 0, "rtcm should not fire: %d", _rx_rtcm);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(dispatch_unset)
|
||||
/* recv_type 为 -1 时 dispatch 不应调用任何回调 */
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
reset_rx();
|
||||
dr_port_dispatch(p, "data", 4);
|
||||
CHECK(_rx_rtcm == 0, NULL);
|
||||
CHECK(_rx_cmd == 0, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(dispatch_invalid_port)
|
||||
reset_rx();
|
||||
dr_port_dispatch(999, "data", 4);
|
||||
dr_port_dispatch(-1, "data", 4);
|
||||
CHECK(_rx_rtcm == 0, NULL);
|
||||
CHECK(_rx_cmd == 0, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(dispatch_deleted_port)
|
||||
dr_port_t p = FIXTURE_CREATE(&_fix);
|
||||
dr_port_set_recv_type(p, DR_RECV_rtcm);
|
||||
dr_port_delete(p);
|
||||
reset_rx();
|
||||
dr_port_dispatch(p, "data", 4);
|
||||
CHECK(_rx_rtcm == 0, "deleted should not fire");
|
||||
_fix.count--;
|
||||
TEST_END()
|
||||
|
||||
/* ================================================================
|
||||
@@ -271,24 +277,20 @@ TEST_END()
|
||||
TEST_START(send_cap_meta)
|
||||
CHECK(dr_send_cap_count() == 2, NULL);
|
||||
CHECK(strcmp(dr_send_cap_name(0), "sensor") == 0, NULL);
|
||||
CHECK(strcmp(dr_send_cap_desc(0), "Sensor data report") == 0, NULL);
|
||||
CHECK(dr_send_cap_name(-1) == NULL, NULL);
|
||||
CHECK(dr_send_cap_name(99) == NULL, NULL);
|
||||
TEST_END()
|
||||
|
||||
TEST_START(recv_cap_meta)
|
||||
CHECK(dr_recv_cap_count() == 2, NULL);
|
||||
CHECK(strcmp(dr_recv_cap_name(0), "rtcm") == 0, NULL);
|
||||
CHECK(strcmp(dr_recv_cap_desc(0), "RTCM correction data") == 0, NULL);
|
||||
CHECK(dr_recv_cap_name(-1) == NULL, NULL);
|
||||
CHECK(dr_recv_cap_desc(99) == NULL, NULL);
|
||||
TEST_END()
|
||||
|
||||
/* ================================================================
|
||||
* 主函数
|
||||
* ================================================================ */
|
||||
|
||||
#define RUN(name) do { reset_h(); _rx_r = 0; test_##name(); } while (0)
|
||||
#define RUN(name) do { reset_h(); reset_rx(); test_##name(); } while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -307,28 +309,30 @@ int main(void)
|
||||
printf("\n");
|
||||
|
||||
dr_reset(); dr_init();
|
||||
printf("[send_type & handler]\n");
|
||||
RUN(send_type_set_get);
|
||||
printf("[send_type / recv_type / handler]\n");
|
||||
RUN(send_type_set_clear);
|
||||
RUN(recv_type_set_clear);
|
||||
RUN(types_independent);
|
||||
RUN(handler_add_remove);
|
||||
RUN(handler_clear);
|
||||
printf("\n");
|
||||
|
||||
printf("[send — auto broadcast]\n");
|
||||
RUN(send_no_match_port);
|
||||
RUN(send_one_port_one_handler);
|
||||
RUN(send_one_port_multi_handler);
|
||||
RUN(send_multi_ports_same_type);
|
||||
RUN(send_multi_port_multi_handler);
|
||||
RUN(send_multi_ports);
|
||||
RUN(send_type_filter);
|
||||
RUN(send_after_remove_handler);
|
||||
RUN(send_after_delete_port);
|
||||
RUN(send_after_delete);
|
||||
RUN(send_zero_length);
|
||||
RUN(send_null_data);
|
||||
RUN(send_multi_cap);
|
||||
printf("\n");
|
||||
|
||||
printf("[receive callback]\n");
|
||||
RUN(recv_callback);
|
||||
printf("[receive — dispatch]\n");
|
||||
RUN(recv_callback_direct);
|
||||
RUN(dispatch_valid);
|
||||
RUN(dispatch_command);
|
||||
RUN(dispatch_unset);
|
||||
RUN(dispatch_invalid_port);
|
||||
RUN(dispatch_deleted_port);
|
||||
printf("\n");
|
||||
|
||||
printf("[capability metadata]\n");
|
||||
|
||||
Reference in New Issue
Block a user