Debian Codegraph @

3rdparty/xspublic/xstypes/xsportinfo.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 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 #ifndef XSPORTINFO_H
34 #define XSPORTINFO_H
35
36 #include "xstypesconfig.h"
37 #include "pstdint.h"
38 #include "xsdeviceid.h"
39 #include "xsbaud.h"
40 #include "xsstring.h"
41 #include <stdio.h>
42
43 #define XSENS_VENDOR_ID 0x2639
44 #define FTDI_VENDOR_ID 0x0403 // needed for Xsens USB-serial converters
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #ifndef __cplusplus
51 typedef struct XsPortInfo XsPortInfo;
52 #else
53 struct XsPortInfo;
54 #endif
55
56 XSTYPES_DLL_API void XsPortInfo_clear(XsPortInfo* thisPtr);
57 XSTYPES_DLL_API int XsPortInfo_empty(XsPortInfo const* thisPtr);
58 XSTYPES_DLL_API int XsPortInfo_portNumber(XsPortInfo const* thisPtr);
59 XSTYPES_DLL_API int XsPortInfo_isUsb(XsPortInfo const* thisPtr);
60 XSTYPES_DLL_API int XsPortInfo_isNetwork(XsPortInfo const* thisPtr);
61 XSTYPES_DLL_API const char* XsPortInfo_networkServiceName(XsPortInfo const* thisPtr);
62 XSTYPES_DLL_API int XsPortInfo_usbBus(XsPortInfo const* thisPtr);
63 XSTYPES_DLL_API int XsPortInfo_usbAddress(XsPortInfo const* thisPtr);
64 XSTYPES_DLL_API void XsPortInfo_swap(XsPortInfo* a, struct XsPortInfo* b);
65
66 #ifdef __cplusplus
67 }
68 #endif
69
70 #if defined(_MSC_VER)
71 #pragma warning(push)
72 #pragma warning(disable : 4996)
73 #endif
74
75 typedef enum XsPortLinesOptions
76 {
77 XPLO_Invalid = 0,
78
79 XPLO_RTS_Set = 1,
80 XPLO_RTS_Clear = (1 << 1),
81 XPLO_RTS_Ignore = (1 << 2),
82
83 XPLO_DTR_Set = (1 << 3),
84 XPLO_DTR_Clear = (1 << 4),
85 XPLO_DTR_Ignore = (1 << 5),
86
87 XPLO_All_Set = XPLO_RTS_Set | XPLO_DTR_Set,
88 XPLO_All_Clear = XPLO_RTS_Clear | XPLO_DTR_Clear,
89 XPLO_All_Ignore = XPLO_RTS_Ignore | XPLO_DTR_Ignore,
90
91 XPLO_RtsCtsFlowControl = (1 << 6)
92 } XsPortLinesOptions;
93
94 struct XsPortInfo {
95 #ifdef __cplusplus
96 /*! \brief Default constructor, creates an empty port info object */
97 XsPortInfo()
98 : m_deviceId(0)
99 , m_baudrate(XBR_Invalid)
100 , m_linesOptions(XPLO_All_Ignore)
101 , m_vid(0)
102 , m_pid(0)
103 {
104 m_portName[0] = '\0';
105 }
106
107 /*! \brief Named constructor, initializes the object to the supplied \a portname and optional \a baudRate
108 \param portname The name of the port, maximum 255 characters
109 \param baudRate The baud rate to configure for the port, for scanning XBR_Invalid may be used to scan all known baud rates
110 \param linesOptions The options for the hardware flow control lines
111 */
112 explicit XsPortInfo(const XsString& portname, XsBaudRate baudRate = XBR_Invalid, XsPortLinesOptions linesOptions = XPLO_All_Ignore)
113 : m_deviceId(0)
114 , m_baudrate(baudRate)
115 , m_linesOptions(linesOptions)
116 , m_vid(0)
117 , m_pid(0)
118 {
119 if (portname.size() < 255)
120 strcpy(m_portName, portname.c_str());
121 else
122 m_portName[0] = '\0';
123 }
124
125 #ifndef XSENS_NO_PORT_NUMBERS
126 /*! \brief Port number constructor, initializes the port to have COM<portNumber> as its name and the optional \a baudRate for a baud rate
127 \param portNr The number of the COM port
128 \param baudRate The baud rate to configure for the port, for scanning XBR_Invalid may be used to scan all known baud rates
129 \param linesOptions The options for the hardware flow control lines
130 \note Numbered COM ports are only available on Windows platforms.
131 */
132 explicit XsPortInfo(int portNr, XsBaudRate baudRate = XBR_Invalid, XsPortLinesOptions linesOptions = XPLO_All_Ignore)
133 : m_deviceId(0)
134 , m_baudrate(baudRate)
135 , m_linesOptions(linesOptions)
136 , m_vid(0)
137 , m_pid(0)
138 {
139 sprintf(m_portName, "COM%d", portNr);
140 }
141 #endif
142
143 /*! \brief \copybrief XsPortInfo_clear */
144 inline void clear()
145 {
146 XsPortInfo_clear(this);
147 }
148
149 /*! \brief \copybrief XsPortInfo_empty */
150 inline bool empty() const
151 {
152 return XsPortInfo_empty(this) != 0;
153 }
154
155 /*! \brief greater than operator, used for sorting the list. */
156 inline bool operator > (const XsPortInfo& p) const { return strcmp(m_portName, p.m_portName) > 0; }
157
158 /*! \brief less than operator, used for sorting the list. */
159 inline bool operator < (const XsPortInfo& p) const { return strcmp(m_portName, p.m_portName) < 0; }
160
161 /*! \brief equality operator, used for finding items in a list. */
162 inline bool operator == (const XsPortInfo& p) const { return strcmp(m_portName, p.m_portName) == 0; }
163
164 /*! \brief equality operator, used for finding items in a list. */
165 inline bool operator == (const char *port) const { return strcmp(m_portName, port) == 0; }
166
167 /*! \copydoc XsPortInfo_portNumber */
168 inline int portNumber() const
169 {
170 return XsPortInfo_portNumber(this);
171 }
172
173 /*! \brief The port name
174 */
175 inline XsString portName() const
176 {
177 return XsString(m_portName);
178 }
179
180 /*! \brief Set the port name
181 */
182 inline void setPortName(const XsString& portName_)
183 {
184 strncpy(m_portName, portName_.c_str(), 256);
185 }
186
187 /*! \brief \copybrief XsPortInfo_isUsb */
188 inline bool isUsb() const
189 {
190 return XsPortInfo_isUsb(this) != 0;
191 }
192
193 /*! \brief \copybrief XsPortInfo_isNetwork */
194 inline bool isNetwork() const
195 {
196 return XsPortInfo_isNetwork(this) != 0;
197 }
198
199 /*! \brief \copybrief XsPortInfo_networkServiceName */
200 inline XsString networkServiceName() const
201 {
202 return XsString(XsPortInfo_networkServiceName(this));
203 }
204
205 /*! \copydoc XsPortInfo_usbBus */
206 inline int usbBus() const
207 {
208 return XsPortInfo_usbBus(this);
209 }
210
211 /*! \copydoc XsPortInfo_usbAddress */
212 inline int usbAddress() const
213 {
214 return XsPortInfo_usbAddress(this);
215 }
216
217 /*! \brief The baudrate
218 */
219 inline XsBaudRate baudrate() const
220 {
221 return m_baudrate;
222 }
223
224 /*! \brief Set the baudrate
225 */
226 inline void setBaudrate(XsBaudRate baudrate_)
227 {
228 m_baudrate = baudrate_;
229 }
230
231 /*! \brief The device ID
232 */
233 inline XsDeviceId deviceId() const
234 {
235 return m_deviceId;
236 }
237
238 /*! \brief Set the device ID
239 */
240 inline void setDeviceId(XsDeviceId deviceId_)
241 {
242 m_deviceId = deviceId_;
243 }
244
245 /*! \brief The options for the hw flow control lines
246 */
247 inline XsPortLinesOptions linesOptions() const
248 {
249 return m_linesOptions;
250 }
251
252 /*! \brief Set the options for the hw flow control lines
253 */
254 inline void setLinesOptions(XsPortLinesOptions linesOptions)
255 {
256 m_linesOptions = linesOptions;
257 }
258
259 /*! \brief Get the vid and pid of this portinfo
260 */
261 inline void getVidPid(uint16_t& vid, uint16_t& pid) const
262 {
263 vid = m_vid;
264 pid = m_pid;
265 }
266
267 /*! \brief Set the vid and pid
268 */
269 inline void setVidPid(uint16_t vid, uint16_t pid)
270 {
271 m_vid = vid;
272 m_pid = pid;
273 }
274
275 private:
276 #endif
277
278 XsDeviceId m_deviceId; //!< The device Id of main Xsens device detected on the port
279 char m_portName[256]; //!< The port name
280 XsBaudRate m_baudrate; //!< The baudrate at which an Xsens device was detected, may be XBR_Invalid for pure USB ports
281 XsPortLinesOptions m_linesOptions; //!< The hardware flow control lines options for the port
282 uint16_t m_vid, m_pid; //!< The USB Vendor Id and Hardware Id of this connection, when available
283 };
284
285 #if defined(_MSC_VER)
286 #pragma warning(pop)
287 #endif
288
289 #if defined(__cplusplus) && !defined(XSENS_NO_STL)
290 #include <ostream>
291 namespace std
292 {
293 template<typename _CharT, typename _Traits>
294 basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& o, XsPortInfo const& xpi)
295 {
296 if (xpi.isUsb())
297 o << "usb ";
298 o << "port " << xpi.portName();
299 if (xpi.baudrate() != XBR_Invalid)
300 o << " at " << xpi.baudrate() << " bps";
301 if (xpi.deviceId() != 0)
302 o << " (" << xpi.deviceId() << ")";
303 return o;
304 }
305 }
306
307 #endif
308
309
310 #endif