communicator.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #ifndef COMMUNICATOR_H
66 #define COMMUNICATOR_H
67 
68 #include "callbackmanagerxda.h"
69 #include "openportstage.h"
70 #include "protocolmanager.h"
71 #include "replyobject.h"
72 
73 #include <xscommon/xsens_mutex.h>
74 #include "iointerfacefile.h"
75 #include "iprotocolhandler.h"
76 #include "serialinterface.h"
77 #include <xstypes/xsresultvalue.h>
78 #include <xstypes/xstimestamp.h>
79 
80 #include <memory>
81 #include <vector>
82 
83 struct XsByteArray;
84 struct XsString;
85 struct XsMessage;
87 namespace xsens
88 {
89 class ReplyMonitor;
90 }
91 
96 {
97 public:
98  void destroy();
99 
101  struct Deleter
102  {
105  {
106  if (c != nullptr)
107  c->destroy();
108  }
109  };
110 
112  template <typename T>
113  using UniquePtr = std::unique_ptr<T, Deleter>;
114 
116  template <typename T>
118  {
119  return UniquePtr<T>(new T(), Deleter());
120  }
121 
123  template <typename T>
124  static UniquePtr<T> createUniquePtr(T* communicator)
125  {
126  return UniquePtr<T>(communicator, Deleter());
127  }
128 
129  Communicator(void);
130 
131  bool doTransaction(const XsMessage& message);
132  bool doTransaction(const XsMessage& message, uint32_t timeout);
133  bool doTransaction(const XsMessage& message, XsMessage& rcv);
134 
136  virtual bool doTransaction(const XsMessage& message, XsMessage& rcv, uint32_t timeout) = 0;
137 
140  {
141  m_defaultTimeout = timeout;
142  }
143 
146  {
147  return m_defaultTimeout;
148  }
149 
150  XsResultValue lastResult() const;
151  XsString lastResultText() const;
152  virtual void handleMessage(const XsMessage& message);
153  XsSize childDeviceCount() const;
154  XsDeviceId masterDeviceId() const;
155 
156  std::shared_ptr<ReplyObject> addReplyObject(uint8_t mid);
157  std::shared_ptr<ReplyObject> addReplyObject(uint8_t mid, XsSize offset, XsSize size, uint8_t const* data);
158  std::shared_ptr<ReplyObject> addReplyObject(ReplyObject* obj);
159 
160  // live stuff
161 
166  virtual XsResultValue gotoConfig(bool detectRs485 = false) = 0;
167 
171  virtual XsResultValue gotoMeasurement() = 0;
172 
176  virtual XsResultValue getDeviceId() = 0;
177 
181  virtual void setGotoConfigTimeout(uint32_t timeout) = 0;
182 
187  virtual bool writeMessage(const XsMessage& message) = 0;
188 
191  virtual void flushPort() = 0;
192 
195  virtual void closePort() = 0;
196 
199  virtual void scheduleClosePort()
200  {
201  closePort();
202  }
203 
206  virtual bool isPortOpen() const = 0;
207 
210  virtual XsPortInfo portInfo() const = 0;
211 
218  virtual bool openPort(const XsPortInfo& portInfo, OpenPortStage stage = OPS_Full, bool detectRs485 = false) = 0;
219 
225  virtual bool reopenPort(OpenPortStage stage = OPS_Full, bool skipDeviceIdCheck = false) = 0;
226 
229  virtual bool isDockedAt(Communicator* other) const = 0;
230 
233  virtual void setKeepAlive(bool enable) = 0;
234 
235  // file stuff
236 
240  virtual void closeLogFile() = 0;
241 
246  virtual XsMessage readMessage(uint8_t msgId = 0) = 0;
247 
253  virtual XsMessage readMessageFromStartOfFile(uint8_t msgId, int maxMsgs = 0) = 0;
254 
260  virtual std::deque<XsMessage> readMessagesFromStartOfFile(uint8_t msgId, int maxMsgs = 0) = 0;
261 
264  virtual void loadLogFile(XsDevice* device) = 0;
265 
268  virtual void abortLoadLogFile() = 0;
269 
274  virtual bool openLogFile(const XsString& filename) = 0;
275 
278  virtual XsString logFileName() const = 0;
279 
282  virtual XsFilePos logFileSize() const = 0;
283 
286  virtual XsTimeStamp logFileDate() const = 0;
287 
290  virtual XsFilePos logFileReadPosition() const = 0;
291 
294  virtual void resetLogFileReadPosition(void) = 0;
295 
298  virtual bool isReadingFromFile() const = 0;
299 
302  virtual void waitForLastTaskCompletion() = 0;
303 
306  virtual bool isLoadLogFileInProgress() const;
307 
310  virtual bool allowReprocessing() const;
311 
317  {
318  return 20;
319  }
320 
321  virtual void setCredentials(XsString const& id, XsString const& key);
322 
323  bool sanityCheck(XsMessage const& msg) const;
324 
328  virtual void addProtocolHandler(IProtocolHandler* handler);
330  bool hasProtocol(XsProtocolType type) const;
331 
333 
334 protected:
335  virtual ~Communicator();
336  virtual void prepareForDestruction();
337 
338  XsDevice* masterDevice() const;
339  std::shared_ptr<ProtocolManager> protocolManager() const;
340 
341  // these may be a bit surprising:
342  void setLastResult(XsResultValue lastResult, XsString const& text = XsString()) const;
344 
345  void setMasterDeviceId(const XsDeviceId& deviceId);
346 
349 
352 
354  std::shared_ptr<ProtocolManager> m_protocolManager;
355 
358 
360  std::unique_ptr<xsens::ReplyMonitor> m_replyMonitor;
361 
364 
367 
370 
373 };
374 
375 #endif
XsString
struct XsString XsString
Definition: xsstring.h:87
Communicator::removeProtocolHandler
void removeProtocolHandler(XsProtocolType type)
Removes a protocol handler.
Definition: communicator.cpp:317
ReplyObject
Abstract reply object. Blocks on a semaphore when requesting the message until the message has been s...
Definition: replyobject.h:81
Communicator::hasProtocol
bool hasProtocol(XsProtocolType type) const
Definition: communicator.cpp:327
Communicator::lastResultText
XsString lastResultText() const
Get the accompanying error text for the value returned by lastResult() It may provide situation-speci...
Definition: communicator.cpp:177
Communicator::gotoMeasurement
virtual XsResultValue gotoMeasurement()=0
Request a device to go to measurement mode.
Communicator::isDockedAt
virtual bool isDockedAt(Communicator *other) const =0
Returns true if the other device is docked at this device.
XsByteArray
A list of uint8_t values.
Communicator::allowReprocessing
virtual bool allowReprocessing() const
Definition: communicator.cpp:355
Communicator::portInfo
virtual XsPortInfo portInfo() const =0
OpenPortStage
OpenPortStage
Port opening stages.
Definition: openportstage.h:76
Communicator::defaultTimeout
uint32_t defaultTimeout() const
Definition: communicator.h:145
Communicator::sanityCheck
bool sanityCheck(XsMessage const &msg) const
Do a sanity check on a potential message.
Definition: communicator.cpp:262
Communicator::setGotoConfigTimeout
virtual void setGotoConfigTimeout(uint32_t timeout)=0
Set the timeout for the gotoConfig function.
Communicator::setMasterDevice
void setMasterDevice(XsDevice *masterDevice)
Sets a master device.
Definition: communicator.cpp:126
Communicator::m_masterDeviceId
XsDeviceId m_masterDeviceId
A master device ID.
Definition: communicator.h:357
Communicator::protocolManager
std::shared_ptr< ProtocolManager > protocolManager() const
Definition: communicator.cpp:142
XsDeviceConfiguration
Structure containing a full device configuration as returned by the ReqConfig message.
Definition: xsdeviceconfiguration.h:143
Communicator::setMasterDeviceId
void setMasterDeviceId(const XsDeviceId &deviceId)
Sets a master device ID.
Definition: communicator.cpp:150
Communicator::Deleter
The communicator deleter.
Definition: communicator.h:101
Communicator::masterDeviceId
XsDeviceId masterDeviceId() const
Definition: communicator.cpp:157
Communicator::handleMessage
virtual void handleMessage(const XsMessage &message)
Handles a message.
Definition: communicator.cpp:191
Communicator::m_preparedForDestruction
bool m_preparedForDestruction
Prepared for destruction boolean variable.
Definition: communicator.h:348
Communicator::addReplyObject
std::shared_ptr< ReplyObject > addReplyObject(uint8_t mid)
Add a MidReplyObject.
Definition: communicator.cpp:274
xstimestamp.h
Communicator::m_lastResult
XsResultValue m_lastResult
A last result variable.
Definition: communicator.h:363
Communicator::gotoConfig
virtual XsResultValue gotoConfig(bool detectRs485=false)=0
Request a device to go to config mode.
Communicator::logFileReadPosition
virtual XsFilePos logFileReadPosition() const =0
Communicator::isPortOpen
virtual bool isPortOpen() const =0
Communicator::Communicator
Communicator(void)
Constructor, creates some management objects and clears the rest by calling initialize()
Definition: communicator.cpp:84
xsens_mutex.h
Communicator::isReadingFromFile
virtual bool isReadingFromFile() const =0
Communicator::addProtocolHandler
virtual void addProtocolHandler(IProtocolHandler *handler)
Adds a protocol handler.
Definition: communicator.cpp:303
Communicator::flushPort
virtual void flushPort()=0
Flushes all remaining data on the open port.
Communicator::loadLogFile
virtual void loadLogFile(XsDevice *device)=0
Load a complete logfile.
XsResultValue
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:82
iprotocolhandler.h
Communicator::lastResult
XsResultValue lastResult() const
Get the result value of the last operation.
Definition: communicator.cpp:167
Communicator::m_replyMonitor
std::unique_ptr< xsens::ReplyMonitor > m_replyMonitor
An unique pointer to a reply monitor.
Definition: communicator.h:360
Communicator::m_handleMux
xsens::Mutex m_handleMux
A handle mutex.
Definition: communicator.h:369
Communicator::readMessage
virtual XsMessage readMessage(uint8_t msgId=0)=0
Read a message from the open file.
Communicator::doTransaction
bool doTransaction(const XsMessage &message)
Write a message and await the reply.
Definition: communicator.cpp:222
Communicator
A base struct for a communication interface.
Definition: communicator.h:95
protocolmanager.h
Communicator::createUniquePtr
static UniquePtr< T > createUniquePtr(T *communicator)
Creates a UniquePtr<T> from a Communicator*.
Definition: communicator.h:124
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
XsPortInfo
Contains a descriptor for opening a communication port to an Xsens device.
Definition: xsportinfo.h:128
Communicator::setCredentials
virtual void setCredentials(XsString const &id, XsString const &key)
Set the credentials required for using the device.
Definition: communicator.cpp:337
Communicator::isLoadLogFileInProgress
virtual bool isLoadLogFileInProgress() const
Definition: communicator.cpp:350
IProtocolHandler
Interface class for protocol handlers.
Definition: iprotocolhandler.h:78
Communicator::setDefaultTimeout
void setDefaultTimeout(uint32_t timeout)
Sets a default timeout.
Definition: communicator.h:139
xsens::Mutex
A base mutex class.
Definition: xsens_mutex.h:132
Communicator::masterDevice
XsDevice * masterDevice() const
Definition: communicator.cpp:135
replyobject.h
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
serialinterface.h
Communicator::Deleter::operator()
void operator()(Communicator *c)
A function that destroys communicator.
Definition: communicator.h:104
Communicator::waitForLastTaskCompletion
virtual void waitForLastTaskCompletion()=0
Wait for the last processing task to complete in the threadpool.
Communicator::m_masterInfo
XsDevice * m_masterInfo
A master device object.
Definition: communicator.h:351
XsMessage
Structure for storing a single message.
Definition: xsmessage.h:202
openportstage.h
Communicator::resetLogFileReadPosition
virtual void resetLogFileReadPosition(void)=0
Resets the logfile read position.
Communicator::readMessageFromStartOfFile
virtual XsMessage readMessageFromStartOfFile(uint8_t msgId, int maxMsgs=0)=0
Read a message from the start of the open file.
XsDeviceId
Contains an Xsens device ID and provides operations for determining the type of device.
Definition: xsdeviceid.h:192
Communicator::openLogFile
virtual bool openLogFile(const XsString &filename)=0
Open the log file `.
Communicator::getDeviceId
virtual XsResultValue getDeviceId()=0
Request a device to get device ID.
Communicator::readMessagesFromStartOfFile
virtual std::deque< XsMessage > readMessagesFromStartOfFile(uint8_t msgId, int maxMsgs=0)=0
Read multiple similar messages from the start of the open file.
Communicator::logFileDate
virtual XsTimeStamp logFileDate() const =0
Communicator::setKeepAlive
virtual void setKeepAlive(bool enable)=0
Either disable or enable (default) the keep alive mechanism (if supported by the device)
Communicator::reopenPort
virtual bool reopenPort(OpenPortStage stage=OPS_Full, bool skipDeviceIdCheck=false)=0
Reopens the port.
Communicator::configurationMessageSearchLimit
static int configurationMessageSearchLimit()
Definition: communicator.h:316
Communicator::prepareForDestruction
virtual void prepareForDestruction()
Prepares communicator for destruction.
Definition: communicator.cpp:114
Communicator::scheduleClosePort
virtual void scheduleClosePort()
Schedules to close the open port.
Definition: communicator.h:199
Communicator::writeMessage
virtual bool writeMessage(const XsMessage &message)=0
Write message to the device.
Communicator::logFileSize
virtual XsFilePos logFileSize() const =0
Communicator::m_protocolManager
std::shared_ptr< ProtocolManager > m_protocolManager
A shared pointer to protocl manager.
Definition: communicator.h:354
Communicator::~Communicator
virtual ~Communicator()
Destructor, waits for the last scheduled task to complete and then cleans up the object by calling cl...
Definition: communicator.cpp:105
Communicator::m_lastResultText
XsString m_lastResultText
A last result string.
Definition: communicator.h:366
Communicator::childDeviceCount
XsSize childDeviceCount() const
Definition: communicator.cpp:184
iointerfacefile.h
Communicator::abortLoadLogFile
virtual void abortLoadLogFile()=0
Aborts loading a logfile.
Communicator::createUniquePtr
static UniquePtr< T > createUniquePtr()
Constructs a new Communicator of type T and returns it as a UniquePtr<T>
Definition: communicator.h:117
Communicator::UniquePtr
std::unique_ptr< T, Deleter > UniquePtr
Initializes of UniquePtr<T>
Definition: communicator.h:113
XsProtocolType
XsProtocolType
Protocol types (XsDevice::enableProtocol())
Definition: xsprotocoltype.h:72
OPS_Full
@ OPS_Full
Definition: openportstage.h:83
callbackmanagerxda.h
Communicator::logFileName
virtual XsString logFileName() const =0
Communicator::setLastResult
void setLastResult(XsResultValue lastResult, XsString const &text=XsString()) const
Sets the last result.
Definition: communicator.cpp:238
Communicator::destroy
void destroy()
Destroys the communicator.
Definition: communicator.cpp:344
xsresultvalue.h
CallbackManagerXda
Class that delegates callbacks to registered XsCallbackHandlerItems.
Definition: callbackmanagerxda.h:78
Communicator::closeLogFile
virtual void closeLogFile()=0
Close the log file.
Communicator::openPort
virtual bool openPort(const XsPortInfo &portInfo, OpenPortStage stage=OPS_Full, bool detectRs485=false)=0
Opens a port.
Communicator::setAndReturnLastResult
XsResultValue setAndReturnLastResult(XsResultValue lastResult, XsString const &text=XsString()) const
Sets the last result and returns it.
Definition: communicator.cpp:248
Communicator::closePort
virtual void closePort()=0
Closes the open port.
XsFilePos
int64_t XsFilePos
The type that is used for positioning inside a file.
Definition: xsfilepos.h:102
XsString
A 0-terminated managed string of characters.
XsDevice
Definition: xsdevice_def.h:164
xsens
Definition: threading.cpp:78
XsTimeStamp
This class contains method to set, retrieve and compare timestamps.
Definition: xstimestamp.h:115
Communicator::m_defaultTimeout
uint32_t m_defaultTimeout
A default timeout variable.
Definition: communicator.h:372


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20