Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(953)

Side by Side Diff: components/usb_service/usb_device_handle.h

Issue 278633003: Extracted UsbDeviceHandle as interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/usb_service.gypi ('k') | components/usb_service/usb_device_handle.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ 5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ 6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/threading/thread_checker.h" 14 #include "base/threading/thread_checker.h"
15 #include "components/usb_service/usb_interface.h" 15 #include "components/usb_service/usb_interface.h"
16 #include "components/usb_service/usb_service_export.h" 16 #include "components/usb_service/usb_service_export.h"
17 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
18 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 { 19 namespace usb_service {
28 20
29 class UsbContext;
30 class UsbConfigDescriptor;
31 class UsbDevice; 21 class UsbDevice;
32 22
33 typedef libusb_device_handle* PlatformUsbDeviceHandle;
34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
35 typedef libusb_transfer* PlatformUsbTransferHandle;
36
37 enum UsbTransferStatus { 23 enum UsbTransferStatus {
38 USB_TRANSFER_COMPLETED = 0, 24 USB_TRANSFER_COMPLETED = 0,
39 USB_TRANSFER_ERROR, 25 USB_TRANSFER_ERROR,
40 USB_TRANSFER_TIMEOUT, 26 USB_TRANSFER_TIMEOUT,
41 USB_TRANSFER_CANCELLED, 27 USB_TRANSFER_CANCELLED,
42 USB_TRANSFER_STALLED, 28 USB_TRANSFER_STALLED,
43 USB_TRANSFER_DISCONNECT, 29 USB_TRANSFER_DISCONNECT,
44 USB_TRANSFER_OVERFLOW, 30 USB_TRANSFER_OVERFLOW,
45 USB_TRANSFER_LENGTH_SHORT, 31 USB_TRANSFER_LENGTH_SHORT,
46 }; 32 };
47 33
48 typedef base::Callback< 34 typedef base::Callback<
49 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)> 35 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>
50 UsbTransferCallback; 36 UsbTransferCallback;
51 37
52 // UsbDeviceHandle class provides basic I/O related functionalities. 38 // UsbDeviceHandle class provides basic I/O related functionalities.
53 class USB_SERVICE_EXPORT UsbDeviceHandle 39 class USB_SERVICE_EXPORT UsbDeviceHandle
54 : public base::RefCountedThreadSafe<UsbDeviceHandle> { 40 : public base::RefCountedThreadSafe<UsbDeviceHandle> {
55 public: 41 public:
56 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; 42 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
57 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; 43 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
58 44
59 scoped_refptr<UsbDevice> device() const; 45 virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
60 PlatformUsbDeviceHandle handle() const { return handle_; }
61 46
62 // Notifies UsbDevice to drop the reference of this object; cancels all the 47 // Notifies UsbDevice to drop the reference of this object; cancels all the
63 // flying transfers. 48 // flying transfers.
64 // It is possible that the object has no other reference after this call. So 49 // 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. 50 // if it is called using a raw pointer, it could be invalidated.
66 // The platform device handle will be closed when UsbDeviceHandle destructs. 51 // The platform device handle will be closed when UsbDeviceHandle destructs.
67 virtual void Close(); 52 virtual void Close() = 0;
68 53
69 // Device manipulation operations. These methods are blocking and must be 54 // Device manipulation operations. These methods are blocking and must be
70 // called on FILE thread. 55 // called on FILE thread.
71 virtual bool ClaimInterface(const int interface_number); 56 virtual bool ClaimInterface(const int interface_number) = 0;
72 virtual bool ReleaseInterface(const int interface_number); 57 virtual bool ReleaseInterface(const int interface_number) = 0;
73 virtual bool SetInterfaceAlternateSetting(const int interface_number, 58 virtual bool SetInterfaceAlternateSetting(const int interface_number,
74 const int alternate_setting); 59 const int alternate_setting) = 0;
75 virtual bool ResetDevice(); 60 virtual bool ResetDevice() = 0;
76 virtual bool GetSerial(base::string16* serial); 61 virtual bool GetSerial(base::string16* serial) = 0;
77 62
78 // Async IO. Can be called on any thread. 63 // Async IO. Can be called on any thread.
79 virtual void ControlTransfer(const UsbEndpointDirection direction, 64 virtual void ControlTransfer(const UsbEndpointDirection direction,
80 const TransferRequestType request_type, 65 const TransferRequestType request_type,
81 const TransferRecipient recipient, 66 const TransferRecipient recipient,
82 const uint8 request, 67 const uint8 request,
83 const uint16 value, 68 const uint16 value,
84 const uint16 index, 69 const uint16 index,
85 net::IOBuffer* buffer, 70 net::IOBuffer* buffer,
86 const size_t length, 71 const size_t length,
87 const unsigned int timeout, 72 const unsigned int timeout,
88 const UsbTransferCallback& callback); 73 const UsbTransferCallback& callback) = 0;
89 74
90 virtual void BulkTransfer(const UsbEndpointDirection direction, 75 virtual void BulkTransfer(const UsbEndpointDirection direction,
91 const uint8 endpoint, 76 const uint8 endpoint,
92 net::IOBuffer* buffer, 77 net::IOBuffer* buffer,
93 const size_t length, 78 const size_t length,
94 const unsigned int timeout, 79 const unsigned int timeout,
95 const UsbTransferCallback& callback); 80 const UsbTransferCallback& callback) = 0;
96 81
97 virtual void InterruptTransfer(const UsbEndpointDirection direction, 82 virtual void InterruptTransfer(const UsbEndpointDirection direction,
98 const uint8 endpoint, 83 const uint8 endpoint,
99 net::IOBuffer* buffer, 84 net::IOBuffer* buffer,
100 const size_t length, 85 const size_t length,
101 const unsigned int timeout, 86 const unsigned int timeout,
102 const UsbTransferCallback& callback); 87 const UsbTransferCallback& callback) = 0;
103 88
104 virtual void IsochronousTransfer(const UsbEndpointDirection direction, 89 virtual void IsochronousTransfer(const UsbEndpointDirection direction,
105 const uint8 endpoint, 90 const uint8 endpoint,
106 net::IOBuffer* buffer, 91 net::IOBuffer* buffer,
107 const size_t length, 92 const size_t length,
108 const unsigned int packets, 93 const unsigned int packets,
109 const unsigned int packet_length, 94 const unsigned int packet_length,
110 const unsigned int timeout, 95 const unsigned int timeout,
111 const UsbTransferCallback& callback); 96 const UsbTransferCallback& callback) = 0;
112 97
113 protected: 98 protected:
114 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; 99 friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
115 friend class UsbDeviceImpl;
116 100
117 // This constructor is called by UsbDevice. 101 UsbDeviceHandle() {};
118 UsbDeviceHandle(scoped_refptr<UsbContext> context,
119 UsbDevice* device,
120 PlatformUsbDeviceHandle handle,
121 scoped_refptr<UsbConfigDescriptor> interfaces);
122 102
123 // This constructor variant is for use in testing only. 103 virtual ~UsbDeviceHandle() {};
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 104
181 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); 105 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
182 }; 106 };
183 107
184 } // namespace usb_service 108 } // namespace usb_service
185 109
186 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ 110 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_
OLDNEW
« no previous file with comments | « components/usb_service.gypi ('k') | components/usb_service/usb_device_handle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698