Skip to content

Commit 3628967

Browse files
authored
Command line static remapping (#217)
Command line static remapping (ROS1 compatible syntax + nodename prefix)
1 parent 7008a7d commit 3628967

20 files changed

+2550
-91
lines changed

rcl/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
2727
endif()
2828

2929
set(${PROJECT_NAME}_sources
30+
src/rcl/arguments.c
3031
src/rcl/client.c
3132
src/rcl/common.c
3233
src/rcl/expand_topic_name.c
@@ -35,6 +36,7 @@ set(${PROJECT_NAME}_sources
3536
src/rcl/node.c
3637
src/rcl/publisher.c
3738
src/rcl/rcl.c
39+
src/rcl/remap.c
3840
src/rcl/rmw_implementation_identifier_check.c
3941
src/rcl/service.c
4042
src/rcl/subscription.c

rcl/include/rcl/arguments.h

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Copyright 2018 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 RCL__ARGUMENTS_H_
16+
#define RCL__ARGUMENTS_H_
17+
18+
#include "rcl/allocator.h"
19+
#include "rcl/macros.h"
20+
#include "rcl/types.h"
21+
#include "rcl/visibility_control.h"
22+
23+
#if __cplusplus
24+
extern "C"
25+
{
26+
#endif
27+
28+
struct rcl_arguments_impl_t;
29+
30+
/// Hold output of parsing command line arguments.
31+
typedef struct rcl_arguments_t
32+
{
33+
/// Private implementation pointer.
34+
struct rcl_arguments_impl_t * impl;
35+
} rcl_arguments_t;
36+
37+
/// Return a rcl_node_t struct with members initialized to `NULL`.
38+
RCL_PUBLIC
39+
RCL_WARN_UNUSED
40+
rcl_arguments_t
41+
rcl_get_zero_initialized_arguments(void);
42+
43+
/// Parse command line arguments into a structure usable by code.
44+
/**
45+
* If an argument does not appear to be a valid ROS argument then it is skipped and parsing
46+
* continues with the next argument in `argv`.
47+
* \sa rcl_arguments_get_count_unparsed()
48+
* \sa rcl_arguments_get_unparsed()
49+
*
50+
* Successfully parsed remap rules are stored in the order they were given in `argv`.
51+
* If given arguments `{"__ns:=/foo", "__ns:=/bar"}` then the namespace used by nodes in this
52+
* process will be `/foo` and not `/bar`.
53+
* \sa rcl_remap_topic_name()
54+
* \sa rcl_remap_service_name()
55+
* \sa rcl_remap_node_name()
56+
* \sa rcl_remap_node_namespace()
57+
*
58+
* <hr>
59+
* Attribute | Adherence
60+
* ------------------ | -------------
61+
* Allocates Memory | Yes
62+
* Thread-Safe | Yes
63+
* Uses Atomics | No
64+
* Lock-Free | Yes
65+
*
66+
* \param[in] argc The number of arguments in argv.
67+
* \param[in] argv Whe values of the arguments.
68+
* \param[in] allocator A valid allocator.
69+
* \param[out] args_output A structure that will contain the result of parsing.
70+
* \return `RCL_RET_OK` if the arguments were parsed successfully, or
71+
* \return `RCL_RET_INVALID_ARGUMENT` if any function arguments are invalid, or
72+
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
73+
* \return `RCL_RET_ERROR` if an unspecified error occurs.
74+
*/
75+
RCL_PUBLIC
76+
RCL_WARN_UNUSED
77+
rcl_ret_t
78+
rcl_parse_arguments(
79+
int argc,
80+
const char * const argv[],
81+
rcl_allocator_t allocator,
82+
rcl_arguments_t * args_output);
83+
84+
/// Return the number of arguments that were not successfully parsed.
85+
/**
86+
* <hr>
87+
* Attribute | Adherence
88+
* ------------------ | -------------
89+
* Allocates Memory | No
90+
* Thread-Safe | Yes
91+
* Uses Atomics | No
92+
* Lock-Free | Yes
93+
*
94+
* \param[in] args An arguments structure that has been parsed.
95+
* \return number of unparsed arguments, or
96+
* \return -1 if args is `NULL` or zero initialized.
97+
*/
98+
RCL_PUBLIC
99+
RCL_WARN_UNUSED
100+
int
101+
rcl_arguments_get_count_unparsed(
102+
rcl_arguments_t * args);
103+
104+
/// Return a list of indexes that weren't successfully parsed.
105+
/**
106+
* Some arguments may not have been successfully parsed, or were not intended as ROS arguments.
107+
* This function populates an array of indexes to these arguments in the original argv array.
108+
* Since the first argument is always assumed to be a process name, the list will always contain
109+
* the index 0.
110+
*
111+
* <hr>
112+
* Attribute | Adherence
113+
* ------------------ | -------------
114+
* Allocates Memory | Yes
115+
* Thread-Safe | Yes
116+
* Uses Atomics | No
117+
* Lock-Free | Yes
118+
*
119+
* \param[in] args An arguments structure that has been parsed.
120+
* \param[in] allocator A valid allocator.
121+
* \param[out] output_unparsed_indices An allocated array of indices into the original argv array.
122+
* This array must be deallocated by the caller using the given allocator.
123+
* If there are no unparsed args then the output will be set to NULL.
124+
* \return `RCL_RET_OK` if everything goes correctly, or
125+
* \return `RCL_RET_INVALID_ARGUMENT` if any function arguments are invalid, or
126+
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
127+
* \return `RCL_RET_ERROR` if an unspecified error occurs.
128+
*/
129+
RCL_PUBLIC
130+
RCL_WARN_UNUSED
131+
rcl_ret_t
132+
rcl_arguments_get_unparsed(
133+
rcl_arguments_t * args,
134+
rcl_allocator_t allocator,
135+
int ** output_unparsed_indices);
136+
137+
/// Reclaim resources held inside rcl_arguments_t structure.
138+
/**
139+
* <hr>
140+
* Attribute | Adherence
141+
* ------------------ | -------------
142+
* Allocates Memory | No
143+
* Thread-Safe | Yes
144+
* Uses Atomics | No
145+
* Lock-Free | Yes
146+
*
147+
* \param[in] args The structure to be deallocated.
148+
* \param[in] allocator A valid allocator.
149+
* \return `RCL_RET_OK` if the memory was successfully freed, or
150+
* \return `RCL_RET_INVALID_ARGUMENT` if any function arguments are invalid, or
151+
* \return `RCL_RET_ERROR` if an unspecified error occurs.
152+
*/
153+
RCL_PUBLIC
154+
RCL_WARN_UNUSED
155+
rcl_ret_t
156+
rcl_arguments_fini(
157+
rcl_arguments_t * args);
158+
159+
/// Get a global instance of command line arguments.
160+
/**
161+
* \sa rcl_init(int, char **, rcl_allocator_t)
162+
* \sa rcl_shutdown()
163+
* This returns parsed command line arguments that were passed to `rcl_init()`.
164+
* The value returned by this function is undefined before `rcl_init()` is called and after
165+
* `rcl_shutdown()` is called.
166+
* The return value must not be finalized.
167+
*
168+
* <hr>
169+
* Attribute | Adherence
170+
* ------------------ | -------------
171+
* Allocates Memory | No
172+
* Thread-Safe | Yes
173+
* Uses Atomics | No
174+
* Lock-Free | Yes
175+
*
176+
* \return a global instance of parsed command line arguments.
177+
*/
178+
RCL_PUBLIC
179+
RCL_WARN_UNUSED
180+
rcl_arguments_t *
181+
rcl_get_global_arguments();
182+
183+
#if __cplusplus
184+
}
185+
#endif
186+
187+
#endif // RCL__ARGUMENTS_H_

rcl/include/rcl/node.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C"
2323
#include <stdint.h>
2424

2525
#include "rcl/allocator.h"
26+
#include "rcl/arguments.h"
2627
#include "rcl/macros.h"
2728
#include "rcl/types.h"
2829
#include "rcl/visibility_control.h"
@@ -65,6 +66,12 @@ typedef struct rcl_node_options_t
6566

6667
/// Custom allocator used for internal allocations.
6768
rcl_allocator_t allocator;
69+
70+
/// If false then only use arguments in this struct, otherwise use global arguments also.
71+
bool use_global_arguments;
72+
73+
/// Command line arguments that apply only to this node.
74+
rcl_arguments_t arguments;
6875
} rcl_node_options_t;
6976

7077
/// Return a rcl_node_t struct with members initialized to `NULL`.

0 commit comments

Comments
 (0)