Statistics
| Branch: | Tag: | Revision:

root / host / lib / transport / libusb1_device_handle.cpp @ fe7df530

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

    
92
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list()
93
{
94
    libusb_context *ctx = NULL;
95
    libusb_device **list;
96
    std::vector<usb_device_handle::sptr> device_list;
97

    
98
    libusb::init(&ctx, libusb_debug_level);
99

    
100
    ssize_t cnt = libusb_get_device_list(ctx, &list);
101

    
102
    if (cnt < 0)
103
        throw std::runtime_error("USB: enumeration failed");
104

    
105
    ssize_t i = 0;
106
    for (i = 0; i < cnt; i++) {
107
        libusb_device *dev = list[i];
108
        device_list.push_back(make_usb_device_handle(dev));
109
    }
110

    
111
    libusb_free_device_list(list, 0);
112
    libusb_exit(ctx);
113
    return device_list; 
114
}