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_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_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_interface.h"
16 #include "components/usb_service/usb_service_export.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 enum UsbTransferStatus
{
38 USB_TRANSFER_COMPLETED
= 0,
41 USB_TRANSFER_CANCELLED
,
43 USB_TRANSFER_DISCONNECT
,
44 USB_TRANSFER_OVERFLOW
,
45 USB_TRANSFER_LENGTH_SHORT
,
48 typedef base::Callback
<
49 void(UsbTransferStatus
, scoped_refptr
<net::IOBuffer
>, size_t)>
52 // UsbDeviceHandle class provides basic I/O related functionalities.
53 class USB_SERVICE_EXPORT UsbDeviceHandle
54 : public base::RefCountedThreadSafe
<UsbDeviceHandle
> {
56 enum TransferRequestType
{ STANDARD
, CLASS
, VENDOR
, RESERVED
};
57 enum TransferRecipient
{ DEVICE
, INTERFACE
, ENDPOINT
, OTHER
};
59 scoped_refptr
<UsbDevice
> device() const;
60 PlatformUsbDeviceHandle
handle() const { return handle_
; }
62 // Notifies UsbDevice to drop the reference of this object; cancels all the
64 // It is possible that the object has no other reference after this call. So
65 // if it is called using a raw pointer, it could be invalidated.
66 // The platform device handle will be closed when UsbDeviceHandle destructs.
69 // Device manipulation operations. These methods are blocking and must be
70 // called on FILE thread.
71 virtual bool ClaimInterface(const int interface_number
);
72 virtual bool ReleaseInterface(const int interface_number
);
73 virtual bool SetInterfaceAlternateSetting(const int interface_number
,
74 const int alternate_setting
);
75 virtual bool ResetDevice();
76 virtual bool GetSerial(base::string16
* serial
);
78 // Async IO. Can be called on any thread.
79 virtual void ControlTransfer(const UsbEndpointDirection direction
,
80 const TransferRequestType request_type
,
81 const TransferRecipient recipient
,
85 net::IOBuffer
* buffer
,
87 const unsigned int timeout
,
88 const UsbTransferCallback
& callback
);
90 virtual void BulkTransfer(const UsbEndpointDirection direction
,
92 net::IOBuffer
* buffer
,
94 const unsigned int timeout
,
95 const UsbTransferCallback
& callback
);
97 virtual void InterruptTransfer(const UsbEndpointDirection direction
,
99 net::IOBuffer
* buffer
,
101 const unsigned int timeout
,
102 const UsbTransferCallback
& callback
);
104 virtual void IsochronousTransfer(const UsbEndpointDirection direction
,
105 const uint8 endpoint
,
106 net::IOBuffer
* buffer
,
108 const unsigned int packets
,
109 const unsigned int packet_length
,
110 const unsigned int timeout
,
111 const UsbTransferCallback
& callback
);
114 friend class base::RefCountedThreadSafe
<UsbDeviceHandle
>;
115 friend class UsbDevice
;
117 // This constructor is called by UsbDevice.
118 UsbDeviceHandle(scoped_refptr
<UsbContext
> context
,
120 PlatformUsbDeviceHandle handle
,
121 scoped_refptr
<UsbConfigDescriptor
> interfaces
);
123 // This constructor variant is for use in testing only.
125 virtual ~UsbDeviceHandle();
130 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle
);
132 class InterfaceClaimer
;
135 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
136 // SetInterfaceAlternateSetting.
137 void RefreshEndpointMap();
139 // Look up the claimed interface by endpoint. Return NULL if the interface
140 // of the endpoint is not found.
141 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
142 unsigned char endpoint
);
144 // Submits a transfer and starts tracking it. Retains the buffer and copies
145 // the completion callback until the transfer finishes, whereupon it invokes
146 // the callback then releases the buffer.
147 void SubmitTransfer(PlatformUsbTransferHandle handle
,
148 UsbTransferType transfer_type
,
149 net::IOBuffer
* buffer
,
151 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
,
152 const UsbTransferCallback
& callback
);
154 // Invokes the callbacks associated with a given transfer, and removes it from
155 // the in-flight transfer set.
156 void TransferComplete(PlatformUsbTransferHandle transfer
);
158 // Informs the object to drop internal references.
159 void InternalClose();
161 PlatformUsbDeviceHandle handle_
;
163 scoped_refptr
<UsbConfigDescriptor
> interfaces_
;
165 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
> > ClaimedInterfaceMap
;
166 ClaimedInterfaceMap claimed_interfaces_
;
168 typedef std::map
<PlatformUsbTransferHandle
, Transfer
> TransferMap
;
169 TransferMap transfers_
;
171 // A map from endpoints to interfaces
172 typedef std::map
<int, int> EndpointMap
;
173 EndpointMap endpoint_map_
;
175 // Retain the UsbContext so that the platform context will not be destroyed
176 // before this handle.
177 scoped_refptr
<UsbContext
> context_
;
179 base::ThreadChecker thread_checker_
;
181 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle
);
184 } // namespace usb_service
186 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_