| 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 DEVICE_USB_USB_DEVICE_HANDLE_H_ | 5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_H_ |
| 6 #define DEVICE_USB_USB_DEVICE_HANDLE_H_ | 6 #define DEVICE_USB_USB_DEVICE_HANDLE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "device/usb/public/interfaces/device.mojom.h" |
| 19 #include "device/usb/usb_descriptors.h" | 20 #include "device/usb/usb_descriptors.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 class IOBuffer; | 23 class IOBuffer; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace device { | 26 namespace device { |
| 26 | 27 |
| 27 class UsbDevice; | 28 class UsbDevice; |
| 28 | 29 |
| 29 enum UsbTransferStatus { | 30 using UsbTransferStatus = mojom::UsbTransferStatus; |
| 30 USB_TRANSFER_COMPLETED = 0, | 31 using UsbControlTransferType = mojom::UsbControlTransferType; |
| 31 USB_TRANSFER_ERROR, | 32 using UsbControlTransferRecipient = mojom::UsbControlTransferRecipient; |
| 32 USB_TRANSFER_TIMEOUT, | |
| 33 USB_TRANSFER_CANCELLED, | |
| 34 USB_TRANSFER_STALLED, | |
| 35 USB_TRANSFER_DISCONNECT, | |
| 36 USB_TRANSFER_OVERFLOW, | |
| 37 USB_TRANSFER_LENGTH_SHORT, | |
| 38 }; | |
| 39 | 33 |
| 40 // UsbDeviceHandle class provides basic I/O related functionalities. | 34 // UsbDeviceHandle class provides basic I/O related functionalities. |
| 41 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { | 35 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { |
| 42 public: | 36 public: |
| 43 struct IsochronousPacket { | 37 struct IsochronousPacket { |
| 44 uint32_t length; | 38 uint32_t length; |
| 45 uint32_t transferred_length; | 39 uint32_t transferred_length; |
| 46 UsbTransferStatus status; | 40 UsbTransferStatus status; |
| 47 }; | 41 }; |
| 48 | 42 |
| 49 using ResultCallback = base::Callback<void(bool)>; | 43 using ResultCallback = base::Callback<void(bool)>; |
| 50 using TransferCallback = base::Callback< | 44 using TransferCallback = base::Callback< |
| 51 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>; | 45 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>; |
| 52 using IsochronousTransferCallback = | 46 using IsochronousTransferCallback = |
| 53 base::Callback<void(scoped_refptr<net::IOBuffer>, | 47 base::Callback<void(scoped_refptr<net::IOBuffer>, |
| 54 const std::vector<IsochronousPacket>& packets)>; | 48 const std::vector<IsochronousPacket>& packets)>; |
| 55 | 49 |
| 56 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | |
| 57 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | |
| 58 | |
| 59 virtual scoped_refptr<UsbDevice> GetDevice() const = 0; | 50 virtual scoped_refptr<UsbDevice> GetDevice() const = 0; |
| 60 | 51 |
| 61 // Notifies UsbDevice to drop the reference of this object; cancels all the | 52 // Notifies UsbDevice to drop the reference of this object; cancels all the |
| 62 // flying transfers. | 53 // flying transfers. |
| 63 // It is possible that the object has no other reference after this call. So | 54 // It is possible that the object has no other reference after this call. So |
| 64 // if it is called using a raw pointer, it could be invalidated. | 55 // if it is called using a raw pointer, it could be invalidated. |
| 65 // The platform device handle will be closed when UsbDeviceHandle destructs. | 56 // The platform device handle will be closed when UsbDeviceHandle destructs. |
| 66 virtual void Close() = 0; | 57 virtual void Close() = 0; |
| 67 | 58 |
| 68 // Device manipulation operations. | 59 // Device manipulation operations. |
| 69 virtual void SetConfiguration(int configuration_value, | 60 virtual void SetConfiguration(int configuration_value, |
| 70 const ResultCallback& callback) = 0; | 61 const ResultCallback& callback) = 0; |
| 71 virtual void ClaimInterface(int interface_number, | 62 virtual void ClaimInterface(int interface_number, |
| 72 const ResultCallback& callback) = 0; | 63 const ResultCallback& callback) = 0; |
| 73 virtual void ReleaseInterface(int interface_number, | 64 virtual void ReleaseInterface(int interface_number, |
| 74 const ResultCallback& callback) = 0; | 65 const ResultCallback& callback) = 0; |
| 75 virtual void SetInterfaceAlternateSetting(int interface_number, | 66 virtual void SetInterfaceAlternateSetting(int interface_number, |
| 76 int alternate_setting, | 67 int alternate_setting, |
| 77 const ResultCallback& callback) = 0; | 68 const ResultCallback& callback) = 0; |
| 78 virtual void ResetDevice(const ResultCallback& callback) = 0; | 69 virtual void ResetDevice(const ResultCallback& callback) = 0; |
| 79 virtual void ClearHalt(uint8_t endpoint, const ResultCallback& callback) = 0; | 70 virtual void ClearHalt(uint8_t endpoint, const ResultCallback& callback) = 0; |
| 80 | 71 |
| 81 // The transfer functions may be called from any thread. The provided callback | 72 // The transfer functions may be called from any thread. The provided callback |
| 82 // will be run on the caller's thread. | 73 // will be run on the caller's thread. |
| 83 virtual void ControlTransfer(UsbEndpointDirection direction, | 74 virtual void ControlTransfer(UsbTransferDirection direction, |
| 84 TransferRequestType request_type, | 75 UsbControlTransferType request_type, |
| 85 TransferRecipient recipient, | 76 UsbControlTransferRecipient recipient, |
| 86 uint8_t request, | 77 uint8_t request, |
| 87 uint16_t value, | 78 uint16_t value, |
| 88 uint16_t index, | 79 uint16_t index, |
| 89 scoped_refptr<net::IOBuffer> buffer, | 80 scoped_refptr<net::IOBuffer> buffer, |
| 90 size_t length, | 81 size_t length, |
| 91 unsigned int timeout, | 82 unsigned int timeout, |
| 92 const TransferCallback& callback) = 0; | 83 const TransferCallback& callback) = 0; |
| 93 | 84 |
| 94 virtual void IsochronousTransferIn( | 85 virtual void IsochronousTransferIn( |
| 95 uint8_t endpoint_number, | 86 uint8_t endpoint_number, |
| 96 const std::vector<uint32_t>& packet_lengths, | 87 const std::vector<uint32_t>& packet_lengths, |
| 97 unsigned int timeout, | 88 unsigned int timeout, |
| 98 const IsochronousTransferCallback& callback) = 0; | 89 const IsochronousTransferCallback& callback) = 0; |
| 99 | 90 |
| 100 virtual void IsochronousTransferOut( | 91 virtual void IsochronousTransferOut( |
| 101 uint8_t endpoint_number, | 92 uint8_t endpoint_number, |
| 102 scoped_refptr<net::IOBuffer> buffer, | 93 scoped_refptr<net::IOBuffer> buffer, |
| 103 const std::vector<uint32_t>& packet_lengths, | 94 const std::vector<uint32_t>& packet_lengths, |
| 104 unsigned int timeout, | 95 unsigned int timeout, |
| 105 const IsochronousTransferCallback& callback) = 0; | 96 const IsochronousTransferCallback& callback) = 0; |
| 106 | 97 |
| 107 virtual void GenericTransfer(UsbEndpointDirection direction, | 98 virtual void GenericTransfer(UsbTransferDirection direction, |
| 108 uint8_t endpoint_number, | 99 uint8_t endpoint_number, |
| 109 scoped_refptr<net::IOBuffer> buffer, | 100 scoped_refptr<net::IOBuffer> buffer, |
| 110 size_t length, | 101 size_t length, |
| 111 unsigned int timeout, | 102 unsigned int timeout, |
| 112 const TransferCallback& callback) = 0; | 103 const TransferCallback& callback) = 0; |
| 113 | 104 |
| 114 // Gets the interface containing |endpoint_address|. Returns nullptr if no | 105 // Gets the interface containing |endpoint_address|. Returns nullptr if no |
| 115 // claimed interface contains that endpoint. | 106 // claimed interface contains that endpoint. |
| 116 virtual const UsbInterfaceDescriptor* FindInterfaceByEndpoint( | 107 virtual const UsbInterfaceDescriptor* FindInterfaceByEndpoint( |
| 117 uint8_t endpoint_address) = 0; | 108 uint8_t endpoint_address) = 0; |
| 118 | 109 |
| 119 protected: | 110 protected: |
| 120 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; | 111 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; |
| 121 | 112 |
| 122 UsbDeviceHandle(); | 113 UsbDeviceHandle(); |
| 123 virtual ~UsbDeviceHandle(); | 114 virtual ~UsbDeviceHandle(); |
| 124 | 115 |
| 125 private: | 116 private: |
| 126 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); | 117 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); |
| 127 }; | 118 }; |
| 128 | 119 |
| 129 } // namespace device | 120 } // namespace device |
| 130 | 121 |
| 131 #endif // DEVICE_USB_USB_DEVICE_HANDLE_H_ | 122 #endif // DEVICE_USB_USB_DEVICE_HANDLE_H_ |
| OLD | NEW |