This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathqos.cpp
317 lines (283 loc) · 9.45 KB
/
qos.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2015-2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <limits>
#include "rmw_connext_shared_cpp/qos.hpp"
namespace
{
bool
is_time_default(const rmw_time_t & time)
{
return time.sec == 0 && time.nsec == 0;
}
DDS_Duration_t
rmw_time_to_dds(const rmw_time_t & time)
{
DDS_Duration_t duration;
duration.sec = static_cast<DDS_Long>(time.sec);
duration.nanosec = static_cast<DDS_UnsignedLong>(time.nsec);
return duration;
}
template<typename DDSEntityQos>
bool
set_entity_qos_from_profile_generic(
const rmw_qos_profile_t & qos_profile,
DDSEntityQos & entity_qos)
{
// Read properties from the rmw profile
switch (qos_profile.history) {
case RMW_QOS_POLICY_HISTORY_KEEP_LAST:
entity_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
break;
case RMW_QOS_POLICY_HISTORY_KEEP_ALL:
entity_qos.history.kind = DDS::KEEP_ALL_HISTORY_QOS;
break;
case RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT:
break;
default:
RMW_SET_ERROR_MSG("Unknown QoS history policy");
return false;
}
switch (qos_profile.reliability) {
case RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT:
entity_qos.reliability.kind = DDS::BEST_EFFORT_RELIABILITY_QOS;
break;
case RMW_QOS_POLICY_RELIABILITY_RELIABLE:
entity_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
break;
case RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT:
break;
default:
RMW_SET_ERROR_MSG("Unknown QoS reliability policy");
return false;
}
switch (qos_profile.durability) {
case RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL:
entity_qos.durability.kind = DDS::TRANSIENT_LOCAL_DURABILITY_QOS;
break;
case RMW_QOS_POLICY_DURABILITY_VOLATILE:
entity_qos.durability.kind = DDS::VOLATILE_DURABILITY_QOS;
break;
case RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT:
break;
default:
RMW_SET_ERROR_MSG("Unknown QoS durability policy");
return false;
}
if (qos_profile.depth != RMW_QOS_POLICY_DEPTH_SYSTEM_DEFAULT) {
entity_qos.history.depth = static_cast<DDS::Long>(qos_profile.depth);
}
// DDS_DeadlineQosPolicy has default value of DDS_DURATION_INFINITE
// don't overwrite if default passed
if (!is_time_default(qos_profile.deadline)) {
entity_qos.deadline.period = rmw_time_to_dds(qos_profile.deadline);
}
switch (qos_profile.liveliness) {
case RMW_QOS_POLICY_LIVELINESS_AUTOMATIC:
entity_qos.liveliness.kind = DDS::AUTOMATIC_LIVELINESS_QOS;
break;
case RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE:
entity_qos.liveliness.kind = DDS::MANUAL_BY_PARTICIPANT_LIVELINESS_QOS;
break;
case RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC:
entity_qos.liveliness.kind = DDS::MANUAL_BY_TOPIC_LIVELINESS_QOS;
break;
case RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT:
break;
default:
RMW_SET_ERROR_MSG("Unknown QoS liveliness policy");
return false;
}
if (!is_time_default(qos_profile.liveliness_lease_duration)) {
entity_qos.liveliness.lease_duration = rmw_time_to_dds(qos_profile.liveliness_lease_duration);
}
// ensure the history depth is at least the requested queue size
assert(entity_qos.history.depth >= 0);
if (
entity_qos.history.kind == DDS::KEEP_LAST_HISTORY_QOS &&
static_cast<size_t>(entity_qos.history.depth) < qos_profile.depth)
{
if (qos_profile.depth > (std::numeric_limits<DDS::Long>::max)()) {
RMW_SET_ERROR_MSG(
"failed to set history depth since the requested queue size exceeds the DDS type");
return false;
}
entity_qos.history.depth = static_cast<DDS::Long>(qos_profile.depth);
}
return true;
}
bool
set_entity_qos_from_profile(
const rmw_qos_profile_t & qos_profile,
DDS::DataReaderQos & entity_qos)
{
// Set any QoS settings that are specific to DataReader, then call the shared version
return set_entity_qos_from_profile_generic(qos_profile, entity_qos);
}
bool
set_entity_qos_from_profile(
const rmw_qos_profile_t & qos_profile,
DDS::DataWriterQos & entity_qos)
{
// Set any QoS settings that are specific to DataWriter, then call the shared version
if (!is_time_default(qos_profile.lifespan)) {
entity_qos.lifespan.duration = rmw_time_to_dds(qos_profile.lifespan);
}
return set_entity_qos_from_profile_generic(qos_profile, entity_qos);
}
} // anonymous namespace
bool
get_datareader_qos(
DDS::DomainParticipant * participant,
const rmw_qos_profile_t & qos_profile,
DDS::DataReaderQos & datareader_qos)
{
DDS::ReturnCode_t status = participant->get_default_datareader_qos(datareader_qos);
if (status != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default datareader qos");
return false;
}
status = DDS::PropertyQosPolicyHelper::add_property(
datareader_qos.property,
"dds.data_reader.history.memory_manager.fast_pool.pool_buffer_max_size",
"4096",
DDS::BOOLEAN_FALSE);
if (status != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to add qos property");
return false;
}
status = DDS::PropertyQosPolicyHelper::add_property(
datareader_qos.property,
"reader_resource_limits.dynamically_allocate_fragmented_samples",
"1",
DDS::BOOLEAN_FALSE);
if (status != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to add qos property");
return false;
}
if (!set_entity_qos_from_profile(qos_profile, datareader_qos)) {
return false;
}
return true;
}
bool
get_datawriter_qos(
DDS::DomainParticipant * participant,
const rmw_qos_profile_t & qos_profile,
DDS::DataWriterQos & datawriter_qos)
{
DDS::ReturnCode_t status = participant->get_default_datawriter_qos(datawriter_qos);
if (status != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default datawriter qos");
return false;
}
status = DDS::PropertyQosPolicyHelper::add_property(
datawriter_qos.property,
"dds.data_writer.history.memory_manager.fast_pool.pool_buffer_max_size",
"4096",
DDS::BOOLEAN_FALSE);
if (status != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to add qos property");
return false;
}
if (!set_entity_qos_from_profile(qos_profile, datawriter_qos)) {
return false;
}
// TODO(wjwwood): conditionally use the async publish mode using a heuristic:
// https://github.com/ros2/rmw_connext/issues/190
datawriter_qos.publish_mode.kind = DDS::ASYNCHRONOUS_PUBLISH_MODE_QOS;
return true;
}
void
dds_qos_lifespan_to_rmw_qos_lifespan(
const DDS::DataWriterQos & dds_qos,
rmw_qos_profile_t * qos)
{
qos->lifespan.sec = dds_qos.lifespan.duration.sec;
qos->lifespan.nsec = dds_qos.lifespan.duration.nanosec;
}
void
dds_qos_lifespan_to_rmw_qos_lifespan(
const DDS::DataReaderQos & /*dds_qos*/,
rmw_qos_profile_t * /*qos*/)
{
// lifespan does does not exist in DataReader, so no-op here
}
template<typename AttributeT>
void
dds_qos_to_rmw_qos(
const AttributeT & dds_qos,
rmw_qos_profile_t * qos)
{
switch (dds_qos.history.kind) {
case DDS_KEEP_LAST_HISTORY_QOS:
qos->history = RMW_QOS_POLICY_HISTORY_KEEP_LAST;
break;
case DDS_KEEP_ALL_HISTORY_QOS:
qos->history = RMW_QOS_POLICY_HISTORY_KEEP_ALL;
break;
default:
qos->history = RMW_QOS_POLICY_HISTORY_UNKNOWN;
break;
}
qos->depth = static_cast<size_t>(dds_qos.history.depth);
switch (dds_qos.reliability.kind) {
case DDS_BEST_EFFORT_RELIABILITY_QOS:
qos->reliability = RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT;
break;
case DDS_RELIABLE_RELIABILITY_QOS:
qos->reliability = RMW_QOS_POLICY_RELIABILITY_RELIABLE;
break;
default:
qos->reliability = RMW_QOS_POLICY_RELIABILITY_UNKNOWN;
break;
}
switch (dds_qos.durability.kind) {
case DDS_TRANSIENT_LOCAL_DURABILITY_QOS:
qos->durability = RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL;
break;
case DDS_VOLATILE_DURABILITY_QOS:
qos->durability = RMW_QOS_POLICY_DURABILITY_VOLATILE;
break;
default:
qos->durability = RMW_QOS_POLICY_DURABILITY_UNKNOWN;
break;
}
qos->deadline.sec = dds_qos.deadline.period.sec;
qos->deadline.nsec = dds_qos.deadline.period.nanosec;
dds_qos_lifespan_to_rmw_qos_lifespan(dds_qos, qos);
switch (dds_qos.liveliness.kind) {
case DDS_AUTOMATIC_LIVELINESS_QOS:
qos->liveliness = RMW_QOS_POLICY_LIVELINESS_AUTOMATIC;
break;
case DDS_MANUAL_BY_PARTICIPANT_LIVELINESS_QOS:
qos->liveliness = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE;
break;
case DDS_MANUAL_BY_TOPIC_LIVELINESS_QOS:
qos->liveliness = RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC;
break;
default:
qos->liveliness = RMW_QOS_POLICY_LIVELINESS_UNKNOWN;
break;
}
qos->liveliness_lease_duration.sec = dds_qos.liveliness.lease_duration.sec;
qos->liveliness_lease_duration.nsec = dds_qos.liveliness.lease_duration.nanosec;
}
template
void dds_qos_to_rmw_qos<DDS::DataWriterQos>(
const DDS::DataWriterQos & dds_qos,
rmw_qos_profile_t * qos);
template
void dds_qos_to_rmw_qos<DDS::DataReaderQos>(
const DDS::DataReaderQos & dds_qos,
rmw_qos_profile_t * qos);