usb_device_handle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_H_
  5. #define SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <map>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "services/device/public/mojom/usb_device.mojom.h"
  13. #include "services/device/usb/usb_descriptors.h"
  14. namespace base {
  15. class RefCountedBytes;
  16. }
  17. namespace device {
  18. class UsbDevice;
  19. // UsbDeviceHandle class provides basic I/O related functionalities.
  20. class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
  21. public:
  22. using ResultCallback = base::OnceCallback<void(bool)>;
  23. using TransferCallback =
  24. base::OnceCallback<void(mojom::UsbTransferStatus,
  25. scoped_refptr<base::RefCountedBytes>,
  26. size_t)>;
  27. using IsochronousTransferCallback = base::OnceCallback<void(
  28. scoped_refptr<base::RefCountedBytes>,
  29. std::vector<mojom::UsbIsochronousPacketPtr> packets)>;
  30. UsbDeviceHandle(const UsbDeviceHandle&) = delete;
  31. UsbDeviceHandle& operator=(const UsbDeviceHandle&) = delete;
  32. virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
  33. // Notifies UsbDevice to drop the reference of this object; cancels all the
  34. // flying transfers.
  35. // It is possible that the object has no other reference after this call. So
  36. // if it is called using a raw pointer, it could be invalidated.
  37. // The platform device handle will be closed when UsbDeviceHandle destructs.
  38. virtual void Close() = 0;
  39. // Device manipulation operations.
  40. virtual void SetConfiguration(int configuration_value,
  41. ResultCallback callback) = 0;
  42. virtual void ClaimInterface(int interface_number,
  43. ResultCallback callback) = 0;
  44. virtual void ReleaseInterface(int interface_number,
  45. ResultCallback callback) = 0;
  46. virtual void SetInterfaceAlternateSetting(int interface_number,
  47. int alternate_setting,
  48. ResultCallback callback) = 0;
  49. virtual void ResetDevice(ResultCallback callback) = 0;
  50. virtual void ClearHalt(mojom::UsbTransferDirection direction,
  51. uint8_t endpoint_number,
  52. ResultCallback callback) = 0;
  53. virtual void ControlTransfer(mojom::UsbTransferDirection direction,
  54. mojom::UsbControlTransferType request_type,
  55. mojom::UsbControlTransferRecipient recipient,
  56. uint8_t request,
  57. uint16_t value,
  58. uint16_t index,
  59. scoped_refptr<base::RefCountedBytes> buffer,
  60. unsigned int timeout,
  61. TransferCallback callback) = 0;
  62. virtual void IsochronousTransferIn(
  63. uint8_t endpoint_number,
  64. const std::vector<uint32_t>& packet_lengths,
  65. unsigned int timeout,
  66. IsochronousTransferCallback callback) = 0;
  67. virtual void IsochronousTransferOut(
  68. uint8_t endpoint_number,
  69. scoped_refptr<base::RefCountedBytes> buffer,
  70. const std::vector<uint32_t>& packet_lengths,
  71. unsigned int timeout,
  72. IsochronousTransferCallback callback) = 0;
  73. virtual void GenericTransfer(mojom::UsbTransferDirection direction,
  74. uint8_t endpoint_number,
  75. scoped_refptr<base::RefCountedBytes> buffer,
  76. unsigned int timeout,
  77. TransferCallback callback) = 0;
  78. // Gets the interface containing |endpoint_address|. Returns nullptr if no
  79. // claimed interface contains that endpoint.
  80. virtual const mojom::UsbInterfaceInfo* FindInterfaceByEndpoint(
  81. uint8_t endpoint_address) = 0;
  82. protected:
  83. friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
  84. UsbDeviceHandle();
  85. virtual ~UsbDeviceHandle();
  86. };
  87. } // namespace device
  88. #endif // SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_H_