-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from 107-systems/service-client
Implement a ROS2 style API Service Client.
- Loading branch information
Showing
16 changed files
with
227 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2020-2023 LXRobotics. | ||
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com> | ||
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors. | ||
*/ | ||
|
||
#ifndef INC_107_ARDUINO_CYPHAL_SERVICE_CLIENT_HPP | ||
#define INC_107_ARDUINO_CYPHAL_SERVICE_CLIENT_HPP | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <memory> | ||
|
||
#include "SubscriptionBase.h" | ||
|
||
#include "libcanard/canard.h" | ||
|
||
/************************************************************************************** | ||
* FORWARD DECLARATION | ||
**************************************************************************************/ | ||
|
||
class Node; | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace impl | ||
{ | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
template <typename T_REQ> | ||
class ServiceClientBase : public SubscriptionBase | ||
{ | ||
public: | ||
ServiceClientBase() : SubscriptionBase{CanardTransferKindResponse} { } | ||
virtual ~ServiceClientBase() { } | ||
virtual bool request(CanardNodeID const remote_node_id, T_REQ const & req) = 0; | ||
}; | ||
|
||
template<typename T_REQ, typename T_RSP, typename OnResponseCb> | ||
class ServiceClient : public ServiceClientBase<T_REQ> | ||
{ | ||
public: | ||
ServiceClient(Node & node_hdl, CanardPortID const port_id, CanardMicrosecond const tx_timeout_usec, OnResponseCb on_response_cb) | ||
: _node_hdl{node_hdl} | ||
, _port_id{port_id} | ||
, _tx_timeout_usec{tx_timeout_usec} | ||
, _on_response_cb{on_response_cb} | ||
, _transfer_id{0} | ||
{ } | ||
virtual ~ServiceClient(); | ||
|
||
|
||
virtual bool request(CanardNodeID const remote_node_id, T_REQ const & req) override; | ||
virtual bool onTransferReceived(CanardRxTransfer const & transfer) override; | ||
|
||
|
||
private: | ||
Node & _node_hdl; | ||
CanardPortID const _port_id; | ||
CanardMicrosecond const _tx_timeout_usec; | ||
OnResponseCb _on_response_cb; | ||
CanardTransferID _transfer_id; | ||
}; | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* impl */ | ||
|
||
/************************************************************************************** | ||
* TYPEDEF | ||
**************************************************************************************/ | ||
|
||
template <typename T_REQ> | ||
using ServiceClient = std::shared_ptr<impl::ServiceClientBase<T_REQ>>; | ||
|
||
/************************************************************************************** | ||
* TEMPLATE IMPLEMENTATION | ||
**************************************************************************************/ | ||
|
||
#include "ServiceClient.ipp" | ||
|
||
#endif /* INC_107_ARDUINO_CYPHAL_SERVICE_CLIENT_HPP */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2020-2023 LXRobotics. | ||
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com> | ||
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors. | ||
*/ | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace impl { | ||
|
||
/************************************************************************************** | ||
* CTOR/DTOR | ||
**************************************************************************************/ | ||
|
||
template<typename T_REQ, typename T_RSP, typename OnResponseCb> | ||
ServiceClient<T_REQ, T_RSP, OnResponseCb>::~ServiceClient() | ||
{ | ||
_node_hdl.unsubscribe(_port_id, SubscriptionBase::canard_transfer_kind()); | ||
} | ||
|
||
/************************************************************************************** | ||
* PUBLIC MEMBER FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
template<typename T_REQ, typename T_RSP, typename OnResponseCb> | ||
bool ServiceClient<T_REQ, T_RSP, OnResponseCb>::request(CanardNodeID const remote_node_id, T_REQ const & req) | ||
{ | ||
CanardTransferMetadata const transfer_metadata = | ||
{ | ||
.priority = CanardPriorityNominal, | ||
.transfer_kind = CanardTransferKindRequest, | ||
.port_id = _port_id, | ||
.remote_node_id = remote_node_id, | ||
.transfer_id = _transfer_id++, | ||
}; | ||
|
||
/* Serialize message into payload buffer. */ | ||
std::array<uint8_t, T_REQ::MAX_PAYLOAD_SIZE> payload_buf{}; | ||
size_t const payload_buf_size = req.serialize(payload_buf.data()); | ||
|
||
/* Serialize transfer into a series of CAN frames. */ | ||
return _node_hdl.enqueue_transfer(_tx_timeout_usec, | ||
&transfer_metadata, | ||
payload_buf_size, | ||
payload_buf.data()); | ||
} | ||
|
||
template<typename T_REQ, typename T_RSP, typename OnResponseCb> | ||
bool ServiceClient<T_REQ, T_RSP, OnResponseCb>::onTransferReceived(CanardRxTransfer const & transfer) | ||
{ | ||
/* Deserialize the response message. */ | ||
T_RSP const rsp = T_RSP::deserialize(transfer); | ||
|
||
/* Invoke the user registered callback. */ | ||
_on_response_cb(rsp); | ||
|
||
return true; | ||
} | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* impl */ |
Oops, something went wrong.