query.hh
Go to the documentation of this file.
1 
37 #ifndef _LibMultiSense_details_query_hh
38 #define _LibMultiSense_details_query_hh
39 
40 #include "details/channel.hh"
41 
42 namespace crl {
43 namespace multisense {
44 namespace details {
45 
46 //
47 // Publishes the given message to the sensor (message must
48 // serialize into a single MTU)
49 
50 template<class T> void impl::publish(const T& message)
51 {
52  const wire::IdType id = T::ID;
53  const wire::VersionType version = T::VERSION;
54 
55  //
56  // An output stream to serialize the data
57 
60 
61  //
62  // Hide the header area
63 
64  stream.seek(sizeof(wire::Header));
65 
66  //
67  // Set the ID and version
68 
69  stream & id;
70  stream & version;
71 
72  //
73  // Add the message payload. We cast away const here because
74  // we have a single serialize() for both directions, and cannot
75  // mark it const.
76 
77  const_cast<T*>(&message)->serialize(stream, version);
78 
79  //
80  // Publish the stream
81 
82  publish(stream);
83 }
84 
85 //
86 // Send a message, wait for a particular repsonse, re-trying if
87 // necessary
88 
89 template <class T> Status impl::waitAck(const T& msg,
90  wire::IdType ackId,
91  const double& timeout,
92  int32_t attempts)
93 {
94  try {
95  ScopedWatch ack(ackId, m_watch);
96 
97  while(attempts-- > 0) {
98 
99  publish(msg);
100 
101  Status status;
102  if (false == ack.wait(status, timeout))
103  continue;
104  else
105  return status;
106  }
107 
108  return Status_TimedOut;
109 
110  } catch (const std::exception& e) {
111  CRL_DEBUG("exception: %s\n", e.what());
112  return Status_Exception;
113  }
114 }
115 
116 //
117 // Send a message (with retry), expecting data message,
118 // extract data payload for user
119 
120 template <class T, class U> Status impl::waitData(const T& command,
121  U& data,
122  const double& timeout,
123  int32_t attempts)
124 {
125  try {
126 
127  //
128  // Set up a watch on the command ID in case it is rejected or
129  // unsupported.
130 
131  ScopedWatch commandAck(T::ID, m_watch);
132 
133  //
134  // Send the command with retry, expecting the data message as a response.
135 
136  Status dataStatus = waitAck(command, MSG_ID(U::ID), timeout, attempts);
137 
138  //
139  // Also store the response from the command. Do not block, as any
140  // response would be registered by this time.
141 
142  Status commandStatus;
143  if (false == commandAck.wait(commandStatus, 0.0))
144  commandStatus = Status_TimedOut;
145 
146  //
147  // If we did not receive the data message, return the response from
148  // the command code instead, unless there was an exception or the
149  // command ack'd OK.
150 
151  if (Status_Ok != dataStatus) {
152  if (Status_Exception == dataStatus || // exception
153  Status_Ok == commandStatus) // data payload timeout or MTU error
154  return dataStatus;
155  else
156  return commandStatus; // command error
157  }
158 
159  //
160  // We have received the data message, extract it for the user.
161 
162  return m_messages.extract(data);
163 
164  } catch (const std::exception& e) {
165  CRL_DEBUG("exception: %s\n", e.what());
166  return Status_Exception;
167  }
168 }
169 
170 }}}; // namespaces
171 
172 #endif // _LibMultiSense_details_publish_hh
static CRL_CONSTEXPR uint8_t COMBINED_HEADER_LENGTH
Definition: Protocol.h:61
#define MSG_ID(x)
Definition: Protocol.h:248
void serialize(Stream &stream, const T &t)
Definition: channel.cc:56
static CRL_CONSTEXPR Status Status_TimedOut
static CRL_CONSTEXPR Status Status_Ok
Status waitAck(const T &msg, wire::IdType id=MSG_ID(T::ID), const double &timeout=DEFAULT_ACK_TIMEOUT(), int32_t attempts=DEFAULT_ACK_ATTEMPTS)
Definition: query.hh:89
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:77
bool wait(Status &status, const double &timeout)
Definition: signal.hh:125
void publish(const T &message)
Definition: query.hh:50
static CRL_CONSTEXPR Status Status_Exception
Status waitData(const T &command, U &data, const double &timeout=DEFAULT_ACK_TIMEOUT(), int32_t attempts=DEFAULT_ACK_ATTEMPTS)
Definition: query.hh:120


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46