Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / components / usb_service / usb_device_handle_impl.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_IMPL_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_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_device_handle.h"
16 #include "components/usb_service/usb_interface.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 UsbDeviceImpl;
32
33 typedef libusb_device_handle* PlatformUsbDeviceHandle;
34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
35 typedef libusb_transfer* PlatformUsbTransferHandle;
36
37 // UsbDeviceHandle class provides basic I/O related functionalities.
38 class UsbDeviceHandleImpl : public UsbDeviceHandle {
39  public:
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,
54                                const uint8 request,
55                                const uint16 value,
56                                const uint16 index,
57                                net::IOBuffer* buffer,
58                                const size_t length,
59                                const unsigned int timeout,
60                                const UsbTransferCallback& callback) OVERRIDE;
61
62   virtual void BulkTransfer(const UsbEndpointDirection direction,
63                             const uint8 endpoint,
64                             net::IOBuffer* buffer,
65                             const size_t length,
66                             const unsigned int timeout,
67                             const UsbTransferCallback& callback) OVERRIDE;
68
69   virtual void InterruptTransfer(const UsbEndpointDirection direction,
70                                  const uint8 endpoint,
71                                  net::IOBuffer* buffer,
72                                  const size_t length,
73                                  const unsigned int timeout,
74                                  const UsbTransferCallback& callback) OVERRIDE;
75
76   virtual void IsochronousTransfer(
77       const UsbEndpointDirection direction,
78       const uint8 endpoint,
79       net::IOBuffer* buffer,
80       const size_t length,
81       const unsigned int packets,
82       const unsigned int packet_length,
83       const unsigned int timeout,
84       const UsbTransferCallback& callback) OVERRIDE;
85
86   PlatformUsbDeviceHandle handle() const { return handle_; }
87
88  protected:
89   friend class UsbDeviceImpl;
90
91   // This constructor is called by UsbDevice.
92   UsbDeviceHandleImpl(scoped_refptr<UsbContext> context,
93                       UsbDeviceImpl* device,
94                       PlatformUsbDeviceHandle handle,
95                       scoped_refptr<UsbConfigDescriptor> interfaces);
96
97   virtual ~UsbDeviceHandleImpl();
98
99  private:
100   friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
101
102   class InterfaceClaimer;
103   struct Transfer;
104
105   // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
106   // SetInterfaceAlternateSetting.
107   void RefreshEndpointMap();
108
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);
113
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,
120                       const size_t length,
121                       scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
122                       const UsbTransferCallback& callback);
123
124   // Invokes the callbacks associated with a given transfer, and removes it from
125   // the in-flight transfer set.
126   void TransferComplete(PlatformUsbTransferHandle transfer);
127
128   bool GetSupportedLanguages();
129   bool GetStringDescriptor(uint8 string_id, base::string16* string);
130
131   // Informs the object to drop internal references.
132   void InternalClose();
133
134   UsbDeviceImpl* device_;
135
136   PlatformUsbDeviceHandle handle_;
137
138   scoped_refptr<UsbConfigDescriptor> interfaces_;
139
140   std::vector<uint16> languages_;
141   std::map<uint8, base::string16> strings_;
142
143   typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
144   ClaimedInterfaceMap claimed_interfaces_;
145
146   typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
147   TransferMap transfers_;
148
149   // A map from endpoints to interfaces
150   typedef std::map<int, int> EndpointMap;
151   EndpointMap endpoint_map_;
152
153   // Retain the UsbContext so that the platform context will not be destroyed
154   // before this handle.
155   scoped_refptr<UsbContext> context_;
156
157   base::ThreadChecker thread_checker_;
158
159   DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
160 };
161
162 }  // namespace usb_service
163
164 #endif  // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_