OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/hid/hid_connection.h" | 5 #include "device/hid/hid_connection.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
7 namespace device { | 9 namespace device { |
8 | 10 |
11 namespace { | |
12 | |
13 const uint8_t kNullReportId = 0x00; | |
14 const uint8_t kAnyReportId = 0xFF; | |
Ken Rockot(use gerrit already)
2014/06/27 14:50:22
How about moving these constants to hid_connection
jracle (use Gerrit)
2014/06/27 20:07:06
Makes sense.
On 2014/06/27 14:50:22, Ken Rockot w
| |
15 | |
16 struct CollectionHasReportId { | |
Ken Rockot(use gerrit already)
2014/06/27 14:50:22
Maybe a brief comment that this is a functor used
jracle (use Gerrit)
2014/06/27 20:07:06
Sure.
On 2014/06/27 14:50:22, Ken Rockot wrote:
| |
17 explicit CollectionHasReportId(const uint8_t report_id) | |
18 : report_id_(report_id) {} | |
19 | |
20 bool operator()(const HidCollectionInfo& info) const { | |
21 if (info.report_ids.size() == 0 || report_id_ == kNullReportId) | |
22 return false; | |
23 | |
24 if (report_id_ == kAnyReportId) | |
25 return true; | |
26 | |
27 return std::find(info.report_ids.begin(), | |
28 info.report_ids.end(), | |
29 report_id_) != info.report_ids.end(); | |
30 } | |
31 | |
32 private: | |
33 const uint8_t report_id_; | |
34 }; | |
35 | |
36 struct CollectionIsProtected { | |
Ken Rockot(use gerrit already)
2014/06/27 14:50:22
Brief comment here as well.
jracle (use Gerrit)
2014/06/27 20:07:06
OK.
On 2014/06/27 14:50:22, Ken Rockot wrote:
| |
37 bool operator()(const HidCollectionInfo& info) const { | |
38 return info.usage.IsProtected(); | |
39 } | |
40 }; | |
41 | |
42 bool FindCollectionByReportId(const HidDeviceInfo& device_info, | |
43 const uint8_t report_id, | |
44 HidCollectionInfo* collection_info) { | |
45 std::vector<HidCollectionInfo>::const_iterator collection_iter = | |
46 std::find_if(device_info.collections.begin(), | |
47 device_info.collections.end(), | |
48 CollectionHasReportId(report_id)); | |
49 if (collection_iter != device_info.collections.end()) { | |
50 if (collection_info) { | |
51 *collection_info = *collection_iter; | |
52 } | |
53 return true; | |
54 } | |
55 | |
56 return false; | |
57 } | |
58 | |
59 bool HasReportId(const HidDeviceInfo& device_info) { | |
60 return FindCollectionByReportId(device_info, kAnyReportId, NULL); | |
61 } | |
62 | |
63 bool HasProtectedCollection(const HidDeviceInfo& device_info) { | |
64 return std::find_if(device_info.collections.begin(), | |
65 device_info.collections.end(), | |
66 CollectionIsProtected()) != device_info.collections.end(); | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 HidConnection::HidConnection(const HidDeviceInfo& device_info) | |
72 : device_info_(device_info) { | |
73 DCHECK(thread_checker_.CalledOnValidThread()); | |
Ken Rockot(use gerrit already)
2014/06/27 14:50:22
Actually I realize this was in the other implement
jracle (use Gerrit)
2014/06/27 20:07:06
Sorry, got it now..! Useless indeed
On 2014/06/27
| |
74 has_protected_collection_ = HasProtectedCollection(device_info); | |
75 has_report_id_ = HasReportId(device_info); | |
76 } | |
77 | |
78 HidConnection::~HidConnection() { | |
79 DCHECK(thread_checker_.CalledOnValidThread()); | |
80 } | |
81 | |
82 void HidConnection::Read(scoped_refptr<net::IOBufferWithSize> buffer, | |
83 const IOCallback& callback) { | |
84 DCHECK(thread_checker_.CalledOnValidThread()); | |
85 if (device_info_.max_input_report_size == 0) { | |
86 // The device does not support input reports. | |
87 callback.Run(false, 0); | |
88 return; | |
89 } | |
90 int expected_buffer_size = device_info_.max_input_report_size; | |
91 if (!has_report_id()) { | |
92 expected_buffer_size--; | |
93 } | |
94 if (buffer->size() < expected_buffer_size) { | |
95 // Receive buffer is too small. | |
96 callback.Run(false, 0); | |
97 return; | |
98 } | |
99 | |
100 PlatformRead(buffer, callback); | |
101 } | |
102 | |
103 void HidConnection::Write(uint8_t report_id, | |
104 scoped_refptr<net::IOBufferWithSize> buffer, | |
105 const IOCallback& callback) { | |
106 DCHECK(thread_checker_.CalledOnValidThread()); | |
107 if (device_info_.max_output_report_size == 0) { | |
108 // The device does not support output reports. | |
109 callback.Run(false, 0); | |
110 return; | |
111 } | |
112 if (IsReportIdProtected(report_id)) { | |
113 callback.Run(false, 0); | |
114 return; | |
115 } | |
116 | |
117 PlatformWrite(report_id, buffer, callback); | |
118 } | |
119 | |
120 void HidConnection::GetFeatureReport( | |
121 uint8_t report_id, | |
122 scoped_refptr<net::IOBufferWithSize> buffer, | |
123 const IOCallback& callback) { | |
124 DCHECK(thread_checker_.CalledOnValidThread()); | |
125 if (device_info_.max_feature_report_size == 0) { | |
126 // The device does not support feature reports. | |
127 callback.Run(false, 0); | |
128 return; | |
129 } | |
130 if (IsReportIdProtected(report_id)) { | |
131 callback.Run(false, 0); | |
132 return; | |
133 } | |
134 int expected_buffer_size = device_info_.max_feature_report_size; | |
135 if (!has_report_id()) { | |
136 expected_buffer_size--; | |
137 } | |
138 if (buffer->size() < expected_buffer_size) { | |
139 // Receive buffer is too small. | |
140 callback.Run(false, 0); | |
141 return; | |
142 } | |
143 | |
144 PlatformGetFeatureReport(report_id, buffer, callback); | |
145 } | |
146 | |
147 void HidConnection::SendFeatureReport( | |
148 uint8_t report_id, | |
149 scoped_refptr<net::IOBufferWithSize> buffer, | |
150 const IOCallback& callback) { | |
151 DCHECK(thread_checker_.CalledOnValidThread()); | |
152 if (device_info_.max_feature_report_size == 0) { | |
153 // The device does not support feature reports. | |
154 callback.Run(false, 0); | |
155 return; | |
156 } | |
157 if (IsReportIdProtected(report_id)) { | |
158 callback.Run(false, 0); | |
159 return; | |
160 } | |
161 | |
162 PlatformSendFeatureReport(report_id, buffer, callback); | |
163 } | |
164 | |
165 void HidConnection::CompleteRead(scoped_refptr<net::IOBufferWithSize> buffer, | |
166 const IOCallback& callback) { | |
167 if (FilterInputReport(buffer, callback)) { | |
168 return; | |
169 } | |
170 | |
171 callback.Run(true, buffer->size()); | |
172 } | |
173 | |
174 bool HidConnection::FilterInputReport( | |
175 scoped_refptr<net::IOBufferWithSize> buffer, | |
176 const IOCallback& callback) { | |
177 if (buffer->size() == 0) { | |
178 return false; | |
179 } | |
180 | |
181 if (IsReportIdProtected(buffer->data()[0])) { | |
182 callback.Run(true, 0); | |
183 return true; | |
184 } | |
185 | |
186 return false; | |
187 } | |
188 | |
189 bool HidConnection::IsReportIdProtected(const uint8_t report_id) { | |
190 HidCollectionInfo collection_info; | |
191 if (FindCollectionByReportId(device_info_, report_id, &collection_info)) { | |
192 return collection_info.usage.IsProtected(); | |
193 } | |
194 | |
195 return has_protected_collection(); | |
196 } | |
197 | |
9 PendingHidReport::PendingHidReport() {} | 198 PendingHidReport::PendingHidReport() {} |
10 | 199 |
11 PendingHidReport::~PendingHidReport() {} | 200 PendingHidReport::~PendingHidReport() {} |
12 | 201 |
13 PendingHidRead::PendingHidRead() {} | 202 PendingHidRead::PendingHidRead() {} |
14 | 203 |
15 PendingHidRead::~PendingHidRead() {} | 204 PendingHidRead::~PendingHidRead() {} |
16 | 205 |
17 HidConnection::HidConnection(const HidDeviceInfo& device_info) | |
18 : device_info_(device_info) {} | |
19 | |
20 HidConnection::~HidConnection() {} | |
21 | |
22 } // namespace device | 206 } // namespace device |
OLD | NEW |