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/devices_app/usb/device_impl.h" | 5 #include "device/devices_app/usb/device_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 if (allowed) { | 88 if (allowed) { |
89 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); | 89 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); |
90 device_handle->ControlTransfer( | 90 device_handle->ControlTransfer( |
91 USB_DIRECTION_INBOUND, | 91 USB_DIRECTION_INBOUND, |
92 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), | 92 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), |
93 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), | 93 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), |
94 params->request, params->value, params->index, buffer, length, timeout, | 94 params->request, params->value, params->index, buffer, length, timeout, |
95 base::Bind(&OnTransferIn, base::Passed(&callback))); | 95 base::Bind(&OnTransferIn, base::Passed(&callback))); |
96 } else { | 96 } else { |
97 mojo::Array<uint8_t> data; | 97 mojo::Array<uint8_t> data; |
98 callback->Run(TRANSFER_STATUS_PERMISSION_DENIED, std::move(data)); | 98 callback->Run(TransferStatus::PERMISSION_DENIED, std::move(data)); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 void OnTransferOut(scoped_ptr<MojoTransferOutCallback> callback, | 102 void OnTransferOut(scoped_ptr<MojoTransferOutCallback> callback, |
103 UsbTransferStatus status, | 103 UsbTransferStatus status, |
104 scoped_refptr<net::IOBuffer> buffer, | 104 scoped_refptr<net::IOBuffer> buffer, |
105 size_t buffer_size) { | 105 size_t buffer_size) { |
106 callback->Run(mojo::ConvertTo<TransferStatus>(status)); | 106 callback->Run(mojo::ConvertTo<TransferStatus>(status)); |
107 } | 107 } |
108 | 108 |
109 void OnControlTransferOutPermissionCheckComplete( | 109 void OnControlTransferOutPermissionCheckComplete( |
110 scoped_refptr<UsbDeviceHandle> device_handle, | 110 scoped_refptr<UsbDeviceHandle> device_handle, |
111 ControlTransferParamsPtr params, | 111 ControlTransferParamsPtr params, |
112 mojo::Array<uint8_t> data, | 112 mojo::Array<uint8_t> data, |
113 int timeout, | 113 int timeout, |
114 scoped_ptr<Device::ControlTransferOutCallback> callback, | 114 scoped_ptr<Device::ControlTransferOutCallback> callback, |
115 bool allowed) { | 115 bool allowed) { |
116 if (allowed) { | 116 if (allowed) { |
117 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 117 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
118 const std::vector<uint8_t>& storage = data.storage(); | 118 const std::vector<uint8_t>& storage = data.storage(); |
119 std::copy(storage.begin(), storage.end(), buffer->data()); | 119 std::copy(storage.begin(), storage.end(), buffer->data()); |
120 device_handle->ControlTransfer( | 120 device_handle->ControlTransfer( |
121 USB_DIRECTION_OUTBOUND, | 121 USB_DIRECTION_OUTBOUND, |
122 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), | 122 mojo::ConvertTo<UsbDeviceHandle::TransferRequestType>(params->type), |
123 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), | 123 mojo::ConvertTo<UsbDeviceHandle::TransferRecipient>(params->recipient), |
124 params->request, params->value, params->index, buffer, data.size(), | 124 params->request, params->value, params->index, buffer, data.size(), |
125 timeout, base::Bind(&OnTransferOut, base::Passed(&callback))); | 125 timeout, base::Bind(&OnTransferOut, base::Passed(&callback))); |
126 } else { | 126 } else { |
127 callback->Run(TRANSFER_STATUS_PERMISSION_DENIED); | 127 callback->Run(TransferStatus::PERMISSION_DENIED); |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 void OnIsochronousTransferIn( | 131 void OnIsochronousTransferIn( |
132 scoped_ptr<Device::IsochronousTransferInCallback> callback, | 132 scoped_ptr<Device::IsochronousTransferInCallback> callback, |
133 uint32_t packet_size, | 133 uint32_t packet_size, |
134 UsbTransferStatus status, | 134 UsbTransferStatus status, |
135 scoped_refptr<net::IOBuffer> buffer, | 135 scoped_refptr<net::IOBuffer> buffer, |
136 size_t buffer_size) { | 136 size_t buffer_size) { |
137 size_t num_packets = buffer_size / packet_size; | 137 size_t num_packets = buffer_size / packet_size; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 device_handle_ = nullptr; | 182 device_handle_ = nullptr; |
183 } | 183 } |
184 | 184 |
185 void DeviceImpl::HasControlTransferPermission( | 185 void DeviceImpl::HasControlTransferPermission( |
186 ControlTransferRecipient recipient, | 186 ControlTransferRecipient recipient, |
187 uint16_t index, | 187 uint16_t index, |
188 const base::Callback<void(bool)>& callback) { | 188 const base::Callback<void(bool)>& callback) { |
189 DCHECK(device_handle_); | 189 DCHECK(device_handle_); |
190 const UsbConfigDescriptor* config = device_->GetActiveConfiguration(); | 190 const UsbConfigDescriptor* config = device_->GetActiveConfiguration(); |
191 | 191 |
192 if (recipient == CONTROL_TRANSFER_RECIPIENT_INTERFACE || | 192 if (recipient == ControlTransferRecipient::INTERFACE || |
193 recipient == CONTROL_TRANSFER_RECIPIENT_ENDPOINT) { | 193 recipient == ControlTransferRecipient::ENDPOINT) { |
194 if (!config) { | 194 if (!config) { |
195 callback.Run(false); | 195 callback.Run(false); |
196 return; | 196 return; |
197 } | 197 } |
198 | 198 |
199 uint8_t interface_number = index & 0xff; | 199 uint8_t interface_number = index & 0xff; |
200 if (recipient == CONTROL_TRANSFER_RECIPIENT_ENDPOINT) { | 200 if (recipient == ControlTransferRecipient::ENDPOINT) { |
201 if (!device_handle_->FindInterfaceByEndpoint(index & 0xff, | 201 if (!device_handle_->FindInterfaceByEndpoint(index & 0xff, |
202 &interface_number)) { | 202 &interface_number)) { |
203 callback.Run(false); | 203 callback.Run(false); |
204 return; | 204 return; |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 permission_provider_->HasInterfacePermission( | 208 permission_provider_->HasInterfacePermission( |
209 interface_number, config->configuration_value, | 209 interface_number, config->configuration_value, |
210 DeviceInfo::From(*device_), callback); | 210 DeviceInfo::From(*device_), callback); |
211 } else if (config) { | 211 } else if (config) { |
212 permission_provider_->HasConfigurationPermission( | 212 permission_provider_->HasConfigurationPermission( |
213 config->configuration_value, DeviceInfo::From(*device_), callback); | 213 config->configuration_value, DeviceInfo::From(*device_), callback); |
214 } else { | 214 } else { |
215 // Client must already have device permission to have gotten this far. | 215 // Client must already have device permission to have gotten this far. |
216 callback.Run(true); | 216 callback.Run(true); |
217 } | 217 } |
218 } | 218 } |
219 | 219 |
220 void DeviceImpl::OnOpen(const OpenCallback& callback, | 220 void DeviceImpl::OnOpen(const OpenCallback& callback, |
221 scoped_refptr<UsbDeviceHandle> handle) { | 221 scoped_refptr<UsbDeviceHandle> handle) { |
222 device_handle_ = handle; | 222 device_handle_ = handle; |
223 callback.Run(handle ? OPEN_DEVICE_ERROR_OK : OPEN_DEVICE_ERROR_ACCESS_DENIED); | 223 callback.Run(handle ? OpenDeviceError::OK : OpenDeviceError::ACCESS_DENIED); |
224 } | 224 } |
225 | 225 |
226 void DeviceImpl::GetDeviceInfo(const GetDeviceInfoCallback& callback) { | 226 void DeviceImpl::GetDeviceInfo(const GetDeviceInfoCallback& callback) { |
227 callback.Run(DeviceInfo::From(*device_)); | 227 callback.Run(DeviceInfo::From(*device_)); |
228 } | 228 } |
229 | 229 |
230 void DeviceImpl::GetConfiguration(const GetConfigurationCallback& callback) { | 230 void DeviceImpl::GetConfiguration(const GetConfigurationCallback& callback) { |
231 const UsbConfigDescriptor* config = device_->GetActiveConfiguration(); | 231 const UsbConfigDescriptor* config = device_->GetActiveConfiguration(); |
232 callback.Run(config ? config->configuration_value : 0); | 232 callback.Run(config ? config->configuration_value : 0); |
233 } | 233 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 318 } |
319 | 319 |
320 device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback)); | 320 device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback)); |
321 } | 321 } |
322 | 322 |
323 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, | 323 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, |
324 uint32_t length, | 324 uint32_t length, |
325 uint32_t timeout, | 325 uint32_t timeout, |
326 const ControlTransferInCallback& callback) { | 326 const ControlTransferInCallback& callback) { |
327 if (!device_handle_) { | 327 if (!device_handle_) { |
328 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<uint8_t>()); | 328 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); |
329 return; | 329 return; |
330 } | 330 } |
331 | 331 |
332 auto callback_ptr = make_scoped_ptr(new ControlTransferInCallback(callback)); | 332 auto callback_ptr = make_scoped_ptr(new ControlTransferInCallback(callback)); |
333 ControlTransferRecipient recipient = params->recipient; | 333 ControlTransferRecipient recipient = params->recipient; |
334 uint16_t index = params->index; | 334 uint16_t index = params->index; |
335 HasControlTransferPermission( | 335 HasControlTransferPermission( |
336 recipient, index, | 336 recipient, index, |
337 base::Bind(&OnControlTransferInPermissionCheckComplete, device_handle_, | 337 base::Bind(&OnControlTransferInPermissionCheckComplete, device_handle_, |
338 base::Passed(¶ms), length, timeout, | 338 base::Passed(¶ms), length, timeout, |
339 base::Passed(&callback_ptr))); | 339 base::Passed(&callback_ptr))); |
340 } | 340 } |
341 | 341 |
342 void DeviceImpl::ControlTransferOut( | 342 void DeviceImpl::ControlTransferOut( |
343 ControlTransferParamsPtr params, | 343 ControlTransferParamsPtr params, |
344 mojo::Array<uint8_t> data, | 344 mojo::Array<uint8_t> data, |
345 uint32_t timeout, | 345 uint32_t timeout, |
346 const ControlTransferOutCallback& callback) { | 346 const ControlTransferOutCallback& callback) { |
347 if (!device_handle_) { | 347 if (!device_handle_) { |
348 callback.Run(TRANSFER_STATUS_ERROR); | 348 callback.Run(TransferStatus::TRANSFER_ERROR); |
349 return; | 349 return; |
350 } | 350 } |
351 | 351 |
352 auto callback_ptr = make_scoped_ptr(new ControlTransferOutCallback(callback)); | 352 auto callback_ptr = make_scoped_ptr(new ControlTransferOutCallback(callback)); |
353 ControlTransferRecipient recipient = params->recipient; | 353 ControlTransferRecipient recipient = params->recipient; |
354 uint16_t index = params->index; | 354 uint16_t index = params->index; |
355 HasControlTransferPermission( | 355 HasControlTransferPermission( |
356 recipient, index, | 356 recipient, index, |
357 base::Bind(&OnControlTransferOutPermissionCheckComplete, device_handle_, | 357 base::Bind(&OnControlTransferOutPermissionCheckComplete, device_handle_, |
358 base::Passed(¶ms), base::Passed(&data), timeout, | 358 base::Passed(¶ms), base::Passed(&data), timeout, |
359 base::Passed(&callback_ptr))); | 359 base::Passed(&callback_ptr))); |
360 } | 360 } |
361 | 361 |
362 void DeviceImpl::GenericTransferIn(uint8_t endpoint_number, | 362 void DeviceImpl::GenericTransferIn(uint8_t endpoint_number, |
363 uint32_t length, | 363 uint32_t length, |
364 uint32_t timeout, | 364 uint32_t timeout, |
365 const GenericTransferInCallback& callback) { | 365 const GenericTransferInCallback& callback) { |
366 if (!device_handle_) { | 366 if (!device_handle_) { |
367 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<uint8_t>()); | 367 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); |
368 return; | 368 return; |
369 } | 369 } |
370 | 370 |
371 auto callback_ptr = make_scoped_ptr(new GenericTransferInCallback(callback)); | 371 auto callback_ptr = make_scoped_ptr(new GenericTransferInCallback(callback)); |
372 uint8_t endpoint_address = endpoint_number | 0x80; | 372 uint8_t endpoint_address = endpoint_number | 0x80; |
373 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); | 373 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(length); |
374 device_handle_->GenericTransfer( | 374 device_handle_->GenericTransfer( |
375 USB_DIRECTION_INBOUND, endpoint_address, buffer, length, timeout, | 375 USB_DIRECTION_INBOUND, endpoint_address, buffer, length, timeout, |
376 base::Bind(&OnTransferIn, base::Passed(&callback_ptr))); | 376 base::Bind(&OnTransferIn, base::Passed(&callback_ptr))); |
377 } | 377 } |
378 | 378 |
379 void DeviceImpl::GenericTransferOut( | 379 void DeviceImpl::GenericTransferOut( |
380 uint8_t endpoint_number, | 380 uint8_t endpoint_number, |
381 mojo::Array<uint8_t> data, | 381 mojo::Array<uint8_t> data, |
382 uint32_t timeout, | 382 uint32_t timeout, |
383 const GenericTransferOutCallback& callback) { | 383 const GenericTransferOutCallback& callback) { |
384 if (!device_handle_) { | 384 if (!device_handle_) { |
385 callback.Run(TRANSFER_STATUS_ERROR); | 385 callback.Run(TransferStatus::TRANSFER_ERROR); |
386 return; | 386 return; |
387 } | 387 } |
388 | 388 |
389 auto callback_ptr = make_scoped_ptr(new GenericTransferOutCallback(callback)); | 389 auto callback_ptr = make_scoped_ptr(new GenericTransferOutCallback(callback)); |
390 uint8_t endpoint_address = endpoint_number; | 390 uint8_t endpoint_address = endpoint_number; |
391 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); | 391 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(data.size()); |
392 const std::vector<uint8_t>& storage = data.storage(); | 392 const std::vector<uint8_t>& storage = data.storage(); |
393 std::copy(storage.begin(), storage.end(), buffer->data()); | 393 std::copy(storage.begin(), storage.end(), buffer->data()); |
394 device_handle_->GenericTransfer( | 394 device_handle_->GenericTransfer( |
395 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, data.size(), timeout, | 395 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, data.size(), timeout, |
396 base::Bind(&OnTransferOut, base::Passed(&callback_ptr))); | 396 base::Bind(&OnTransferOut, base::Passed(&callback_ptr))); |
397 } | 397 } |
398 | 398 |
399 void DeviceImpl::IsochronousTransferIn( | 399 void DeviceImpl::IsochronousTransferIn( |
400 uint8_t endpoint_number, | 400 uint8_t endpoint_number, |
401 uint32_t num_packets, | 401 uint32_t num_packets, |
402 uint32_t packet_length, | 402 uint32_t packet_length, |
403 uint32_t timeout, | 403 uint32_t timeout, |
404 const IsochronousTransferInCallback& callback) { | 404 const IsochronousTransferInCallback& callback) { |
405 if (!device_handle_) { | 405 if (!device_handle_) { |
406 callback.Run(TRANSFER_STATUS_ERROR, mojo::Array<mojo::Array<uint8_t>>()); | 406 callback.Run(TransferStatus::TRANSFER_ERROR, |
| 407 mojo::Array<mojo::Array<uint8_t>>()); |
407 return; | 408 return; |
408 } | 409 } |
409 | 410 |
410 auto callback_ptr = | 411 auto callback_ptr = |
411 make_scoped_ptr(new IsochronousTransferInCallback(callback)); | 412 make_scoped_ptr(new IsochronousTransferInCallback(callback)); |
412 uint8_t endpoint_address = endpoint_number | 0x80; | 413 uint8_t endpoint_address = endpoint_number | 0x80; |
413 size_t transfer_size = static_cast<size_t>(num_packets) * packet_length; | 414 size_t transfer_size = static_cast<size_t>(num_packets) * packet_length; |
414 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size); | 415 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size); |
415 device_handle_->IsochronousTransfer( | 416 device_handle_->IsochronousTransfer( |
416 USB_DIRECTION_INBOUND, endpoint_address, buffer, transfer_size, | 417 USB_DIRECTION_INBOUND, endpoint_address, buffer, transfer_size, |
417 num_packets, packet_length, timeout, | 418 num_packets, packet_length, timeout, |
418 base::Bind(&OnIsochronousTransferIn, base::Passed(&callback_ptr), | 419 base::Bind(&OnIsochronousTransferIn, base::Passed(&callback_ptr), |
419 packet_length)); | 420 packet_length)); |
420 } | 421 } |
421 | 422 |
422 void DeviceImpl::IsochronousTransferOut( | 423 void DeviceImpl::IsochronousTransferOut( |
423 uint8_t endpoint_number, | 424 uint8_t endpoint_number, |
424 mojo::Array<mojo::Array<uint8_t>> packets, | 425 mojo::Array<mojo::Array<uint8_t>> packets, |
425 uint32_t timeout, | 426 uint32_t timeout, |
426 const IsochronousTransferOutCallback& callback) { | 427 const IsochronousTransferOutCallback& callback) { |
427 if (!device_handle_) { | 428 if (!device_handle_) { |
428 callback.Run(TRANSFER_STATUS_ERROR); | 429 callback.Run(TransferStatus::TRANSFER_ERROR); |
429 return; | 430 return; |
430 } | 431 } |
431 | 432 |
432 auto callback_ptr = | 433 auto callback_ptr = |
433 make_scoped_ptr(new IsochronousTransferOutCallback(callback)); | 434 make_scoped_ptr(new IsochronousTransferOutCallback(callback)); |
434 uint8_t endpoint_address = endpoint_number; | 435 uint8_t endpoint_address = endpoint_number; |
435 uint32_t packet_size = 0; | 436 uint32_t packet_size = 0; |
436 for (size_t i = 0; i < packets.size(); ++i) { | 437 for (size_t i = 0; i < packets.size(); ++i) { |
437 packet_size = | 438 packet_size = |
438 std::max(packet_size, static_cast<uint32_t>(packets[i].size())); | 439 std::max(packet_size, static_cast<uint32_t>(packets[i].size())); |
439 } | 440 } |
440 size_t transfer_size = packet_size * packets.size(); | 441 size_t transfer_size = packet_size * packets.size(); |
441 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size); | 442 scoped_refptr<net::IOBuffer> buffer = CreateTransferBuffer(transfer_size); |
442 memset(buffer->data(), 0, transfer_size); | 443 memset(buffer->data(), 0, transfer_size); |
443 for (size_t i = 0; i < packets.size(); ++i) { | 444 for (size_t i = 0; i < packets.size(); ++i) { |
444 uint8_t* packet = | 445 uint8_t* packet = |
445 reinterpret_cast<uint8_t*>(&buffer->data()[i * packet_size]); | 446 reinterpret_cast<uint8_t*>(&buffer->data()[i * packet_size]); |
446 DCHECK_LE(packets[i].size(), static_cast<size_t>(packet_size)); | 447 DCHECK_LE(packets[i].size(), static_cast<size_t>(packet_size)); |
447 memcpy(packet, packets[i].storage().data(), packets[i].size()); | 448 memcpy(packet, packets[i].storage().data(), packets[i].size()); |
448 } | 449 } |
449 device_handle_->IsochronousTransfer( | 450 device_handle_->IsochronousTransfer( |
450 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, transfer_size, | 451 USB_DIRECTION_OUTBOUND, endpoint_address, buffer, transfer_size, |
451 static_cast<uint32_t>(packets.size()), packet_size, timeout, | 452 static_cast<uint32_t>(packets.size()), packet_size, timeout, |
452 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); | 453 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); |
453 } | 454 } |
454 | 455 |
455 } // namespace usb | 456 } // namespace usb |
456 } // namespace device | 457 } // namespace device |
OLD | NEW |