| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "device/usb/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <numeric> | 11 #include <numeric> |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "device/usb/mojo/type_converters.h" | 19 #include "device/usb/mojo/type_converters.h" |
| 20 #include "device/usb/usb_descriptors.h" | 20 #include "device/usb/usb_descriptors.h" |
| 21 #include "device/usb/usb_device.h" | 21 #include "device/usb/usb_device.h" |
| 22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
| 23 | 23 |
| 24 namespace device { | 24 namespace device { |
| 25 |
| 26 using mojom::UsbControlTransferParamsPtr; |
| 27 using mojom::UsbControlTransferRecipient; |
| 28 |
| 25 namespace usb { | 29 namespace usb { |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { | 33 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { |
| 30 return new net::IOBuffer( | 34 return new net::IOBuffer( |
| 31 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); | 35 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); |
| 32 } | 36 } |
| 33 | 37 |
| 34 void OnTransferIn(const Device::GenericTransferInCallback& callback, | 38 void OnTransferIn(const mojom::UsbDevice::GenericTransferInCallback& callback, |
| 35 UsbTransferStatus status, | 39 UsbTransferStatus status, |
| 36 scoped_refptr<net::IOBuffer> buffer, | 40 scoped_refptr<net::IOBuffer> buffer, |
| 37 size_t buffer_size) { | 41 size_t buffer_size) { |
| 38 std::vector<uint8_t> data; | 42 std::vector<uint8_t> data; |
| 39 if (buffer) { | 43 if (buffer) { |
| 40 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a | 44 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a |
| 41 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move | 45 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move |
| 42 // instead of copy. | 46 // instead of copy. |
| 43 data.resize(buffer_size); | 47 data.resize(buffer_size); |
| 44 std::copy(buffer->data(), buffer->data() + buffer_size, data.begin()); | 48 std::copy(buffer->data(), buffer->data() + buffer_size, data.begin()); |
| 45 } | 49 } |
| 46 | 50 |
| 47 callback.Run(mojo::ConvertTo<TransferStatus>(status), data); | 51 callback.Run(mojo::ConvertTo<mojom::UsbTransferStatus>(status), data); |
| 48 } | 52 } |
| 49 | 53 |
| 50 void OnTransferOut(const Device::GenericTransferOutCallback& callback, | 54 void OnTransferOut(const mojom::UsbDevice::GenericTransferOutCallback& callback, |
| 51 UsbTransferStatus status, | 55 UsbTransferStatus status, |
| 52 scoped_refptr<net::IOBuffer> buffer, | 56 scoped_refptr<net::IOBuffer> buffer, |
| 53 size_t buffer_size) { | 57 size_t buffer_size) { |
| 54 callback.Run(mojo::ConvertTo<TransferStatus>(status)); | 58 callback.Run(mojo::ConvertTo<mojom::UsbTransferStatus>(status)); |
| 55 } | 59 } |
| 56 | 60 |
| 57 std::vector<IsochronousPacketPtr> BuildIsochronousPacketArray( | 61 std::vector<mojom::UsbIsochronousPacketPtr> BuildIsochronousPacketArray( |
| 58 const std::vector<uint32_t>& packet_lengths, | 62 const std::vector<uint32_t>& packet_lengths, |
| 59 TransferStatus status) { | 63 mojom::UsbTransferStatus status) { |
| 60 std::vector<IsochronousPacketPtr> packets; | 64 std::vector<mojom::UsbIsochronousPacketPtr> packets; |
| 61 packets.reserve(packet_lengths.size()); | 65 packets.reserve(packet_lengths.size()); |
| 62 for (uint32_t packet_length : packet_lengths) { | 66 for (uint32_t packet_length : packet_lengths) { |
| 63 auto packet = IsochronousPacket::New(); | 67 auto packet = mojom::UsbIsochronousPacket::New(); |
| 64 packet->length = packet_length; | 68 packet->length = packet_length; |
| 65 packet->status = status; | 69 packet->status = status; |
| 66 packets.push_back(std::move(packet)); | 70 packets.push_back(std::move(packet)); |
| 67 } | 71 } |
| 68 return packets; | 72 return packets; |
| 69 } | 73 } |
| 70 | 74 |
| 71 void OnIsochronousTransferIn( | 75 void OnIsochronousTransferIn( |
| 72 const Device::IsochronousTransferInCallback& callback, | 76 const mojom::UsbDevice::IsochronousTransferInCallback& callback, |
| 73 scoped_refptr<net::IOBuffer> buffer, | 77 scoped_refptr<net::IOBuffer> buffer, |
| 74 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { | 78 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { |
| 75 std::vector<uint8_t> data; | 79 std::vector<uint8_t> data; |
| 76 if (buffer) { | 80 if (buffer) { |
| 77 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a | 81 // TODO(rockot/reillyg): We should change UsbDeviceHandle to use a |
| 78 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move | 82 // std::vector<uint8_t> instead of net::IOBuffer. Then we could move |
| 79 // instead of copy. | 83 // instead of copy. |
| 80 uint32_t buffer_size = | 84 uint32_t buffer_size = |
| 81 std::accumulate(packets.begin(), packets.end(), 0u, | 85 std::accumulate(packets.begin(), packets.end(), 0u, |
| 82 [](const uint32_t& a, | 86 [](const uint32_t& a, |
| 83 const UsbDeviceHandle::IsochronousPacket& packet) { | 87 const UsbDeviceHandle::IsochronousPacket& packet) { |
| 84 return a + packet.length; | 88 return a + packet.length; |
| 85 }); | 89 }); |
| 86 data.resize(buffer_size); | 90 data.resize(buffer_size); |
| 87 std::copy(buffer->data(), buffer->data() + buffer_size, data.begin()); | 91 std::copy(buffer->data(), buffer->data() + buffer_size, data.begin()); |
| 88 } | 92 } |
| 89 callback.Run(data, | 93 callback.Run( |
| 90 mojo::ConvertTo<std::vector<IsochronousPacketPtr>>(packets)); | 94 data, |
| 95 mojo::ConvertTo<std::vector<mojom::UsbIsochronousPacketPtr>>(packets)); |
| 91 } | 96 } |
| 92 | 97 |
| 93 void OnIsochronousTransferOut( | 98 void OnIsochronousTransferOut( |
| 94 const Device::IsochronousTransferOutCallback& callback, | 99 const mojom::UsbDevice::IsochronousTransferOutCallback& callback, |
| 95 scoped_refptr<net::IOBuffer> buffer, | 100 scoped_refptr<net::IOBuffer> buffer, |
| 96 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { | 101 const std::vector<UsbDeviceHandle::IsochronousPacket>& packets) { |
| 97 callback.Run(mojo::ConvertTo<std::vector<IsochronousPacketPtr>>(packets)); | 102 callback.Run( |
| 103 mojo::ConvertTo<std::vector<mojom::UsbIsochronousPacketPtr>>(packets)); |
| 98 } | 104 } |
| 99 | 105 |
| 100 } // namespace | 106 } // namespace |
| 101 | 107 |
| 102 // static | 108 // static |
| 103 void DeviceImpl::Create(scoped_refptr<UsbDevice> device, | 109 void DeviceImpl::Create(scoped_refptr<device::UsbDevice> device, |
| 104 base::WeakPtr<PermissionProvider> permission_provider, | 110 base::WeakPtr<PermissionProvider> permission_provider, |
| 105 DeviceRequest request) { | 111 mojom::UsbDeviceRequest request) { |
| 106 auto* device_impl = | 112 auto* device_impl = |
| 107 new DeviceImpl(std::move(device), std::move(permission_provider)); | 113 new DeviceImpl(std::move(device), std::move(permission_provider)); |
| 108 device_impl->binding_ = mojo::MakeStrongBinding(base::WrapUnique(device_impl), | 114 device_impl->binding_ = mojo::MakeStrongBinding(base::WrapUnique(device_impl), |
| 109 std::move(request)); | 115 std::move(request)); |
| 110 } | 116 } |
| 111 | 117 |
| 112 DeviceImpl::~DeviceImpl() { | 118 DeviceImpl::~DeviceImpl() { |
| 113 CloseHandle(); | 119 CloseHandle(); |
| 114 } | 120 } |
| 115 | 121 |
| 116 DeviceImpl::DeviceImpl(scoped_refptr<UsbDevice> device, | 122 DeviceImpl::DeviceImpl(scoped_refptr<device::UsbDevice> device, |
| 117 base::WeakPtr<PermissionProvider> permission_provider) | 123 base::WeakPtr<PermissionProvider> permission_provider) |
| 118 : device_(std::move(device)), | 124 : device_(std::move(device)), |
| 119 permission_provider_(std::move(permission_provider)), | 125 permission_provider_(std::move(permission_provider)), |
| 120 observer_(this), | 126 observer_(this), |
| 121 weak_factory_(this) { | 127 weak_factory_(this) { |
| 122 DCHECK(device_); | 128 DCHECK(device_); |
| 123 observer_.Add(device_.get()); | 129 observer_.Add(device_.get()); |
| 124 } | 130 } |
| 125 | 131 |
| 126 void DeviceImpl::CloseHandle() { | 132 void DeviceImpl::CloseHandle() { |
| 127 if (device_handle_) { | 133 if (device_handle_) { |
| 128 device_handle_->Close(); | 134 device_handle_->Close(); |
| 129 if (permission_provider_) | 135 if (permission_provider_) |
| 130 permission_provider_->DecrementConnectionCount(); | 136 permission_provider_->DecrementConnectionCount(); |
| 131 } | 137 } |
| 132 device_handle_ = nullptr; | 138 device_handle_ = nullptr; |
| 133 } | 139 } |
| 134 | 140 |
| 135 bool DeviceImpl::HasControlTransferPermission( | 141 bool DeviceImpl::HasControlTransferPermission( |
| 136 ControlTransferRecipient recipient, | 142 UsbControlTransferRecipient recipient, |
| 137 uint16_t index) { | 143 uint16_t index) { |
| 138 DCHECK(device_handle_); | 144 DCHECK(device_handle_); |
| 139 const UsbConfigDescriptor* config = device_->active_configuration(); | 145 const UsbConfigDescriptor* config = device_->active_configuration(); |
| 140 | 146 |
| 141 if (!permission_provider_) | 147 if (!permission_provider_) |
| 142 return false; | 148 return false; |
| 143 | 149 |
| 144 if (recipient == ControlTransferRecipient::INTERFACE || | 150 if (recipient == UsbControlTransferRecipient::INTERFACE || |
| 145 recipient == ControlTransferRecipient::ENDPOINT) { | 151 recipient == UsbControlTransferRecipient::ENDPOINT) { |
| 146 if (!config) | 152 if (!config) |
| 147 return false; | 153 return false; |
| 148 | 154 |
| 149 const UsbInterfaceDescriptor* interface = nullptr; | 155 const UsbInterfaceDescriptor* interface = nullptr; |
| 150 if (recipient == ControlTransferRecipient::ENDPOINT) { | 156 if (recipient == UsbControlTransferRecipient::ENDPOINT) { |
| 151 interface = device_handle_->FindInterfaceByEndpoint(index & 0xff); | 157 interface = device_handle_->FindInterfaceByEndpoint(index & 0xff); |
| 152 } else { | 158 } else { |
| 153 auto interface_it = | 159 auto interface_it = |
| 154 std::find_if(config->interfaces.begin(), config->interfaces.end(), | 160 std::find_if(config->interfaces.begin(), config->interfaces.end(), |
| 155 [index](const UsbInterfaceDescriptor& this_iface) { | 161 [index](const UsbInterfaceDescriptor& this_iface) { |
| 156 return this_iface.interface_number == (index & 0xff); | 162 return this_iface.interface_number == (index & 0xff); |
| 157 }); | 163 }); |
| 158 if (interface_it != config->interfaces.end()) | 164 if (interface_it != config->interfaces.end()) |
| 159 interface = &*interface_it; | 165 interface = &*interface_it; |
| 160 } | 166 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 177 const OpenCallback& callback, | 183 const OpenCallback& callback, |
| 178 scoped_refptr<UsbDeviceHandle> handle) { | 184 scoped_refptr<UsbDeviceHandle> handle) { |
| 179 if (!self) { | 185 if (!self) { |
| 180 handle->Close(); | 186 handle->Close(); |
| 181 return; | 187 return; |
| 182 } | 188 } |
| 183 | 189 |
| 184 self->device_handle_ = std::move(handle); | 190 self->device_handle_ = std::move(handle); |
| 185 if (self->device_handle_ && self->permission_provider_) | 191 if (self->device_handle_ && self->permission_provider_) |
| 186 self->permission_provider_->IncrementConnectionCount(); | 192 self->permission_provider_->IncrementConnectionCount(); |
| 187 callback.Run(self->device_handle_ ? OpenDeviceError::OK | 193 callback.Run(self->device_handle_ ? mojom::UsbOpenDeviceError::OK |
| 188 : OpenDeviceError::ACCESS_DENIED); | 194 : mojom::UsbOpenDeviceError::ACCESS_DENIED); |
| 189 } | 195 } |
| 190 | 196 |
| 191 void DeviceImpl::OnPermissionGrantedForOpen(const OpenCallback& callback, | 197 void DeviceImpl::OnPermissionGrantedForOpen(const OpenCallback& callback, |
| 192 bool granted) { | 198 bool granted) { |
| 193 if (granted && permission_provider_ && | 199 if (granted && permission_provider_ && |
| 194 permission_provider_->HasDevicePermission(device_)) { | 200 permission_provider_->HasDevicePermission(device_)) { |
| 195 device_->Open( | 201 device_->Open( |
| 196 base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); | 202 base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); |
| 197 } else { | 203 } else { |
| 198 callback.Run(OpenDeviceError::ACCESS_DENIED); | 204 callback.Run(mojom::UsbOpenDeviceError::ACCESS_DENIED); |
| 199 } | 205 } |
| 200 } | 206 } |
| 201 | 207 |
| 202 void DeviceImpl::Open(const OpenCallback& callback) { | 208 void DeviceImpl::Open(const OpenCallback& callback) { |
| 203 if (device_handle_) { | 209 if (device_handle_) { |
| 204 callback.Run(OpenDeviceError::ALREADY_OPEN); | 210 callback.Run(mojom::UsbOpenDeviceError::ALREADY_OPEN); |
| 205 return; | 211 return; |
| 206 } | 212 } |
| 207 | 213 |
| 208 if (!device_->permission_granted()) { | 214 if (!device_->permission_granted()) { |
| 209 device_->RequestPermission( | 215 device_->RequestPermission( |
| 210 base::Bind(&DeviceImpl::OnPermissionGrantedForOpen, | 216 base::Bind(&DeviceImpl::OnPermissionGrantedForOpen, |
| 211 weak_factory_.GetWeakPtr(), callback)); | 217 weak_factory_.GetWeakPtr(), callback)); |
| 212 return; | 218 return; |
| 213 } | 219 } |
| 214 | 220 |
| 215 if (permission_provider_ && | 221 if (permission_provider_ && |
| 216 permission_provider_->HasDevicePermission(device_)) { | 222 permission_provider_->HasDevicePermission(device_)) { |
| 217 device_->Open( | 223 device_->Open( |
| 218 base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); | 224 base::Bind(&DeviceImpl::OnOpen, weak_factory_.GetWeakPtr(), callback)); |
| 219 } else { | 225 } else { |
| 220 callback.Run(OpenDeviceError::ACCESS_DENIED); | 226 callback.Run(mojom::UsbOpenDeviceError::ACCESS_DENIED); |
| 221 } | 227 } |
| 222 } | 228 } |
| 223 | 229 |
| 224 void DeviceImpl::Close(const CloseCallback& callback) { | 230 void DeviceImpl::Close(const CloseCallback& callback) { |
| 225 CloseHandle(); | 231 CloseHandle(); |
| 226 callback.Run(); | 232 callback.Run(); |
| 227 } | 233 } |
| 228 | 234 |
| 229 void DeviceImpl::SetConfiguration(uint8_t value, | 235 void DeviceImpl::SetConfiguration(uint8_t value, |
| 230 const SetConfigurationCallback& callback) { | 236 const SetConfigurationCallback& callback) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 void DeviceImpl::ClearHalt(uint8_t endpoint, | 315 void DeviceImpl::ClearHalt(uint8_t endpoint, |
| 310 const ClearHaltCallback& callback) { | 316 const ClearHaltCallback& callback) { |
| 311 if (!device_handle_) { | 317 if (!device_handle_) { |
| 312 callback.Run(false); | 318 callback.Run(false); |
| 313 return; | 319 return; |
| 314 } | 320 } |
| 315 | 321 |
| 316 device_handle_->ClearHalt(endpoint, callback); | 322 device_handle_->ClearHalt(endpoint, callback); |
| 317 } | 323 } |
| 318 | 324 |
| 319 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, | 325 void DeviceImpl::ControlTransferIn(UsbControlTransferParamsPtr params, |
| 320 uint32_t length, | 326 uint32_t length, |
| 321 uint32_t timeout, | 327 uint32_t timeout, |
| 322 const ControlTransferInCallback& callback) { | 328 const ControlTransferInCallback& callback) { |
| 323 if (!device_handle_) { | 329 if (!device_handle_) { |
| 324 callback.Run(TransferStatus::TRANSFER_ERROR, base::nullopt); | 330 callback.Run(mojom::UsbTransferStatus::TRANSFER_ERROR, base::nullopt); |
| 325 return; | 331 return; |
| 326 } | 332 } |
| 327 | 333 |
| 328 if (HasControlTransferPermission(params->recipient, params->index)) { | 334 if (HasControlTransferPermission(params->recipient, params->index)) { |
| 329 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); | 335 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); |
| 330 device_handle_->ControlTransfer( | 336 device_handle_->ControlTransfer( |
| 331 USB_DIRECTION_INBOUND, | 337 USB_DIRECTION_INBOUND, |
| 332 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), | 338 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), |
| 333 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), | 339 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), |
| 334 params->request, params->value, params->index, buffer, length, timeout, | 340 params->request, params->value, params->index, buffer, length, timeout, |
| 335 base::Bind(&OnTransferIn, callback)); | 341 base::Bind(&OnTransferIn, callback)); |
| 336 } else { | 342 } else { |
| 337 callback.Run(TransferStatus::PERMISSION_DENIED, base::nullopt); | 343 callback.Run(mojom::UsbTransferStatus::PERMISSION_DENIED, base::nullopt); |
| 338 } | 344 } |
| 339 } | 345 } |
| 340 | 346 |
| 341 void DeviceImpl::ControlTransferOut( | 347 void DeviceImpl::ControlTransferOut( |
| 342 ControlTransferParamsPtr params, | 348 UsbControlTransferParamsPtr params, |
| 343 const std::vector<uint8_t>& data, | 349 const std::vector<uint8_t>& data, |
| 344 uint32_t timeout, | 350 uint32_t timeout, |
| 345 const ControlTransferOutCallback& callback) { | 351 const ControlTransferOutCallback& callback) { |
| 346 if (!device_handle_) { | 352 if (!device_handle_) { |
| 347 callback.Run(TransferStatus::TRANSFER_ERROR); | 353 callback.Run(mojom::UsbTransferStatus::TRANSFER_ERROR); |
| 348 return; | 354 return; |
| 349 } | 355 } |
| 350 | 356 |
| 351 if (HasControlTransferPermission(params->recipient, params->index)) { | 357 if (HasControlTransferPermission(params->recipient, params->index)) { |
| 352 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 358 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
| 353 std::copy(data.begin(), data.end(), buffer->data()); | 359 std::copy(data.begin(), data.end(), buffer->data()); |
| 354 device_handle_->ControlTransfer( | 360 device_handle_->ControlTransfer( |
| 355 USB_DIRECTION_OUTBOUND, | 361 USB_DIRECTION_OUTBOUND, |
| 356 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), | 362 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), |
| 357 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), | 363 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), |
| 358 params->request, params->value, params->index, buffer, data.size(), | 364 params->request, params->value, params->index, buffer, data.size(), |
| 359 timeout, base::Bind(&OnTransferOut, callback)); | 365 timeout, base::Bind(&OnTransferOut, callback)); |
| 360 } else { | 366 } else { |
| 361 callback.Run(TransferStatus::PERMISSION_DENIED); | 367 callback.Run(mojom::UsbTransferStatus::PERMISSION_DENIED); |
| 362 } | 368 } |
| 363 } | 369 } |
| 364 | 370 |
| 365 void DeviceImpl::GenericTransferIn(uint8_t endpoint_number, | 371 void DeviceImpl::GenericTransferIn(uint8_t endpoint_number, |
| 366 uint32_t length, | 372 uint32_t length, |
| 367 uint32_t timeout, | 373 uint32_t timeout, |
| 368 const GenericTransferInCallback& callback) { | 374 const GenericTransferInCallback& callback) { |
| 369 if (!device_handle_) { | 375 if (!device_handle_) { |
| 370 callback.Run(TransferStatus::TRANSFER_ERROR, base::nullopt); | 376 callback.Run(mojom::UsbTransferStatus::TRANSFER_ERROR, base::nullopt); |
| 371 return; | 377 return; |
| 372 } | 378 } |
| 373 | 379 |
| 374 uint8_t endpoint_address = endpoint_number | 0x80; | 380 uint8_t endpoint_address = endpoint_number | 0x80; |
| 375 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); | 381 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); |
| 376 device_handle_->GenericTransfer(USB_DIRECTION_INBOUND, endpoint_address, | 382 device_handle_->GenericTransfer(USB_DIRECTION_INBOUND, endpoint_address, |
| 377 buffer, length, timeout, | 383 buffer, length, timeout, |
| 378 base::Bind(&OnTransferIn, callback)); | 384 base::Bind(&OnTransferIn, callback)); |
| 379 } | 385 } |
| 380 | 386 |
| 381 void DeviceImpl::GenericTransferOut( | 387 void DeviceImpl::GenericTransferOut( |
| 382 uint8_t endpoint_number, | 388 uint8_t endpoint_number, |
| 383 const std::vector<uint8_t>& data, | 389 const std::vector<uint8_t>& data, |
| 384 uint32_t timeout, | 390 uint32_t timeout, |
| 385 const GenericTransferOutCallback& callback) { | 391 const GenericTransferOutCallback& callback) { |
| 386 if (!device_handle_) { | 392 if (!device_handle_) { |
| 387 callback.Run(TransferStatus::TRANSFER_ERROR); | 393 callback.Run(mojom::UsbTransferStatus::TRANSFER_ERROR); |
| 388 return; | 394 return; |
| 389 } | 395 } |
| 390 | 396 |
| 391 uint8_t endpoint_address = endpoint_number; | 397 uint8_t endpoint_address = endpoint_number; |
| 392 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 398 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
| 393 std::copy(data.begin(), data.end(), buffer->data()); | 399 std::copy(data.begin(), data.end(), buffer->data()); |
| 394 device_handle_->GenericTransfer(USB_DIRECTION_OUTBOUND, endpoint_address, | 400 device_handle_->GenericTransfer(USB_DIRECTION_OUTBOUND, endpoint_address, |
| 395 buffer, data.size(), timeout, | 401 buffer, data.size(), timeout, |
| 396 base::Bind(&OnTransferOut, callback)); | 402 base::Bind(&OnTransferOut, callback)); |
| 397 } | 403 } |
| 398 | 404 |
| 399 void DeviceImpl::IsochronousTransferIn( | 405 void DeviceImpl::IsochronousTransferIn( |
| 400 uint8_t endpoint_number, | 406 uint8_t endpoint_number, |
| 401 const std::vector<uint32_t>& packet_lengths, | 407 const std::vector<uint32_t>& packet_lengths, |
| 402 uint32_t timeout, | 408 uint32_t timeout, |
| 403 const IsochronousTransferInCallback& callback) { | 409 const IsochronousTransferInCallback& callback) { |
| 404 if (!device_handle_) { | 410 if (!device_handle_) { |
| 405 callback.Run(base::nullopt, | 411 callback.Run(base::nullopt, |
| 406 BuildIsochronousPacketArray(packet_lengths, | 412 BuildIsochronousPacketArray( |
| 407 TransferStatus::TRANSFER_ERROR)); | 413 packet_lengths, mojom::UsbTransferStatus::TRANSFER_ERROR)); |
| 408 return; | 414 return; |
| 409 } | 415 } |
| 410 | 416 |
| 411 uint8_t endpoint_address = endpoint_number | 0x80; | 417 uint8_t endpoint_address = endpoint_number | 0x80; |
| 412 device_handle_->IsochronousTransferIn( | 418 device_handle_->IsochronousTransferIn( |
| 413 endpoint_address, packet_lengths, timeout, | 419 endpoint_address, packet_lengths, timeout, |
| 414 base::Bind(&OnIsochronousTransferIn, callback)); | 420 base::Bind(&OnIsochronousTransferIn, callback)); |
| 415 } | 421 } |
| 416 | 422 |
| 417 void DeviceImpl::IsochronousTransferOut( | 423 void DeviceImpl::IsochronousTransferOut( |
| 418 uint8_t endpoint_number, | 424 uint8_t endpoint_number, |
| 419 const std::vector<uint8_t>& data, | 425 const std::vector<uint8_t>& data, |
| 420 const std::vector<uint32_t>& packet_lengths, | 426 const std::vector<uint32_t>& packet_lengths, |
| 421 uint32_t timeout, | 427 uint32_t timeout, |
| 422 const IsochronousTransferOutCallback& callback) { | 428 const IsochronousTransferOutCallback& callback) { |
| 423 if (!device_handle_) { | 429 if (!device_handle_) { |
| 424 callback.Run(BuildIsochronousPacketArray(packet_lengths, | 430 callback.Run(BuildIsochronousPacketArray( |
| 425 TransferStatus::TRANSFER_ERROR)); | 431 packet_lengths, mojom::UsbTransferStatus::TRANSFER_ERROR)); |
| 426 return; | 432 return; |
| 427 } | 433 } |
| 428 | 434 |
| 429 uint8_t endpoint_address = endpoint_number; | 435 uint8_t endpoint_address = endpoint_number; |
| 430 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 436 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
| 431 std::copy(data.begin(), data.end(), buffer->data()); | 437 std::copy(data.begin(), data.end(), buffer->data()); |
| 432 device_handle_->IsochronousTransferOut( | 438 device_handle_->IsochronousTransferOut( |
| 433 endpoint_address, buffer, packet_lengths, timeout, | 439 endpoint_address, buffer, packet_lengths, timeout, |
| 434 base::Bind(&OnIsochronousTransferOut, callback)); | 440 base::Bind(&OnIsochronousTransferOut, callback)); |
| 435 } | 441 } |
| 436 | 442 |
| 437 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { | 443 void DeviceImpl::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) { |
| 438 DCHECK_EQ(device_, device); | 444 DCHECK_EQ(device_, device); |
| 439 binding_->Close(); | 445 binding_->Close(); |
| 440 } | 446 } |
| 441 | 447 |
| 442 } // namespace usb | 448 } // namespace usb |
| 443 } // namespace device | 449 } // namespace device |
| OLD | NEW |