devicefactory.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 "devicefactory.h"
66 
67 #include <xstypes/xsdeviceid.h>
68 #include "xsdevice_def.h"
69 #include "communicator.h"
70 #include "devicetypes.h"
71 #include "mtixdevice.h"
72 #include "mtix0device.h"
73 #include "mtix00device.h"
74 #include "mtigdevice.h"
75 #include "mti7_mti8device.h"
76 #include "mti3x0device.h"
77 #include "mti6x0device.h"
78 #include "mti8x0device.h"
79 #include "dotdevice.h"
80 
88 {
89 }
90 
98 bool DeviceFactory::registerMasterDeviceType(DeviceTypeId deviceTypeId, MasterConstructFunc constructFunc)
99 {
100  bool rv = m_masterConstructors.insert(std::make_pair(deviceTypeId, constructFunc)).second;
101  // it should be a master XOR a standalone (or neither)
102  assert(m_standaloneConstructors.find(deviceTypeId) == m_standaloneConstructors.end());
103  return rv;
104 }
105 
113 bool DeviceFactory::registerStandaloneDeviceType(DeviceTypeId deviceTypeId, StandaloneConstructFunc constructFunc)
114 {
115  m_standaloneConstructors[deviceTypeId] = constructFunc;
116  // it should be a master XOR a standalone (or neither)
117  assert(m_masterConstructors.find(deviceTypeId) == m_masterConstructors.end());
118  return true;
119 }
120 
128 {
129  XsDevice* device = nullptr;
130  if (deviceTypeId != DeviceType::INVALID)
131  {
132  auto i = m_masterConstructors.find(deviceTypeId);
133  if (i != m_masterConstructors.end())
134  {
135  device = (i->second)(*this, comm);
136  device->addRef();
137  }
138  else
139  {
140  auto j = m_standaloneConstructors.find(deviceTypeId);
141  if (j != m_standaloneConstructors.end())
142  {
143  device = (j->second)(comm);
144  device->addRef();
145  }
146  }
147  }
148  return device;
149 }
150 
160 XsDevice* DeviceFactory::createMasterDevice(Communicator* communicator, bool doInitialize)
161 {
162  if (communicator != nullptr)
163  {
164  DeviceTypeId deviceTypeId = deviceToTypeId(communicator->masterDeviceId());
165  XsDevice* constructedDevice = constructDevice(deviceTypeId, communicator);
166 
167  if (!doInitialize || initializeDevice(constructedDevice))
168  {
169 #ifndef XSENS_DEBUG
170  // this is support debug info, we don't want to spam the debugger windows
171  JLWRITEG("Created master device with id: " << constructedDevice->deviceId() << " and firmware version: " << constructedDevice->firmwareVersion().toString());
172 #endif
173  return constructedDevice;
174  }
175 
176  // construction failed
177  if (constructedDevice)
178  constructedDevice->removeRef();
179  else
180  communicator->destroy();
181  }
182  return nullptr;
183 }
184 
190 {
191  if (deviceId.isMti() || deviceId.isMtig())
192  {
193  if (deviceId.isMtig())
194  return DeviceType::MTIG;
195  if (deviceId.isMtiX00())
196  return DeviceType::MTI_X00;
197  if (deviceId.isMtiX0())
198  return DeviceType::MTI_X0;
199  if (deviceId.isMtiX() && deviceId.isGnss())
200  return DeviceType::MTI_7_MTI_8;//this is used for both the MTi-7 and MTi-8
201  if (deviceId.isMtiX())
202  return DeviceType::MTI_X;
203  if (deviceId.isMti3X0())
204  return DeviceType::MTI_3X0;
205  if (deviceId.isMti6X0())
206  return DeviceType::MTI_6X0;
207  if (deviceId.isMti8X0())
208  return DeviceType::MTI_8X0;
209  if (deviceId.isDot())
210  return DeviceType::DOT;
211  }
212 
213  return DeviceType::INVALID;
214 }
215 
219 {
220  (void) deviceId;
221 }
222 
229 {
230  if (dev && dev->initialize())
231  return true;
232  return false;
233 }
234 
240 {
241  return device.isInitialized() ? true : initializeDevice(&device);
242 }
243 
247 {
257 }
DeviceType::MTI_X0
static const DeviceFactory::DeviceTypeId MTI_X0
Definition: devicetypes.h:78
XsDevice::isInitialized
bool isInitialized() const
Returns true when the device is initialized.
Definition: xsdevice_def.cpp:3721
DeviceType::DOT
static const DeviceFactory::DeviceTypeId DOT
Definition: devicetypes.h:93
mti8x0device.h
XsDevice::deviceId
XsDeviceId const & deviceId() const
Return the device ID of the device.
Definition: xsdevice_def.cpp:742
DeviceType::MTI_X00
static const DeviceFactory::DeviceTypeId MTI_X00
Definition: devicetypes.h:79
DeviceType::MTI_3X0
static const DeviceFactory::DeviceTypeId MTI_3X0
Definition: devicetypes.h:84
DeviceType::MTI_7_MTI_8
static const DeviceFactory::DeviceTypeId MTI_7_MTI_8
Definition: devicetypes.h:81
DeviceType::MTIG
static const DeviceFactory::DeviceTypeId MTIG
Definition: devicetypes.h:80
XsDevice::removeRef
virtual void removeRef()
Decrease this XsDevices reference counter with 1.
Definition: xsdevice_def.cpp:3831
mti7_mti8device.h
DeviceType::MTI_6X0
static const DeviceFactory::DeviceTypeId MTI_6X0
Definition: devicetypes.h:82
mti6x0device.h
XsDevice::initialize
virtual bool XSNOEXPORT initialize()
Communicator::masterDeviceId
XsDeviceId masterDeviceId() const
Definition: communicator.cpp:157
DeviceFactory::removeExistingDevice
virtual void removeExistingDevice(XsDeviceId const &deviceId)
Tell our device manager to remove any devices matching deviceId.
Definition: devicefactory.cpp:218
DeviceFactory::DeviceTypeId
unsigned int DeviceTypeId
Provides the device type ID as unsigned int.
Definition: devicefactory.h:82
MtiXDevice::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mtixdevice.h:80
mti3x0device.h
DeviceFactory::deviceToTypeId
virtual DeviceTypeId deviceToTypeId(XsDeviceId const &deviceId) const
converts an XsDeviceId to an DeviceTypeId
Definition: devicefactory.cpp:189
MtiX00Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mtix00device.h:77
DeviceFactory::registerStandaloneDeviceType
bool registerStandaloneDeviceType(DeviceTypeId deviceTypeId, StandaloneConstructFunc constructFunc)
Registers a standalone device type. After registration, the factory is able to cerate an instance of ...
Definition: devicefactory.cpp:113
devicetypes.h
Communicator
A base struct for a communication interface.
Definition: communicator.h:95
DeviceType::MTI_X
static const DeviceFactory::DeviceTypeId MTI_X
Definition: devicetypes.h:77
XsDevice::addRef
virtual void addRef()
Increase reference count of XsDevice pointer XsDevice pointers stay alive while reference counter is ...
Definition: xsdevice_def.cpp:3810
XsDevice::firmwareVersion
virtual XsVersion firmwareVersion() const
Return the firmware version.
Definition: xsdevice_def.cpp:675
DeviceFactory::createMasterDevice
virtual XsDevice * createMasterDevice(Communicator *communicator, bool doInitialize=true)
Creates and initializes a master device with a specified communicator. The type of the new device is ...
Definition: devicefactory.cpp:160
DotDevice::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: dotdevice.h:80
DeviceType::INVALID
static const DeviceFactory::DeviceTypeId INVALID
Definition: devicefactory.h:114
MtigDevice::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mtigdevice.h:77
dotdevice.h
xsdevice_def.h
devicefactory.h
DeviceFactory::constructDevice
virtual XsDevice * constructDevice(DeviceTypeId deviceTypeId, Communicator *comm)
Constructs a master device of a specified deviceTypeId with a specified communicator....
Definition: devicefactory.cpp:127
mtixdevice.h
xsdeviceid.h
XsDeviceId
Contains an Xsens device ID and provides operations for determining the type of device.
Definition: xsdeviceid.h:192
DeviceFactory::m_standaloneConstructors
std::map< DeviceTypeId, StandaloneConstructFunc > m_standaloneConstructors
Definition: devicefactory.h:109
Mti3X0Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mti3x0device.h:80
mtix00device.h
DeviceFactory::m_masterConstructors
std::map< DeviceTypeId, MasterConstructFunc > m_masterConstructors
Definition: devicefactory.h:108
JLWRITEG
#define JLWRITEG(msg)
Definition: journaller.h:284
mtix0device.h
DeviceFactory::DeviceFactory
DeviceFactory()
Default constructor.
Definition: devicefactory.cpp:87
MtiX0Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mtix0device.h:77
MTi7_MTi8Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mti7_mti8device.h:76
DeviceFactory::registerMasterDeviceType
bool registerMasterDeviceType(DeviceTypeId deviceTypeId, MasterConstructFunc constructFunc)
Registers a master device type. After registration, the factory is able to cerate an instance of the ...
Definition: devicefactory.cpp:98
Mti8X0Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mti8x0device.h:77
communicator.h
Communicator::destroy
void destroy()
Destroys the communicator.
Definition: communicator.cpp:344
DeviceFactory::registerDevices
virtual void registerDevices()
register all known device types
Definition: devicefactory.cpp:246
DeviceFactory::initializeDevice
bool initializeDevice(XsDevice &device) const
Initializes a device (if not already initialized)
Definition: devicefactory.cpp:239
DeviceType::MTI_8X0
static const DeviceFactory::DeviceTypeId MTI_8X0
Definition: devicetypes.h:83
Mti6X0Device::constructStandalone
static XsDevice * constructStandalone(Communicator *comm)
Constructs a standalone device using a provided communicator.
Definition: mti6x0device.h:77
XsDevice
Definition: xsdevice_def.h:164
mtigdevice.h


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