query.hh
Go to the documentation of this file.
1 
37 #ifndef _LibMultiSense_details_query_hh
38 #define _LibMultiSense_details_query_hh
39 
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 response, 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
crl::multisense::details::impl::publish
void publish(const T &message)
Definition: query.hh:50
crl::multisense::Status_Ok
static CRL_CONSTEXPR Status Status_Ok
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:99
crl::multisense::details::wire::COMBINED_HEADER_LENGTH
static CRL_CONSTEXPR uint8_t COMBINED_HEADER_LENGTH
Definition: Protocol.hh:71
CRL_DEBUG
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:71
command
ROSLIB_DECL std::string command(const std::string &cmd)
crl::multisense::details::impl::m_messages
MessageMap m_messages
Definition: Legacy/include/MultiSense/details/channel.hh:510
crl::multisense::details::impl::m_watch
MessageWatch m_watch
Definition: Legacy/include/MultiSense/details/channel.hh:505
crl
Definition: Legacy/details/channel.cc:61
multisense::legacy::serialize
std::vector< uint8_t > serialize(const T &message, uint16_t sequence_id, size_t mtu)
Serialize a MultiSense Wire message for transmission. This adds the wire header to the message for tr...
Definition: message.hh:103
crl::multisense::details::utility::BufferStream::seek
void seek(std::size_t idx)
Definition: BufferStream.hh:93
crl::multisense::details::utility::BufferStreamWriter
Definition: BufferStream.hh:259
crl::multisense::details::wire::VersionType
uint16_t VersionType
Definition: Protocol.hh:137
MSG_ID
#define MSG_ID(x)
Definition: Protocol.hh:344
crl::multisense::details::impl::waitData
Status waitData(const T &command, U &data, const double &timeout=DEFAULT_ACK_TIMEOUT(), int32_t attempts=DEFAULT_ACK_ATTEMPTS)
Definition: query.hh:120
crl::multisense::details::MessageMap::extract
Status extract(T &msg)
Definition: Legacy/include/MultiSense/details/storage.hh:69
crl::multisense::details::impl::m_sensorMtu
int32_t m_sensorMtu
Definition: Legacy/include/MultiSense/details/channel.hh:415
crl::multisense::Status
int32_t Status
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:94
channel.hh
crl::multisense::Status_Exception
static CRL_CONSTEXPR Status Status_Exception
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:105
multisense
Definition: factory.cc:39
crl::multisense::details::ScopedWatch::wait
bool wait(Status &status, const double &timeout)
Definition: signal.hh:125
crl::multisense::Status_TimedOut
static CRL_CONSTEXPR Status Status_TimedOut
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:100
crl::multisense::details::wire::Header
Header
Definition: Protocol.hh:128
crl::multisense::details::ScopedWatch
Definition: signal.hh:113
crl::multisense::details::wire::IdType
uint16_t IdType
Definition: Protocol.hh:136
crl::multisense::details::impl::waitAck
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


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:09