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_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/threading/thread_checker.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "net/base/io_buffer.h"
17 #include "third_party/libusb/src/libusb/libusb.h"
20 class SingleThreadTaskRunner
;
26 struct UsbConfigDescriptor
;
29 typedef libusb_device_handle
* PlatformUsbDeviceHandle
;
30 typedef libusb_iso_packet_descriptor
* PlatformUsbIsoPacketDescriptor
;
31 typedef libusb_transfer
* PlatformUsbTransferHandle
;
33 // UsbDeviceHandle class provides basic I/O related functionalities.
34 class UsbDeviceHandleImpl
: public UsbDeviceHandle
{
36 scoped_refptr
<UsbDevice
> GetDevice() const override
;
37 void Close() override
;
38 bool ClaimInterface(int interface_number
) override
;
39 bool ReleaseInterface(int interface_number
) override
;
40 bool SetInterfaceAlternateSetting(int interface_number
,
41 int alternate_setting
) override
;
42 bool ResetDevice() override
;
43 bool GetStringDescriptor(uint8 string_id
, base::string16
* string
) override
;
45 void ControlTransfer(UsbEndpointDirection direction
,
46 TransferRequestType request_type
,
47 TransferRecipient recipient
,
51 net::IOBuffer
* buffer
,
54 const UsbTransferCallback
& callback
) override
;
56 void BulkTransfer(UsbEndpointDirection direction
,
58 net::IOBuffer
* buffer
,
61 const UsbTransferCallback
& callback
) override
;
63 void InterruptTransfer(UsbEndpointDirection direction
,
65 net::IOBuffer
* buffer
,
68 const UsbTransferCallback
& callback
) override
;
70 void IsochronousTransfer(UsbEndpointDirection direction
,
72 net::IOBuffer
* buffer
,
75 unsigned int packet_length
,
77 const UsbTransferCallback
& callback
) override
;
79 PlatformUsbDeviceHandle
handle() const { return handle_
; }
82 friend class UsbDeviceImpl
;
84 // This constructor is called by UsbDevice.
85 UsbDeviceHandleImpl(scoped_refptr
<UsbContext
> context
,
86 UsbDeviceImpl
* device
,
87 PlatformUsbDeviceHandle handle
,
88 const UsbConfigDescriptor
& config
);
90 ~UsbDeviceHandleImpl() override
;
93 class InterfaceClaimer
;
96 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
97 // SetInterfaceAlternateSetting.
98 void RefreshEndpointMap();
100 // Look up the claimed interface by endpoint. Return NULL if the interface
101 // of the endpoint is not found.
102 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
103 unsigned char endpoint
);
105 // If the device's task runner is on the current thread then the transfer will
106 // be submitted directly, otherwise a task to do so it posted. The callback
107 // will be called on the current message loop of the thread where this
108 // function was called.
109 void PostOrSubmitTransfer(PlatformUsbTransferHandle handle
,
110 UsbTransferType transfer_type
,
111 net::IOBuffer
* buffer
,
113 const UsbTransferCallback
& callback
);
115 // Submits a transfer and starts tracking it. Retains the buffer and copies
116 // the completion callback until the transfer finishes, whereupon it invokes
117 // the callback then releases the buffer.
118 void SubmitTransfer(PlatformUsbTransferHandle handle
,
119 UsbTransferType transfer_type
,
120 net::IOBuffer
* buffer
,
122 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
123 const UsbTransferCallback
& callback
);
125 static void LIBUSB_CALL
126 PlatformTransferCallback(PlatformUsbTransferHandle handle
);
128 // Invokes the callbacks associated with a given transfer, and removes it from
129 // the in-flight transfer set.
130 void CompleteTransfer(PlatformUsbTransferHandle transfer
);
132 bool GetSupportedLanguages();
134 // Informs the object to drop internal references.
135 void InternalClose();
137 UsbDeviceImpl
* device_
;
139 PlatformUsbDeviceHandle handle_
;
141 const UsbConfigDescriptor
& config_
;
143 std::vector
<uint16
> languages_
;
144 std::map
<uint8
, base::string16
> strings_
;
146 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
> > ClaimedInterfaceMap
;
147 ClaimedInterfaceMap claimed_interfaces_
;
149 typedef std::map
<PlatformUsbTransferHandle
, Transfer
> TransferMap
;
150 TransferMap transfers_
;
152 // A map from endpoints to interfaces
153 typedef std::map
<int, int> EndpointMap
;
154 EndpointMap endpoint_map_
;
156 // Retain the UsbContext so that the platform context will not be destroyed
157 // before this handle.
158 scoped_refptr
<UsbContext
> context_
;
160 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
162 base::ThreadChecker thread_checker_
;
164 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
167 } // namespace device
169 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_