Skip to content

Commit

Permalink
Define a rmw_time_t to rclcpp::Duration func
Browse files Browse the repository at this point in the history
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 <aprotyas@u.rochester.edu>
  • Loading branch information
aprotyas committed Oct 29, 2021
1 parent 699ab3a commit 1562d57
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/domain_bridge/domain_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "generic_publisher.hpp"
#include "generic_subscription.hpp"
#include "wait_for_graph_events.hpp"
#include "utils.hpp"

namespace domain_bridge
{
Expand Down Expand Up @@ -349,7 +350,9 @@ class DomainBridgeImpl
qos.deadline(rclcpp::Duration(deadline_ns));
}
} else {
qos.deadline(qos_match.qos.deadline());
rmw_time_t rmw_deadline{qos_match.qos.get_rmw_qos_profile().deadline};
rclcpp::Duration deadline{utils::from_rmw_time(rmw_deadline)};
qos.deadline(deadline);
}
if (qos_options.lifespan()) {
const auto lifespan_ns = qos_options.lifespan().value();
Expand All @@ -360,7 +363,9 @@ class DomainBridgeImpl
qos.lifespan(rclcpp::Duration(lifespan_ns));
}
} else {
qos.lifespan(qos_match.qos.lifespan());
rmw_time_t rmw_lifespan{qos_match.qos.get_rmw_qos_profile().lifespan};
rclcpp::Duration lifespan{utils::from_rmw_time(rmw_lifespan)};
qos.lifespan(lifespan);
}

qos.liveliness(qos_match.qos.get_rmw_qos_profile().liveliness);
Expand Down
42 changes: 42 additions & 0 deletions src/domain_bridge/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2021, 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.

#ifndef DOMAIN_BRIDGE__UTILS_HPP_
#define DOMAIN_BRIDGE__UTILS_HPP_

#include "rcl/time.h"
#include "rclcpp/duration.hpp"
#include "rmw/types.h"

namespace domain_bridge
{
namespace utils
{
rclcpp::Duration from_rmw_time(rmw_time_t duration)
{
constexpr rcl_duration_value_t limit_ns = std::numeric_limits<rcl_duration_value_t>::max();
constexpr rcl_duration_value_t limit_sec = RCL_NS_TO_S(limit_ns);
if (duration.sec > limit_sec || duration.nsec > limit_ns) {
return rclcpp::Duration{limit_ns};
}
uint64_t total_ns = RCL_S_TO_NS(duration.sec) + duration.nsec;
if (total_ns > limit_ns) {
return rclcpp::Duration{limit_ns};
}
return rclcpp::Duration{static_cast<rcl_duration_value_t>(total_ns)};
}
} // namespace utils
} // namespace domain_bridge

#endif // DOMAIN_BRIDGE__UTILS_HPP_
13 changes: 9 additions & 4 deletions src/domain_bridge/wait_for_graph_events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@

#include "rcl/node_options.h"
#include "rclcpp/client.hpp"
#include "rclcpp/duration.hpp"
#include "rclcpp/node.hpp"
#include "rclcpp/qos.hpp"
#include "rmw/qos_profiles.h"
#include "rmw/types.h"

#include "utils.hpp"

namespace domain_bridge
{

Expand Down Expand Up @@ -231,11 +234,13 @@ class WaitForGraphEvents
if (profile.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL) {
transient_local_count++;
}
if (profile.deadline() > max_deadline) {
max_deadline = profile.deadline();
rclcpp::Duration deadline{utils::from_rmw_time(profile.deadline)};
if (deadline > max_deadline) {
max_deadline = deadline;
}
if (profile.lifespan() > max_lifespan) {
max_lifespan = profile.lifespan();
rclcpp::Duration lifespan{utils::from_rmw_time(profile.lifespan)};
if (lifespan > max_lifespan) {
max_lifespan = lifespan;
}
}

Expand Down

0 comments on commit 1562d57

Please sign in to comment.