| OLD | NEW |
| 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_IMPL_H_ |
| 6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ | 6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_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_device_handle.h" |
| 15 #include "components/usb_service/usb_interface.h" | 16 #include "components/usb_service/usb_interface.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; | 19 struct libusb_device_handle; |
| 20 struct libusb_iso_packet_descriptor; | 20 struct libusb_iso_packet_descriptor; |
| 21 struct libusb_transfer; | 21 struct libusb_transfer; |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 class MessageLoopProxy; | 24 class MessageLoopProxy; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace usb_service { | 27 namespace usb_service { |
| 28 | 28 |
| 29 class UsbContext; | 29 class UsbContext; |
| 30 class UsbConfigDescriptor; | 30 class UsbConfigDescriptor; |
| 31 class UsbDevice; | 31 class UsbDeviceImpl; |
| 32 | 32 |
| 33 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 33 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
| 34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | 34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
| 35 typedef libusb_transfer* PlatformUsbTransferHandle; | 35 typedef libusb_transfer* PlatformUsbTransferHandle; |
| 36 | 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. | 37 // UsbDeviceHandle class provides basic I/O related functionalities. |
| 53 class USB_SERVICE_EXPORT UsbDeviceHandle | 38 class UsbDeviceHandleImpl : public UsbDeviceHandle { |
| 54 : public base::RefCountedThreadSafe<UsbDeviceHandle> { | |
| 55 public: | 39 public: |
| 56 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | 40 virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE; |
| 57 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 41 virtual void Close() OVERRIDE; |
| 58 | 42 virtual bool ClaimInterface(const int interface_number) OVERRIDE; |
| 59 scoped_refptr<UsbDevice> device() const; | 43 virtual bool ReleaseInterface(const int interface_number) OVERRIDE; |
| 60 PlatformUsbDeviceHandle handle() const { return handle_; } | 44 virtual bool SetInterfaceAlternateSetting( |
| 61 | 45 const int interface_number, |
| 62 // Notifies UsbDevice to drop the reference of this object; cancels all the | 46 const int alternate_setting) OVERRIDE; |
| 63 // flying transfers. | 47 virtual bool ResetDevice() OVERRIDE; |
| 64 // It is possible that the object has no other reference after this call. So | 48 virtual bool GetSerial(base::string16* serial) OVERRIDE; |
| 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, | 49 virtual void ControlTransfer(const UsbEndpointDirection direction, |
| 80 const TransferRequestType request_type, | 50 const TransferRequestType request_type, |
| 81 const TransferRecipient recipient, | 51 const TransferRecipient recipient, |
| 82 const uint8 request, | 52 const uint8 request, |
| 83 const uint16 value, | 53 const uint16 value, |
| 84 const uint16 index, | 54 const uint16 index, |
| 85 net::IOBuffer* buffer, | 55 net::IOBuffer* buffer, |
| 86 const size_t length, | 56 const size_t length, |
| 87 const unsigned int timeout, | 57 const unsigned int timeout, |
| 88 const UsbTransferCallback& callback); | 58 const UsbTransferCallback& callback) OVERRIDE; |
| 89 | 59 |
| 90 virtual void BulkTransfer(const UsbEndpointDirection direction, | 60 virtual void BulkTransfer(const UsbEndpointDirection direction, |
| 91 const uint8 endpoint, | 61 const uint8 endpoint, |
| 92 net::IOBuffer* buffer, | 62 net::IOBuffer* buffer, |
| 93 const size_t length, | 63 const size_t length, |
| 94 const unsigned int timeout, | 64 const unsigned int timeout, |
| 95 const UsbTransferCallback& callback); | 65 const UsbTransferCallback& callback) OVERRIDE; |
| 96 | 66 |
| 97 virtual void InterruptTransfer(const UsbEndpointDirection direction, | 67 virtual void InterruptTransfer(const UsbEndpointDirection direction, |
| 98 const uint8 endpoint, | 68 const uint8 endpoint, |
| 99 net::IOBuffer* buffer, | 69 net::IOBuffer* buffer, |
| 100 const size_t length, | 70 const size_t length, |
| 101 const unsigned int timeout, | 71 const unsigned int timeout, |
| 102 const UsbTransferCallback& callback); | 72 const UsbTransferCallback& callback) OVERRIDE; |
| 103 | 73 |
| 104 virtual void IsochronousTransfer(const UsbEndpointDirection direction, | 74 virtual void IsochronousTransfer( |
| 105 const uint8 endpoint, | 75 const UsbEndpointDirection direction, |
| 106 net::IOBuffer* buffer, | 76 const uint8 endpoint, |
| 107 const size_t length, | 77 net::IOBuffer* buffer, |
| 108 const unsigned int packets, | 78 const size_t length, |
| 109 const unsigned int packet_length, | 79 const unsigned int packets, |
| 110 const unsigned int timeout, | 80 const unsigned int packet_length, |
| 111 const UsbTransferCallback& callback); | 81 const unsigned int timeout, |
| 82 const UsbTransferCallback& callback) OVERRIDE; |
| 83 |
| 84 PlatformUsbDeviceHandle handle() const { return handle_; } |
| 112 | 85 |
| 113 protected: | 86 protected: |
| 114 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; | |
| 115 friend class UsbDeviceImpl; | 87 friend class UsbDeviceImpl; |
| 116 | 88 |
| 117 // This constructor is called by UsbDevice. | 89 // This constructor is called by UsbDevice. |
| 118 UsbDeviceHandle(scoped_refptr<UsbContext> context, | 90 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, |
| 119 UsbDevice* device, | 91 UsbDeviceImpl* device, |
| 120 PlatformUsbDeviceHandle handle, | 92 PlatformUsbDeviceHandle handle, |
| 121 scoped_refptr<UsbConfigDescriptor> interfaces); | 93 scoped_refptr<UsbConfigDescriptor> interfaces); |
| 122 | 94 |
| 123 // This constructor variant is for use in testing only. | 95 virtual ~UsbDeviceHandleImpl(); |
| 124 UsbDeviceHandle(); | |
| 125 virtual ~UsbDeviceHandle(); | |
| 126 | |
| 127 UsbDevice* device_; | |
| 128 | 96 |
| 129 private: | 97 private: |
| 130 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle); | 98 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle); |
| 131 | 99 |
| 132 class InterfaceClaimer; | 100 class InterfaceClaimer; |
| 133 struct Transfer; | 101 struct Transfer; |
| 134 | 102 |
| 135 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | 103 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and |
| 136 // SetInterfaceAlternateSetting. | 104 // SetInterfaceAlternateSetting. |
| 137 void RefreshEndpointMap(); | 105 void RefreshEndpointMap(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 151 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 152 const UsbTransferCallback& callback); | 120 const UsbTransferCallback& callback); |
| 153 | 121 |
| 154 // Invokes the callbacks associated with a given transfer, and removes it from | 122 // Invokes the callbacks associated with a given transfer, and removes it from |
| 155 // the in-flight transfer set. | 123 // the in-flight transfer set. |
| 156 void TransferComplete(PlatformUsbTransferHandle transfer); | 124 void TransferComplete(PlatformUsbTransferHandle transfer); |
| 157 | 125 |
| 158 // Informs the object to drop internal references. | 126 // Informs the object to drop internal references. |
| 159 void InternalClose(); | 127 void InternalClose(); |
| 160 | 128 |
| 129 UsbDeviceImpl* device_; |
| 130 |
| 161 PlatformUsbDeviceHandle handle_; | 131 PlatformUsbDeviceHandle handle_; |
| 162 | 132 |
| 163 scoped_refptr<UsbConfigDescriptor> interfaces_; | 133 scoped_refptr<UsbConfigDescriptor> interfaces_; |
| 164 | 134 |
| 165 typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap; | 135 typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap; |
| 166 ClaimedInterfaceMap claimed_interfaces_; | 136 ClaimedInterfaceMap claimed_interfaces_; |
| 167 | 137 |
| 168 typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap; | 138 typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap; |
| 169 TransferMap transfers_; | 139 TransferMap transfers_; |
| 170 | 140 |
| 171 // A map from endpoints to interfaces | 141 // A map from endpoints to interfaces |
| 172 typedef std::map<int, int> EndpointMap; | 142 typedef std::map<int, int> EndpointMap; |
| 173 EndpointMap endpoint_map_; | 143 EndpointMap endpoint_map_; |
| 174 | 144 |
| 175 // Retain the UsbContext so that the platform context will not be destroyed | 145 // Retain the UsbContext so that the platform context will not be destroyed |
| 176 // before this handle. | 146 // before this handle. |
| 177 scoped_refptr<UsbContext> context_; | 147 scoped_refptr<UsbContext> context_; |
| 178 | 148 |
| 179 base::ThreadChecker thread_checker_; | 149 base::ThreadChecker thread_checker_; |
| 180 | 150 |
| 181 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); | 151 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); |
| 182 }; | 152 }; |
| 183 | 153 |
| 184 } // namespace usb_service | 154 } // namespace usb_service |
| 185 | 155 |
| 186 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ | 156 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ |
| OLD | NEW |