Skip to content

Update rosidl cpp path to emit rosidl::Buffer for uint8[] type#942

Merged
sloretz merged 3 commits intorollingfrom
native_buffer/2-rosidl_cpp
Apr 6, 2026
Merged

Update rosidl cpp path to emit rosidl::Buffer for uint8[] type#942
sloretz merged 3 commits intorollingfrom
native_buffer/2-rosidl_cpp

Conversation

@nvcyc
Copy link
Copy Markdown
Contributor

@nvcyc nvcyc commented Mar 15, 2026

Description

This pull request makes the C++ rosidl layer Buffer-aware, enabling rclcpp message types to use rosidl::Buffer<uint8_t> for uint8[] fields. It modifies rosidl_runtime_cpp, rosidl_generator_cpp, and rosidl_typesupport_introspection_cpp so that generated C++ messages, trait utilities, and introspection metadata all understand the new Buffer type introduced in PR #941.

This pull request consists of the following key changes:

  • rosidl_runtime_cpp: Added trait specializations for Buffer<T> and a dependency on rosidl_buffer.
  • rosidl_generator_cpp: The code generator now emits rosidl::Buffer<uint8_t> instead of std::vector<uint8_t> for uint8[] message fields.
  • rosidl_typesupport_introspection_cpp: The goal for the changes in C++ introspection is to keep it working with CPU-based buffers. The buffer-based introspection accessors throw exceptions for non-CPU backends.

Is this user-facing behavior change?

With this PR, all C++ message types with uint8[] fields (e.g., sensor_msgs::msg::Image::data) will have their field type changed from std::vector<uint8_t> to rosidl::Buffer<uint8_t>. By default, a CPU-based Buffer<T> type is used, which is a transparent drop-in replacement for std::vector<T>, so existing user code should compile and work unchanged.

Did you use Generative AI?

Yes. Claude (claude-4.6-opus) via Cursor was used to assist with creating an initial prototype version of the changes contained in this PR.

Additional Information

This PR is part of the broader ROS 2 native buffer feature introduced in this post. It depends on PR #941 (rosidl_buffer + rosidl_buffer_backend).

Copy link
Copy Markdown
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass.

@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch from 0f9fd11 to 986d123 Compare March 19, 2026 08:52
@nvcyc nvcyc requested a review from hidmic March 20, 2026 05:27
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch from 3085e51 to f06ac85 Compare March 22, 2026 08:41
@nvcyc nvcyc force-pushed the native_buffer/1-rosidl_buffer branch from 326b4f8 to c53b788 Compare March 22, 2026 17:55
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch 2 times, most recently from 20ba671 to 4623bdf Compare March 29, 2026 04:26
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch from 4623bdf to d04fd63 Compare March 29, 2026 18:57
@nvcyc nvcyc force-pushed the native_buffer/1-rosidl_buffer branch from a19b54f to 6ec582e Compare March 29, 2026 19:25
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch 3 times, most recently from c3f83ab to a79158a Compare March 29, 2026 23:40
Base automatically changed from native_buffer/1-rosidl_buffer to rolling March 31, 2026 07:27
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch 2 times, most recently from 57e8d58 to 2bd85fd Compare April 1, 2026 06:59
@ahcorde
Copy link
Copy Markdown
Contributor

ahcorde commented Apr 1, 2026

Pulls: #942
Gist: https://gist.githubusercontent.com/ahcorde/020ed9cf4a4644358e26421045421eab/raw/4e1386848d2131706907b422cd4346e61f320b21/ros2.repos
BUILD args: --packages-above-and-dependencies rosidl_generator_cpp rosidl_runtime_cpp rosidl_typesupport_introspection_cpp
TEST args: --packages-above rosidl_generator_cpp rosidl_runtime_cpp rosidl_typesupport_introspection_cpp
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/18787

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@nvcyc
Copy link
Copy Markdown
Contributor Author

nvcyc commented Apr 1, 2026

This PR depends on ros2/rosidl_typesupport_fastrtps#144 so can't be CI-tested alone.
I've triggered CI tests in its downstream PR #943 with all the dependencies which will also test changes in this PR.

