-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLibUSB.cpp
266 lines (239 loc) · 7.86 KB
/
LibUSB.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// $Id: LibUSB.cpp 605 2011-02-22 04:00:58Z felfert $
//
// Copyright (C) 2006 The OpenNX Team
// Author: Fritz Elfert
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as
// published by the Free Software Foundation; either version 2 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 Library General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "LibUSB.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/defs.h"
#endif
#include "MyDynlib.h"
#include "LibUSB.h"
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <wx/log.h>
#include <wx/arrimpl.cpp>
#include "trace.h"
ENABLE_TRACE;
WX_DEFINE_OBJARRAY(ArrayOfUSBDevices);
#ifdef SUPPORT_USBIP
#include <usb.h>
typedef void (*Tusb_init)(void);
typedef int (*Tusb_find_busses)(void);
typedef int (*Tusb_find_devices)(void);
typedef struct usb_bus *(*Tusb_get_busses)(void);
typedef int (*Tusb_get_string_simple)(usb_dev_handle *dev, int index, char *buf,
size_t buflen);
typedef usb_dev_handle *(*Tusb_open)(struct usb_device *dev);
typedef int (*Tusb_close)(usb_dev_handle *dev);
#endif
USBDevice::USBDevice(int v, int p, unsigned char c) {
m_iVendor = v;
m_iProduct = p;
m_iClass = c;
}
wxString USBDevice::toString() {
wxString ret = wxString::Format(wxT("%d-%d %04X/%04X/%02X"),
(int)m_iBusNum, (int)m_iDevNum, (int)m_iVendor,
(int)m_iProduct, (int)m_iClass);
if (!m_sVendor.IsEmpty())
ret.Append(wxT(" ")).Append(m_sVendor);
if (!m_sProduct.IsEmpty())
ret.Append(wxT(" ")).Append(m_sProduct);
if (!m_sSerial.IsEmpty())
ret.Append(wxT(" #")).Append(m_sSerial);
return ret;
}
wxString USBDevice::toShortString() {
wxString ret = m_sVendor;
if ((!ret.IsEmpty()) && (!m_sProduct.IsEmpty()))
ret.Append(wxT(" "));
ret.Append(m_sProduct);
return ret;
}
#ifdef SUPPORT_USBIP
static wxString readSysfs(int bus, int dev, const wxString &attr)
{
wxLogNull nolog;
int i;
// Find the device with the specified devnum
for (i = 0; i < 16 ; ++i) {
wxString path(wxString::Format(wxT("/sys/bus/usb/devices/%d-%d/devnum"), bus, i));
wxFileInputStream fis(path);
wxTextInputStream tis(fis);
if (dev == tis.Read16S())
break;
}
// Read the attribute
wxString path(wxString::Format(wxT("/sys/bus/usb/devices/%d-%d/%s"), bus, i, attr.c_str()));
wxFileInputStream fis(path);
wxTextInputStream tis(fis);
return tis.ReadLine();
}
#endif
void USB::adddev(MyDynamicLibrary *dll, struct usb_device *dev, unsigned char dclass)
{
#ifdef SUPPORT_USBIP
usb_dev_handle *udev;
char string[256];
switch (dclass) {
case USB_CLASS_HUB:
case USB_CLASS_HID:
return;
}
wxDYNLIB_FUNCTION(Tusb_open, usb_open, *dll);
if (!pfnusb_open)
return;
wxDYNLIB_FUNCTION(Tusb_close, usb_close, *dll);
if (!pfnusb_close)
return;
wxDYNLIB_FUNCTION(Tusb_get_string_simple, usb_get_string_simple, *dll);
if (!pfnusb_get_string_simple)
return;
udev = pfnusb_open(dev);
if (!udev)
return;
USBDevice d(dev->descriptor.idVendor, dev->descriptor.idProduct, dclass);
if (dev->descriptor.iManufacturer) {
if (0 < pfnusb_get_string_simple(udev,
dev->descriptor.iManufacturer, string, sizeof(string)))
d.m_sVendor = wxConvUTF8.cMB2WX(string);
}
if (dev->descriptor.iProduct) {
if (0 < pfnusb_get_string_simple(udev,
dev->descriptor.iProduct, string, sizeof(string)))
d.m_sProduct = wxConvUTF8.cMB2WX(string);
}
if (dev->descriptor.iSerialNumber) {
if (0 < pfnusb_get_string_simple(udev,
dev->descriptor.iSerialNumber, string, sizeof(string)))
d.m_sSerial = wxConvUTF8.cMB2WX(string);
}
wxString tmp(dev->bus->dirname, wxConvUTF8);
long lval;
tmp.ToLong(&lval);
d.m_iBusNum = lval;
tmp = wxConvUTF8.cMB2WX(dev->filename);
tmp.ToLong(&lval);
d.m_iDevNum = lval;
// Try fallback to sysfs, if libusb could not read info due to
// permission problems.
if (d.m_sVendor.IsEmpty()) {
d.m_sVendor = readSysfs(d.m_iBusNum, d.m_iDevNum, wxT("manufacturer"));
}
if (d.m_sProduct.IsEmpty()) {
d.m_sProduct = readSysfs(d.m_iBusNum, d.m_iDevNum, wxT("product"));
}
if (d.m_sSerial.IsEmpty()) {
d.m_sSerial = readSysfs(d.m_iBusNum, d.m_iDevNum, wxT("serial"));
}
myLogTrace(MYTRACETAG, wxT("Device found: %s"), VMB(d.toString()));
m_aDevices.Add(d);
pfnusb_close(udev);
#else
wxUnusedVar(dll);
wxUnusedVar(dev);
wxUnusedVar(dclass);
#endif
}
void USB::usbscan(MyDynamicLibrary *dll)
{
#ifdef SUPPORT_USBIP
struct usb_bus *bus;
wxDYNLIB_FUNCTION(Tusb_find_busses, usb_find_busses, *dll);
if (!pfnusb_find_busses)
return;
wxDYNLIB_FUNCTION(Tusb_find_devices, usb_find_devices, *dll);
if (!pfnusb_find_devices)
return;
wxDYNLIB_FUNCTION(Tusb_get_busses, usb_get_busses, *dll);
if (!pfnusb_get_busses)
return;
pfnusb_find_busses();
pfnusb_find_devices();
for (bus = pfnusb_get_busses(); bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
int devclass = dev->descriptor.bDeviceClass;
if ((devclass == USB_CLASS_PER_INTERFACE) && (dev->config)) {
for (int c = 0; c < dev->descriptor.bNumConfigurations; c++) {
struct usb_config_descriptor *cfg = &dev->config[c];
for (int i = 0; i < cfg->bNumInterfaces; i++) {
struct usb_interface *uif = &cfg->interface[i];
for (int ac = 0; ac < uif->num_altsetting; ac++)
adddev(dll, dev, uif->altsetting[ac].bInterfaceClass);
}
}
} else
adddev(dll, dev, devclass);
}
}
#else
wxUnusedVar(dll);
#endif
}
USB::USB() {
m_bAvailable = false;
#ifdef SUPPORT_USBIP
wxLogNull noerrors;
MyDynamicLibrary dll;
if (dll.Load(wxT("libusb"))) {
wxDYNLIB_FUNCTION(Tusb_init, usb_init, dll);
if (!pfnusb_init)
return;
wxDYNLIB_FUNCTION(Tusb_find_busses, usb_find_busses, dll);
if (!pfnusb_find_busses)
return;
wxDYNLIB_FUNCTION(Tusb_find_devices, usb_find_devices, dll);
if (!pfnusb_find_devices)
return;
wxDYNLIB_FUNCTION(Tusb_get_busses, usb_get_busses, dll);
if (!pfnusb_get_busses)
return;
wxDYNLIB_FUNCTION(Tusb_open, usb_open, dll);
if (!pfnusb_open)
return;
wxDYNLIB_FUNCTION(Tusb_close, usb_close, dll);
if (!pfnusb_close)
return;
wxDYNLIB_FUNCTION(Tusb_get_string_simple, usb_get_string_simple, dll);
if (!pfnusb_get_string_simple)
return;
pfnusb_init();
m_bAvailable = true;
usbscan(&dll);
}
#endif
}
ArrayOfUSBDevices USB::GetDevices() {
return m_aDevices;
}
bool USB::IsAvailable() {
myLogTrace(MYTRACETAG, wxT("USB::IsAvailable() = %s"), m_bAvailable ? "true" : "false");
return m_bAvailable;
}