fake_usb_device_handle.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "services/device/usb/fake_usb_device_handle.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/notreached.h"
  10. #include "services/device/usb/usb_device.h"
  11. namespace device {
  12. using mojom::UsbControlTransferRecipient;
  13. using mojom::UsbControlTransferType;
  14. using mojom::UsbTransferDirection;
  15. using mojom::UsbTransferStatus;
  16. FakeUsbDeviceHandle::FakeUsbDeviceHandle(const uint8_t* data, size_t size)
  17. : data_(data), size_(size), position_(0) {}
  18. scoped_refptr<UsbDevice> FakeUsbDeviceHandle::GetDevice() const {
  19. NOTIMPLEMENTED();
  20. return nullptr;
  21. }
  22. void FakeUsbDeviceHandle::Close() {
  23. NOTIMPLEMENTED();
  24. }
  25. void FakeUsbDeviceHandle::SetConfiguration(int configuration_value,
  26. ResultCallback callback) {
  27. NOTIMPLEMENTED();
  28. }
  29. void FakeUsbDeviceHandle::ClaimInterface(int interface_number,
  30. ResultCallback callback) {
  31. NOTIMPLEMENTED();
  32. }
  33. void FakeUsbDeviceHandle::ReleaseInterface(int interface_number,
  34. ResultCallback callback) {
  35. NOTIMPLEMENTED();
  36. }
  37. void FakeUsbDeviceHandle::SetInterfaceAlternateSetting(
  38. int interface_number,
  39. int alternate_setting,
  40. ResultCallback callback) {
  41. NOTIMPLEMENTED();
  42. }
  43. void FakeUsbDeviceHandle::ResetDevice(ResultCallback callback) {
  44. NOTIMPLEMENTED();
  45. }
  46. void FakeUsbDeviceHandle::ClearHalt(mojom::UsbTransferDirection direction,
  47. uint8_t endpoint_number,
  48. ResultCallback callback) {
  49. NOTIMPLEMENTED();
  50. }
  51. void FakeUsbDeviceHandle::ControlTransfer(
  52. UsbTransferDirection direction,
  53. UsbControlTransferType request_type,
  54. UsbControlTransferRecipient recipient,
  55. uint8_t request,
  56. uint16_t value,
  57. uint16_t index,
  58. scoped_refptr<base::RefCountedBytes> buffer,
  59. unsigned int timeout,
  60. UsbDeviceHandle::TransferCallback callback) {
  61. if (position_ == size_) {
  62. std::move(callback).Run(UsbTransferStatus::DISCONNECT, buffer, 0);
  63. return;
  64. }
  65. if (data_[position_++] % 2) {
  66. size_t bytes_transferred = 0;
  67. if (position_ + 2 <= size_) {
  68. bytes_transferred = data_[position_] | data_[position_ + 1] << 8;
  69. position_ += 2;
  70. bytes_transferred = std::min(bytes_transferred, buffer->size());
  71. bytes_transferred = std::min(bytes_transferred, size_ - position_);
  72. }
  73. if (direction == UsbTransferDirection::INBOUND) {
  74. memcpy(buffer->front(), &data_[position_], bytes_transferred);
  75. position_ += bytes_transferred;
  76. }
  77. std::move(callback).Run(UsbTransferStatus::COMPLETED, buffer,
  78. bytes_transferred);
  79. } else {
  80. std::move(callback).Run(UsbTransferStatus::TRANSFER_ERROR, buffer, 0);
  81. }
  82. }
  83. void FakeUsbDeviceHandle::IsochronousTransferIn(
  84. uint8_t endpoint_number,
  85. const std::vector<uint32_t>& packet_lengths,
  86. unsigned int timeout,
  87. IsochronousTransferCallback callback) {
  88. NOTIMPLEMENTED();
  89. }
  90. void FakeUsbDeviceHandle::IsochronousTransferOut(
  91. uint8_t endpoint_number,
  92. scoped_refptr<base::RefCountedBytes> buffer,
  93. const std::vector<uint32_t>& packet_lengths,
  94. unsigned int timeout,
  95. IsochronousTransferCallback callback) {
  96. NOTIMPLEMENTED();
  97. }
  98. void FakeUsbDeviceHandle::GenericTransfer(
  99. UsbTransferDirection direction,
  100. uint8_t endpoint_number,
  101. scoped_refptr<base::RefCountedBytes> buffer,
  102. unsigned int timeout,
  103. TransferCallback callback) {
  104. NOTIMPLEMENTED();
  105. }
  106. const mojom::UsbInterfaceInfo* FakeUsbDeviceHandle::FindInterfaceByEndpoint(
  107. uint8_t endpoint_address) {
  108. NOTIMPLEMENTED();
  109. return nullptr;
  110. }
  111. FakeUsbDeviceHandle::~FakeUsbDeviceHandle() = default;
  112. } // namespace device