Skip to content

Commit 13949d8

Browse files
committed
Support domain-scoped topics via TopicConfig; replace topic_name with topic_configs.
1 parent 2c69579 commit 13949d8

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

SharedTopic.hpp

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ module_description: SharedTopic 是一个基于 UART 的多 Topic 数据共享
77
- uart_name: "usart1"
88
- task_stack_depth: 2048
99
- buffer_size: 256
10-
- topic_name:
10+
- topic_configs:
1111
- "topic1"
12-
- "topic2"
12+
- ["topic2", "libxr_def_domain"]
1313
template_args: []
1414
required_hardware: uart_name, ramfs
1515
depends: []
@@ -28,44 +28,60 @@ depends: []
2828
#include "thread.hpp"
2929
#include "uart.hpp"
3030

31-
class SharedTopic : public LibXR::Application {
31+
class SharedTopic : public LibXR::Application
32+
{
3233
public:
34+
struct TopicConfig
35+
{
36+
const char *name;
37+
const char *domain = "libxr_def_domain";
38+
39+
TopicConfig(const char *name) : name(name) {}
40+
41+
TopicConfig(const char *name, const char *domain) : name(name), domain(domain) {}
42+
};
43+
3344
SharedTopic(LibXR::HardwareContainer &hw, LibXR::ApplicationManager &app,
34-
const char *uart_name, uint32_t task_stack_depth,
35-
uint32_t buffer_size,
36-
std::initializer_list<const char *> topic_names)
45+
const char *uart_name, uint32_t task_stack_depth, uint32_t buffer_size,
46+
std::initializer_list<TopicConfig> topic_configs)
3747
: uart_(hw.template Find<LibXR::UART>(uart_name)),
3848
server_(buffer_size),
3949
rx_buffer_(new uint8_t[buffer_size], buffer_size),
40-
cmd_file_(LibXR::RamFS::CreateFile("shared_topic", CommandFunc, this)) {
41-
for (const char *name : topic_names) {
42-
auto topic = LibXR::Topic::Find(name);
43-
if (topic == nullptr) {
44-
XR_LOG_ERROR("Topic not found: %s", name);
50+
cmd_file_(LibXR::RamFS::CreateFile("shared_topic", CommandFunc, this))
51+
{
52+
for (auto config : topic_configs)
53+
{
54+
auto domain = LibXR::Topic::Domain(config.domain);
55+
auto topic = LibXR::Topic::Find(config.name, &domain);
56+
if (topic == nullptr)
57+
{
58+
XR_LOG_ERROR("Topic not found: %s/%s", config.domain, config.name);
4559
ASSERT(false);
4660
}
4761
server_.Register(topic);
4862
}
4963

5064
hw.template FindOrExit<LibXR::RamFS>({"ramfs"})->Add(cmd_file_);
5165

52-
rx_thread_.Create(this, RxThreadFun, "SharedTopic::RxThread",
53-
task_stack_depth, LibXR::Thread::Priority::REALTIME);
66+
rx_thread_.Create(this, RxThreadFun, "SharedTopic::RxThread", task_stack_depth,
67+
LibXR::Thread::Priority::REALTIME);
5468

5569
app.Register(*this);
5670
}
5771

58-
static void RxThreadFun(SharedTopic *self) {
72+
static void RxThreadFun(SharedTopic *self)
73+
{
5974
LibXR::Semaphore sem;
6075
LibXR::ReadOperation op(sem);
61-
while (true) {
62-
auto size = LibXR::max(
63-
sizeof(LibXR::Topic::PackedDataHeader),
64-
LibXR::min(self->uart_->read_port_->Size(), self->rx_buffer_.size_));
65-
auto ans = self->uart_->Read(
66-
LibXR::RawData{self->rx_buffer_.addr_, size}, op);
67-
68-
if (ans == ErrorCode::OK) {
76+
while (true)
77+
{
78+
auto size =
79+
LibXR::max(sizeof(LibXR::Topic::PackedDataHeader),
80+
LibXR::min(self->uart_->read_port_->Size(), self->rx_buffer_.size_));
81+
auto ans = self->uart_->Read(LibXR::RawData{self->rx_buffer_.addr_, size}, op);
82+
83+
if (ans == ErrorCode::OK)
84+
{
6985
self->server_.ParseData(LibXR::RawData{self->rx_buffer_.addr_, size});
7086
}
7187

@@ -75,27 +91,34 @@ class SharedTopic : public LibXR::Application {
7591

7692
void OnMonitor() override {}
7793

78-
static int CommandFunc(SharedTopic *self, int argc, char **argv) {
79-
if (argc == 1) {
94+
static int CommandFunc(SharedTopic *self, int argc, char **argv)
95+
{
96+
if (argc == 1)
97+
{
8098
LibXR::STDIO::Printf("Usage:\r\n");
81-
LibXR::STDIO::Printf(
82-
" monitor [time_ms] [interval_ms] - test received speed\r\n");
99+
LibXR::STDIO::Printf(" monitor [time_ms] [interval_ms] - test received speed\r\n");
83100
return 0;
84-
} else if (argc == 4) {
85-
if (strcmp(argv[1], "monitor") == 0) {
101+
}
102+
else if (argc == 4)
103+
{
104+
if (strcmp(argv[1], "monitor") == 0)
105+
{
86106
int time = atoi(argv[2]);
87107
int delay = atoi(argv[3]);
88108
auto start = self->rx_count_;
89-
while (time > 0) {
109+
while (time > 0)
110+
{
90111
LibXR::Thread::Sleep(delay);
91112
LibXR::STDIO::Printf(
92-
"%f Mbps\r\n", static_cast<float>(self->rx_count_ - start) * 8.0 /
93-
1024.0 / 1024.0 / delay * 1000.0);
113+
"%f Mbps\r\n", static_cast<float>(self->rx_count_ - start) * 8.0 / 1024.0 /
114+
1024.0 / delay * 1000.0);
94115
time -= delay;
95116
start = self->rx_count_;
96117
}
97118
}
98-
} else {
119+
}
120+
else
121+
{
99122
LibXR::STDIO::Printf("Error: Invalid arguments.\r\n");
100123
return -1;
101124
}

0 commit comments

Comments
 (0)