root / host / lib / transport / libusb1_device_handle.cpp @ b6099569
History | View | Annotate | Download (2.9 kB)
| 1 |
//
|
|---|---|
| 2 |
// Copyright 2010 Ettus Research LLC
|
| 3 |
//
|
| 4 |
// This program is free software: you can redistribute it and/or modify
|
| 5 |
// it under the terms of the GNU General Public License as published by
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or
|
| 7 |
// (at your option) any later version.
|
| 8 |
//
|
| 9 |
// This program is distributed in the hope that it will be useful,
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
// GNU General Public License for more details.
|
| 13 |
//
|
| 14 |
// You should have received a copy of the GNU General Public License
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 16 |
//
|
| 17 |
|
| 18 |
#include "libusb1_base.hpp" |
| 19 |
#include <uhd/utils/assert.hpp> |
| 20 |
|
| 21 |
using namespace uhd::transport; |
| 22 |
|
| 23 |
const int libusb_debug_level = 3; |
| 24 |
|
| 25 |
class libusb1_device_handle_impl : public usb_device_handle { |
| 26 |
public:
|
| 27 |
libusb1_device_handle_impl(std::string serial,
|
| 28 |
boost::uint32_t product_id, |
| 29 |
boost::uint32_t vendor_id, |
| 30 |
boost::uint32_t device_addr) |
| 31 |
: _serial(serial), _product_id(product_id), |
| 32 |
_vendor_id(vendor_id), _device_addr(device_addr) |
| 33 |
{
|
| 34 |
/* NOP */
|
| 35 |
} |
| 36 |
|
| 37 |
~libusb1_device_handle_impl() |
| 38 |
{
|
| 39 |
/* NOP */
|
| 40 |
} |
| 41 |
|
| 42 |
std::string get_serial() const |
| 43 |
{
|
| 44 |
return _serial;
|
| 45 |
} |
| 46 |
|
| 47 |
boost::uint16_t get_vendor_id() const
|
| 48 |
{
|
| 49 |
return _vendor_id;
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
boost::uint16_t get_product_id() const
|
| 54 |
{
|
| 55 |
return _product_id;
|
| 56 |
} |
| 57 |
|
| 58 |
boost::uint16_t get_device_addr() const
|
| 59 |
{
|
| 60 |
return _device_addr;
|
| 61 |
} |
| 62 |
|
| 63 |
private:
|
| 64 |
std::string _serial;
|
| 65 |
boost::uint32_t _product_id; |
| 66 |
boost::uint32_t _vendor_id; |
| 67 |
boost::uint32_t _device_addr; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
usb_device_handle::sptr make_usb_device_handle(libusb_device *dev) |
| 72 |
{
|
| 73 |
libusb_device_descriptor desc; |
| 74 |
|
| 75 |
if (libusb_get_device_descriptor(dev, &desc) < 0) { |
| 76 |
UHD_ASSERT_THROW("USB: failed to get device descriptor");
|
| 77 |
} |
| 78 |
|
| 79 |
std::string serial = libusb::get_serial(dev);
|
| 80 |
boost::uint32_t product_id = desc.idProduct; |
| 81 |
boost::uint32_t vendor_id = desc.idVendor; |
| 82 |
boost::uint32_t device_addr = libusb_get_device_address(dev); |
| 83 |
|
| 84 |
return usb_device_handle::sptr(new libusb1_device_handle_impl( |
| 85 |
serial, |
| 86 |
product_id, |
| 87 |
vendor_id, |
| 88 |
device_addr)); |
| 89 |
} |
| 90 |
|
| 91 |
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list() |
| 92 |
{
|
| 93 |
libusb_context *ctx = NULL;
|
| 94 |
std::vector<libusb_device *> libusb_device_list; |
| 95 |
std::vector<usb_device_handle::sptr> device_handle_list; |
| 96 |
|
| 97 |
libusb::init(&ctx, libusb_debug_level); |
| 98 |
|
| 99 |
libusb_device_list = libusb::get_fsf_device_list(ctx); |
| 100 |
|
| 101 |
for (size_t i = 0; i < libusb_device_list.size(); i++) { |
| 102 |
libusb_device *dev = libusb_device_list[i]; |
| 103 |
device_handle_list.push_back(make_usb_device_handle(dev)); |
| 104 |
} |
| 105 |
|
| 106 |
libusb_exit(ctx); |
| 107 |
return device_handle_list;
|
| 108 |
} |