deviceredetector.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 "xscontrollerconfig.h"
66 #include <xstypes/xsdeviceid.h>
68 #include <xstypes/xstime.h>
69 #include <xstypes/xsdid.h>
70 #include "xsscanner.h"
71 //#include "networkcommunicator.h"
72 #include "deviceredetector.h"
73 
80 {
81 #ifdef XSENS_WINDOWS
82  if (portInfo.isUsb())
83 #else
84  (void)portInfo;
85  if (1)
86 #endif
87  {
98  }
99  else
100  {
111  }
112 }
113 
120 bool DeviceRedetector::redetect(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
121 {
122  FunctionPointer currentFunction;
123  currentFunction = m_detectFunctions[deviceId.deviceType(false)];
124 
125  if (currentFunction)
126  return (this->*currentFunction)(deviceId, portInfo, skipDeviceIdCheck);
127  else
128  {
129  JLDEBUGG("No functions defined for device. Defaulting to OneComPort" << deviceId);
130  return redetectOneComPort(deviceId, portInfo, skipDeviceIdCheck);
131  }
132 }
133 
140 bool DeviceRedetector::redetectNoScan(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
141 {
142  (void)deviceId;
143  (void)portInfo;
144  (void)skipDeviceIdCheck;
145  return true;
146 }
147 
155 bool DeviceRedetector::redetectScanPorts(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
156 {
157  JLDEBUGG("Redetecting device " << deviceId);
158 
159  bool found = false;
160  int count = 0;
161  while (!found && count < 25)
162  {
163  XsTime::msleep(100);
164  XsPortInfoArray portInfoArray = XsScanner::scanPorts(XBR_Invalid, 100, false, true);
165 
166  JLDEBUGG("Attempt " << count << ". Detected " << portInfoArray.size() << " devices.");
167 
168  for (uint32_t i = 0; i < portInfoArray.size(); i++)
169  {
170  portInfo = portInfoArray[i];
171  if ((skipDeviceIdCheck && !portInfo.empty()) || portInfo.deviceId() == deviceId)
172  {
173  portInfo = portInfoArray[i];
174  JLDEBUGG("Found device: " << portInfo);
175  found = true;
176  break;
177  }
178  }
179  XsTime::msleep(1000);
180  count++;
181  }
182  JLDEBUGG("Redetecting device " << deviceId << " . Result: " << found << " after " << count << " tries");
183  return found;
184 }
185 
192 bool DeviceRedetector::redetectEnumerateSerialPorts(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
193 {
194  JLDEBUGG("Redetecting device " << deviceId);
195 
196  bool found = false;
197  int count = 0;
198  while (!found && count < 20)
199  {
200  XsTime::msleep(100);
201  XsPortInfoArray portInfoArray = XsScanner::enumerateSerialPorts();
202 
203  JLDEBUGG("Attempt " << count << ". Detected " << portInfoArray.size() << " devices.");
204 
205  for (uint32_t i = 0; i < portInfoArray.size(); i++)
206  {
207  JLDEBUGG(portInfoArray[i]);
208  if ((skipDeviceIdCheck && !portInfoArray[i].empty()) || portInfoArray[i].deviceId() == deviceId)
209  {
210  XsTime::msleep(3000);
211  portInfo = portInfoArray[i];
212  found = true;
213  break;
214  }
215  }
216  count++;
217  }
218  JLDEBUGG("Redetecting device " << deviceId << " . Result: " << found << " after " << count << " tries");
219  return found;
220 }
221 
228 bool DeviceRedetector::redetectOneComPort(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
229 {
230  JLDEBUGG("Redetecting device " << deviceId);
231 
232  bool found = false;
233  int count = 0;
234 
235  XsStringArray portList;
236  portList.push_back(portInfo.portName());
237 
238  XsIntArray portLinesOptions;
239  portLinesOptions.push_back((int)portInfo.linesOptions());
240 
241  while (!found && count < 20)
242  {
243  XsTime::msleep(100);
244 
245  XsPortInfoArray detectedPorts = XsScanner::scanComPortList(portList, portLinesOptions, XBR_Invalid, 100);
246 
247  assert(detectedPorts.size() <= 1); // at most one port found
248 
249  JLDEBUGG("Attempt " << count << ".");
250 
251  if (!detectedPorts.empty())
252  {
253  const XsPortInfo portInfoDetected = detectedPorts[0];
254 
255  JLDEBUGG(portInfoDetected);
256 
257  if ((skipDeviceIdCheck && !portInfoDetected.empty()) || portInfoDetected.deviceId() == deviceId)
258  {
259  uint16_t vid, pid;
260  portInfo.getVidPid(vid, pid);
261  portInfo = portInfoDetected;
262  portInfo.setVidPid(vid, pid);
263  found = true;
264  break;
265  }
266  }
267  count++;
268  }
269 
270  JLDEBUGG("Redetecting device " << deviceId << " . Result: " << found << " after " << count << " tries");
271  JLDEBUGG("PortInfo " << portInfo);
272  return found;
273 }
274 
281 bool DeviceRedetector::redetectEnumerateNetworkDevices(const XsDeviceId& deviceId, XsPortInfo& portInfo, bool skipDeviceIdCheck)
282 {
283  (void)deviceId;
284  (void)portInfo;
285  (void)skipDeviceIdCheck;
286  return false;
287 }
DeviceRedetector::redetectScanPorts
virtual bool redetectScanPorts(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
Scan for devices until the requested device is found.
Definition: deviceredetector.cpp:155
XS_DID_TYPEH_MT_X00
#define XS_DID_TYPEH_MT_X00
Definition: xsdid.h:87
XsIntArray
A list of XsInt values.
XsStringArray
A list of XsString values.
DeviceRedetector::m_detectFunctions
std::map< XsDeviceId, FunctionPointer > m_detectFunctions
A map filled with detect functions.
Definition: deviceredetector.h:93
DeviceRedetector::DeviceRedetector
DeviceRedetector(const XsPortInfo &portInfo)
Construct an Device Redetector object.
Definition: deviceredetector.cpp:79
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
XS_DID64_BIT
#define XS_DID64_BIT
Definition: xsdid.h:216
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
xsdid.h
XsDeviceId
struct XsDeviceId XsDeviceId
Definition: xsdeviceid.h:912
xsdeviceid.h
DeviceRedetector::FunctionPointer
bool(DeviceRedetector::* FunctionPointer)(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
A typedef for a function pointer.
Definition: deviceredetector.h:90
XsPortInfoArray
A list of XsPortInfo values.
XS_DID_TYPEH_MT_X0
#define XS_DID_TYPEH_MT_X0
Definition: xsdid.h:86
DeviceRedetector::redetectEnumerateSerialPorts
virtual bool redetectEnumerateSerialPorts(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
Redectets and enumerates the serial ports for a given device.
Definition: deviceredetector.cpp:192
XsDeviceId
Contains an Xsens device ID and provides operations for determining the type of device.
Definition: xsdeviceid.h:192
DeviceRedetector::redetectOneComPort
virtual bool redetectOneComPort(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
Redectes a device on one com port.
Definition: deviceredetector.cpp:228
JLDEBUGG
#define JLDEBUGG(msg)
Definition: journaller.h:280
xstime.h
DeviceRedetector::redetectNoScan
virtual bool redetectNoScan(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
Definition: deviceredetector.cpp:140
DeviceRedetector::redetectEnumerateNetworkDevices
virtual bool redetectEnumerateNetworkDevices(const XsDeviceId &deviceId, XsPortInfo &portInfo, bool skipDeviceIdCheck)
Definition: deviceredetector.cpp:281
XS_DID_TYPEH_MT_3X0
#define XS_DID_TYPEH_MT_3X0
Definition: xsdid.h:90
XS_DID_TYPEH_MT_X_MPU
#define XS_DID_TYPEH_MT_X_MPU
Definition: xsdid.h:91
xsscanner.h
XBR_Invalid
XBR_Invalid
Not a valid baud rate.
Definition: xsbaudrate.h:126
xsportinfoarray.h
deviceredetector.h
xscontrollerconfig.h


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