service_server.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_NODE_SERVICE_SERVER_HPP_INCLUDED
6 #define UAVCAN_NODE_SERVICE_SERVER_HPP_INCLUDED
7 
11 
12 #if !defined(UAVCAN_CPP_VERSION) || !defined(UAVCAN_CPP11)
13 # error UAVCAN_CPP_VERSION
14 #endif
15 
16 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
17 # include <functional>
18 #endif
19 
20 namespace uavcan
21 {
39 template <typename ResponseDataType_>
40 class ServiceResponseDataStructure : public ResponseDataType_
41 {
42  // Fields are weirdly named to avoid name clashing with the inherited data type
43  bool _enabled_;
44 
45 public:
46  typedef ResponseDataType_ ResponseDataType;
47 
49  : _enabled_(true)
50  { }
51 
56  void setResponseEnabled(bool x) { _enabled_ = x; }
57 
61  bool isResponseEnabled() const { return _enabled_; }
62 };
63 
81 template <typename DataType_,
82 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
83  typename Callback_ = std::function<void (const ReceivedDataStructure<typename DataType_::Request>&,
84  ServiceResponseDataStructure<typename DataType_::Response>&)>
85 #else
86  typename Callback_ = void (*)(const ReceivedDataStructure<typename DataType_::Request>&,
87  ServiceResponseDataStructure<typename DataType_::Response>&)
88 #endif
89  >
92 {
93 public:
94  typedef DataType_ DataType;
95  typedef typename DataType::Request RequestType;
96  typedef typename DataType::Response ResponseType;
97  typedef Callback_ Callback;
98 
99 private:
102 
106 
108  {
110 
112 
113  if (coerceOrFallback<bool>(callback_, true))
114  {
115  UAVCAN_ASSERT(response.isResponseEnabled()); // Enabled by default
116  callback_(request, response);
117  }
118  else
119  {
120  handleFatalError("Srv serv clbk");
121  }
122 
123  if (response.isResponseEnabled())
124  {
125  publisher_.setPriority(request.getPriority()); // Responding at the same priority.
126 
127  const int res = publisher_.publish(response, TransferTypeServiceResponse, request.getSrcNodeID(),
128  request.getTransferID());
129  if (res < 0)
130  {
131  UAVCAN_TRACE("ServiceServer", "Response publication failure: %i", res);
133  response_failure_count_++;
134  }
135  }
136  else
137  {
138  UAVCAN_TRACE("ServiceServer", "Response was suppressed by the application");
139  }
140  }
141 
142 public:
145  , publisher_(node, getDefaultTxTimeout())
146  , callback_()
147  , response_failure_count_(0)
148  {
149  UAVCAN_ASSERT(getTxTimeout() == getDefaultTxTimeout()); // Making sure it is valid
150 
152  }
153 
158  int start(const Callback& callback)
159  {
160  stop();
161 
162  if (!coerceOrFallback<bool>(callback, true))
163  {
164  UAVCAN_TRACE("ServiceServer", "Invalid callback");
165  return -ErrInvalidParam;
166  }
167  callback_ = callback;
168 
169  const int publisher_res = publisher_.init();
170  if (publisher_res < 0)
171  {
172  UAVCAN_TRACE("ServiceServer", "Publisher initialization failure: %i", publisher_res);
173  return publisher_res;
174  }
175  return SubscriberType::startAsServiceRequestListener();
176  }
177 
181  using SubscriberType::stop;
182 
184  static MonotonicDuration getMinTxTimeout() { return PublisherType::getMinTxTimeout(); }
185  static MonotonicDuration getMaxTxTimeout() { return PublisherType::getMaxTxTimeout(); }
186 
187  MonotonicDuration getTxTimeout() const { return publisher_.getTxTimeout(); }
188  void setTxTimeout(MonotonicDuration tx_timeout) { publisher_.setTxTimeout(tx_timeout); }
189 
195  uint32_t getRequestFailureCount() const { return SubscriberType::getFailureCount(); }
196  uint32_t getResponseFailureCount() const { return response_failure_count_; }
197 };
198 
199 }
200 
201 #endif // UAVCAN_NODE_SERVICE_SERVER_HPP_INCLUDED
response
const std::string response
generic_publisher.hpp
uavcan::ServiceResponseDataStructure::ServiceResponseDataStructure
ServiceResponseDataStructure()
Definition: service_server.hpp:48
uavcan::ServiceServer::getDefaultTxTimeout
static MonotonicDuration getDefaultTxTimeout()
Definition: service_server.hpp:183
uavcan::ServiceServer::response_failure_count_
uint32_t response_failure_count_
Definition: service_server.hpp:105
uavcan::DataTypeKindService
@ DataTypeKindService
Definition: data_type.hpp:21
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
uavcan::GenericPublisherBase::setTxTimeout
void setTxTimeout(MonotonicDuration tx_timeout)
Definition: uc_generic_publisher.cpp:56
uavcan::GenericPublisherBase::getNode
INode & getNode() const
Definition: generic_publisher.hpp:76
uavcan::ServiceServer::DataType
DataType_ DataType
Definition: service_server.hpp:94
uavcan::ServiceResponseDataStructure::isResponseEnabled
bool isResponseEnabled() const
Definition: service_server.hpp:61
uavcan::TransferTypeServiceResponse
@ TransferTypeServiceResponse
Definition: transfer.hpp:20
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
uavcan::INode::getDispatcher
Dispatcher & getDispatcher()
Definition: abstract_node.hpp:28
generic_subscriber.hpp
uavcan::ReceivedDataStructure
Definition: generic_subscriber.hpp:39
UAVCAN_TRACE
#define UAVCAN_TRACE(...)
Definition: libuavcan/libuavcan/include/uavcan/debug.hpp:31
uavcan::StaticAssert
struct UAVCAN_EXPORT StaticAssert
Definition: templates.hpp:29
uavcan::ServiceServer::getMinTxTimeout
static MonotonicDuration getMinTxTimeout()
Definition: service_server.hpp:184
uavcan::ServiceServer::ResponseType
DataType::Response ResponseType
Definition: service_server.hpp:96
uavcan::MonotonicDuration
Definition: time.hpp:182
uavcan::TransferTypeServiceRequest
@ TransferTypeServiceRequest
Definition: transfer.hpp:21
uavcan::GenericPublisher::publish
int publish(const DataStruct &message, TransferType transfer_type, NodeID dst_node_id, MonotonicTime blocking_deadline=MonotonicTime())
Definition: generic_publisher.hpp:132
uavcan::ServiceResponseDataStructure::_enabled_
bool _enabled_
Definition: service_server.hpp:43
uavcan::ServiceServer::Callback
Callback_ Callback
Definition: service_server.hpp:97
uavcan::ServiceServer::handleReceivedDataStruct
virtual void handleReceivedDataStruct(ReceivedDataStructure< RequestType > &request)
Definition: service_server.hpp:107
uavcan::ServiceServer
Definition: service_server.hpp:90
uavcan::ServiceServer::PublisherType
GenericPublisher< DataType, ResponseType > PublisherType
Definition: service_server.hpp:101
uavcan::ServiceServer::publisher_
PublisherType publisher_
Definition: service_server.hpp:103
uavcan::ServiceServer::getRequestFailureCount
uint32_t getRequestFailureCount() const
Definition: service_server.hpp:195
uavcan::ReceivedDataStructure::getPriority
TransferPriority getPriority() const
Definition: generic_subscriber.hpp:72
uavcan::ReceivedDataStructure::getTransferID
TransferID getTransferID() const
Definition: generic_subscriber.hpp:74
uavcan::ServiceServer::getMaxTxTimeout
static MonotonicDuration getMaxTxTimeout()
Definition: service_server.hpp:185
uavcan::ServiceServer::getResponseFailureCount
uint32_t getResponseFailureCount() const
Definition: service_server.hpp:196
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::DataTypeKind
DataTypeKind
Definition: data_type.hpp:19
uavcan::INode
Definition: abstract_node.hpp:19
uavcan::ServiceServer::start
int start(const Callback &callback)
Definition: service_server.hpp:158
build_config.hpp
uavcan::ServiceServer::RequestType
DataType::Request RequestType
Definition: service_server.hpp:95
uavcan::GenericPublisher::init
int init()
Definition: generic_publisher.hpp:118
uavcan::ReceivedDataStructure::getTransferType
TransferType getTransferType() const
Definition: generic_subscriber.hpp:73
uavcan::TransferPerfCounter::addError
void addError()
Definition: perf_counter.hpp:52
uavcan::ServiceServer::callback_
Callback callback_
Definition: service_server.hpp:104
uavcan::MethodBinder
Definition: method_binder.hpp:20
uavcan::ServiceServer::ServiceServer
ServiceServer(INode &node)
Definition: service_server.hpp:143
uavcan::Dispatcher::getTransferPerfCounter
const TransferPerfCounter & getTransferPerfCounter() const
Definition: dispatcher.hpp:236
uavcan::handleFatalError
UAVCAN_EXPORT void handleFatalError(const char *msg)
Definition: uc_error.cpp:20
uavcan::ServiceResponseDataStructure
Definition: service_server.hpp:40
uavcan::GenericPublisherBase::getTxTimeout
MonotonicDuration getTxTimeout() const
Definition: generic_publisher.hpp:58
uavcan::GenericPublisher< DataType, ResponseType >
uavcan::ServiceServer::SubscriberType
GenericSubscriber< DataType, RequestType, TransferListener > SubscriberType
Definition: service_server.hpp:100
uavcan::GenericSubscriber
Definition: generic_subscriber.hpp:128
uavcan::ServiceResponseDataStructure::setResponseEnabled
void setResponseEnabled(bool x)
Definition: service_server.hpp:56
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::ServiceServer::getTxTimeout
MonotonicDuration getTxTimeout() const
Definition: service_server.hpp:187
uavcan::GenericPublisherBase::setPriority
void setPriority(const TransferPriority prio)
Definition: generic_publisher.hpp:74
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::ReceivedDataStructure::getSrcNodeID
NodeID getSrcNodeID() const
Definition: generic_subscriber.hpp:75
uavcan::ServiceServer::setTxTimeout
void setTxTimeout(MonotonicDuration tx_timeout)
Definition: service_server.hpp:188
pyuavcan_v0.driver.timestamp_estimator.x
x
Definition: timestamp_estimator.py:221
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan::ServiceResponseDataStructure::ResponseDataType
ResponseDataType_ ResponseDataType
Definition: service_server.hpp:46


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03