Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.4 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
#include <iostream>
21

    
22
using namespace uhd::transport;
23

    
24
const int libusb_debug_level = 0;
25

    
26
/****************************************************************
27
 * libusb USB device handle implementation class
28
 ***************************************************************/
29
class libusb1_device_handle_impl : public usb_device_handle {
30
public:
31
    libusb1_device_handle_impl(std::string serial,
32
                               boost::uint32_t product_id,
33
                               boost::uint32_t vendor_id,
34
                               boost::uint32_t device_addr)
35
      : _serial(serial), _product_id(product_id), 
36
        _vendor_id(vendor_id), _device_addr(device_addr)
37
    {
38
        /* NOP */
39
    }
40

    
41
    ~libusb1_device_handle_impl()
42
    {
43
        /* NOP */
44
    }
45

    
46
    std::string get_serial() const
47
    {
48
        return _serial;
49
    }
50

    
51
    boost::uint16_t get_vendor_id() const
52
    {
53
        return _vendor_id;
54
    }
55

    
56

    
57
    boost::uint16_t get_product_id() const
58
    {
59
        return _product_id;
60
    }
61

    
62
    boost::uint16_t get_device_addr() const
63
    {
64
        return _device_addr;
65
    }
66

    
67
private:
68
    std::string     _serial;
69
    boost::uint32_t _product_id;
70
    boost::uint32_t _vendor_id;
71
    boost::uint32_t _device_addr;
72
};
73

    
74

    
75
usb_device_handle::sptr make_usb_device_handle(libusb_device *dev)
76
{
77
    libusb_device_descriptor desc;
78

    
79
    if (libusb_get_device_descriptor(dev, &desc) < 0) {
80
        UHD_ASSERT_THROW("USB: failed to get device descriptor");
81
    }
82

    
83
    std::string     serial      = libusb::get_serial(dev);
84
    boost::uint32_t product_id  = desc.idProduct;
85
    boost::uint32_t vendor_id   = desc.idVendor;
86
    boost::uint32_t device_addr = libusb_get_device_address(dev);
87

    
88
    return usb_device_handle::sptr(new libusb1_device_handle_impl(
89
        serial,
90
        product_id,
91
        vendor_id,
92
        device_addr));
93
}
94

    
95
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list(boost::uint16_t vid, boost::uint16_t pid)
96
{
97
    libusb_context *ctx = NULL;
98
    libusb_device** libusb_device_list;
99
    std::vector<usb_device_handle::sptr> device_handle_list;
100
    libusb_device_descriptor desc;
101

    
102
    libusb::init(&ctx, libusb_debug_level);
103

    
104
    size_t dev_size = libusb_get_device_list(ctx, &libusb_device_list);
105
    for (size_t i = 0; i < dev_size; i++) {
106
        libusb_device *dev = libusb_device_list[i];
107
        if(libusb_get_device_descriptor(dev, &desc) < 0) {
108
          UHD_ASSERT_THROW("USB: failed to get device descriptor");
109
        }
110
        if(desc.idVendor == vid && desc.idProduct == pid) {
111
          device_handle_list.push_back(make_usb_device_handle(dev));
112
        }
113
    }
114

    
115
    libusb_exit(ctx);
116
    return device_handle_list; 
117
}