-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrtps_bindings.cpp
More file actions
640 lines (610 loc) · 25.9 KB
/
Copy pathrtps_bindings.cpp
File metadata and controls
640 lines (610 loc) · 25.9 KB
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
// Hand-written pybind11 bindings for espp::RtpsParticipant (the facade over the
// embeddedRTPS engine in components/rtps — see its REFACTOR_PLAN.md).
//
// Why hand-written (like cdr): the participant exposes std::function callbacks
// taking std::span<const uint8_t> (no pybind caster) and is invoked from engine
// background threads, so callbacks must be wrapped GIL-correctly. This shim
// exposes a clean Python API:
// - RtpsParticipant(Config(interface_address=..., ...))
// - add_writer(topic=..., type_name=..., reliable=...)
// - add_reader(topic=..., type_name=..., reliable=..., on_sample=callable(bytes))
// - publish(topic, bytes)
//
// It is kept out of the generated pybind_espp.cpp so regeneration never clobbers it.
#include <algorithm>
#include <functional>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "rtps_participant.hpp"
namespace py = pybind11;
using Rtps = espp::RtpsParticipant;
namespace {
py::bytes to_bytes(std::span<const uint8_t> s) {
return py::bytes(reinterpret_cast<const char *>(s.data()), s.size());
}
// Wrap a Python callable into a C++ std::function that the engine may copy and
// invoke from background threads that do not hold the GIL. Capturing the
// py::function directly would inc_ref without the GIL (a crash); a shared_ptr
// keeps copies GIL-free, and the callable is invoked / destroyed under the GIL.
// shared_ptr deleter that reacquires the GIL: the engine destroys its copies of
// these std::functions from background threads / under gil_scoped_release (e.g.
// in stop()), and destroying a py::function without the GIL aborts.
inline std::shared_ptr<py::function> make_gil_safe_holder(const py::function &fn) {
return std::shared_ptr<py::function>(new py::function(fn), [](py::function *p) {
py::gil_scoped_acquire gil;
delete p;
});
}
// Same GIL-safe holder pattern for an arbitrary py::object (e.g. a
// concurrent.futures.Future captured into a background reply callback).
inline std::shared_ptr<py::object> make_gil_safe_object(const py::object &obj) {
return std::shared_ptr<py::object>(new py::object(obj), [](py::object *p) {
py::gil_scoped_acquire gil;
delete p;
});
}
Rtps::sample_callback_t wrap_sample_callback(const py::function &fn) {
if (!fn) {
return {};
}
auto cb = make_gil_safe_holder(fn);
return [cb](std::span<const uint8_t> payload) {
py::gil_scoped_acquire gil;
try {
(*cb)(to_bytes(payload));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("RtpsParticipant on_sample");
}
};
}
Rtps::matched_callback_t wrap_matched_callback(const py::function &fn) {
if (!fn) {
return {};
}
auto cb = make_gil_safe_holder(fn);
return [cb]() {
py::gil_scoped_acquire gil;
try {
(*cb)();
} catch (py::error_already_set &e) {
e.discard_as_unraisable("RtpsParticipant matched callback");
}
};
}
std::vector<uint8_t> to_vec(const py::bytes &b) {
std::string s = b;
return std::vector<uint8_t>(s.begin(), s.end());
}
// A service handler: Python callable(bytes) -> bytes. Runs on an engine thread.
Rtps::service_handler_t wrap_service_handler(const py::function &fn) {
auto cb = make_gil_safe_holder(fn);
return [cb](std::span<const uint8_t> request) -> std::vector<uint8_t> {
py::gil_scoped_acquire gil;
try {
py::object r = (*cb)(to_bytes(request));
if (r.is_none()) {
return {};
}
return to_vec(r.cast<py::bytes>());
} catch (py::error_already_set &e) {
e.discard_as_unraisable("RtpsParticipant service handler");
return {};
}
};
}
// A reply callback for call_async: Python callable(bytes).
Rtps::ServiceClient::reply_callback_t wrap_reply_callback(const py::function &fn) {
auto cb = make_gil_safe_holder(fn);
return [cb](std::span<const uint8_t> reply) {
py::gil_scoped_acquire gil;
try {
(*cb)(to_bytes(reply));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("RtpsParticipant reply callback");
}
};
}
// Python-facing Config: like Rtps::Config but with py::function callbacks.
struct PyRtpsConfig {
std::string interface_address{};
py::function on_publisher_matched{};
py::function on_subscriber_matched{};
espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN};
};
Rtps::Config to_config(const PyRtpsConfig &pc) {
return Rtps::Config{
.interface_address = pc.interface_address,
.on_publisher_matched = wrap_matched_callback(pc.on_publisher_matched),
.on_subscriber_matched = wrap_matched_callback(pc.on_subscriber_matched),
.log_level = pc.log_level,
};
}
py::function as_function(const py::object &obj) {
if (obj.is_none()) {
return py::function{};
}
return obj.cast<py::function>();
}
} // namespace
void py_init_rtps(py::module &m) {
auto rtps = py::class_<Rtps>(
m, "RtpsParticipant",
"RTPS/DDS participant (embeddedRTPS engine) for pub/sub interop with FastDDS and ROS 2.\n"
"Payloads are CDR-encapsulated bytes (see the cdr component / struct.pack).\n"
"For ROS 2 use topic 'rt/<name>' and type '<pkg>::msg::dds_::<Type>_'.");
py::enum_<Rtps::Reliability>(rtps, "Reliability")
.value("BEST_EFFORT", Rtps::Reliability::BEST_EFFORT)
.value("RELIABLE", Rtps::Reliability::RELIABLE);
py::class_<PyRtpsConfig>(rtps, "Config")
.def(py::init([](std::string interface_address, const py::object &on_publisher_matched,
const py::object &on_subscriber_matched, espp::Logger::Verbosity log_level) {
PyRtpsConfig c;
c.interface_address = std::move(interface_address);
c.on_publisher_matched = as_function(on_publisher_matched);
c.on_subscriber_matched = as_function(on_subscriber_matched);
c.log_level = log_level;
return c;
}),
py::arg("interface_address") = std::string{},
py::arg("on_publisher_matched") = py::none(),
py::arg("on_subscriber_matched") = py::none(),
py::arg("log_level") = espp::Logger::Verbosity::WARN)
.def_readwrite("interface_address", &PyRtpsConfig::interface_address)
.def_readwrite("on_publisher_matched", &PyRtpsConfig::on_publisher_matched)
.def_readwrite("on_subscriber_matched", &PyRtpsConfig::on_subscriber_matched)
.def_readwrite("log_level", &PyRtpsConfig::log_level);
rtps.def(py::init([](const PyRtpsConfig &config) { return new Rtps(to_config(config)); }),
py::arg("config") = PyRtpsConfig{})
.def("start", &Rtps::start, py::call_guard<py::gil_scoped_release>(),
"Start the participant (transport + SPDP/SEDP discovery).")
.def("stop", &Rtps::stop, py::call_guard<py::gil_scoped_release>(),
"Stop the participant and its discovery/transport threads.")
.def("is_started", &Rtps::is_started)
.def(
"add_writer",
[](Rtps &self, const std::string &topic, const std::string &type_name, bool reliable) {
return self.add_writer({.topic = topic,
.type_name = type_name,
.reliability = reliable ? Rtps::Reliability::RELIABLE
: Rtps::Reliability::BEST_EFFORT});
},
py::arg("topic"), py::arg("type_name"), py::arg("reliable") = false,
py::call_guard<py::gil_scoped_release>(), "Add a publishing endpoint.")
.def(
"add_reader",
[](Rtps &self, const std::string &topic, const std::string &type_name, bool reliable,
const py::object &on_sample) {
// wrap under the GIL (we hold it here), then release for the engine call
auto cb = wrap_sample_callback(as_function(on_sample));
py::gil_scoped_release release;
return self.add_reader({.topic = topic,
.type_name = type_name,
.reliability = reliable ? Rtps::Reliability::RELIABLE
: Rtps::Reliability::BEST_EFFORT,
.on_sample = std::move(cb)});
},
py::arg("topic"), py::arg("type_name"), py::arg("reliable") = false,
py::arg("on_sample") = py::none(),
"Add a subscribing endpoint; on_sample receives each sample as bytes.")
.def(
"publish",
[](Rtps &self, const std::string &topic, const py::bytes &data) {
std::string s = data;
const std::vector<uint8_t> payload(s.begin(), s.end());
py::gil_scoped_release release;
return self.publish(topic, payload);
},
py::arg("topic"), py::arg("data"),
"Publish a CDR-encapsulated sample (bytes) on a topic added with add_writer().");
// ---- Services (RMI, ROS 2-interoperable) --------------------------------
py::class_<Rtps::ServiceClient, std::shared_ptr<Rtps::ServiceClient>>(
rtps, "ServiceClient", "Handle for calling a ROS 2-interoperable service.")
.def(
"call",
[](Rtps::ServiceClient &self, const py::bytes &request, double timeout) -> py::object {
auto req = to_vec(request);
std::optional<std::vector<uint8_t>> r;
{
py::gil_scoped_release rel;
r = self.call(req, std::chrono::milliseconds(static_cast<long>(timeout * 1000)));
}
return r ? py::object(to_bytes(*r)) : py::none();
},
py::arg("request"), py::arg("timeout") = 5.0,
"Blocking call (RMI). Returns the reply bytes, or None on timeout.")
.def(
"call_async",
[](Rtps::ServiceClient &self, const py::bytes &request, const py::function &on_reply) {
auto cb = wrap_reply_callback(on_reply);
auto req = to_vec(request);
py::gil_scoped_release rel;
return self.call_async(req, std::move(cb));
},
py::arg("request"), py::arg("on_reply"),
"Async call (AMI): on_reply(bytes) is invoked when the reply arrives.")
.def(
"call_future",
[](Rtps::ServiceClient &self, const py::bytes &request) {
py::object fut = py::module_::import("concurrent.futures").attr("Future")();
auto fut_holder = make_gil_safe_object(fut);
auto req = to_vec(request);
bool queued;
{
py::gil_scoped_release rel;
queued = self.call_async(req, [fut_holder](std::span<const uint8_t> reply) {
py::gil_scoped_acquire gil;
try {
(*fut_holder).attr("set_result")(to_bytes(reply));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("ServiceClient.call_future");
}
});
}
if (!queued) {
fut.attr("set_result")(py::none());
}
return fut;
},
py::arg("request"),
"Async call (AMI): returns a concurrent.futures.Future for the reply bytes "
"(result is None if the request could not be queued). Use fut.result(timeout=...).");
rtps.def(
"add_service_server",
[](Rtps &self, const std::string &service, const std::string &type_name,
const py::function &handler) {
auto h = wrap_service_handler(handler);
py::gil_scoped_release rel;
return self.add_service_server({service, type_name}, std::move(h));
},
py::arg("service"), py::arg("type_name"), py::arg("handler"),
"Add a ROS 2 service server; handler(request_bytes) -> reply_bytes.")
.def(
"add_service_client",
[](Rtps &self, const std::string &service, const std::string &type_name) {
py::gil_scoped_release rel;
return self.add_service_client({service, type_name});
},
py::arg("service"), py::arg("type_name"), "Add a ROS 2 service client.");
// ---- Actions (AMI, ROS 2-interoperable) ---------------------------------
py::class_<Rtps::ActionGoalHandle>(
rtps, "ActionGoalHandle", "Server-side handle to a running goal (in the execute callback).")
.def("goal", [](Rtps::ActionGoalHandle &h) { return to_bytes(h.goal()); })
.def(
"publish_feedback",
[](Rtps::ActionGoalHandle &h, const py::bytes &fb) {
auto v = to_vec(fb);
py::gil_scoped_release rel;
h.publish_feedback(v);
},
py::arg("feedback"))
.def(
"succeed",
[](Rtps::ActionGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.succeed(v);
},
py::arg("result"))
.def(
"abort",
[](Rtps::ActionGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.abort(v);
},
py::arg("result"))
.def(
"canceled",
[](Rtps::ActionGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.canceled(v);
},
py::arg("result"), "Terminate the goal CANCELED (in response to a cancel request).")
.def("is_canceling", &Rtps::ActionGoalHandle::is_canceling);
py::class_<Rtps::ActionClient, std::shared_ptr<Rtps::ActionClient>>(
rtps, "ActionClient", "Handle for driving a ROS 2-interoperable action.")
.def(
"send_goal",
[](Rtps::ActionClient &self, const py::bytes &goal, const py::function &on_feedback,
const py::function &on_result) -> py::object {
auto fb = make_gil_safe_holder(on_feedback);
auto rc = make_gil_safe_holder(on_result);
auto goal_v = to_vec(goal);
std::optional<Rtps::GoalId> gid;
{
py::gil_scoped_release rel;
gid = self.send_goal(
goal_v,
[fb](std::span<const uint8_t> f) {
py::gil_scoped_acquire gil;
try {
(*fb)(to_bytes(f));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("action feedback");
}
},
[rc](int8_t status, std::span<const uint8_t> r) {
py::gil_scoped_acquire gil;
try {
(*rc)(status, to_bytes(r));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("action result");
}
});
}
if (!gid) {
return py::none();
}
return py::object(py::bytes(reinterpret_cast<const char *>(gid->data()), gid->size()));
},
py::arg("goal"), py::arg("on_feedback"), py::arg("on_result"),
"Send a goal. on_feedback(bytes); on_result(status:int, bytes). Returns the goal id.")
.def(
"cancel_goal",
[](Rtps::ActionClient &self, const py::bytes &goal_id) {
auto id_v = to_vec(goal_id);
Rtps::GoalId id{};
if (id_v.size() != id.size()) {
return false;
}
std::copy(id_v.begin(), id_v.end(), id.begin());
py::gil_scoped_release rel;
return self.cancel_goal(id);
},
py::arg("goal_id"), "Request cancellation of a goal by its id (from send_goal).");
rtps.def(
"add_action_server",
[](Rtps &self, const std::string &action, const std::string &type_name,
const py::function &on_goal, const py::function &execute) {
auto og = make_gil_safe_holder(on_goal);
auto ex = make_gil_safe_holder(execute);
py::gil_scoped_release rel;
return self.add_action_server(
{action, type_name},
[og](const Rtps::GoalId &, std::span<const uint8_t> goal) -> bool {
py::gil_scoped_acquire gil;
try {
return (*og)(to_bytes(goal)).cast<bool>();
} catch (py::error_already_set &e) {
e.discard_as_unraisable("action on_goal");
return false;
}
},
[ex](Rtps::ActionGoalHandle h) {
py::gil_scoped_acquire gil;
try {
(*ex)(h);
} catch (py::error_already_set &e) {
e.discard_as_unraisable("action execute");
}
});
},
py::arg("action"), py::arg("type_name"), py::arg("on_goal"), py::arg("execute"),
"Add a ROS 2 action server. on_goal(goal_bytes)->bool; execute(ActionGoalHandle).");
rtps.def(
"add_action_client",
[](Rtps &self, const std::string &action, const std::string &type_name) {
py::gil_scoped_release rel;
return self.add_action_client({action, type_name});
},
py::arg("action"), py::arg("type_name"), "Add a ROS 2 action client.");
// ---- Native (espp<->espp) services + actions ----------------------------
py::class_<Rtps::NativeServiceClient, std::shared_ptr<Rtps::NativeServiceClient>>(
rtps, "NativeServiceClient", "Handle for a lean native (espp<->espp) service.")
.def(
"call",
[](Rtps::NativeServiceClient &self, const py::bytes &request,
double timeout) -> py::object {
auto req = to_vec(request);
std::optional<std::vector<uint8_t>> r;
{
py::gil_scoped_release rel;
r = self.call(req, std::chrono::milliseconds(static_cast<long>(timeout * 1000)));
}
return r ? py::object(to_bytes(*r)) : py::none();
},
py::arg("request"), py::arg("timeout") = 5.0)
.def(
"call_async",
[](Rtps::NativeServiceClient &self, const py::bytes &request,
const py::function &on_reply) {
auto cb = make_gil_safe_holder(on_reply);
auto req = to_vec(request);
py::gil_scoped_release rel;
return self.call_async(req, [cb](std::span<const uint8_t> reply) {
py::gil_scoped_acquire gil;
try {
(*cb)(to_bytes(reply));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native reply");
}
});
},
py::arg("request"), py::arg("on_reply"))
.def(
"call_future",
[](Rtps::NativeServiceClient &self, const py::bytes &request) {
py::object fut = py::module_::import("concurrent.futures").attr("Future")();
auto fut_holder = make_gil_safe_object(fut);
auto req = to_vec(request);
bool queued;
{
py::gil_scoped_release rel;
queued = self.call_async(req, [fut_holder](std::span<const uint8_t> reply) {
py::gil_scoped_acquire gil;
try {
(*fut_holder).attr("set_result")(to_bytes(reply));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("NativeServiceClient.call_future");
}
});
}
if (!queued) {
fut.attr("set_result")(py::none());
}
return fut;
},
py::arg("request"),
"Async call (AMI): returns a concurrent.futures.Future for the reply bytes.");
py::class_<Rtps::NativeActionClient, std::shared_ptr<Rtps::NativeActionClient>>(
rtps, "NativeActionClient", "Handle for a lean native (espp<->espp) action.")
.def(
"send_goal",
[](Rtps::NativeActionClient &self, const py::bytes &goal, const py::function &on_feedback,
const py::function &on_result, const py::object &on_accepted) {
auto fb = make_gil_safe_holder(on_feedback);
auto rc = make_gil_safe_holder(on_result);
Rtps::NativeActionClient::accepted_callback_t acc = nullptr;
if (!on_accepted.is_none()) {
auto ac = make_gil_safe_holder(on_accepted.cast<py::function>());
acc = [ac](uint32_t handle) {
py::gil_scoped_acquire gil;
try {
(*ac)(handle);
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native on_accepted");
}
};
}
auto goal_v = to_vec(goal);
py::gil_scoped_release rel;
return self.send_goal(
goal_v,
[fb](std::span<const uint8_t> f) {
py::gil_scoped_acquire gil;
try {
(*fb)(to_bytes(f));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native feedback");
}
},
[rc](uint8_t status, std::span<const uint8_t> r) {
py::gil_scoped_acquire gil;
try {
(*rc)(status, to_bytes(r));
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native result");
}
},
std::move(acc));
},
py::arg("goal"), py::arg("on_feedback"), py::arg("on_result"),
py::arg("on_accepted") = py::none())
.def(
"cancel_goal",
[](Rtps::NativeActionClient &self, uint32_t goal_handle) {
py::gil_scoped_release rel;
return self.cancel_goal(goal_handle);
},
py::arg("goal_handle"));
rtps.def(
"add_native_service_server",
[](Rtps &self, const std::string &service, const std::string &type_name,
const py::function &handler) {
auto h = wrap_service_handler(handler);
py::gil_scoped_release rel;
return self.add_native_service_server({service, type_name}, std::move(h));
},
py::arg("service"), py::arg("type_name"), py::arg("handler"))
.def(
"add_native_service_client",
[](Rtps &self, const std::string &service, const std::string &type_name) {
py::gil_scoped_release rel;
return self.add_native_service_client({service, type_name});
},
py::arg("service"), py::arg("type_name"))
.def(
"add_native_action_server",
[](Rtps &self, const std::string &action, const std::string &type_name,
const py::function &on_goal, const py::function &execute,
const py::object &on_cancel) {
auto og = make_gil_safe_holder(on_goal);
auto ex = make_gil_safe_holder(execute);
Rtps::native_cancel_callback_t oc = nullptr;
if (!on_cancel.is_none()) {
auto ocw = make_gil_safe_holder(on_cancel.cast<py::function>());
oc = [ocw](uint32_t handle) -> bool {
py::gil_scoped_acquire gil;
try {
return (*ocw)(handle).cast<bool>();
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native on_cancel");
return false;
}
};
}
py::gil_scoped_release rel;
return self.add_native_action_server(
{action, type_name},
[og](std::span<const uint8_t> goal) -> bool {
py::gil_scoped_acquire gil;
try {
return (*og)(to_bytes(goal)).cast<bool>();
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native on_goal");
return false;
}
},
[ex](Rtps::NativeGoalHandle h) {
py::gil_scoped_acquire gil;
try {
(*ex)(h);
} catch (py::error_already_set &e) {
e.discard_as_unraisable("native execute");
}
},
std::move(oc));
},
py::arg("action"), py::arg("type_name"), py::arg("on_goal"), py::arg("execute"),
py::arg("on_cancel") = py::none())
.def(
"add_native_action_client",
[](Rtps &self, const std::string &action, const std::string &type_name) {
py::gil_scoped_release rel;
return self.add_native_action_client({action, type_name});
},
py::arg("action"), py::arg("type_name"));
py::class_<Rtps::NativeGoalHandle>(rtps, "NativeGoalHandle",
"Server-side handle to a running native goal.")
.def("goal", [](Rtps::NativeGoalHandle &h) { return to_bytes(h.goal()); })
.def("goal_handle", &Rtps::NativeGoalHandle::goal_handle)
.def("is_canceling", &Rtps::NativeGoalHandle::is_canceling)
.def(
"publish_feedback",
[](Rtps::NativeGoalHandle &h, const py::bytes &fb) {
auto v = to_vec(fb);
py::gil_scoped_release rel;
h.publish_feedback(v);
},
py::arg("feedback"))
.def(
"succeed",
[](Rtps::NativeGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.succeed(v);
},
py::arg("result"))
.def(
"abort",
[](Rtps::NativeGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.abort(v);
},
py::arg("result"))
.def(
"canceled",
[](Rtps::NativeGoalHandle &h, const py::bytes &result) {
auto v = to_vec(result);
py::gil_scoped_release rel;
h.canceled(v);
},
py::arg("result"));
}