diff options
author | Thomas Tsou <ttsou@vt.edu> | 2010-08-26 12:21:50 -0700 |
---|---|---|
committer | Thomas Tsou <ttsou@vt.edu> | 2010-08-26 12:21:50 -0700 |
commit | fe7df530e69834e974108d2c3e682f38b8a75524 (patch) | |
tree | eef6db48251f83f0df21f10b4f9e8187eea4d9c2 /host/lib/transport/libusb1_control.cpp | |
parent | 687e118eefd7e35e3a094152a51927bd82ac030d (diff) | |
download | uhd-fe7df530e69834e974108d2c3e682f38b8a75524.tar.gz uhd-fe7df530e69834e974108d2c3e682f38b8a75524.tar.bz2 uhd-fe7df530e69834e974108d2c3e682f38b8a75524.zip |
usrp1: Modifiy USB transport implementations to use new interface
Common libusb1 code is consolidated in the libusb base file.
Diffstat (limited to 'host/lib/transport/libusb1_control.cpp')
-rw-r--r-- | host/lib/transport/libusb1_control.cpp | 170 |
1 files changed, 10 insertions, 160 deletions
diff --git a/host/lib/transport/libusb1_control.cpp b/host/lib/transport/libusb1_control.cpp index c2f7060e8..8bf271256 100644 --- a/host/lib/transport/libusb1_control.cpp +++ b/host/lib/transport/libusb1_control.cpp @@ -15,24 +15,20 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/types/usb_descriptor.hpp> +#include "libusb1_base.hpp" #include <uhd/transport/usb_control.hpp> -#include <libusb-1.0/libusb.h> -#include <boost/asio.hpp> -#include <stdexcept> -#include <iostream> using namespace uhd::transport; -static int libusb_debug_level = 0; -static int libusb_timeout = 0; +const int libusb_debug_level = 3; +const int libusb_timeout = 0; /*********************************************************************** * libusb-1.0 implementation of USB control transport **********************************************************************/ class libusb_control_impl : public usb_control { public: - libusb_control_impl(uhd::usb_descriptor_t descriptor); + libusb_control_impl(usb_device_handle::sptr handle); ~libusb_control_impl(); size_t submit(boost::uint8_t request_type, @@ -42,34 +38,19 @@ public: unsigned char *buff, boost::uint16_t length); - static uhd::usb_descriptor_t create_descriptor(libusb_device *dev); - static std::string get_serial(libusb_device *dev); - private: - uhd::usb_descriptor_t _descriptor; - libusb_context *_ctx; libusb_device_handle *_dev_handle; - - bool open_device(); - bool open_interface(); - bool compare_device(libusb_device *dev, uhd::usb_descriptor_t descriptor); }; -libusb_control_impl::libusb_control_impl(uhd::usb_descriptor_t descriptor) - : _descriptor(descriptor), _ctx(NULL), _dev_handle(NULL) +libusb_control_impl::libusb_control_impl(usb_device_handle::sptr handle) { - if (libusb_init(&_ctx) < 0) - throw std::runtime_error("USB: failed to initialize libusb"); + libusb::init(&_ctx, libusb_debug_level); - libusb_set_debug(_ctx, libusb_debug_level); + _dev_handle = libusb::open_device(_ctx, handle); - if (!open_device()) - throw std::runtime_error("USB: failed to open device"); - - if (!open_interface()) - throw std::runtime_error("USB: failed to open device interface"); + libusb::open_interface(_dev_handle, 0); } @@ -80,111 +61,6 @@ libusb_control_impl::~libusb_control_impl() } -uhd::usb_descriptor_t libusb_control_impl::create_descriptor(libusb_device *dev) -{ - libusb_device_descriptor desc; - - if (libusb_get_device_descriptor(dev, &desc) < 0) - throw std::runtime_error("USB: failed to get device descriptor"); - - uhd::usb_descriptor_t descriptor; - - descriptor.serial = get_serial(dev); - descriptor.product_id = desc.idProduct; - descriptor.vendor_id = desc.idVendor; - descriptor.device_addr = libusb_get_device_address(dev); - - return descriptor; -} - - -std::string libusb_control_impl::get_serial(libusb_device *dev) -{ - unsigned char buff[32]; - - libusb_device_descriptor desc; - if (libusb_get_device_descriptor(dev, &desc) < 0) - return ""; - - if (desc.iSerialNumber == 0) - return ""; - - //open the device because we have to - libusb_device_handle *dev_handle; - if (libusb_open(dev, &dev_handle) < 0) - return ""; - - if (libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber, - buff, sizeof(buff)) < 0) { - return ""; - } - - libusb_close(dev_handle); - - return (char*) buff; -} - - -bool libusb_control_impl::open_device() -{ - libusb_device **list; - libusb_device *dev; - - ssize_t cnt = libusb_get_device_list(_ctx, &list); - - if (cnt < 0) - return cnt; - - ssize_t i = 0; - for (i = 0; i < cnt; i++) { - dev = list[i]; - if (compare_device(dev, _descriptor)) - goto found; - } - return false; - -found: - int ret; - if ((ret = libusb_open(dev, &_dev_handle)) < 0) - return false; - else - return true; -} - - -bool libusb_control_impl::compare_device(libusb_device *dev, - uhd::usb_descriptor_t descriptor) -{ - std::string serial = descriptor.serial; - boost::uint16_t vendor_id = descriptor.vendor_id; - boost::uint16_t product_id = descriptor.product_id; - boost::uint8_t device_addr = descriptor.device_addr; - - libusb_device_descriptor libusb_desc; - if (libusb_get_device_descriptor(dev, &libusb_desc) < 0) - return false; - if (serial != get_serial(dev)) - return false; - if (vendor_id != libusb_desc.idVendor) - return false; - if (product_id != libusb_desc.idProduct) - return false; - if (device_addr != libusb_get_device_address(dev)) - return false; - - return true; -} - - -bool libusb_control_impl::open_interface() -{ - if (libusb_claim_interface(_dev_handle, 0) < 0) - return false; - else - return true; -} - - size_t libusb_control_impl::submit(boost::uint8_t request_type, boost::uint8_t request, boost::uint16_t value, @@ -206,33 +82,7 @@ size_t libusb_control_impl::submit(boost::uint8_t request_type, /*********************************************************************** * USB control public make functions **********************************************************************/ -usb_control::sptr usb_control::make(uhd::usb_descriptor_t descriptor) +usb_control::sptr usb_control::make(usb_device_handle::sptr handle) { - return sptr(new libusb_control_impl(descriptor)); + return sptr(new libusb_control_impl(handle)); } - -uhd::usb_descriptors_t usb_control::get_device_list() -{ - libusb_device **list; - uhd::usb_descriptors_t descriptors; - - if (libusb_init(NULL) < 0) - throw std::runtime_error("USB: failed to initialize libusb"); - - ssize_t cnt = libusb_get_device_list(NULL, &list); - - if (cnt < 0) - throw std::runtime_error("USB: failed to get device list"); - - ssize_t i = 0; - for (i = 0; i < cnt; i++) { - libusb_device *dev = list[i]; - descriptors.push_back(libusb_control_impl::create_descriptor(dev)); - } - - libusb_free_device_list(list, 0); - libusb_exit(NULL); - return descriptors; -} - - |