| 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::TransferStatus | |
| 47 TypeConverter<device::usb::TransferStatus, device::UsbTransferStatus>::Convert( | |
| 48 const device::UsbTransferStatus& status) { | |
| 49 switch (status) { | |
| 50 case device::USB_TRANSFER_COMPLETED: | |
| 51 return device::usb::TRANSFER_STATUS_COMPLETED; | |
| 52 case device::USB_TRANSFER_ERROR: | |
| 53 return device::usb::TRANSFER_STATUS_ERROR; | |
| 54 case device::USB_TRANSFER_TIMEOUT: | |
| 55 return device::usb::TRANSFER_STATUS_TIMEOUT; | |
| 56 case device::USB_TRANSFER_CANCELLED: | |
| 57 return device::usb::TRANSFER_STATUS_CANCELLED; | |
| 58 case device::USB_TRANSFER_STALLED: | |
| 59 return device::usb::TRANSFER_STATUS_STALLED; | |
| 60 case device::USB_TRANSFER_DISCONNECT: | |
| 61 return device::usb::TRANSFER_STATUS_DISCONNECT; | |
| 62 case device::USB_TRANSFER_OVERFLOW: | |
| 63 return device::usb::TRANSFER_STATUS_BABBLE; | |
| 64 case device::USB_TRANSFER_LENGTH_SHORT: | |
| 65 return device::usb::TRANSFER_STATUS_SHORT_PACKET; | |
| 66 default: | |
| 67 NOTREACHED(); | |
| 68 return device::usb::TRANSFER_STATUS_ERROR; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 device::UsbDeviceHandle::TransferRequestType | |
| 74 TypeConverter<device::UsbDeviceHandle::TransferRequestType, | |
| 75 device::usb::ControlTransferType>:: | |
| 76 Convert(const device::usb::ControlTransferType& type) { | |
| 77 switch (type) { | |
| 78 case device::usb::CONTROL_TRANSFER_TYPE_STANDARD: | |
| 79 return device::UsbDeviceHandle::STANDARD; | |
| 80 case device::usb::CONTROL_TRANSFER_TYPE_CLASS: | |
| 81 return device::UsbDeviceHandle::CLASS; | |
| 82 case device::usb::CONTROL_TRANSFER_TYPE_VENDOR: | |
| 83 return device::UsbDeviceHandle::VENDOR; | |
| 84 case device::usb::CONTROL_TRANSFER_TYPE_RESERVED: | |
| 85 return device::UsbDeviceHandle::RESERVED; | |
| 86 default: | |
| 87 NOTREACHED(); | |
| 88 return device::UsbDeviceHandle::RESERVED; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 device::UsbDeviceHandle::TransferRecipient | |
| 94 TypeConverter<device::UsbDeviceHandle::TransferRecipient, | |
| 95 device::usb::ControlTransferRecipient>:: | |
| 96 Convert(const device::usb::ControlTransferRecipient& recipient) { | |
| 97 switch (recipient) { | |
| 98 case device::usb::CONTROL_TRANSFER_RECIPIENT_DEVICE: | |
| 99 return device::UsbDeviceHandle::DEVICE; | |
| 100 case device::usb::CONTROL_TRANSFER_RECIPIENT_INTERFACE: | |
| 101 return device::UsbDeviceHandle::INTERFACE; | |
| 102 case device::usb::CONTROL_TRANSFER_RECIPIENT_ENDPOINT: | |
| 103 return device::UsbDeviceHandle::ENDPOINT; | |
| 104 case device::usb::CONTROL_TRANSFER_RECIPIENT_OTHER: | |
| 105 return device::UsbDeviceHandle::OTHER; | |
| 106 default: | |
| 107 NOTREACHED(); | |
| 108 return device::UsbDeviceHandle::OTHER; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 device::usb::EndpointType | |
| 114 TypeConverter<device::usb::EndpointType, device::UsbTransferType>::Convert( | |
| 115 const device::UsbTransferType& type) { | |
| 116 switch (type) { | |
| 117 case device::USB_TRANSFER_ISOCHRONOUS: | |
| 118 return device::usb::ENDPOINT_TYPE_ISOCHRONOUS; | |
| 119 case device::USB_TRANSFER_BULK: | |
| 120 return device::usb::ENDPOINT_TYPE_BULK; | |
| 121 case device::USB_TRANSFER_INTERRUPT: | |
| 122 return device::usb::ENDPOINT_TYPE_INTERRUPT; | |
| 123 // Note that we do not expose control transfer in the public interface | |
| 124 // because control endpoints are implied rather than explicitly enumerated | |
| 125 // there. | |
| 126 default: | |
| 127 NOTREACHED(); | |
| 128 return device::usb::ENDPOINT_TYPE_BULK; | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 // static | |
| 133 device::usb::EndpointInfoPtr | |
| 134 TypeConverter<device::usb::EndpointInfoPtr, device::UsbEndpointDescriptor>:: | |
| 135 Convert(const device::UsbEndpointDescriptor& endpoint) { | |
| 136 device::usb::EndpointInfoPtr info = device::usb::EndpointInfo::New(); | |
| 137 info->endpoint_number = endpoint.address; | |
| 138 info->direction = | |
| 139 ConvertTo<device::usb::TransferDirection>(endpoint.direction); | |
| 140 info->type = ConvertTo<device::usb::EndpointType>(endpoint.transfer_type); | |
| 141 info->packet_size = static_cast<uint32_t>(endpoint.maximum_packet_size); | |
| 142 return info.Pass(); | |
| 143 } | |
| 144 | |
| 145 // static | |
| 146 device::usb::AlternateInterfaceInfoPtr | |
| 147 TypeConverter<device::usb::AlternateInterfaceInfoPtr, | |
| 148 device::UsbInterfaceDescriptor>:: | |
| 149 Convert(const device::UsbInterfaceDescriptor& interface) { | |
| 150 device::usb::AlternateInterfaceInfoPtr info = | |
| 151 device::usb::AlternateInterfaceInfo::New(); | |
| 152 info->alternate_setting = interface.alternate_setting; | |
| 153 info->class_code = interface.interface_class; | |
| 154 info->subclass_code = interface.interface_subclass; | |
| 155 info->protocol_code = interface.interface_protocol; | |
| 156 | |
| 157 // Filter out control endpoints for the public interface. | |
| 158 info->endpoints = mojo::Array<device::usb::EndpointInfoPtr>::New(0); | |
| 159 for (const auto& endpoint : interface.endpoints) { | |
| 160 if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL) | |
| 161 info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint)); | |
| 162 } | |
| 163 | |
| 164 return info.Pass(); | |
| 165 } | |
| 166 | |
| 167 // static | |
| 168 mojo::Array<device::usb::InterfaceInfoPtr> | |
| 169 TypeConverter<mojo::Array<device::usb::InterfaceInfoPtr>, | |
| 170 std::vector<device::UsbInterfaceDescriptor>>:: | |
| 171 Convert(const std::vector<device::UsbInterfaceDescriptor>& interfaces) { | |
| 172 auto infos = mojo::Array<device::usb::InterfaceInfoPtr>::New(0); | |
| 173 | |
| 174 // Aggregate each alternate setting into an InterfaceInfo corresponding to its | |
| 175 // interface number. | |
| 176 std::map<uint8_t, device::usb::InterfaceInfo*> interface_map; | |
| 177 for (size_t i = 0; i < interfaces.size(); ++i) { | |
| 178 auto alternate = device::usb::AlternateInterfaceInfo::From(interfaces[i]); | |
| 179 auto iter = interface_map.find(interfaces[i].interface_number); | |
| 180 if (iter == interface_map.end()) { | |
| 181 // This is the first time we're seeing an alternate with this interface | |
| 182 // number, so add a new InterfaceInfo to the array and map the number. | |
| 183 auto info = device::usb::InterfaceInfo::New(); | |
| 184 iter = interface_map.insert(std::make_pair(interfaces[i].interface_number, | |
| 185 info.get())).first; | |
| 186 infos.push_back(info.Pass()); | |
| 187 } | |
| 188 iter->second->alternates.push_back(alternate.Pass()); | |
| 189 } | |
| 190 | |
| 191 return infos.Pass(); | |
| 192 } | |
| 193 | |
| 194 // static | |
| 195 device::usb::ConfigurationInfoPtr | |
| 196 TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>:: | |
| 197 Convert(const device::UsbConfigDescriptor& config) { | |
| 198 device::usb::ConfigurationInfoPtr info = | |
| 199 device::usb::ConfigurationInfo::New(); | |
| 200 info->configuration_value = config.configuration_value; | |
| 201 info->interfaces = | |
| 202 mojo::Array<device::usb::InterfaceInfoPtr>::From(config.interfaces); | |
| 203 return info.Pass(); | |
| 204 } | |
| 205 | |
| 206 // static | |
| 207 device::usb::DeviceInfoPtr | |
| 208 TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert( | |
| 209 const device::UsbDevice& device) { | |
| 210 device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New(); | |
| 211 info->guid = device.guid(); | |
| 212 info->vendor_id = device.vendor_id(); | |
| 213 info->product_id = device.product_id(); | |
| 214 info->manufacturer = base::UTF16ToUTF8(device.manufacturer_string()); | |
| 215 info->product = base::UTF16ToUTF8(device.product_string()); | |
| 216 info->serial_number = base::UTF16ToUTF8(device.serial_number()); | |
| 217 return info.Pass(); | |
| 218 } | |
| 219 | |
| 220 } // namespace mojo | |
| OLD | NEW |