communicator.cpp
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 #include "communicator.h"
66 #include "protocolhandler.h"
67 #include "replymonitor.h"
68 #include <xstypes/xsportinfo.h>
69 #include "usbinterface.h"
70 #include <xstypes/xsdid.h>
71 #include <xstypes/xsbusid.h>
72 #include "xsdeviceconfiguration.h"
73 #include "xsusbhubinfo.h"
74 #include "xsscanner.h"
77 #include "xsdevice_def.h"
78 
79 using namespace xsens;
80 using namespace XsTime;
81 
85  : m_preparedForDestruction(false)
86  , m_masterInfo(nullptr)
87  , m_protocolManager(new ProtocolManager(*this))
88  , m_masterDeviceId(0)
89  , m_replyMonitor(new ReplyMonitor)
90  , m_lastResult(XRV_OK)
91  , m_defaultTimeout(500)
92 {
93 #ifdef XSENS_DEBUG
95  assert(addOk);
96 #else
97  protocolManager()->add(new ProtocolHandler());
98 #endif
99 
100  JLDEBUGG("Created " << (void*) this);
101 }
102 
106 {
107  JLDEBUGG("Destroyed " << (void*) this);
108  assert(m_preparedForDestruction);
109  m_masterInfo = nullptr;
110 }
111 
115 {
116  m_replyMonitor.reset(nullptr);
117  m_protocolManager.reset();
118 
120 }
121 
122 
127 {
129  assert(m_masterInfo == nullptr);
131 }
132 
136 {
137  return m_masterInfo;
138 }
139 
142 std::shared_ptr<ProtocolManager> Communicator::protocolManager() const
143 {
144  return m_protocolManager;
145 }
146 
151 {
152  m_masterDeviceId = id;
153 }
154 
158 {
159  return m_masterDeviceId;
160 }
161 
168 {
169  return m_lastResult;
170 }
171 
178 {
179  return m_lastResultText;
180 }
181 
185 {
186  return m_masterInfo ? m_masterInfo->deviceConfigurationConst().numberOfDevices() : 0;
187 }
188 
192 {
193  // handle one message at a time. This is only really necessary for dual-stream interfaces such as to the bodypack
194  xsens::Lock locky(&m_handleMux);
195 
196  //JLTRACEG("Received " << JLHEXLOG(message.getMessageId()) << " size " << message.getTotalMessageSize());
197  if (message.getMessageId() == XMID_Error)
198  {
199  char buffer[256];
200  XsSize sz = message.getTotalMessageSize();
201  const uint8_t* m = message.getMessageStart();
202  for (XsSize i = 0; i < sz; ++i)
203  sprintf(buffer + 2 * i, "%02X", (unsigned int) m[i]);
204  buffer[2 * sz] = 0;
205  JLALERTG("Error message received: " << buffer);
207  }
208  if (!m_replyMonitor->addReply(message) && m_masterInfo)
209  m_masterInfo->handleMessage(message);
210 }
211 
215 {
216  XsMessage rcv;
217  return doTransaction(msg, rcv, timeout);
218 }
219 
223 {
224  return doTransaction(msg, defaultTimeout());
225 }
226 
230 {
231  return doTransaction(msg, rcv, defaultTimeout());
232 }
233 
239 {
240  (void)setAndReturnLastResult(res, text);
241 }
242 
249 {
250  m_lastResultText = text;
251  return m_lastResult = res;
252 }
253 
262 bool Communicator::sanityCheck(XsMessage const& msg) const
263 {
264  if (m_masterInfo)
266 
267  return true;
268 }
269 
274 std::shared_ptr<ReplyObject> Communicator::addReplyObject(uint8_t mid)
275 {
276  assert(m_replyMonitor != nullptr);
277  return m_replyMonitor->addReplyObject(new MidReplyObject(mid));
278 }
279 
287 std::shared_ptr<ReplyObject> Communicator::addReplyObject(uint8_t mid, XsSize offset, XsSize size, uint8_t const* data)
288 {
289  assert(m_replyMonitor != nullptr);
290  return m_replyMonitor->addReplyObject(new MidAndDataReplyObject(mid, offset, size, data));
291 }
292 
297 std::shared_ptr<ReplyObject> Communicator::addReplyObject(ReplyObject* obj)
298 {
299  assert(m_replyMonitor != nullptr && obj != nullptr);
300  return m_replyMonitor->addReplyObject(obj);
301 }
302 
304 {
305  assert(handler != 0);
306 #ifdef XSENS_DEBUG
307  ProtocolManager::value_type addOk = protocolManager()->add(handler);
308  assert(addOk);
309 #else
310  protocolManager()->add(handler);
311 #endif
312 }
313 
318 {
319  bool removeOk = protocolManager()->remove(type);
320  assert(removeOk);
321  (void)removeOk; // Prevent unused variable warning in release builds
322 }
323 
328 {
329  return protocolManager()->hasProtocol(type);
330 }
331 
337 void Communicator::setCredentials(XsString const& id, XsString const& key)
338 {
339  (void) id;
340  (void) key;
341 }
342 
345 {
347  delete this;
348 }
349 
351 {
352  return false;
353 }
354 
356 {
357  return true;
358 }
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
XsDevice::handleMessage
virtual XSNOEXPORT void handleMessage(const XsMessage &msg)
msg
msg
Communicator::allowReprocessing
virtual bool allowReprocessing() const
Definition: communicator.cpp:355
ProtocolHandler
Message protocol handling class.
Definition: protocolhandler.h:71
xsusbhubinfo.h
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
replymonitor.h
XMID_Error
@ XMID_Error
Definition: xsxbusmessageid.h:187
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
Communicator::setMasterDeviceId
void setMasterDeviceId(const XsDeviceId &deviceId)
Sets a master device ID.
Definition: communicator.cpp:150
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
xsens::ReplyMonitor
A monitor class for receiving replies messages in a thread.
Definition: replymonitor.h:91
xsens_janitors.h
Communicator::addReplyObject
std::shared_ptr< ReplyObject > addReplyObject(uint8_t mid)
Add a MidReplyObject.
Definition: communicator.cpp:274
protocolhandler.h
Communicator::m_lastResult
XsResultValue m_lastResult
A last result variable.
Definition: communicator.h:363
JLALERTG
#define JLALERTG(msg)
Definition: journaller.h:281
xsportinfo.h
Communicator::Communicator
Communicator(void)
Constructor, creates some management objects and clears the rest by calling initialize()
Definition: communicator.cpp:84
JLL_Alert
@ JLL_Alert
only log fatal, error and alert messages
Definition: journalloglevel.h:92
data
data
xsens_debugtools.h
XRV_OK
@ XRV_OK
0: Operation was performed successfully
Definition: xsresultvalue.h:85
Communicator::addProtocolHandler
virtual void addProtocolHandler(IProtocolHandler *handler)
Adds a protocol handler.
Definition: communicator.cpp:303
XsResultValue
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:82
Communicator::lastResult
XsResultValue lastResult() const
Get the result value of the last operation.
Definition: communicator.cpp:167
JLIF
#define JLIF(journal, level, todo)
Definition: journaller.h:298
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::doTransaction
bool doTransaction(const XsMessage &message)
Write a message and await the reply.
Definition: communicator.cpp:222
XsTime
Namespace for time and date constants and operations.
XsDevice::deviceConfigurationConst
virtual XsDeviceConfiguration const &XSNOEXPORT deviceConfigurationConst() const
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
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
XsDevice::messageLooksSane
virtual XSNOEXPORT bool messageLooksSane(const XsMessage &msg) const
usbinterface.h
IProtocolHandler
Interface class for protocol handlers.
Definition: iprotocolhandler.h:78
xsbusid.h
Communicator::masterDevice
XsDevice * masterDevice() const
Definition: communicator.cpp:135
xsdevice_def.h
xsdid.h
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
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
XsDeviceId
Contains an Xsens device ID and provides operations for determining the type of device.
Definition: xsdeviceid.h:192
MidAndDataReplyObject
Reply object that checks the message identifier and data in the data field.
Definition: replyobject.h:123
ProtocolManager::value_type
std::shared_ptr< IProtocolHandler > value_type
A typedef for a value type.
Definition: protocolmanager.h:86
MidReplyObject
Reply object that only checks the message identifier.
Definition: replyobject.h:108
Communicator::prepareForDestruction
virtual void prepareForDestruction()
Prepares communicator for destruction.
Definition: communicator.cpp:114
JLDEBUGG
#define JLDEBUGG(msg)
Definition: journaller.h:280
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
xsens::Lock
A base class for a Lock.
Definition: xsens_mutex.h:947
XsProtocolType
XsProtocolType
Protocol types (XsDevice::enableProtocol())
Definition: xsprotocoltype.h:72
Communicator::setLastResult
void setLastResult(XsResultValue lastResult, XsString const &text=XsString()) const
Sets the last result.
Definition: communicator.cpp:238
communicator.h
Communicator::destroy
void destroy()
Destroys the communicator.
Definition: communicator.cpp:344
xsscanner.h
xsdeviceconfiguration.h
Communicator::setAndReturnLastResult
XsResultValue setAndReturnLastResult(XsResultValue lastResult, XsString const &text=XsString()) const
Sets the last result and returns it.
Definition: communicator.cpp:248
XsString
A 0-terminated managed string of characters.
XsDevice
Definition: xsdevice_def.h:164
xsens
Definition: threading.cpp:78
ProtocolManager
Supplies multi-protocol data stream parsing.
Definition: protocolmanager.h:81
gJournal
Journaller * gJournal
Definition: configure_outputs.cpp:4


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