-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLibUsb.cpp
323 lines (256 loc) · 8.98 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (c) 2011 Darren Hague & Eric Brandt
* Modified to support Linux and OSX by Mark Liversedge
*
* 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 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 General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <QString>
#include <QDebug>
#ifndef Q_CC_MSVC
#include <unistd.h>
#endif
#include "LibUsb.h"
#define LINUX 0
#define WINDOWS 1
#define OSX 2
#ifdef Q_OS_MAC
#define OperatingSystem 2
#elif defined Q_OS_WIN32
#define OperatingSystem 1
#elif defined Q_OS_LINUX
#define OperatingSystem 0
#endif
LibUsb::LibUsb(int type) : type(type)
{
intf = NULL;
readBufIndex = 0;
readBufSize = 0;
// Initialize the library.
usb_init();
usb_set_debug(0);
usb_find_busses();
usb_find_devices();
}
int LibUsb::open()
{
// reset counters
intf = NULL;
readBufIndex = 0;
readBufSize = 0;
// Find all busses.
usb_find_busses();
// Find all connected devices.
usb_find_devices();
switch (type) {
// Search USB busses for USB2 ANT+ stick host controllers
default:
case TYPE_ANT: device = OpenAntStick();
break;
}
if (device == NULL) return -1;
// Clear halt is needed, but ignore return code
usb_clear_halt(device, writeEndpoint);
usb_clear_halt(device, readEndpoint);
return 0;
}
bool LibUsb::find()
{
usb_set_debug(0);
usb_find_busses();
usb_find_devices();
switch (type) {
// Search USB busses for USB2 ANT+ stick host controllers
default:
case TYPE_ANT: return findAntStick();
break;
}
}
void LibUsb::close()
{
if (device) {
// stop any further write attempts whilst we close down
usb_dev_handle *p = device;
device = NULL;
usb_release_interface(p, interface);
//usb_reset(p);
usb_close(p);
}
}
int LibUsb::read(char *buf, int bytes)
{
// check it isn't closed already
if (!device) return -1;
// The USB2 stick really doesn't like you reading 1 byte when more are available
// so we use a local buffered read
int bufRemain = readBufSize - readBufIndex;
// Can we entirely satisfy the request from the buffer?
if (bufRemain >= bytes)
{
// Yes, so do it
memcpy(buf, readBuf+readBufIndex, bytes);
readBufIndex += bytes;
return bytes;
}
// No, so partially satisfy by emptying the buffer, then refill the buffer for the rest
// !!! CHECK bufRemain > 0 rather than non-zero fixes annoying bug with ANT+
// devices not working again after restart "sometimes"
if (bufRemain > 0) {
memcpy(buf, readBuf+readBufIndex, bufRemain);
}
readBufSize = 0;
readBufIndex = 0;
int rc = usb_bulk_read(device, readEndpoint, readBuf, 64, 125);
if (rc < 0)
{
// don't report timeouts - lots of noise so commented out
//qDebug()<<"usb_bulk_read Error reading: "<<rc<< usb_strerror();
return rc;
}
readBufSize = rc;
int bytesToGo = bytes - bufRemain;
if (bytesToGo < readBufSize)
{
// If we have enough bytes in the buffer, return them
memcpy(buf+bufRemain, readBuf, bytesToGo);
readBufIndex += bytesToGo;
rc = bytes;
} else {
// Otherwise, just return what we can
memcpy(buf+bufRemain, readBuf, readBufSize);
rc = bufRemain + readBufSize;
readBufSize = 0;
readBufIndex = 0;
}
return rc;
}
int LibUsb::write(char *buf, int bytes)
{
// check it isn't closed
if (!device) return -1;
int rc;
if (OperatingSystem == WINDOWS) {
rc = usb_interrupt_write(device, writeEndpoint, buf, bytes, 1000);
} else {
// we use a non-interrupted write on Linux/Mac since the interrupt
// write block size is incorrectly implemented in the version of
// libusb we build with. It is no less efficient.
rc = usb_bulk_write(device, writeEndpoint, buf, bytes, 125);
}
if (rc < 0)
{
// Report timeouts - previously we ignored -110 errors. This masked a serious
// problem with write on Linux/Mac, the USB stick needed a reset to avoid
// these error messages, so we DO report them now
qDebug()<<"usb_interrupt_write Error writing ["<<rc<<"]: "<< usb_strerror();
}
return rc;
}
bool LibUsb::findAntStick()
{
struct usb_bus* bus;
struct usb_device* dev;
bool found = false;
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == GARMIN_USB2_VID &&
(dev->descriptor.idProduct == GARMIN_USB2_PID || dev->descriptor.idProduct == GARMIN_OEM_PID)) {
found = true;
}
}
}
return found;
}
struct usb_dev_handle* LibUsb::OpenAntStick()
{
struct usb_bus* bus;
struct usb_device* dev;
struct usb_dev_handle* udev;
// for Mac and Linux we do a bus reset on it first...
#ifndef WIN32
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == GARMIN_USB2_VID &&
(dev->descriptor.idProduct == GARMIN_USB2_PID || dev->descriptor.idProduct == GARMIN_OEM_PID)) {
if ((udev = usb_open(dev))) {
usb_reset(udev);
usb_close(udev);
}
}
}
}
#endif
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == GARMIN_USB2_VID &&
(dev->descriptor.idProduct == GARMIN_USB2_PID || dev->descriptor.idProduct == GARMIN_OEM_PID)) {
//Avoid noisy output
qDebug() << "Found a Garmin USB2 ANT+ stick" << bus->dirname << " " << dev->filename;
if ((udev = usb_open(dev))) {
if (dev->descriptor.bNumConfigurations) {
if ((intf = usb_find_interface(&dev->config[0])) != NULL) {
#ifdef Q_OS_LINUX
usb_detach_kernel_driver_np(udev, interface);
#endif
int rc = usb_set_configuration(udev, 1);
if (rc < 0) {
qDebug()<<"usb_set_configuration Error: "<< usb_strerror();
if (OperatingSystem == LINUX) {
// looks like the udev rule has not been implemented
qDebug()<<"check permissions on:"<<QString("/dev/bus/usb/%1/%2").arg(bus->dirname).arg(dev->filename);
qDebug()<<"did you remember to setup a udev rule for this device?";
}
}
rc = usb_claim_interface(udev, interface);
if (rc < 0) qDebug()<<"usb_claim_interface Error: "<< usb_strerror();
if (OperatingSystem != OSX) {
// fails on Mac OS X, we don't actually need it anyway
rc = usb_set_altinterface(udev, alternate);
if (rc < 0) qDebug()<<"usb_set_altinterface Error: "<< usb_strerror();
}
return udev;
}
}
usb_close(udev);
}
}
}
}
return NULL;
}
struct usb_interface_descriptor* LibUsb::usb_find_interface(struct usb_config_descriptor* config_descriptor)
{
struct usb_interface_descriptor* intf;
readEndpoint = -1;
writeEndpoint = -1;
interface = -1;
alternate = -1;
if (!config_descriptor) return NULL;
if (!config_descriptor->bNumInterfaces) return NULL;
if (!config_descriptor->interface[0].num_altsetting) return NULL;
intf = &config_descriptor->interface[0].altsetting[0];
if (intf->bNumEndpoints != 2) return NULL;
interface = intf->bInterfaceNumber;
alternate = intf->bAlternateSetting;
for (int i = 0 ; i < 2; i++)
{
if (intf->endpoint[i].bEndpointAddress & USB_ENDPOINT_DIR_MASK)
readEndpoint = intf->endpoint[i].bEndpointAddress;
else
writeEndpoint = intf->endpoint[i].bEndpointAddress;
}
if (readEndpoint < 0 || writeEndpoint < 0)
return NULL;
return intf;
}