Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10610)

Unified Diff: device/usb/type_converters.cc

Issue 1155163008: Build a basic Mojo service framework for device/usb (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/usb/type_converters.cc
diff --git a/device/usb/type_converters.cc b/device/usb/type_converters.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cc3017141cb806bccef70de90623a43d846ac2ea
--- /dev/null
+++ b/device/usb/type_converters.cc
@@ -0,0 +1,136 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "device/usb/type_converters.h"
+
+#include "base/logging.h"
+#include "base/strings/utf_string_conversions.h"
+#include "device/usb/usb_device.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
+
+namespace mojo {
+
+// static
+device::UsbDeviceFilter
+TypeConverter<device::UsbDeviceFilter, device::usb::DeviceFilterPtr>::Convert(
+ const device::usb::DeviceFilterPtr& mojo_filter) {
+ device::UsbDeviceFilter filter;
+ if (mojo_filter->has_vendor_id)
+ filter.SetVendorId(mojo_filter->vendor_id);
+ if (mojo_filter->has_product_id)
+ filter.SetProductId(mojo_filter->product_id);
+ if (mojo_filter->has_class_code)
+ filter.SetInterfaceClass(mojo_filter->class_code);
+ if (mojo_filter->has_subclass_code)
+ filter.SetInterfaceSubclass(mojo_filter->subclass_code);
+ if (mojo_filter->has_protocol_code)
+ filter.SetInterfaceProtocol(mojo_filter->protocol_code);
+ return filter;
+}
+
+// static
+device::usb::TransferDirection
+TypeConverter<device::usb::TransferDirection, device::UsbEndpointDirection>::
+ Convert(const device::UsbEndpointDirection& direction) {
+ if (direction == device::USB_DIRECTION_INBOUND)
+ return device::usb::TRANSFER_DIRECTION_IN;
+ DCHECK(direction == device::USB_DIRECTION_OUTBOUND);
+ return device::usb::TRANSFER_DIRECTION_OUT;
+}
+
+// static
+device::usb::EndpointType
+TypeConverter<device::usb::EndpointType, device::UsbTransferType>::Convert(
+ const device::UsbTransferType& type) {
+ switch (type) {
+ case device::USB_TRANSFER_ISOCHRONOUS:
+ return device::usb::ENDPOINT_TYPE_ISOCHRONOUS;
+ case device::USB_TRANSFER_BULK:
+ return device::usb::ENDPOINT_TYPE_BULK;
+ case device::USB_TRANSFER_INTERRUPT:
+ return device::usb::ENDPOINT_TYPE_INTERRUPT;
+ // Note that we do not expose control transfer in the public interface
+ // because control endpoints are implied rather than explicitly enumerated
+ // there.
+ default:
+ NOTREACHED();
+ return device::usb::ENDPOINT_TYPE_BULK;
+ }
+}
+
+// static
+device::usb::EndpointInfoPtr
+TypeConverter<device::usb::EndpointInfoPtr, device::UsbEndpointDescriptor>::
+ Convert(const device::UsbEndpointDescriptor& endpoint) {
+ device::usb::EndpointInfoPtr info = device::usb::EndpointInfo::New();
+ info->endpoint_number = endpoint.address;
+ info->direction =
+ ConvertTo<device::usb::TransferDirection>(endpoint.direction);
+ info->type = ConvertTo<device::usb::EndpointType>(endpoint.transfer_type);
+ info->packet_size = static_cast<uint32_t>(endpoint.maximum_packet_size);
+ return info.Pass();
+}
+
+// static
+device::usb::AlternateInterfaceInfoPtr
+TypeConverter<device::usb::AlternateInterfaceInfoPtr,
+ device::UsbInterfaceDescriptor>::
+ Convert(const device::UsbInterfaceDescriptor& interface) {
+ device::usb::AlternateInterfaceInfoPtr info =
+ device::usb::AlternateInterfaceInfo::New();
+ info->alternate_setting = interface.alternate_setting;
+ info->class_code = interface.interface_class;
+ info->subclass_code = interface.interface_subclass;
+ info->protocol_code = interface.interface_protocol;
+
+ // Filter out control endpoints for the public interface.
+ info->endpoints = mojo::Array<device::usb::EndpointInfoPtr>::New(0);
+ for (const auto& endpoint : interface.endpoints) {
+ if (endpoint.transfer_type != device::USB_TRANSFER_CONTROL)
+ info->endpoints.push_back(device::usb::EndpointInfo::From(endpoint));
+ }
+
+ return info.Pass();
+}
+
+// static
+device::usb::InterfaceInfoPtr
+TypeConverter<device::usb::InterfaceInfoPtr, device::UsbInterfaceDescriptor>::
+ Convert(const device::UsbInterfaceDescriptor& interface) {
+ device::usb::InterfaceInfoPtr info = device::usb::InterfaceInfo::New();
+ info->interface_number = interface.interface_number;
+ // 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.
+ // currently selected.
+ info->alternates =
+ mojo::Array<device::usb::AlternateInterfaceInfoPtr>::New(1);
+ info->alternates[0] = device::usb::AlternateInterfaceInfo::From(interface);
+ return info.Pass();
+}
+
+// static
+device::usb::ConfigurationInfoPtr
+TypeConverter<device::usb::ConfigurationInfoPtr, device::UsbConfigDescriptor>::
+ Convert(const device::UsbConfigDescriptor& config) {
+ device::usb::ConfigurationInfoPtr info =
+ device::usb::ConfigurationInfo::New();
+ info->configuration_value = config.configuration_value;
+ info->interfaces =
+ mojo::Array<device::usb::InterfaceInfoPtr>::From(config.interfaces);
+ return info.Pass();
+}
+
+// static
+device::usb::DeviceInfoPtr
+TypeConverter<device::usb::DeviceInfoPtr, device::UsbDevice>::Convert(
+ const device::UsbDevice& device) {
+ device::usb::DeviceInfoPtr info = device::usb::DeviceInfo::New();
+ info->vendor_id = device.vendor_id();
+ info->product_id = device.product_id();
+ info->manufacturer = base::UTF16ToUTF8(device.manufacturer_string());
+ info->product = base::UTF16ToUTF8(device.product_string());
+ info->serial_number = base::UTF16ToUTF8(device.serial_number());
+ return info.Pass();
+}
+
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698