nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1// nros-cpp: Action client class
2// Freestanding C++ — no exceptions, no STL required
3
9
10#ifndef NROS_RCLCPP_ACTION_CLIENT_HPP
11#define NROS_RCLCPP_ACTION_CLIENT_HPP
12
13#include <cstdint>
14#include <cstddef>
15#include <string.h>
16
18#include "rclcpp/detail/entity_name.hpp" // The one entity-name copy
19#include "rclcpp/logging.hpp" // NROS_RCLCPP_REFUSE_* + rclcpp::detail::refuse
20#include "rclcpp/owned.hpp" // `Client<A>::SharedPtr`
21#include "rclcpp/result.hpp"
22#include "rclcpp/size_bound.hpp" // rclcpp::rx_buffer_capacity<M> — the receive-buffer size
23#include "rclcpp/future.hpp"
24#include "rclcpp/stream.hpp"
25#include "rclcpp/time.hpp" // `async_cancel_goals_before(const rclcpp::Time&)`
26// `CancelReturnCode` (the `action_msgs/srv/CancelGoal` RPC status
27// `cancel_goal` resolves to) is declared beside its per-goal sibling
28// `CancelResponse` in action_server.hpp, which is also where
29// polling_action_server.hpp reaches for them.
31#include "rclcpp_action/detail/upstream.hpp" // Hosted goal-handle plumbing
32
33// FFI declarations sourced from cbindgen-generated
34// `nros_cpp_ffi.h`. cbindgen renders Rust `*const [u8; 16]` /
35// `*mut [u8; 16]` parameters as C++ pointer-to-array
36// (`const uint8_t (*goal_id)[16]`); member-function bodies below
37// take `goal_id` as a 16-byte array (decays to `uint8_t*`) and
38// `reinterpret_cast` at the call site to bridge the two ABI
39// equivalent shapes.
40#include "rclcpp/detail/nros_cpp_ffi.h"
41
42extern "C" {
44 const uint8_t goal_id[16],
45 void* ctx);
46typedef void (*nros_cpp_action_client_feedback_callback_t)(const uint8_t goal_id[16],
47 const uint8_t* data, size_t len,
48 void* ctx);
49typedef void (*nros_cpp_action_client_result_callback_t)(const uint8_t goal_id[16], int32_t status,
50 const uint8_t* data, size_t len,
51 void* ctx);
52
56 nros_cpp_action_client_result_callback_t result, void* context);
57
60typedef void (*nros_cpp_action_client_cancel_reply_callback_t)(const uint8_t goal_id[16],
61 int64_t seq, const uint8_t* reply,
62 size_t reply_len, void* ctx);
63
67} // extern "C"
68
76namespace rclcpp {
77class Node;
78}
79
80// ============================================================================
81// `rclcpp_action::Client<A>` -- DEFINED here (rclcpp_action:: is the home)
82// ============================================================================
83//
84// The definition moved from `nros::ActionClient<A>` to
85// `rclcpp_action::Client<A>` (the `nros::` alias is gone). A RENAME
86// as well as a move -- upstream's namespace already says "action", so the type
87// is `Client`, not `ActionClient`. Note the two `Client`s do not collide:
88// `rclcpp::Client<S>` is the SERVICE client, in a different namespace.
89namespace rclcpp_action {
90
96enum class ResultCode : int8_t {
101};
102
107static const size_t MAX_CANCEL_INFOS = 16;
108
114static const size_t CANCEL_STASH_CAP = 4;
115
116template <typename A> class Client;
117
129template <typename A> class ClientGoalHandle {
130 public:
131 using Feedback = typename A::Feedback;
132 using Result = typename A::Result;
133#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
134 using SharedPtr = ::std::shared_ptr<ClientGoalHandle>;
135 using ConstSharedPtr = ::std::shared_ptr<const ClientGoalHandle>;
137 struct WrappedResult {
138 GoalUUID goal_id;
139 ResultCode code;
140 ::std::shared_ptr<Result> result;
141 };
142 using GoalResponseCallback = ::std::function<void(SharedPtr)>;
143 using FeedbackCallback =
144 ::std::function<void(SharedPtr, const ::std::shared_ptr<const Feedback>)>;
145 using ResultCallback = ::std::function<void(const WrappedResult&)>;
146#else
147 using SharedPtr = detail::Refused<ClientGoalHandle>;
148 using ConstSharedPtr = detail::Refused<ClientGoalHandle>;
149 using WrappedResult = detail::Refused<ClientGoalHandle>;
150#endif
151
152 explicit ClientGoalHandle(const GoalUUID& id)
153 : goal_id_(id), status_(static_cast<int8_t>(GoalStatus::Accepted)), feedback_aware_(false),
154 result_aware_(false) {}
157
159 const GoalUUID& get_goal_id() const { return goal_id_; }
161 int8_t get_status() const { return status_; }
163 bool is_feedback_aware() const { return feedback_aware_; }
165 bool is_result_aware() const { return result_aware_; }
166
167 private:
168 friend class Client<A>;
169
170#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
174 struct ResultState {
175 ::std::promise<WrappedResult> promise;
176 ::std::shared_future<WrappedResult> future;
177 bool done = false;
178 };
179 ResultState* result_state() {
180 ResultState* st = result_.template as<ResultState>();
181 if (st == nullptr) {
182 st = new ResultState();
183 st->future = st->promise.get_future().share();
184 result_.reset(st, [](void* p) { delete static_cast<ResultState*>(p); });
185 }
186 return st;
187 }
189 void set_result(const WrappedResult& wrapped) {
190 ResultState* st = result_.template as<ResultState>();
191 if (st != nullptr && !st->done) {
192 st->done = true;
193 st->promise.set_value(wrapped);
194 }
195 }
196 bool result_done() const {
197 ResultState* st = result_.template as<ResultState>();
198 return st != nullptr && st->done;
199 }
200#endif
201
202 GoalUUID goal_id_;
203 int8_t status_;
204 bool feedback_aware_;
205 bool result_aware_;
208 detail::HostedBox result_;
209};
210
211#if defined(NROS_CPP_HAS_SHARED_PTR) // hosted-family: action-interop
212namespace detail {
215struct CancelGoalInfo {
216 struct {
217 uint8_t uuid[16];
218 } goal_id;
219 struct {
220 int32_t sec;
221 uint32_t nanosec;
222 } stamp;
223};
227struct CancelResponse {
228 static constexpr int8_t ERROR_NONE = 0;
229 static constexpr int8_t ERROR_REJECTED = 1;
230 static constexpr int8_t ERROR_UNKNOWN_GOAL_ID = 2;
231 static constexpr int8_t ERROR_GOAL_TERMINATED = 3;
232 using SharedPtr = ::std::shared_ptr<CancelResponse>;
233 int8_t return_code = ERROR_NONE;
234 ::std::vector<CancelGoalInfo> goals_canceling;
235};
236} // namespace detail
237#endif
238
256template <typename A> class Client {
257 public:
258 using GoalType = typename A::Goal;
259 using ResultType = typename A::Result;
260 using FeedbackType = typename A::Feedback;
261
265 using SharedPtr = ::rclcpp::Owned<Client>;
270#if defined(NROS_CPP_HAS_SHARED_PTR) // hosted-family: action-interop
273 using CancelResponse = detail::CancelResponse;
274 using CancelCallback = ::std::function<void(typename CancelResponse::SharedPtr)>;
275 using ResultCallback = typename GoalHandle::ResultCallback;
276#else
277 using CancelResponse = detail::Refused<Client>;
278#endif
279
280 private:
281#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
282 using FeedbackPtr = ::std::shared_ptr<const FeedbackType>;
283#else
284 using FeedbackPtr = detail::Refused<FeedbackType>;
285#endif
286
287 public:
292 struct GoalAccept {
296 uint8_t goal_id[16];
298
299 // Receive-buffer size for `Future<GoalAccept>`; the payload is 17 bytes
300 // (16-byte goal_id + 1 accepted byte), rounded up.
301 static const size_t SERIALIZED_SIZE_MAX = 32;
302
303 // The 17-byte wire layout is owned Rust-side
304 // (`nros_cpp_action_goal_accept_decode`, beside its producer); the header
305 // no longer hand-decodes it, so the two cannot drift.
306 static int ffi_deserialize(const uint8_t* data, size_t len, GoalAccept* out) {
307 if (!out) return -1;
308 return nros_cpp_action_goal_accept_decode(data, len, &out->goal_id, &out->accepted) ==
309 NROS_CPP_RET_OK
310 ? 0
311 : -1;
312 }
313 };
314
338 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
339 return ::rclcpp::Result(
340 nros_cpp_action_client_wait_for_action_server(storage_, timeout_ms));
341 }
342
344 template <typename T = void>::rclcpp::Result wait_for_action_server() {
345 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_UNBOUNDED_WAIT);
346 return ::rclcpp::Result(::rclcpp::ErrorCode::Unsupported);
347 }
348
349#ifdef NROS_CPP_HAS_STD_CHRONO // hosted-family: chrono-interop
355 template <typename Rep, typename Period>
356 ::rclcpp::Result wait_for_action_server(::std::chrono::duration<Rep, Period> timeout) {
357 const int64_t ms = ::rclcpp::detail::chrono_wait_budget_ms(timeout);
358 if (ms < 0) {
359 NROS_RCLCPP_SAY_REFUSED(NROS_RCLCPP_REFUSE_UNBOUNDED_WAIT_RUNTIME);
360 return ::rclcpp::Result(::rclcpp::ErrorCode::Unsupported);
361 }
362 return wait_for_action_server(static_cast<uint32_t>(ms));
363 }
364#endif // NROS_CPP_HAS_STD_CHRONO
365
380 if (!initialized_) return false;
381 return nros_cpp_action_client_action_server_is_ready(
382 const_cast<void*>(static_cast<const void*>(storage_)));
383 }
384
391 const char* get_action_name() const { return initialized_ ? action_name_ : ""; }
392
406 ::rclcpp::Result send_goal(const GoalType& goal, uint8_t goal_id[16]) {
407 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
408
409 uint8_t buf[::rclcpp::detail::buffer_bounds<GoalType>::tx];
410 size_t len = 0;
411 if (GoalType::ffi_serialize(&goal, buf, sizeof(buf), &len) != 0) {
412 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
413 }
414 return ::rclcpp::Result(nros_cpp_action_client_send_goal(
415 storage_, buf, len, reinterpret_cast<uint8_t(*)[16]>(goal_id)));
416 }
417
421 return send_goal(goal, goal_id.data());
422 }
423
432 ::rclcpp::Result get_result(const uint8_t goal_id[16], ResultType& result) {
434 }
435
437 ::rclcpp::Result get_result(const ::rclcpp_action::GoalUUID& goal_id, ResultType& result) {
439 result);
440 }
441
444 template <size_t Cap>
445 ::rclcpp::Result get_result_sized(const uint8_t goal_id[16], ResultType& result) {
446 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
447
448 uint8_t buf[Cap];
449 size_t len = 0;
450 nros_cpp_ret_t ret = nros_cpp_action_client_get_result(
451 storage_, executor_, reinterpret_cast<const uint8_t(*)[16]>(goal_id), buf, sizeof(buf),
452 &len);
453 if (ret != 0) return ::rclcpp::Result(ret);
454
455 if (ResultType::ffi_deserialize(buf, len, &result) != 0) {
456 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
457 }
458 return ::rclcpp::Result::success();
459 }
460
461 // =================================================================
462 // Future-based API — non-blocking, polled via Future<T>
463 // =================================================================
464
476
491 ::rclcpp::Future<GoalAccept> send_goal_future(const GoalType& goal) {
492 if (!initialized_) return ::rclcpp::Future<GoalAccept>();
493
494 uint8_t buf[::rclcpp::detail::buffer_bounds<GoalType>::tx];
495 size_t len = 0;
496 if (GoalType::ffi_serialize(&goal, buf, sizeof(buf), &len) != 0) {
497 return ::rclcpp::Future<GoalAccept>();
499
500 uint8_t goal_id[16];
501 nros_cpp_ret_t ret = nros_cpp_action_client_send_goal_async(
502 storage_, buf, len, reinterpret_cast<uint8_t(*)[16]>(goal_id));
503 if (ret != 0) return ::rclcpp::Future<GoalAccept>();
504
505 // Keyed by THIS goal on this client's acceptance list:
506 // the acceptance reaches it whether the executor dispatched it during
507 // `spin_once` (through `on_goal_response`) or the future's own poll
508 // took it (`nros_cpp_action_client_poll`, same callbacks).
509 return ::rclcpp::Future<GoalAccept>(storage_, &nros_cpp_action_client_poll, goal_id,
510 &accepts_,
511 executor_ // driven by `Future::wait_for`
512 );
513 }
514
523 /// auto fut = client.get_result_future(goal_id);
524 /// ResultType result;
525 /// RCLCPP_TRY(fut.wait(executor.handle(), 10000, result));
526 /// ```
527 ///
528 /// Keyed by `goal_id`: the future receives that goal's result
529 /// and no other, whichever client or future polls first. A result for a
530 /// goal no future waits on -- including one whose future was cancelled or
531 /// destroyed first -- is dropped (the client's result callback still sees
532 /// it). Several futures for one goal each receive its result, one reply
533 /// apiece.
534 ///
535 /// @param goal_id 16-byte goal UUID from send_goal() or GoalAccept.
536 /// @return Future that resolves to ResultType. Returns a consumed
537 /// (empty) future on send failure, including a full table (at most
538 /// `MAX_CONCURRENT_GOALS` get_result requests pending per client).
542
544 ::rclcpp::Future<ResultType> get_result_future(const ::rclcpp_action::GoalUUID& goal_id) {
546 goal_id.data());
547 }
548
553 template <size_t Cap>
556 if (!initialized_) return Fut();
557
558 nros_cpp_ret_t ret = nros_cpp_action_client_get_result_async(
559 storage_, reinterpret_cast<const uint8_t(*)[16]>(goal_id));
560 if (ret != 0) return Fut();
561
562 // Keyed by THIS goal on this client's result list; see
563 // `send_goal_future`.
564 return Fut(storage_, &nros_cpp_action_client_poll, goal_id, &results_,
565 executor_ // driven by `Future::wait_for`
566 );
567 }
573
575 /// ErrorCode::Error if deserialization failed; otherwise the
576 /// FFI error code.
580
583 template <size_t Cap>::rclcpp::Result try_recv_feedback_sized(FeedbackType& feedback) {
584 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
585
586 uint8_t buf[Cap];
587 size_t len = 0;
588 nros_cpp_ret_t ret = take_feedback(this, buf, sizeof(buf), &len);
589 if (ret != 0) return ::rclcpp::Result(ret);
590 if (len == 0) return ::rclcpp::Result(::rclcpp::ErrorCode::TryAgain);
591 if (FeedbackType::ffi_deserialize(buf, len, &feedback) != 0) {
592 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
593 }
594 return ::rclcpp::Result::success();
595 }
596
610 /// RCLCPP_TRY(client.feedback_stream().wait_next(executor.handle(), 500, fb));
611 /// ```
612 ///
613 /// Usage (non-blocking):
614 /// ```cpp
615 /// FeedbackType fb;
616 /// Result r = client.feedback_stream().try_next(fb);
617 /// if (r.ok()) { ... }
618 /// ```
620 if (initialized_ && !feedback_stream_.is_valid()) {
621 feedback_stream_.bind(this, &Client::take_feedback);
623 return feedback_stream_;
624 }
625
626 const ::rclcpp::Stream<FeedbackType>& feedback_stream() const { return feedback_stream_; }
627
628 // =================================================================
629 // Async (non-blocking) API — callbacks invoked during spin_once()
630 // =================================================================
631
637 /// All callbacks receive the context pointer for user state.
638 ///
639 /// On a HOSTED build it ALSO carries upstream's three
640 /// per-goal fields, `goal_response_callback` / `feedback_callback` /
641 /// `result_callback`, taking upstream's shapes (`GoalHandle::SharedPtr`,
642 /// `std::shared_ptr<const Feedback>`, `const WrappedResult&`). Those are
643 /// read by `async_send_goal`, per goal, as upstream does; the raw pointers
644 /// are read by `set_callbacks`, per client. A freestanding build refuses an
645 /// assignment to the upstream fields.
648 void (*goal_response)(bool accepted, const uint8_t goal_id[16], void* ctx);
650 void (*feedback)(const uint8_t goal_id[16], const uint8_t* data, size_t len, void* ctx);
651 /// Called when the result is received.
652 void (*result)(const uint8_t goal_id[16], int32_t status, const uint8_t* data, size_t len,
653 void* ctx);
654
655 void* context;
656
658 detail::UpstreamCallback<void(typename GoalHandle::SharedPtr)> goal_response_callback;
660 detail::UpstreamCallback<void(typename GoalHandle::SharedPtr, FeedbackPtr)>
663 /// the goal is accepted, as upstream does.
664 detail::UpstreamCallback<void(const WrappedResult&)> result_callback;
665
667 };
672
676 /// @param goal_id Output 16-byte goal UUID (filled on success).
677 /// @return Result indicating success or failure.
678 ::rclcpp::Result send_goal_async(const GoalType& goal, uint8_t goal_id[16]) {
679 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
680
681 uint8_t buf[::rclcpp::detail::buffer_bounds<GoalType>::tx];
682 size_t len = 0;
683 if (GoalType::ffi_serialize(&goal, buf, sizeof(buf), &len) != 0) {
684 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
685 }
686 return ::rclcpp::Result(nros_cpp_action_client_send_goal_async(
687 storage_, buf, len, reinterpret_cast<uint8_t(*)[16]>(goal_id)));
688 }
689
692 return send_goal_async(goal, goal_id.data());
693 }
694
695#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
713 ::rclcpp::Future<GoalAccept>
714 async_send_goal(const GoalType& goal, const SendGoalOptions& options = SendGoalOptions()) {
715 uint8_t goal_id[16];
716 if (!send_goal_async(goal, goal_id).ok()) return ::rclcpp::Future<GoalAccept>();
717 UpstreamState* st = upstream_state();
718 prune_upstream(st);
719 UpstreamGoal g;
720 g.id = GoalUUID(goal_id);
721 g.options = options;
722 st->goals.push_back(g);
723 return ::rclcpp::Future<GoalAccept>(storage_, &nros_cpp_action_client_poll, goal_id,
724 &accepts_, executor_);
725 }
726#else
728 template <typename T = void>
730 const SendGoalOptions& = SendGoalOptions()) {
731 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
732 return ::rclcpp::Future<GoalAccept>();
733 }
734#endif
735
736#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
758 detail::DrivenSharedFuture<WrappedResult>
759 async_get_result(typename GoalHandle::SharedPtr goal_handle,
760 ResultCallback result_callback = nullptr) {
761 if (!initialized_ || !goal_handle) return detail::DrivenSharedFuture<WrappedResult>();
762 typename GoalHandle::ResultState* rs = goal_handle->result_state();
763 detail::DrivenSharedFuture<WrappedResult> fut(rs->future, pump_cell());
764 if (rs->done) return fut;
765 UpstreamState* st = upstream_state();
766 size_t at = 0;
767 bool created = false;
768 if (upstream_goal(goal_handle->get_goal_id().data(), at) == nullptr) {
769 UpstreamGoal g;
770 g.id = goal_handle->get_goal_id();
771 g.accepted = true;
772 st->goals.push_back(g);
773 at = st->goals.size() - 1;
774 created = true;
775 }
776 if (!goal_handle->result_aware_) {
777 if (!get_result_async(goal_handle->get_goal_id()).ok()) {
778 if (created) st->goals.erase(st->goals.begin() + static_cast<long>(at));
779 return detail::DrivenSharedFuture<WrappedResult>();
780 }
781 goal_handle->result_aware_ = true;
782 }
783 UpstreamGoal& g = st->goals[at];
784 g.accepted = true;
785 g.handle = goal_handle;
786 g.weak.reset();
787 if (result_callback) g.options.result_callback = result_callback;
788 return fut;
789 }
790
807 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
808 async_cancel_goal(typename GoalHandle::SharedPtr goal_handle,
809 CancelCallback cancel_callback = nullptr) {
810 if (!goal_handle) return detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>();
811 return send_cancel(goal_handle->get_goal_id(), 0, 0, cancel_callback);
812 }
813
817 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
818 async_cancel_all_goals(CancelCallback cancel_callback = nullptr) {
819 return send_cancel(GoalUUID(), 0, 0, cancel_callback);
820 }
821
826 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
827 async_cancel_goals_before(const ::rclcpp::Time& stamp,
828 CancelCallback cancel_callback = nullptr) {
829 const int64_t ns = stamp.nanoseconds();
830 const int64_t sec = ns >= 0 ? ns / 1000000000LL : -((-ns + 999999999LL) / 1000000000LL);
831 const int64_t nsec = ns - sec * 1000000000LL;
832 return send_cancel(GoalUUID(), static_cast<int32_t>(sec), static_cast<uint32_t>(nsec),
833 cancel_callback);
834 }
835#endif
836#ifndef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
841 template <typename T = void, typename... Args> void async_get_result(Args&&...) {
842 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
843 }
844
845 template <typename T = void, typename... Args> void async_cancel_goal(Args&&...) {
846 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
847 }
848
849 template <typename T = void, typename... Args> void async_cancel_all_goals(Args&&...) {
850 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
851 }
852
853 template <typename T = void, typename... Args> void async_cancel_goals_before(Args&&...) {
854 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
855 }
856#endif
857
872 ::rclcpp::Result cancel_goal(const uint8_t goal_id[16]) {
873 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
874 return ::rclcpp::Result(nros_cpp_action_client_cancel_goal(
875 storage_, reinterpret_cast<const uint8_t(*)[16]>(goal_id)));
876 }
877
879 ::rclcpp::Result cancel_goal(const ::rclcpp_action::GoalUUID& goal_id) {
880 return cancel_goal(goal_id.data());
881 }
882
895 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
896 int8_t code = 0;
897 // A reply the spin took off the wire for a request no
898 // future or callback waits on is kept here, not dropped.
899 if (take_stray_cancel(code)) {
900 out = static_cast<::rclcpp_action::CancelReturnCode>(code);
901 return ::rclcpp::Result::success();
902 }
903 nros_cpp_ret_t ret = nros_cpp_action_client_try_recv_cancel_response(storage_, &code);
904 if (ret != 0) return ::rclcpp::Result(ret);
905 out = static_cast<::rclcpp_action::CancelReturnCode>(code);
906 return ::rclcpp::Result::success();
907 }
908
916 ::rclcpp::Result get_result_async(const uint8_t goal_id[16]) {
917 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
918 return ::rclcpp::Result(nros_cpp_action_client_get_result_async(
919 storage_, reinterpret_cast<const uint8_t(*)[16]>(goal_id)));
920 }
921
923 ::rclcpp::Result get_result_async(const ::rclcpp_action::GoalUUID& goal_id) {
924 return get_result_async(goal_id.data());
925 }
926
940 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
941 // The client keeps its OWN callbacks installed (they feed
942 // the goal-keyed futures and the feedback slot) and forwards to these.
943 user_ = options;
944 return install_dispatch();
945 }
946
952 void poll() {
953 if (!initialized_) return;
954 // Cancel replies included: they reach `on_cancel_reply`.
955 nros_cpp_action_client_poll(storage_);
956 }
957
959 bool is_valid() const { return initialized_; }
960
975 abandon_upstream(); // Waiting futures settle; no future names this client
976 accepts_.orphan_all();
977 results_.orphan_all();
978 if (initialized_) {
979 nros_cpp_action_client_destroy(storage_);
980 initialized_ = false;
981 }
982 feedback_stream_ = ::rclcpp::Stream<FeedbackType>();
983 }
984
985 // Move semantics (non-copyable). Relocation goes through the
986 // `nros_cpp_action_client_relocate` runtime call, which also
987 // RE-POINTS the arena entry's callback `context` at the new `storage_`
988 // (before that, a moved-from temporary left the entry naming
989 // dead bytes). The feedback stream is rebound to the new storage afterwards.
990 //
991 // The pending futures and the client's own callbacks follow
992 // it too: both lists move (each future's storage pointer re-targeted) and
993 // the callbacks are re-installed with the new address as their context.
994 Client(Client&& other)
995 : executor_(other.executor_), initialized_(other.initialized_), action_name_{} {
996 ::memcpy(action_name_, other.action_name_, sizeof(action_name_));
997 adopt(other);
998 }
999
1001 if (this != &other) {
1002 abandon_upstream();
1003 accepts_.orphan_all();
1004 results_.orphan_all();
1005 if (initialized_) {
1006 nros_cpp_action_client_destroy(storage_);
1007 feedback_stream_ = ::rclcpp::Stream<FeedbackType>();
1008 }
1009 executor_ = other.executor_;
1010 initialized_ = other.initialized_;
1011 ::memcpy(action_name_, other.action_name_, sizeof(action_name_));
1012 adopt(other);
1013 }
1014 return *this;
1015 }
1016
1019 Client() : executor_(nullptr), initialized_(false), action_name_{} {}
1020
1021 private:
1022 Client(const Client&) = delete;
1023 Client& operator=(const Client&) = delete;
1024
1025 friend class ::rclcpp::Node;
1026
1028 ::rclcpp::Result install_dispatch() {
1029 // An abandoned future tells THIS client, which frees the
1030 // request's slot unless something else still waits on its reply.
1031 accepts_.forget = &Client::forget_accept;
1032 accepts_.forget_ctx = this;
1033 results_.forget = &Client::forget_result;
1034 results_.forget_ctx = this;
1036 storage_, &Client::on_goal_response, &Client::on_feedback, &Client::on_result, this);
1037 if (ret != 0) return ::rclcpp::Result(ret);
1038 // The executor's spin takes cancel replies and hands them
1039 // here (same `this` context).
1040 return ::rclcpp::Result(
1041 nros_cpp_action_client_set_cancel_reply_callback(storage_, &Client::on_cancel_reply));
1042 }
1043
1044 // The goal-slot leak. A request in the Rust core's bounded
1045 // per-sub-client table (`MAX_CONCURRENT_GOALS`) used to come back only
1046 // with its reply, so a future dropped first — with a reply that never
1047 // came — held its slot forever, and the fifth such goal refused every
1048 // later send. The future is the request's owner; when it is abandoned the
1049 // request is forgotten, UNLESS another owner still waits on that reply:
1050 // a client-wide callback (`set_callbacks`) or an upstream goal
1051 // (`async_send_goal` / `async_get_result`). A late reply for a forgotten
1052 // request is dropped by the correlator.
1053 static void forget_accept(void* ctx, const uint8_t goal[16]) {
1054 static_cast<Client*>(ctx)->forget_request(NROS_CPP_ACTION_CHANNEL_SEND_GOAL, goal);
1055 }
1056 static void forget_result(void* ctx, const uint8_t goal[16]) {
1057 static_cast<Client*>(ctx)->forget_request(NROS_CPP_ACTION_CHANNEL_GET_RESULT, goal);
1058 }
1059 void forget_request(int32_t channel, const uint8_t goal[16]) {
1060 if (!initialized_) return;
1061 const bool accept = channel == NROS_CPP_ACTION_CHANNEL_SEND_GOAL;
1062 if (accept ? user_.goal_response != nullptr : user_.result != nullptr) return;
1063 if (upstream_wants(accept, goal)) return;
1064 (void)nros_cpp_action_client_forget(storage_, channel,
1065 reinterpret_cast<const uint8_t(*)[16]>(goal));
1066 }
1067
1069 void adopt(Client& other) {
1070 user_ = other.user_;
1071 upstream_ = static_cast<detail::HostedBox&&>(other.upstream_);
1072 repoint_pump(); // The futures this client handed out follow it
1073 feedback_len_ = other.feedback_len_;
1074 feedback_ready_ = other.feedback_ready_;
1075 for (size_t i = 0; i < CANCEL_STASH_CAP; ++i)
1076 stash_codes_[i] = other.stash_codes_[i];
1077 stash_head_ = other.stash_head_;
1078 stash_len_ = other.stash_len_;
1079 other.stash_len_ = 0;
1080 feedback_truncated_ = other.feedback_truncated_;
1081 for (size_t i = 0; i < feedback_len_; ++i)
1082 feedback_buf_[i] = other.feedback_buf_[i];
1083 other.feedback_ready_ = false;
1084 if (other.initialized_) {
1085 nros_cpp_action_client_relocate(other.storage_, storage_);
1086 other.accepts_.move_to(accepts_, storage_);
1087 other.results_.move_to(results_, storage_);
1088 other.initialized_ = false;
1089 (void)install_dispatch();
1090 }
1091 other.feedback_stream_ = ::rclcpp::Stream<FeedbackType>();
1092 }
1093
1094 // The three callbacks every `Client<A>` keeps installed. The
1095 // executor's dispatch (and `nros_cpp_action_client_poll`) hands each reply
1096 // here, attributed to the goal its request named; it goes to
1097 // the future waiting on THAT goal on THIS client, if any -- dropped
1098 // otherwise, never handed to another goal -- and then to the user's
1099 // callback (`set_callbacks`).
1100 static void on_goal_response(bool accepted, const uint8_t goal_id[16], void* ctx) {
1101 Client* self = static_cast<Client*>(ctx);
1102 ::rclcpp::detail::PendingLink* owner = self->accepts_.resolve_goal(goal_id);
1103 if (owner != nullptr) {
1104 // The 17-byte layout `GoalAccept::ffi_deserialize` decodes
1105 // (`nros_cpp_action_goal_accept_decode`).
1106 uint8_t wire[17];
1107 for (int i = 0; i < 16; ++i)
1108 wire[i] = goal_id[i];
1109 wire[16] = accepted ? 1 : 0;
1110 self->accepts_.deliver(owner, wire, sizeof(wire));
1111 }
1112 self->upstream_goal_response(accepted, goal_id);
1113 if (self->user_.goal_response != nullptr)
1114 self->user_.goal_response(accepted, goal_id, self->user_.context);
1115 }
1116
1117 static void on_feedback(const uint8_t goal_id[16], const uint8_t* data, size_t len, void* ctx) {
1118 Client* self = static_cast<Client*>(ctx);
1119 const size_t n = len < sizeof(self->feedback_buf_) ? len : sizeof(self->feedback_buf_);
1120 for (size_t i = 0; i < n; ++i)
1121 self->feedback_buf_[i] = data[i];
1122 self->feedback_len_ = n;
1123 self->feedback_truncated_ = n < len;
1124 self->feedback_ready_ = true;
1125 self->upstream_feedback(goal_id, data, len);
1126 if (self->user_.feedback != nullptr)
1127 self->user_.feedback(goal_id, data, len, self->user_.context);
1128 }
1129
1130 static void on_result(const uint8_t goal_id[16], int32_t status, const uint8_t* data,
1131 size_t len, void* ctx) {
1132 Client* self = static_cast<Client*>(ctx);
1133 ::rclcpp::detail::PendingLink* owner = self->results_.resolve_goal(goal_id);
1134 if (owner != nullptr) self->results_.deliver(owner, data, len);
1135 self->upstream_result(goal_id, status, data, len);
1136 if (self->user_.result != nullptr)
1137 self->user_.result(goal_id, status, data, len, self->user_.context);
1138 }
1139
1140 // upstream's per-goal callbacks (`async_send_goal`). Hosted
1141 // only: `upstream_` is empty on a freestanding build, so these do nothing.
1142#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1148 struct UpstreamGoal {
1149 GoalUUID id;
1150 SendGoalOptions options;
1152 ::std::weak_ptr<GoalHandle> weak;
1153 bool accepted = false;
1154 };
1155#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1158 struct CancelWaiter {
1159 GoalUUID key;
1162 int64_t seq;
1163 ::std::promise<typename CancelResponse::SharedPtr> promise;
1164 CancelCallback callback;
1165 };
1166#endif
1167 struct UpstreamState {
1168 ::std::vector<UpstreamGoal> goals;
1169#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1170 ::std::vector<CancelWaiter> cancels;
1171#endif
1172 ::std::shared_ptr<detail::PumpCell> pump;
1173 };
1174
1175 UpstreamState* upstream_state() {
1176 UpstreamState* st = upstream_.template as<UpstreamState>();
1177 if (st == nullptr) {
1178 st = new UpstreamState();
1179 upstream_.reset(st, [](void* p) { delete static_cast<UpstreamState*>(p); });
1180 }
1181 return st;
1182 }
1183
1184 UpstreamState* upstream_goal(const uint8_t goal_id[16], size_t& at) const {
1185 UpstreamState* st = upstream_.template as<UpstreamState>();
1186 if (st == nullptr) return nullptr;
1187 const GoalUUID id(goal_id);
1188 for (at = 0; at < st->goals.size(); ++at)
1189 if (st->goals[at].id == id) return st;
1190 return nullptr;
1191 }
1192
1194 static void prune_upstream(UpstreamState* st) {
1195 for (size_t i = 0; i < st->goals.size();) {
1196 const UpstreamGoal& g = st->goals[i];
1197 if (g.accepted && !g.handle && g.weak.expired())
1198 st->goals.erase(st->goals.begin() + static_cast<long>(i));
1199 else
1200 ++i;
1201 }
1202 }
1203
1205 ::std::shared_ptr<detail::PumpCell> pump_cell() {
1206 UpstreamState* st = upstream_state();
1207 if (!st->pump) {
1208 st->pump = ::std::make_shared<detail::PumpCell>();
1209 st->pump->fn = &Client::pump_entry;
1210 }
1211 st->pump->ctx = this;
1212 st->pump->executor = executor_;
1213 return st->pump;
1214 }
1215
1216 static void pump_entry(void* ctx) {
1217 Client* self = static_cast<Client*>(ctx);
1218 if (!self->initialized_) return;
1219 (void)nros_cpp_action_client_poll(self->storage_); // cancels too
1220 }
1221#endif
1222
1228 bool upstream_wants(bool accept, const uint8_t goal_id[16]) {
1229#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1230 size_t at = 0;
1231 UpstreamState* st = upstream_goal(goal_id, at);
1232 if (st == nullptr) return false;
1233 UpstreamGoal& g = st->goals[at];
1234 if (accept) {
1235 if (g.accepted) return false;
1236 const bool wanted = static_cast<bool>(g.options.goal_response_callback) ||
1237 static_cast<bool>(g.options.feedback_callback) ||
1238 static_cast<bool>(g.options.result_callback);
1239 if (!wanted) st->goals.erase(st->goals.begin() + static_cast<long>(at));
1240 return wanted;
1241 }
1242 return g.handle && g.handle->result_aware_;
1243#else
1244 (void)accept;
1245 (void)goal_id;
1246 return false;
1247#endif
1248 }
1249
1251 void repoint_pump() {
1252#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1253 UpstreamState* st = upstream_.template as<UpstreamState>();
1254 if (st != nullptr && st->pump) {
1255 st->pump->ctx = this;
1256 st->pump->executor = executor_;
1257 }
1258#endif
1259 }
1260
1265 void abandon_upstream() {
1266#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1267 UpstreamState* st = upstream_.template as<UpstreamState>();
1268 if (st == nullptr) return;
1269 if (st->pump) st->pump->ctx = nullptr;
1270#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1271 for (CancelWaiter& w : st->cancels)
1272 w.promise.set_value(typename CancelResponse::SharedPtr());
1273 st->cancels.clear();
1274 for (UpstreamGoal& g : st->goals) {
1275 if (g.handle && g.handle->result_aware_) {
1276 WrappedResult wrapped;
1277 wrapped.goal_id = g.id;
1278 wrapped.code = ResultCode::UNKNOWN;
1279 g.handle->set_result(wrapped);
1280 }
1281 }
1282#endif
1283 upstream_.reset();
1284#endif
1285 }
1286
1287#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1288 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
1289 send_cancel(const GoalUUID& key, int32_t sec, uint32_t nanosec, CancelCallback& cb) {
1290 using Fut = detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>;
1291 if (!initialized_) return Fut();
1293 if (nros_cpp_action_client_cancel_goals(storage_,
1294 reinterpret_cast<const uint8_t(*)[16]>(key.data()),
1295 sec, nanosec, &seq) != 0)
1296 return Fut();
1297 UpstreamState* st = upstream_state();
1298 CancelWaiter w;
1299 w.key = key;
1300 w.seq = seq;
1301 w.callback = cb;
1302 ::std::shared_future<typename CancelResponse::SharedPtr> f = w.promise.get_future().share();
1303 st->cancels.push_back(static_cast<CancelWaiter&&>(w));
1304 return Fut(f, pump_cell());
1305 }
1306#endif
1307
1317 static void on_cancel_reply(const uint8_t goal[16], int64_t seq, const uint8_t* reply,
1318 size_t len, void* ctx) {
1319 Client* self = static_cast<Client*>(ctx);
1320 int8_t code = 0;
1321 size_t count = 0;
1322 nros_cpp_action_goal_info_t infos[::rclcpp_action::MAX_CANCEL_INFOS];
1323 const bool taken =
1324 nros_cpp_action_cancel_reply_decode(reply, len, &code, infos,
1326 if (self->upstream_cancel_reply(goal, seq, taken, code, infos, count)) return;
1327 if (taken) self->stash_cancel(code);
1328 }
1329
1331 void stash_cancel(int8_t code) {
1332 if (stash_len_ == CANCEL_STASH_CAP) {
1333 stash_head_ = static_cast<uint8_t>((stash_head_ + 1) % CANCEL_STASH_CAP);
1334 --stash_len_;
1335 }
1336 stash_codes_[(stash_head_ + stash_len_) % CANCEL_STASH_CAP] = code;
1337 ++stash_len_;
1338 }
1339
1340 bool take_stray_cancel(int8_t& code) {
1341 if (stash_len_ == 0) return false;
1342 code = stash_codes_[stash_head_];
1343 stash_head_ = static_cast<uint8_t>((stash_head_ + 1) % CANCEL_STASH_CAP);
1344 --stash_len_;
1345 return true;
1346 }
1347
1350 bool upstream_cancel_reply(const uint8_t goal[16], int64_t seq, bool taken, int8_t code,
1351 const nros_cpp_action_goal_info_t* infos, size_t count) {
1352#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1353 UpstreamState* st = upstream_.template as<UpstreamState>();
1354 if (st == nullptr) return false;
1355 const GoalUUID key(goal);
1356 size_t at = 0;
1357 while (at < st->cancels.size() &&
1358 !(seq != ::rclcpp::detail::SEQUENCE_ID_UNKNOWN ? st->cancels[at].seq == seq
1359 : st->cancels[at].key == key))
1360 ++at;
1361 if (at == st->cancels.size()) return false;
1362 CancelWaiter w = static_cast<CancelWaiter&&>(st->cancels[at]);
1363 st->cancels.erase(st->cancels.begin() + static_cast<long>(at));
1364 if (!taken) { // malformed: the request is over, with no response
1365 w.promise.set_value(typename CancelResponse::SharedPtr());
1366 if (w.callback) w.callback(typename CancelResponse::SharedPtr());
1367 return true;
1368 }
1369 typename CancelResponse::SharedPtr resp = ::std::make_shared<CancelResponse>();
1370 resp->return_code = code;
1371 const size_t n =
1373 for (size_t k = 0; k < n; ++k) {
1374 detail::CancelGoalInfo info;
1375 for (int b = 0; b < 16; ++b)
1376 info.goal_id.uuid[b] = infos[k].uuid[b];
1377 info.stamp.sec = infos[k].stamp_sec;
1378 info.stamp.nanosec = infos[k].stamp_nanosec;
1379 resp->goals_canceling.push_back(info);
1380 }
1381 w.promise.set_value(resp);
1382 if (w.callback) w.callback(resp);
1383 return true;
1384#else
1385 (void)goal;
1386 (void)seq;
1387 (void)taken;
1388 (void)code;
1389 (void)infos;
1390 (void)count;
1391 return false;
1392#endif
1393 }
1394
1395 void upstream_goal_response(bool accepted, const uint8_t goal_id[16]) {
1396#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1397 size_t at = 0;
1398 UpstreamState* st = upstream_goal(goal_id, at);
1399 if (st == nullptr) return;
1400 UpstreamGoal& g = st->goals[at];
1401 if (g.accepted) return; // a duplicate acceptance
1402 auto cb = g.options.goal_response_callback;
1403 if (!accepted) {
1404 st->goals.erase(st->goals.begin() + static_cast<long>(at));
1405 if (cb) cb(nullptr);
1406 return;
1407 }
1408 typename GoalHandle::SharedPtr handle = ::std::make_shared<GoalHandle>(g.id);
1409 handle->feedback_aware_ = static_cast<bool>(g.options.feedback_callback);
1410 handle->result_aware_ = static_cast<bool>(g.options.result_callback);
1411 g.accepted = true;
1412 if (handle->result_aware_)
1413 g.handle = handle; // the pending result request keeps it, as upstream's does
1414 else
1415 g.weak = handle; // feedback-only: the caller's copy keeps it
1416 if (!handle->feedback_aware_ && !handle->result_aware_)
1417 st->goals.erase(st->goals.begin() + static_cast<long>(at));
1418 // `g` may be gone from here on: the callback can send another goal.
1419 if (cb) cb(handle);
1420 if (handle->result_aware_ && !get_result_async(goal_id).ok()) {
1421 // Nobody could ever answer it: unbind, as upstream would have thrown.
1422 handle->result_aware_ = false;
1423 if (upstream_goal(goal_id, at) != nullptr) {
1424 st->goals[at].handle.reset();
1425 st->goals[at].weak = handle;
1426 }
1427 }
1428#else
1429 (void)accepted;
1430 (void)goal_id;
1431#endif
1432 }
1433
1434 void upstream_feedback(const uint8_t goal_id[16], const uint8_t* data, size_t len) {
1435#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1436 size_t at = 0;
1437 UpstreamState* st = upstream_goal(goal_id, at);
1438 if (st == nullptr) return;
1439 UpstreamGoal& g = st->goals[at];
1440 typename GoalHandle::SharedPtr handle = g.handle ? g.handle : g.weak.lock();
1441 if (!handle) {
1442 // A feedback-only goal whose handle the caller dropped.
1443 if (g.accepted) st->goals.erase(st->goals.begin() + static_cast<long>(at));
1444 return;
1445 }
1446 auto cb = g.options.feedback_callback;
1447 if (!cb) return;
1448 ::std::shared_ptr<FeedbackType> fb = ::std::make_shared<FeedbackType>();
1449 if (FeedbackType::ffi_deserialize(data, len, fb.get()) != 0) return;
1450 cb(handle, fb);
1451#else
1452 (void)goal_id;
1453 (void)data;
1454 (void)len;
1455#endif
1456 }
1457
1458 void upstream_result(const uint8_t goal_id[16], int32_t status, const uint8_t* data,
1459 size_t len) {
1460#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1461 size_t at = 0;
1462 UpstreamState* st = upstream_goal(goal_id, at);
1463 if (st == nullptr) return;
1464 UpstreamGoal g = st->goals[at];
1465 st->goals.erase(st->goals.begin() + static_cast<long>(at));
1466 typename GoalHandle::SharedPtr handle = g.handle ? g.handle : g.weak.lock();
1467 if (handle) handle->status_ = static_cast<int8_t>(status);
1468 WrappedResult wrapped;
1469 wrapped.goal_id = g.id;
1470 wrapped.code = (status == 4 || status == 5 || status == 6) ? static_cast<ResultCode>(status)
1471 : ResultCode::UNKNOWN;
1472 wrapped.result = ::std::make_shared<ResultType>();
1473 if (ResultType::ffi_deserialize(data, len, wrapped.result.get()) != 0) {
1474 wrapped.result.reset();
1475 }
1476#ifdef NROS_CPP_HAS_STD_PROMISE // hosted-family: action-interop
1477 if (handle) handle->set_result(wrapped);
1478#endif
1479 if (g.options.result_callback) g.options.result_callback(wrapped);
1480#else
1481 (void)goal_id;
1482 (void)status;
1483 (void)data;
1484 (void)len;
1485#endif
1486 }
1487
1491 static nros_cpp_ret_t take_feedback(void* ctx, uint8_t* buf, size_t cap, size_t* out_len) {
1492 Client* self = static_cast<Client*>(ctx);
1493 *out_len = 0;
1494 if (!self->initialized_)
1496 if (!self->feedback_ready_) (void)nros_cpp_action_client_poll(self->storage_);
1497 if (!self->feedback_ready_) return 0;
1498 self->feedback_ready_ = false;
1499 if (self->feedback_truncated_ || self->feedback_len_ > cap) {
1500 *out_len = self->feedback_len_;
1501 return static_cast<nros_cpp_ret_t>(::rclcpp::ErrorCode::Error);
1502 }
1503 for (size_t i = 0; i < self->feedback_len_; ++i)
1504 buf[i] = self->feedback_buf_[i];
1505 *out_len = self->feedback_len_;
1506 return 0;
1507 }
1508
1509 alignas(8) uint8_t storage_[NROS_CPP_ACTION_CLIENT_STORAGE_SIZE];
1510 void* executor_; // Stashed executor handle for blocking helpers
1511 bool initialized_;
1512 ::rclcpp::Stream<FeedbackType> feedback_stream_;
1513 // A `char action_name_[256]` was once here for an accessor that was
1514 // never written, and was deleted as dead
1515 // state. A later change writes `get_action_name()`, so it is back and
1516 // it is read — see the note on `Server<A>::get_action_name`.
1517 char action_name_[::rclcpp_action::ACTION_NAME_MAX];
1526 SendGoalOptions user_;
1528 uint8_t feedback_buf_[::rclcpp::rx_buffer_capacity<FeedbackType>::value];
1529 size_t feedback_len_ = 0;
1530 bool feedback_ready_ = false;
1531 bool feedback_truncated_ = false;
1534 int8_t stash_codes_[CANCEL_STASH_CAP] = {};
1535 uint8_t stash_head_ = 0;
1536 uint8_t stash_len_ = 0;
1540 detail::HostedBox upstream_;
1541};
1542
1543} // namespace rclcpp_action
1544
1545// Out-of-line definition of Node::create_action_client<A>().
1546#include "rclcpp/node.hpp"
1547
1548namespace rclcpp {} // namespace rclcpp
1549
1550namespace rclcpp {
1551template <typename A>
1553 const ::rclcpp::QoS& qos) {
1554 if (!initialized_) return Result(::rclcpp::ErrorCode::NotInitialized);
1556 const nros_cpp_action_hashes_t hashes = ::rclcpp::detail::action_endpoint_hashes<A>();
1557 nros_cpp_ret_t ret = nros_cpp_action_client_create(
1558 &handle_, action_name, A::TYPE_NAME, A::Goal::TYPE_HASH, &hashes, ffi_qos, out.storage_);
1559 if (ret == 0) {
1560 out.executor_ = executor_handle_;
1561 // The client's own callbacks, from the start: they feed
1562 // its goal-keyed futures and its feedback slot.
1563 out.initialized_ = true;
1564 (void)out.install_dispatch();
1565 // Remember the name for `get_action_name()`, truncating
1566 // at `rclcpp_action::ACTION_NAME_MAX` exactly as the server and the polling tiers do.
1567 ::rclcpp::detail::assign_entity_name(out.action_name_, action_name);
1568 out.initialized_ = true;
1569 }
1570 return Result(ret);
1571}
1572} // namespace rclcpp
1573
1574namespace rclcpp {} // namespace rclcpp
1575
1576#endif // NROS_RCLCPP_ACTION_CLIENT_HPP
Definition client.hpp:130
Definition future.hpp:337
Definition node.hpp:874
Result create_action_client(::rclcpp_action::Client< A > &out, const char *action_name, const ::rclcpp::QoS &qos=::rclcpp::QoS::services())
Definition client.hpp:1552
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:227
Definition stream.hpp:62
Definition client.hpp:129
typename A::Result Result
Definition client.hpp:132
detail::Refused< ClientGoalHandle > SharedPtr
Definition client.hpp:147
bool is_feedback_aware() const
A feedback callback was given for this goal.
Definition client.hpp:163
const GoalUUID & get_goal_id() const
The goal's id.
Definition client.hpp:159
int8_t get_status() const
The last status this client saw for the goal (action_msgs numbers).
Definition client.hpp:161
ClientGoalHandle(const ClientGoalHandle &)=delete
ClientGoalHandle & operator=(const ClientGoalHandle &)=delete
detail::Refused< ClientGoalHandle > ConstSharedPtr
Definition client.hpp:148
detail::Refused< ClientGoalHandle > WrappedResult
Definition client.hpp:149
typename A::Feedback Feedback
Definition client.hpp:131
bool is_result_aware() const
A result callback was given for this goal (its result was requested).
Definition client.hpp:165
ClientGoalHandle(const GoalUUID &id)
Definition client.hpp:152
Definition client.hpp:256
::rclcpp::Result send_goal(const GoalType &goal, uint8_t goal_id[16])
Definition client.hpp:406
::rclcpp_action::ClientGoalHandle< A > GoalHandle
Upstream's goal handle for this action.
Definition client.hpp:267
::rclcpp::Result wait_for_action_server(uint32_t timeout_ms)
Definition client.hpp:337
typename A::Result ResultType
Definition client.hpp:259
::rclcpp::Result send_goal(const GoalType &goal, ::rclcpp_action::GoalUUID &goal_id)
Definition client.hpp:420
typename A::Feedback FeedbackType
Definition client.hpp:260
bool action_server_is_ready() const
Definition client.hpp:379
::rclcpp::Result get_result(const uint8_t goal_id[16], ResultType &result)
Definition client.hpp:432
::rclcpp::Result wait_for_action_server()
REFUSED — wait_for_action_server() with no budget.
Definition client.hpp:344
::rclcpp::Result get_result(const ::rclcpp_action::GoalUUID &goal_id, ResultType &result)
get_result taking a GoalUUID value.
Definition client.hpp:437
detail::Refused< Client > CancelResponse
Definition client.hpp:277
::rclcpp::Owned< Client > SharedPtr
Definition client.hpp:265
const char * get_action_name() const
Definition client.hpp:391
typename GoalHandle::WrappedResult WrappedResult
Upstream's result wrapper (hosted; refused on a freestanding build).
Definition client.hpp:269
::rclcpp::Result get_result_sized(const uint8_t goal_id[16], ResultType &result)
Definition client.hpp:445
typename A::Goal GoalType
Definition client.hpp:258
Inline storage-size macros for opaque entity buffers.
rclcpp::Future<T> — single-shot deferred result.
int nros_cpp_ret_t
Definition future.hpp:25
constexpr nros_cpp_qos_t qos_to_ffi(const QoS &qos)
Definition qos.hpp:719
nros_cpp_action_hashes_t action_endpoint_hashes()
Definition server.hpp:89
constexpr int64_t SEQUENCE_ID_UNKNOWN
Definition future.hpp:59
Definition node.hpp:203
::rclcpp::Result send_goal_async(const GoalType &goal, uint8_t goal_id[16])
Definition client.hpp:678
::rclcpp::Result try_recv_feedback(FeedbackType &feedback)
Definition client.hpp:577
::rclcpp::Future< GoalAccept > async_send_goal(const GoalType &, const SendGoalOptions &=SendGoalOptions())
REFUSED on a freestanding build.
Definition client.hpp:729
::rclcpp::Stream< FeedbackType > & feedback_stream()
Definition client.hpp:619
GoalStatus
Definition server.hpp:263
@ Accepted
Definition server.hpp:265
::rclcpp::Result cancel_goal(const uint8_t goal_id[16])
Definition client.hpp:872
static const size_t ACTION_NAME_MAX
Definition server.hpp:191
::rclcpp::Result try_recv_cancel_response(::rclcpp_action::CancelReturnCode &out)
Definition client.hpp:894
void poll()
Definition client.hpp:952
void async_cancel_goals_before(Args &&...)
REFUSED without std::promise.
Definition client.hpp:853
::rclcpp::Result try_recv_feedback_sized(FeedbackType &feedback)
Definition client.hpp:583
static const size_t CANCEL_STASH_CAP
Definition client.hpp:114
::rclcpp::Future< ResultType, Cap > get_result_future_sized(const uint8_t goal_id[16])
Definition client.hpp:554
::rclcpp::Result get_result_async(const uint8_t goal_id[16])
Definition client.hpp:916
Client & operator=(Client &&other)
Definition client.hpp:1000
static const size_t MAX_CANCEL_INFOS
Definition client.hpp:107
void async_cancel_all_goals(Args &&...)
REFUSED without std::promise.
Definition client.hpp:849
CancelReturnCode
Definition server.hpp:254
::rclcpp::Result set_callbacks(const SendGoalOptions &options)
Definition client.hpp:939
bool is_valid() const
Check if the action client is initialized and valid.
Definition client.hpp:959
ResultCode
Definition client.hpp:96
@ ABORTED
Definition client.hpp:100
@ UNKNOWN
Definition client.hpp:97
@ CANCELED
Definition client.hpp:99
@ SUCCEEDED
Definition client.hpp:98
::rclcpp::Future< ResultType > get_result_future(const uint8_t goal_id[16])
Definition client.hpp:539
void async_cancel_goal(Args &&...)
REFUSED without std::promise.
Definition client.hpp:845
Definition rclcpp.hpp:75
bool ok()
Definition node.hpp:3641
@ Error
Generic failure not covered by a more specific code.
Definition result.hpp:118
@ NotInitialized
Definition result.hpp:131
@ Unsupported
Operation not implemented by the active backend.
Definition result.hpp:152
@ TryAgain
Transient — no data ready yet (non-blocking take). Retry later.
Definition result.hpp:148
ResultOf< void > Result
Definition result.hpp:247
rclcpp::Node and global session helpers.
**auto handle
Definition client.hpp:127
nros_cpp_ret_t nros_cpp_action_client_set_callbacks(void *handle, nros_cpp_action_client_goal_response_callback_t goal_response, nros_cpp_action_client_feedback_callback_t feedback, nros_cpp_action_client_result_callback_t result, void *context)
::rclcpp::Result send_goal(const GoalType &goal, uint8_t goal_id[16])
Definition client.hpp:637
::rclcpp::Result wait_for_action_server()
REFUSED — wait_for_action_server() with no budget.
Definition client.hpp:575
nros_cpp_ret_t nros_cpp_action_client_set_cancel_reply_callback(void *handle, nros_cpp_action_client_cancel_reply_callback_t cb)
typename GoalHandle::WrappedResult WrappedResult
Upstream's result wrapper (hosted; refused on a freestanding build).
Definition client.hpp:500
typename A::Goal GoalType
Definition client.hpp:489
typename A::Feedback FeedbackType
Definition client.hpp:491
void(* nros_cpp_action_client_goal_response_callback_t)(bool accepted, const uint8_t goal_id[16], void *ctx)
Definition client.hpp:43
void(* nros_cpp_action_client_feedback_callback_t)(const uint8_t goal_id[16], const uint8_t *data, size_t len, void *ctx)
Definition client.hpp:46
void(* nros_cpp_action_client_cancel_reply_callback_t)(const uint8_t goal_id[16], int64_t seq, const uint8_t *reply, size_t reply_len, void *ctx)
Definition client.hpp:60
::rclcpp::Owned< Client > SharedPtr
Definition client.hpp:496
void(* nros_cpp_action_client_result_callback_t)(const uint8_t goal_id[16], int32_t status, const uint8_t *data, size_t len, void *ctx)
Definition client.hpp:49
detail::Refused< Client > CancelResponse
Definition client.hpp:508
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.
rclcpp::Stream<T> — multi-shot message receiver.
Definition qos.hpp:84
Definition future.hpp:126
void * forget_ctx
Definition future.hpp:136
void(* forget)(void *ctx, const uint8_t goal[16])
Definition future.hpp:135
Definition client.hpp:292
static int ffi_deserialize(const uint8_t *data, size_t len, GoalAccept *out)
Definition client.hpp:306
static const size_t SERIALIZED_SIZE_MAX
Definition client.hpp:301
uint8_t goal_id[16]
Definition client.hpp:296
bool accepted
Definition client.hpp:297
Definition server.hpp:126
uint8_t * data()
Pointer to the 16 bytes — what the raw-array overloads and the FFI take.
Definition server.hpp:144
Definition client.hpp:646
SendGoalOptions()
Definition client.hpp:666
detail::UpstreamCallback< void(typename GoalHandle::SharedPtr, FeedbackPtr)> feedback_callback
Upstream's: feedback for the goal.
Definition client.hpp:661
void(* feedback)(const uint8_t goal_id[16], const uint8_t *data, size_t len, void *ctx)
Called when feedback is received for the goal.
Definition client.hpp:650
detail::UpstreamCallback< void(const WrappedResult &)> result_callback
Definition client.hpp:664
void(* goal_response)(bool accepted, const uint8_t goal_id[16], void *ctx)
Called when the server accepts or rejects the goal.
Definition client.hpp:648
void(* result)(const uint8_t goal_id[16], int32_t status, const uint8_t *data, size_t len, void *ctx)
Called when the result is received.
Definition client.hpp:652
detail::UpstreamCallback< void(typename GoalHandle::SharedPtr)> goal_response_callback
Upstream's: the goal was accepted (a handle) or rejected (null).
Definition client.hpp:658
void * context
User context pointer passed to all callbacks.
Definition client.hpp:655