ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
event_handler.cpp
1 // Copyright 2019 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 #include <stdexcept>
16 #include <string>
17 
18 #include "rcl/error_handling.h"
19 #include "rcl/event.h"
20 
21 #include "rclcpp/event_handler.hpp"
22 #include "rclcpp/exceptions/exceptions.hpp"
23 
24 namespace rclcpp
25 {
26 
27 UnsupportedEventTypeException::UnsupportedEventTypeException(
28  rcl_ret_t ret,
29  const rcl_error_state_t * error_state,
30  const std::string & prefix)
31 : UnsupportedEventTypeException(exceptions::RCLErrorBase(ret, error_state), prefix)
32 {}
33 
34 UnsupportedEventTypeException::UnsupportedEventTypeException(
35  const exceptions::RCLErrorBase & base_exc,
36  const std::string & prefix)
37 : exceptions::RCLErrorBase(base_exc),
38  std::runtime_error(prefix + (prefix.empty() ? "" : ": ") + base_exc.formatted_message)
39 {}
40 
41 EventHandlerBase::~EventHandlerBase()
42 {
43  if (rcl_event_fini(&event_handle_) != RCL_RET_OK) {
44  RCUTILS_LOG_ERROR_NAMED(
45  "rclcpp",
46  "Error in destruction of rcl event handle: %s", rcl_get_error_string().str);
47  rcl_reset_error();
48  }
49 }
50 
52 size_t
53 EventHandlerBase::get_number_of_ready_events()
54 {
55  return 1;
56 }
57 
59 void
60 EventHandlerBase::add_to_wait_set(rcl_wait_set_t & wait_set)
61 {
62  rcl_ret_t ret = rcl_wait_set_add_event(&wait_set, &event_handle_, &wait_set_event_index_);
63  if (RCL_RET_OK != ret) {
64  exceptions::throw_from_rcl_error(ret, "Couldn't add event to wait set");
65  }
66 }
67 
69 bool
70 EventHandlerBase::is_ready(const rcl_wait_set_t & wait_set)
71 {
72  return wait_set.events[wait_set_event_index_] == &event_handle_;
73 }
74 
75 void
76 EventHandlerBase::set_on_new_event_callback(
77  rcl_event_callback_t callback,
78  const void * user_data)
79 {
81  &event_handle_,
82  callback,
83  user_data);
84 
85  if (RCL_RET_OK != ret) {
86  using rclcpp::exceptions::throw_from_rcl_error;
87  throw_from_rcl_error(ret, "failed to set the on new message callback for Event");
88  }
89 }
90 
91 } // namespace rclcpp
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_set_callback(const rcl_event_t *event, rcl_event_callback_t callback, const void *user_data)
Set the callback function for the event.
Definition: event.c:235
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
Definition: event.c:189
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Container for subscription's, guard condition's, etc to be waited on.
Definition: wait.h:42
const rcl_event_t ** events
Storage for event pointers.
Definition: wait.h:64
#define RCL_RET_OK
Success return code.
Definition: types.h:27
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_event(rcl_wait_set_t *wait_set, const rcl_event_t *event, size_t *index)
Store a pointer to the event in the next empty spot in the set.
Definition: wait.c:497