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
3 // found in the LICENSE file.
5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/thread_checker.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "third_party/libusb/src/libusb/libusb.h"
19 class SequencedTaskRunner
;
20 class SingleThreadTaskRunner
;
31 struct UsbConfigDescriptor
;
34 typedef libusb_device_handle
* PlatformUsbDeviceHandle
;
35 typedef libusb_iso_packet_descriptor
* PlatformUsbIsoPacketDescriptor
;
36 typedef libusb_transfer
* PlatformUsbTransferHandle
;
38 // UsbDeviceHandle class provides basic I/O related functionalities.
39 class UsbDeviceHandleImpl
: public UsbDeviceHandle
{
41 scoped_refptr
<UsbDevice
> GetDevice() const override
;
42 void Close() override
;
43 void SetConfiguration(int configuration_value
,
44 const ResultCallback
& callback
) override
;
45 void ClaimInterface(int interface_number
,
46 const ResultCallback
& callback
) override
;
47 bool ReleaseInterface(int interface_number
) override
;
48 void SetInterfaceAlternateSetting(int interface_number
,
49 int alternate_setting
,
50 const ResultCallback
& callback
) override
;
51 void ResetDevice(const ResultCallback
& callback
) override
;
53 void ControlTransfer(UsbEndpointDirection direction
,
54 TransferRequestType request_type
,
55 TransferRecipient recipient
,
59 scoped_refptr
<net::IOBuffer
> buffer
,
62 const TransferCallback
& callback
) override
;
64 void BulkTransfer(UsbEndpointDirection direction
,
66 scoped_refptr
<net::IOBuffer
> buffer
,
69 const TransferCallback
& callback
) override
;
71 void InterruptTransfer(UsbEndpointDirection direction
,
73 scoped_refptr
<net::IOBuffer
> buffer
,
76 const TransferCallback
& callback
) override
;
78 void IsochronousTransfer(UsbEndpointDirection direction
,
80 scoped_refptr
<net::IOBuffer
> buffer
,
83 unsigned int packet_length
,
85 const TransferCallback
& callback
) override
;
88 friend class UsbDeviceImpl
;
90 // This constructor is called by UsbDeviceImpl.
92 scoped_refptr
<UsbContext
> context
,
93 scoped_refptr
<UsbDeviceImpl
> device
,
94 PlatformUsbDeviceHandle handle
,
95 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
97 ~UsbDeviceHandleImpl() override
;
99 PlatformUsbDeviceHandle
handle() const { return handle_
; }
102 class InterfaceClaimer
;
105 void SetConfigurationOnBlockingThread(int configuration_value
,
106 const ResultCallback
& callback
);
107 void SetConfigurationComplete(bool success
, const ResultCallback
& callback
);
108 void ClaimInterfaceOnBlockingThread(int interface_number
,
109 const ResultCallback
& callback
);
110 void ClaimInterfaceComplete(int interface_number
,
112 const ResultCallback
& callback
);
113 void SetInterfaceAlternateSettingOnBlockingThread(
114 int interface_number
,
115 int alternate_setting
,
116 const ResultCallback
& callback
);
117 void SetInterfaceAlternateSettingComplete(int interface_number
,
118 int alternate_setting
,
120 const ResultCallback
& callback
);
121 void ResetDeviceOnBlockingThread(const ResultCallback
& callback
);
123 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
124 // SetInterfaceAlternateSetting.
125 void RefreshEndpointMap();
127 // Look up the claimed interface by endpoint. Return NULL if the interface
128 // of the endpoint is not found.
129 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
130 unsigned char endpoint
);
132 void ControlTransferInternal(
133 UsbEndpointDirection direction
,
134 TransferRequestType request_type
,
135 TransferRecipient recipient
,
139 scoped_refptr
<net::IOBuffer
> buffer
,
141 unsigned int timeout
,
142 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
143 const TransferCallback
& callback
);
145 void BulkTransferInternal(
146 UsbEndpointDirection direction
,
148 scoped_refptr
<net::IOBuffer
> buffer
,
150 unsigned int timeout
,
151 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
152 const TransferCallback
& callback
);
154 void InterruptTransferInternal(
155 UsbEndpointDirection direction
,
157 scoped_refptr
<net::IOBuffer
> buffer
,
159 unsigned int timeout
,
160 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
161 const TransferCallback
& callback
);
163 void IsochronousTransferInternal(
164 UsbEndpointDirection direction
,
166 scoped_refptr
<net::IOBuffer
> buffer
,
168 unsigned int packets
,
169 unsigned int packet_length
,
170 unsigned int timeout
,
171 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
172 const TransferCallback
& callback
);
174 // Submits a transfer and starts tracking it. Retains the buffer and copies
175 // the completion callback until the transfer finishes, whereupon it invokes
176 // the callback then releases the buffer.
177 void SubmitTransfer(scoped_ptr
<Transfer
> transfer
);
179 // Removes the transfer from the in-flight transfer set and invokes the
180 // completion callback.
181 void TransferComplete(Transfer
* transfer
, const base::Closure
& callback
);
183 // Informs the object to drop internal references.
184 void InternalClose();
186 scoped_refptr
<UsbDeviceImpl
> device_
;
188 PlatformUsbDeviceHandle handle_
;
190 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
>> ClaimedInterfaceMap
;
191 ClaimedInterfaceMap claimed_interfaces_
;
193 // This set holds weak pointers to pending transfers.
194 std::set
<Transfer
*> transfers_
;
196 // A map from endpoints to interfaces
197 typedef std::map
<int, int> EndpointMap
;
198 EndpointMap endpoint_map_
;
200 // Retain the UsbContext so that the platform context will not be destroyed
201 // before this handle.
202 scoped_refptr
<UsbContext
> context_
;
204 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
205 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
207 base::ThreadChecker thread_checker_
;
209 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
212 } // namespace device
214 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_