Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/usb/type_converters.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "device/usb/usb_device.h" | |
| 10 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 | |
| 14 // static | |
| 15 device::UsbDeviceFilter | |
| 16 TypeConverter<device::UsbDeviceFilter, device::usb::DeviceFilterPtr>::Convert( | |
| 17 const device::usb::DeviceFilterPtr& mojo_filter) { | |
| 18 device::UsbDeviceFilter filter; | |
| 19 if (mojo_filter->has_vendor_id) | |
| 20 filter.SetVendorId(mojo_filter->vendor_id); | |
| 21 if (mojo_filter->has_product_id) | |
| 22 filter.SetProductId(mojo_filter->product_id); | |
| 23 if (mojo_filter->has_class_code) | |
| 24 filter.SetInterfaceClass(mojo_filter->class_code); | |
| 25 if (mojo_filter->has_subclass_code) | |
| 26 filter.SetInterfaceSubclass(mojo_filter->subclass_code); | |
| 27 if (mojo_filter->has_protocol_code) | |
| 28 filter.SetInterfaceProtocol(mojo_filter->protocol_code); | |
| 29 return filter; | |
| 30 } | |
| 31 | |
| 32 // static | |
| 33 device::usb::TransferDirection | |
| 34 TypeConverter<device::usb::TransferDirection, device::UsbEndpointDirection>:: | |
| 35 Convert(const device::UsbEndpointDirection& direction) { | |
| 36 if (direction == device::USB_DIRECTION_INBOUND) | |
| 37 return device::usb::TRANSFER_DIRECTION_IN; | |
| 38 DCHECK(direction == device::USB_DIRECTION_OUTBOUND); | |
| 39 return device::usb::TRANSFER_DIRECTION_OUT; | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 device::usb::EndpointType | |
| 44 TypeConverter<device::usb::EndpointType, device::UsbTransferType>::Convert( | |
| 45 const device::UsbTransferType& type) { | |
| 46 switch (type) { | |
| 47 case device::USB_TRANSFER_ISOCHRONOUS: | |
| 48 return device::usb::ENDPOINT_TYPE_ISOCHRONOUS; | |
| 49 case device::USB_TRANSFER_BULK: | |
| 50 return device::usb::ENDPOINT_TYPE_BULK; | |
| 51 case device::USB_TRANSFER_INTERRUPT: | |
| 52 return device::usb::ENDPOINT_TYPE_INTERRUPT; | |
| 53 // Note that we do not expose control transfer in the public interface | |
| 54 // because control endpoints are implied rather than explicitly enumerated | |
| 55 // there. | |
| 56 default: | |
| 57 NOTREACHED(); | |
| 58 return device::usb::ENDPOINT_TYPE_BULK; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 device::usb::EndpointInfoPtr | |
| 64 TypeConverter<device::usb::EndpointInfoPtr, device::UsbEndpointDescriptor>:: | |
| 65 Convert(const device::UsbEndpointDescriptor& endpoint) { | |
| 66 device::usb::EndpointInfoPtr info = device::usb::EndpointInfo::New(); | |
| 67 info->endpoint_number = endpoint.address; | |
| 68 info->direction = | |
| 69 ConvertTo<device::usb::TransferDirection>(endpoint.direction); | |
| 70 info->type = ConvertTo<device::usb::EndpointType>(endpoint.transfer_type); | |
| 71 info->packet_size = static_cast<uint32_t>(endpoint.maximum_packet_size); | |
| 72 return info.Pass(); | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 device::usb::AlternateInterfaceInfoPtr | |
| 77 TypeConverter<device::usb::AlternateInterfaceInfoPtr, | |
| 78 device::UsbInterfaceDescriptor>:: | |
| 79 Convert(const device::UsbInterfaceDescriptor& interface) { | |
| 80 device::usb::AlternateInterfaceInfoPtr info = | |
| 81 device::usb::AlternateInterfaceInfo::New(); | |
| 82 info->alternate_setting = interface.alternate_setting; | |
| 83 info->class_code = interface.interface_class; | |
| 84 info->subclass_code = interface.interface_subclass; | |
| 85 info->protocol_code = interface.interface_protocol; | |
| 86 | |
| 87 // Filter out control endpoints for the public interface. | |
| 88 info->endpoints = mojo::Array<device::usb::EndpointInfoPtr>::New(0); | |
| 89 for (const auto& endpoint : interface.endpoints) { | |
| 90 if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL) | |
| 91 info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint)); | |
| 92 } | |
| 93 | |
| 94 return info.Pass(); | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 device::usb::InterfaceInfoPtr | |
| 99 TypeConverter<device::usb::InterfaceInfoPtr, device::UsbInterfaceDescriptor>:: | |
| 100 Convert(const device::UsbInterfaceDescriptor& interface) { | |
| 101 device::usb::InterfaceInfoPtr info = device::usb::InterfaceInfo::New(); | |
| 102 info->interface_number = interface.interface_number; | |
| 103 // TODO(rockot/reillyg): Support interface alternates other than the one | |
|
Reilly Grant (use Gerrit)
2015/05/27 21:00:53
You can implement this now. You will get multiple
Ken Rockot(use gerrit already)
2015/05/27 22:35:03
Ahhh. Done.
| |
| 104 // currently selected. | |
| 105 info->alternates = | |
| 106 mojo::Array<device::usb::AlternateInterfaceInfoPtr>::New(1); | |
| 107 info->alternates[0] = device::usb::AlternateInterfaceInfo::From(interface); | |
| 108 return info.Pass(); | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 device::usb::ConfigurationInfoPtr | |
| 113 TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>:: | |
| 114 Convert(const device::UsbConfigDescriptor& config) { | |
| 115 device::usb::ConfigurationInfoPtr info = | |
| 116 device::usb::ConfigurationInfo::New(); | |
| 117 info->configuration_value = config.configuration_value; | |
| 118 info->interfaces = | |
| 119 mojo::Array<device::usb::InterfaceInfoPtr>::From(config.interfaces); | |
| 120 return info.Pass(); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 device::usb::DeviceInfoPtr | |
| 125 TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert( | |
| 126 const device::UsbDevice& device) { | |
| 127 device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New(); | |
| 128 info->vendor_id = device.vendor_id(); | |
| 129 info->product_id = device.product_id(); | |
| 130 info->manufacturer = base::UTF16ToUTF8(device.manufacturer_string()); | |
| 131 info->product = base::UTF16ToUTF8(device.product_string()); | |
| 132 info->serial_number = base::UTF16ToUTF8(device.serial_number()); | |
| 133 return info.Pass(); | |
| 134 } | |
| 135 | |
| 136 } // namespace mojo | |
| OLD | NEW |