Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / usb_service / usb_device_handle.h
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.
4
5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
7
8 #include <map>
9 #include <vector>
10
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"
18
19 struct libusb_device_handle;
20 struct libusb_iso_packet_descriptor;
21 struct libusb_transfer;
22
23 namespace base {
24 class MessageLoopProxy;
25 }
26
27 namespace usb_service {
28
29 class UsbContext;
30 class UsbConfigDescriptor;
31 class UsbDevice;
32
33 typedef libusb_device_handle* PlatformUsbDeviceHandle;
34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
35 typedef libusb_transfer* PlatformUsbTransferHandle;
36
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,
46 };
47
48 typedef base::Callback<
49     void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>
50     UsbTransferCallback;
51
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 };
58
59   scoped_refptr<UsbDevice> device() const;
60   PlatformUsbDeviceHandle handle() const { return handle_; }
61
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();
68
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);
77
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);
89
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);
96
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);
103
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);
112
113  protected:
114   friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
115   friend class UsbDeviceImpl;
116
117   // This constructor is called by UsbDevice.
118   UsbDeviceHandle(scoped_refptr<UsbContext> context,
119                   UsbDevice* device,
120                   PlatformUsbDeviceHandle handle,
121                   scoped_refptr<UsbConfigDescriptor> interfaces);
122
123   // This constructor variant is for use in testing only.
124   UsbDeviceHandle();
125   virtual ~UsbDeviceHandle();
126
127   UsbDevice* device_;
128
129  private:
130   friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
131
132   class InterfaceClaimer;
133   struct Transfer;
134
135   // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
136   // SetInterfaceAlternateSetting.
137   void RefreshEndpointMap();
138
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);
143
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);
153
154   // Invokes the callbacks associated with a given transfer, and removes it from
155   // the in-flight transfer set.
156   void TransferComplete(PlatformUsbTransferHandle transfer);
157
158   // Informs the object to drop internal references.
159   void InternalClose();
160
161   PlatformUsbDeviceHandle handle_;
162
163   scoped_refptr<UsbConfigDescriptor> interfaces_;
164
165   typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
166   ClaimedInterfaceMap claimed_interfaces_;
167
168   typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
169   TransferMap transfers_;
170
171   // A map from endpoints to interfaces
172   typedef std::map<int, int> EndpointMap;
173   EndpointMap endpoint_map_;
174
175   // Retain the UsbContext so that the platform context will not be destroyed
176   // before this handle.
177   scoped_refptr<UsbContext> context_;
178
179   base::ThreadChecker thread_checker_;
180
181   DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
182 };
183
184 }  // namespace usb_service
185
186 #endif  // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_