aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/libusb1_device_handle.cpp
blob: 43d0f0e2664b921752da9db419665d0002eaf147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Copyright 2010 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "libusb1_base.hpp"
#include <uhd/utils/assert.hpp>
#include <iostream>

using namespace uhd::transport;

const int libusb_debug_level = 0;

/****************************************************************
 * libusb USB device handle implementation class
 ***************************************************************/
class libusb1_device_handle_impl : public usb_device_handle {
public:
    libusb1_device_handle_impl(std::string serial,
                               boost::uint32_t product_id,
                               boost::uint32_t vendor_id,
                               boost::uint32_t device_addr)
      : _serial(serial), _product_id(product_id), 
        _vendor_id(vendor_id), _device_addr(device_addr)
    {
        /* NOP */
    }

    ~libusb1_device_handle_impl()
    {
        /* NOP */
    }

    std::string get_serial() const
    {
        return _serial;
    }

    boost::uint16_t get_vendor_id() const
    {
        return _vendor_id;
    }


    boost::uint16_t get_product_id() const
    {
        return _product_id;
    }

    boost::uint16_t get_device_addr() const
    {
        return _device_addr;
    }

private:
    std::string     _serial;
    boost::uint32_t _product_id;
    boost::uint32_t _vendor_id;
    boost::uint32_t _device_addr;
};


usb_device_handle::sptr make_usb_device_handle(libusb_device *dev)
{
    libusb_device_descriptor desc;

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

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

    return usb_device_handle::sptr(new libusb1_device_handle_impl(
        serial,
        product_id,
        vendor_id,
        device_addr));
}

std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list(boost::uint16_t vid, boost::uint16_t pid)
{
    libusb_context *ctx = NULL;
    libusb_device** libusb_device_list;
    std::vector<usb_device_handle::sptr> device_handle_list;
    libusb_device_descriptor desc;

    libusb::init(&ctx, libusb_debug_level);

    size_t dev_size = libusb_get_device_list(ctx, &libusb_device_list);
    for (size_t i = 0; i < dev_size; i++) {
        libusb_device *dev = libusb_device_list[i];
        if(libusb_get_device_descriptor(dev, &desc) < 0) {
          UHD_ASSERT_THROW("USB: failed to get device descriptor");
        }
        if(desc.idVendor == vid && desc.idProduct == pid) {
          device_handle_list.push_back(make_usb_device_handle(dev));
        }
    }

    libusb_exit(ctx);
    return device_handle_list; 
}