1 // Copyright 2014 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.
5 #include "components/usb_service/usb_device_filter.h"
7 #include "base/values.h"
8 #include "components/usb_service/usb_device.h"
9 #include "components/usb_service/usb_device_handle.h"
10 #include "components/usb_service/usb_interface.h"
12 namespace usb_service {
16 const char kProductIdKey[] = "productId";
17 const char kVendorIdKey[] = "vendorId";
18 const char kInterfaceClassKey[] = "interfaceClass";
19 const char kInterfaceSubclassKey[] = "interfaceSubclass";
20 const char kInterfaceProtocolKey[] = "interfaceProtocol";
24 UsbDeviceFilter::UsbDeviceFilter()
25 : vendor_id_set_(false),
26 product_id_set_(false),
27 interface_class_set_(false),
28 interface_subclass_set_(false),
29 interface_protocol_set_(false) {
32 UsbDeviceFilter::~UsbDeviceFilter() {
35 void UsbDeviceFilter::SetVendorId(uint16 vendor_id) {
36 vendor_id_set_ = true;
37 vendor_id_ = vendor_id;
40 void UsbDeviceFilter::SetProductId(uint16 product_id) {
41 product_id_set_ = true;
42 product_id_ = product_id;
45 void UsbDeviceFilter::SetInterfaceClass(uint8 interface_class) {
46 interface_class_set_ = true;
47 interface_class_ = interface_class;
50 void UsbDeviceFilter::SetInterfaceSubclass(uint8 interface_subclass) {
51 interface_subclass_set_ = true;
52 interface_subclass_ = interface_subclass;
55 void UsbDeviceFilter::SetInterfaceProtocol(uint8 interface_protocol) {
56 interface_protocol_set_ = true;
57 interface_protocol_ = interface_protocol;
60 bool UsbDeviceFilter::Matches(scoped_refptr<UsbDevice> device) {
62 if (device->vendor_id() != vendor_id_) {
66 if (product_id_set_ && device->product_id() != product_id_) {
71 if (interface_class_set_) {
72 bool foundMatch = false;
73 scoped_refptr<const UsbConfigDescriptor> config = device->ListInterfaces();
75 // TODO(reillyg): Check device configuration if the class is not defined at
76 // a per-interface level. This is not really important because most devices
77 // have per-interface classes. The only counter-examples I know of are hubs.
79 for (size_t i = 0; i < config->GetNumInterfaces() && !foundMatch; ++i) {
80 scoped_refptr<const UsbInterfaceDescriptor> iface =
81 config->GetInterface(i);
83 for (size_t j = 0; j < iface->GetNumAltSettings() && !foundMatch; ++j) {
84 scoped_refptr<const UsbInterfaceAltSettingDescriptor> altSetting =
85 iface->GetAltSetting(j);
87 if (altSetting->GetInterfaceClass() == interface_class_ &&
88 (!interface_subclass_set_ ||
89 (altSetting->GetInterfaceSubclass() == interface_subclass_ &&
90 (!interface_protocol_set_ ||
91 altSetting->GetInterfaceProtocol() == interface_protocol_)))) {
105 base::Value* UsbDeviceFilter::ToValue() const {
106 scoped_ptr<base::DictionaryValue> obj(new base::DictionaryValue());
108 if (vendor_id_set_) {
109 obj->SetInteger(kVendorIdKey, vendor_id_);
110 if (product_id_set_) {
111 obj->SetInteger(kProductIdKey, product_id_);
115 if (interface_class_set_) {
116 obj->SetInteger(kInterfaceClassKey, interface_class_);
117 if (interface_subclass_set_) {
118 obj->SetInteger(kInterfaceSubclassKey, interface_subclass_);
119 if (interface_protocol_set_) {
120 obj->SetInteger(kInterfaceProtocolKey, interface_protocol_);
125 return obj.release();
128 } // namespace usb_service