Skip to content

Commit dad5a51

Browse files
nwnestevemxgrey
authoredOct 26, 2024··
Action message support (#417)
* Added action template * Added action generation * Added basic create_action_client function * dded action generation * checkin * Fix missing exported pre_field_serde field * Removed extra code * Sketch out action server construction and destruction This follows generally the same pattern as the service server. It required adding a typesupport function to the Action trait and pulling in some more rcl_action bindings. * Fix action typesupport function * Add ActionImpl trait with internal messages and services This is incomplete, since the service types aren't yet being generated. * Split srv.rs.em into idiomatic and rmw template files This results in the exact same file being produced for services, except for some whitespace changes. However, it enables actions to invoke the respective service template for its generation, similar to how the it works for services and their underlying messages. * Generate underlying service definitions for actions Not tested * Add runtime trait to get the UUID from a goal request C++ uses duck typing for this, knowing that for any `Action`, the type `Action::Impl::SendGoalService::Request` will always have a `goal_id` field of type `unique_identifier_msgs::msg::UUID` without having to prove this to the compiler. Rust's generics are more strict, requiring that this be proven using type bounds. The `Request` type is also action-specific as it contains a `goal` field containing the `Goal` message type of the action. We therefore cannot enforce that all `Request`s are a specific type in `rclrs`. This seems most easily represented using associated type bounds on the `SendGoalService` associated type within `ActionImpl`. To avoid introducing to `rosidl_runtime_rs` a circular dependency on message packages like `unique_identifier_msgs`, the `ExtractUuid` trait only operates on a byte array rather than a more nicely typed `UUID` message type. I'll likely revisit this as we introduce more similar bounds on the generated types. * Integrate RMW message methods into ActionImpl Rather than having a bunch of standalone traits implementing various message functions like `ExtractUuid` and `SetAccepted`, with the trait bounds on each associated type in `ActionImpl`, we'll instead add these functions directly to the `ActionImpl` trait. This is simpler on both the rosidl_runtime_rs and the rclrs side. * Add rosidl_runtime_rs::ActionImpl::create_feedback_message() Adds a trait method to create a feedback message given the goal ID and the user-facing feedback message type. Depending on how many times we do this, it may end up valuable to define a GoalUuid type in rosidl_runtime_rs itself. We wouldn't be able to utilize the `RCL_ACTION_UUID_SIZE` constant imported from `rcl_action`, but this is pretty much guaranteed to be 16 forever. Defining this method signature also required inverting the super-trait relationship between Action and ActionImpl. Now ActionImpl is the sub-trait as this gives it access to all of Action's associated types. Action doesn't need to care about anything from ActionImpl (hopefully). * Add GetResultService methods to ActionImpl * Implement ActionImpl trait methods in generator These still don't build without errors, but it's close. * Replace set_result_response_status with create_result_response rclrs needs to be able to generically construct result responses, including the user-defined result field. * Implement client-side trait methods for action messages This adds methods to ActionImpl for creating and accessing action-specific message types. These are needed by the rclrs ActionClient to generically read and write RMW messages. Due to issues with qualified paths in certain places (rust-lang/rust#86935), the generator now refers directly to its service and message types rather than going through associated types of the various traits. This also makes the generated code a little easier to read, with the trait method signatures from rosidl_runtime_rs still enforcing type-safety. * Format the rosidl_runtime_rs::ActionImpl trait * Wrap longs lines in rosidl_generator_rs action.rs This at least makes the template easier to read, but also helps with the generated code. In general, the generated code could actually fit on one line for the function signatures, but it's not a big deal to split it across multiple. * Use idiomatic message types in Action trait This is user-facing and so should use the friendly message types. * Cleanup ActionImpl using type aliases Signed-off-by: Michael X. Grey <[email protected]> * Formatting * Switch from std::os::raw::c_void to std::ffi::c_void While these are aliases of each other, we might as well use the more appropriate std::ffi version, as requested by reviewers. * Clean up rosidl_generator_rs's cmake files Some of the variables are present but no longer used. Others were not updated with the action changes. * Add a short doc page on the message generation pipeline This should help newcomers orient themselves around the rosidl_*_rs packages. --------- Signed-off-by: Michael X. Grey <[email protected]> Co-authored-by: Esteve Fernandez <[email protected]> Co-authored-by: Michael X. Grey <[email protected]>
1 parent 06ee9bf commit dad5a51

File tree

12 files changed

+495
-82
lines changed

12 files changed

+495
-82
lines changed
 

‎docs/message-generation.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# `ros2_rust` Message Generation
2+
3+
The `ros2_rust` project strives to maintain consistency with the upstream ROS 2 message generation
4+
system. To this end, it provides two main packages: `rosidl_generator_rs` and `rosidl_runtime_rs`.
5+
These packages provide the infrastructure required for defining and using ROS 2 messages, services,
6+
and actions in Rust.
7+
8+
At a high level, the `rosidl_generator_rs` package handles generation of interface crates from the
9+
`.msg`, `.srv`, and `.action` files defined by a user. The `rosidl_runtime_rs` package provides
10+
common functionality shared across message packages, such as support types and traits. Each of these
11+
packages is described in more detail below.
12+
13+
## `rosidl_generator_rs`
14+
15+
`rosidl_generator_rs` follows a very similar pattern to the other message generation packages for
16+
ROS 2. To tie into this pipeline, it registers itself as a `"rosidl_generate_idl_interfaces"`
17+
extension with `ament`. Doing so ensures that message packages calling `rosidl_generate_interfaces`
18+
will invoke the Rust language generator in addition to any others. This is accomplished using the
19+
various CMake scripts under the `cmake` directory. When this happens, the input interface files will
20+
be converted into IDL files which, along with additional metadata, are fed into the `generate_rs`
21+
function of `rosidl_generator_rs/__init__.py`.
22+
23+
From here, the IDL files are parsed into an internal representation using the upstream
24+
[`rosidl_parser`](https://github.com/ros2/rosidl/tree/rolling/rosidl_parser) package. This abstract
25+
representation is then used to instantiate the various template files under the `resource`
26+
subdirectory, producing a full Rust crate for each package.
27+
28+
For each input message type, two `struct`s are generated:
29+
30+
- An ergonomic representation of the message, using more idiomatic `std` types like `Vec` and
31+
`String` for sequence and string fields. These are placed directly in the `msg` submodule of the
32+
crate.
33+
- A FFI-suitable `struct` that is ABI-compatible with the layout expected by the RMW layer. While
34+
less ergonomic, these avoid the conversion overhead when passed to RMW. These `struct`s are placed
35+
under the `msg::rmw` submodule.
36+
37+
All the produced `struct`s implement the standard traits from `std` when possible, such as `Clone`,
38+
`PartialEq`, and `Debug`. Additionally, when the generated crate's `serde` feature is enabled, these
39+
structs implement the `Serialize` and `Deserialize` traits.
40+
41+
## `rosidl_runtime_rs`
42+
43+
`rosidl_runtime_rs` is a runtime support package, providing `Message`, `Service`, and `Action`
44+
traits that are implemented by the corresponding structs generated by `rosidl_generator_rs`. These
45+
allow for generic interaction with these various interface types, both in client libraries like
46+
`rclrs` and in user code.
47+
48+
The package also provides a number of string and sequence types that are ABI-compatible with their
49+
`rosidl_runtime_c` equivalents. This allows for more ergonomic use of the RMW-native message types.

‎rosidl_generator_rs/cmake/custom_command.cmake

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
add_custom_command(
1616
OUTPUT
17-
${_generated_extension_files}
1817
${_generated_common_rs_files}
1918
${_generated_msg_rs_files}
20-
${_generated_msg_c_files}
2119
${_generated_srv_rs_files}
22-
${_generated_srv_c_files}
20+
${_generated_action_rs_files}
2321
COMMAND ${PYTHON_EXECUTABLE} ${rosidl_generator_rs_BIN}
2422
--generator-arguments-file "${generator_arguments_file}"
2523
--typesupport-impls "${_typesupport_impls}"
@@ -34,11 +32,9 @@ else()
3432
add_custom_target(
3533
${rosidl_generate_interfaces_TARGET}${_target_suffix} ALL
3634
DEPENDS
37-
${_generated_extension_files}
3835
${_generated_common_rs_files}
3936
${_generated_msg_rs_files}
40-
${_generated_msg_c_files}
4137
${_generated_srv_rs_files}
42-
${_generated_srv_c_files}
38+
${_generated_action_rs_files}
4339
)
4440
endif()

‎rosidl_generator_rs/cmake/rosidl_generator_rs_generate_interfaces.cmake

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ set(_generated_common_rs_files "")
2626

2727
set(_generated_msg_rs_files "")
2828
set(_generated_srv_rs_files "")
29+
set(_generated_action_rs_files "")
2930

3031
set(_has_msg FALSE)
3132
set(_has_srv FALSE)
33+
set(_has_action FALSE)
3234

3335
foreach(_idl_file ${rosidl_generate_interfaces_ABS_IDL_FILES})
3436
get_filename_component(_parent_folder "${_idl_file}" DIRECTORY)
@@ -37,13 +39,13 @@ foreach(_idl_file ${rosidl_generate_interfaces_ABS_IDL_FILES})
3739

3840
if(_parent_folder STREQUAL "msg")
3941
set(_has_msg TRUE)
40-
set(_idl_file_without_actions ${_idl_file_without_actions} ${_idl_file})
42+
set(_idl_files ${_idl_files} ${_idl_file})
4143
elseif(_parent_folder STREQUAL "srv")
4244
set(_has_srv TRUE)
43-
set(_idl_file_without_actions ${_idl_file_without_actions} ${_idl_file})
45+
set(_idl_files ${_idl_files} ${_idl_file})
4446
elseif(_parent_folder STREQUAL "action")
4547
set(_has_action TRUE)
46-
message(WARNING "Rust actions generation is not implemented")
48+
set(_idl_files ${_idl_files} ${_idl_file})
4749
else()
4850
message(FATAL_ERROR "Interface file with unknown parent folder: ${_idl_file}")
4951
endif()
@@ -67,6 +69,12 @@ if(${_has_srv})
6769
)
6870
endif()
6971

72+
if(${_has_action})
73+
list(APPEND _generated_action_rs_files
74+
"${_output_path}/rust/src/action.rs"
75+
)
76+
endif()
77+
7078
set(_dependency_files "")
7179
set(_dependencies "")
7280
foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
@@ -81,12 +89,15 @@ endforeach()
8189
set(target_dependencies
8290
"${rosidl_generator_rs_BIN}"
8391
${rosidl_generator_rs_GENERATOR_FILES}
92+
"${rosidl_generator_rs_TEMPLATE_DIR}/action.rs.em"
8493
"${rosidl_generator_rs_TEMPLATE_DIR}/msg_idiomatic.rs.em"
8594
"${rosidl_generator_rs_TEMPLATE_DIR}/msg_rmw.rs.em"
8695
"${rosidl_generator_rs_TEMPLATE_DIR}/msg.rs.em"
96+
"${rosidl_generator_rs_TEMPLATE_DIR}/srv_idiomatic.rs.em"
97+
"${rosidl_generator_rs_TEMPLATE_DIR}/srv_rmw.rs.em"
8798
"${rosidl_generator_rs_TEMPLATE_DIR}/srv.rs.em"
8899
${rosidl_generate_interfaces_ABS_IDL_FILES}
89-
${_idl_file_without_actions}
100+
${_idl_files}
90101
${_dependency_files})
91102
foreach(dep ${target_dependencies})
92103
if(NOT EXISTS "${dep}")
@@ -99,7 +110,7 @@ rosidl_write_generator_arguments(
99110
"${generator_arguments_file}"
100111
PACKAGE_NAME "${PROJECT_NAME}"
101112
IDL_TUPLES "${rosidl_generate_interfaces_IDL_TUPLES}"
102-
ROS_INTERFACE_FILES "${_idl_file_without_actions}"
113+
ROS_INTERFACE_FILES "${_idl_files}"
103114
ROS_INTERFACE_DEPENDENCIES "${_dependencies}"
104115
OUTPUT_DIR "${_output_path}"
105116
TEMPLATE_DIR "${rosidl_generator_rs_TEMPLATE_DIR}"
@@ -130,6 +141,7 @@ set_property(
130141
${_generated_common_rs_files}
131142
${_generated_msg_rs_files}
132143
${_generated_srv_rs_files}
144+
${_generated_action_rs_files}
133145
PROPERTY GENERATED 1)
134146

135147
set(_rsext_suffix "__rsext")
@@ -144,7 +156,8 @@ endif()
144156
if(BUILD_TESTING AND rosidl_generate_interfaces_ADD_LINTER_TESTS)
145157
if(
146158
NOT _generated_msg_rs_files STREQUAL "" OR
147-
NOT _generated_srv_rs_files STREQUAL ""
159+
NOT _generated_srv_rs_files STREQUAL "" OR
160+
NOT _generated_action_rs_files STREQUAL ""
148161
)
149162
# TODO(esteve): add linters for Rust files
150163
endif()
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
@{
2+
from rosidl_parser.definition import (
3+
ACTION_FEEDBACK_MESSAGE_SUFFIX,
4+
ACTION_FEEDBACK_SUFFIX,
5+
ACTION_GOAL_SERVICE_SUFFIX,
6+
ACTION_GOAL_SUFFIX,
7+
ACTION_RESULT_SERVICE_SUFFIX,
8+
ACTION_RESULT_SUFFIX,
9+
SERVICE_REQUEST_MESSAGE_SUFFIX,
10+
SERVICE_RESPONSE_MESSAGE_SUFFIX,
11+
)
12+
13+
action_msg_specs = []
14+
15+
for subfolder, action in action_specs:
16+
action_msg_specs.append((subfolder, action.goal))
17+
action_msg_specs.append((subfolder, action.result))
18+
action_msg_specs.append((subfolder, action.feedback))
19+
action_msg_specs.append((subfolder, action.feedback_message))
20+
21+
action_srv_specs = []
22+
23+
for subfolder, action in action_specs:
24+
action_srv_specs.append((subfolder, action.send_goal_service))
25+
action_srv_specs.append((subfolder, action.get_result_service))
26+
}@
27+
28+
pub mod rmw {
29+
@{
30+
TEMPLATE(
31+
'msg_rmw.rs.em',
32+
package_name=package_name, interface_path=interface_path,
33+
msg_specs=action_msg_specs,
34+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
35+
pre_field_serde=pre_field_serde,
36+
get_idiomatic_rs_type=get_idiomatic_rs_type,
37+
constant_value_to_rs=constant_value_to_rs)
38+
39+
TEMPLATE(
40+
'srv_rmw.rs.em',
41+
package_name=package_name, interface_path=interface_path,
42+
srv_specs=action_srv_specs,
43+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
44+
pre_field_serde=pre_field_serde,
45+
get_idiomatic_rs_type=get_idiomatic_rs_type,
46+
constant_value_to_rs=constant_value_to_rs)
47+
}@
48+
} // mod rmw
49+
50+
@{
51+
TEMPLATE(
52+
'msg_idiomatic.rs.em',
53+
package_name=package_name, interface_path=interface_path,
54+
msg_specs=action_msg_specs,
55+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
56+
pre_field_serde=pre_field_serde,
57+
get_idiomatic_rs_type=get_idiomatic_rs_type,
58+
constant_value_to_rs=constant_value_to_rs)
59+
}@
60+
61+
@{
62+
TEMPLATE(
63+
'srv_idiomatic.rs.em',
64+
package_name=package_name, interface_path=interface_path,
65+
srv_specs=action_srv_specs,
66+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
67+
pre_field_serde=pre_field_serde,
68+
get_idiomatic_rs_type=get_idiomatic_rs_type,
69+
constant_value_to_rs=constant_value_to_rs)
70+
}@
71+
72+
@[for subfolder, action_spec in action_specs]
73+
74+
@{
75+
type_name = action_spec.namespaced_type.name
76+
}@
77+
78+
#[link(name = "@(package_name)__rosidl_typesupport_c")]
79+
extern "C" {
80+
fn rosidl_typesupport_c__get_action_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::ffi::c_void;
81+
}
82+
83+
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
84+
pub struct @(type_name);
85+
86+
impl rosidl_runtime_rs::Action for @(type_name) {
87+
type Goal = crate::@(subfolder)::@(type_name)@(ACTION_GOAL_SUFFIX);
88+
type Result = crate::@(subfolder)::@(type_name)@(ACTION_RESULT_SUFFIX);
89+
type Feedback = crate::@(subfolder)::@(type_name)@(ACTION_FEEDBACK_SUFFIX);
90+
91+
fn get_type_support() -> *const std::ffi::c_void {
92+
// SAFETY: No preconditions for this function.
93+
unsafe { rosidl_typesupport_c__get_action_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
94+
}
95+
}
96+
97+
impl rosidl_runtime_rs::ActionImpl for @(type_name) {
98+
type GoalStatusMessage = action_msgs::msg::rmw::GoalStatusArray;
99+
type FeedbackMessage = crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_MESSAGE_SUFFIX);
100+
101+
type SendGoalService = crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX);
102+
type CancelGoalService = action_msgs::srv::rmw::CancelGoal;
103+
type GetResultService = crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX);
104+
105+
fn create_goal_request(
106+
goal_id: &[u8; 16],
107+
goal: crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SUFFIX),
108+
) -> crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX) {
109+
crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX) {
110+
goal_id: unique_identifier_msgs::msg::rmw::UUID { uuid: *goal_id },
111+
goal,
112+
}
113+
}
114+
115+
fn get_goal_request_uuid(
116+
request: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX),
117+
) -> &[u8; 16] {
118+
&request.goal_id.uuid
119+
}
120+
121+
fn create_goal_response(
122+
accepted: bool,
123+
stamp: (i32, u32),
124+
) -> crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX) {
125+
crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX) {
126+
accepted,
127+
stamp: builtin_interfaces::msg::rmw::Time {
128+
sec: stamp.0,
129+
nanosec: stamp.1,
130+
},
131+
}
132+
}
133+
134+
fn get_goal_response_accepted(
135+
response: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX),
136+
) -> bool {
137+
response.accepted
138+
}
139+
140+
fn get_goal_response_stamp(
141+
response: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_GOAL_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX),
142+
) -> (i32, u32) {
143+
(response.stamp.sec, response.stamp.nanosec)
144+
}
145+
146+
fn create_feedback_message(
147+
goal_id: &[u8; 16],
148+
feedback: crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_SUFFIX),
149+
) -> crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_MESSAGE_SUFFIX) {
150+
let mut message = crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_MESSAGE_SUFFIX)::default();
151+
message.goal_id.uuid = *goal_id;
152+
message.feedback = feedback;
153+
message
154+
}
155+
156+
fn get_feedback_message_uuid(
157+
feedback: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_MESSAGE_SUFFIX),
158+
) -> &[u8; 16] {
159+
&feedback.goal_id.uuid
160+
}
161+
162+
fn get_feedback_message_feedback(
163+
feedback: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_MESSAGE_SUFFIX),
164+
) -> &crate::@(subfolder)::rmw::@(type_name)@(ACTION_FEEDBACK_SUFFIX) {
165+
&feedback.feedback
166+
}
167+
168+
fn create_result_request(
169+
goal_id: &[u8; 16],
170+
) -> crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX) {
171+
crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX) {
172+
goal_id: unique_identifier_msgs::msg::rmw::UUID { uuid: *goal_id },
173+
}
174+
}
175+
176+
fn get_result_request_uuid(
177+
request: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_REQUEST_MESSAGE_SUFFIX),
178+
) -> &[u8; 16] {
179+
&request.goal_id.uuid
180+
}
181+
182+
fn create_result_response(
183+
status: i8,
184+
result: crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SUFFIX),
185+
) -> crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX) {
186+
crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX) {
187+
status,
188+
result,
189+
}
190+
}
191+
192+
fn get_result_response_result(
193+
response: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX),
194+
) -> &crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SUFFIX) {
195+
&response.result
196+
}
197+
198+
fn get_result_response_status(
199+
response: &crate::@(subfolder)::rmw::@(type_name)@(ACTION_RESULT_SERVICE_SUFFIX)@(SERVICE_RESPONSE_MESSAGE_SUFFIX),
200+
) -> i8 {
201+
response.status
202+
}
203+
}
204+
205+
@[end for]

