forked from charlieh0tel/google-libusb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglibusb_endpoint.cc
362 lines (309 loc) · 11.7 KB
/
glibusb_endpoint.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2012 Google Inc. All Rights Reserved.
#include "glibusb_endpoint.h"
#include <stddef.h>
#include <glog/logging.h>
#include <boost/scoped_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "gbuffer.h"
#include "glibusb_endpoint_internal.h"
#include "glibusb_internal.h"
#include "glibusb_transfer.h"
namespace glibusb {
namespace {
int LibusbGetMaxPacketSize(libusb_device_handle *handle,
unsigned char endpoint)
{
libusb_device *device = CHECK_NOTNULL(libusb_get_device(handle));
return libusb_get_max_packet_size(device, endpoint);
}
int LibusbGetMaxIsoPacketSize(libusb_device_handle *handle,
unsigned char endpoint)
{
libusb_device *device = CHECK_NOTNULL(libusb_get_device(handle));
return libusb_get_max_iso_packet_size(device, endpoint);
}
} // namespace
Notification::Notification(bool prenotify)
: notified_(prenotify) {}
bool Notification::HasBeenNotified() const {
boost::lock_guard<boost::mutex> lock(mutex_);
return notified_;
}
void Notification::WaitForNotification() const {
boost::unique_lock<boost::mutex> lock(mutex_);
notified_changed_.wait(
lock,
boost::bind(&Notification::HasBeenNotifiedUnlocked, this));
}
bool Notification::WaitForNotificationWithTimeout(int64_t milliseconds) const {
boost::unique_lock<boost::mutex> lock(mutex_);
auto timeout = boost::posix_time::milliseconds(milliseconds);
notified_changed_.timed_wait(
lock, timeout,
boost::bind(&Notification::HasBeenNotifiedUnlocked, this));
return notified_;
}
void Notification::Notify() {
boost::lock_guard<boost::mutex> lock(mutex_);
CHECK(!notified_) << ": already notified";
notified_ = true;
notified_changed_.notify_all();
}
bool Notification::HasBeenNotifiedUnlocked() const {
return notified_;
}
////////////////////////////////////////////////////////////////////////
const int32_t UsbEndpoint::kMaxBulkTransferBytes;
UsbEndpoint::UsbEndpoint(DirectionType direction, TransferType transfer,
int endpoint_address)
: direction_(direction),
transfer_(transfer),
endpoint_address_and_direction_(endpoint_address | direction) {
CHECK_EQ(endpoint_address_and_direction_ & 0x80, direction)
<< ": Direction in address doesn't match specified direction.";
CHECK_EQ(endpoint_address_and_direction_ & 0x8f,
endpoint_address_and_direction_)
<< ": Invalid endpoint address.";
}
////////////////////////////////////////////////////////////////////////
UsbInEndpoint::UsbInEndpoint(TransferType transfer, int endpoint_address)
: UsbEndpoint(UsbEndpoint::kIn, transfer, endpoint_address) {
}
bool UsbInEndpoint::Read(Buffer *out) {
IoStatus status = ReadAtMostWithTimeout(kMaxBulkTransferBytes, 0, out);
return status == kSuccess;
}
UsbEndpoint::IoStatus
UsbInEndpoint::ReadWithTimeout(int32_t timeout_milliseconds,
Buffer *out) {
return ReadAtMostWithTimeout(kMaxBulkTransferBytes,
timeout_milliseconds, out);
}
bool UsbInEndpoint::ReadAtMost(uint32_t length, Buffer *out) {
IoStatus status = ReadAtMostWithTimeout(length, 0, out);
return status == kSuccess;
}
UsbEndpoint::IoStatus UsbInEndpoint::ReadAtMostWithTimeout(
uint32_t length, int32_t timeout_milliseconds, Buffer *out) {
return DoRead(length, timeout_milliseconds, out, NULL);
}
UsbEndpoint::IoStatus UsbInEndpoint::ReadAtMostWithTimeoutAndNotification(
uint32_t length, int32_t timeout_milliseconds, Buffer *out,
Notification *quit) {
CHECK_NOTNULL(quit);
return DoRead(length, timeout_milliseconds, out, quit);
}
////////////////////////////////////////////////////////////////////////
UsbOutEndpoint::UsbOutEndpoint(TransferType transfer, int endpoint_address)
: UsbEndpoint(UsbEndpoint::kOut, transfer, endpoint_address) {
}
bool UsbOutEndpoint::Write(const Buffer &buffer) {
IoStatus status = DoWrite(buffer, 0);
return status == kSuccess;
}
UsbEndpoint::IoStatus UsbOutEndpoint::WriteWithTimeout(const Buffer &buffer,
int32_t timeout_milliseconds) {
return DoWrite(buffer, timeout_milliseconds);
}
////////////////////////////////////////////////////////////////////////
PhysicalUsbInEndpoint::PhysicalUsbInEndpoint(
struct libusb_context *context,
struct libusb_device_handle *handle,
const struct libusb_endpoint_descriptor *descriptor)
: UsbInEndpoint(DescriptorToTransfer(descriptor),
DescriptorToAddress(descriptor)),
libusb_context_(CHECK_NOTNULL(context)),
handle_(CHECK_NOTNULL(handle)) {
VLOG(1) << "0x" << std::hex << static_cast<int>(endpoint_address_and_direction())
<< ", max_packet_size " << std::dec << descriptor->wMaxPacketSize;
CHECK_EQ(DescriptorToDirection(descriptor), UsbEndpoint::kIn);
}
PhysicalUsbInEndpoint::~PhysicalUsbInEndpoint() {
CHECK_NOTNULL(handle_);
handle_ = nullptr;
libusb_context_ = nullptr;
}
int PhysicalUsbInEndpoint::DoGetMaxPacketSize() {
CHECK_NOTNULL(handle_);
return LibusbGetMaxPacketSize(handle_, endpoint_address());
}
int PhysicalUsbInEndpoint::DoGetMaxIsoPacketSize() {
CHECK_NOTNULL(handle_);
return LibusbGetMaxIsoPacketSize(handle_, endpoint_address());
}
namespace {
unsigned char LibUsbTransferType(int transfer_type) {
switch (transfer_type) {
case UsbEndpoint::kControl:
return LIBUSB_TRANSFER_TYPE_CONTROL;
case UsbEndpoint::kBulk:
return LIBUSB_TRANSFER_TYPE_BULK;
case UsbEndpoint::kInterrupt:
return LIBUSB_TRANSFER_TYPE_INTERRUPT;
case UsbEndpoint::kIsochronous:
return LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
default:
LOG(FATAL) << "transfer_type " << transfer_type << " is bogus";
}
}
const char kTransferTypeNameControl[] = "control";
const char kTransferTypeNameBulk[] = "bulk";
const char kTransferTypeNameInterrupt[] = "interrupt";
const char kTransferTypeNameIsochronous[] = "isochronous";
const char *TransferTypeName(int transfer_type) {
switch (transfer_type) {
case UsbEndpoint::kControl:
return kTransferTypeNameControl;
case UsbEndpoint::kBulk:
return kTransferTypeNameBulk;
case UsbEndpoint::kInterrupt:
return kTransferTypeNameInterrupt;
case UsbEndpoint::kIsochronous:
return kTransferTypeNameIsochronous;
default:
LOG(FATAL) << "transfer_type " << transfer_type << " is bogus";
}
}
} // namespace
UsbEndpoint::IoStatus PhysicalUsbInEndpoint::DoRead(
uint32_t length, int32_t timeout_milliseconds, Buffer *out,
Notification *quit) {
CHECK_NOTNULL(handle_);
CHECK_GE(timeout_milliseconds, 0);
CHECK_NOTNULL(out);
VLOG(2) << "read on 0x" << std::hex << endpoint_address_and_direction()
<< ", size 0x" << std::hex << length
<< ", timeout " << std::dec << timeout_milliseconds << " [ms]";
boost::scoped_ptr<Buffer> whole_buffer(new Buffer());
whole_buffer->Resize(length);
void *p = whole_buffer->GetBufferPointer(length);
int transferred;
const unsigned int timeout = static_cast<unsigned int>(timeout_milliseconds);
int r;
unsigned char transfer_type = LibUsbTransferType(transfer());
r = do_sync_bulk_transfer(libusb_context_,
handle_, endpoint_address_and_direction(),
static_cast<unsigned char *>(p), length,
&transferred, timeout, transfer_type,
quit);
switch (r) {
case LIBUSB_SUCCESS:
{
size_t size_transferred = static_cast<size_t>(transferred);
whole_buffer->Resize(size_transferred);
VLOG(2)
<< "read on 0x"
<< std::hex << static_cast<int>(endpoint_address_and_direction())
<< ", size_transferred=0x" << std::hex << size_transferred;
out->Copy(*whole_buffer);
return kSuccess;
}
case LIBUSB_ERROR_TIMEOUT:
VLOG(2) << "libusb_" << TransferTypeName(transfer())
<< "_transfer timeout";
return kTimeout;
case LIBUSB_ERROR_IO:
VLOG(1) << "Device i/o error.";
return kFail;
case LIBUSB_ERROR_NO_DEVICE:
VLOG(1) << "Device disconnected.";
return kNoDevice;
case LIBUSB_ERROR_OTHER:
LOG(INFO) << "libusb_" << TransferTypeName(transfer())
<< "_transfer failed with r=" << std::dec << r;
return kUnknown;
default:
// Most of these are more esoteric.
LOG(INFO) << "libusb_" << TransferTypeName(transfer())
<< "_transfer failed with r=" << std::dec << r;
return kFail;
}
}
////////////////////////////////////////////////////////////////////////
PhysicalUsbOutEndpoint::PhysicalUsbOutEndpoint(
struct libusb_context *context,
struct libusb_device_handle *handle,
const struct libusb_endpoint_descriptor *descriptor)
: UsbOutEndpoint(DescriptorToTransfer(descriptor),
DescriptorToAddress(descriptor)),
libusb_context_(CHECK_NOTNULL(context)),
handle_(CHECK_NOTNULL(handle)) {
VLOG(1) << "0x" << std::hex << static_cast<int>(endpoint_address_and_direction())
<< ", max_packet_size " << std::dec << descriptor->wMaxPacketSize;
CHECK_EQ(DescriptorToDirection(descriptor), UsbEndpoint::kOut);
}
PhysicalUsbOutEndpoint::~PhysicalUsbOutEndpoint() {
CHECK_NOTNULL(handle_);
handle_ = nullptr;
libusb_context_ = nullptr;
}
int PhysicalUsbOutEndpoint::DoGetMaxPacketSize() {
CHECK_NOTNULL(handle_);
return LibusbGetMaxPacketSize(handle_, endpoint_address());
}
int PhysicalUsbOutEndpoint::DoGetMaxIsoPacketSize() {
CHECK_NOTNULL(handle_);
return LibusbGetMaxIsoPacketSize(handle_, endpoint_address());
}
UsbEndpoint::IoStatus PhysicalUsbOutEndpoint::DoWrite(
const Buffer &buffer, int32_t timeout_milliseconds) {
CHECK_NOTNULL(handle_);
CHECK_EQ(direction(), kOut);
VLOG(2) << "writing on 0x" << std::hex << endpoint_address_and_direction()
<< ", length=" << std::dec << buffer.Length()
<< ", timeout " << std::dec << timeout_milliseconds << " [ms]";
size_t length = buffer.Length();
const unsigned char *p =
static_cast<const unsigned char *>(buffer.GetBufferPointer(length));
const unsigned int timeout = static_cast<unsigned int>(timeout_milliseconds);
int transferred;
int r;
switch (transfer()) {
case kBulk:
VLOG(2) << "libusb_bulk_transfer, length=" << std::dec << length;
r = libusb_bulk_transfer(handle_, endpoint_address_and_direction(),
const_cast<unsigned char *>(p),
length, &transferred,
timeout);
VLOG(2) << "libusb_bulk_transfer, r=" << std::dec << r
<< ", transferred=" << std::dec << transferred;
break;
case kInterrupt:
VLOG(2) << "libusb_interrupt_transfer, length="
<< std::dec << length;
r = libusb_interrupt_transfer(handle_, endpoint_address_and_direction(),
const_cast<unsigned char *>(p),
length, &transferred,
timeout);
VLOG(2) << "libusb_interrupt_transfer, r=" << std::dec << r
<< ", transferred=" << std::dec << transferred;
break;
default:
LOG(FATAL) << "bogus transfer() value";
}
size_t size_transferred;
switch (r) {
case LIBUSB_SUCCESS:
size_transferred = static_cast<size_t>(transferred);
CHECK_EQ(size_transferred, length);
return kSuccess;
case LIBUSB_ERROR_TIMEOUT:
VLOG(2) << "libusb_" << TransferTypeName(transfer()) << "_transfer timeout";
return kTimeout;
case LIBUSB_ERROR_IO:
VLOG(1) << "Device i/o error.";
return kFail;
case LIBUSB_ERROR_NO_DEVICE:
VLOG(1) << "Device disconnected.";
return kNoDevice;
case LIBUSB_ERROR_OTHER:
LOG(INFO) << "libusb_" << TransferTypeName(transfer())
<< "_transfer failed with r=" << std::dec << r;
return kUnknown;
default:
VLOG(1) << "libusb_" << TransferTypeName(transfer())
<< "_transfer failed, r=" << std::dec << r;
return kFail;
}
}
} // namespace glibusb