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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
#define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
#include <stddef.h>
#include <stdint.h>
#include <map>
#include <set>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "device/usb/usb_device_handle.h"
#include "third_party/libusb/src/libusb/libusb.h"
namespace base {
class SequencedTaskRunner;
class SingleThreadTaskRunner;
class TaskRunner;
}
namespace net {
class IOBuffer;
}
namespace device {
struct EndpointMapValue {
int interface_number;
UsbTransferType transfer_type;
};
class UsbContext;
struct UsbConfigDescriptor;
class UsbDeviceImpl;
typedef libusb_device_handle* PlatformUsbDeviceHandle;
typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
typedef libusb_transfer* PlatformUsbTransferHandle;
// UsbDeviceHandle class provides basic I/O related functionalities.
class UsbDeviceHandleImpl : public UsbDeviceHandle {
public:
scoped_refptr<UsbDevice> GetDevice() const override;
void Close() override;
void SetConfiguration(int configuration_value,
const ResultCallback& callback) override;
void ClaimInterface(int interface_number,
const ResultCallback& callback) override;
bool ReleaseInterface(int interface_number) override;
void SetInterfaceAlternateSetting(int interface_number,
int alternate_setting,
const ResultCallback& callback) override;
void ResetDevice(const ResultCallback& callback) override;
void ClearHalt(uint8_t endpoint, const ResultCallback& callback) override;
void ControlTransfer(UsbEndpointDirection direction,
TransferRequestType request_type,
TransferRecipient recipient,
uint8_t request,
uint16_t value,
uint16_t index,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int timeout,
const TransferCallback& callback) override;
void IsochronousTransfer(UsbEndpointDirection direction,
uint8_t endpoint_number,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int packets,
unsigned int packet_length,
unsigned int timeout,
const TransferCallback& callback) override;
void GenericTransfer(UsbEndpointDirection direction,
uint8_t endpoint_number,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int timeout,
const TransferCallback& callback) override;
bool FindInterfaceByEndpoint(uint8_t endpoint_address,
uint8_t* interface_number) override;
protected:
friend class UsbDeviceImpl;
// This constructor is called by UsbDeviceImpl.
UsbDeviceHandleImpl(
scoped_refptr<UsbContext> context,
scoped_refptr<UsbDeviceImpl> device,
PlatformUsbDeviceHandle handle,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
~UsbDeviceHandleImpl() override;
PlatformUsbDeviceHandle handle() const { return handle_; }
private:
class InterfaceClaimer;
class Transfer;
void SetConfigurationOnBlockingThread(int configuration_value,
const ResultCallback& callback);
void SetConfigurationComplete(bool success, const ResultCallback& callback);
void ClaimInterfaceOnBlockingThread(int interface_number,
const ResultCallback& callback);
void ClaimInterfaceComplete(int interface_number,
bool success,
const ResultCallback& callback);
void SetInterfaceAlternateSettingOnBlockingThread(
int interface_number,
int alternate_setting,
const ResultCallback& callback);
void SetInterfaceAlternateSettingComplete(int interface_number,
int alternate_setting,
bool success,
const ResultCallback& callback);
void ResetDeviceOnBlockingThread(const ResultCallback& callback);
void ClearHaltOnBlockingThread(uint8_t endpoint,
const ResultCallback& callback);
// Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
// SetInterfaceAlternateSetting.
void RefreshEndpointMap();
// Look up the claimed interface by endpoint. Return NULL if the interface
// of the endpoint is not found.
scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
uint8_t endpoint);
void ControlTransferInternal(
UsbEndpointDirection direction,
TransferRequestType request_type,
TransferRecipient recipient,
uint8_t request,
uint16_t value,
uint16_t index,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int timeout,
scoped_refptr<base::TaskRunner> callback_task_runner,
const TransferCallback& callback);
void IsochronousTransferInternal(
uint8_t endpoint_address,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int packets,
unsigned int packet_length,
unsigned int timeout,
scoped_refptr<base::TaskRunner> callback_task_runner,
const TransferCallback& callback);
void GenericTransferInternal(
uint8_t endpoint_address,
scoped_refptr<net::IOBuffer> buffer,
size_t length,
unsigned int timeout,
scoped_refptr<base::TaskRunner> callback_task_runner,
const TransferCallback& callback);
// Submits a transfer and starts tracking it. Retains the buffer and copies
// the completion callback until the transfer finishes, whereupon it invokes
// the callback then releases the buffer.
void SubmitTransfer(scoped_ptr<Transfer> transfer);
// Removes the transfer from the in-flight transfer set and invokes the
// completion callback.
void TransferComplete(Transfer* transfer, const base::Closure& callback);
// Informs the object to drop internal references.
void InternalClose();
scoped_refptr<UsbDeviceImpl> device_;
PlatformUsbDeviceHandle handle_;
typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
ClaimedInterfaceMap claimed_interfaces_;
// This set holds weak pointers to pending transfers.
std::set<Transfer*> transfers_;
// A map from endpoints to EndpointMapValue
typedef std::map<int, EndpointMapValue> EndpointMap;
EndpointMap endpoint_map_;
// Retain the UsbContext so that the platform context will not be destroyed
// before this handle.
scoped_refptr<UsbContext> context_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
};
} // namespace device
#endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
|