-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathrcl_bindings.cpp
1306 lines (1083 loc) · 46.3 KB
/
rcl_bindings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017 Intel Corporation. All rights reserved.
//
// 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_bindings.hpp"
#include <rcl/error_handling.h>
#include <rcl/expand_topic_name.h>
#include <rcl/graph.h>
#include <rcl/node.h>
#include <rcl/rcl.h>
#include <rcl/validate_topic_name.h>
#include <rmw/error_handling.h>
#include <rmw/rmw.h>
#include <rmw/validate_full_topic_name.h>
#include <rmw/validate_namespace.h>
#include <rmw/validate_node_name.h>
#include <rosidl_generator_c/string_functions.h>
#include <memory>
#include <string>
#include "handle_manager.hpp"
#include "macros.hpp"
#include "rcl_handle.hpp"
#include "rcl_utilities.hpp"
namespace rclnodejs {
rcl_guard_condition_t* g_sigint_gc = nullptr;
#ifdef OS_WINDOWS
_crt_signal_t g_original_signal_handler = NULL;
#else
sig_t g_original_signal_handler = NULL;
#endif // OS_WINDOWS
static void Catch(int signo) {
if (g_sigint_gc) {
rcl_ret_t ret = rcl_trigger_guard_condition(g_sigint_gc);
if (ret != RCL_RET_OK) {
Nan::ThrowError(rcl_get_error_string().str);
rcl_reset_error();
}
}
if (g_original_signal_handler != nullptr) {
g_original_signal_handler(signo);
}
}
std::unique_ptr<rmw_qos_profile_t> GetQoSProfile(v8::Local<v8::Value> qos);
NAN_METHOD(Init) {
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, allocator);
RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_context_t* context =
reinterpret_cast<rcl_context_t*>(context_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_init(0, nullptr, &init_options, context),
rcl_get_error_string().str);
g_sigint_gc = reinterpret_cast<rcl_guard_condition_t*>(
malloc(sizeof(rcl_guard_condition_t)));
*g_sigint_gc = rcl_get_zero_initialized_guard_condition();
rcl_guard_condition_options_t sigint_gc_options =
rcl_guard_condition_get_default_options();
rcl_guard_condition_init(g_sigint_gc, context, sigint_gc_options);
g_original_signal_handler = signal(SIGINT, Catch);
}
NAN_METHOD(CreateNode) {
std::string node_name(*v8::String::Utf8Value(info[0]));
std::string name_space(*v8::String::Utf8Value(info[1]));
RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[2]->ToObject());
rcl_context_t* context =
reinterpret_cast<rcl_context_t*>(context_handle->ptr());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(malloc(sizeof(rcl_node_t)));
*node = rcl_get_zero_initialized_node();
rcl_node_options_t options = rcl_node_get_default_options();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_node_init(node, node_name.c_str(),
name_space.c_str(), context, &options),
rcl_get_error_string().str);
auto handle = RclHandle::NewInstance(node, nullptr,
[node] { return rcl_node_fini(node); });
info.GetReturnValue().Set(handle);
}
NAN_METHOD(CreateTimer) {
int64_t period_ms = info[0]->IntegerValue();
RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[1]->ToObject());
rcl_context_t* context =
reinterpret_cast<rcl_context_t*>(context_handle->ptr());
rcl_timer_t* timer =
reinterpret_cast<rcl_timer_t*>(malloc(sizeof(rcl_timer_t)));
*timer = rcl_get_zero_initialized_timer();
rcl_clock_t* clock =
reinterpret_cast<rcl_clock_t*>(malloc(sizeof(rcl_clock_t)));
rcl_allocator_t allocator = rcl_get_default_allocator();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_init(RCL_STEADY_TIME, clock, &allocator),
rcl_get_error_string().str);
THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK,
rcl_timer_init(timer, clock, context, RCL_MS_TO_NS(period_ms), nullptr,
rcl_get_default_allocator()),
rcl_get_error_string().str);
auto js_obj = RclHandle::NewInstance(timer, nullptr, [timer, clock] {
rcl_ret_t ret_clock = rcl_clock_fini(clock);
free(clock);
rcl_ret_t ret_timer = rcl_timer_fini(timer);
return ret_clock || ret_timer;
});
info.GetReturnValue().Set(js_obj);
}
NAN_METHOD(IsTimerReady) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
bool is_ready = false;
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_timer_is_ready(timer, &is_ready),
rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::New(is_ready));
}
NAN_METHOD(CallTimer) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_timer_call(timer),
rcl_get_error_string().str);
}
NAN_METHOD(CancelTimer) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_timer_cancel(timer),
rcl_get_error_string().str);
}
NAN_METHOD(IsTimerCanceled) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
bool is_canceled = false;
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_timer_is_canceled(timer, &is_canceled),
rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::New(is_canceled));
}
NAN_METHOD(ResetTimer) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_timer_reset(timer),
rcl_get_error_string().str);
}
NAN_METHOD(TimerGetTimeUntilNextCall) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
int64_t remaining_time = 0;
THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_timer_get_time_until_next_call(timer, &remaining_time),
rcl_get_error_string().str);
info.GetReturnValue().Set(
Nan::New<v8::String>(std::to_string(RCL_NS_TO_MS(remaining_time)))
.ToLocalChecked());
}
NAN_METHOD(TimerGetTimeSinceLastCall) {
RclHandle* timer_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_timer_t* timer = reinterpret_cast<rcl_timer_t*>(timer_handle->ptr());
int64_t elapsed_time = 0;
THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_timer_get_time_since_last_call(timer, &elapsed_time),
rcl_get_error_string().str);
info.GetReturnValue().Set(
Nan::New<v8::String>(std::to_string(RCL_NS_TO_MS(elapsed_time)))
.ToLocalChecked());
}
NAN_METHOD(CreateTimePoint) {
std::string str(*v8::String::Utf8Value(info[0]));
uint32_t clock_type = info[1]->Uint32Value();
rcl_time_point_t* time_point =
reinterpret_cast<rcl_time_point_t*>(malloc(sizeof(rcl_time_point_t)));
time_point->nanoseconds = std::stoll(str);
time_point->clock_type = static_cast<rcl_clock_type_t>(clock_type);
auto js_obj = RclHandle::NewInstance(time_point, nullptr, nullptr);
info.GetReturnValue().Set(js_obj);
}
NAN_METHOD(GetNanoseconds) {
RclHandle* time_point_handle =
RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_time_point_t* time_point =
reinterpret_cast<rcl_time_point_t*>(time_point_handle->ptr());
info.GetReturnValue().Set(
Nan::New<v8::String>(std::to_string(time_point->nanoseconds))
.ToLocalChecked());
}
NAN_METHOD(CreateDuration) {
std::string str(*v8::String::Utf8Value(info[0]));
rcl_duration_t* duration =
reinterpret_cast<rcl_duration_t*>(malloc(sizeof(rcl_duration_t)));
duration->nanoseconds = std::stoll(str);
auto js_obj = RclHandle::NewInstance(duration, nullptr, nullptr);
info.GetReturnValue().Set(js_obj);
}
NAN_METHOD(GetDurationNanoseconds) {
RclHandle* duration_handle =
RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_duration_t* duration =
reinterpret_cast<rcl_duration_t*>(duration_handle->ptr());
info.GetReturnValue().Set(
Nan::New<v8::String>(std::to_string(duration->nanoseconds))
.ToLocalChecked());
}
NAN_METHOD(SetRosTimeOverrideIsEnabled) {
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(clock_handle->ptr());
bool enabled = Nan::To<bool>(info[1]).FromJust();
if (enabled) {
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_enable_ros_time_override(clock),
rcl_get_error_string().str);
} else {
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_disable_ros_time_override(clock),
rcl_get_error_string().str);
}
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(SetRosTimeOverride) {
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(clock_handle->ptr());
RclHandle* time_point_handle =
RclHandle::Unwrap<RclHandle>(info[1]->ToObject());
rcl_time_point_t* time_point =
reinterpret_cast<rcl_time_point_t*>(time_point_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_set_ros_time_override(clock, time_point->nanoseconds),
rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(GetRosTimeOverrideIsEnabled) {
RclHandle* clock_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(clock_handle->ptr());
bool is_enabled;
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_is_enabled_ros_time_override(clock, &is_enabled),
rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::New(is_enabled));
}
NAN_METHOD(CreateClock) {
auto clock_type =
static_cast<rcl_clock_type_t>(Nan::To<int32_t>(info[0]).FromJust());
rcl_clock_t* clock =
reinterpret_cast<rcl_clock_t*>(malloc(sizeof(rcl_clock_t)));
rcl_allocator_t allocator = rcl_get_default_allocator();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_init(clock_type, clock, &allocator),
rcl_get_error_string().str);
info.GetReturnValue().Set(RclHandle::NewInstance(
clock, nullptr, [clock]() { return rcl_clock_fini(clock); }));
}
static void ReturnJSTimeObj(
Nan::NAN_METHOD_ARGS_TYPE info,
int64_t nanoseconds,
rcl_clock_type_t clock_type = RCL_CLOCK_UNINITIALIZED) {
auto obj = v8::Object::New(v8::Isolate::GetCurrent());
const auto sec = static_cast<std::int32_t>(RCL_NS_TO_S(nanoseconds));
const auto nanosec =
static_cast<std::int32_t>(nanoseconds % (1000 * 1000 * 1000));
const int32_t type = clock_type;
obj->Set(Nan::New("sec").ToLocalChecked(), Nan::New(sec));
obj->Set(Nan::New("nanosec").ToLocalChecked(), Nan::New(nanosec));
if (clock_type != RCL_CLOCK_UNINITIALIZED) {
obj->Set(Nan::New("type").ToLocalChecked(), Nan::New(type));
}
info.GetReturnValue().Set(obj);
}
NAN_METHOD(ClockGetNow) {
rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
rcl_time_point_t time_point;
time_point.clock_type = clock->type;
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_get_now(clock, &time_point.nanoseconds),
rcl_get_error_string().str);
ReturnJSTimeObj(info, time_point.nanoseconds, time_point.clock_type);
}
NAN_METHOD(StaticClockGetNow) {
int32_t type = Nan::To<int32_t>(info[0]).FromJust();
if (type < RCL_ROS_TIME && type > RCL_STEADY_TIME) {
info.GetReturnValue().Set(Nan::Undefined());
return;
}
rcl_clock_t ros_clock;
rcl_time_point_t rcl_time;
rcl_allocator_t allocator = rcl_get_default_allocator();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_init(static_cast<rcl_clock_type_t>(type),
&ros_clock, &allocator),
rcl_get_error_string().str);
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_clock_get_now(&ros_clock, &rcl_time.nanoseconds),
rcl_get_error_string().str);
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, rcl_clock_fini(&ros_clock),
rcl_get_error_string().str);
ReturnJSTimeObj(info, rcl_time.nanoseconds, rcl_time.clock_type);
}
NAN_METHOD(TimeDiff) {
int64_t s_sec = Nan::To<int32_t>(info[0]).FromJust();
uint32_t s_nano = Nan::To<uint32_t>(info[1]).FromJust();
int32_t s_type = Nan::To<int32_t>(info[2]).FromJust();
int64_t f_sec = Nan::To<int32_t>(info[3]).FromJust();
uint32_t f_nano = Nan::To<uint32_t>(info[4]).FromJust();
int32_t f_type = Nan::To<int32_t>(info[5]).FromJust();
rcl_time_point_t start;
rcl_time_point_t finish;
rcl_duration_t delta;
start.nanoseconds = s_sec * 1000 * 1000 * 1000 + s_nano;
start.clock_type = static_cast<rcl_clock_type_t>(s_type);
finish.nanoseconds = f_sec * 1000 * 1000 * 1000 + f_nano;
finish.clock_type = static_cast<rcl_clock_type_t>(f_type);
auto ret = rcl_difference_times(&start, &finish, &delta);
if (ret == RCL_RET_OK) {
ReturnJSTimeObj(info, delta.nanoseconds);
return;
}
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(RclTake) {
RclHandle* subscription_handle =
RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_subscription_t* subscription =
reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
void* msg_taken = node::Buffer::Data(info[1]->ToObject());
rcl_ret_t ret = rcl_take(subscription, msg_taken, nullptr, nullptr);
if (ret != RCL_RET_OK && ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
Nan::ThrowError(rcl_get_error_string().str);
rcl_reset_error();
info.GetReturnValue().Set(Nan::False());
return;
}
if (ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
info.GetReturnValue().Set(Nan::True());
return;
}
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(CreateSubscription) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string package_name(*Nan::Utf8String(info[1]->ToString()));
std::string message_sub_folder(*Nan::Utf8String(info[2]->ToString()));
std::string message_name(*Nan::Utf8String(info[3]->ToString()));
std::string topic(*Nan::Utf8String(info[4]->ToString()));
rcl_subscription_t* subscription =
reinterpret_cast<rcl_subscription_t*>(malloc(sizeof(rcl_subscription_t)));
*subscription = rcl_get_zero_initialized_subscription();
rcl_subscription_options_t subscription_ops =
rcl_subscription_get_default_options();
auto qos_profile = GetQoSProfile(info[5]);
if (qos_profile) {
subscription_ops.qos = *qos_profile;
}
const rosidl_message_type_support_t* ts =
GetMessageTypeSupport(package_name, message_sub_folder, message_name);
if (ts) {
THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK,
rcl_subscription_init(subscription, node, ts, topic.c_str(),
&subscription_ops),
rcl_get_error_string().str);
auto js_obj =
RclHandle::NewInstance(subscription, node_handle, [subscription, node] {
return rcl_subscription_fini(subscription, node);
});
info.GetReturnValue().Set(js_obj);
} else {
Nan::ThrowError(GetErrorMessageAndClear());
}
}
NAN_METHOD(CreatePublisher) {
// Extract arguments
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string package_name(*Nan::Utf8String(info[1]->ToString()));
std::string message_sub_folder(*Nan::Utf8String(info[2]->ToString()));
std::string message_name(*Nan::Utf8String(info[3]->ToString()));
std::string topic(*Nan::Utf8String(info[4]->ToString()));
// Prepare publisher object
rcl_publisher_t* publisher =
reinterpret_cast<rcl_publisher_t*>(malloc(sizeof(rcl_publisher_t)));
*publisher = rcl_get_zero_initialized_publisher();
// Get type support object dynamically
const rosidl_message_type_support_t* ts =
GetMessageTypeSupport(package_name, message_sub_folder, message_name);
if (ts) {
// Using default options
rcl_publisher_options_t publisher_ops = rcl_publisher_get_default_options();
auto qos_profile = GetQoSProfile(info[5]);
if (qos_profile) {
publisher_ops.qos = *qos_profile;
}
// Initialize the publisher
THROW_ERROR_IF_NOT_EQUAL(
rcl_publisher_init(publisher, node, ts, topic.c_str(), &publisher_ops),
RCL_RET_OK, rcl_get_error_string().str);
// Wrap the handle into JS object
auto js_obj = RclHandle::NewInstance(
publisher, node_handle,
[publisher, node]() { return rcl_publisher_fini(publisher, node); });
// Everything is done
info.GetReturnValue().Set(js_obj);
} else {
Nan::ThrowError(GetErrorMessageAndClear());
}
}
NAN_METHOD(Publish) {
rcl_publisher_t* publisher = reinterpret_cast<rcl_publisher_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
void* buffer = node::Buffer::Data(info[1]->ToObject());
THROW_ERROR_IF_NOT_EQUAL(rcl_publish(publisher, buffer, nullptr), RCL_RET_OK,
rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(CreateClient) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string service_name(*Nan::Utf8String(info[1]->ToString()));
std::string interface_name(*Nan::Utf8String(info[2]->ToString()));
std::string package_name(*Nan::Utf8String(info[3]->ToString()));
const rosidl_service_type_support_t* ts =
GetServiceTypeSupport(package_name, interface_name);
if (ts) {
rcl_client_t* client =
reinterpret_cast<rcl_client_t*>(malloc(sizeof(rcl_client_t)));
*client = rcl_get_zero_initialized_client();
rcl_client_options_t client_ops = rcl_client_get_default_options();
auto qos_profile = GetQoSProfile(info[4]);
if (qos_profile) {
client_ops.qos = *qos_profile;
}
THROW_ERROR_IF_NOT_EQUAL(
rcl_client_init(client, node, ts, service_name.c_str(), &client_ops),
RCL_RET_OK, rcl_get_error_string().str);
auto js_obj = RclHandle::NewInstance(client, node_handle, [client, node] {
return rcl_client_fini(client, node);
});
info.GetReturnValue().Set(js_obj);
} else {
Nan::ThrowError(GetErrorMessageAndClear());
}
}
NAN_METHOD(SendRequest) {
rcl_client_t* client = reinterpret_cast<rcl_client_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
void* buffer = node::Buffer::Data(info[1]->ToObject());
int64_t sequence_number;
THROW_ERROR_IF_NOT_EQUAL(rcl_send_request(client, buffer, &sequence_number),
RCL_RET_OK, rcl_get_error_string().str);
info.GetReturnValue().Set(Nan::New((uint32_t)sequence_number));
}
NAN_METHOD(RclTakeResponse) {
rcl_client_t* client = reinterpret_cast<rcl_client_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
int64_t sequence_number = info[1]->IntegerValue();
rmw_request_id_t* header =
reinterpret_cast<rmw_request_id_t*>(malloc(sizeof(rmw_request_id_t)));
header->sequence_number = sequence_number;
void* taken_response = node::Buffer::Data(info[2]->ToObject());
rcl_ret_t ret = rcl_take_response(client, header, taken_response);
free(header);
if (ret == RCL_RET_OK) {
info.GetReturnValue().Set(Nan::True());
return;
}
rcl_reset_error();
info.GetReturnValue().Set(Nan::False());
}
NAN_METHOD(CreateService) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string service_name(*Nan::Utf8String(info[1]->ToString()));
std::string interface_name(*Nan::Utf8String(info[2]->ToString()));
std::string package_name(*Nan::Utf8String(info[3]->ToString()));
const rosidl_service_type_support_t* ts =
GetServiceTypeSupport(package_name, interface_name);
if (ts) {
rcl_service_t* service =
reinterpret_cast<rcl_service_t*>(malloc(sizeof(rcl_service_t)));
*service = rcl_get_zero_initialized_service();
rcl_service_options_t service_ops = rcl_service_get_default_options();
auto qos_profile = GetQoSProfile(info[4]);
if (qos_profile) {
service_ops.qos = *qos_profile;
}
THROW_ERROR_IF_NOT_EQUAL(
rcl_service_init(service, node, ts, service_name.c_str(), &service_ops),
RCL_RET_OK, rcl_get_error_string().str);
auto js_obj = RclHandle::NewInstance(service, node_handle, [service, node] {
return rcl_service_fini(service, node);
});
info.GetReturnValue().Set(js_obj);
} else {
Nan::ThrowError(GetErrorMessageAndClear());
}
}
NAN_METHOD(RclTakeRequest) {
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
rmw_request_id_t* header =
reinterpret_cast<rmw_request_id_t*>(malloc(sizeof(rmw_request_id_t)));
void* taken_request = node::Buffer::Data(info[2]->ToObject());
rcl_ret_t ret = rcl_take_request(service, header, taken_request);
if (ret != RCL_RET_SERVICE_TAKE_FAILED) {
auto js_obj = RclHandle::NewInstance(header);
info.GetReturnValue().Set(js_obj);
return;
}
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(SendResponse) {
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
RclHandle::Unwrap<RclHandle>(info[0]->ToObject())->ptr());
void* buffer = node::Buffer::Data(info[1]->ToObject());
rmw_request_id_t* header = reinterpret_cast<rmw_request_id_t*>(
RclHandle::Unwrap<RclHandle>(info[2]->ToObject())->ptr());
THROW_ERROR_IF_NOT_EQUAL(rcl_send_response(service, header, buffer),
RCL_RET_OK, rcl_get_error_string().str);
}
NAN_METHOD(ValidateFullTopicName) {
int validation_result;
size_t invalid_index;
std::string topic_name(*Nan::Utf8String(info[0]->ToString()));
rmw_ret_t ret = rmw_validate_full_topic_name(
topic_name.c_str(), &validation_result, &invalid_index);
if (ret != RMW_RET_OK) {
if (ret == RMW_RET_BAD_ALLOC) {
Nan::ThrowError(rmw_get_error_string().str);
}
rmw_reset_error();
return info.GetReturnValue().Set(Nan::Undefined());
}
if (validation_result == RMW_NAMESPACE_VALID) {
info.GetReturnValue().Set(Nan::Null());
return;
}
const char* validation_message =
rmw_full_topic_name_validation_result_string(validation_result);
THROW_ERROR_IF_EQUAL(nullptr, validation_message,
"Unable to get validation error message");
v8::Local<v8::Array> result_list = Nan::New<v8::Array>(2);
Nan::Set(
result_list, 0,
Nan::New<v8::String>(std::string(validation_message)).ToLocalChecked());
Nan::Set(result_list, 1, Nan::New((int32_t)invalid_index));
info.GetReturnValue().Set(result_list);
}
NAN_METHOD(ValidateNodeName) {
int validation_result;
size_t invalid_index;
std::string node_name(*Nan::Utf8String(info[0]->ToString()));
rmw_ret_t ret = rmw_validate_node_name(node_name.c_str(), &validation_result,
&invalid_index);
if (ret != RMW_RET_OK) {
if (ret == RMW_RET_BAD_ALLOC) {
Nan::ThrowError(rmw_get_error_string().str);
}
rmw_reset_error();
return info.GetReturnValue().Set(Nan::Undefined());
}
if (validation_result == RMW_NODE_NAME_VALID) {
info.GetReturnValue().Set(Nan::Null());
return;
}
const char* validation_message =
rmw_node_name_validation_result_string(validation_result);
THROW_ERROR_IF_EQUAL(nullptr, validation_message,
"Unable to get validation error message");
v8::Local<v8::Array> result_list = Nan::New<v8::Array>(2);
Nan::Set(
result_list, 0,
Nan::New<v8::String>(std::string(validation_message)).ToLocalChecked());
Nan::Set(result_list, 1, Nan::New((int32_t)invalid_index));
info.GetReturnValue().Set(result_list);
}
NAN_METHOD(ValidateTopicName) {
int validation_result;
size_t invalid_index;
std::string topic_name(*Nan::Utf8String(info[0]->ToString()));
rmw_ret_t ret = rcl_validate_topic_name(topic_name.c_str(),
&validation_result, &invalid_index);
if (ret != RMW_RET_OK) {
if (ret == RMW_RET_BAD_ALLOC) {
Nan::ThrowError(rmw_get_error_string().str);
}
rmw_reset_error();
return info.GetReturnValue().Set(Nan::Undefined());
}
if (validation_result == RMW_NODE_NAME_VALID) {
info.GetReturnValue().Set(Nan::Null());
return;
}
const char* validation_message =
rcl_topic_name_validation_result_string(validation_result);
THROW_ERROR_IF_EQUAL(nullptr, validation_message,
"Unable to get validation error message");
v8::Local<v8::Array> result_list = Nan::New<v8::Array>(2);
Nan::Set(
result_list, 0,
Nan::New<v8::String>(std::string(validation_message)).ToLocalChecked());
Nan::Set(result_list, 1, Nan::New((int32_t)invalid_index));
info.GetReturnValue().Set(result_list);
}
NAN_METHOD(ValidateNamespace) {
int validation_result;
size_t invalid_index;
std::string namespace_name(*Nan::Utf8String(info[0]->ToString()));
rmw_ret_t ret = rmw_validate_namespace(namespace_name.c_str(),
&validation_result, &invalid_index);
if (ret != RMW_RET_OK) {
if (ret == RMW_RET_BAD_ALLOC) {
Nan::ThrowError(rmw_get_error_string().str);
}
rmw_reset_error();
return info.GetReturnValue().Set(Nan::Undefined());
}
if (validation_result == RMW_NODE_NAME_VALID) {
info.GetReturnValue().Set(Nan::Null());
return;
}
const char* validation_message =
rmw_namespace_validation_result_string(validation_result);
THROW_ERROR_IF_EQUAL(nullptr, validation_message,
"Unable to get validation error message");
v8::Local<v8::Array> result_list = Nan::New<v8::Array>(2);
Nan::Set(
result_list, 0,
Nan::New<v8::String>(std::string(validation_message)).ToLocalChecked());
Nan::Set(result_list, 1, Nan::New((int32_t)invalid_index));
info.GetReturnValue().Set(result_list);
}
NAN_METHOD(ExpandTopicName) {
std::string topic_name(*Nan::Utf8String(info[0]->ToString()));
std::string node_name(*Nan::Utf8String(info[1]->ToString()));
std::string node_namespace(*Nan::Utf8String(info[2]->ToString()));
char* expanded_topic = nullptr;
rcl_allocator_t allocator = rcl_get_default_allocator();
rcutils_allocator_t rcutils_allocator = rcutils_get_default_allocator();
rcutils_string_map_t substitutions_map =
rcutils_get_zero_initialized_string_map();
rcutils_ret_t rcutils_ret =
rcutils_string_map_init(&substitutions_map, 0, rcutils_allocator);
if (rcutils_ret != RCUTILS_RET_OK) {
if (rcutils_ret == RCUTILS_RET_BAD_ALLOC) {
Nan::ThrowError(rcutils_get_error_string().str);
}
rcutils_reset_error();
info.GetReturnValue().Set(Nan::Undefined());
return;
}
rcl_ret_t ret = rcl_get_default_topic_name_substitutions(&substitutions_map);
if (ret != RCL_RET_OK) {
if (ret == RCL_RET_BAD_ALLOC) {
Nan::ThrowError(rcl_get_error_string().str);
}
rcl_reset_error();
rcutils_ret = rcutils_string_map_fini(&substitutions_map);
if (rcutils_ret != RCUTILS_RET_OK) {
Nan::ThrowError(rcutils_get_error_string().str);
rcutils_reset_error();
}
info.GetReturnValue().Set(Nan::Undefined());
return;
}
ret = rcl_expand_topic_name(topic_name.c_str(), node_name.c_str(),
node_namespace.c_str(), &substitutions_map,
allocator, &expanded_topic);
rcutils_ret = rcutils_string_map_fini(&substitutions_map);
if (rcutils_ret != RCUTILS_RET_OK) {
Nan::ThrowError(rcutils_get_error_string().str);
rcutils_reset_error();
allocator.deallocate(expanded_topic, allocator.state);
info.GetReturnValue().Set(Nan::Undefined());
return;
}
if (ret != RCL_RET_OK) {
Nan::ThrowError(rcl_get_error_string().str);
rcl_reset_error();
info.GetReturnValue().Set(Nan::Undefined());
return;
}
if (!expanded_topic) {
info.GetReturnValue().Set(Nan::Undefined());
return;
}
rcl_allocator_t topic_allocator = rcl_get_default_allocator();
std::string topic(expanded_topic);
allocator.deallocate(expanded_topic, topic_allocator.state);
info.GetReturnValue().Set(Nan::New<v8::String>(topic).ToLocalChecked());
}
NAN_METHOD(GetNodeName) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
const char* node_name = rcl_node_get_name(node);
if (!node_name) {
info.GetReturnValue().Set(Nan::Undefined());
} else {
info.GetReturnValue().Set(Nan::New<v8::String>(node_name).ToLocalChecked());
}
}
NAN_METHOD(GetNamespace) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
const char* node_namespace = rcl_node_get_namespace(node);
if (!node_namespace) {
info.GetReturnValue().Set(Nan::Undefined());
} else {
info.GetReturnValue().Set(
Nan::New<v8::String>(node_namespace).ToLocalChecked());
}
}
const rmw_qos_profile_t* GetQoSProfileFromString(const std::string& profile) {
const rmw_qos_profile_t* qos_profile = nullptr;
if (profile == "qos_profile_sensor_data") {
qos_profile = &rmw_qos_profile_sensor_data;
} else if (profile == "qos_profile_system_default") {
qos_profile = &rmw_qos_profile_system_default;
} else if (profile == "qos_profile_services_default") {
qos_profile = &rmw_qos_profile_services_default;
} else if (profile == "qos_profile_parameters") {
qos_profile = &rmw_qos_profile_parameters;
} else if (profile == "qos_profile_parameter_events") {
qos_profile = &rmw_qos_profile_parameter_events;
} else {
return &rmw_qos_profile_default;
}
return qos_profile;
}
std::unique_ptr<rmw_qos_profile_t> GetQosProfileFromObject(
v8::Local<v8::Object> object) {
std::unique_ptr<rmw_qos_profile_t> qos_profile =
std::make_unique<rmw_qos_profile_t>();
qos_profile->history = static_cast<rmw_qos_history_policy_t>(
object->Get(Nan::New("history").ToLocalChecked())->Uint32Value());
qos_profile->depth =
object->Get(Nan::New("depth").ToLocalChecked())->Uint32Value();
qos_profile->reliability = static_cast<rmw_qos_reliability_policy_t>(
object->Get(Nan::New("reliability").ToLocalChecked())->Uint32Value());
qos_profile->durability = static_cast<rmw_qos_durability_policy_t>(
object->Get(Nan::New("durability").ToLocalChecked())->Uint32Value());
qos_profile->avoid_ros_namespace_conventions =
object->Get(Nan::New("avoidRosNameSpaceConventions").ToLocalChecked())
->BooleanValue();
return qos_profile;
}
std::unique_ptr<rmw_qos_profile_t> GetQoSProfile(v8::Local<v8::Value> qos) {
std::unique_ptr<rmw_qos_profile_t> qos_profile =
std::make_unique<rmw_qos_profile_t>();
if (qos->IsString()) {
*qos_profile = *GetQoSProfileFromString(
std::string(*Nan::Utf8String(qos->ToString())));
} else if (qos->IsObject()) {
qos_profile = GetQosProfileFromObject(qos->ToObject());
} else {
return qos_profile;
}
return qos_profile;
}
int DestroyContext(rcl_context_t* context) {
rcl_ret_t ret = RCL_RET_OK;
if (context->impl) {
if (rcl_context_is_valid(context)) {
if (RCL_RET_OK != rcl_shutdown(context)) {
Nan::ThrowError(rcl_get_error_string().str);
}
ret = rcl_context_fini(context);
}
}
return ret;
}
NAN_METHOD(Shutdown) {
RclHandle* context_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_context_t* context =
reinterpret_cast<rcl_context_t*>(context_handle->ptr());
THROW_ERROR_IF_NOT_EQUAL(rcl_shutdown(context), RCL_RET_OK,
rcl_get_error_string().str);
if (g_sigint_gc) {
rcl_guard_condition_fini(g_sigint_gc);
free(g_sigint_gc);
g_sigint_gc = nullptr;
}
signal(SIGINT, g_original_signal_handler);
g_original_signal_handler = nullptr;
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(InitString) {
void* buffer = node::Buffer::Data(info[0]->ToObject());
rosidl_generator_c__String* ptr =
reinterpret_cast<rosidl_generator_c__String*>(buffer);
rosidl_generator_c__String__init(ptr);
info.GetReturnValue().Set(Nan::Undefined());
}
inline char* GetBufAddr(v8::Local<v8::Value> buf) {
return node::Buffer::Data(buf.As<v8::Object>());
}
NAN_METHOD(FreeMemeoryAtOffset) {
v8::Local<v8::Value> buf = info[0];
if (!node::Buffer::HasInstance(buf)) {
return Nan::ThrowTypeError("Buffer instance expected as first argument");
}
int64_t offset =
info[1]->IsNumber() ? Nan::To<int64_t>(info[1]).FromJust() : 0;
auto ptr = GetBufAddr(buf) + offset;
if (ptr == nullptr) {
return Nan::ThrowError("Cannot read from NULL pointer");
}
char* val = *reinterpret_cast<char**>(ptr);
free(val);
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(CreateArrayBufferFromAddress) {
auto address = GetBufAddr(info[0]);
int32_t length = Nan::To<int32_t>(info[1]).FromJust();
auto array_buffer =
v8::ArrayBuffer::New(v8::Isolate::GetCurrent(), address, length,
v8::ArrayBufferCreationMode::kExternalized);
info.GetReturnValue().Set(array_buffer);
}
NAN_METHOD(CreateArrayBufferCleaner) {
auto address = GetBufAddr(info[0]);