-
Notifications
You must be signed in to change notification settings - Fork 79
Add RMW serialize and deserialize functions #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) 2025, The Robot Web Tools Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const rclnodejs = require('bindings')('rclnodejs'); | ||
|
|
||
| class Serialization { | ||
| /** | ||
| * Serialize a message to a buffer. | ||
| * @param {object} message - The message to serialize. | ||
| * @param {function} typeClass - The class of the message type to serialize. | ||
| * @return {Buffer} The serialized message as a Buffer. | ||
| */ | ||
| static serializeMessage(message, typeClass) { | ||
| if (!(message instanceof typeClass)) { | ||
| throw new TypeError('Message must be a valid ros2 message type'); | ||
| } | ||
| return rclnodejs.serialize( | ||
| typeClass.type().pkgName, | ||
| typeClass.type().subFolder, | ||
| typeClass.type().interfaceName, | ||
| message.serialize() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Deserialize a message from a buffer. | ||
| * @param {Buffer} buffer - The buffer containing the serialized message. | ||
| * @param {function} typeClass - The class of the message type to deserialize into. | ||
| * @return {object} The deserialized message object. | ||
| */ | ||
| static deserializeMessage(buffer, typeClass) { | ||
| if (!(buffer instanceof Buffer)) { | ||
| throw new TypeError('Buffer is required for deserialization'); | ||
| } | ||
| const rosMsg = new typeClass(); | ||
| rclnodejs.deserialize( | ||
| typeClass.type().pkgName, | ||
| typeClass.type().subFolder, | ||
| typeClass.type().interfaceName, | ||
| buffer, | ||
| rosMsg.toRawROS() | ||
| ); | ||
| return rosMsg; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Serialization; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||
| // Copyright (c) 2025, The Robot Web Tools Contributors | ||||||
| // | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| // you may not use this file except in compliance with the License. | ||||||
| // You may obtain a copy of the License at | ||||||
| // | ||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| // | ||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| // See the License for the specific language governing permissions and | ||||||
| // limitations under the License. | ||||||
|
|
||||||
| #include "rcl_serialization_bindings.h" | ||||||
|
|
||||||
| #include <rmw/rmw.h> | ||||||
| #include <rmw/serialized_message.h> | ||||||
| #include <rosidl_runtime_c/message_type_support_struct.h> | ||||||
|
|
||||||
| #include <string> | ||||||
|
|
||||||
| #include "rcl_utilities.h" | ||||||
|
|
||||||
| namespace { | ||||||
|
|
||||||
| struct SerializedMessage { | ||||||
| explicit SerializedMessage(Napi::Env env, rcutils_allocator_t allocator) | ||||||
| : env(env) { | ||||||
| rcl_msg = rmw_get_zero_initialized_serialized_message(); | ||||||
| rcutils_ret_t rcutils_ret = | ||||||
| rmw_serialized_message_init(&rcl_msg, 0u, &allocator); | ||||||
| if (RCUTILS_RET_OK != rcutils_ret) { | ||||||
| Napi::Error::New(env, "failed to initialize serialized message") | ||||||
| .ThrowAsJavaScriptException(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| ~SerializedMessage() { | ||||||
| rcutils_ret_t ret = rmw_serialized_message_fini(&rcl_msg); | ||||||
| if (RCUTILS_RET_OK != ret) { | ||||||
| Napi::Error::New(env, | ||||||
| "failed to fini rcl_serialized_msg_t in destructor.") | ||||||
| .ThrowAsJavaScriptException(); | ||||||
|
||||||
| .ThrowAsJavaScriptException(); | |
| .Value(); // Log the error without throwing an exception. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) 2025, The Robot Web Tools Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef SRC_RCL_SERIALIZATION_BINDINGS_H_ | ||
| #define SRC_RCL_SERIALIZATION_BINDINGS_H_ | ||
|
|
||
| #include <napi.h> | ||
|
|
||
| namespace rclnodejs { | ||
|
|
||
| Napi::Object InitSerializationBindings(Napi::Env env, Napi::Object exports); | ||
|
|
||
| } | ||
|
|
||
| #endif // SRC_RCL_SERIALIZATION_BINDINGS_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) 2025, The Robot Web Tools Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| const assert = require('assert'); | ||
| const rclnodejs = require('../index.js'); | ||
| const { serializeMessage, deserializeMessage } = require('../index.js'); | ||
|
|
||
| describe('rclnodejs publisher test suite', function () { | ||
| [ | ||
| { | ||
| type: 'std_msgs/msg/String', | ||
| value: 'Hello ROS 2.0 Serialization!', | ||
| }, | ||
| { | ||
| type: 'std_msgs/msg/MultiArrayDimension', | ||
| value: { label: 'label name 0', size: 256, stride: 4 }, | ||
| }, | ||
| { | ||
| type: 'geometry_msgs/msg/Point', | ||
| value: { x: 1.5, y: 2.75, z: 3.0 }, | ||
| }, | ||
| ].forEach((testCase) => { | ||
| it('Test serialize a message of type ' + testCase.type, function () { | ||
| const MyMessage = rclnodejs.require(testCase.type); | ||
| const rosMsg = new MyMessage(testCase.value); | ||
| const buffer = serializeMessage(rosMsg, MyMessage); | ||
|
|
||
| assert(buffer instanceof Buffer); | ||
| const deserializedRosMsg = deserializeMessage(buffer, MyMessage); | ||
| assert.deepStrictEqual( | ||
| deserializedRosMsg.toPlainObject(), | ||
| rosMsg.toPlainObject() | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing exceptions from a destructor can lead to termination or unexpected behavior during object cleanup. Consider logging the error or handling the failure without throwing in the destructor.