Skip to content

Commit 1562d57

Browse files
committed
Define a rmw_time_t to rclcpp::Duration func
This commit introduces a utility header that defines a conversion function from `rmw_time_t` to `rclcpp::Duration`. This conversion function is necessary because `rmw_time_t` holds time values (both sec/nsec) as `uint64_t` fields, but `rclcpp::Duration` has a constructor that accepts sec as a `int32_t` value, raising the concern of a narrowing conversion. Similar in spirit to a function with a similar signature introduced in ros2/rclcpp#1467. Signed-off-by: Abrar Rahman Protyasha <[email protected]>
1 parent 699ab3a commit 1562d57

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

src/domain_bridge/domain_bridge.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "generic_publisher.hpp"
4646
#include "generic_subscription.hpp"
4747
#include "wait_for_graph_events.hpp"
48+
#include "utils.hpp"
4849

4950
namespace domain_bridge
5051
{
@@ -349,7 +350,9 @@ class DomainBridgeImpl
349350
qos.deadline(rclcpp::Duration(deadline_ns));
350351
}
351352
} else {
352-
qos.deadline(qos_match.qos.deadline());
353+
rmw_time_t rmw_deadline{qos_match.qos.get_rmw_qos_profile().deadline};
354+
rclcpp::Duration deadline{utils::from_rmw_time(rmw_deadline)};
355+
qos.deadline(deadline);
353356
}
354357
if (qos_options.lifespan()) {
355358
const auto lifespan_ns = qos_options.lifespan().value();
@@ -360,7 +363,9 @@ class DomainBridgeImpl
360363
qos.lifespan(rclcpp::Duration(lifespan_ns));
361364
}
362365
} else {
363-
qos.lifespan(qos_match.qos.lifespan());
366+
rmw_time_t rmw_lifespan{qos_match.qos.get_rmw_qos_profile().lifespan};
367+
rclcpp::Duration lifespan{utils::from_rmw_time(rmw_lifespan)};
368+
qos.lifespan(lifespan);
364369
}
365370

366371
qos.liveliness(qos_match.qos.get_rmw_qos_profile().liveliness);

src/domain_bridge/utils.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2021, Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef DOMAIN_BRIDGE__UTILS_HPP_
16+
#define DOMAIN_BRIDGE__UTILS_HPP_
17+
18+
#include "rcl/time.h"
19+
#include "rclcpp/duration.hpp"
20+
#include "rmw/types.h"
21+
22+
namespace domain_bridge
23+
{
24+
namespace utils
25+
{
26+
rclcpp::Duration from_rmw_time(rmw_time_t duration)
27+
{
28+
constexpr rcl_duration_value_t limit_ns = std::numeric_limits<rcl_duration_value_t>::max();
29+
constexpr rcl_duration_value_t limit_sec = RCL_NS_TO_S(limit_ns);
30+
if (duration.sec > limit_sec || duration.nsec > limit_ns) {
31+
return rclcpp::Duration{limit_ns};
32+
}
33+
uint64_t total_ns = RCL_S_TO_NS(duration.sec) + duration.nsec;
34+
if (total_ns > limit_ns) {
35+
return rclcpp::Duration{limit_ns};
36+
}
37+
return rclcpp::Duration{static_cast<rcl_duration_value_t>(total_ns)};
38+
}
39+
} // namespace utils
40+
} // namespace domain_bridge
41+
42+
#endif // DOMAIN_BRIDGE__UTILS_HPP_

src/domain_bridge/wait_for_graph_events.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030

3131
#include "rcl/node_options.h"
3232
#include "rclcpp/client.hpp"
33+
#include "rclcpp/duration.hpp"
3334
#include "rclcpp/node.hpp"
3435
#include "rclcpp/qos.hpp"
3536
#include "rmw/qos_profiles.h"
3637
#include "rmw/types.h"
3738

39+
#include "utils.hpp"
40+
3841
namespace domain_bridge
3942
{
4043

@@ -231,11 +234,13 @@ class WaitForGraphEvents
231234
if (profile.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL) {
232235
transient_local_count++;
233236
}
234-
if (profile.deadline() > max_deadline) {
235-
max_deadline = profile.deadline();
237+
rclcpp::Duration deadline{utils::from_rmw_time(profile.deadline)};
238+
if (deadline > max_deadline) {
239+
max_deadline = deadline;
236240
}
237-
if (profile.lifespan() > max_lifespan) {
238-
max_lifespan = profile.lifespan();
241+
rclcpp::Duration lifespan{utils::from_rmw_time(profile.lifespan)};
242+
if (lifespan > max_lifespan) {
243+
max_lifespan = lifespan;
239244
}
240245
}
241246

0 commit comments

Comments
 (0)