Revision b6099569
| b/host/lib/transport/libusb1_base.cpp | ||
|---|---|---|
| 16 | 16 |
// |
| 17 | 17 |
|
| 18 | 18 |
#include "libusb1_base.hpp" |
| 19 |
#include <uhd/utils/assert.hpp> |
|
| 19 | 20 |
#include <iostream> |
| 20 | 21 |
|
| 21 | 22 |
using namespace uhd::transport; |
| 22 | 23 |
|
| 24 |
bool check_fsf_device(libusb_device *dev) |
|
| 25 |
{
|
|
| 26 |
libusb_device_descriptor desc; |
|
| 27 |
|
|
| 28 |
if (libusb_get_device_descriptor(dev, &desc) < 0) {
|
|
| 29 |
UHD_ASSERT_THROW("USB: failed to get device descriptor");
|
|
| 30 |
} |
|
| 31 |
|
|
| 32 |
return desc.idVendor == 0xfffe; |
|
| 33 |
} |
|
| 34 |
|
|
| 23 | 35 |
void libusb::init(libusb_context **ctx, int debug_level) |
| 24 | 36 |
{
|
| 25 | 37 |
if (libusb_init(ctx) < 0) |
| ... | ... | |
| 28 | 40 |
libusb_set_debug(*ctx, debug_level); |
| 29 | 41 |
} |
| 30 | 42 |
|
| 43 |
std::vector<libusb_device *> libusb::get_fsf_device_list(libusb_context *ctx) |
|
| 44 |
{
|
|
| 45 |
libusb_device **libusb_dev_list; |
|
| 46 |
std::vector<libusb_device *> fsf_dev_list; |
|
| 47 |
|
|
| 48 |
ssize_t dev_cnt = libusb_get_device_list(ctx, &libusb_dev_list); |
|
| 49 |
|
|
| 50 |
//find the FSF devices |
|
| 51 |
for (ssize_t i = 0; i < dev_cnt; i++) {
|
|
| 52 |
libusb_device *dev = libusb_dev_list[i]; |
|
| 53 |
|
|
| 54 |
if (check_fsf_device(dev)) |
|
| 55 |
fsf_dev_list.push_back(dev); |
|
| 56 |
else |
|
| 57 |
libusb_unref_device(dev); |
|
| 58 |
} |
|
| 59 |
|
|
| 60 |
libusb_free_device_list(libusb_dev_list, 0); |
|
| 61 |
|
|
| 62 |
return fsf_dev_list; |
|
| 63 |
} |
|
| 31 | 64 |
|
| 32 | 65 |
libusb_device_handle *libusb::open_device(libusb_context *ctx, |
| 33 | 66 |
usb_device_handle::sptr handle) |
| 34 | 67 |
{
|
| 35 |
libusb_device **dev_list;
|
|
| 36 |
libusb_device_handle *dev_handle;
|
|
| 68 |
libusb_device_handle *dev_handle = NULL;
|
|
| 69 |
std::vector<libusb_device *> fsf_dev_list = get_fsf_device_list(ctx);
|
|
| 37 | 70 |
|
| 38 |
ssize_t dev_cnt = libusb_get_device_list(ctx, &dev_list); |
|
| 39 |
|
|
| 40 |
//find and open the receive device |
|
| 41 |
for (ssize_t i = 0; i < dev_cnt; i++) {
|
|
| 42 |
libusb_device *dev = dev_list[i]; |
|
| 71 |
//find and open the USB device |
|
| 72 |
for (size_t i = 0; i < fsf_dev_list.size(); i++) {
|
|
| 73 |
libusb_device *dev = fsf_dev_list[i]; |
|
| 43 | 74 |
|
| 44 | 75 |
if (compare_device(dev, handle)) {
|
| 45 | 76 |
libusb_open(dev, &dev_handle); |
| 77 |
libusb_unref_device(dev); |
|
| 46 | 78 |
break; |
| 47 | 79 |
} |
| 80 |
|
|
| 81 |
libusb_unref_device(dev); |
|
| 48 | 82 |
} |
| 49 | 83 |
|
| 50 |
libusb_free_device_list(dev_list, 0); |
|
| 51 |
|
|
| 52 | 84 |
return dev_handle; |
| 53 | 85 |
} |
| 54 | 86 |
|
| b/host/lib/transport/libusb1_base.hpp | ||
|---|---|---|
| 27 | 27 |
namespace libusb {
|
| 28 | 28 |
void init(libusb_context **ctx, int debug_level); |
| 29 | 29 |
|
| 30 |
std::vector<libusb_device *> get_fsf_device_list(libusb_context *ctx); |
|
| 31 |
|
|
| 30 | 32 |
libusb_device_handle *open_device(libusb_context *ctx, |
| 31 | 33 |
usb_device_handle::sptr handle); |
| 32 | 34 |
|
| b/host/lib/transport/libusb1_device_handle.cpp | ||
|---|---|---|
| 88 | 88 |
device_addr)); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 |
bool check_fsf_device(libusb_device *dev) |
|
| 92 |
{
|
|
| 93 |
libusb_device_descriptor desc; |
|
| 94 |
|
|
| 95 |
if (libusb_get_device_descriptor(dev, &desc) < 0) {
|
|
| 96 |
UHD_ASSERT_THROW("USB: failed to get device descriptor");
|
|
| 97 |
} |
|
| 98 |
|
|
| 99 |
return desc.idVendor == 0xfffe; |
|
| 100 |
} |
|
| 101 |
|
|
| 102 | 91 |
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list() |
| 103 | 92 |
{
|
| 104 | 93 |
libusb_context *ctx = NULL; |
| 105 |
libusb_device **list;
|
|
| 106 |
std::vector<usb_device_handle::sptr> device_list; |
|
| 94 |
std::vector<libusb_device *> libusb_device_list;
|
|
| 95 |
std::vector<usb_device_handle::sptr> device_handle_list;
|
|
| 107 | 96 |
|
| 108 | 97 |
libusb::init(&ctx, libusb_debug_level); |
| 109 | 98 |
|
| 110 |
ssize_t cnt = libusb_get_device_list(ctx, &list); |
|
| 111 |
|
|
| 112 |
if (cnt < 0) |
|
| 113 |
throw std::runtime_error("USB: enumeration failed");
|
|
| 99 |
libusb_device_list = libusb::get_fsf_device_list(ctx); |
|
| 114 | 100 |
|
| 115 |
ssize_t i = 0; |
|
| 116 |
for (i = 0; i < cnt; i++) {
|
|
| 117 |
libusb_device *dev = list[i]; |
|
| 118 |
if (check_fsf_device(dev)) |
|
| 119 |
device_list.push_back(make_usb_device_handle(dev)); |
|
| 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)); |
|
| 120 | 104 |
} |
| 121 | 105 |
|
| 122 |
libusb_free_device_list(list, 0); |
|
| 123 | 106 |
libusb_exit(ctx); |
| 124 |
return device_list; |
|
| 107 |
return device_handle_list;
|
|
| 125 | 108 |
} |
Also available in: Unified diff