650 void (*
feedback)(
const uint8_t goal_id[16],
const uint8_t* data,
size_t len,
void* ctx);
652 void (*
result)(
const uint8_t goal_id[16], int32_t status,
const uint8_t* data,
size_t len,
681 uint8_t buf[::rclcpp::detail::buffer_bounds<GoalType>::tx];
683 if (GoalType::ffi_serialize(&goal, buf,
sizeof(buf), &len) != 0) {
686 return ::rclcpp::Result(nros_cpp_action_client_send_goal_async(
687 storage_, buf, len,
reinterpret_cast<uint8_t(*)[16]
>(goal_id)));
695#ifdef NROS_CPP_HAS_SHARED_PTR
713 ::rclcpp::Future<GoalAccept>
714 async_send_goal(
const GoalType& goal,
const SendGoalOptions& options = SendGoalOptions()) {
716 if (!send_goal_async(goal, goal_id).ok()) return ::rclcpp::Future<GoalAccept>();
717 UpstreamState* st = upstream_state();
720 g.id = GoalUUID(goal_id);
722 st->goals.push_back(g);
723 return ::rclcpp::Future<GoalAccept>(storage_, &nros_cpp_action_client_poll, goal_id,
724 &accepts_, executor_);
728 template <
typename T =
void>
731 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
732 return ::rclcpp::Future<GoalAccept>();
736#ifdef NROS_CPP_HAS_STD_PROMISE
758 detail::DrivenSharedFuture<WrappedResult>
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();
767 bool created =
false;
768 if (upstream_goal(goal_handle->get_goal_id().data(), at) ==
nullptr) {
770 g.id = goal_handle->get_goal_id();
772 st->goals.push_back(g);
773 at = st->goals.size() - 1;
776 if (!goal_handle->result_aware_) {
778 if (created) st->goals.erase(st->goals.begin() +
static_cast<long>(at));
779 return detail::DrivenSharedFuture<WrappedResult>();
781 goal_handle->result_aware_ =
true;
783 UpstreamGoal& g = st->goals[at];
785 g.handle = goal_handle;
787 if (result_callback) g.options.result_callback = result_callback;
807 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
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);
817 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
819 return send_cancel(GoalUUID(), 0, 0, cancel_callback);
826 detail::DrivenSharedFuture<typename CancelResponse::SharedPtr>
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),
836#ifndef NROS_CPP_HAS_STD_PROMISE
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);
846 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
850 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
854 static_assert(::rclcpp::detail::refuse<T>::value, NROS_RCLCPP_REFUSE_ACTION_GOAL_HANDLE);
874 return ::rclcpp::Result(nros_cpp_action_client_cancel_goal(
875 storage_,
reinterpret_cast<const uint8_t(*)[16]
>(goal_id)));
899 if (take_stray_cancel(code)) {
901 return ::rclcpp::Result::success();
903 nros_cpp_ret_t ret = nros_cpp_action_client_try_recv_cancel_response(storage_, &code);
904 if (ret != 0) return ::rclcpp::Result(ret);
906 return ::rclcpp::Result::success();
918 return ::rclcpp::Result(nros_cpp_action_client_get_result_async(
919 storage_,
reinterpret_cast<const uint8_t(*)[16]
>(goal_id)));
944 return install_dispatch();
953 if (!initialized_)
return;
955 nros_cpp_action_client_poll(storage_);
976 accepts_.orphan_all();
977 results_.orphan_all();
979 nros_cpp_action_client_destroy(storage_);
980 initialized_ =
false;
995 : executor_(other.executor_), initialized_(other.initialized_), action_name_{} {
996 ::memcpy(action_name_, other.action_name_,
sizeof(action_name_));
1001 if (
this != &other) {
1003 accepts_.orphan_all();
1004 results_.orphan_all();
1006 nros_cpp_action_client_destroy(storage_);
1009 executor_ = other.executor_;
1010 initialized_ = other.initialized_;
1011 ::memcpy(action_name_, other.action_name_,
sizeof(action_name_));
1019 Client() : executor_(nullptr), initialized_(false), action_name_{} {}
1025 friend class ::rclcpp::Node;
1031 accepts_.
forget = &Client::forget_accept;
1033 results_.
forget = &Client::forget_result;
1036 storage_, &Client::on_goal_response, &Client::on_feedback, &Client::on_result,
this);
1037 if (ret != 0) return ::rclcpp::Result(ret);
1040 return ::rclcpp::Result(
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);
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);
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;
1063 if (upstream_wants(accept, goal))
return;
1064 (void)nros_cpp_action_client_forget(storage_, channel,
1065 reinterpret_cast<const uint8_t(*)[16]
>(goal));
1069 void adopt(
Client& other) {
1070 user_ = other.user_;
1071 upstream_ =
static_cast<detail::HostedBox&&
>(other.upstream_);
1073 feedback_len_ = other.feedback_len_;
1074 feedback_ready_ = other.feedback_ready_;
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();
1100 static void on_goal_response(
bool accepted,
const uint8_t goal_id[16],
void* ctx) {
1103 if (owner !=
nullptr) {
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));
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);
1117 static void on_feedback(
const uint8_t goal_id[16],
const uint8_t* data,
size_t len,
void* 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);
1130 static void on_result(
const uint8_t goal_id[16], int32_t status,
const uint8_t* data,
1131 size_t len,
void* ctx) {
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);
1142#ifdef NROS_CPP_HAS_SHARED_PTR
1148 struct UpstreamGoal {
1150 SendGoalOptions options;
1152 ::std::weak_ptr<GoalHandle> weak;
1153 bool accepted =
false;
1155#ifdef NROS_CPP_HAS_STD_PROMISE
1158 struct CancelWaiter {
1163 ::std::promise<typename CancelResponse::SharedPtr> promise;
1164 CancelCallback callback;
1167 struct UpstreamState {
1168 ::std::vector<UpstreamGoal> goals;
1169#ifdef NROS_CPP_HAS_STD_PROMISE
1170 ::std::vector<CancelWaiter> cancels;
1172 ::std::shared_ptr<detail::PumpCell> pump;
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); });
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;
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));
1205 ::std::shared_ptr<detail::PumpCell> pump_cell() {
1206 UpstreamState* st = upstream_state();
1208 st->pump = ::std::make_shared<detail::PumpCell>();
1209 st->pump->fn = &Client::pump_entry;
1211 st->pump->ctx =
this;
1212 st->pump->executor = executor_;
1216 static void pump_entry(
void* ctx) {
1218 if (!self->initialized_)
return;
1219 (void)nros_cpp_action_client_poll(self->storage_);
1228 bool upstream_wants(
bool accept,
const uint8_t goal_id[16]) {
1229#ifdef NROS_CPP_HAS_SHARED_PTR
1231 UpstreamState* st = upstream_goal(goal_id, at);
1232 if (st ==
nullptr)
return false;
1233 UpstreamGoal& g = st->goals[at];
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));
1242 return g.handle && g.handle->result_aware_;
1251 void repoint_pump() {
1252#ifdef NROS_CPP_HAS_SHARED_PTR
1253 UpstreamState* st = upstream_.template as<UpstreamState>();
1254 if (st !=
nullptr && st->pump) {
1255 st->pump->ctx =
this;
1256 st->pump->executor = executor_;
1265 void abandon_upstream() {
1266#ifdef NROS_CPP_HAS_SHARED_PTR
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
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_) {
1277 wrapped.goal_id = g.id;
1278 wrapped.code = ResultCode::UNKNOWN;
1279 g.handle->set_result(wrapped);
1287#ifdef NROS_CPP_HAS_STD_PROMISE
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)
1297 UpstreamState* st = upstream_state();
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());
1317 static void on_cancel_reply(
const uint8_t goal[16], int64_t seq,
const uint8_t* reply,
1318 size_t len,
void* ctx) {
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);
1331 void stash_cancel(int8_t code) {
1332 if (stash_len_ == CANCEL_STASH_CAP) {
1336 stash_codes_[(stash_head_ + stash_len_) % CANCEL_STASH_CAP] = code;
1340 bool take_stray_cancel(int8_t& code) {
1341 if (stash_len_ == 0)
return false;
1342 code = stash_codes_[stash_head_];
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
1353 UpstreamState* st = upstream_.template as<UpstreamState>();
1354 if (st ==
nullptr)
return false;
1355 const GoalUUID key(goal);
1357 while (at < st->cancels.size() &&
1359 : st->cancels[at].key == key))
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));
1365 w.promise.set_value(
typename CancelResponse::SharedPtr());
1366 if (w.callback) w.callback(
typename CancelResponse::SharedPtr());
1369 typename CancelResponse::SharedPtr resp = ::std::make_shared<CancelResponse>();
1370 resp->return_code = code;
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);
1381 w.promise.set_value(resp);
1382 if (w.callback) w.callback(resp);
1395 void upstream_goal_response(
bool accepted,
const uint8_t goal_id[16]) {
1396#ifdef NROS_CPP_HAS_SHARED_PTR
1398 UpstreamState* st = upstream_goal(goal_id, at);
1399 if (st ==
nullptr)
return;
1400 UpstreamGoal& g = st->goals[at];
1401 if (g.accepted)
return;
1402 auto cb = g.options.goal_response_callback;
1404 st->goals.erase(st->goals.begin() +
static_cast<long>(at));
1405 if (cb) cb(
nullptr);
1409 handle->feedback_aware_ =
static_cast<bool>(g.options.feedback_callback);
1410 handle->result_aware_ =
static_cast<bool>(g.options.result_callback);
1412 if (
handle->result_aware_)
1416 if (!
handle->feedback_aware_ && !
handle->result_aware_)
1417 st->goals.erase(st->goals.begin() +
static_cast<long>(at));
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;
1434 void upstream_feedback(
const uint8_t goal_id[16],
const uint8_t* data,
size_t len) {
1435#ifdef NROS_CPP_HAS_SHARED_PTR
1437 UpstreamState* st = upstream_goal(goal_id, at);
1438 if (st ==
nullptr)
return;
1439 UpstreamGoal& g = st->goals[at];
1443 if (g.accepted) st->goals.erase(st->goals.begin() +
static_cast<long>(at));
1446 auto cb = g.options.feedback_callback;
1448 ::std::shared_ptr<FeedbackType> fb = ::std::make_shared<FeedbackType>();
1449 if (FeedbackType::ffi_deserialize(data, len, fb.get()) != 0)
return;
1458 void upstream_result(
const uint8_t goal_id[16], int32_t status,
const uint8_t* data,
1460#ifdef NROS_CPP_HAS_SHARED_PTR
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));
1467 if (
handle)
handle->status_ =
static_cast<int8_t
>(status);
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();
1476#ifdef NROS_CPP_HAS_STD_PROMISE
1479 if (g.options.result_callback) g.options.result_callback(wrapped);
1491 static nros_cpp_ret_t take_feedback(
void* ctx, uint8_t* buf,
size_t cap,
size_t* out_len) {
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_;
1503 for (
size_t i = 0; i < self->feedback_len_; ++i)
1504 buf[i] = self->feedback_buf_[i];
1505 *out_len = self->feedback_len_;
1509 alignas(8) uint8_t storage_[NROS_CPP_ACTION_CLIENT_STORAGE_SIZE];
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;
1535 uint8_t stash_head_ = 0;
1536 uint8_t stash_len_ = 0;
1540 detail::HostedBox upstream_;
1551template <
typename A>
1553 const ::rclcpp::QoS& qos) {
1558 &handle_, action_name, A::TYPE_NAME, A::Goal::TYPE_HASH, &hashes, ffi_qos, out.storage_);
1560 out.executor_ = executor_handle_;
1563 out.initialized_ =
true;
1564 (void)out.install_dispatch();
1567 ::rclcpp::detail::assign_entity_name(out.action_name_, action_name);
1568 out.initialized_ =
true;