USBDevice.cpp
Go to the documentation of this file.
1 #include "USBDevice.h"
2 
3 #include <sstream>
4 #include <stdexcept>
5 
6 #include <libusb-1.0/libusb.h>
7 
9  uint16_t idVendor,
10  uint16_t idProduct)
11  : m_ctx(NULL)
12  , m_handle(NULL)
13  , m_version(0.0)
14  , m_idVendor(idVendor)
15  , m_idProduct(idProduct)
16 {
17  int result = libusb_init(&m_ctx);
18  if (result != LIBUSB_SUCCESS) {
19  throw std::runtime_error(libusb_error_name(result));
20  }
21 }
22 
24 {
25  if (m_handle) {
26  // ignore the error (we don't want to throw in dtor)
27  libusb_release_interface(m_handle, 0);
28 
29  // function returns void => no error checking
30  libusb_close(m_handle);
31  }
32 
33  // function returns void => no error checking
34  libusb_exit(m_ctx);
35 }
36 
38  uint16_t idVendor,
39  uint16_t idProduct)
40 {
41  libusb_context* ctx;
42  int result = libusb_init(&ctx);
43  if (result != LIBUSB_SUCCESS) {
44  throw std::runtime_error(libusb_error_name(result));
45  }
46 
47  // discover devices
48  libusb_device **list;
49  ssize_t cnt = libusb_get_device_list(NULL, &list);
50  ssize_t i = 0;
51  uint32_t num = 0;
52  int err = 0;
53  if (cnt < 0) {
54  throw std::runtime_error("Error during get_device_list");
55  }
56  for (i = 0; i < cnt; i++) {
57  libusb_device *device = list[i];
58  libusb_device_descriptor deviceDescriptor;
59  err = libusb_get_device_descriptor(device, &deviceDescriptor);
60  if (err != LIBUSB_SUCCESS) {
61  libusb_free_device_list(list, 1);
62  throw std::runtime_error(libusb_error_name(err));
63  }
64  else if (deviceDescriptor.idVendor == idVendor &&
65  deviceDescriptor.idProduct == idProduct) {
66  ++num;
67  }
68  }
69  libusb_free_device_list(list, 1);
70 
71  // function returns void => no error checking
72  libusb_exit(ctx);
73 
74  return num;
75 }
76 
78  uint32_t devid)
79 {
80  // discover devices
81  libusb_device **list;
82  libusb_device *found = NULL;
83  ssize_t cnt = libusb_get_device_list(NULL, &list);
84  ssize_t i = 0;
85  uint32_t foundid = 0;
86  int err = 0;
87  if (cnt < 0) {
88  throw std::runtime_error("Error during get_device_list");
89  }
90  for (i = 0; i < cnt; i++) {
91  libusb_device *device = list[i];
92  libusb_device_descriptor deviceDescriptor;
93  err = libusb_get_device_descriptor(device, &deviceDescriptor);
94  if (err != LIBUSB_SUCCESS) {
95  libusb_free_device_list(list, 1);
96  throw std::runtime_error(libusb_error_name(err));
97  }
98  else if (deviceDescriptor.idVendor == m_idVendor &&
99  deviceDescriptor.idProduct == m_idProduct) {
100  if (foundid == devid) {
101  found = device;
102  break;
103  }
104  ++foundid;
105  }
106  }
107  if (found) {
108  err = libusb_open(found, &m_handle);
109  if (err != LIBUSB_SUCCESS) {
110  libusb_free_device_list(list, 1);
111  throw std::runtime_error(libusb_error_name(err));
112  }
113  libusb_device_descriptor deviceDescriptor;
114  err = libusb_get_device_descriptor(found, &deviceDescriptor);
115  if (err != LIBUSB_SUCCESS) {
116  throw std::runtime_error(libusb_error_name(err));
117  }
118  std::stringstream sstr;
119  sstr << std::hex << (deviceDescriptor.bcdDevice >> 8) << "." << (deviceDescriptor.bcdDevice & 0xFF);
120  sstr >> m_version;
121  }
122  libusb_free_device_list(list, 1);
123 
124  // configure
125  if (m_handle)
126  {
127  err = libusb_set_configuration(m_handle, 1);
128  if (err != LIBUSB_SUCCESS) {
129  throw std::runtime_error(libusb_error_name(err));
130  }
131 
132  err = libusb_claim_interface(m_handle, 0);
133  if (err != LIBUSB_SUCCESS) {
134  throw std::runtime_error(libusb_error_name(err));
135  }
136  }
137  else
138  {
139  std::stringstream sstr;
140  sstr << "No matching USB Device with devid = " << devid << " found! Do you have a Crazyradio dongle connected?";
141  throw std::runtime_error(sstr.str());
142  }
143 }
144 
146  uint8_t request,
147  uint16_t value,
148  uint16_t index,
149  const unsigned char* data,
150  uint16_t length)
151 {
152  if (!m_handle) {
153  throw std::runtime_error("No valid device handle!");
154  }
155 
156  int status = libusb_control_transfer(
157  m_handle,
158  LIBUSB_REQUEST_TYPE_VENDOR,
159  request,
160  value,
161  index,
162  (unsigned char*)data,
163  length,
164  /*timeout*/ 1000);
165  if (status != LIBUSB_SUCCESS) {
166  throw std::runtime_error(libusb_error_name(status));
167  }
168 }
virtual ~USBDevice()
Definition: USBDevice.cpp:23
static uint32_t numDevices(uint16_t idVendor, uint16_t idProduct)
Definition: USBDevice.cpp:37
const T value
Definition: crtp.h:26
void sendVendorSetup(uint8_t request, uint16_t value, uint16_t index, const unsigned char *data, uint16_t length)
Definition: USBDevice.cpp:145
uint8_t length
Definition: crtp.h:22
libusb_context * m_ctx
Definition: USBDevice.h:33
uint8_t data[29]
Definition: crtp.h:363
uint8_t result
Definition: crtp.h:440
void open(uint32_t devid)
Definition: USBDevice.cpp:77
crtpParamTocGetItemRequest request
Definition: crtp.h:21
uint16_t m_idVendor
Definition: USBDevice.h:39
uint16_t m_idProduct
Definition: USBDevice.h:40
USBDevice(uint16_t idVendor, uint16_t idProduct)
Definition: USBDevice.cpp:8
float m_version
Definition: USBDevice.h:36
uint8_t status
Definition: crtp.h:440
libusb_device_handle * m_handle
Definition: USBDevice.h:34


crazyflie_cpp
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:10