| 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 <map> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "device/usb/usb_device.h" | |
| 13 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 | |
| 17 // static | |
| 18 device::UsbDeviceFilter | |
| 19 TypeConverter<device::UsbDeviceFilter, device::usb::DeviceFilterPtr>::Convert( | |
| 20 const device::usb::DeviceFilterPtr& mojo_filter) { | |
| 21 device::UsbDeviceFilter filter; | |
| 22 if (mojo_filter->has_vendor_id) | |
| 23 filter.SetVendorId(mojo_filter->vendor_id); | |
| 24 if (mojo_filter->has_product_id) | |
| 25 filter.SetProductId(mojo_filter->product_id); | |
| 26 if (mojo_filter->has_class_code) | |
| 27 filter.SetInterfaceClass(mojo_filter->class_code); | |
| 28 if (mojo_filter->has_subclass_code) | |
| 29 filter.SetInterfaceSubclass(mojo_filter->subclass_code); | |
| 30 if (mojo_filter->has_protocol_code) | |
| 31 filter.SetInterfaceProtocol(mojo_filter->protocol_code); | |
| 32 return filter; | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 device::usb::TransferDirection | |
| 37 TypeConverter<device::usb::TransferDirection, device::UsbEndpointDirection>:: | |
| 38 Convert(const device::UsbEndpointDirection& direction) { | |
| 39 if (direction == device::USB_DIRECTION_INBOUND) | |
| 40 return device::usb::TRANSFER_DIRECTION_IN; | |
| 41 DCHECK(direction == device::USB_DIRECTION_OUTBOUND); | |
| 42 return device::usb::TRANSFER_DIRECTION_OUT; | |
| 43 } | |
| 44 | |
| 45 // static | |
| 46 device::usb::EndpointType | |
| 47 TypeConverter<device::usb::EndpointType, device::UsbTransferType>::Convert( | |
| 48 const device::UsbTransferType& type) { | |
| 49 switch (type) { | |
| 50 case device::USB_TRANSFER_ISOCHRONOUS: | |
| 51 return device::usb::ENDPOINT_TYPE_ISOCHRONOUS; | |
| 52 case device::USB_TRANSFER_BULK: | |
| 53 return device::usb::ENDPOINT_TYPE_BULK; | |
| 54 case device::USB_TRANSFER_INTERRUPT: | |
| 55 return device::usb::ENDPOINT_TYPE_INTERRUPT; | |
| 56 // Note that we do not expose control transfer in the public interface | |
| 57 // because control endpoints are implied rather than explicitly enumerated | |
| 58 // there. | |
| 59 default: | |
| 60 NOTREACHED(); | |
| 61 return device::usb::ENDPOINT_TYPE_BULK; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 device::usb::EndpointInfoPtr | |
| 67 TypeConverter<device::usb::EndpointInfoPtr, device::UsbEndpointDescriptor>:: | |
| 68 Convert(const device::UsbEndpointDescriptor& endpoint) { | |
| 69 device::usb::EndpointInfoPtr info = device::usb::EndpointInfo::New(); | |
| 70 info->endpoint_number = endpoint.address; | |
| 71 info->direction = | |
| 72 ConvertTo<device::usb::TransferDirection>(endpoint.direction); | |
| 73 info->type = ConvertTo<device::usb::EndpointType>(endpoint.transfer_type); | |
| 74 info->packet_size = static_cast<uint32_t>(endpoint.maximum_packet_size); | |
| 75 return info.Pass(); | |
| 76 } | |
| 77 | |
| 78 // static | |
| 79 device::usb::AlternateInterfaceInfoPtr | |
| 80 TypeConverter<device::usb::AlternateInterfaceInfoPtr, | |
| 81 device::UsbInterfaceDescriptor>:: | |
| 82 Convert(const device::UsbInterfaceDescriptor& interface) { | |
| 83 device::usb::AlternateInterfaceInfoPtr info = | |
| 84 device::usb::AlternateInterfaceInfo::New(); | |
| 85 info->alternate_setting = interface.alternate_setting; | |
| 86 info->class_code = interface.interface_class; | |
| 87 info->subclass_code = interface.interface_subclass; | |
| 88 info->protocol_code = interface.interface_protocol; | |
| 89 | |
| 90 // Filter out control endpoints for the public interface. | |
| 91 info->endpoints = mojo::Array<device::usb::EndpointInfoPtr>::New(0); | |
| 92 for (const auto& endpoint : interface.endpoints) { | |
| 93 if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL) | |
| 94 info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint)); | |
| 95 } | |
| 96 | |
| 97 return info.Pass(); | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 mojo::Array<device::usb::InterfaceInfoPtr> | |
| 102 TypeConverter<mojo::Array<device::usb::InterfaceInfoPtr>, | |
| 103 std::vector<device::UsbInterfaceDescriptor>>:: | |
| 104 Convert(const std::vector<device::UsbInterfaceDescriptor>& interfaces) { | |
| 105 auto infos = mojo::Array<device::usb::InterfaceInfoPtr>::New(0); | |
| 106 | |
| 107 // Aggregate each alternate setting into an InterfaceInfo corresponding to its | |
| 108 // interface number. | |
| 109 std::map<uint8_t, device::usb::InterfaceInfo*> interface_map; | |
| 110 for (size_t i = 0; i < interfaces.size(); ++i) { | |
| 111 auto alternate = device::usb::AlternateInterfaceInfo::From(interfaces[i]); | |
| 112 auto iter = interface_map.find(interfaces[i].interface_number); | |
| 113 if (iter == interface_map.end()) { | |
| 114 // This is the first time we're seeing an alternate with this interface | |
| 115 // number, so add a new InterfaceInfo to the array and map the number. | |
| 116 auto info = device::usb::InterfaceInfo::New(); | |
| 117 iter = interface_map.insert(std::make_pair(interfaces[i].interface_number, | |
| 118 info.get())).first; | |
| 119 infos.push_back(info.Pass()); | |
| 120 } | |
| 121 iter->second->alternates.push_back(alternate.Pass()); | |
| 122 } | |
| 123 | |
| 124 return infos.Pass(); | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 device::usb::ConfigurationInfoPtr | |
| 129 TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>:: | |
| 130 Convert(const device::UsbConfigDescriptor& config) { | |
| 131 device::usb::ConfigurationInfoPtr info = | |
| 132 device::usb::ConfigurationInfo::New(); | |
| 133 info->configuration_value = config.configuration_value; | |
| 134 info->interfaces = | |
| 135 mojo::Array<device::usb::InterfaceInfoPtr>::From(config.interfaces); | |
| 136 return info.Pass(); | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 device::usb::DeviceInfoPtr | |
| 141 TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert( | |
| 142 const device::UsbDevice& device) { | |
| 143 device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New(); | |
| 144 info->vendor_id = device.vendor_id(); | |
| 145 info->product_id = device.product_id(); | |
| 146 info->manufacturer = base::UTF16ToUTF8(device.manufacturer_string()); | |
| 147 info->product = base::UTF16ToUTF8(device.product_string()); | |
| 148 info->serial_number = base::UTF16ToUTF8(device.serial_number()); | |
| 149 return info.Pass(); | |
| 150 } | |
| 151 | |
| 152 } // namespace mojo | |
| OLD | NEW |