‎rosidl_generator_rs/resource/lib.rs.em

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ pub mod msg;
77
@[if len(srv_specs) > 0]@
88
pub mod srv;
99
@[end if]@
10+
11+
@[if len(action_specs) > 0]@
12+
pub mod action;
13+
@[end if]@

‎rosidl_generator_rs/resource/msg_rmw.rs.em

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type_name = msg_spec.structure.namespaced_type.name
1919

2020
#[link(name = "@(package_name)__rosidl_typesupport_c")]
2121
extern "C" {
22-
fn rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
22+
fn rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::ffi::c_void;
2323
}
2424

2525
#[link(name = "@(package_name)__rosidl_generator_c")]
@@ -103,7 +103,7 @@ impl rosidl_runtime_rs::Message for @(type_name) {
103103
104104
impl rosidl_runtime_rs::RmwMessage for @(type_name) where Self: Sized {
105105
const TYPE_NAME: &'static str = "@(package_name)/@(subfolder)/@(type_name)";
106-
fn get_type_support() -> *const std::os::raw::c_void {
106+
fn get_type_support() -> *const std::ffi::c_void {
107107
// SAFETY: No preconditions for this function.
108108
unsafe { rosidl_typesupport_c__get_message_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
109109
}
Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,23 @@
1-
@{
2-
req_res_specs = []
3-
4-
for subfolder, service in srv_specs:
5-
req_res_specs.append((subfolder, service.request_message))
6-
req_res_specs.append((subfolder, service.response_message))
7-
}@
8-
91
@{
102
TEMPLATE(
11-
'msg_idiomatic.rs.em',
3+
'srv_idiomatic.rs.em',
124
package_name=package_name, interface_path=interface_path,
13-
msg_specs=req_res_specs,
5+
srv_specs=srv_specs,
146
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
157
pre_field_serde=pre_field_serde,
168
get_idiomatic_rs_type=get_idiomatic_rs_type,
179
constant_value_to_rs=constant_value_to_rs)
18-
}@
19-
20-
@[for subfolder, srv_spec in srv_specs]
21-
22-
@{
23-
type_name = srv_spec.namespaced_type.name
24-
}@
25-
26-
#[link(name = "@(package_name)__rosidl_typesupport_c")]
27-
extern "C" {
28-
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
2910
}
3011

31-
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
32-
pub struct @(type_name);
33-
34-
impl rosidl_runtime_rs::Service for @(type_name) {
35-
type Request = crate::@(subfolder)::@(type_name)_Request;
36-
type Response = crate::@(subfolder)::@(type_name)_Response;
37-
38-
fn get_type_support() -> *const std::os::raw::c_void {
39-
// SAFETY: No preconditions for this function.
40-
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
41-
}
42-
}
43-
44-
@[end for]
45-
4612
pub mod rmw {
4713
@{
4814
TEMPLATE(
49-
'msg_rmw.rs.em',
15+
'srv_rmw.rs.em',
5016
package_name=package_name, interface_path=interface_path,
51-
msg_specs=req_res_specs,
17+
srv_specs=srv_specs,
5218
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
5319
pre_field_serde=pre_field_serde,
5420
get_idiomatic_rs_type=get_idiomatic_rs_type,
5521
constant_value_to_rs=constant_value_to_rs)
5622
}@
57-
58-
@[for subfolder, srv_spec in srv_specs]
59-
60-
@{
61-
type_name = srv_spec.namespaced_type.name
62-
}@
63-
64-
#[link(name = "@(package_name)__rosidl_typesupport_c")]
65-
extern "C" {
66-
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::os::raw::c_void;
67-
}
68-
69-
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
70-
pub struct @(type_name);
71-
72-
impl rosidl_runtime_rs::Service for @(type_name) {
73-
type Request = crate::@(subfolder)::rmw::@(type_name)_Request;
74-
type Response = crate::@(subfolder)::rmw::@(type_name)_Response;
75-
76-
fn get_type_support() -> *const std::os::raw::c_void {
77-
// SAFETY: No preconditions for this function.
78-
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
79-
}
80-
}
81-
82-
@[end for]
83-
8423
} // mod rmw
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@{
2+
req_res_specs = []
3+
4+
for subfolder, service in srv_specs:
5+
req_res_specs.append((subfolder, service.request_message))
6+
req_res_specs.append((subfolder, service.response_message))
7+
}@
8+
9+
@{
10+
TEMPLATE(
11+
'msg_idiomatic.rs.em',
12+
package_name=package_name, interface_path=interface_path,
13+
msg_specs=req_res_specs,
14+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
15+
pre_field_serde=pre_field_serde,
16+
get_idiomatic_rs_type=get_idiomatic_rs_type,
17+
constant_value_to_rs=constant_value_to_rs)
18+
}@
19+
20+
@[for subfolder, srv_spec in srv_specs]
21+
22+
@{
23+
type_name = srv_spec.namespaced_type.name
24+
}@
25+
26+
#[link(name = "@(package_name)__rosidl_typesupport_c")]
27+
extern "C" {
28+
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::ffi::c_void;
29+
}
30+
31+
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
32+
pub struct @(type_name);
33+
34+
impl rosidl_runtime_rs::Service for @(type_name) {
35+
type Request = crate::@(subfolder)::@(type_name)_Request;
36+
type Response = crate::@(subfolder)::@(type_name)_Response;
37+
38+
fn get_type_support() -> *const std::ffi::c_void {
39+
// SAFETY: No preconditions for this function.
40+
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
41+
}
42+
}
43+
44+
@[end for]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@{
2+
req_res_specs = []
3+
4+
for subfolder, service in srv_specs:
5+
req_res_specs.append((subfolder, service.request_message))
6+
req_res_specs.append((subfolder, service.response_message))
7+
}@
8+
9+
@{
10+
TEMPLATE(
11+
'msg_rmw.rs.em',
12+
package_name=package_name, interface_path=interface_path,
13+
msg_specs=req_res_specs,
14+
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
15+
pre_field_serde=pre_field_serde,
16+
get_idiomatic_rs_type=get_idiomatic_rs_type,
17+
constant_value_to_rs=constant_value_to_rs)
18+
}@
19+
20+
@[for subfolder, srv_spec in srv_specs]
21+
22+
@{
23+
type_name = srv_spec.namespaced_type.name
24+
}@
25+
26+
#[link(name = "@(package_name)__rosidl_typesupport_c")]
27+
extern "C" {
28+
fn rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() -> *const std::ffi::c_void;
29+
}
30+
31+
// Corresponds to @(package_name)__@(subfolder)__@(type_name)
32+
pub struct @(type_name);
33+
34+
impl rosidl_runtime_rs::Service for @(type_name) {
35+
type Request = crate::@(subfolder)::rmw::@(type_name)_Request;
36+
type Response = crate::@(subfolder)::rmw::@(type_name)_Response;
37+
38+
fn get_type_support() -> *const std::ffi::c_void {
39+
// SAFETY: No preconditions for this function.
40+
unsafe { rosidl_typesupport_c__get_service_type_support_handle__@(package_name)__@(subfolder)__@(type_name)() }
41+
}
42+
}
43+
44+
@[end for]

‎rosidl_generator_rs/rosidl_generator_rs/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import rosidl_pycommon
2424

2525
from rosidl_parser.definition import AbstractGenericString
26+
from rosidl_parser.definition import AbstractNestedType
27+
from rosidl_parser.definition import AbstractSequence
28+
from rosidl_parser.definition import AbstractString
29+
from rosidl_parser.definition import AbstractWString
30+
from rosidl_parser.definition import Action
2631
from rosidl_parser.definition import Array
2732
from rosidl_parser.definition import BasicType
2833
from rosidl_parser.definition import BoundedSequence
@@ -86,13 +91,20 @@ def generate_rs(generator_arguments_file, typesupport_impls):
8691
os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'],
8792
}
8893

94+
mapping_actions = {
95+
os.path.join(template_dir, 'action.rs.em'): ['rust/src/%s.rs'],
96+
}
97+
8998
# Ensure the required templates exist
9099
for template_file in mapping_msgs.keys():
91100
assert os.path.exists(template_file), \
92101
'Messages template file %s not found' % template_file
93102
for template_file in mapping_srvs.keys():
94103
assert os.path.exists(template_file), \
95104
'Services template file %s not found' % template_file
105+
for template_file in mapping_actions.keys():
106+
assert os.path.exists(template_file), \
107+
'Actions template file %s not found' % template_file
96108

97109
data = {
98110
'pre_field_serde': pre_field_serde,
@@ -107,6 +119,7 @@ def generate_rs(generator_arguments_file, typesupport_impls):
107119
convert_lower_case_underscore_to_camel_case,
108120
'msg_specs': [],
109121
'srv_specs': [],
122+
'action_specs': [],
110123
'package_name': args['package_name'],
111124
'typesupport_impls': typesupport_impls,
112125
'interface_path': idl_rel_path,
@@ -121,6 +134,9 @@ def generate_rs(generator_arguments_file, typesupport_impls):
121134
for service in idl_content.get_elements_of_type(Service):
122135
data['srv_specs'].append(('srv', service))
123136

137+
for action in idl_content.get_elements_of_type(Action):
138+
data['action_specs'].append(('action', action))
139+
124140
if data['msg_specs']:
125141
for template_file, generated_filenames in mapping_msgs.items():
126142
for generated_filename in generated_filenames:
@@ -143,6 +159,17 @@ def generate_rs(generator_arguments_file, typesupport_impls):
143159
generated_file,
144160
minimum_timestamp=latest_target_timestamp)
145161

162+
if data['action_specs']:
163+
for template_file, generated_filenames in mapping_actions.items():
164+
for generated_filename in generated_filenames:
165+
generated_file = os.path.join(args['output_dir'],
166+
generated_filename % 'action')
167+
rosidl_pycommon.expand_template(
168+
os.path.join(template_dir, template_file),
169+
data.copy(),
170+
generated_file,
171+
minimum_timestamp=latest_target_timestamp)
172+
146173
rosidl_pycommon.expand_template(
147174
os.path.join(template_dir, 'lib.rs.em'),
148175
data.copy(),

‎rosidl_runtime_rs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ mod string;
99
pub use string::{BoundedString, BoundedWString, String, StringExceedsBoundsError, WString};
1010

1111
mod traits;
12-
pub use traits::{Message, RmwMessage, SequenceAlloc, Service};
12+
pub use traits::{Action, ActionImpl, Message, RmwMessage, SequenceAlloc, Service};

‎rosidl_runtime_rs/src/traits.rs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait RmwMessage: Clone + Debug + Default + Send + Sync + Message {
4141
const TYPE_NAME: &'static str;
4242

4343
/// Get a pointer to the correct `rosidl_message_type_support_t` structure.
44-
fn get_type_support() -> *const std::os::raw::c_void;
44+
fn get_type_support() -> *const std::ffi::c_void;
4545
}
4646

4747
/// Trait for types that can be used in a `rclrs::Subscription` and a `rclrs::Publisher`.
@@ -157,5 +157,97 @@ pub trait Service: 'static {
157157
type Response: Message;
158158

159159
/// Get a pointer to the correct `rosidl_service_type_support_t` structure.
160-
fn get_type_support() -> *const std::os::raw::c_void;
160+
fn get_type_support() -> *const std::ffi::c_void;
161161
}
162+
163+
/// Trait for actions.
164+
///
165+
/// User code never needs to call this trait's method, much less implement this trait.
166+
pub trait Action: 'static {
167+
/// The goal message associated with this action.
168+
type Goal: Message;
169+
170+
/// The result message associated with this action.
171+
type Result: Message;
172+
173+
/// The feedback message associated with this action.
174+
type Feedback: Message;
175+
176+
/// Get a pointer to the correct `rosidl_action_type_support_t` structure.
177+
fn get_type_support() -> *const std::ffi::c_void;
178+
}
179+
180+
/// Trait for action implementation details.
181+
///
182+
/// User code never needs to implement this trait, nor use its associated types.
183+
pub trait ActionImpl: 'static + Action {
184+
/// The goal_status message associated with this action.
185+
type GoalStatusMessage: Message;
186+
187+
/// The feedback message associated with this action.
188+
type FeedbackMessage: Message;
189+
190+
/// The send_goal service associated with this action.
191+
type SendGoalService: Service;
192+
193+
/// The cancel_goal service associated with this action.
194+
type CancelGoalService: Service;
195+
196+
/// The get_result service associated with this action.
197+
type GetResultService: Service;
198+
199+
/// Create a goal request message with the given UUID and goal.
200+
fn create_goal_request(goal_id: &[u8; 16], goal: RmwGoalData<Self>) -> RmwGoalRequest<Self>;
201+
202+
/// Get the UUID of a goal request.
203+
fn get_goal_request_uuid(request: &RmwGoalRequest<Self>) -> &[u8; 16];
204+
205+
/// Create a goal response message with the given acceptance and timestamp.
206+
fn create_goal_response(accepted: bool, stamp: (i32, u32)) -> RmwGoalResponse<Self>;
207+
208+
/// Get the `accepted` field of a goal response.
209+
fn get_goal_response_accepted(response: &RmwGoalResponse<Self>) -> bool;
210+
211+
/// Get the `stamp` field of a goal response.
212+
fn get_goal_response_stamp(response: &RmwGoalResponse<Self>) -> (i32, u32);
213+
214+
/// Create a feedback message with the given goal ID and contents.
215+
fn create_feedback_message(
216+
goal_id: &[u8; 16],
217+
feedback: RmwFeedbackData<Self>,
218+
) -> RmwFeedbackMessage<Self>;
219+
220+
/// Get the UUID of a feedback message.
221+
fn get_feedback_message_uuid(feedback: &RmwFeedbackMessage<Self>) -> &[u8; 16];
222+
223+
/// Get the feedback of a feedback message.
224+
fn get_feedback_message_feedback(feedback: &RmwFeedbackMessage<Self>)
225+
-> &RmwFeedbackData<Self>;
226+
227+
/// Create a result request message with the given goal ID.
228+
fn create_result_request(goal_id: &[u8; 16]) -> RmwResultRequest<Self>;
229+
230+
/// Get the UUID of a result request.
231+
fn get_result_request_uuid(request: &RmwResultRequest<Self>) -> &[u8; 16];
232+
233+
/// Create a result response message with the given status and contents.
234+
fn create_result_response(status: i8, result: RmwResultData<Self>) -> RmwResultResponse<Self>;
235+
236+
/// Get the result of a result response.
237+
fn get_result_response_result(response: &RmwResultResponse<Self>) -> &RmwResultData<Self>;
238+
239+
/// Get the status of a result response.
240+
fn get_result_response_status(response: &RmwResultResponse<Self>) -> i8;
241+
}
242+
243+
// Type definitions to simplify the ActionImpl trait
244+
pub type RmwServiceRequest<S> = <<S as Service>::Request as Message>::RmwMsg;
245+
pub type RmwServiceResponse<S> = <<S as Service>::Response as Message>::RmwMsg;
246+
pub type RmwGoalRequest<A> = RmwServiceRequest<<A as ActionImpl>::SendGoalService>;
247+
pub type RmwGoalResponse<A> = RmwServiceResponse<<A as ActionImpl>::SendGoalService>;
248+
pub type RmwGoalData<A> = <<A as Action>::Goal as Message>::RmwMsg;
249+
pub type RmwFeedbackData<A> = <<A as Action>::Feedback as Message>::RmwMsg;
250+
pub type RmwFeedbackMessage<A> = <<A as ActionImpl>::FeedbackMessage as Message>::RmwMsg;
251+
pub type RmwResultRequest<A> = RmwServiceRequest<<A as ActionImpl>::GetResultService>;
252+
pub type RmwResultResponse<A> = RmwServiceResponse<<A as ActionImpl>::GetResultService>;
253+
pub type RmwResultData<A> = <<A as Action>::Result as Message>::RmwMsg;

0 commit comments

Comments
 (0)
Please sign in to comment.