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..a5f813b74f0b31f4cf6e334004213f68f42d1649 100644 |
--- a/device/usb/usb_device_impl.cc |
+++ b/device/usb/usb_device_impl.cc |
@@ -89,6 +89,60 @@ UsbUsageType GetUsageType(const libusb_endpoint_descriptor* descriptor) { |
} |
} |
+void SetConfigDescriptor(const libusb_config_descriptor* platform_config, |
+ 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 +162,7 @@ UsbDeviceImpl::UsbDeviceImpl( |
blocking_task_runner_(blocking_task_runner) { |
CHECK(platform_device) << "platform_device cannot be NULL"; |
libusb_ref_device(platform_device); |
+ GetAllConfigurations(); |
RefreshConfiguration(); |
} |
@@ -159,11 +214,16 @@ bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { |
return false; |
} |
-const UsbConfigDescriptor* UsbDeviceImpl::GetConfiguration() { |
+const UsbConfigDescriptor* UsbDeviceImpl::GetCurrentConfiguration() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
return configuration_.get(); |
} |
+const std::vector<UsbConfigDescriptor>& UsbDeviceImpl::configurations() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ return configurations_; |
+} |
+ |
void UsbDeviceImpl::OnDisconnect() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
@@ -188,58 +248,33 @@ void UsbDeviceImpl::RefreshConfiguration() { |
} |
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); |
+ SetConfigDescriptor(platform_config, configuration_.get()); |
Reilly Grant (use Gerrit)
2015/07/31 23:36:12
Instead of reparsing platform_config just find the
juncai
2015/08/03 17:46:01
Done.
|
+ libusb_free_config_descriptor(platform_config); |
+} |
- interface.endpoints.push_back(endpoint); |
+void UsbDeviceImpl::GetAllConfigurations() { |
+ 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; |
+ 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); |
+ return; |
} |
- 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); |
+ 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); |
} |
- |
- configuration_->extra_data = std::vector<uint8_t>( |
- platform_config->extra, |
- platform_config->extra + platform_config->extra_length); |
- |
- libusb_free_config_descriptor(platform_config); |
} |
#if defined(OS_CHROMEOS) |