OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/strings/string16.h" | |
16 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
17 #include "device/usb/usb_device_handle.h" | 15 #include "device/usb/usb_device_handle.h" |
18 #include "net/base/io_buffer.h" | |
19 #include "third_party/libusb/src/libusb/libusb.h" | 16 #include "third_party/libusb/src/libusb/libusb.h" |
20 | 17 |
21 namespace base { | 18 namespace base { |
| 19 class SequencedTaskRunner; |
22 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 21 class TaskRunner; |
| 22 } |
| 23 |
| 24 namespace net { |
| 25 class IOBuffer; |
23 } | 26 } |
24 | 27 |
25 namespace device { | 28 namespace device { |
26 | 29 |
27 class UsbContext; | 30 class UsbContext; |
28 struct UsbConfigDescriptor; | 31 struct UsbConfigDescriptor; |
29 class UsbDeviceImpl; | 32 class UsbDeviceImpl; |
30 | 33 |
31 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 34 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
32 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | 35 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
33 typedef libusb_transfer* PlatformUsbTransferHandle; | 36 typedef libusb_transfer* PlatformUsbTransferHandle; |
34 | 37 |
35 // UsbDeviceHandle class provides basic I/O related functionalities. | 38 // UsbDeviceHandle class provides basic I/O related functionalities. |
36 class UsbDeviceHandleImpl : public UsbDeviceHandle { | 39 class UsbDeviceHandleImpl : public UsbDeviceHandle { |
37 public: | 40 public: |
38 scoped_refptr<UsbDevice> GetDevice() const override; | 41 scoped_refptr<UsbDevice> GetDevice() const override; |
39 void Close() override; | 42 void Close() override; |
40 bool SetConfiguration(int configuration_value) override; | 43 void SetConfiguration(int configuration_value, |
41 bool ClaimInterface(int interface_number) override; | 44 const ResultCallback& callback) override; |
| 45 void ClaimInterface(int interface_number, |
| 46 const ResultCallback& callback) override; |
42 bool ReleaseInterface(int interface_number) override; | 47 bool ReleaseInterface(int interface_number) override; |
43 bool SetInterfaceAlternateSetting(int interface_number, | 48 void SetInterfaceAlternateSetting(int interface_number, |
44 int alternate_setting) override; | 49 int alternate_setting, |
45 bool ResetDevice() override; | 50 const ResultCallback& callback) override; |
46 bool GetStringDescriptor(uint8 string_id, base::string16* string) override; | 51 void ResetDevice(const ResultCallback& callback) override; |
47 | 52 |
48 void ControlTransfer(UsbEndpointDirection direction, | 53 void ControlTransfer(UsbEndpointDirection direction, |
49 TransferRequestType request_type, | 54 TransferRequestType request_type, |
50 TransferRecipient recipient, | 55 TransferRecipient recipient, |
51 uint8 request, | 56 uint8 request, |
52 uint16 value, | 57 uint16 value, |
53 uint16 index, | 58 uint16 index, |
54 net::IOBuffer* buffer, | 59 scoped_refptr<net::IOBuffer> buffer, |
55 size_t length, | 60 size_t length, |
56 unsigned int timeout, | 61 unsigned int timeout, |
57 const UsbTransferCallback& callback) override; | 62 const TransferCallback& callback) override; |
58 | 63 |
59 void BulkTransfer(UsbEndpointDirection direction, | 64 void BulkTransfer(UsbEndpointDirection direction, |
60 uint8 endpoint, | 65 uint8 endpoint, |
61 net::IOBuffer* buffer, | 66 scoped_refptr<net::IOBuffer> buffer, |
62 size_t length, | 67 size_t length, |
63 unsigned int timeout, | 68 unsigned int timeout, |
64 const UsbTransferCallback& callback) override; | 69 const TransferCallback& callback) override; |
65 | 70 |
66 void InterruptTransfer(UsbEndpointDirection direction, | 71 void InterruptTransfer(UsbEndpointDirection direction, |
67 uint8 endpoint, | 72 uint8 endpoint, |
68 net::IOBuffer* buffer, | 73 scoped_refptr<net::IOBuffer> buffer, |
69 size_t length, | 74 size_t length, |
70 unsigned int timeout, | 75 unsigned int timeout, |
71 const UsbTransferCallback& callback) override; | 76 const TransferCallback& callback) override; |
72 | 77 |
73 void IsochronousTransfer(UsbEndpointDirection direction, | 78 void IsochronousTransfer(UsbEndpointDirection direction, |
74 uint8 endpoint, | 79 uint8 endpoint, |
75 net::IOBuffer* buffer, | 80 scoped_refptr<net::IOBuffer> buffer, |
76 size_t length, | 81 size_t length, |
77 unsigned int packets, | 82 unsigned int packets, |
78 unsigned int packet_length, | 83 unsigned int packet_length, |
79 unsigned int timeout, | 84 unsigned int timeout, |
80 const UsbTransferCallback& callback) override; | 85 const TransferCallback& callback) override; |
81 | |
82 PlatformUsbDeviceHandle handle() const { return handle_; } | |
83 | 86 |
84 protected: | 87 protected: |
85 friend class UsbDeviceImpl; | 88 friend class UsbDeviceImpl; |
86 | 89 |
87 // This constructor is called by UsbDevice. | 90 // This constructor is called by UsbDeviceImpl. |
88 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, | 91 UsbDeviceHandleImpl( |
89 scoped_refptr<UsbDeviceImpl> device, | 92 scoped_refptr<UsbContext> context, |
90 PlatformUsbDeviceHandle handle); | 93 scoped_refptr<UsbDeviceImpl> device, |
| 94 PlatformUsbDeviceHandle handle, |
| 95 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
91 | 96 |
92 ~UsbDeviceHandleImpl() override; | 97 ~UsbDeviceHandleImpl() override; |
93 | 98 |
| 99 PlatformUsbDeviceHandle handle() const { return handle_; } |
| 100 |
94 private: | 101 private: |
95 friend class Transfer; | |
96 | |
97 class InterfaceClaimer; | 102 class InterfaceClaimer; |
98 class Transfer; | 103 class Transfer; |
99 | 104 |
| 105 void SetConfigurationOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 106 int configuration_value, |
| 107 const ResultCallback& callback); |
| 108 void SetConfigurationComplete(bool success, const ResultCallback& callback); |
| 109 void ClaimInterfaceOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 110 int interface_number, |
| 111 const ResultCallback& callback); |
| 112 void ClaimInterfaceComplete(int interface_number, |
| 113 bool success, |
| 114 const ResultCallback& callback); |
| 115 void SetInterfaceAlternateSettingOnBlockingThread( |
| 116 PlatformUsbDeviceHandle handle, |
| 117 int interface_number, |
| 118 int alternate_setting, |
| 119 const ResultCallback& callback); |
| 120 void SetInterfaceAlternateSettingComplete(int interface_number, |
| 121 int alternate_setting, |
| 122 bool success, |
| 123 const ResultCallback& callback); |
| 124 void ResetDeviceOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 125 const ResultCallback& callback); |
| 126 void ResetDeviceComplete(bool success, const ResultCallback& callback); |
| 127 |
100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | 128 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and |
101 // SetInterfaceAlternateSetting. | 129 // SetInterfaceAlternateSetting. |
102 void RefreshEndpointMap(); | 130 void RefreshEndpointMap(); |
103 | 131 |
104 // Look up the claimed interface by endpoint. Return NULL if the interface | 132 // Look up the claimed interface by endpoint. Return NULL if the interface |
105 // of the endpoint is not found. | 133 // of the endpoint is not found. |
106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( | 134 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( |
107 unsigned char endpoint); | 135 unsigned char endpoint); |
108 | 136 |
109 // If the device's task runner is on the current thread then the transfer will | 137 void ControlTransferInternal( |
110 // be submitted directly, otherwise a task to do so it posted. The callback | 138 UsbEndpointDirection direction, |
111 // will be called on the current message loop of the thread where this | 139 TransferRequestType request_type, |
112 // function was called. | 140 TransferRecipient recipient, |
113 void PostOrSubmitTransfer(scoped_ptr<Transfer> transfer); | 141 uint8 request, |
| 142 uint16 value, |
| 143 uint16 index, |
| 144 scoped_refptr<net::IOBuffer> buffer, |
| 145 size_t length, |
| 146 unsigned int timeout, |
| 147 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 148 const TransferCallback& callback); |
| 149 |
| 150 void BulkTransferInternal( |
| 151 UsbEndpointDirection direction, |
| 152 uint8 endpoint, |
| 153 scoped_refptr<net::IOBuffer> buffer, |
| 154 size_t length, |
| 155 unsigned int timeout, |
| 156 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 157 const TransferCallback& callback); |
| 158 |
| 159 void InterruptTransferInternal( |
| 160 UsbEndpointDirection direction, |
| 161 uint8 endpoint, |
| 162 scoped_refptr<net::IOBuffer> buffer, |
| 163 size_t length, |
| 164 unsigned int timeout, |
| 165 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 166 const TransferCallback& callback); |
| 167 |
| 168 void IsochronousTransferInternal( |
| 169 UsbEndpointDirection direction, |
| 170 uint8 endpoint, |
| 171 scoped_refptr<net::IOBuffer> buffer, |
| 172 size_t length, |
| 173 unsigned int packets, |
| 174 unsigned int packet_length, |
| 175 unsigned int timeout, |
| 176 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 177 const TransferCallback& callback); |
114 | 178 |
115 // Submits a transfer and starts tracking it. Retains the buffer and copies | 179 // Submits a transfer and starts tracking it. Retains the buffer and copies |
116 // the completion callback until the transfer finishes, whereupon it invokes | 180 // the completion callback until the transfer finishes, whereupon it invokes |
117 // the callback then releases the buffer. | 181 // the callback then releases the buffer. |
118 void SubmitTransfer(scoped_ptr<Transfer> transfer); | 182 void SubmitTransfer(scoped_ptr<Transfer> transfer); |
119 | 183 |
120 // Invokes the callbacks associated with a given transfer, and removes it from | 184 // Removes the transfer from the in-flight transfer set and invokes the |
121 // the in-flight transfer set. | 185 // completion callback. |
122 void CompleteTransfer(scoped_ptr<Transfer> transfer); | 186 void TransferComplete(Transfer* transfer, const base::Closure& callback); |
123 | |
124 bool GetSupportedLanguages(); | |
125 | 187 |
126 // Informs the object to drop internal references. | 188 // Informs the object to drop internal references. |
127 void InternalClose(); | 189 void InternalClose(); |
128 | 190 |
129 scoped_refptr<UsbDeviceImpl> device_; | 191 scoped_refptr<UsbDeviceImpl> device_; |
130 | 192 |
131 PlatformUsbDeviceHandle handle_; | 193 PlatformUsbDeviceHandle handle_; |
132 | 194 |
133 std::vector<uint16> languages_; | |
134 std::map<uint8, base::string16> strings_; | |
135 | |
136 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; | 195 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; |
137 ClaimedInterfaceMap claimed_interfaces_; | 196 ClaimedInterfaceMap claimed_interfaces_; |
138 | 197 |
139 // This set holds weak pointers to pending transfers. | 198 // This set holds weak pointers to pending transfers. |
140 std::set<Transfer*> transfers_; | 199 std::set<Transfer*> transfers_; |
141 | 200 |
142 // A map from endpoints to interfaces | 201 // A map from endpoints to interfaces |
143 typedef std::map<int, int> EndpointMap; | 202 typedef std::map<int, int> EndpointMap; |
144 EndpointMap endpoint_map_; | 203 EndpointMap endpoint_map_; |
145 | 204 |
146 // Retain the UsbContext so that the platform context will not be destroyed | 205 // Retain the UsbContext so that the platform context will not be destroyed |
147 // before this handle. | 206 // before this handle. |
148 scoped_refptr<UsbContext> context_; | 207 scoped_refptr<UsbContext> context_; |
149 | 208 |
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 209 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 210 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
151 | 211 |
152 base::ThreadChecker thread_checker_; | 212 base::ThreadChecker thread_checker_; |
153 base::WeakPtrFactory<UsbDeviceHandleImpl> weak_factory_; | |
154 | 213 |
155 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); | 214 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); |
156 }; | 215 }; |
157 | 216 |
158 } // namespace device | 217 } // namespace device |
159 | 218 |
160 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 219 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
OLD | NEW |