blob: 52d1356e7ab74cfc7afba178a0dcd09e00d68a89 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// Copyright 2014 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/usb_device_filter.h"
#include <utility>
#include "base/values.h"
#include "device/usb/usb_descriptors.h"
#include "device/usb/usb_device.h"
namespace device {
namespace {
const char kProductIdKey[] = "productId";
const char kVendorIdKey[] = "vendorId";
const char kInterfaceClassKey[] = "interfaceClass";
const char kInterfaceSubclassKey[] = "interfaceSubclass";
const char kInterfaceProtocolKey[] = "interfaceProtocol";
} // namespace
UsbDeviceFilter::UsbDeviceFilter()
: vendor_id_set_(false),
product_id_set_(false),
interface_class_set_(false),
interface_subclass_set_(false),
interface_protocol_set_(false) {
}
UsbDeviceFilter::UsbDeviceFilter(const UsbDeviceFilter& other) = default;
UsbDeviceFilter::~UsbDeviceFilter() {
}
void UsbDeviceFilter::SetVendorId(uint16_t vendor_id) {
vendor_id_set_ = true;
vendor_id_ = vendor_id;
}
void UsbDeviceFilter::SetProductId(uint16_t product_id) {
product_id_set_ = true;
product_id_ = product_id;
}
void UsbDeviceFilter::SetInterfaceClass(uint8_t interface_class) {
interface_class_set_ = true;
interface_class_ = interface_class;
}
void UsbDeviceFilter::SetInterfaceSubclass(uint8_t interface_subclass) {
interface_subclass_set_ = true;
interface_subclass_ = interface_subclass;
}
void UsbDeviceFilter::SetInterfaceProtocol(uint8_t interface_protocol) {
interface_protocol_set_ = true;
interface_protocol_ = interface_protocol;
}
bool UsbDeviceFilter::Matches(scoped_refptr<UsbDevice> device) const {
if (vendor_id_set_) {
if (device->vendor_id() != vendor_id_) {
return false;
}
if (product_id_set_ && device->product_id() != product_id_) {
return false;
}
}
if (interface_class_set_) {
for (const UsbConfigDescriptor& config : device->configurations()) {
for (const UsbInterfaceDescriptor& iface : config.interfaces) {
if (iface.interface_class == interface_class_ &&
(!interface_subclass_set_ ||
(iface.interface_subclass == interface_subclass_ &&
(!interface_protocol_set_ ||
iface.interface_protocol == interface_protocol_)))) {
return true;
}
}
}
return false;
}
return true;
}
scoped_ptr<base::Value> UsbDeviceFilter::ToValue() const {
scoped_ptr<base::DictionaryValue> obj(new base::DictionaryValue());
if (vendor_id_set_) {
obj->SetInteger(kVendorIdKey, vendor_id_);
if (product_id_set_) {
obj->SetInteger(kProductIdKey, product_id_);
}
}
if (interface_class_set_) {
obj->SetInteger(kInterfaceClassKey, interface_class_);
if (interface_subclass_set_) {
obj->SetInteger(kInterfaceSubclassKey, interface_subclass_);
if (interface_protocol_set_) {
obj->SetInteger(kInterfaceProtocolKey, interface_protocol_);
}
}
}
return std::move(obj);
}
// static
bool UsbDeviceFilter::MatchesAny(scoped_refptr<UsbDevice> device,
const std::vector<UsbDeviceFilter>& filters) {
for (std::vector<UsbDeviceFilter>::const_iterator i = filters.begin();
i != filters.end();
++i) {
if (i->Matches(device)) {
return true;
}
}
return false;
}
} // namespace device
|