Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_USB_USB_DEVICE_H_ | 5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/port.h" |
| 13 #include "base/threading/thread_checker.h" | |
| 13 #include "chrome/browser/usb/usb_interface.h" | 14 #include "chrome/browser/usb/usb_interface.h" |
| 14 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 | 17 |
| 17 struct libusb_device; | 18 struct libusb_device; |
| 18 struct libusb_device_handle; | 19 struct libusb_device_handle; |
| 19 struct libusb_iso_packet_descriptor; | 20 struct libusb_iso_packet_descriptor; |
| 20 struct libusb_transfer; | 21 struct libusb_transfer; |
| 21 | 22 |
| 22 typedef libusb_device* PlatformUsbDevice; | 23 typedef libusb_device* PlatformUsbDevice; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 34 USB_TRANSFER_COMPLETED = 0, | 35 USB_TRANSFER_COMPLETED = 0, |
| 35 USB_TRANSFER_ERROR, | 36 USB_TRANSFER_ERROR, |
| 36 USB_TRANSFER_TIMEOUT, | 37 USB_TRANSFER_TIMEOUT, |
| 37 USB_TRANSFER_CANCELLED, | 38 USB_TRANSFER_CANCELLED, |
| 38 USB_TRANSFER_STALLED, | 39 USB_TRANSFER_STALLED, |
| 39 USB_TRANSFER_DISCONNECT, | 40 USB_TRANSFER_DISCONNECT, |
| 40 USB_TRANSFER_OVERFLOW, | 41 USB_TRANSFER_OVERFLOW, |
| 41 USB_TRANSFER_LENGTH_SHORT, | 42 USB_TRANSFER_LENGTH_SHORT, |
| 42 }; | 43 }; |
| 43 | 44 |
| 44 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, | 45 typedef base::Callback<void( |
| 46 UsbTransferStatus, | |
| 47 scoped_refptr<net::IOBuffer>, | |
| 45 size_t)> UsbTransferCallback; | 48 size_t)> UsbTransferCallback; |
| 46 typedef base::Callback<void(bool)> UsbInterfaceCallback; | 49 typedef base::Callback<void(bool success)> UsbInterfaceCallback; |
| 50 typedef base::Callback<void(bool success)> UsbResetDeviceCallback; | |
| 47 | 51 |
| 48 // A UsbDevice wraps the platform's underlying representation of what a USB | 52 // A UsbDevice wraps the platform's underlying representation of what a USB |
| 49 // device actually is, and provides accessors for performing many of the | 53 // device actually is, and provides accessors for performing many of the |
| 50 // standard USB operations. | 54 // standard USB operations. |
| 51 class UsbDevice : public base::RefCounted<UsbDevice> { | 55 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { |
| 52 public: | 56 public: |
| 53 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | 57 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
| 54 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 58 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| 55 | 59 |
| 56 // Usually you will not want to directly create a UsbDevice, favoring to let | 60 int device() const { return device_; } |
| 57 // the UsbService take care of the logistics of getting a platform device | 61 PlatformUsbDeviceHandle handle() const { return handle_; } |
| 58 // handle and handling events for it. | |
| 59 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); | |
| 60 | |
| 61 PlatformUsbDeviceHandle handle() { return handle_; } | |
| 62 | 62 |
| 63 // Close the USB device and release the underlying platform device. |callback| | 63 // Close the USB device and release the underlying platform device. |callback| |
| 64 // is invoked after the device has been closed. | 64 // is invoked after the device has been closed. |
| 65 virtual void Close(const base::Callback<void()>& callback); | 65 virtual void Close(const base::Callback<void()>& callback); |
| 66 | 66 |
| 67 // FILE thread: | |
| 67 virtual void ListInterfaces(UsbConfigDescriptor* config, | 68 virtual void ListInterfaces(UsbConfigDescriptor* config, |
| 68 const UsbInterfaceCallback& callback); | 69 const UsbInterfaceCallback& callback); |
| 69 | |
| 70 virtual void ClaimInterface(const int interface_number, | 70 virtual void ClaimInterface(const int interface_number, |
| 71 const UsbInterfaceCallback& callback); | 71 const UsbInterfaceCallback& callback); |
| 72 | |
| 73 virtual void ReleaseInterface(const int interface_number, | 72 virtual void ReleaseInterface(const int interface_number, |
| 74 const UsbInterfaceCallback& callback); | 73 const UsbInterfaceCallback& callback); |
| 75 | |
| 76 virtual void SetInterfaceAlternateSetting( | 74 virtual void SetInterfaceAlternateSetting( |
| 77 const int interface_number, | 75 const int interface_number, |
| 78 const int alternate_setting, | 76 const int alternate_setting, |
| 79 const UsbInterfaceCallback& callback); | 77 const UsbInterfaceCallback& callback); |
| 80 | 78 |
| 79 // Async IO. Can be called on any thread. | |
| 81 virtual void ControlTransfer(const UsbEndpointDirection direction, | 80 virtual void ControlTransfer(const UsbEndpointDirection direction, |
| 82 const TransferRequestType request_type, | 81 const TransferRequestType request_type, |
| 83 const TransferRecipient recipient, | 82 const TransferRecipient recipient, |
| 84 const uint8 request, | 83 const uint8 request, |
| 85 const uint16 value, | 84 const uint16 value, |
| 86 const uint16 index, | 85 const uint16 index, |
| 87 net::IOBuffer* buffer, | 86 net::IOBuffer* buffer, |
| 88 const size_t length, | 87 const size_t length, |
| 89 const unsigned int timeout, | 88 const unsigned int timeout, |
| 90 const UsbTransferCallback& callback); | 89 const UsbTransferCallback& callback); |
| 91 | |
| 92 virtual void BulkTransfer(const UsbEndpointDirection direction, | 90 virtual void BulkTransfer(const UsbEndpointDirection direction, |
| 93 const uint8 endpoint, | 91 const uint8 endpoint, |
| 94 net::IOBuffer* buffer, | 92 net::IOBuffer* buffer, |
| 95 const size_t length, | 93 const size_t length, |
| 96 const unsigned int timeout, | 94 const unsigned int timeout, |
| 97 const UsbTransferCallback& callback); | 95 const UsbTransferCallback& callback); |
| 98 | |
| 99 virtual void InterruptTransfer(const UsbEndpointDirection direction, | 96 virtual void InterruptTransfer(const UsbEndpointDirection direction, |
| 100 const uint8 endpoint, | 97 const uint8 endpoint, |
| 101 net::IOBuffer* buffer, | 98 net::IOBuffer* buffer, |
| 102 const size_t length, | 99 const size_t length, |
| 103 const unsigned int timeout, | 100 const unsigned int timeout, |
| 104 const UsbTransferCallback& callback); | 101 const UsbTransferCallback& callback); |
| 105 | |
| 106 virtual void IsochronousTransfer(const UsbEndpointDirection direction, | 102 virtual void IsochronousTransfer(const UsbEndpointDirection direction, |
| 107 const uint8 endpoint, | 103 const uint8 endpoint, |
| 108 net::IOBuffer* buffer, | 104 net::IOBuffer* buffer, |
| 109 const size_t length, | 105 const size_t length, |
| 110 const unsigned int packets, | 106 const unsigned int packets, |
| 111 const unsigned int packet_length, | 107 const unsigned int packet_length, |
| 112 const unsigned int timeout, | 108 const unsigned int timeout, |
| 113 const UsbTransferCallback& callback); | 109 const UsbTransferCallback& callback); |
| 114 | 110 |
| 115 virtual void ResetDevice(const base::Callback<void(bool)>& callback); | 111 // FILE thread: |
| 116 | 112 virtual void ResetDevice(const UsbResetDeviceCallback& callback); |
| 117 // Normal code should not call this function. It is called by the platform's | |
| 118 // callback mechanism in such a way that it cannot be made private. Invokes | |
| 119 // the callbacks associated with a given transfer, and removes it from the | |
| 120 // in-flight transfer set. | |
| 121 void TransferComplete(PlatformUsbTransferHandle transfer); | |
| 122 | 113 |
| 123 protected: | 114 protected: |
| 124 // This constructor variant is for use in testing only. | 115 // This constructor variant is for use in testing only. |
| 125 UsbDevice(); | 116 UsbDevice(); |
| 126 | 117 |
| 127 friend class base::RefCounted<UsbDevice>; | 118 friend class base::RefCountedThreadSafe<UsbDevice>; |
| 128 virtual ~UsbDevice(); | 119 virtual ~UsbDevice(); |
| 129 | 120 |
| 130 private: | 121 private: |
| 131 struct Transfer { | 122 // UsbDeviceHandle should only be created from UsbDevice class. |
| 132 Transfer(); | 123 UsbDevice(UsbService* service, int device, PlatformUsbDeviceHandle handle); |
| 133 ~Transfer(); | |
| 134 | 124 |
| 135 UsbTransferType transfer_type; | 125 friend class UsbDeviceStub; |
| 136 scoped_refptr<net::IOBuffer> buffer; | |
| 137 size_t length; | |
| 138 UsbTransferCallback callback; | |
| 139 }; | |
| 140 | 126 |
| 141 // Checks that the device has not yet been closed. | 127 static void HandleTransferCompletionFileThread( |
| 142 void CheckDevice(); | 128 PlatformUsbTransferHandle transfer); |
| 129 | |
| 130 static void API_CALL HandleTransferCompletion( | |
|
pfeldman
2013/07/22 18:23:13
Why API_CALL?
Bei Zhang
2013/07/23 17:58:44
This callback needs __stdcall on windows.
On 2013
| |
| 131 PlatformUsbTransferHandle transfer); | |
| 132 | |
| 133 void TransferComplete(PlatformUsbTransferHandle transfer); | |
| 134 | |
| 135 void InternalClose(); | |
| 143 | 136 |
| 144 // Submits a transfer and starts tracking it. Retains the buffer and copies | 137 // Submits a transfer and starts tracking it. Retains the buffer and copies |
| 145 // the completion callback until the transfer finishes, whereupon it invokes | 138 // the completion callback until the transfer finishes, whereupon it invokes |
| 146 // the callback then releases the buffer. | 139 // the callback then releases the buffer. |
| 147 void SubmitTransfer(PlatformUsbTransferHandle handle, | 140 void SubmitTransfer(PlatformUsbTransferHandle handle, |
| 148 UsbTransferType transfer_type, | 141 UsbTransferType transfer_type, |
| 149 net::IOBuffer* buffer, | 142 net::IOBuffer* buffer, |
| 150 const size_t length, | 143 const size_t length, |
| 151 const UsbTransferCallback& callback); | 144 const UsbTransferCallback& callback); |
| 152 | 145 |
| 153 // The UsbService isn't referenced here to prevent a dependency cycle between | 146 // The UsbService isn't referenced here to prevent a dependency cycle between |
| 154 // the service and the devices. Since a service owns every device, and is | 147 // the service and the devices. Since a service owns every device, and is |
| 155 // responsible for its destruction, there is no case where a UsbDevice can | 148 // responsible for its destruction, there is no case where a UsbDevice can |
| 156 // have outlived its originating UsbService. | 149 // have outlived its originating UsbService. |
| 157 UsbService* const service_; | 150 UsbService* const service_; |
| 151 const int device_; | |
| 158 PlatformUsbDeviceHandle handle_; | 152 PlatformUsbDeviceHandle handle_; |
| 153 base::ThreadChecker thread_checker_; | |
| 159 | 154 |
| 160 // transfers_ tracks all in-flight transfers associated with this device, | 155 // transfers_ tracks all in-flight transfers associated with this device, |
| 161 // allowing the device to retain the buffer and callback associated with a | 156 // allowing the device to retain the buffer and callback associated with a |
| 162 // transfer until such time that it completes. It is protected by lock_. | 157 // transfer until such time that it completes. |
| 163 base::Lock lock_; | 158 struct TransferInfo; |
| 164 std::map<PlatformUsbTransferHandle, Transfer> transfers_; | 159 std::map<PlatformUsbTransferHandle, TransferInfo> transfers_; |
| 165 | 160 |
| 166 DISALLOW_COPY_AND_ASSIGN(UsbDevice); | 161 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
| 167 }; | 162 }; |
| 168 | 163 |
| 169 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ | 164 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ |
| OLD | NEW |