OLD | NEW |
| (Empty) |
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 #include "third_party/libusb/src/libusb/libusb.h" | |
19 | |
20 namespace base { | |
21 class SingleThreadTaskRunner; | |
22 } | |
23 | |
24 namespace usb_service { | |
25 | |
26 class UsbContext; | |
27 class UsbConfigDescriptor; | |
28 class UsbDeviceImpl; | |
29 | |
30 typedef libusb_device_handle* PlatformUsbDeviceHandle; | |
31 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | |
32 typedef libusb_transfer* PlatformUsbTransferHandle; | |
33 | |
34 // UsbDeviceHandle class provides basic I/O related functionalities. | |
35 class UsbDeviceHandleImpl : public UsbDeviceHandle { | |
36 public: | |
37 virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE; | |
38 virtual void Close() OVERRIDE; | |
39 virtual bool ClaimInterface(const int interface_number) OVERRIDE; | |
40 virtual bool ReleaseInterface(const int interface_number) OVERRIDE; | |
41 virtual bool SetInterfaceAlternateSetting( | |
42 const int interface_number, | |
43 const int alternate_setting) OVERRIDE; | |
44 virtual bool ResetDevice() OVERRIDE; | |
45 virtual bool GetManufacturer(base::string16* manufacturer) OVERRIDE; | |
46 virtual bool GetProduct(base::string16* product) OVERRIDE; | |
47 virtual bool GetSerial(base::string16* serial) OVERRIDE; | |
48 virtual void ControlTransfer(const UsbEndpointDirection direction, | |
49 const TransferRequestType request_type, | |
50 const TransferRecipient recipient, | |
51 const uint8 request, | |
52 const uint16 value, | |
53 const uint16 index, | |
54 net::IOBuffer* buffer, | |
55 const size_t length, | |
56 const unsigned int timeout, | |
57 const UsbTransferCallback& callback) OVERRIDE; | |
58 | |
59 virtual void BulkTransfer(const UsbEndpointDirection direction, | |
60 const uint8 endpoint, | |
61 net::IOBuffer* buffer, | |
62 const size_t length, | |
63 const unsigned int timeout, | |
64 const UsbTransferCallback& callback) OVERRIDE; | |
65 | |
66 virtual void InterruptTransfer(const UsbEndpointDirection direction, | |
67 const uint8 endpoint, | |
68 net::IOBuffer* buffer, | |
69 const size_t length, | |
70 const unsigned int timeout, | |
71 const UsbTransferCallback& callback) OVERRIDE; | |
72 | |
73 virtual void IsochronousTransfer( | |
74 const UsbEndpointDirection direction, | |
75 const uint8 endpoint, | |
76 net::IOBuffer* buffer, | |
77 const size_t length, | |
78 const unsigned int packets, | |
79 const unsigned int packet_length, | |
80 const unsigned int timeout, | |
81 const UsbTransferCallback& callback) OVERRIDE; | |
82 | |
83 PlatformUsbDeviceHandle handle() const { return handle_; } | |
84 | |
85 protected: | |
86 friend class UsbDeviceImpl; | |
87 | |
88 // This constructor is called by UsbDevice. | |
89 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, | |
90 UsbDeviceImpl* device, | |
91 PlatformUsbDeviceHandle handle, | |
92 scoped_refptr<UsbConfigDescriptor> interfaces); | |
93 | |
94 virtual ~UsbDeviceHandleImpl(); | |
95 | |
96 private: | |
97 class InterfaceClaimer; | |
98 struct Transfer; | |
99 | |
100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | |
101 // SetInterfaceAlternateSetting. | |
102 void RefreshEndpointMap(); | |
103 | |
104 // Look up the claimed interface by endpoint. Return NULL if the interface | |
105 // of the endpoint is not found. | |
106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( | |
107 unsigned char endpoint); | |
108 | |
109 // If the device's task runner is on the current thread then the transfer will | |
110 // be submitted directly, otherwise a task to do so it posted. The callback | |
111 // will be called on the current message loop of the thread where this | |
112 // function was called. | |
113 void PostOrSubmitTransfer(PlatformUsbTransferHandle handle, | |
114 UsbTransferType transfer_type, | |
115 net::IOBuffer* buffer, | |
116 size_t length, | |
117 const UsbTransferCallback& callback); | |
118 | |
119 // Submits a transfer and starts tracking it. Retains the buffer and copies | |
120 // the completion callback until the transfer finishes, whereupon it invokes | |
121 // the callback then releases the buffer. | |
122 void SubmitTransfer(PlatformUsbTransferHandle handle, | |
123 UsbTransferType transfer_type, | |
124 net::IOBuffer* buffer, | |
125 const size_t length, | |
126 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
127 const UsbTransferCallback& callback); | |
128 | |
129 static void LIBUSB_CALL | |
130 PlatformTransferCallback(PlatformUsbTransferHandle handle); | |
131 | |
132 // Invokes the callbacks associated with a given transfer, and removes it from | |
133 // the in-flight transfer set. | |
134 void CompleteTransfer(PlatformUsbTransferHandle transfer); | |
135 | |
136 bool GetSupportedLanguages(); | |
137 bool GetStringDescriptor(uint8 string_id, base::string16* string); | |
138 | |
139 // Informs the object to drop internal references. | |
140 void InternalClose(); | |
141 | |
142 UsbDeviceImpl* device_; | |
143 | |
144 PlatformUsbDeviceHandle handle_; | |
145 | |
146 scoped_refptr<UsbConfigDescriptor> interfaces_; | |
147 | |
148 std::vector<uint16> languages_; | |
149 std::map<uint8, base::string16> strings_; | |
150 | |
151 typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap; | |
152 ClaimedInterfaceMap claimed_interfaces_; | |
153 | |
154 typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap; | |
155 TransferMap transfers_; | |
156 | |
157 // A map from endpoints to interfaces | |
158 typedef std::map<int, int> EndpointMap; | |
159 EndpointMap endpoint_map_; | |
160 | |
161 // Retain the UsbContext so that the platform context will not be destroyed | |
162 // before this handle. | |
163 scoped_refptr<UsbContext> context_; | |
164 | |
165 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
166 | |
167 base::ThreadChecker thread_checker_; | |
168 | |
169 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); | |
170 }; | |
171 | |
172 } // namespace usb_service | |
173 | |
174 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ | |
OLD | NEW |