ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
init_options.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/init_options.h"
21 
22 #include "./common.h"
23 #include "./init_options_impl.h"
24 #include "rcutils/macros.h"
25 #include "rcl/error_handling.h"
26 #include "rmw/error_handling.h"
27 #include "rcutils/logging_macros.h"
28 
31 {
32  return (const rcl_init_options_t) {
33  .impl = 0,
34  }; // NOLINT(readability/braces): false positive
35 }
36 
38 static inline
40 _rcl_init_options_zero_init(rcl_init_options_t * init_options, rcl_allocator_t allocator)
41 {
42  init_options->impl = allocator.allocate(sizeof(rcl_init_options_impl_t), allocator.state);
43  RCL_CHECK_FOR_NULL_WITH_MSG(
44  init_options->impl,
45  "failed to allocate memory for init options impl",
46  return RCL_RET_BAD_ALLOC);
47  init_options->impl->allocator = allocator;
48  init_options->impl->rmw_init_options = rmw_get_zero_initialized_init_options();
49 
50  return RCL_RET_OK;
51 }
52 
55 {
56  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
57  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ALREADY_INIT);
58  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_BAD_ALLOC);
59  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
60 
61  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, RCL_RET_INVALID_ARGUMENT);
62  if (NULL != init_options->impl) {
63  RCL_SET_ERROR_MSG("given init_options (rcl_init_options_t) is already initialized");
64  return RCL_RET_ALREADY_INIT;
65  }
67 
68  rcl_ret_t ret = _rcl_init_options_zero_init(init_options, allocator);
69  if (RCL_RET_OK != ret) {
70  return ret;
71  }
72  rmw_ret_t rmw_ret = rmw_init_options_init(&(init_options->impl->rmw_init_options), allocator);
73  if (RMW_RET_OK != rmw_ret) {
74  allocator.deallocate(init_options->impl, allocator.state);
75  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
76  return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
77  }
78 
79  // rmw defaults discovery range to LOCALHOST
80  // Intentionally set discovery options to NOT_SET so we can determine
81  // if a user intentionally sets them later
82  init_options->impl->rmw_init_options.discovery_options.automatic_discovery_range =
83  RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET;
84  return RCL_RET_OK;
85 }
86 
89 {
90  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
91  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ALREADY_INIT);
92  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_BAD_ALLOC);
93  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
94 
95  RCL_CHECK_ARGUMENT_FOR_NULL(src, RCL_RET_INVALID_ARGUMENT);
96  RCL_CHECK_ARGUMENT_FOR_NULL(src->impl, RCL_RET_INVALID_ARGUMENT);
97  RCL_CHECK_ALLOCATOR(&src->impl->allocator, return RCL_RET_INVALID_ARGUMENT);
98  RCL_CHECK_ARGUMENT_FOR_NULL(dst, RCL_RET_INVALID_ARGUMENT);
99  if (NULL != dst->impl) {
100  RCL_SET_ERROR_MSG("given dst (rcl_init_options_t) is already initialized");
101  return RCL_RET_ALREADY_INIT;
102  }
103 
104  // initialize dst (since we know it's in a zero initialized state)
105  rcl_ret_t ret = _rcl_init_options_zero_init(dst, src->impl->allocator);
106  if (RCL_RET_OK != ret) {
107  return ret; // error already set
108  }
109 
110  // copy src information into dst
111  rmw_ret_t rmw_ret =
112  rmw_init_options_copy(&(src->impl->rmw_init_options), &(dst->impl->rmw_init_options));
113  if (RMW_RET_OK != rmw_ret) {
114  rmw_error_string_t error_string = rmw_get_error_string();
115  rmw_reset_error();
116  ret = rcl_init_options_fini(dst);
117  if (RCL_RET_OK != ret) {
118  RCUTILS_LOG_ERROR_NAMED(
119  "rcl",
120  "failed to finalize dst rcl_init_options while handling failure to "
121  "copy rmw_init_options, original ret '%d' and error: %s", rmw_ret, error_string.str);
122  return ret; // error already set
123  }
124  RCL_SET_ERROR_MSG(error_string.str);
125  return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
126  }
127 
128  return RCL_RET_OK;
129 }
130 
131 rcl_ret_t
133 {
134  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, RCL_RET_INVALID_ARGUMENT);
135  RCL_CHECK_ARGUMENT_FOR_NULL(init_options->impl, RCL_RET_INVALID_ARGUMENT);
136  rcl_allocator_t allocator = init_options->impl->allocator;
137  RCL_CHECK_ALLOCATOR(&allocator, return RCL_RET_INVALID_ARGUMENT);
138  rmw_ret_t rmw_ret = rmw_init_options_fini(&(init_options->impl->rmw_init_options));
139  if (RMW_RET_OK != rmw_ret) {
140  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
141  return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
142  }
143  allocator.deallocate(init_options->impl, allocator.state);
144  return RCL_RET_OK;
145 }
146 
147 rcl_ret_t
148 rcl_init_options_get_domain_id(const rcl_init_options_t * init_options, size_t * domain_id)
149 {
150  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, RCL_RET_INVALID_ARGUMENT);
151  RCL_CHECK_ARGUMENT_FOR_NULL(init_options->impl, RCL_RET_INVALID_ARGUMENT);
152  RCL_CHECK_ARGUMENT_FOR_NULL(domain_id, RCL_RET_INVALID_ARGUMENT);
153  *domain_id = init_options->impl->rmw_init_options.domain_id;
154  return RCL_RET_OK;
155 }
156 
157 rcl_ret_t
158 rcl_init_options_set_domain_id(rcl_init_options_t * init_options, size_t domain_id)
159 {
160  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, RCL_RET_INVALID_ARGUMENT);
161  RCL_CHECK_ARGUMENT_FOR_NULL(init_options->impl, RCL_RET_INVALID_ARGUMENT);
162  init_options->impl->rmw_init_options.domain_id = domain_id;
163  return RCL_RET_OK;
164 }
165 
166 rmw_init_options_t *
168 {
169  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, NULL);
170  RCL_CHECK_ARGUMENT_FOR_NULL(init_options->impl, NULL);
171  return &(init_options->impl->rmw_init_options);
172 }
173 
174 const rcl_allocator_t *
176 {
177  RCL_CHECK_ARGUMENT_FOR_NULL(init_options, NULL);
178  RCL_CHECK_ARGUMENT_FOR_NULL(init_options->impl, NULL);
179  return &(init_options->impl->allocator);
180 }
181 
182 #ifdef __cplusplus
183 }
184 #endif
#define RCL_CHECK_ALLOCATOR(allocator, fail_statement)
Check that the given allocator is initialized.
Definition: allocator.h:49
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_init(rcl_init_options_t *init_options, rcl_allocator_t allocator)
Initialize given init_options with the default values and implementation specific values.
Definition: init_options.c:54
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_set_domain_id(rcl_init_options_t *init_options, size_t domain_id)
Set a domain id in the init options provided.
Definition: init_options.c:158
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_copy(const rcl_init_options_t *src, rcl_init_options_t *dst)
Copy the given source init_options to the destination init_options.
Definition: init_options.c:88
RCL_PUBLIC RCL_WARN_UNUSED rcl_init_options_t rcl_get_zero_initialized_init_options(void)
Return a zero initialized rcl_init_options_t struct.
Definition: init_options.c:30
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_get_domain_id(const rcl_init_options_t *init_options, size_t *domain_id)
Return the domain_id stored in the init options.
Definition: init_options.c:148
RCL_PUBLIC RCL_WARN_UNUSED const rcl_allocator_t * rcl_init_options_get_allocator(const rcl_init_options_t *init_options)
Return the allocator stored in the init_options.
Definition: init_options.c:175
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_fini(rcl_init_options_t *init_options)
Finalize the given init_options.
Definition: init_options.c:132
RCL_PUBLIC RCL_WARN_UNUSED rmw_init_options_t * rcl_init_options_get_rmw_init_options(rcl_init_options_t *init_options)
Return the rmw init options which are stored internally.
Definition: init_options.c:167
Encapsulation of init options and implementation defined init options.
Definition: init_options.h:36
rcl_init_options_impl_t * impl
Implementation specific pointer.
Definition: init_options.h:38
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
Definition: types.h:41
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
Definition: types.h:33
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:35
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:29
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24