-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathros2_parser.cpp
More file actions
174 lines (155 loc) · 5.71 KB
/
ros2_parser.cpp
File metadata and controls
174 lines (155 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "ros2_parser.h"
#include <set>
#include <rosidl_typesupport_introspection_c/message_introspection.h>
#include <rosidl_typesupport_introspection_cpp/message_introspection.hpp>
#include <rosidl_typesupport_introspection_cpp/field_types.hpp>
#include <rosidl_typesupport_cpp/identifier.hpp>
#include <rosidl_typesupport_introspection_cpp/identifier.hpp>
#include <fmt/core.h>
#include "typesupport_wrapper.h"
bool TypeHasHeader(const rosidl_message_type_support_t* type_support)
{
auto members = static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers*>(type_support->data);
if (members->member_count_ >= 1 && members->members_)
{
const rosidl_typesupport_introspection_cpp::MessageMember& first_field = members->members_[0];
if (first_field.members_ == nullptr)
{
return false;
}
const auto* header_members =
static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers*>(first_field.members_->data);
if (strcmp(header_members->message_name_, "Header") == 0 && strcmp(header_members->message_namespace_, "std_msgs::"
"msg") == 0)
{
return true;
}
}
return false;
}
std::string CreateSchema(const std::string& base_type)
{
std::string schema;
using namespace rosidl_typesupport_introspection_cpp;
std::set<std::string> secondary_types_pending;
std::set<std::string> secondary_types_done;
auto addTypeToSchema = [&](const std::string& type_name, bool add_header) {
auto introspection_library = wrapper::get_typesupport_library(type_name, "rosidl_typesupport_introspection_cpp");
auto introspection_support = wrapper::get_message_typesupport_handle(
type_name, "rosidl_typesupport_introspection_cpp", introspection_library);
if (add_header)
{
schema += fmt::format("=====================================\nMSG: {}\n", type_name);
}
const auto* members = static_cast<const MessageMembers*>(introspection_support->data);
for (size_t i = 0; i < members->member_count_; i++)
{
const MessageMember& member = members->members_[i];
switch (member.type_id_)
{
case ROS_TYPE_FLOAT32:
schema += "float32";
break;
case ROS_TYPE_FLOAT64:
schema += "float64";
break;
case ROS_TYPE_UINT8:
case ROS_TYPE_BYTE:
case ROS_TYPE_CHAR:
schema += "uint8";
break;
case ROS_TYPE_BOOLEAN:
schema += "bool";
break;
case ROS_TYPE_INT8:
schema += "int8";
break;
case ROS_TYPE_UINT16:
schema += "uint16";
break;
case ROS_TYPE_INT16:
schema += "int16";
break;
case ROS_TYPE_UINT32:
schema += "uint32";
break;
case ROS_TYPE_INT32:
schema += "int32";
break;
case ROS_TYPE_UINT64:
schema += "uint64";
break;
case ROS_TYPE_INT64:
schema += "int64";
break;
case ROS_TYPE_STRING:
schema += "string";
break;
case ROS_TYPE_WSTRING:
schema += "string";
break;
case ROS_TYPE_MESSAGE: {
auto type_info = reinterpret_cast<const MessageMembers*>(member.members_->data);
std::string package = type_info->message_namespace_;
for (const std::string& suffix : { "::msg", "::srv", "::action" })
{
if (package.size() >= suffix.size() &&
package.compare(package.size() - suffix.size(), suffix.size(), suffix) == 0)
{
package.resize(package.size() - suffix.size());
break;
}
}
const std::string field_type = fmt::format("{}/{}", package, type_info->message_name_);
schema += field_type;
if (secondary_types_done.count(field_type) == 0)
{
secondary_types_pending.insert(field_type);
}
}
break;
}
if (member.is_array_)
{
if (member.array_size_ > 0)
{
schema += fmt::format("[{}]", member.array_size_);
}
else
{
schema += "[]";
}
}
schema += fmt::format(" {}\n", member.name_);
}
};
addTypeToSchema(base_type, false);
while (!secondary_types_pending.empty())
{
auto it = secondary_types_pending.begin();
std::string other_type = *it;
secondary_types_done.insert(other_type);
addTypeToSchema(other_type, true);
secondary_types_pending.erase(it);
}
return schema;
}
TopicInfo CreateTopicInfo(const std::string& topic_name, const std::string& type_name)
{
TopicInfo info;
info.topic_name = topic_name;
info.type = type_name;
info.introspection_library = wrapper::get_typesupport_library(type_name, "rosidl_typesupport_introspection_cpp");
info.introspection_support = wrapper::get_message_typesupport_handle(
type_name, "rosidl_typesupport_introspection_cpp", info.introspection_library);
auto identifier = rosidl_typesupport_cpp::typesupport_identifier;
info.support_library = wrapper::get_typesupport_library(type_name, identifier);
info.type_support = wrapper::get_message_typesupport_handle(type_name, identifier, info.support_library);
info.has_header_stamp = TypeHasHeader(info.introspection_support);
return info;
}
std::shared_ptr<PJ::MessageParser> CreateParserROS2(const PJ::ParserFactories& factories, const std::string& topic_name,
const std::string& type_name, PJ::PlotDataMapRef& data)
{
return factories.at("ros2msg")->createParser(topic_name, type_name, CreateSchema(type_name), data);
}