Copy link
Copy Markdown
Member

@wjwwood wjwwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@nvcyc
Copy link
Copy Markdown
Contributor Author

nvcyc commented Apr 4, 2026

Triggered with custom ros2.repos that contain all PRs: https://raw.githubusercontent.com/nvcyc/ros2/refs/heads/rolling-native-buffer-prs/ros2.repos

Started by user CY Chen
Running as SYSTEM
Building on the built-in node in workspace /var/lib/jenkins/workspace/ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/18827

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Signed-off-by: CY Chen <cyc@nvidia.com>
@nvcyc nvcyc force-pushed the native_buffer/2-rosidl_cpp branch from 2bd85fd to 3a8edd9 Compare April 4, 2026 17:11
* Add support for rosidl::Buffer in rosidl C path for rclpy

Signed-off-by: CY Chen <cyc@nvidia.com>

* Add rosidl_buffer_py to rosidl_runtime_packages group

Signed-off-by: CY Chen <cyc@nvidia.com>

* Remove unused has_buffer_fields_ field from introspection C

Signed-off-by: CY Chen <cyc@nvidia.com>

* Simplify rosidl_buffer Python bindings

Signed-off-by: CY Chen <cyc@nvidia.com>

* Add owns_rosidl_buffer in sequence struct to handle buffer lifecycle

Signed-off-by: CY Chen <cyc@nvidia.com>

* Add Python type stub for Buffer class

Signed-off-by: CY Chen <cyc@nvidia.com>

* Remove unused test assertion for introspection

Signed-off-by: CY Chen <cyc@nvidia.com>

* Add comments for the limitation of _take_buffer_from_ptr

Signed-off-by: CY Chen <cyc@nvidia.com>

* Revert adding a line in msg__functions.c.em

Signed-off-by: CY Chen <cyc@nvidia.com>

* Update rosidl_buffer/c_helpers.h to use the right comment style

Signed-off-by: CY Chen <cyc@nvidia.com>

---------

Signed-off-by: CY Chen <cyc@nvidia.com>
@sloretz sloretz merged commit 334dee9 into rolling Apr 6, 2026
3 of 4 checks passed
@sloretz sloretz deleted the native_buffer/2-rosidl_cpp branch April 6, 2026 23:02
minggangw added a commit to RobotWebTools/rclnodejs that referenced this pull request Apr 8, 2026
)

ROS 2 Rolling merged ros2/rosidl#941 and ros2/rosidl#942 as part of the native buffer feature. The ROSIDL_RUNTIME_C__PRIMITIVE_SEQUENCE macro now emits two extra boolean fields (is_rosidl_buffer, owns_rosidl_buffer) in every primitive sequence struct, growing them from 24 to 32 bytes. Non-primitive sequences (e.g. Parameter__Sequence) are unchanged at 24 bytes.

The rclnodejs code generator produces ref-struct-di definitions for these sequence types. Because the generated layout lacked the new fields, every message containing a primitive sequence had misaligned field offsets when serialized, causing Fast DDS to segfault in get_serialized_size_* during rcl_publish.

Changes:

1. rosidl_gen/templates/message-template.js — Detect Rolling+ at generation time via DistroUtils.getDistroId() and compute needsRosidlBufferFields once per message. Conditionally emit is_rosidl_buffer and owns_rosidl_buffer in the generated sequence struct only for primitive-package types (Bool, Byte, Int8, …, Float64, String) on Rolling+. Generated files get clean, static struct definitions with no runtime require() or ternary.

2. .github/workflows/linux-x64-build-and-test.yml — Add rosidl_buffer_py and pybind11 to rosdep --skip-keys for the Rolling nightly install, since these new packages from the native buffer feature have unresolvable rosdep keys in the tarball context.

Verified: Rolling: test-parameters (33), test-parameter-service (9), test-action-client (19) — all pass. Jazzy: test-parameters (33), test-parameter-service (9), test-action-client (18+1 pending) — all pass.

Fix: #1481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants