nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
server.hpp
Go to the documentation of this file.
1// nros-cpp: Action server class
2// Freestanding C++ — no exceptions, no STL required
3
9
10#ifndef NROS_RCLCPP_ACTION_SERVER_HPP
11#define NROS_RCLCPP_ACTION_SERVER_HPP
12
13#include <cstdint>
14#include <cstddef>
15#include <string.h>
16
17#include "rclcpp/detail/callback_context.hpp" // the one-word context carrier
19#include "rclcpp/detail/entity_name.hpp" // The one entity-name copy
20#include "rclcpp/owned.hpp" // `Server<A>::SharedPtr`
21#include "rclcpp/result.hpp"
22#include "rclcpp_action/detail/upstream.hpp" // Hosted goal-handle plumbing
23// The feedback/result TX buffers below size through
24// `detail::buffer_bounds`, so this header needs it directly rather than by
25// luck through a transitive include.
26#include "rclcpp/size_bound.hpp"
27
28// Most action_server FFI symbols come from
29// `nros_cpp_ffi.h`; cbindgen renders Rust `*const [u8; 16]` as
30// pointer-to-array (`const uint8_t (*goal_id)[16]`), so callsites
31// below `reinterpret_cast` from the array-decay shape used in C++
32// member-function signatures.
33//
34// `nros_cpp_action_server_set_callbacks` is excluded from cbindgen
35// output (its Rust signature uses `Option<extern "C" fn(...)>`,
36// which cbindgen renders as opaque structs). Declare locally below
37// with plain function-pointer typedefs that match the actual ABI.
38#include "rclcpp/detail/nros_cpp_ffi.h"
39
40extern "C" {
41typedef int32_t (*nros_cpp_goal_callback_t)(const uint8_t goal_id[16], const uint8_t* data,
42 size_t len, void* ctx);
43typedef int32_t (*nros_cpp_cancel_callback_t)(const uint8_t goal_id[16], void* ctx);
44/* Post-accept hook. Excluded from cbindgen for the same reason as
45 * the two above (`Option<extern "C" fn>` renders as an opaque struct), so this
46 * declaration is MIRRORED in `rclc/component.h` as well; `just check c`'s
47 * cross-include TU is the gate that catches a half-updated mirror. */
48typedef void (*nros_cpp_accepted_callback_t)(const uint8_t goal_id[16], void* ctx);
49
52 void* ctx);
55 void* ctx);
56} // extern "C"
57
58namespace rclcpp {
59namespace detail {
60
61// The hash each of an action's five endpoints is keyed on. An
62// Iron+ peer keys every endpoint on its own type, so the one `Goal::TYPE_HASH`
63// these entry points used to pass matched none of them. Generated action types
64// carry the five as `<Endpoint>_HASH` members; a type generated by an older
65// `nros` does not, and gets all-NULL, which the runtime reads as "use the type
66// hash" (the old behaviour). C++14, so detection is spelled by hand.
67template <class...> struct nros_void_ {
68 typedef void type;
69};
70
71template <class A, class = void> struct action_endpoint_hashes_of {
72 static nros_cpp_action_hashes_t get() {
73 nros_cpp_action_hashes_t h = {nullptr, nullptr, nullptr, nullptr, nullptr};
74 return h;
75 }
76};
77
78template <class A>
80 typename nros_void_<decltype(A::SEND_GOAL_SERVICE_HASH)>::type> {
81 static nros_cpp_action_hashes_t get() {
82 nros_cpp_action_hashes_t h = {A::SEND_GOAL_SERVICE_HASH, A::CANCEL_GOAL_SERVICE_HASH,
83 A::GET_RESULT_SERVICE_HASH, A::FEEDBACK_MESSAGE_HASH,
84 A::STATUS_MESSAGE_HASH};
85 return h;
86 }
87};
88
89template <class A> inline nros_cpp_action_hashes_t action_endpoint_hashes() {
91}
92
93} // namespace detail
94} // namespace rclcpp
95
96// The goal vocabulary lives in `rclcpp_action::`, where upstream
97// keeps it. It was `nros::` until then, and `rclcpp_action::` re-exported two
98// of the five names.
99namespace rclcpp_action {
100
126struct GoalUUID {
128 static const size_t SIZE = 16;
129
131 uint8_t uuid[16];
132
134 GoalUUID() : uuid{} {}
135
137 explicit GoalUUID(const uint8_t* src) : uuid{} {
138 for (size_t i = 0; i < SIZE; ++i) {
139 uuid[i] = src[i];
140 }
141 }
142
144 uint8_t* data() { return uuid; }
145 const uint8_t* data() const { return uuid; }
146
148 bool is_zero() const {
149 for (size_t i = 0; i < SIZE; ++i) {
150 if (uuid[i] != 0) return false;
151 }
152 return true;
153 }
154
155 bool operator==(const GoalUUID& other) const {
156 for (size_t i = 0; i < SIZE; ++i) {
157 if (uuid[i] != other.uuid[i]) return false;
158 }
159 return true;
160 }
161
162 bool operator!=(const GoalUUID& other) const { return !(*this == other); }
163
167 bool operator<(const GoalUUID& other) const {
168 for (size_t i = 0; i < SIZE; ++i) {
169 if (uuid[i] != other.uuid[i]) return uuid[i] < other.uuid[i];
170 }
171 return false;
172 }
173};
174
175static_assert(sizeof(GoalUUID) == 16,
176 "rclcpp_action::GoalUUID must be exactly the 16 bytes the FFI passes as uint8_t[16]");
177static_assert(alignof(GoalUUID) == alignof(uint8_t),
178 "rclcpp_action::GoalUUID must not add alignment over its uint8_t[16]");
179
191static const size_t ACTION_NAME_MAX = 256;
192
206enum class GoalResponse : int8_t {
210};
211
226enum class CancelResponse : int8_t {
229};
230
236constexpr int32_t to_ffi(GoalResponse r) {
237 return (r == GoalResponse::ACCEPT_AND_EXECUTE) ? 1
239 : 0;
240}
241
244constexpr int32_t to_ffi(CancelResponse r) {
245 return (r == CancelResponse::ACCEPT) ? 1 : 0;
246}
247
254enum class CancelReturnCode : int8_t {
255 Ok = 0,
259};
260
263enum class GoalStatus : int8_t {
271};
272
273} // namespace rclcpp_action
274
282namespace rclcpp {
283class Node;
284}
285
286// ============================================================================
287// `rclcpp_action::Server<A>` -- DEFINED here (rclcpp_action:: is the home)
288// ============================================================================
289//
290// The definition moved from `nros::ActionServer<A>` to
291// `rclcpp_action::Server<A>` (the `nros::` alias is gone). This one
292// is a RENAME as well as a move -- upstream's namespace already says "action",
293// so the type is `Server`, not `ActionServer`.
294//
295// The vocabulary the signatures use (`GoalUUID`, `GoalResponse`,
296// `CancelResponse`, `GoalStatus`, `CancelReturnCode`) is declared above, in
297// `rclcpp_action::` where upstream keeps it (it was `nros::`
298// before). `GoalResponse` and `CancelResponse` carry upstream's values
299// now, so a ported `rclcpp_action::GoalResponse::REJECT` compiles AND
300// means what it meant upstream. Before that they were 0-based, and exposing
301// them under the upstream name would have turned a compile error into a silent
302// renumbering.
303namespace rclcpp_action {
304
305template <typename A> class ServerGoalHandle;
306namespace detail {
307template <typename A> struct ServerUpstream;
308} // namespace detail
309
350template <typename A> class Server {
351 public:
352 using GoalType = typename A::Goal;
353 using ResultType = typename A::Result;
354 using FeedbackType = typename A::Feedback;
355
360 using SharedPtr = ::rclcpp::Owned<Server>;
363
365 using TypedGoalFn = ::rclcpp_action::GoalResponse (*)(const uint8_t uuid[16],
366 const GoalType& goal);
368 using TypedGoalFnWithCtx = ::rclcpp_action::GoalResponse (*)(const uint8_t uuid[16],
369 const GoalType& goal, void* ctx);
371 using TypedCancelFn = ::rclcpp_action::CancelResponse (*)(const uint8_t uuid[16]);
373 using TypedCancelFnWithCtx = ::rclcpp_action::CancelResponse (*)(const uint8_t uuid[16],
374 void* ctx);
376 using TypedAcceptedFn = void (*)(const uint8_t uuid[16]);
378 using TypedAcceptedFnWithCtx = void (*)(const uint8_t uuid[16], void* ctx);
380 using TypedVisitorFn = void (*)(const uint8_t uuid[16], ::rclcpp_action::GoalStatus status);
381
388 template <typename F>::rclcpp::Result set_goal_callback(F f) {
389 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
390 user_goal_fn_ = TypedGoalFn(f); // compile error if F is not convertible
391 user_goal_fn_ctx_ = nullptr; // mutually exclusive with _with_ctx
392 user_goal_ctx_ = nullptr;
393 return install_callbacks();
394 }
395
404 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
405 user_goal_fn_ctx_ = f;
406 user_goal_ctx_ = ctx;
407 user_goal_fn_ = nullptr;
408 return install_callbacks();
409 }
410
414 template <typename F>::rclcpp::Result set_cancel_callback(F f) {
415 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
416 user_cancel_fn_ = TypedCancelFn(f); // compile error if F is not convertible
417 user_cancel_fn_ctx_ = nullptr; // mutually exclusive with _with_ctx
418 user_cancel_ctx_ = nullptr;
419 return install_callbacks();
420 }
421
429 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
430 user_cancel_fn_ctx_ = f;
431 user_cancel_ctx_ = ctx;
432 user_cancel_fn_ = nullptr;
433 return install_callbacks();
434 }
435
456 template <typename F>::rclcpp::Result set_accepted_callback(F f) {
457 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
458 user_accepted_fn_ = TypedAcceptedFn(f); // compile error if F is not convertible
459 user_accepted_fn_ctx_ = nullptr; // mutually exclusive with _with_ctx
460 user_accepted_ctx_ = nullptr;
461 return install_callbacks();
462 }
463
469 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
470 user_accepted_fn_ctx_ = f;
471 user_accepted_ctx_ = ctx;
472 user_accepted_fn_ = nullptr;
473 return install_callbacks();
474 }
475
485 ::rclcpp::Result publish_feedback(const uint8_t goal_id[16], const FeedbackType& feedback) {
486 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
487
488 uint8_t buf[::rclcpp::detail::buffer_bounds<FeedbackType>::tx];
489 size_t len = 0;
490 if (FeedbackType::ffi_serialize(&feedback, buf, sizeof(buf), &len) != 0) {
491 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
492 }
493 return ::rclcpp::Result(nros_cpp_action_server_publish_feedback(
494 storage_, executor_, reinterpret_cast<const uint8_t(*)[16]>(goal_id), buf, len));
495 }
496
511 const ResultType& result) {
512 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
513
514 uint8_t buf[::rclcpp::detail::buffer_bounds<ResultType>::tx];
515 size_t len = 0;
516 if (ResultType::ffi_serialize(&result, buf, sizeof(buf), &len) != 0) {
517 return ::rclcpp::Result(::rclcpp::ErrorCode::Error);
518 }
519 return ::rclcpp::Result(nros_cpp_action_server_complete_goal(
520 storage_, executor_, reinterpret_cast<const uint8_t(*)[16]>(goal_id),
521 static_cast<int32_t>(status), buf, len));
522 }
523
526 ::rclcpp::Result complete_goal(const uint8_t goal_id[16], const ResultType& result) {
528 }
529
531 ::rclcpp::Result publish_feedback(const ::rclcpp_action::GoalUUID& goal_id,
532 const FeedbackType& feedback) {
533 return publish_feedback(goal_id.data(), feedback);
534 }
535
537 ::rclcpp::Result complete_goal(const ::rclcpp_action::GoalUUID& goal_id,
538 ::rclcpp_action::GoalStatus status, const ResultType& result) {
539 return complete_goal(goal_id.data(), status, result);
540 }
541
543 ::rclcpp::Result complete_goal(const ::rclcpp_action::GoalUUID& goal_id,
544 const ResultType& result) {
545 return complete_goal(goal_id.data(), ::rclcpp_action::GoalStatus::Succeeded, result);
546 }
547
562 ::rclcpp::Result execute(const uint8_t goal_id[16]) {
563 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
564 return ::rclcpp::Result(nros_cpp_action_server_execute(
565 storage_, executor_, reinterpret_cast<const uint8_t(*)[16]>(goal_id)));
566 }
567
569 ::rclcpp::Result execute(const ::rclcpp_action::GoalUUID& goal_id) {
570 return execute(goal_id.data());
571 }
572
589 ::rclcpp::Result succeed(const uint8_t goal_id[16], const ResultType& result) {
591 }
592
597 ::rclcpp::Result abort(const uint8_t goal_id[16], const ResultType& result) {
599 }
600
609 ::rclcpp::Result canceled(const uint8_t goal_id[16], const ResultType& result) {
611 }
612
614 ::rclcpp::Result succeed(const ::rclcpp_action::GoalUUID& goal_id, const ResultType& result) {
615 return complete_goal(goal_id.data(), ::rclcpp_action::GoalStatus::Succeeded, result);
616 }
617
619 ::rclcpp::Result abort(const ::rclcpp_action::GoalUUID& goal_id, const ResultType& result) {
620 return complete_goal(goal_id.data(), ::rclcpp_action::GoalStatus::Aborted, result);
621 }
622
624 ::rclcpp::Result canceled(const ::rclcpp_action::GoalUUID& goal_id, const ResultType& result) {
625 return complete_goal(goal_id.data(), ::rclcpp_action::GoalStatus::Canceled, result);
626 }
627
649 template <typename F>::rclcpp::Result for_each_active_goal(F f) {
650 if (!initialized_) return ::rclcpp::Result(::rclcpp::ErrorCode::NotInitialized);
651 TypedVisitorFn visitor = TypedVisitorFn(f); // compile error if F is not convertible
652
653 auto trampoline = [](const uint8_t goal_id[16], int8_t status, void* ctx) {
654 TypedVisitorFn fn = ::rclcpp::detail::fn_from_context<TypedVisitorFn>(ctx);
655 if (fn == nullptr) return;
656 fn(goal_id, static_cast<::rclcpp_action::GoalStatus>(status));
657 };
658 return ::rclcpp::Result(nros_cpp_action_server_for_each_active_goal(
659 storage_, executor_,
660 reinterpret_cast<void (*)(const uint8_t(*)[16], int8_t, void*)>(+trampoline),
661 ::rclcpp::detail::fn_to_context(visitor)));
662 }
663
686 const char* get_action_name() const { return initialized_ ? action_name_ : ""; }
687
695 bool goal_exists(const uint8_t goal_id[16]) const {
696 if (!initialized_) return false;
697 return nros_cpp_action_server_goal_exists(storage_, executor_,
698 reinterpret_cast<const uint8_t(*)[16]>(goal_id));
699 }
700
702 bool goal_exists(const ::rclcpp_action::GoalUUID& goal_id) const {
703 return goal_exists(goal_id.data());
704 }
705
707 bool is_valid() const { return initialized_; }
708
724 if (initialized_) {
725 nros_cpp_action_server_detach(storage_, executor_);
726 nros_cpp_action_server_destroy(storage_);
727 initialized_ = false;
728 }
729 }
730
731 // Move semantics (non-copyable). Relocation goes through the
732 // `nros_cpp_action_server_relocate` runtime call; then
733 // `nros_cpp_action_server_retarget` re-points the executor's arena entry
734 // (whose callback context is the STORAGE address) at the new storage —
735 // without it a moved server dispatched through the moved-from
736 // bytes — and `install_callbacks()` re-registers the goal/cancel
737 // trampolines with the new `this` as THEIR context.
738 Server(Server&& other)
739 : executor_(other.executor_), user_goal_fn_(other.user_goal_fn_),
740 user_goal_fn_ctx_(other.user_goal_fn_ctx_), user_goal_ctx_(other.user_goal_ctx_),
741 user_cancel_fn_(other.user_cancel_fn_), user_cancel_fn_ctx_(other.user_cancel_fn_ctx_),
742 user_cancel_ctx_(other.user_cancel_ctx_), user_accepted_fn_(other.user_accepted_fn_),
743 user_accepted_fn_ctx_(other.user_accepted_fn_ctx_),
744 user_accepted_ctx_(other.user_accepted_ctx_), action_name_{},
745 initialized_(other.initialized_),
746 upstream_(static_cast<detail::HostedBox&&>(other.upstream_)) {
747 ::memcpy(action_name_, other.action_name_, sizeof(action_name_));
748 if (other.initialized_) {
749 nros_cpp_action_server_relocate(other.storage_, storage_);
750 // The arena entry follows the storage.
751 (void)nros_cpp_action_server_retarget(storage_, executor_);
752 other.initialized_ = false;
753 (void)install_callbacks();
754 }
756 }
757
759 if (this != &other) {
760 if (initialized_) {
761 // Same pairing as `~Server()`: release the arena
762 // entry this storage owns before the storage is overwritten by
763 // the relocate below.
764 nros_cpp_action_server_detach(storage_, executor_);
765 nros_cpp_action_server_destroy(storage_);
766 }
767 executor_ = other.executor_;
768 user_goal_fn_ = other.user_goal_fn_;
769 user_goal_fn_ctx_ = other.user_goal_fn_ctx_;
770 user_goal_ctx_ = other.user_goal_ctx_;
771 user_cancel_fn_ = other.user_cancel_fn_;
772 user_cancel_fn_ctx_ = other.user_cancel_fn_ctx_;
773 user_cancel_ctx_ = other.user_cancel_ctx_;
774 user_accepted_fn_ = other.user_accepted_fn_;
775 user_accepted_fn_ctx_ = other.user_accepted_fn_ctx_;
776 user_accepted_ctx_ = other.user_accepted_ctx_;
777 ::memcpy(action_name_, other.action_name_, sizeof(action_name_));
778 initialized_ = other.initialized_;
779 upstream_ = static_cast<detail::HostedBox&&>(other.upstream_);
780 if (other.initialized_) {
781 nros_cpp_action_server_relocate(other.storage_, storage_);
782 // The arena entry follows the storage.
783 (void)nros_cpp_action_server_retarget(storage_, executor_);
784 other.initialized_ = false;
785 (void)install_callbacks();
786 }
788 }
789 return *this;
790 }
791
795 : executor_(nullptr), user_goal_fn_(nullptr), user_goal_fn_ctx_(nullptr),
796 user_goal_ctx_(nullptr), user_cancel_fn_(nullptr), user_cancel_fn_ctx_(nullptr),
797 user_cancel_ctx_(nullptr), user_accepted_fn_(nullptr), user_accepted_fn_ctx_(nullptr),
798 user_accepted_ctx_(nullptr), action_name_{}, initialized_(false) {}
799
800 private:
801 Server(const Server&) = delete;
802 Server& operator=(const Server&) = delete;
803
804 friend class ::rclcpp::Node;
805 // The hosted goal handle asks this server for its goal's
806 // status, and `create_server` binds upstream's callbacks onto it.
807 friend class ::rclcpp_action::ServerGoalHandle<A>;
808 friend struct detail::ServerUpstream<A>;
809
811 bool goal_status(const uint8_t goal_id[16], int8_t& out) {
812 if (!initialized_) return false;
813 struct Query {
814 const uint8_t* id;
815 int8_t status;
816 bool found;
817 } q = {goal_id, 0, false};
818 auto visit = [](const uint8_t(*id)[16], int8_t status, void* ctx) {
819 Query* qq = static_cast<Query*>(ctx);
820 if (::memcmp(*id, qq->id, 16) == 0) {
821 qq->status = status;
822 qq->found = true;
823 }
824 };
825 if (nros_cpp_action_server_for_each_active_goal(storage_, executor_, +visit, &q) != 0)
826 return false;
827 out = q.status;
828 return q.found;
829 }
830
831 // ── C trampolines ───────────────────────────────────────────────
832 //
833 // `ctx` is a pointer to this `ActionServer<A>` instance, so the
834 // trampoline reads the user's stored function pointer via the
835 // instance's own fields — no shared mutable statics.
836
837 static int32_t goal_trampoline(const uint8_t goal_id[16], const uint8_t* data, size_t len,
838 void* ctx) {
839 auto* self = static_cast<Server*>(ctx);
840 if (!self) return ::rclcpp_action::to_ffi(::rclcpp_action::GoalResponse::REJECT);
841 GoalType g;
842 if (GoalType::ffi_deserialize(data, len, &g) != 0) {
843 return ::rclcpp_action::to_ffi(::rclcpp_action::GoalResponse::REJECT);
844 }
845 if (self->user_goal_fn_ctx_ != nullptr) {
846 return ::rclcpp_action::to_ffi(
847 self->user_goal_fn_ctx_(goal_id, g, self->user_goal_ctx_));
848 }
849 if (self->user_goal_fn_ != nullptr) {
850 return ::rclcpp_action::to_ffi(self->user_goal_fn_(goal_id, g));
851 }
852 return ::rclcpp_action::to_ffi(::rclcpp_action::GoalResponse::REJECT);
853 }
854
855 static int32_t cancel_trampoline(const uint8_t goal_id[16], void* ctx) {
856 auto* self = static_cast<Server*>(ctx);
857 if (!self) return ::rclcpp_action::to_ffi(::rclcpp_action::CancelResponse::ACCEPT);
858 if (self->user_cancel_fn_ctx_ != nullptr) {
859 return ::rclcpp_action::to_ffi(
860 self->user_cancel_fn_ctx_(goal_id, self->user_cancel_ctx_));
861 }
862 if (self->user_cancel_fn_ != nullptr) {
863 return ::rclcpp_action::to_ffi(self->user_cancel_fn_(goal_id));
864 }
865 return ::rclcpp_action::to_ffi(::rclcpp_action::CancelResponse::ACCEPT);
866 }
867
868 static void accepted_trampoline(const uint8_t goal_id[16], void* ctx) {
869 auto* self = static_cast<Server*>(ctx);
870 if (!self) return;
871 if (self->user_accepted_fn_ctx_ != nullptr) {
872 self->user_accepted_fn_ctx_(goal_id, self->user_accepted_ctx_);
873 return;
874 }
875 if (self->user_accepted_fn_ != nullptr) {
876 self->user_accepted_fn_(goal_id);
877 }
878 }
879
880 ::rclcpp::Result install_callbacks() {
881 bool goal_set = (user_goal_fn_ != nullptr) || (user_goal_fn_ctx_ != nullptr);
882 bool cancel_set = (user_cancel_fn_ != nullptr) || (user_cancel_fn_ctx_ != nullptr);
883 bool accepted_set = (user_accepted_fn_ != nullptr) || (user_accepted_fn_ctx_ != nullptr);
884 nros_cpp_goal_callback_t gcb = goal_set ? &goal_trampoline : nullptr;
885 nros_cpp_cancel_callback_t ccb = cancel_set ? &cancel_trampoline : nullptr;
886 nros_cpp_ret_t ret = nros_cpp_action_server_set_callbacks(storage_, gcb, ccb, this);
887 if (ret != 0) return ::rclcpp::Result(ret);
888 // A SECOND install call rather than a fourth parameter on
889 // the first: `nros_cpp_action_server_set_callbacks` is declared in
890 // three places and called directly by C components, so growing it
891 // would have broken every one of them. Both calls pass the same
892 // `this`, which is the shared context slot the runtime keeps.
893 nros_cpp_accepted_callback_t acb = accepted_set ? &accepted_trampoline : nullptr;
894 return ::rclcpp::Result(nros_cpp_action_server_set_accepted_callback(storage_, acb, this));
895 }
896
897 alignas(8) uint8_t storage_[NROS_CPP_ACTION_SERVER_STORAGE_SIZE];
898 void* executor_; // Executor context needed for feedback/result operations
899 TypedGoalFn user_goal_fn_;
900 TypedGoalFnWithCtx user_goal_fn_ctx_;
901 void* user_goal_ctx_;
902 TypedCancelFn user_cancel_fn_;
903 TypedCancelFnWithCtx user_cancel_fn_ctx_;
904 void* user_cancel_ctx_;
905 TypedAcceptedFn user_accepted_fn_;
906 TypedAcceptedFnWithCtx user_accepted_fn_ctx_;
907 void* user_accepted_ctx_;
908 // A `TypedVisitorFn user_visitor_fn_` sat here, written at
909 // the head of `for_each_active_goal` and cleared at its tail. The visitor
910 // travels in the FFI's own `void* ctx` now; see that function.
911 // A `char action_name_[256]` was once here for a `get_action_name()`
912 // that was never written, and was deleted as
913 // dead state. A later change writes the accessor, so it is back and it
914 // is read — see `get_action_name()` for why the FFI cannot answer instead.
915 char action_name_[::rclcpp_action::ACTION_NAME_MAX];
916 bool initialized_;
920 detail::HostedBox upstream_;
921};
922
939template <typename A> class ServerGoalHandle {
940 public:
941 using Goal = typename A::Goal;
942 using Feedback = typename A::Feedback;
943 using Result = typename A::Result;
944#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
945 using SharedPtr = ::std::shared_ptr<ServerGoalHandle>;
946 using ConstSharedPtr = ::std::shared_ptr<const ServerGoalHandle>;
947#else
948 using SharedPtr = detail::Refused<ServerGoalHandle>;
949 using ConstSharedPtr = detail::Refused<ServerGoalHandle>;
950#endif
951
952 ServerGoalHandle(Server<A>* server, const GoalUUID& uuid)
953 : server_(server), uuid_(uuid), goal_() {}
954
956 const GoalUUID& get_goal_id() const { return uuid_; }
958 bool is_canceling() const { return status() == GoalStatus::Canceling; }
960 bool is_executing() const { return status() == GoalStatus::Executing; }
967 void execute() {
968 if (server_ != nullptr) (void)server_->execute(uuid_.data());
969 }
970
971 bool is_active() const {
972 const GoalStatus s = status();
973 return s == GoalStatus::Accepted || s == GoalStatus::Executing ||
975 }
976
977#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
979 ::std::shared_ptr<const Goal> get_goal() const {
980 auto* g = goal_.template as<::std::shared_ptr<const Goal>>();
981 return g != nullptr ? *g : ::std::shared_ptr<const Goal>();
982 }
984 void publish_feedback(::std::shared_ptr<Feedback> feedback) {
985 if (feedback && server_ != nullptr)
986 (void)server_->publish_feedback(uuid_.data(), *feedback);
987 }
989 void succeed(::std::shared_ptr<Result> result) { terminate(GoalStatus::Succeeded, result); }
991 void abort(::std::shared_ptr<Result> result) { terminate(GoalStatus::Aborted, result); }
993 void canceled(::std::shared_ptr<Result> result) { terminate(GoalStatus::Canceled, result); }
994#endif
995
996 private:
997 friend struct detail::ServerUpstream<A>;
998
999 GoalStatus status() const {
1000 int8_t s = 0;
1001 if (server_ == nullptr || !server_->goal_status(uuid_.data(), s))
1002 return GoalStatus::Unknown;
1003 return static_cast<GoalStatus>(s);
1004 }
1005
1006#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1007 void terminate(GoalStatus status, const ::std::shared_ptr<Result>& result) {
1008 if (server_ == nullptr) return;
1009 const Result empty{};
1010 (void)server_->complete_goal(uuid_.data(), status, result ? *result : empty);
1011 detail::ServerUpstream<A>::forget(*server_, uuid_);
1012 }
1013#endif
1014
1015 Server<A>* server_;
1016 GoalUUID uuid_;
1018 detail::HostedBox goal_;
1019};
1020
1021namespace detail {
1022
1028template <typename A> struct ServerUpstream {
1029 using Goal = typename A::Goal;
1031
1033 static void rehome(Server<A>& s) {
1034#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1035 if (State* st = s.upstream_.template as<State>()) st->server = &s;
1036#else
1037 (void)s;
1038#endif
1039 }
1040
1041#ifdef NROS_CPP_HAS_SHARED_PTR // hosted-family: action-interop
1042 using GoalFn = ::std::function<GoalResponse(const GoalUUID&, ::std::shared_ptr<const Goal>)>;
1043 using CancelFn = ::std::function<CancelResponse(::std::shared_ptr<Handle>)>;
1044 using AcceptedFn = ::std::function<void(::std::shared_ptr<Handle>)>;
1045
1046 struct Entry {
1047 GoalUUID id;
1048 ::std::shared_ptr<const Goal> goal;
1051 bool execute;
1052 };
1053 struct State {
1054 GoalFn goal;
1055 CancelFn cancel;
1056 AcceptedFn accepted;
1057 Server<A>* server;
1058 ::std::vector<Entry> goals;
1059 };
1060
1061 static void destroy(void* p) { delete static_cast<State*>(p); }
1062
1063 template <typename G, typename C, typename Acc>
1064 static ::rclcpp::Result bind(Server<A>& s, G&& goal, C&& cancel, Acc&& accepted) {
1065 State* st = new State{GoalFn(static_cast<G&&>(goal)),
1066 CancelFn(static_cast<C&&>(cancel)),
1067 AcceptedFn(static_cast<Acc&&>(accepted)),
1068 &s,
1069 {}};
1070 s.upstream_.reset(st, &destroy);
1071 ::rclcpp::Result r = s.set_goal_callback_with_ctx(&on_goal, st);
1072 if (r.ok()) r = s.set_cancel_callback_with_ctx(&on_cancel, st);
1073 if (r.ok()) r = s.set_accepted_callback_with_ctx(&on_accepted, st);
1074 return r;
1075 }
1076
1078 static void forget(Server<A>& s, const GoalUUID& id) {
1079 State* st = s.upstream_.template as<State>();
1080 if (st == nullptr) return;
1081 for (size_t i = 0; i < st->goals.size(); ++i) {
1082 if (st->goals[i].id == id) {
1083 st->goals.erase(st->goals.begin() + static_cast<long>(i));
1084 return;
1085 }
1086 }
1087 }
1088
1089 private:
1090 static ::std::shared_ptr<Handle> make_handle(State* st, const GoalUUID& id) {
1091 ::std::shared_ptr<Handle> h = ::std::make_shared<Handle>(st->server, id);
1092 for (const Entry& e : st->goals) {
1093 if (e.id == id) {
1094 h->goal_.reset(new ::std::shared_ptr<const Goal>(e.goal), [](void* p) {
1095 delete static_cast<::std::shared_ptr<const Goal>*>(p);
1096 });
1097 break;
1098 }
1099 }
1100 return h;
1101 }
1102
1103 static GoalResponse on_goal(const uint8_t uuid[16], const Goal& goal, void* ctx) {
1104 State* st = static_cast<State*>(ctx);
1105 if (!st->goal) return GoalResponse::REJECT;
1106 const GoalUUID id(uuid);
1107 ::std::shared_ptr<const Goal> msg = ::std::make_shared<const Goal>(goal);
1108 const GoalResponse r = st->goal(id, msg);
1109 if (r != GoalResponse::REJECT) {
1110 forget(*st->server, id);
1111 st->goals.push_back(Entry{id, msg, r == GoalResponse::ACCEPT_AND_EXECUTE});
1112 }
1113 return r;
1114 }
1115
1116 static CancelResponse on_cancel(const uint8_t uuid[16], void* ctx) {
1117 State* st = static_cast<State*>(ctx);
1118 if (!st->cancel) return CancelResponse::REJECT;
1119 return st->cancel(make_handle(st, GoalUUID(uuid)));
1120 }
1121
1122 static void on_accepted(const uint8_t uuid[16], void* ctx) {
1123 State* st = static_cast<State*>(ctx);
1124 const GoalUUID id(uuid);
1125 // upstream's server calls `execute()` itself for
1126 // ACCEPT_AND_EXECUTE, so the goal is EXECUTING (and the status array
1127 // says so) by the time `handle_accepted` sees it; ACCEPT_AND_DEFER
1128 // stays ACCEPTED until the handle's `execute()`.
1129 for (const Entry& e : st->goals) {
1130 if (e.id == id) {
1131 if (e.execute) (void)st->server->execute(uuid);
1132 break;
1133 }
1134 }
1135 if (st->accepted) st->accepted(make_handle(st, id));
1136 }
1137#endif
1138};
1139
1140} // namespace detail
1141
1142} // namespace rclcpp_action
1143
1144// Out-of-line definition of Node::create_action_server<A>().
1145#include "rclcpp/node.hpp"
1146
1147namespace rclcpp {} // namespace rclcpp
1148
1149namespace rclcpp {
1150template <typename A>
1152 const ::rclcpp::QoS& qos,
1153 const ::rclcpp::ActionServerOptions& options) {
1154 if (!initialized_) return Result(::rclcpp::ErrorCode::NotInitialized);
1156 nros_cpp_ret_t ret = nros_cpp_action_server_create(&handle_, action_name, A::TYPE_NAME,
1157 A::Goal::TYPE_HASH, ffi_qos, out.storage_);
1158 if (ret != 0) return Result(ret);
1159 // Register with executor — creates transport handles (3 queryables + 2 publishers).
1160 // Deferred from create to avoid FreeRTOS QEMU deadlocks.
1161 // names are passed at register-time (buffers live on the C++
1162 // `rclcpp_action::Server<A>` class, not in the runtime struct).
1163 // `sched_context` binds the action's (arena-registered)
1164 // goal-service handle to a scheduling context. UNSET ⇒ 0 (inherit, no-op).
1165 uint8_t sched = (options.sched_context == ::rclcpp::SCHED_CONTEXT_UNSET)
1166 ? 0u
1167 : static_cast<uint8_t>(options.sched_context);
1168 const nros_cpp_action_hashes_t hashes = ::rclcpp::detail::action_endpoint_hashes<A>();
1169 ret = nros_cpp_action_server_register(out.storage_, executor_handle_, action_name, A::TYPE_NAME,
1170 A::Goal::TYPE_HASH, &hashes, sched);
1171 if (ret == 0) {
1172 out.executor_ = executor_handle_;
1173 // Remember the name for `get_action_name()`. Truncating
1174 // copy, as `Subscription`'s and the two polling tiers' do: a name longer
1175 // than `rclcpp_action::ACTION_NAME_MAX` is not a reason to fail a create that the
1176 // runtime already accepted.
1177 ::rclcpp::detail::assign_entity_name(out.action_name_, action_name);
1178 out.initialized_ = true;
1179 }
1180 return Result(ret);
1181}
1182} // namespace rclcpp
1183
1184namespace rclcpp {} // namespace rclcpp
1185
1186#endif // NROS_RCLCPP_ACTION_SERVER_HPP
Result create_action_server(::rclcpp_action::Server< A > &out, const char *action_name, const ::rclcpp::QoS &qos=::rclcpp::QoS::services(), const ::rclcpp::ActionServerOptions &options={})
Definition server.hpp:1151
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:227
Definition server.hpp:939
detail::Refused< ServerGoalHandle > SharedPtr
Definition server.hpp:948
ServerGoalHandle(Server< A > *server, const GoalUUID &uuid)
Definition server.hpp:952
typename A::Feedback Feedback
Definition server.hpp:942
typename A::Goal Goal
Definition server.hpp:941
typename A::Result Result
Definition server.hpp:943
detail::Refused< ServerGoalHandle > ConstSharedPtr
Definition server.hpp:949
void execute()
Definition server.hpp:967
bool is_active() const
The goal has not reached a terminal state.
Definition server.hpp:971
bool is_canceling() const
The goal is CANCELING: a cancel request for it was accepted.
Definition server.hpp:958
bool is_executing() const
The goal is EXECUTING.
Definition server.hpp:960
const GoalUUID & get_goal_id() const
The goal's id.
Definition server.hpp:956
Definition server.hpp:350
void(*)(const uint8_t uuid[16], ::rclcpp_action::GoalStatus status) TypedVisitorFn
User-facing visitor signature for for_each_active_goal.
Definition server.hpp:380
~Server()
Definition server.hpp:723
::rclcpp::Owned< Server > SharedPtr
Definition server.hpp:360
::rclcpp::Result abort(const ::rclcpp_action::GoalUUID &goal_id, const ResultType &result)
abort taking a GoalUUID value.
Definition server.hpp:619
::rclcpp::Result complete_goal(const uint8_t goal_id[16], const ResultType &result)
Definition server.hpp:526
::rclcpp::Result abort(const uint8_t goal_id[16], const ResultType &result)
Definition server.hpp:597
::rclcpp_action::GoalResponse(*)(const uint8_t uuid[16], const GoalType &goal, void *ctx) TypedGoalFnWithCtx
User-facing typed goal callback signature with user context.
Definition server.hpp:368
::rclcpp::Result publish_feedback(const uint8_t goal_id[16], const FeedbackType &feedback)
Definition server.hpp:485
::rclcpp::Result complete_goal(const uint8_t goal_id[16], ::rclcpp_action::GoalStatus status, const ResultType &result)
Definition server.hpp:510
::rclcpp::Result set_goal_callback_with_ctx(TypedGoalFnWithCtx f, void *ctx)
Definition server.hpp:403
bool goal_exists(const ::rclcpp_action::GoalUUID &goal_id) const
goal_exists taking a GoalUUID value.
Definition server.hpp:702
::rclcpp::Result set_accepted_callback_with_ctx(TypedAcceptedFnWithCtx f, void *ctx)
Definition server.hpp:468
::rclcpp::Result set_cancel_callback(F f)
Definition server.hpp:414
typename A::Feedback FeedbackType
Definition server.hpp:354
::rclcpp::Result succeed(const ::rclcpp_action::GoalUUID &goal_id, const ResultType &result)
succeed taking a GoalUUID value.
Definition server.hpp:614
::rclcpp::Result complete_goal(const ::rclcpp_action::GoalUUID &goal_id, ::rclcpp_action::GoalStatus status, const ResultType &result)
complete_goal taking a GoalUUID value.
Definition server.hpp:537
::rclcpp::Result canceled(const uint8_t goal_id[16], const ResultType &result)
Definition server.hpp:609
const char * get_action_name() const
Definition server.hpp:686
Server(Server &&other)
Definition server.hpp:738
Server()
Definition server.hpp:794
void(*)(const uint8_t uuid[16], void *ctx) TypedAcceptedFnWithCtx
User-facing typed accepted-goal callback signature with user context.
Definition server.hpp:378
::rclcpp::Result canceled(const ::rclcpp_action::GoalUUID &goal_id, const ResultType &result)
canceled taking a GoalUUID value.
Definition server.hpp:624
Server & operator=(Server &&other)
Definition server.hpp:758
void(*)(const uint8_t uuid[16]) TypedAcceptedFn
User-facing typed accepted-goal callback signature.
Definition server.hpp:376
::rclcpp_action::CancelResponse(*)(const uint8_t uuid[16], void *ctx) TypedCancelFnWithCtx
User-facing typed cancel callback signature with user context.
Definition server.hpp:373
::rclcpp::Result set_goal_callback(F f)
Definition server.hpp:388
::rclcpp::Result execute(const ::rclcpp_action::GoalUUID &goal_id)
execute taking a GoalUUID value.
Definition server.hpp:569
::rclcpp::Result set_accepted_callback(F f)
Definition server.hpp:456
::rclcpp::Result publish_feedback(const ::rclcpp_action::GoalUUID &goal_id, const FeedbackType &feedback)
publish_feedback taking a GoalUUID value.
Definition server.hpp:531
::rclcpp_action::GoalResponse(*)(const uint8_t uuid[16], const GoalType &goal) TypedGoalFn
User-facing typed goal callback signature.
Definition server.hpp:365
bool goal_exists(const uint8_t goal_id[16]) const
Definition server.hpp:695
::rclcpp::Result set_cancel_callback_with_ctx(TypedCancelFnWithCtx f, void *ctx)
Definition server.hpp:428
typename A::Goal GoalType
Definition server.hpp:352
::rclcpp_action::CancelResponse(*)(const uint8_t uuid[16]) TypedCancelFn
User-facing typed cancel callback signature.
Definition server.hpp:371
bool is_valid() const
Check if the action server is initialized and valid.
Definition server.hpp:707
::rclcpp_action::ServerGoalHandle< A > GoalHandle
Upstream's rclcpp_action::ServerGoalHandle<A> for this action.
Definition server.hpp:362
::rclcpp::Result complete_goal(const ::rclcpp_action::GoalUUID &goal_id, const ResultType &result)
complete_goal taking a GoalUUID value.
Definition server.hpp:543
typename A::Result ResultType
Definition server.hpp:353
::rclcpp::Result for_each_active_goal(F f)
Definition server.hpp:649
::rclcpp::Result succeed(const uint8_t goal_id[16], const ResultType &result)
Definition server.hpp:589
::rclcpp::Result execute(const uint8_t goal_id[16])
Definition server.hpp:562
Inline storage-size macros for opaque entity buffers.
int nros_cpp_ret_t
Definition future.hpp:25
Definition rclcpp.hpp:437
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
Definition server.hpp:306
Definition node.hpp:203
constexpr int32_t to_ffi(GoalResponse r)
Definition server.hpp:236
CancelResponse
Definition server.hpp:226
@ ACCEPT
Definition server.hpp:228
GoalStatus
Definition server.hpp:263
@ Canceled
Definition server.hpp:269
@ Accepted
Definition server.hpp:265
@ Executing
Definition server.hpp:266
@ Succeeded
Definition server.hpp:268
@ Aborted
Definition server.hpp:270
@ Canceling
Definition server.hpp:267
@ Unknown
Definition server.hpp:264
static const size_t ACTION_NAME_MAX
Definition server.hpp:191
Client & operator=(Client &&other)
Definition client.hpp:1000
GoalResponse
Definition server.hpp:206
@ ACCEPT_AND_DEFER
Definition server.hpp:209
@ REJECT
Definition server.hpp:207
@ ACCEPT_AND_EXECUTE
Definition server.hpp:208
CancelReturnCode
Definition server.hpp:254
@ Ok
Definition server.hpp:255
@ Rejected
Definition server.hpp:256
@ GoalTerminated
Definition server.hpp:258
@ UnknownGoal
Definition server.hpp:257
Definition rclcpp.hpp:75
@ Error
Generic failure not covered by a more specific code.
Definition result.hpp:118
@ NotInitialized
Definition result.hpp:131
ResultOf< void > Result
Definition result.hpp:247
rclcpp::Node and global session helpers.
**auto handle
Definition client.hpp:127
typename A::Goal GoalType
Definition client.hpp:489
detail::Refused< Client > CancelResponse
Definition client.hpp:508
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.
int32_t(* nros_cpp_goal_callback_t)(const uint8_t goal_id[16], const uint8_t *data, size_t len, void *ctx)
Definition server.hpp:41
nros_cpp_ret_t nros_cpp_action_server_set_callbacks(void *handle, nros_cpp_goal_callback_t goal_cb, nros_cpp_cancel_callback_t cancel_cb, void *ctx)
int32_t(* nros_cpp_cancel_callback_t)(const uint8_t goal_id[16], void *ctx)
Definition server.hpp:43
nros_cpp_ret_t nros_cpp_action_server_set_accepted_callback(void *handle, nros_cpp_accepted_callback_t accepted_cb, void *ctx)
void(* nros_cpp_accepted_callback_t)(const uint8_t goal_id[16], void *ctx)
Definition server.hpp:48
Definition qos.hpp:84
static nros_cpp_action_hashes_t get()
Definition server.hpp:72
Definition server.hpp:67
void type
Definition server.hpp:68
Definition server.hpp:126
bool operator!=(const GoalUUID &other) const
Definition server.hpp:162
static const size_t SIZE
Byte count — unique_identifier_msgs/UUID is a fixed uint8[16].
Definition server.hpp:128
bool operator==(const GoalUUID &other) const
Definition server.hpp:155
const uint8_t * data() const
Definition server.hpp:145
bool is_zero() const
True when every byte is zero — mirrors nros_core::GoalId::is_zero().
Definition server.hpp:148
uint8_t uuid[16]
UUID bytes, in wire order.
Definition server.hpp:131
GoalUUID()
The zero ("null") goal id — mirrors nros_core::GoalId::zero().
Definition server.hpp:134
uint8_t * data()
Pointer to the 16 bytes — what the raw-array overloads and the FFI take.
Definition server.hpp:144
GoalUUID(const uint8_t *src)
Copy the 16 bytes at src, which must point to at least that many.
Definition server.hpp:137
bool operator<(const GoalUUID &other) const
Definition server.hpp:167
Definition server.hpp:1028
static void rehome(Server< A > &s)
Point the bound state at s after a move.
Definition server.hpp:1033
typename A::Goal Goal
Definition server.hpp:1029
ServerGoalHandle< A > Handle
Definition server.hpp:1030
**timer cancel()