Disable firewall check. It takes signifficant time, need to be on FILE thread.
[chromium-blink-merge.git] / components / usb_service / usb_device_handle.h
blob60b7c64ac5eccafa2e6bdddd170870d6da197e99
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_
8 #include <map>
9 #include <vector>
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;
23 namespace base {
24 class MessageLoopProxy;
27 namespace usb_service {
29 class UsbContext;
30 class UsbConfigDescriptor;
31 class UsbDevice;
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,
39 USB_TRANSFER_ERROR,
40 USB_TRANSFER_TIMEOUT,
41 USB_TRANSFER_CANCELLED,
42 USB_TRANSFER_STALLED,
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)>
50 UsbTransferCallback;
52 // UsbDeviceHandle class provides basic I/O related functionalities.
53 class USB_SERVICE_EXPORT UsbDeviceHandle
54 : public base::RefCountedThreadSafe<UsbDeviceHandle> {
55 public:
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
63 // flying transfers.
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.
67 virtual void Close();
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,
82 const uint8 request,
83 const uint16 value,
84 const uint16 index,
85 net::IOBuffer* buffer,
86 const size_t length,
87 const unsigned int timeout,
88 const UsbTransferCallback& callback);
90 virtual void BulkTransfer(const UsbEndpointDirection direction,
91 const uint8 endpoint,
92 net::IOBuffer* buffer,
93 const size_t length,
94 const unsigned int timeout,
95 const UsbTransferCallback& callback);
97 virtual void InterruptTransfer(const UsbEndpointDirection direction,
98 const uint8 endpoint,
99 net::IOBuffer* buffer,
100 const size_t length,
101 const unsigned int timeout,
102 const UsbTransferCallback& callback);
104 virtual void IsochronousTransfer(const UsbEndpointDirection direction,
105 const uint8 endpoint,
106 net::IOBuffer* buffer,
107 const size_t length,
108 const unsigned int packets,
109 const unsigned int packet_length,
110 const unsigned int timeout,
111 const UsbTransferCallback& callback);
113 protected:
114 friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
115 friend class UsbDevice;
117 // This constructor is called by UsbDevice.
118 UsbDeviceHandle(scoped_refptr<UsbContext> context,
119 UsbDevice* device,
120 PlatformUsbDeviceHandle handle,
121 scoped_refptr<UsbConfigDescriptor> interfaces);
123 // This constructor variant is for use in testing only.
124 UsbDeviceHandle();
125 virtual ~UsbDeviceHandle();
127 UsbDevice* device_;
129 private:
130 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
132 class InterfaceClaimer;
133 struct Transfer;
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,
150 const size_t length,
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_