channel.hh
Go to the documentation of this file.
1 
36 #ifndef _LibMultiSense_details_channel_hh
37 #define _LibMultiSense_details_channel_hh
38 
39 #include "MultiSenseChannel.hh"
40 
44 #include "details/utility/Units.hh"
45 #include "details/listeners.hh"
46 #include "details/signal.hh"
47 #include "details/storage.hh"
48 #include "details/wire/Protocol.h"
52 
53 #ifndef WIN32
54 #include <netinet/ip.h>
55 #include <unistd.h>
56 #endif
57 
58 #include <vector>
59 #include <list>
60 #include <set>
61 #include <map>
62 #include <iostream>
63 #include <fstream>
64 
65 namespace crl {
66 namespace multisense {
67 namespace details {
68 
69 //
70 // The implementation details
71 
72 class impl : public Channel {
73 public:
74 
75  //
76  // Construction
77 
78  impl(const std::string& address);
79  ~impl();
80 
81  //
82  // Public API
83 
84  virtual Status addIsolatedCallback (image::Callback callback,
85  DataSource imageSourceMask,
86  void *userDataP);
87  virtual Status addIsolatedCallback (lidar::Callback callback,
88  void *userDataP);
89  virtual Status addIsolatedCallback (pps::Callback callback,
90  void *userDataP);
91  virtual Status addIsolatedCallback (imu::Callback callback,
92  void *userDataP);
93 
98 
99  virtual void* reserveCallbackBuffer ();
100  virtual Status releaseCallbackBuffer (void *referenceP);
101 
102  virtual Status networkTimeSynchronization(bool enabled);
103 
104  virtual Status startStreams (DataSource mask);
105  virtual Status stopStreams (DataSource mask);
106  virtual Status getEnabledStreams (DataSource& mask);
107 
108  virtual Status startDirectedStream (const DirectedStream& stream);
109  virtual Status startDirectedStreams (const std::vector<DirectedStream>& streams);
110  virtual Status stopDirectedStream (const DirectedStream& stream);
111  virtual Status getDirectedStreams (std::vector<DirectedStream>& streams);
112  virtual Status maxDirectedStreams (uint32_t& maximum);
113 
115 
116  virtual Status setMotorSpeed (float rpm);
117 
119  virtual Status setLightingConfig (const lighting::Config& c);
120 
122 
123  virtual Status getSensorVersion (VersionType& version);
124  virtual Status getApiVersion (VersionType& version);
126 
127  virtual Status getImageConfig (image::Config& c);
128  virtual Status setImageConfig (const image::Config& c);
129 
131  virtual Status setImageCalibration (const image::Calibration& c);
132 
135 
137  virtual Status setTransmitDelay (const image::TransmitDelay& c);
138 
140  virtual Status setLidarCalibration (const lidar::Calibration& c);
141 
142  virtual Status getImageHistogram (int64_t frameId, image::Histogram& histogram);
143 
144  virtual Status getDeviceModes (std::vector<system::DeviceMode>& modes);
145 
146  virtual Status getMtu (int32_t& mtu);
147  virtual Status setMtu (int32_t mtu);
148 
150  virtual Status setNetworkConfig (const system::NetworkConfig& c);
151 
152  virtual Status getDeviceInfo (system::DeviceInfo& info);
153  virtual Status setDeviceInfo (const std::string& key,
154  const system::DeviceInfo& i);
155 
156  virtual Status getDeviceStatus (system::StatusMessage& status);
157 
159  virtual Status setExternalCalibration (const system::ExternalCalibration& calibration);
160 
161  virtual Status flashBitstream (const std::string& file);
162  virtual Status flashFirmware (const std::string& file);
163 
164  virtual Status verifyBitstream (const std::string& file);
165  virtual Status verifyFirmware (const std::string& file);
166 
167  virtual Status getImuInfo (uint32_t& maxSamplesPerMessage,
168  std::vector<imu::Info>& info);
169  virtual Status getImuConfig (uint32_t& samplesPerMessage,
170  std::vector<imu::Config>& c);
171  virtual Status setImuConfig (bool storeSettingsInFlash,
172  uint32_t samplesPerMessage,
173  const std::vector<imu::Config>& c);
174 
175  virtual Status getLargeBufferDetails (uint32_t& bufferCount,
176  uint32_t& bufferSize);
177  virtual Status setLargeBuffers (const std::vector<uint8_t*>& buffers,
178  uint32_t bufferSize);
179  virtual Status getLocalUdpPort (uint16_t& port);
180 
181 private:
182 
183  //
184  // A handler prototype for custom UDP datagram reassembly
185 
186  typedef void (*UdpAssembler)(utility::BufferStreamWriter& stream,
187  const uint8_t *dataP,
188  uint32_t offset,
189  uint32_t length);
190 
191  //
192  // The version of this API
193 
194  static CRL_CONSTEXPR VersionType API_VERSION = 0x0308; // 3.8
195 
196  //
197  // Misc. internal constants
198 
199  static CRL_CONSTEXPR uint32_t MAX_MTU_SIZE = 9000;
200  static CRL_CONSTEXPR uint16_t DEFAULT_SENSOR_TX_PORT = 9001;
201  static CRL_CONSTEXPR uint32_t RX_POOL_LARGE_BUFFER_SIZE = (10 * (1024 * 1024));
203  static CRL_CONSTEXPR uint32_t RX_POOL_SMALL_BUFFER_SIZE = (10 * (1024));
205 
206  static CRL_CONSTEXPR double DEFAULT_ACK_TIMEOUT () { return 0.2; }
207  static CRL_CONSTEXPR uint32_t DEFAULT_ACK_ATTEMPTS = 5;
208  static CRL_CONSTEXPR uint32_t IMAGE_META_CACHE_DEPTH = 20;
211 
212  //
213  // We must protect ourselves from user callbacks misbehaving
214  // and gobbling up all of our RX buffers
215  //
216  // These define the maximum number of datums that we will
217  // queue up in a user dispatch thread.
218 
221 
222  //
223  // PPS and IMU callbacks do not reserve an RX buffer, so queue
224  // depths are limited by RAM (via heap.)
225 
228 
229  //
230  // The maximum number of directed streams
231 
232  static CRL_CONSTEXPR uint32_t MAX_DIRECTED_STREAMS = 8;
233 
234  //
235  // A re-assembler for multi-packet messages
236 
237  class UdpTracker {
238  public:
239 
240  UdpTracker(uint32_t t,
241  UdpAssembler a,
244  m_bytesAssembled(0),
246  m_lastByteOffset(-1),
247  m_assembler(a),
248  m_stream(s) {};
249 
251  uint32_t packets() { return m_packetsAssembled; };
252 
253  bool assemble(uint32_t bytes,
254  uint32_t offset,
255  const uint8_t *dataP) {
256 
257  if (offset <= m_lastByteOffset)
258  CRL_EXCEPTION("out-of-order or duplicate packet");
259 
260  m_assembler(m_stream, dataP, offset, bytes);
261 
262  m_bytesAssembled += bytes;
263  m_lastByteOffset = offset;
265 
267  return true;
268  return false;
269  }
270 
271  private:
272 
279  };
280 
281  //
282  // The socket identifier and local port
283 
284  int32_t m_serverSocket;
286 
287  //
288  // The address of the sensor
289 
290  struct sockaddr_in m_sensorAddress;
291 
292  //
293  // The operating MTU of the sensor
294 
295  int32_t m_sensorMtu;
296 
297  //
298  // A buffer to receive incoming UDP packets
299 
300  std::vector<uint8_t> m_incomingBuffer;
301 
302  //
303  // Sequence ID for multi-packet message reassembly
304 
305  uint16_t m_txSeqId;
306  int32_t m_lastRxSeqId;
308 
309  //
310  // A cache to track incoming messages by sequence ID
311 
313 
314  //
315  // A pool of RX buffers, to reduce the amount of internal copying
316 
317  typedef std::vector<utility::BufferStreamWriter*> BufferPool;
318 
321 
322  //
323  // A cache of image meta data
324 
326 
327  //
328  // A map of custom UDP assemblers
329 
330  typedef std::map<wire::IdType, UdpAssembler> UdpAssemblerMap;
331 
332  UdpAssemblerMap m_udpAssemblerMap;
333 
334  //
335  // Mutex for callback registration and dispatching
336 
338 
339  //
340  // Mutex for stream control
341 
343 
344  //
345  // A flag to shut down the internal threads
346 
348 
349  //
350  // Internal UDP reception thread
351 
354 
355  //
356  // Internal status thread
357 
359 
360  //
361  // The lists of user callbacks
362 
363  std::list<ImageListener*> m_imageListeners;
364  std::list<LidarListener*> m_lidarListeners;
365  std::list<PpsListener*> m_ppsListeners;
366  std::list<ImuListener*> m_imuListeners;
367 
368  //
369  // A message signal interface
370 
372 
373  //
374  // A message storage interface
375 
377 
378  //
379  // The mask of currently enabled streams (desired)
380 
382 
383  //
384  // The current sensor time offset
385 
388  double m_timeOffset;
390 
391  //
392  // Cached version info from the device
393 
395 
396  //
397  // Cached StatusResponseMessage from the MultiSense
398 
400 
401  //
402  // Private procedures
403 
404  template<class T, class U> Status waitData(const T& command,
405  U& data,
406  const double& timeout=DEFAULT_ACK_TIMEOUT(),
407  int32_t attempts=DEFAULT_ACK_ATTEMPTS);
408 #if defined (_MSC_VER)
409  template<class T> Status waitAck (const T& msg,
410  wire::IdType id,
411  const double& timeout,
412  int32_t attempts);
413  template<class T> Status waitAck (const T& msg) {
414  return waitAck (msg, MSG_ID(T::ID), double(DEFAULT_ACK_TIMEOUT()), DEFAULT_ACK_ATTEMPTS);
415  }
416 #else
417  template<class T> Status waitAck (const T& msg,
418  wire::IdType id=MSG_ID(T::ID),
419  const double& timeout=DEFAULT_ACK_TIMEOUT(),
420  int32_t attempts=DEFAULT_ACK_ATTEMPTS);
421 #endif
422 
423  template<class T> void publish (const T& message);
424  void publish (const utility::BufferStreamWriter& stream);
425  void dispatch (utility::BufferStreamWriter& buffer);
427  image::Header& header);
429  lidar::Header& header);
430  void dispatchPps (pps::Header& header);
431  void dispatchImu (imu::Header& header);
432 
433 
434  utility::BufferStreamWriter& findFreeBuffer (uint32_t messageLength);
435  const int64_t& unwrapSequenceId(uint16_t id);
436  UdpAssembler getUdpAssembler (const uint8_t *udpDatagramP,
437  uint32_t length);
438 
439  void eraseFlashRegion (uint32_t region);
440  void programOrVerifyFlashRegion(std::ifstream& file,
441  uint32_t operation,
442  uint32_t region);
443  Status doFlashOp (const std::string& filename,
444  uint32_t operation,
445  uint32_t region);
446 
447  void applySensorTimeOffset(const double& offset);
448  double sensorToLocalTime (const double& sensorTime);
449  void sensorToLocalTime (const double& sensorTime,
450  uint32_t& seconds,
451  uint32_t& microseconds);
452 
453  void cleanup();
454  void bind ();
455  void handle ();
456 
457  //
458  // Static members
459 
462  static uint32_t hardwareApiToWire(uint32_t h);
463  static uint32_t hardwareWireToApi(uint32_t h);
464  static uint32_t imagerApiToWire(uint32_t h);
465  static uint32_t imagerWireToApi(uint32_t h);
466 #if WIN32
467  static DWORD WINAPI rxThread (void *userDataP);
468  static DWORD WINAPI statusThread (void *userDataP);
469 #else
470  static void *rxThread (void *userDataP);
471  static void *statusThread (void *userDataP);
472 #endif
473 };
474 
475 
476 }}}; // namespaces
477 
478 #endif // LibMultiSense_details_channel_hh
std::list< ImageListener * > m_imageListeners
Definition: channel.hh:363
static uint32_t imagerWireToApi(uint32_t h)
Definition: channel.cc:446
virtual Status setNetworkConfig(const system::NetworkConfig &c)
Definition: public.cc:933
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
static uint32_t hardwareWireToApi(uint32_t h)
Definition: channel.cc:418
virtual Status getMtu(int32_t &mtu)
Definition: public.cc:919
virtual Status verifyBitstream(const std::string &file)
Definition: public.cc:1131
virtual Status getLocalUdpPort(uint16_t &port)
Definition: public.cc:1297
virtual Status setImuConfig(bool storeSettingsInFlash, uint32_t samplesPerMessage, const std::vector< imu::Config > &c)
Definition: public.cc:1218
double sensorToLocalTime(const double &sensorTime)
Definition: channel.cc:481
virtual Status verifyFirmware(const std::string &file)
Definition: public.cc:1141
virtual Status setTriggerSource(TriggerSource s)
Definition: public.cc:514
virtual Status getExternalCalibration(system::ExternalCalibration &calibration)
Definition: public.cc:1034
bool assemble(uint32_t bytes, uint32_t offset, const uint8_t *dataP)
Definition: channel.hh:253
static CRL_CONSTEXPR uint32_t MAX_DIRECTED_STREAMS
Definition: channel.hh:232
virtual Status getLightingSensorStatus(lighting::SensorStatus &status)
Definition: public.cc:586
struct sockaddr_in m_sensorAddress
Definition: channel.hh:290
virtual Status networkTimeSynchronization(bool enabled)
Definition: public.cc:399
virtual Status removeIsolatedCallback(image::Callback callback)
Definition: public.cc:201
UdpTracker(uint32_t t, UdpAssembler a, utility::BufferStreamWriter &s)
Definition: channel.hh:240
void(* Callback)(const Header &header, void *userDataP)
UdpAssemblerMap m_udpAssemblerMap
Definition: channel.hh:332
virtual Status setExternalCalibration(const system::ExternalCalibration &calibration)
Definition: public.cc:1052
virtual Status getLightingConfig(lighting::Config &c)
Definition: public.cc:548
virtual Status getImageConfig(image::Config &c)
Definition: public.cc:637
void applySensorTimeOffset(const double &offset)
Definition: channel.cc:463
wire::StatusResponse m_statusResponseMessage
Definition: channel.hh:399
virtual Status setLidarCalibration(const lidar::Calibration &c)
Definition: public.cc:852
static wire::SourceType sourceApiToWire(DataSource mask)
Definition: channel.cc:355
std::string * frameId(M &m)
std::list< PpsListener * > m_ppsListeners
Definition: channel.hh:365
virtual Status getLargeBufferDetails(uint32_t &bufferCount, uint32_t &bufferSize)
Definition: public.cc:1243
virtual Status startDirectedStream(const DirectedStream &stream)
Definition: public.cc:448
virtual Status setMotorSpeed(float rpm)
Definition: public.cc:540
DepthCache< int64_t, wire::ImageMeta > m_imageMetaCache
Definition: channel.hh:325
void dispatch(utility::BufferStreamWriter &buffer)
Definition: dispatch.cc:163
virtual Status setMtu(int32_t mtu)
Definition: public.cc:894
DepthCache< int64_t, UdpTracker > m_udpTrackerCache
Definition: channel.hh:312
virtual Status getDeviceStatus(system::StatusMessage &status)
Definition: public.cc:1001
utility::Thread * m_rxThreadP
Definition: channel.hh:352
UdpAssembler getUdpAssembler(const uint8_t *udpDatagramP, uint32_t length)
Definition: dispatch.cc:491
void(* Callback)(const Header &header, void *userDataP)
virtual Status stopStreams(DataSource mask)
Definition: public.cc:422
virtual Status getNetworkConfig(system::NetworkConfig &c)
Definition: public.cc:940
virtual Status getSensorCalibration(image::SensorCalibration &c)
Definition: public.cc:779
void eraseFlashRegion(uint32_t region)
Definition: flash.cc:51
virtual Status setSensorCalibration(const image::SensorCalibration &c)
Definition: public.cc:795
std::vector< uint8_t > m_incomingBuffer
Definition: channel.hh:300
static CRL_CONSTEXPR uint32_t RX_POOL_SMALL_BUFFER_COUNT
Definition: channel.hh:204
virtual Status startDirectedStreams(const std::vector< DirectedStream > &streams)
Definition: public.cc:459
#define MSG_ID(x)
Definition: Protocol.h:248
static CRL_CONSTEXPR uint32_t RX_POOL_SMALL_BUFFER_SIZE
Definition: channel.hh:203
virtual Status addIsolatedCallback(image::Callback callback, DataSource imageSourceMask, void *userDataP)
Definition: public.cc:116
void(* UdpAssembler)(utility::BufferStreamWriter &stream, const uint8_t *dataP, uint32_t offset, uint32_t length)
Definition: channel.hh:186
utility::Mutex m_dispatchLock
Definition: channel.hh:337
static CRL_CONSTEXPR uint32_t MAX_USER_PPS_QUEUE_SIZE
Definition: channel.hh:226
virtual Status getVersionInfo(system::VersionInfo &v)
Definition: public.cc:621
static CRL_CONSTEXPR uint32_t IMAGE_META_CACHE_DEPTH
Definition: channel.hh:208
virtual Status getSensorVersion(VersionType &version)
Definition: public.cc:603
static CRL_CONSTEXPR VersionType API_VERSION
Definition: channel.hh:194
static CRL_CONSTEXPR uint16_t DEFAULT_SENSOR_TX_PORT
Definition: channel.hh:200
Definition: channel.cc:56
virtual Status getImuConfig(uint32_t &samplesPerMessage, std::vector< imu::Config > &c)
Definition: public.cc:1191
static CRL_CONSTEXPR uint32_t RX_POOL_LARGE_BUFFER_COUNT
Definition: channel.hh:202
virtual Status getEnabledStreams(DataSource &mask)
Definition: public.cc:436
wire::VersionResponse m_sensorVersion
Definition: channel.hh:394
utility::BufferStreamWriter & stream()
Definition: channel.hh:250
static CRL_CONSTEXPR uint32_t MAX_USER_IMU_QUEUE_SIZE
Definition: channel.hh:227
const int64_t & unwrapSequenceId(uint16_t id)
Definition: dispatch.cc:543
utility::BufferStreamWriter & findFreeBuffer(uint32_t messageLength)
Definition: dispatch.cc:517
static CRL_CONSTEXPR uint32_t DEFAULT_ACK_ATTEMPTS
Definition: channel.hh:207
virtual Status getImageHistogram(int64_t frameId, image::Histogram &histogram)
Definition: public.cc:363
virtual Status setLargeBuffers(const std::vector< uint8_t * > &buffers, uint32_t bufferSize)
Definition: public.cc:1255
static uint32_t imagerApiToWire(uint32_t h)
Definition: channel.cc:433
utility::Mutex m_streamLock
Definition: channel.hh:342
static CRL_CONSTEXPR uint32_t MAX_MTU_SIZE
Definition: channel.hh:199
static CRL_CONSTEXPR uint32_t UDP_TRACKER_CACHE_DEPTH
Definition: channel.hh:209
static CRL_CONSTEXPR double DEFAULT_ACK_TIMEOUT()
Definition: channel.hh:206
virtual Status startStreams(DataSource mask)
Definition: public.cc:408
virtual Status setDeviceInfo(const std::string &key, const system::DeviceInfo &i)
Definition: public.cc:1071
virtual void * reserveCallbackBuffer()
Definition: public.cc:313
virtual Status getApiVersion(VersionType &version)
Definition: public.cc:612
virtual Status setImageCalibration(const image::Calibration &c)
Definition: public.cc:759
static CRL_CONSTEXPR uint32_t MAX_USER_LASER_QUEUE_SIZE
Definition: channel.hh:220
void dispatchImu(imu::Header &header)
Definition: dispatch.cc:148
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
virtual Status stopDirectedStream(const DirectedStream &stream)
Definition: public.cc:474
void dispatchPps(pps::Header &header)
Definition: dispatch.cc:133
virtual Status getImageCalibration(image::Calibration &c)
Definition: public.cc:735
static CRL_CONSTEXPR uint32_t RX_POOL_LARGE_BUFFER_SIZE
Definition: channel.hh:201
std::list< LidarListener * > m_lidarListeners
Definition: channel.hh:364
virtual Status getImuInfo(uint32_t &maxSamplesPerMessage, std::vector< imu::Info > &info)
Definition: public.cc:1151
virtual Status maxDirectedStreams(uint32_t &maximum)
Definition: public.cc:505
virtual Status getDeviceModes(std::vector< system::DeviceMode > &modes)
Definition: public.cc:865
static uint32_t hardwareApiToWire(uint32_t h)
Definition: channel.cc:403
impl(const std::string &address)
Definition: channel.cc:63
virtual Status flashBitstream(const std::string &file)
Definition: public.cc:1111
void publish(const T &message)
Definition: query.hh:50
void(* Callback)(const Header &header, void *userDataP)
static void * rxThread(void *userDataP)
Definition: dispatch.cc:698
virtual Status setTransmitDelay(const image::TransmitDelay &c)
Definition: public.cc:823
void dispatchLidar(utility::BufferStream &buffer, lidar::Header &header)
Definition: dispatch.cc:118
std::list< ImuListener * > m_imuListeners
Definition: channel.hh:366
std::vector< utility::BufferStreamWriter * > BufferPool
Definition: channel.hh:317
static CRL_CONSTEXPR uint32_t TIME_SYNC_OFFSET_DECAY
Definition: channel.hh:210
virtual Status getLidarCalibration(lidar::Calibration &c)
Definition: public.cc:835
virtual Status getTransmitDelay(image::TransmitDelay &c)
Definition: public.cc:808
utility::BufferStreamWriter m_stream
Definition: channel.hh:278
std::map< wire::IdType, UdpAssembler > UdpAssemblerMap
Definition: channel.hh:330
static CRL_CONSTEXPR uint32_t MAX_USER_IMAGE_QUEUE_SIZE
Definition: channel.hh:219
Status waitData(const T &command, U &data, const double &timeout=DEFAULT_ACK_TIMEOUT(), int32_t attempts=DEFAULT_ACK_ATTEMPTS)
Definition: query.hh:120
static void * statusThread(void *userDataP)
Definition: channel.cc:505
virtual Status flashFirmware(const std::string &file)
Definition: public.cc:1121
virtual Status setLightingConfig(const lighting::Config &c)
Definition: public.cc:569
void programOrVerifyFlashRegion(std::ifstream &file, uint32_t operation, uint32_t region)
Definition: flash.cc:115
#define CRL_CONSTEXPR
Definition: Portability.hh:38
virtual Status setImageConfig(const image::Config &c)
Definition: public.cc:698
virtual Status getDirectedStreams(std::vector< DirectedStream > &streams)
Definition: public.cc:485
utility::Thread * m_statusThreadP
Definition: channel.hh:358
static DataSource sourceWireToApi(wire::SourceType mask)
Definition: channel.cc:379
Status doFlashOp(const std::string &filename, uint32_t operation, uint32_t region)
Definition: flash.cc:184
virtual Status getDeviceInfo(system::DeviceInfo &info)
Definition: public.cc:957
void(* Callback)(const Header &header, void *userDataP)
void dispatchImage(utility::BufferStream &buffer, image::Header &header)
Definition: dispatch.cc:102
virtual Status releaseCallbackBuffer(void *referenceP)
Definition: public.cc:337


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