proxycommunicator.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 "proxycommunicator.h"
66 #include "callbackmanagerxda.h"
67 #include <xstypes/xsportinfo.h>
69 #include "deviceredetector.h"
70 
71 
85 ProxyCommunicator::ProxyCommunicator(int channelId, uint32_t channelLatency)
86  : m_channelId(channelId)
87  , m_channelLatency(channelLatency)
88 {
89 }
90 
94 {
95 }
96 
100 {
101  //ProxyCommunicator maintains no stream. No action required
102 }
103 
107 {
109 }
110 
114 {
115  return !m_activePortInfo.portName().empty();
116 }
117 
125 bool ProxyCommunicator::openPort(const XsPortInfo& portInfo, OpenPortStage stage, bool detectRs485)
126 {
127  auto result = [this]()
128  {
129  bool ok = (lastResult() == XRV_OK);
130  if (!ok)
132  return ok;
133  };
134 
135  if (stage & OPS_OpenPort)
136  {
137  if (isPortOpen())
138  {
140  return (this->portInfo() == portInfo);
141  }
142  }
143 
144  if (stage & OPS_InitDevice)
145  {
146  if (gotoConfig(detectRs485) != XRV_OK)
147  return result();
148 
149  if (getDeviceId() != XRV_OK)
150  return result();
151  }
152 
155  m_activePortInfo.setDeviceId(masterDeviceId());
156  return result();
157 }
158 
164 bool ProxyCommunicator::reopenPort(OpenPortStage stage, bool skipDeviceIdCheck)
165 {
166  XsPortInfo pi = portInfo();
167 
168  closePort();
169  DeviceRedetector redetector(pi);
170  if (!redetector.redetect(masterDeviceId(), pi, skipDeviceIdCheck))
171  return false;
172 
173  return openPort(pi, stage);
174 }
175 
179 {
180  return false;
181 }
182 
186 {
188  return XRV_OK;
189 }
190 
194 {
195  addRawData(data);
196 }
197 
200 {
202 }
203 
207 {
208  //No action required as data is pushed (no polling required) in the buffer by the handleReceivedData function
209  return XRV_OK;
210 }
211 
215 {
217 }
218 
224 XsResultValue ProxyCommunicator::processBufferedData(const XsByteArray& rawIn, std::deque<XsMessage>& messages)
225 {
226  return extractMessages(rawIn, messages);
227 }
228 
232 {
234  char tmp[16];
235  snprintf(tmp, sizeof(tmp), "PROXY#%d", channelId);
236  portInfo.setPortName(tmp);
237  return portInfo;
238 }
239 
243 {
244  return m_channelLatency;
245 }
ProxyCommunicator::isPortOpen
bool isPortOpen() const override
Checks if the associated port is open.
Definition: proxycommunicator.cpp:113
ProxyCommunicator::defaultInterfaceTimeout
uint32_t defaultInterfaceTimeout() const override
Returns the default timeout needed for this interface.
Definition: proxycommunicator.cpp:242
msg
msg
XsByteArray
A list of uint8_t values.
ProxyCommunicator::writeRawData
XsResultValue writeRawData(const XsByteArray &data) override
Writes raw data to the communication channel.
Definition: proxycommunicator.cpp:185
OpenPortStage
OpenPortStage
Port opening stages.
Definition: openportstage.h:76
OPS_OpenPort
@ OPS_OpenPort
Definition: openportstage.h:78
DeviceCommunicator::doTransaction
virtual bool doTransaction(const XsMessage &msg, XsMessage &rcv, uint32_t timeout) override
Write a message and await the reply.
Definition: devicecommunicator.cpp:110
OPS_InitDevice
@ OPS_InitDevice
Definition: openportstage.h:80
DeviceCommunicator::handleMessage
void handleMessage(const XsMessage &message) override
Handles a message.
Definition: devicecommunicator.cpp:327
ProxyCommunicator::openPort
bool openPort(const XsPortInfo &portInfo, OpenPortStage stage=OPS_Full, bool detectRs485=false) override
Opens a proxy port.
Definition: proxycommunicator.cpp:125
ProxyCommunicator::doTransaction
bool doTransaction(const XsMessage &msg, XsMessage &rcv, uint32_t timeout) override
Write a message and await the reply.
Definition: proxycommunicator.cpp:199
SerialCommunicator::portInfo
XsPortInfo portInfo() const override
Definition: serialcommunicator.cpp:236
Communicator::masterDeviceId
XsDeviceId masterDeviceId() const
Definition: communicator.cpp:157
DeviceCommunicator::extractMessages
XsResultValue extractMessages(const XsByteArray &rawIn, std::deque< XsMessage > &messages, RxChannelId channel=0)
Read all messages available in the incoming data stream after adding new data supplied in rawIn.
Definition: devicecommunicator.cpp:290
ProxyCommunicator::processBufferedData
XsResultValue processBufferedData(const XsByteArray &rawIn, std::deque< XsMessage > &messages) override
Read all messages from the buffered read data after adding new data supplied in rawIn.
Definition: proxycommunicator.cpp:224
DeviceRedetector
A class which re-detects a device with a certain device Id.
Definition: deviceredetector.h:73
xsens_janitors.h
ProxyCommunicator::isDockedAt
bool isDockedAt(Communicator *other) const override
Has no effect for the ProxyCommunicator. Always returns false.
Definition: proxycommunicator.cpp:178
ProxyCommunicator::closePort
void closePort() override
Closes the port.
Definition: proxycommunicator.cpp:106
XRV_ALREADYOPEN
@ XRV_ALREADYOPEN
269: An I/O device is already opened with this object
Definition: xsresultvalue.h:139
xsportinfo.h
ok
ROSCPP_DECL bool ok()
data
data
XRV_OK
@ XRV_OK
0: Operation was performed successfully
Definition: xsresultvalue.h:85
ProxyCommunicator::reopenPort
bool reopenPort(OpenPortStage stage=OPS_Full, bool skipDeviceIdCheck=false) override
Closes and tries to reopen the port.
Definition: proxycommunicator.cpp:164
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
DeviceRedetector::redetect
bool redetect(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck=false)
Base redetect function which calls the appropriate redetect based on deviceId.
Definition: deviceredetector.cpp:120
ProxyCommunicator::handleMessage
void handleMessage(const XsMessage &message) override
Handles a message received on the communication channel.
Definition: proxycommunicator.cpp:214
Communicator
A base struct for a communication interface.
Definition: communicator.h:95
XsPortInfo
struct XsPortInfo XsPortInfo
Definition: xsportinfo.h:83
SerialCommunicator::gotoConfig
XsResultValue gotoConfig(bool detectRs485=false) override
Request a device to go to config mode.
Definition: serialcommunicator.cpp:138
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
proxycommunicator.h
ProxyCommunicator::createPortInfo
static XsPortInfo createPortInfo(int channelId)
Creates a default port info object based on the given user-provided channel identifier.
Definition: proxycommunicator.cpp:231
XsMessage
Structure for storing a single message.
Definition: xsmessage.h:202
ProxyCommunicator::m_channelLatency
uint32_t m_channelLatency
Definition: proxycommunicator.h:111
SerialCommunicator::m_activePortInfo
XsPortInfo m_activePortInfo
The information about the port this communicator is currently connected to.
Definition: serialcommunicator.h:124
ProxyCommunicator::ProxyCommunicator
ProxyCommunicator(int channelId, uint32_t channelLatency)
Constructor.
Definition: proxycommunicator.cpp:85
CallbackManagerXda::onTransmissionRequest
void onTransmissionRequest(int channelId, const XsByteArray *data) override
Definition: callbackmanagerxda.cpp:624
ProxyCommunicator::readDataToBuffer
XsResultValue readDataToBuffer(XsByteArray &raw) override
Has no effect for the ProxyCommunicator.
Definition: proxycommunicator.cpp:206
ProxyCommunicator::handleReceivedData
void handleReceivedData(const XsByteArray &data)
Handles data received from the communication channel.
Definition: proxycommunicator.cpp:193
SerialCommunicator::prepareForDestruction
virtual void prepareForDestruction() override
Prepares for a destruction.
Definition: serialcommunicator.cpp:101
callbackmanagerxda.h
DeviceCommunicator::getDeviceId
XsResultValue getDeviceId() override
Request a device to get device ID.
Definition: devicecommunicator.cpp:168
ProxyCommunicator::~ProxyCommunicator
~ProxyCommunicator() override
Destructor.
Definition: proxycommunicator.cpp:93
Communicator::setLastResult
void setLastResult(XsResultValue lastResult, XsString const &text=XsString()) const
Sets the last result.
Definition: communicator.cpp:238
ProxyCommunicator::flushPort
void flushPort() override
Flushes all remaining data on the open port. Has no effect for the ProxyCommunicator.
Definition: proxycommunicator.cpp:99
DataParser::addRawData
void addRawData(const XsByteArray &arr)
Adds the raw data to an array.
Definition: dataparser.cpp:95
deviceredetector.h
ProxyCommunicator::m_channelId
int m_channelId
Definition: proxycommunicator.h:110


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