xslibusb.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 "xslibusb.h"
67 
68 #ifdef HAVE_LIBUSB
69 
73 XsLibUsb::XsLibUsb(void)
74 {
75  m_libraryLoader = new XsLibraryLoader();
76  initLibrary();
77 }
78 
79 XsLibUsb::~XsLibUsb(void)
80 {
81  delete m_libraryLoader;
82 }
83 
84 void XsLibUsb::tryLoadLibrary()
85 {
86  if (m_libraryLoader->isLoaded())
87  return;
88  if (m_libraryLoader->load("libusb-1.0.so"))
89  return;
90  if (m_libraryLoader->load("libusb1.0.so"))
91  return;
92 }
93 
94 void XsLibUsb::initLibrary()
95 {
96  tryLoadLibrary();
97 
98  memset(&m_libUsb, 0, sizeof(m_libUsb));
99 
100  if (m_libraryLoader->isLoaded())
101  {
102  m_libUsb.init = (libUSB_init*)m_libraryLoader->resolve("libusb_init");
103  m_libUsb.exit = (libUSB_exit*)m_libraryLoader->resolve("libusb_exit");
104  m_libUsb.open = (libUSB_open*)m_libraryLoader->resolve("libusb_open");
105  m_libUsb.close = (libUSB_close*)m_libraryLoader->resolve("libusb_close");
106  m_libUsb.kernel_driver_active = (libUSB_kernel_driver_active*)m_libraryLoader->resolve("libusb_kernel_driver_active");
107  m_libUsb.attach_kernel_driver = (libUSB_attach_kernel_driver*)m_libraryLoader->resolve("libusb_attach_kernel_driver");
108  m_libUsb.detach_kernel_driver = (libUSB_detach_kernel_driver*)m_libraryLoader->resolve("libusb_detach_kernel_driver");
109  m_libUsb.ref_device = (libUSB_ref_device*)m_libraryLoader->resolve("libusb_ref_device");
110  m_libUsb.unref_device = (libUSB_unref_device*)m_libraryLoader->resolve("libusb_unref_device");
111  m_libUsb.claim_interface = (libUSB_claim_interface*)m_libraryLoader->resolve("libusb_claim_interface");
112  m_libUsb.release_interface = (libUSB_release_interface*)m_libraryLoader->resolve("libusb_release_interface");
113  m_libUsb.get_active_config_descriptor = (libUSB_get_active_config_descriptor*)m_libraryLoader->resolve("libusb_get_active_config_descriptor");
114  m_libUsb.free_config_descriptor = (libUSB_free_config_descriptor*)m_libraryLoader->resolve("libusb_free_config_descriptor");
115  m_libUsb.get_bus_number = (libUSB_get_bus_number*)m_libraryLoader->resolve("libusb_get_bus_number");
116  m_libUsb.get_device = (libUSB_get_device*)m_libraryLoader->resolve("libusb_get_device");
117  m_libUsb.get_device_address = (libUSB_get_device_address*)m_libraryLoader->resolve("libusb_get_device_address");
118  m_libUsb.get_device_descriptor = (libUSB_get_device_descriptor*)m_libraryLoader->resolve("libusb_get_device_descriptor");
119  m_libUsb.get_device_list = (libUSB_get_device_list*)m_libraryLoader->resolve("libusb_get_device_list");
120  m_libUsb.free_device_list = (libUSB_free_device_list*)m_libraryLoader->resolve("libusb_free_device_list");
121  m_libUsb.get_string_descriptor_ascii = (libUSB_get_string_descriptor_ascii*)m_libraryLoader->resolve("libusb_get_string_descriptor_ascii");
122  m_libUsb.bulk_transfer = (libUSB_bulk_transfer*)m_libraryLoader->resolve("libusb_bulk_transfer");
123  m_libUsb.set_debug = (libUSB_set_debug*)m_libraryLoader->resolve("libusb_set_debug");
124  }
125 }
126 
137 int XsLibUsb::init(libusb_context** ctx)
138 {
139  if (m_libUsb.init)
140  return m_libUsb.init(ctx);
141  else
142  return LIBUSB_ERROR_NOT_SUPPORTED;
143 }
144 
148 void XsLibUsb::exit(libusb_context* ctx)
149 {
150  if (m_libUsb.exit)
151  m_libUsb.exit(ctx);
152 }
153 
170 int XsLibUsb::open(libusb_device* dev, libusb_device_handle** handle)
171 {
172  if (m_libUsb.open)
173  return m_libUsb.open(dev, handle);
174  else
175  return LIBUSB_ERROR_NOT_SUPPORTED;
176 }
177 
184 void XsLibUsb::close(libusb_device_handle* dev_handle)
185 {
186  if (m_libUsb.close)
187  m_libUsb.close(dev_handle);
188 }
189 
205 int XsLibUsb::kernel_driver_active(libusb_device_handle* dev, int interface_number)
206 {
207  if (m_libUsb.kernel_driver_active)
208  return m_libUsb.kernel_driver_active(dev, interface_number);
209  else
210  return LIBUSB_ERROR_NOT_SUPPORTED;
211 }
212 
230 int XsLibUsb::attach_kernel_driver(libusb_device_handle* dev, int interface_number)
231 {
232  if (m_libUsb.attach_kernel_driver)
233  return m_libUsb.attach_kernel_driver(dev, interface_number);
234  else
235  return LIBUSB_ERROR_NOT_SUPPORTED;
236 }
237 
253 int XsLibUsb::detach_kernel_driver(libusb_device_handle* dev, int interface_number)
254 {
255  if (m_libUsb.detach_kernel_driver)
256  return m_libUsb.detach_kernel_driver(dev, interface_number);
257  else
258  return LIBUSB_ERROR_NOT_SUPPORTED;
259 }
260 
265 libusb_device* XsLibUsb::ref_device(libusb_device* dev)
266 {
267  if (m_libUsb.ref_device)
268  return m_libUsb.ref_device(dev);
269  else
270  return NULL;
271 }
272 
277 void XsLibUsb::unref_device(libusb_device* dev)
278 {
279  if (m_libUsb.unref_device)
280  m_libUsb.unref_device(dev);
281 }
282 
304 int XsLibUsb::claim_interface(libusb_device_handle* dev, int interface_number)
305 {
306  if (m_libUsb.claim_interface)
307  return m_libUsb.claim_interface(dev, interface_number);
308  else
309  return LIBUSB_ERROR_NOT_SUPPORTED;
310 }
311 
325 int XsLibUsb::release_interface(libusb_device_handle* dev, int interface_number)
326 {
327  if (m_libUsb.release_interface)
328  return m_libUsb.release_interface(dev, interface_number);
329  else
330  return LIBUSB_ERROR_NOT_SUPPORTED;
331 }
332 
346 int XsLibUsb::get_active_config_descriptor(libusb_device* dev, struct libusb_config_descriptor** config)
347 {
348  if (m_libUsb.get_active_config_descriptor)
349  return m_libUsb.get_active_config_descriptor(dev, config);
350  else
351  return LIBUSB_ERROR_NOT_SUPPORTED;
352 }
353 
360 void XsLibUsb::free_config_descriptor(struct libusb_config_descriptor* config)
361 {
362  if (m_libUsb.free_config_descriptor)
363  m_libUsb.free_config_descriptor(config);
364 }
365 
370 uint8_t XsLibUsb::get_bus_number(libusb_device* dev)
371 {
372  if (m_libUsb.get_bus_number)
373  return m_libUsb.get_bus_number(dev);
374  else
375  return 0;
376 }
377 
385 libusb_device* XsLibUsb::get_device(libusb_device_handle* dev_handle)
386 {
387  if (m_libUsb.get_device)
388  return m_libUsb.get_device(dev_handle);
389  else
390  return NULL;
391 }
392 
397 uint8_t XsLibUsb::get_device_address(libusb_device* dev)
398 {
399  if (m_libUsb.get_device_address)
400  return m_libUsb.get_device_address(dev);
401  else
402  return 0;
403 }
404 
413 int XsLibUsb::get_device_descriptor(libusb_device* dev, struct libusb_device_descriptor* desc)
414 {
415  if (m_libUsb.get_device_descriptor)
416  return m_libUsb.get_device_descriptor(dev, desc);
417  else
418  return LIBUSB_ERROR_NOT_SUPPORTED;
419 }
420 
438 ssize_t XsLibUsb::get_device_list(libusb_context* ctx, libusb_device*** list)
439 {
440  if (m_libUsb.get_device_list)
441  return m_libUsb.get_device_list(ctx, list);
442  else
443  return LIBUSB_ERROR_NOT_SUPPORTED;
444 }
445 
451 void XsLibUsb::free_device_list(libusb_device** list, int unref_devices)
452 {
453  if (m_libUsb.free_device_list)
454  m_libUsb.free_device_list(list, unref_devices);
455 }
456 
467 int XsLibUsb::get_string_descriptor_ascii(libusb_device_handle* dev, uint8_t desc_index, unsigned char* data, int length)
468 {
469  if (m_libUsb.get_string_descriptor_ascii)
470  return m_libUsb.get_string_descriptor_ascii(dev, desc_index, data, length);
471  else
472  return LIBUSB_ERROR_NOT_SUPPORTED;
473 }
474 
506 int XsLibUsb::bulk_transfer(libusb_device_handle* dev_handle, unsigned char endpoint, unsigned char* data, int length, int* actual_length, unsigned int timeout)
507 {
508  if (m_libUsb.bulk_transfer)
509  return m_libUsb.bulk_transfer(dev_handle, endpoint, data, length, actual_length, timeout);
510  else
511  return LIBUSB_ERROR_NOT_SUPPORTED;
512 }
513 
541 void XsLibUsb::set_debug(libusb_context* ctx, int level)
542 {
543  if (m_libUsb.set_debug)
544  m_libUsb.set_debug(ctx, level);
545 }
546 
547 #else
548 
550 // this is just here to prevent compiler warnings about this file being empty
551 bool haveWinUsb = false;
554 #endif // HAVE_LIBUSB
xslibraryloader.h
xslibusb.h
XsLibraryLoader
struct XsLibraryLoader XsLibraryLoader
Definition: xslibraryloader.h:78


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