|
16 | 16 |
|
17 | 17 | #include <rcl/error_handling.h> |
18 | 18 | #include <rcl/rcl.h> |
| 19 | +#include <rmw/types.h> |
19 | 20 |
|
20 | 21 | #include <cstdio> |
21 | 22 | #include <memory> |
@@ -53,6 +54,53 @@ Napi::Value RclTake(const Napi::CallbackInfo& info) { |
53 | 54 | return env.Undefined(); |
54 | 55 | } |
55 | 56 |
|
| 57 | +Napi::Value RclTakeWithInfo(const Napi::CallbackInfo& info) { |
| 58 | + Napi::Env env = info.Env(); |
| 59 | + |
| 60 | + RclHandle* subscription_handle = |
| 61 | + RclHandle::Unwrap(info[0].As<Napi::Object>()); |
| 62 | + rcl_subscription_t* subscription = |
| 63 | + reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr()); |
| 64 | + void* msg_taken = info[1].As<Napi::Buffer<char>>().Data(); |
| 65 | + |
| 66 | + rmw_message_info_t message_info = rmw_get_zero_initialized_message_info(); |
| 67 | + rcl_ret_t ret = rcl_take(subscription, msg_taken, &message_info, nullptr); |
| 68 | + |
| 69 | + if (ret != RCL_RET_OK && ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) { |
| 70 | + std::string error_string = rcl_get_error_string().str; |
| 71 | + rcl_reset_error(); |
| 72 | + Napi::Error::New(env, error_string).ThrowAsJavaScriptException(); |
| 73 | + return env.Undefined(); |
| 74 | + } |
| 75 | + |
| 76 | + if (ret == RCL_RET_SUBSCRIPTION_TAKE_FAILED) { |
| 77 | + return env.Undefined(); |
| 78 | + } |
| 79 | + |
| 80 | + // Build JS object with message info fields |
| 81 | + Napi::Object js_info = Napi::Object::New(env); |
| 82 | + js_info.Set("source_timestamp", |
| 83 | + Napi::BigInt::New(env, message_info.source_timestamp)); |
| 84 | + js_info.Set("received_timestamp", |
| 85 | + Napi::BigInt::New(env, message_info.received_timestamp)); |
| 86 | + js_info.Set( |
| 87 | + "publication_sequence_number", |
| 88 | + Napi::BigInt::New( |
| 89 | + env, static_cast<int64_t>(message_info.publication_sequence_number))); |
| 90 | + js_info.Set( |
| 91 | + "reception_sequence_number", |
| 92 | + Napi::BigInt::New( |
| 93 | + env, static_cast<int64_t>(message_info.reception_sequence_number))); |
| 94 | + |
| 95 | + // Publisher GID as Buffer |
| 96 | + auto gid_buf = |
| 97 | + Napi::Buffer<uint8_t>::Copy(env, message_info.publisher_gid.data, |
| 98 | + sizeof(message_info.publisher_gid.data)); |
| 99 | + js_info.Set("publisher_gid", gid_buf); |
| 100 | + |
| 101 | + return js_info; |
| 102 | +} |
| 103 | + |
56 | 104 | Napi::Value CreateSubscription(const Napi::CallbackInfo& info) { |
57 | 105 | Napi::Env env = info.Env(); |
58 | 106 |
|
@@ -422,6 +470,7 @@ Napi::Value GetPublisherCount(const Napi::CallbackInfo& info) { |
422 | 470 |
|
423 | 471 | Napi::Object InitSubscriptionBindings(Napi::Env env, Napi::Object exports) { |
424 | 472 | exports.Set("rclTake", Napi::Function::New(env, RclTake)); |
| 473 | + exports.Set("rclTakeWithInfo", Napi::Function::New(env, RclTakeWithInfo)); |
425 | 474 | exports.Set("createSubscription", |
426 | 475 | Napi::Function::New(env, CreateSubscription)); |
427 | 476 | exports.Set("rclTakeRaw", Napi::Function::New(env, RclTakeRaw)); |
|
0 commit comments