ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
guard_condition.c
1 // Copyright 2015 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 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 #include "rcl/guard_condition.h"
21 
22 #include "rcl/error_handling.h"
23 #include "rcl/rcl.h"
24 #include "rmw/error_handling.h"
25 #include "rmw/rmw.h"
26 
27 #include "./context_impl.h"
28 
30 {
31  rmw_guard_condition_t * rmw_handle;
32  bool allocated_rmw_guard_condition;
34 };
35 
38 {
39  static rcl_guard_condition_t null_guard_condition = {
40  .context = 0,
41  .impl = 0
42  };
43  return null_guard_condition;
44 }
45 
47 __rcl_guard_condition_init_from_rmw_impl(
48  rcl_guard_condition_t * guard_condition,
49  const rmw_guard_condition_t * rmw_guard_condition,
50  rcl_context_t * context,
51  const rcl_guard_condition_options_t options)
52 {
53  // This function will create an rmw_guard_condition if the parameter is null.
54 
55  // Perform argument validation.
56  const rcl_allocator_t * allocator = &options.allocator;
57  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
58  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, RCL_RET_INVALID_ARGUMENT);
59  // Ensure the guard_condition handle is zero initialized.
60  if (guard_condition->impl) {
61  RCL_SET_ERROR_MSG("guard_condition already initialized, or memory was unintialized");
62  return RCL_RET_ALREADY_INIT;
63  }
64  // Make sure rcl has been initialized.
65  RCL_CHECK_ARGUMENT_FOR_NULL(context, RCL_RET_INVALID_ARGUMENT);
66  if (!rcl_context_is_valid(context)) {
67  RCL_SET_ERROR_MSG(
68  "the given context is not valid, "
69  "either rcl_init() was not called or rcl_shutdown() was called.");
70  return RCL_RET_NOT_INIT;
71  }
72  // Allocate space for the guard condition impl.
73  guard_condition->impl = (rcl_guard_condition_impl_t *)allocator->allocate(
74  sizeof(rcl_guard_condition_impl_t), allocator->state);
75  if (!guard_condition->impl) {
76  RCL_SET_ERROR_MSG("allocating memory failed");
77  return RCL_RET_BAD_ALLOC;
78  }
79  // Create the rmw guard condition.
80  if (rmw_guard_condition) {
81  // If given, just assign (cast away const).
82  guard_condition->impl->rmw_handle = (rmw_guard_condition_t *)rmw_guard_condition;
83  guard_condition->impl->allocated_rmw_guard_condition = false;
84  } else {
85  // Otherwise create one.
86  guard_condition->impl->rmw_handle = rmw_create_guard_condition(&(context->impl->rmw_context));
87  if (!guard_condition->impl->rmw_handle) {
88  // Deallocate impl and exit.
89  allocator->deallocate(guard_condition->impl, allocator->state);
90  guard_condition->impl = NULL;
91  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
92  return RCL_RET_ERROR;
93  }
94  guard_condition->impl->allocated_rmw_guard_condition = true;
95  }
96  // Copy options into impl.
97  guard_condition->impl->options = options;
98  return RCL_RET_OK;
99 }
100 
101 rcl_ret_t
103  rcl_guard_condition_t * guard_condition,
104  rcl_context_t * context,
105  const rcl_guard_condition_options_t options)
106 {
107  // NULL indicates "create a new rmw guard condition".
108  return __rcl_guard_condition_init_from_rmw_impl(guard_condition, NULL, context, options);
109 }
110 
111 rcl_ret_t
113  rcl_guard_condition_t * guard_condition,
114  const rmw_guard_condition_t * rmw_guard_condition,
115  rcl_context_t * context,
116  const rcl_guard_condition_options_t options)
117 {
118  return __rcl_guard_condition_init_from_rmw_impl(
119  guard_condition, rmw_guard_condition, context, options);
120 }
121 
122 rcl_ret_t
124 {
125  // Perform argument validation.
126  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, RCL_RET_INVALID_ARGUMENT);
127  rcl_ret_t result = RCL_RET_OK;
128  if (guard_condition->impl) {
129  // assuming the allocator is valid because it is checked in rcl_guard_condition_init()
130  rcl_allocator_t allocator = guard_condition->impl->options.allocator;
131  if (guard_condition->impl->rmw_handle && guard_condition->impl->allocated_rmw_guard_condition) {
132  if (rmw_destroy_guard_condition(guard_condition->impl->rmw_handle) != RMW_RET_OK) {
133  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
134  result = RCL_RET_ERROR;
135  }
136  }
137  allocator.deallocate(guard_condition->impl, allocator.state);
138  guard_condition->impl = NULL;
139  }
140  return result;
141 }
142 
145 {
146  // !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
147  static rcl_guard_condition_options_t default_options;
148  default_options.allocator = rcl_get_default_allocator();
149  return default_options;
150 }
151 
152 rcl_ret_t
154 {
155  const rcl_guard_condition_options_t * options = rcl_guard_condition_get_options(guard_condition);
156  if (!options) {
157  return RCL_RET_INVALID_ARGUMENT; // error already set
158  }
159  // Trigger the guard condition.
160  if (rmw_trigger_guard_condition(guard_condition->impl->rmw_handle) != RMW_RET_OK) {
161  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
162  return RCL_RET_ERROR;
163  }
164  return RCL_RET_OK;
165 }
166 
169 {
170  // Perform argument validation.
171  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, NULL);
172  RCL_CHECK_FOR_NULL_WITH_MSG(
173  guard_condition->impl,
174  "guard condition implementation is invalid",
175  return NULL);
176  return &guard_condition->impl->options;
177 }
178 
179 rmw_guard_condition_t *
181 {
182  const rcl_guard_condition_options_t * options = rcl_guard_condition_get_options(guard_condition);
183  if (!options) {
184  return NULL; // error already set
185  }
186  return guard_condition->impl->rmw_handle;
187 }
188 
189 #ifdef __cplusplus
190 }
191 #endif
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Definition: allocator.h:37
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
Definition: allocator.h:56
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_context_is_valid(const rcl_context_t *context)
Return true if the given context is currently valid, otherwise false.
Definition: context.c:94
RCL_PUBLIC RCL_WARN_UNUSED const rcl_guard_condition_options_t * rcl_guard_condition_get_options(const rcl_guard_condition_t *guard_condition)
Return the guard condition options.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
Return a rcl_guard_condition_t struct with members set to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rmw_guard_condition_t * rcl_guard_condition_get_rmw_handle(const rcl_guard_condition_t *guard_condition)
Return the rmw guard condition handle.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_trigger_guard_condition(rcl_guard_condition_t *guard_condition)
Trigger a rcl guard condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_init(rcl_guard_condition_t *guard_condition, rcl_context_t *context, const rcl_guard_condition_options_t options)
Initialize a rcl guard_condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_fini(rcl_guard_condition_t *guard_condition)
Finalize a rcl_guard_condition_t.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_options_t rcl_guard_condition_get_default_options(void)
Return the default options in a rcl_guard_condition_options_t struct.
rcl_ret_t rcl_guard_condition_init_from_rmw(rcl_guard_condition_t *guard_condition, const rmw_guard_condition_t *rmw_guard_condition, rcl_context_t *context, const rcl_guard_condition_options_t options)
Same as rcl_guard_condition_init(), but reusing an existing rmw handle.
rmw_context_t rmw_context
rmw context.
Definition: context_impl.h:40
Encapsulates the non-global state of an init/shutdown cycle.
Definition: context.h:114
rcl_context_impl_t * impl
Implementation specific pointer.
Definition: context.h:120
Options available for a rcl guard condition.
rcl_allocator_t allocator
Custom allocator for the guard condition, used for internal allocations.
Handle for a rcl guard condition.
rcl_context_t * context
Context associated with this guard condition.
rcl_guard_condition_impl_t * impl
Pointer to the guard condition implementation.
#define RCL_RET_NOT_INIT
rcl_init() not yet called return code.
Definition: types.h:42
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
Definition: types.h:40
#define RCL_RET_OK
Success return code.
Definition: types.h:26
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
Definition: types.h:32
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:34
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:28
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23