| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/usb/mojo/type_converters.h" | 5 #include "device/usb/mojo/type_converters.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "device/usb/usb_device.h" | 15 #include "device/usb/usb_device.h" |
| 16 #include "mojo/common/common_type_converters.h" |
| 16 #include "mojo/public/cpp/bindings/array.h" | 17 #include "mojo/public/cpp/bindings/array.h" |
| 17 | 18 |
| 18 namespace mojo { | 19 namespace mojo { |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 device::UsbDeviceFilter | 22 device::UsbDeviceFilter |
| 22 TypeConverter<device::UsbDeviceFilter, device::usb::DeviceFilterPtr>::Convert( | 23 TypeConverter<device::UsbDeviceFilter, device::usb::DeviceFilterPtr>::Convert( |
| 23 const device::usb::DeviceFilterPtr& mojo_filter) { | 24 const device::usb::DeviceFilterPtr& mojo_filter) { |
| 24 device::UsbDeviceFilter filter; | 25 device::UsbDeviceFilter filter; |
| 25 if (mojo_filter->has_vendor_id) | 26 if (mojo_filter->has_vendor_id) |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 device::UsbInterfaceDescriptor>:: | 152 device::UsbInterfaceDescriptor>:: |
| 152 Convert(const device::UsbInterfaceDescriptor& interface) { | 153 Convert(const device::UsbInterfaceDescriptor& interface) { |
| 153 device::usb::AlternateInterfaceInfoPtr info = | 154 device::usb::AlternateInterfaceInfoPtr info = |
| 154 device::usb::AlternateInterfaceInfo::New(); | 155 device::usb::AlternateInterfaceInfo::New(); |
| 155 info->alternate_setting = interface.alternate_setting; | 156 info->alternate_setting = interface.alternate_setting; |
| 156 info->class_code = interface.interface_class; | 157 info->class_code = interface.interface_class; |
| 157 info->subclass_code = interface.interface_subclass; | 158 info->subclass_code = interface.interface_subclass; |
| 158 info->protocol_code = interface.interface_protocol; | 159 info->protocol_code = interface.interface_protocol; |
| 159 | 160 |
| 160 // Filter out control endpoints for the public interface. | 161 // Filter out control endpoints for the public interface. |
| 161 info->endpoints = mojo::Array<device::usb::EndpointInfoPtr>::New(0); | 162 info->endpoints.reserve(interface.endpoints.size()); |
| 162 for (const auto& endpoint : interface.endpoints) { | 163 for (const auto& endpoint : interface.endpoints) { |
| 163 if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL) | 164 if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL) |
| 164 info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint)); | 165 info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint)); |
| 165 } | 166 } |
| 166 | 167 |
| 167 return info; | 168 return info; |
| 168 } | 169 } |
| 169 | 170 |
| 170 // static | 171 // static |
| 171 mojo::Array<device::usb::InterfaceInfoPtr> | 172 std::vector<device::usb::InterfaceInfoPtr> |
| 172 TypeConverter<mojo::Array<device::usb::InterfaceInfoPtr>, | 173 TypeConverter<std::vector<device::usb::InterfaceInfoPtr>, |
| 173 std::vector<device::UsbInterfaceDescriptor>>:: | 174 std::vector<device::UsbInterfaceDescriptor>>:: |
| 174 Convert(const std::vector<device::UsbInterfaceDescriptor>& interfaces) { | 175 Convert(const std::vector<device::UsbInterfaceDescriptor>& interfaces) { |
| 175 auto infos = mojo::Array<device::usb::InterfaceInfoPtr>::New(0); | 176 std::vector<device::usb::InterfaceInfoPtr> infos; |
| 176 | 177 |
| 177 // Aggregate each alternate setting into an InterfaceInfo corresponding to its | 178 // Aggregate each alternate setting into an InterfaceInfo corresponding to its |
| 178 // interface number. | 179 // interface number. |
| 179 std::map<uint8_t, device::usb::InterfaceInfo*> interface_map; | 180 std::map<uint8_t, device::usb::InterfaceInfo*> interface_map; |
| 180 for (size_t i = 0; i < interfaces.size(); ++i) { | 181 for (size_t i = 0; i < interfaces.size(); ++i) { |
| 181 auto alternate = device::usb::AlternateInterfaceInfo::From(interfaces[i]); | 182 auto alternate = device::usb::AlternateInterfaceInfo::From(interfaces[i]); |
| 182 auto iter = interface_map.find(interfaces[i].interface_number); | 183 auto iter = interface_map.find(interfaces[i].interface_number); |
| 183 if (iter == interface_map.end()) { | 184 if (iter == interface_map.end()) { |
| 184 // This is the first time we're seeing an alternate with this interface | 185 // This is the first time we're seeing an alternate with this interface |
| 185 // number, so add a new InterfaceInfo to the array and map the number. | 186 // number, so add a new InterfaceInfo to the array and map the number. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 198 } | 199 } |
| 199 | 200 |
| 200 // static | 201 // static |
| 201 device::usb::ConfigurationInfoPtr | 202 device::usb::ConfigurationInfoPtr |
| 202 TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>:: | 203 TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>:: |
| 203 Convert(const device::UsbConfigDescriptor& config) { | 204 Convert(const device::UsbConfigDescriptor& config) { |
| 204 device::usb::ConfigurationInfoPtr info = | 205 device::usb::ConfigurationInfoPtr info = |
| 205 device::usb::ConfigurationInfo::New(); | 206 device::usb::ConfigurationInfo::New(); |
| 206 info->configuration_value = config.configuration_value; | 207 info->configuration_value = config.configuration_value; |
| 207 info->interfaces = | 208 info->interfaces = |
| 208 mojo::Array<device::usb::InterfaceInfoPtr>::From(config.interfaces); | 209 mojo::ConvertTo<std::vector<device::usb::InterfaceInfoPtr>>( |
| 210 config.interfaces); |
| 209 return info; | 211 return info; |
| 210 } | 212 } |
| 211 | 213 |
| 212 // static | 214 // static |
| 213 device::usb::DeviceInfoPtr | 215 device::usb::DeviceInfoPtr |
| 214 TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert( | 216 TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert( |
| 215 const device::UsbDevice& device) { | 217 const device::UsbDevice& device) { |
| 216 device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New(); | 218 device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New(); |
| 217 info->guid = device.guid(); | 219 info->guid = device.guid(); |
| 218 info->usb_version_major = device.usb_version() >> 8; | 220 info->usb_version_major = device.usb_version() >> 8; |
| 219 info->usb_version_minor = device.usb_version() >> 4 & 0xf; | 221 info->usb_version_minor = device.usb_version() >> 4 & 0xf; |
| 220 info->usb_version_subminor = device.usb_version() & 0xf; | 222 info->usb_version_subminor = device.usb_version() & 0xf; |
| 221 info->class_code = device.device_class(); | 223 info->class_code = device.device_class(); |
| 222 info->subclass_code = device.device_subclass(); | 224 info->subclass_code = device.device_subclass(); |
| 223 info->protocol_code = device.device_protocol(); | 225 info->protocol_code = device.device_protocol(); |
| 224 info->vendor_id = device.vendor_id(); | 226 info->vendor_id = device.vendor_id(); |
| 225 info->product_id = device.product_id(); | 227 info->product_id = device.product_id(); |
| 226 info->device_version_major = device.device_version() >> 8; | 228 info->device_version_major = device.device_version() >> 8; |
| 227 info->device_version_minor = device.device_version() >> 4 & 0xf; | 229 info->device_version_minor = device.device_version() >> 4 & 0xf; |
| 228 info->device_version_subminor = device.device_version() & 0xf; | 230 info->device_version_subminor = device.device_version() & 0xf; |
| 229 info->manufacturer_name = base::UTF16ToUTF8(device.manufacturer_string()); | 231 info->manufacturer_name = base::UTF16ToUTF8(device.manufacturer_string()); |
| 230 info->product_name = base::UTF16ToUTF8(device.product_string()); | 232 info->product_name = base::UTF16ToUTF8(device.product_string()); |
| 231 info->serial_number = base::UTF16ToUTF8(device.serial_number()); | 233 info->serial_number = base::UTF16ToUTF8(device.serial_number()); |
| 232 const device::UsbConfigDescriptor* config = device.active_configuration(); | 234 const device::UsbConfigDescriptor* config = device.active_configuration(); |
| 233 info->active_configuration = config ? config->configuration_value : 0; | 235 info->active_configuration = config ? config->configuration_value : 0; |
| 234 info->configurations = mojo::Array<device::usb::ConfigurationInfoPtr>::From( | 236 info->configurations = |
| 235 device.configurations()); | 237 mojo::ConvertTo<std::vector<device::usb::ConfigurationInfoPtr>>( |
| 238 device.configurations()); |
| 236 return info; | 239 return info; |
| 237 } | 240 } |
| 238 | 241 |
| 239 // static | 242 // static |
| 240 device::usb::IsochronousPacketPtr | 243 device::usb::IsochronousPacketPtr |
| 241 TypeConverter<device::usb::IsochronousPacketPtr, | 244 TypeConverter<device::usb::IsochronousPacketPtr, |
| 242 device::UsbDeviceHandle::IsochronousPacket>:: | 245 device::UsbDeviceHandle::IsochronousPacket>:: |
| 243 Convert(const device::UsbDeviceHandle::IsochronousPacket& packet) { | 246 Convert(const device::UsbDeviceHandle::IsochronousPacket& packet) { |
| 244 device::usb::IsochronousPacketPtr info = | 247 device::usb::IsochronousPacketPtr info = |
| 245 device::usb::IsochronousPacket::New(); | 248 device::usb::IsochronousPacket::New(); |
| 246 info->length = packet.length; | 249 info->length = packet.length; |
| 247 info->transferred_length = packet.transferred_length; | 250 info->transferred_length = packet.transferred_length; |
| 248 info->status = mojo::ConvertTo<device::usb::TransferStatus>(packet.status); | 251 info->status = mojo::ConvertTo<device::usb::TransferStatus>(packet.status); |
| 249 return info; | 252 return info; |
| 250 } | 253 } |
| 251 | 254 |
| 252 } // namespace mojo | 255 } // namespace mojo |
| OLD | NEW |