@@ -7,9 +7,9 @@ module_description: SharedTopicClient 是一个多 Topic 数据共享与串口
77 - uart_name: "uart_cdc"
88 - task_stack_depth: 2048
99 - buffer_size: 256
10- - topic_names :
10+ - topic_configs :
1111 - "topic1"
12- - "topic2"
12+ - [ "topic2", "libxr_def_domain"]
1313template_args: []
1414required_hardware: uart_name
1515depends: []
@@ -19,52 +19,67 @@ depends: []
1919#include " app_framework.hpp"
2020#include " uart.hpp"
2121
22- class SharedTopicClient : public LibXR ::Application {
22+ class SharedTopicClient : public LibXR ::Application
23+ {
2324 public:
24- typedef struct {
25+ typedef struct
26+ {
2527 SharedTopicClient *client;
2628 uint32_t topic_crc32;
2729 uint32_t index;
2830 } CallbackInfo;
2931
30- SharedTopicClient (LibXR::HardwareContainer &hw,
31- LibXR::ApplicationManager &app, const char *uart_name,
32- uint32_t task_stack_depth, uint32_t buffer_size,
33- std::initializer_list<const char *> topic_names)
32+ struct TopicConfig
33+ {
34+ const char *name;
35+ const char *domain = " libxr_def_domain" ;
36+
37+ TopicConfig (const char *name) : name(name) {}
38+
39+ TopicConfig (const char *name, const char *domain) : name(name), domain(domain) {}
40+ };
41+
42+ SharedTopicClient (LibXR::HardwareContainer &hw, LibXR::ApplicationManager &app,
43+ const char *uart_name, uint32_t task_stack_depth,
44+ uint32_t buffer_size,
45+ std::initializer_list<TopicConfig> topic_configs)
3446 : uart_(hw.template Find<LibXR::UART>(uart_name)),
3547 tx_buffer_ (new uint8_t [buffer_size], buffer_size),
36- tx_queue_(buffer_size) {
48+ tx_queue_(buffer_size)
49+ {
3750 ASSERT (uart_ != nullptr );
3851
39- topics_pack_buffer_ = new LibXR::RawData[topic_names .size ()];
52+ topics_pack_buffer_ = new LibXR::RawData[topic_configs .size ()];
4053
4154 uint32_t i = 0 ;
4255
43- for (auto name : topic_names) {
44- auto ans = LibXR::Topic::Find (name);
45- if (ans == nullptr ) {
46- XR_LOG_ERROR (" Topic not found: %s" , name);
56+ for (auto config : topic_configs)
57+ {
58+ auto domain = LibXR::Topic::Domain (config.domain );
59+ auto ans = LibXR::Topic::Find (config.name , &domain);
60+ if (ans == nullptr )
61+ {
62+ XR_LOG_ERROR (" Topic not found: %s/%s" , config.domain , config.name );
4763 ASSERT (false );
4864 }
4965 topics_pack_buffer_[i] = LibXR::RawData (
5066 new uint8_t [ans->data_ .max_length + LibXR::Topic::PACK_BASE_SIZE],
5167 ans->data_ .max_length + LibXR::Topic::PACK_BASE_SIZE);
5268
5369 void (*func)(bool , CallbackInfo, LibXR::RawData &) =
54- [](bool in_isr, CallbackInfo info, LibXR::RawData &data) {
55- LibXR::WriteOperation op;
56- LibXR::Topic::PackData (info.topic_crc32 ,
57- info.client ->topics_pack_buffer_ [info.index ],
58- data);
59- info.client ->tx_queue_ .PushBatch (
60- static_cast <uint8_t *>(
61- info.client ->topics_pack_buffer_ [info.index ].addr_ ),
62- info.client ->topics_pack_buffer_ [info.index ].size_ );
63- info.client ->tx_sem_ .PostFromCallback (in_isr);
64- };
65-
66- auto msg_cb = LibXR::Topic::Callback::Create (
67- func, CallbackInfo{this , ans->data_ .crc32 , i});
70+ [](bool in_isr, CallbackInfo info, LibXR::RawData &data)
71+ {
72+ LibXR::WriteOperation op;
73+ LibXR::Topic::PackData (info.topic_crc32 ,
74+ info.client ->topics_pack_buffer_ [info.index ], data);
75+ info.client ->tx_queue_ .PushBatch (
76+ static_cast <uint8_t *>(info.client ->topics_pack_buffer_ [info.index ].addr_ ),
77+ info.client ->topics_pack_buffer_ [info.index ].size_ );
78+ info.client ->tx_sem_ .PostFromCallback (in_isr);
79+ };
80+
81+ auto msg_cb =
82+ LibXR::Topic::Callback::Create (func, CallbackInfo{this , ans->data_ .crc32 , i});
6883
6984 LibXR::Topic topic (ans);
7085
@@ -73,25 +88,27 @@ class SharedTopicClient : public LibXR::Application {
7388 i++;
7489 }
7590
76- tx_thread_.Create (this , TxThreadFun, " SharedTopicClientTxThread" ,
77- task_stack_depth, LibXR::Thread::Priority::REALTIME);
91+ tx_thread_.Create (this , TxThreadFun, " SharedTopicClientTxThread" , task_stack_depth,
92+ LibXR::Thread::Priority::REALTIME);
7893
7994 app.Register (*this );
8095 }
8196
82- static void TxThreadFun (SharedTopicClient *client) {
97+ static void TxThreadFun (SharedTopicClient *client)
98+ {
8399 LibXR::Semaphore write_op_sem;
84100 LibXR::WriteOperation op (write_op_sem);
85101 LibXR::WriteOperation op_none;
86- while (true ) {
102+ while (true )
103+ {
87104 client->tx_sem_ .Wait ();
88- auto size =
89- LibXR::min (client-> tx_queue_ . Size (), client-> tx_buffer_ . size_ );
90- if (size > 0 && client->tx_queue_ .PopBatch (
91- static_cast < uint8_t *>(client-> tx_buffer_ . addr_ ),
92- size) == ErrorCode::OK) {
93- client->uart_ ->Write (
94- { static_cast < uint8_t *>(client-> tx_buffer_ . addr_ ), size}, op_none);
105+ auto size = LibXR::min (client-> tx_queue_ . Size (), client-> tx_buffer_ . size_ );
106+ if (size > 0 &&
107+ client->tx_queue_ .PopBatch (static_cast < uint8_t *>(client-> tx_buffer_ . addr_ ),
108+ size) == ErrorCode::OK)
109+ {
110+ client->uart_ ->Write ({ static_cast < uint8_t *>(client-> tx_buffer_ . addr_ ), size},
111+ op_none);
95112 }
96113 }
97114 }
0 commit comments