USBDevice.cpp
Go to the documentation of this file.
00001 #include "USBDevice.h"
00002 
00003 #include <sstream>
00004 #include <stdexcept>
00005 
00006 #include <libusb-1.0/libusb.h>
00007 
00008 USBDevice::USBDevice(
00009     uint16_t idVendor,
00010     uint16_t idProduct)
00011     : m_ctx(NULL)
00012     , m_handle(NULL)
00013     , m_version(0.0)
00014     , m_idVendor(idVendor)
00015     , m_idProduct(idProduct)
00016 {
00017     int result = libusb_init(&m_ctx);
00018     if (result != LIBUSB_SUCCESS) {
00019         throw std::runtime_error(libusb_error_name(result));
00020     }
00021 }
00022 
00023 USBDevice::~USBDevice()
00024 {
00025     if (m_handle) {
00026         // ignore the error (we don't want to throw in dtor)
00027         libusb_release_interface(m_handle, 0);
00028 
00029         // function returns void => no error checking
00030         libusb_close(m_handle);
00031     }
00032 
00033     // function returns void => no error checking
00034     libusb_exit(m_ctx);
00035 }
00036 
00037 uint32_t USBDevice::numDevices(
00038   uint16_t idVendor,
00039   uint16_t idProduct)
00040 {
00041     libusb_context* ctx;
00042     int result = libusb_init(&ctx);
00043     if (result != LIBUSB_SUCCESS) {
00044         throw std::runtime_error(libusb_error_name(result));
00045     }
00046 
00047     // discover devices
00048     libusb_device **list;
00049     ssize_t cnt = libusb_get_device_list(NULL, &list);
00050     ssize_t i = 0;
00051     uint32_t num = 0;
00052     int err = 0;
00053     if (cnt < 0) {
00054         throw std::runtime_error("Error during get_device_list");
00055     }
00056     for (i = 0; i < cnt; i++) {
00057         libusb_device *device = list[i];
00058         libusb_device_descriptor deviceDescriptor;
00059         err = libusb_get_device_descriptor(device, &deviceDescriptor);
00060         if (err != LIBUSB_SUCCESS) {
00061             libusb_free_device_list(list, 1);
00062             throw std::runtime_error(libusb_error_name(err));
00063         }
00064         else if (deviceDescriptor.idVendor == idVendor &&
00065                  deviceDescriptor.idProduct == idProduct) {
00066             ++num;
00067         }
00068     }
00069     libusb_free_device_list(list, 1);
00070 
00071     // function returns void => no error checking
00072     libusb_exit(ctx);
00073 
00074     return num;
00075 }
00076 
00077 void USBDevice::open(
00078     uint32_t devid)
00079 {
00080     // discover devices
00081     libusb_device **list;
00082     libusb_device *found = NULL;
00083     ssize_t cnt = libusb_get_device_list(NULL, &list);
00084     ssize_t i = 0;
00085     uint32_t foundid = 0;
00086     int err = 0;
00087     if (cnt < 0) {
00088         throw std::runtime_error("Error during get_device_list");
00089     }
00090     for (i = 0; i < cnt; i++) {
00091         libusb_device *device = list[i];
00092         libusb_device_descriptor deviceDescriptor;
00093         err = libusb_get_device_descriptor(device, &deviceDescriptor);
00094         if (err != LIBUSB_SUCCESS) {
00095             libusb_free_device_list(list, 1);
00096             throw std::runtime_error(libusb_error_name(err));
00097         }
00098         else if (deviceDescriptor.idVendor == m_idVendor &&
00099                  deviceDescriptor.idProduct == m_idProduct) {
00100             if (foundid == devid) {
00101                 found = device;
00102                 break;
00103             }
00104             ++foundid;
00105         }
00106     }
00107     if (found) {
00108         err = libusb_open(found, &m_handle);
00109         if (err != LIBUSB_SUCCESS) {
00110             libusb_free_device_list(list, 1);
00111             throw std::runtime_error(libusb_error_name(err));
00112         }
00113         libusb_device_descriptor deviceDescriptor;
00114         err = libusb_get_device_descriptor(found, &deviceDescriptor);
00115         if (err != LIBUSB_SUCCESS) {
00116             throw std::runtime_error(libusb_error_name(err));
00117         }
00118         std::stringstream sstr;
00119         sstr << std::hex << (deviceDescriptor.bcdDevice >> 8) << "." << (deviceDescriptor.bcdDevice & 0xFF);
00120         sstr >> m_version;
00121     }
00122     libusb_free_device_list(list, 1);
00123 
00124     // configure
00125     if (m_handle)
00126     {
00127         err = libusb_set_configuration(m_handle, 1);
00128         if (err != LIBUSB_SUCCESS) {
00129             throw std::runtime_error(libusb_error_name(err));
00130         }
00131 
00132         err = libusb_claim_interface(m_handle, 0);
00133         if (err != LIBUSB_SUCCESS) {
00134             throw std::runtime_error(libusb_error_name(err));
00135         }
00136     }
00137     else
00138     {
00139         std::stringstream sstr;
00140         sstr << "No matching USB Device with devid = " << devid << " found!";
00141         throw std::runtime_error(sstr.str());
00142     }
00143 }
00144 
00145 void USBDevice::sendVendorSetup(
00146     uint8_t request,
00147     uint16_t value,
00148     uint16_t index,
00149     const unsigned char* data,
00150     uint16_t length)
00151 {
00152     if (!m_handle) {
00153         throw std::runtime_error("No valid device handle!");
00154     }
00155 
00156     int status = libusb_control_transfer(
00157         m_handle,
00158         LIBUSB_REQUEST_TYPE_VENDOR,
00159         request,
00160         value,
00161         index,
00162         (unsigned char*)data,
00163         length,
00164         /*timeout*/ 1000);
00165     if (status != LIBUSB_SUCCESS) {
00166         throw std::runtime_error(libusb_error_name(status));
00167     }
00168 }


crazyflie_cpp
Author(s): Wolfgang Hoenig
autogenerated on Sun Oct 8 2017 02:47:59