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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// Copyright (c) 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/hid/hid_connection.h"
#include <algorithm>
namespace device {
namespace {
// Functor used to filter collections by report ID.
struct CollectionHasReportId {
explicit CollectionHasReportId(const uint8_t report_id)
: report_id_(report_id) {}
bool operator()(const HidCollectionInfo& info) const {
if (info.report_ids.size() == 0 ||
report_id_ == HidConnection::kNullReportId)
return false;
if (report_id_ == HidConnection::kAnyReportId)
return true;
return std::find(info.report_ids.begin(),
info.report_ids.end(),
report_id_) != info.report_ids.end();
}
private:
const uint8_t report_id_;
};
// Functor returning true if collection has a protected usage.
struct CollectionIsProtected {
bool operator()(const HidCollectionInfo& info) const {
return info.usage.IsProtected();
}
};
bool FindCollectionByReportId(const HidDeviceInfo& device_info,
const uint8_t report_id,
HidCollectionInfo* collection_info) {
std::vector<HidCollectionInfo>::const_iterator collection_iter =
std::find_if(device_info.collections.begin(),
device_info.collections.end(),
CollectionHasReportId(report_id));
if (collection_iter != device_info.collections.end()) {
if (collection_info) {
*collection_info = *collection_iter;
}
return true;
}
return false;
}
bool HasProtectedCollection(const HidDeviceInfo& device_info) {
return std::find_if(device_info.collections.begin(),
device_info.collections.end(),
CollectionIsProtected()) != device_info.collections.end();
}
} // namespace
HidConnection::HidConnection(const HidDeviceInfo& device_info)
: device_info_(device_info) {
has_protected_collection_ = HasProtectedCollection(device_info);
}
HidConnection::~HidConnection() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void HidConnection::Read(scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (device_info_.max_input_report_size == 0) {
// The device does not support input reports.
callback.Run(false, 0);
return;
}
int expected_buffer_size = device_info_.max_input_report_size;
if (device_info().has_report_id) {
expected_buffer_size++;
}
if (buffer->size() < expected_buffer_size) {
// Receive buffer is too small.
callback.Run(false, 0);
return;
}
PlatformRead(buffer, callback);
}
void HidConnection::Write(uint8_t report_id,
scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (device_info_.max_output_report_size == 0) {
// The device does not support output reports.
callback.Run(false, 0);
return;
}
if (IsReportIdProtected(report_id)) {
callback.Run(false, 0);
return;
}
PlatformWrite(report_id, buffer, callback);
}
void HidConnection::GetFeatureReport(
uint8_t report_id,
scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (device_info_.max_feature_report_size == 0) {
// The device does not support feature reports.
callback.Run(false, 0);
return;
}
if (IsReportIdProtected(report_id)) {
callback.Run(false, 0);
return;
}
int expected_buffer_size = device_info_.max_feature_report_size;
if (device_info().has_report_id) {
expected_buffer_size++;
}
if (buffer->size() < expected_buffer_size) {
// Receive buffer is too small.
callback.Run(false, 0);
return;
}
PlatformGetFeatureReport(report_id, buffer, callback);
}
void HidConnection::SendFeatureReport(
uint8_t report_id,
scoped_refptr<net::IOBufferWithSize> buffer,
const IOCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (device_info_.max_feature_report_size == 0) {
// The device does not support feature reports.
callback.Run(false, 0);
return;
}
if (IsReportIdProtected(report_id)) {
callback.Run(false, 0);
return;
}
PlatformSendFeatureReport(report_id, buffer, callback);
}
bool HidConnection::CompleteRead(scoped_refptr<net::IOBufferWithSize> buffer,
int bytes_read,
const IOCallback& callback) {
DCHECK_LE(bytes_read, buffer->size());
if (bytes_read == 0 || IsReportIdProtected(buffer->data()[0])) {
return false;
}
callback.Run(true, bytes_read);
return true;
}
bool HidConnection::IsReportIdProtected(const uint8_t report_id) {
HidCollectionInfo collection_info;
if (FindCollectionByReportId(device_info_, report_id, &collection_info)) {
return collection_info.usage.IsProtected();
}
return has_protected_collection();
}
PendingHidReport::PendingHidReport() {}
PendingHidReport::~PendingHidReport() {}
PendingHidRead::PendingHidRead() {}
PendingHidRead::~PendingHidRead() {}
} // namespace device
|