Statistics
| Branch: | Tag: | Revision:

root / host / lib / transport / libusb1_device_handle.cpp @ 5e047eb5

History | View | Annotate | Download (3.2 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
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
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list()
103
{
104
    libusb_context *ctx = NULL;
105
    libusb_device **list;
106
    std::vector<usb_device_handle::sptr> device_list;
107

    
108
    libusb::init(&ctx, libusb_debug_level);
109

    
110
    ssize_t cnt = libusb_get_device_list(ctx, &list);
111

    
112
    if (cnt < 0)
113
        throw std::runtime_error("USB: enumeration failed");
114

    
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));
120
    }
121

    
122
    libusb_free_device_list(list, 0);
123
    libusb_exit(ctx);
124
    return device_list; 
125
}