fake_usb_device_handle.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2016 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_FAKE_USB_DEVICE_HANDLE_H_
  5. #define SERVICES_DEVICE_USB_FAKE_USB_DEVICE_HANDLE_H_
  6. #include <vector>
  7. #include "services/device/usb/usb_device_handle.h"
  8. namespace device {
  9. // This class implements a fake USB device handle that will handle control
  10. // requests by responding with data out of the provided buffer. The format of
  11. // each record in the buffer is:
  12. //
  13. // 0 1 2 3...
  14. // +---------+---------+---------+---------- - - -
  15. // | success | length | data...
  16. // +---------+---------+---------+---------- - - -
  17. //
  18. // If |success| is 1 the transfer will succeed and respond with |length| bytes
  19. // of the following data (no data is consumed for outgoing transfers). If
  20. // |success| is 0 the transfer will fail with USB_TRANSFER_ERROR. If the buffer
  21. // is exhausted all following transfers will fail with USB_TRANSFER_DISCONNECT.
  22. class FakeUsbDeviceHandle : public UsbDeviceHandle {
  23. public:
  24. FakeUsbDeviceHandle(const uint8_t* data, size_t size);
  25. scoped_refptr<UsbDevice> GetDevice() const override;
  26. void Close() override;
  27. void SetConfiguration(int configuration_value,
  28. ResultCallback callback) override;
  29. void ClaimInterface(int interface_number, ResultCallback callback) override;
  30. void ReleaseInterface(int interface_number, ResultCallback callback) override;
  31. void SetInterfaceAlternateSetting(int interface_number,
  32. int alternate_setting,
  33. ResultCallback callback) override;
  34. void ResetDevice(ResultCallback callback) override;
  35. void ClearHalt(mojom::UsbTransferDirection direction,
  36. uint8_t endpoint_number,
  37. ResultCallback callback) override;
  38. void ControlTransfer(mojom::UsbTransferDirection direction,
  39. mojom::UsbControlTransferType request_type,
  40. mojom::UsbControlTransferRecipient recipient,
  41. uint8_t request,
  42. uint16_t value,
  43. uint16_t index,
  44. scoped_refptr<base::RefCountedBytes> buffer,
  45. unsigned int timeout,
  46. TransferCallback callback) override;
  47. void IsochronousTransferIn(uint8_t endpoint,
  48. const std::vector<uint32_t>& packet_lengths,
  49. unsigned int timeout,
  50. IsochronousTransferCallback callback) override;
  51. void IsochronousTransferOut(uint8_t endpoint,
  52. scoped_refptr<base::RefCountedBytes> buffer,
  53. const std::vector<uint32_t>& packet_lengths,
  54. unsigned int timeout,
  55. IsochronousTransferCallback callback) override;
  56. void GenericTransfer(mojom::UsbTransferDirection direction,
  57. uint8_t endpoint_number,
  58. scoped_refptr<base::RefCountedBytes> buffer,
  59. unsigned int timeout,
  60. TransferCallback callback) override;
  61. const mojom::UsbInterfaceInfo* FindInterfaceByEndpoint(
  62. uint8_t endpoint_address) override;
  63. private:
  64. ~FakeUsbDeviceHandle() override;
  65. const uint8_t* const data_;
  66. const size_t size_;
  67. size_t position_;
  68. };
  69. } // namespace device
  70. #endif // SERVICES_DEVICE_USB_FAKE_USB_DEVICE_HANDLE_H_