|
| 1 | +// Copyright (c) 2025, The Robot Web Tools Contributors |
| 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 | +#include "rcl_serialization_bindings.h" |
| 16 | + |
| 17 | +#include <rmw/rmw.h> |
| 18 | +#include <rmw/serialized_message.h> |
| 19 | +#include <rosidl_runtime_c/message_type_support_struct.h> |
| 20 | + |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "rcl_utilities.h" |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +struct SerializedMessage { |
| 28 | + explicit SerializedMessage(Napi::Env env, rcutils_allocator_t allocator) |
| 29 | + : env(env) { |
| 30 | + rcl_msg = rmw_get_zero_initialized_serialized_message(); |
| 31 | + rcutils_ret_t rcutils_ret = |
| 32 | + rmw_serialized_message_init(&rcl_msg, 0u, &allocator); |
| 33 | + if (RCUTILS_RET_OK != rcutils_ret) { |
| 34 | + Napi::Error::New(env, "failed to initialize serialized message") |
| 35 | + .ThrowAsJavaScriptException(); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + ~SerializedMessage() { |
| 40 | + rcutils_ret_t ret = rmw_serialized_message_fini(&rcl_msg); |
| 41 | + if (RCUTILS_RET_OK != ret) { |
| 42 | + Napi::Error::New(env, |
| 43 | + "failed to fini rcl_serialized_msg_t in destructor.") |
| 44 | + .ThrowAsJavaScriptException(); |
| 45 | + rcutils_reset_error(); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + rcl_serialized_message_t rcl_msg; |
| 50 | + Napi::Env env; |
| 51 | +}; |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
| 55 | +namespace rclnodejs { |
| 56 | + |
| 57 | +Napi::Value Serialize(const Napi::CallbackInfo& info) { |
| 58 | + Napi::Env env = info.Env(); |
| 59 | + |
| 60 | + std::string package_name = info[0].As<Napi::String>().Utf8Value(); |
| 61 | + std::string message_sub_folder = info[1].As<Napi::String>().Utf8Value(); |
| 62 | + std::string message_name = info[2].As<Napi::String>().Utf8Value(); |
| 63 | + void* ros_msg = info[3].As<Napi::Buffer<char>>().Data(); |
| 64 | + const rosidl_message_type_support_t* ts = |
| 65 | + GetMessageTypeSupport(package_name, message_sub_folder, message_name); |
| 66 | + |
| 67 | + // Create a serialized message object. |
| 68 | + SerializedMessage serialized_msg(env, rcutils_get_default_allocator()); |
| 69 | + |
| 70 | + // Serialize |
| 71 | + rmw_ret_t rmw_ret = rmw_serialize(ros_msg, ts, &serialized_msg.rcl_msg); |
| 72 | + if (RMW_RET_OK != rmw_ret) { |
| 73 | + Napi::Error::New(env, "Failed to serialize ROS message") |
| 74 | + .ThrowAsJavaScriptException(); |
| 75 | + return env.Undefined(); |
| 76 | + } |
| 77 | + Napi::Buffer<char> buffer = Napi::Buffer<char>::Copy( |
| 78 | + env, reinterpret_cast<const char*>(serialized_msg.rcl_msg.buffer), |
| 79 | + serialized_msg.rcl_msg.buffer_length); |
| 80 | + return buffer; |
| 81 | +} |
| 82 | + |
| 83 | +Napi::Value Deserialize(const Napi::CallbackInfo& info) { |
| 84 | + Napi::Env env = info.Env(); |
| 85 | + |
| 86 | + std::string package_name = info[0].As<Napi::String>().Utf8Value(); |
| 87 | + std::string message_sub_folder = info[1].As<Napi::String>().Utf8Value(); |
| 88 | + std::string message_name = info[2].As<Napi::String>().Utf8Value(); |
| 89 | + const rosidl_message_type_support_t* ts = |
| 90 | + GetMessageTypeSupport(package_name, message_sub_folder, message_name); |
| 91 | + Napi::Buffer<char> serialized = info[3].As<Napi::Buffer<char>>(); |
| 92 | + void* msg_taken = info[4].As<Napi::Buffer<char>>().Data(); |
| 93 | + |
| 94 | + // Create a serialized message object |
| 95 | + rcl_serialized_message_t serialized_msg = |
| 96 | + rmw_get_zero_initialized_serialized_message(); |
| 97 | + // Just copy pointer to avoid extra allocation and copy |
| 98 | + serialized_msg.buffer_capacity = serialized.Length(); |
| 99 | + serialized_msg.buffer_length = serialized.Length(); |
| 100 | + serialized_msg.buffer = reinterpret_cast<uint8_t*>(serialized.Data()); |
| 101 | + |
| 102 | + // Deserialize |
| 103 | + rmw_ret_t rmw_ret = rmw_deserialize(&serialized_msg, ts, msg_taken); |
| 104 | + |
| 105 | + if (RMW_RET_OK != rmw_ret) { |
| 106 | + Napi::Error::New(env, "failed to deserialize ROS message") |
| 107 | + .ThrowAsJavaScriptException(); |
| 108 | + } |
| 109 | + |
| 110 | + return env.Undefined(); |
| 111 | +} |
| 112 | + |
| 113 | +Napi::Object InitSerializationBindings(Napi::Env env, Napi::Object exports) { |
| 114 | + exports.Set("serialize", Napi::Function::New(env, Serialize)); |
| 115 | + exports.Set("deserialize", Napi::Function::New(env, Deserialize)); |
| 116 | + return exports; |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace rclnodejs |
0 commit comments