aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/libusb1_base.cpp
blob: 92dcd969f08d3f8889d13cf8714cd9d54ba29960 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// 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;

/**********************************************************
 * Helper Methods
 **********************************************************/
/*
 * Check for FSF device
 * Compare the device's descriptor Vendor ID
 * \param dev pointer to libusb_device
 * \return true if Vendor ID matches 0xfffe
 */
bool check_fsf_device(libusb_device *dev)
{
    libusb_device_descriptor desc;

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

    return desc.idVendor == 0xfffe;
}

/**********************************************************
 * libusb namespace 
 **********************************************************/
void libusb::init(libusb_context **ctx, int debug_level)
{
    if (libusb_init(ctx) < 0)
        std::cerr << "error: libusb_init" << std::endl;

    libusb_set_debug(*ctx, debug_level);
}

std::vector<libusb_device *> libusb::get_fsf_device_list(libusb_context *ctx)
{
    libusb_device **libusb_dev_list;
    std::vector<libusb_device *> fsf_dev_list;

    ssize_t dev_cnt = libusb_get_device_list(ctx, &libusb_dev_list);

    //find the FSF devices 
    for (ssize_t i = 0; i < dev_cnt; i++) {
        libusb_device *dev = libusb_dev_list[i];

        if (check_fsf_device(dev))
            fsf_dev_list.push_back(dev);
        else
            libusb_unref_device(dev);
    }

    libusb_free_device_list(libusb_dev_list, 0);

    return fsf_dev_list;
}

libusb_device_handle *libusb::open_device(libusb_context *ctx,
                                          usb_device_handle::sptr handle)
{
    libusb_device_handle *dev_handle = NULL;
    std::vector<libusb_device *> fsf_dev_list = get_fsf_device_list(ctx);

    //find and open the USB device 
    for (size_t i = 0; i < fsf_dev_list.size(); i++) {
        libusb_device *dev = fsf_dev_list[i];

        if (compare_device(dev, handle)) {
            libusb_open(dev, &dev_handle);
            libusb_unref_device(dev);
            break;
        }
            
        libusb_unref_device(dev);
    }

    return dev_handle;
}


bool libusb::compare_device(libusb_device *dev,
                            usb_device_handle::sptr handle)
{
    std::string serial         = handle->get_serial();
    boost::uint16_t vendor_id  = handle->get_vendor_id();
    boost::uint16_t product_id = handle->get_product_id();
    boost::uint8_t device_addr = handle->get_device_addr();

    libusb_device_descriptor libusb_desc;
    if (libusb_get_device_descriptor(dev, &libusb_desc) < 0)
        return false;
    if (serial != get_serial(dev))
        return false;
    if (vendor_id != libusb_desc.idVendor)
        return false;
    if (product_id != libusb_desc.idProduct)
        return false; 
    if (device_addr != libusb_get_device_address(dev))
        return false;

    return true;
}


bool libusb::open_interface(libusb_device_handle *dev_handle,
                            int interface)
{
    int ret = libusb_claim_interface(dev_handle, interface);
    if (ret < 0) {
        std::cerr << "error: libusb_claim_interface() " << ret << std::endl;
        return false;
    }
    else {
        return true;
    }
}


std::string libusb::get_serial(libusb_device *dev)
{
    unsigned char buff[32];

    libusb_device_descriptor desc;
    if (libusb_get_device_descriptor(dev, &desc) < 0)
        return "";

    if (desc.iSerialNumber == 0)
        return "";

    //open the device because we have to
    libusb_device_handle *dev_handle;
    if (libusb_open(dev, &dev_handle) < 0)
        return "";

    if (libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber,
                                           buff, sizeof(buff)) < 0) {
        return "";
    }

    libusb_close(dev_handle);

    return (char*) buff;
}