Chromium Code Reviews| Index: device/usb/usb_device_impl.cc |
| diff --git a/device/usb/usb_device_impl.cc b/device/usb/usb_device_impl.cc |
| index 114e6acaa568fec23630556cd6347d1519aec033..87bb6cbd7d9a99da99dbdca20d45398400a7a8ce 100644 |
| --- a/device/usb/usb_device_impl.cc |
| +++ b/device/usb/usb_device_impl.cc |
| @@ -14,7 +14,6 @@ |
| #include "base/thread_task_runner_handle.h" |
| #include "components/device_event_log/device_event_log.h" |
| #include "device/usb/usb_context.h" |
| -#include "device/usb/usb_descriptors.h" |
| #include "device/usb/usb_device_handle_impl.h" |
| #include "device/usb/usb_error.h" |
| #include "third_party/libusb/src/libusb/libusb.h" |
| @@ -89,6 +88,60 @@ UsbUsageType GetUsageType(const libusb_endpoint_descriptor* descriptor) { |
| } |
| } |
| +void SetConfigDescriptor(const libusb_config_descriptor* platform_config, |
|
Reilly Grant (use Gerrit)
2015/08/03 18:04:04
I suggest renaming this to "ConvertConfigDescripto
juncai
2015/08/03 21:56:25
Done.
|
| + UsbConfigDescriptor* configuration) { |
| + configuration->configuration_value = platform_config->bConfigurationValue; |
| + configuration->self_powered = (platform_config->bmAttributes & 0x40) != 0; |
| + configuration->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0; |
| + configuration->maximum_power = platform_config->MaxPower * 2; |
| + |
| + for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) { |
| + const struct libusb_interface* platform_interface = |
| + &platform_config->interface[i]; |
| + for (int j = 0; j < platform_interface->num_altsetting; ++j) { |
| + const struct libusb_interface_descriptor* platform_alt_setting = |
| + &platform_interface->altsetting[j]; |
| + UsbInterfaceDescriptor interface; |
| + |
| + interface.interface_number = platform_alt_setting->bInterfaceNumber; |
| + interface.alternate_setting = platform_alt_setting->bAlternateSetting; |
| + interface.interface_class = platform_alt_setting->bInterfaceClass; |
| + interface.interface_subclass = platform_alt_setting->bInterfaceSubClass; |
| + interface.interface_protocol = platform_alt_setting->bInterfaceProtocol; |
| + |
| + for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) { |
| + const struct libusb_endpoint_descriptor* platform_endpoint = |
| + &platform_alt_setting->endpoint[k]; |
| + UsbEndpointDescriptor endpoint; |
| + |
| + endpoint.address = platform_endpoint->bEndpointAddress; |
| + endpoint.direction = GetDirection(platform_endpoint); |
| + endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize; |
| + endpoint.synchronization_type = |
| + GetSynchronizationType(platform_endpoint); |
| + endpoint.transfer_type = GetTransferType(platform_endpoint); |
| + endpoint.usage_type = GetUsageType(platform_endpoint); |
| + endpoint.polling_interval = platform_endpoint->bInterval; |
| + endpoint.extra_data = std::vector<uint8_t>( |
| + platform_endpoint->extra, |
| + platform_endpoint->extra + platform_endpoint->extra_length); |
| + |
| + interface.endpoints.push_back(endpoint); |
| + } |
| + |
| + interface.extra_data = std::vector<uint8_t>( |
| + platform_alt_setting->extra, |
| + platform_alt_setting->extra + platform_alt_setting->extra_length); |
| + |
| + configuration->interfaces.push_back(interface); |
| + } |
| + } |
| + |
| + configuration->extra_data = std::vector<uint8_t>( |
| + platform_config->extra, |
| + platform_config->extra + platform_config->extra_length); |
| +} |
| + |
| } // namespace |
| UsbDeviceImpl::UsbDeviceImpl( |
| @@ -108,6 +161,7 @@ UsbDeviceImpl::UsbDeviceImpl( |
| blocking_task_runner_(blocking_task_runner) { |
| CHECK(platform_device) << "platform_device cannot be NULL"; |
| libusb_ref_device(platform_device); |
| + set_configurations(); |
|
Reilly Grant (use Gerrit)
2015/08/03 18:04:04
This function is non-trivial and so cannot use thi
juncai
2015/08/03 21:56:25
Done.
|
| RefreshConfiguration(); |
|
Reilly Grant (use Gerrit)
2015/08/03 18:04:04
I suggest renaming this to "RefreshActiveConfigura
juncai
2015/08/03 21:56:25
Done.
|
| } |
| @@ -159,9 +213,9 @@ bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { |
| return false; |
| } |
| -const UsbConfigDescriptor* UsbDeviceImpl::GetConfiguration() { |
| +const UsbConfigDescriptor* UsbDeviceImpl::GetActiveConfiguration() { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - return configuration_.get(); |
| + return configuration_; |
| } |
| void UsbDeviceImpl::OnDisconnect() { |
| @@ -177,6 +231,32 @@ void UsbDeviceImpl::OnDisconnect() { |
| } |
| } |
| +void UsbDeviceImpl::set_configurations() { |
| + libusb_device_descriptor device_descriptor; |
| + int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); |
| + if (rv == LIBUSB_SUCCESS) { |
| + uint8_t num_configurations = device_descriptor.bNumConfigurations; |
| + configurations_.reserve(num_configurations); |
| + for (uint8_t i = 0; i < num_configurations; i++) { |
| + libusb_config_descriptor* platform_config; |
| + rv = libusb_get_config_descriptor(platform_device_, i, &platform_config); |
| + if (rv != LIBUSB_SUCCESS) { |
| + USB_LOG(EVENT) << "Failed to get config descriptor: " |
| + << ConvertPlatformUsbErrorToString(rv); |
| + continue; |
| + } |
| + |
| + UsbConfigDescriptor config_descriptor; |
| + SetConfigDescriptor(platform_config, &config_descriptor); |
| + configurations_.push_back(config_descriptor); |
| + libusb_free_config_descriptor(platform_config); |
| + } |
| + } else { |
| + USB_LOG(EVENT) << "Failed to get device descriptor: " |
| + << ConvertPlatformUsbErrorToString(rv); |
| + } |
| +} |
| + |
| void UsbDeviceImpl::RefreshConfiguration() { |
| libusb_config_descriptor* platform_config; |
| int rv = |
| @@ -187,58 +267,16 @@ void UsbDeviceImpl::RefreshConfiguration() { |
| return; |
| } |
| - configuration_.reset(new UsbConfigDescriptor()); |
| - configuration_->configuration_value = platform_config->bConfigurationValue; |
| - configuration_->self_powered = (platform_config->bmAttributes & 0x40) != 0; |
| - configuration_->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0; |
| - configuration_->maximum_power = platform_config->MaxPower * 2; |
| - |
| - for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) { |
| - const struct libusb_interface* platform_interface = |
| - &platform_config->interface[i]; |
| - for (int j = 0; j < platform_interface->num_altsetting; ++j) { |
| - const struct libusb_interface_descriptor* platform_alt_setting = |
| - &platform_interface->altsetting[j]; |
| - UsbInterfaceDescriptor interface; |
| - |
| - interface.interface_number = platform_alt_setting->bInterfaceNumber; |
| - interface.alternate_setting = platform_alt_setting->bAlternateSetting; |
| - interface.interface_class = platform_alt_setting->bInterfaceClass; |
| - interface.interface_subclass = platform_alt_setting->bInterfaceSubClass; |
| - interface.interface_protocol = platform_alt_setting->bInterfaceProtocol; |
| - |
| - for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) { |
| - const struct libusb_endpoint_descriptor* platform_endpoint = |
| - &platform_alt_setting->endpoint[k]; |
| - UsbEndpointDescriptor endpoint; |
| - |
| - endpoint.address = platform_endpoint->bEndpointAddress; |
| - endpoint.direction = GetDirection(platform_endpoint); |
| - endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize; |
| - endpoint.synchronization_type = |
| - GetSynchronizationType(platform_endpoint); |
| - endpoint.transfer_type = GetTransferType(platform_endpoint); |
| - endpoint.usage_type = GetUsageType(platform_endpoint); |
| - endpoint.polling_interval = platform_endpoint->bInterval; |
| - endpoint.extra_data = std::vector<uint8_t>( |
| - platform_endpoint->extra, |
| - platform_endpoint->extra + platform_endpoint->extra_length); |
| - |
| - interface.endpoints.push_back(endpoint); |
| - } |
| - |
| - interface.extra_data = std::vector<uint8_t>( |
| - platform_alt_setting->extra, |
| - platform_alt_setting->extra + platform_alt_setting->extra_length); |
| - |
| - configuration_->interfaces.push_back(interface); |
| + configuration_ = nullptr; |
| + size_t num_configurations = configurations_.size(); |
| + for (size_t i = 0; i < num_configurations; ++i) { |
| + if (configurations_[i].configuration_value == |
| + platform_config->bConfigurationValue) { |
| + configuration_ = &configurations_[i]; |
| + break; |
| } |
| } |
| - configuration_->extra_data = std::vector<uint8_t>( |
| - platform_config->extra, |
| - platform_config->extra + platform_config->extra_length); |
| - |
| libusb_free_config_descriptor(platform_config); |
| } |