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 COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_
6 #define COMPONENTS_USB_SERVICE_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 "components/usb_service/usb_device_handle.h"
16 #include "components/usb_service/usb_interface.h"
17 #include "net/base/io_buffer.h"
19 struct libusb_device_handle
;
20 struct libusb_iso_packet_descriptor
;
21 struct libusb_transfer
;
24 class MessageLoopProxy
;
27 namespace usb_service
{
30 class UsbConfigDescriptor
;
33 typedef libusb_device_handle
* PlatformUsbDeviceHandle
;
34 typedef libusb_iso_packet_descriptor
* PlatformUsbIsoPacketDescriptor
;
35 typedef libusb_transfer
* PlatformUsbTransferHandle
;
37 // UsbDeviceHandle class provides basic I/O related functionalities.
38 class UsbDeviceHandleImpl
: public UsbDeviceHandle
{
40 virtual scoped_refptr
<UsbDevice
> GetDevice() const OVERRIDE
;
41 virtual void Close() OVERRIDE
;
42 virtual bool ClaimInterface(const int interface_number
) OVERRIDE
;
43 virtual bool ReleaseInterface(const int interface_number
) OVERRIDE
;
44 virtual bool SetInterfaceAlternateSetting(
45 const int interface_number
,
46 const int alternate_setting
) OVERRIDE
;
47 virtual bool ResetDevice() OVERRIDE
;
48 virtual bool GetManufacturer(base::string16
* manufacturer
) OVERRIDE
;
49 virtual bool GetProduct(base::string16
* product
) OVERRIDE
;
50 virtual bool GetSerial(base::string16
* serial
) OVERRIDE
;
51 virtual void ControlTransfer(const UsbEndpointDirection direction
,
52 const TransferRequestType request_type
,
53 const TransferRecipient recipient
,
57 net::IOBuffer
* buffer
,
59 const unsigned int timeout
,
60 const UsbTransferCallback
& callback
) OVERRIDE
;
62 virtual void BulkTransfer(const UsbEndpointDirection direction
,
64 net::IOBuffer
* buffer
,
66 const unsigned int timeout
,
67 const UsbTransferCallback
& callback
) OVERRIDE
;
69 virtual void InterruptTransfer(const UsbEndpointDirection direction
,
71 net::IOBuffer
* buffer
,
73 const unsigned int timeout
,
74 const UsbTransferCallback
& callback
) OVERRIDE
;
76 virtual void IsochronousTransfer(
77 const UsbEndpointDirection direction
,
79 net::IOBuffer
* buffer
,
81 const unsigned int packets
,
82 const unsigned int packet_length
,
83 const unsigned int timeout
,
84 const UsbTransferCallback
& callback
) OVERRIDE
;
86 PlatformUsbDeviceHandle
handle() const { return handle_
; }
89 friend class UsbDeviceImpl
;
91 // This constructor is called by UsbDevice.
92 UsbDeviceHandleImpl(scoped_refptr
<UsbContext
> context
,
93 UsbDeviceImpl
* device
,
94 PlatformUsbDeviceHandle handle
,
95 scoped_refptr
<UsbConfigDescriptor
> interfaces
);
97 virtual ~UsbDeviceHandleImpl();
100 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle
);
102 class InterfaceClaimer
;
105 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
106 // SetInterfaceAlternateSetting.
107 void RefreshEndpointMap();
109 // Look up the claimed interface by endpoint. Return NULL if the interface
110 // of the endpoint is not found.
111 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
112 unsigned char endpoint
);
114 // Submits a transfer and starts tracking it. Retains the buffer and copies
115 // the completion callback until the transfer finishes, whereupon it invokes
116 // the callback then releases the buffer.
117 void SubmitTransfer(PlatformUsbTransferHandle handle
,
118 UsbTransferType transfer_type
,
119 net::IOBuffer
* buffer
,
121 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
122 const UsbTransferCallback
& callback
);
124 // Invokes the callbacks associated with a given transfer, and removes it from
125 // the in-flight transfer set.
126 void TransferComplete(PlatformUsbTransferHandle transfer
);
128 bool GetSupportedLanguages();
129 bool GetStringDescriptor(uint8 string_id
, base::string16
* string
);
131 // Informs the object to drop internal references.
132 void InternalClose();
134 UsbDeviceImpl
* device_
;
136 PlatformUsbDeviceHandle handle_
;
138 scoped_refptr
<UsbConfigDescriptor
> interfaces_
;
140 std::vector
<uint16
> languages_
;
141 std::map
<uint8
, base::string16
> strings_
;
143 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
> > ClaimedInterfaceMap
;
144 ClaimedInterfaceMap claimed_interfaces_
;
146 typedef std::map
<PlatformUsbTransferHandle
, Transfer
> TransferMap
;
147 TransferMap transfers_
;
149 // A map from endpoints to interfaces
150 typedef std::map
<int, int> EndpointMap
;
151 EndpointMap endpoint_map_
;
153 // Retain the UsbContext so that the platform context will not be destroyed
154 // before this handle.
155 scoped_refptr
<UsbContext
> context_
;
157 base::ThreadChecker thread_checker_
;
159 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
162 } // namespace usb_service
164 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_