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_win.h" | 5 #include "device/hid/hid_connection_win.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/stl_util.h" | |
12 #include "base/threading/thread_restrictions.h" | |
13 #include "base/win/object_watcher.h" | 11 #include "base/win/object_watcher.h" |
14 #include "base/win/scoped_handle.h" | |
15 #include "device/hid/hid_service.h" | |
16 #include "device/hid/hid_service_win.h" | |
17 | 12 |
18 #define INITGUID | 13 #define INITGUID |
19 | 14 |
20 #include <windows.h> | 15 #include <windows.h> |
21 #include <hidclass.h> | 16 #include <hidclass.h> |
22 | 17 |
23 extern "C" { | 18 extern "C" { |
24 #include <hidsdi.h> | 19 #include <hidsdi.h> |
25 } | 20 } |
26 | 21 |
27 #include <setupapi.h> | 22 #include <setupapi.h> |
28 #include <winioctl.h> | 23 #include <winioctl.h> |
29 | 24 |
30 namespace device { | 25 namespace device { |
31 | 26 |
27 namespace { | |
28 | |
29 const uint8_t kNullReportId = 0x00; | |
30 | |
31 } // namespace | |
32 | |
32 struct PendingHidTransfer : public base::RefCounted<PendingHidTransfer>, | 33 struct PendingHidTransfer : public base::RefCounted<PendingHidTransfer>, |
33 public base::win::ObjectWatcher::Delegate, | 34 public base::win::ObjectWatcher::Delegate, |
34 public base::MessageLoop::DestructionObserver { | 35 public base::MessageLoop::DestructionObserver { |
35 PendingHidTransfer(scoped_refptr<HidConnectionWin> connection, | 36 PendingHidTransfer(scoped_refptr<HidConnectionWin> connection, |
36 scoped_refptr<net::IOBufferWithSize> target_buffer, | 37 scoped_refptr<net::IOBufferWithSize> target_buffer, |
37 scoped_refptr<net::IOBufferWithSize> receive_buffer, | 38 scoped_refptr<net::IOBufferWithSize> receive_buffer, |
38 HidConnection::IOCallback callback); | 39 HidConnection::IOCallback callback); |
39 | 40 |
40 void TakeResultFromWindowsAPI(BOOL result); | 41 void TakeResultFromWindowsAPI(BOOL result); |
41 | 42 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 Release(); | 97 Release(); |
97 } | 98 } |
98 | 99 |
99 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { | 100 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { |
100 watcher_.StopWatching(); | 101 watcher_.StopWatching(); |
101 connection_->OnTransferCanceled(this); | 102 connection_->OnTransferCanceled(this); |
102 } | 103 } |
103 | 104 |
104 HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info) | 105 HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info) |
105 : HidConnection(device_info) { | 106 : HidConnection(device_info) { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | |
107 file_.Set(CreateFileA(device_info.device_id.c_str(), | 107 file_.Set(CreateFileA(device_info.device_id.c_str(), |
108 GENERIC_WRITE | GENERIC_READ, | 108 GENERIC_WRITE | GENERIC_READ, |
109 FILE_SHARE_READ | FILE_SHARE_WRITE, | 109 FILE_SHARE_READ | FILE_SHARE_WRITE, |
110 NULL, | 110 NULL, |
111 OPEN_EXISTING, | 111 OPEN_EXISTING, |
112 FILE_FLAG_OVERLAPPED, | 112 FILE_FLAG_OVERLAPPED, |
113 NULL)); | 113 NULL)); |
114 | 114 |
115 if (!file_.IsValid() && | 115 if (!file_.IsValid() && |
116 GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) { | 116 GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) { |
117 file_.Set(CreateFileA(device_info.device_id.c_str(), | 117 file_.Set(CreateFileA(device_info.device_id.c_str(), |
118 GENERIC_READ, | 118 GENERIC_READ, |
119 FILE_SHARE_READ, | 119 FILE_SHARE_READ, |
120 NULL, | 120 NULL, |
121 OPEN_EXISTING, | 121 OPEN_EXISTING, |
122 FILE_FLAG_OVERLAPPED, | 122 FILE_FLAG_OVERLAPPED, |
123 NULL)); | 123 NULL)); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 bool HidConnectionWin::available() const { | |
128 return file_.IsValid(); | |
129 } | |
130 | |
131 HidConnectionWin::~HidConnectionWin() { | 127 HidConnectionWin::~HidConnectionWin() { |
132 DCHECK(thread_checker_.CalledOnValidThread()); | |
133 CancelIo(file_.Get()); | 128 CancelIo(file_.Get()); |
134 } | 129 } |
135 | 130 |
136 void HidConnectionWin::Read(scoped_refptr<net::IOBufferWithSize> buffer, | 131 void HidConnectionWin::PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer, |
137 const HidConnection::IOCallback& callback) { | 132 const HidConnection::IOCallback& callback) { |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 133 scoped_refptr<net::IOBufferWithSize> receive_buffer = |
139 if (device_info().input_report_size == 0) { | 134 new net::IOBufferWithSize(device_info().max_input_report_size); |
140 // The device does not support input reports. | |
141 callback.Run(false, 0); | |
142 return; | |
143 } | |
144 | 135 |
145 // This fairly awkward logic is correct: If Windows does not expect a device | |
146 // to supply a report ID in its input reports, it requires the buffer to be | |
147 // 1 byte larger than what the device actually sends. | |
148 int receive_buffer_size = device_info().input_report_size; | |
149 int expected_buffer_size = receive_buffer_size; | |
150 if (!device_info().has_report_id) | |
151 expected_buffer_size -= 1; | |
152 | |
153 if (buffer->size() < expected_buffer_size) { | |
154 callback.Run(false, 0); | |
155 return; | |
156 } | |
157 | |
158 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); | |
159 if (receive_buffer_size != expected_buffer_size) | |
160 receive_buffer = new net::IOBufferWithSize(receive_buffer_size); | |
161 scoped_refptr<PendingHidTransfer> transfer( | 136 scoped_refptr<PendingHidTransfer> transfer( |
162 new PendingHidTransfer(this, buffer, receive_buffer, callback)); | 137 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
163 transfers_.insert(transfer); | 138 transfers_.insert(transfer); |
164 transfer->TakeResultFromWindowsAPI( | 139 transfer->TakeResultFromWindowsAPI( |
165 ReadFile(file_.Get(), | 140 ReadFile(file_.Get(), |
166 receive_buffer->data(), | 141 receive_buffer->data(), |
167 static_cast<DWORD>(receive_buffer->size()), | 142 static_cast<DWORD>(receive_buffer->size()), |
168 NULL, | 143 NULL, |
169 transfer->GetOverlapped())); | 144 transfer->GetOverlapped())); |
170 } | 145 } |
171 | 146 |
172 void HidConnectionWin::Write(uint8_t report_id, | 147 void HidConnectionWin::PlatformWrite( |
173 scoped_refptr<net::IOBufferWithSize> buffer, | 148 uint8_t report_id, |
174 const HidConnection::IOCallback& callback) { | 149 scoped_refptr<net::IOBufferWithSize> buffer, |
175 DCHECK(thread_checker_.CalledOnValidThread()); | 150 const HidConnection::IOCallback& callback) { |
176 if (device_info().output_report_size == 0) { | |
177 // The device does not support output reports. | |
178 callback.Run(false, 0); | |
179 return; | |
180 } | |
181 | |
182 // The Windows API always wants either a report ID (if supported) or | 151 // The Windows API always wants either a report ID (if supported) or |
183 // zero at the front of every output report. | 152 // zero at the front of every output report. |
184 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); | 153 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
185 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); | 154 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
186 output_buffer->data()[0] = report_id; | 155 output_buffer->data()[0] = report_id; |
187 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); | 156 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
188 | 157 |
189 scoped_refptr<PendingHidTransfer> transfer( | 158 scoped_refptr<PendingHidTransfer> transfer( |
190 new PendingHidTransfer(this, output_buffer, NULL, callback)); | 159 new PendingHidTransfer(this, output_buffer, NULL, callback)); |
191 transfers_.insert(transfer); | 160 transfers_.insert(transfer); |
192 transfer->TakeResultFromWindowsAPI( | 161 transfer->TakeResultFromWindowsAPI( |
193 WriteFile(file_.Get(), | 162 WriteFile(file_.Get(), |
194 output_buffer->data(), | 163 output_buffer->data(), |
195 static_cast<DWORD>(output_buffer->size()), | 164 static_cast<DWORD>(output_buffer->size()), |
196 NULL, | 165 NULL, |
197 transfer->GetOverlapped())); | 166 transfer->GetOverlapped())); |
198 } | 167 } |
199 | 168 |
200 void HidConnectionWin::GetFeatureReport( | 169 void HidConnectionWin::PlatformGetFeatureReport( |
201 uint8_t report_id, | 170 uint8_t report_id, |
202 scoped_refptr<net::IOBufferWithSize> buffer, | 171 scoped_refptr<net::IOBufferWithSize> buffer, |
203 const IOCallback& callback) { | 172 const IOCallback& callback) { |
204 DCHECK(thread_checker_.CalledOnValidThread()); | 173 scoped_refptr<net::IOBufferWithSize> receive_buffer = |
205 if (device_info().feature_report_size == 0) { | 174 new net::IOBufferWithSize(device_info().max_feature_report_size); |
206 // The device does not support feature reports. | |
207 callback.Run(false, 0); | |
208 return; | |
209 } | |
210 | |
211 int receive_buffer_size = device_info().feature_report_size; | |
212 int expected_buffer_size = receive_buffer_size; | |
213 if (!device_info().has_report_id) | |
214 expected_buffer_size -= 1; | |
215 if (buffer->size() < expected_buffer_size) { | |
216 callback.Run(false, 0); | |
217 return; | |
218 } | |
219 | |
220 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); | |
221 if (receive_buffer_size != expected_buffer_size) | |
222 receive_buffer = new net::IOBufferWithSize(receive_buffer_size); | |
223 | |
224 // The first byte of the destination buffer is the report ID being requested. | 175 // The first byte of the destination buffer is the report ID being requested. |
225 receive_buffer->data()[0] = report_id; | 176 receive_buffer->data()[0] = report_id; |
177 | |
226 scoped_refptr<PendingHidTransfer> transfer( | 178 scoped_refptr<PendingHidTransfer> transfer( |
227 new PendingHidTransfer(this, buffer, receive_buffer, callback)); | 179 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
228 transfers_.insert(transfer); | 180 transfers_.insert(transfer); |
229 transfer->TakeResultFromWindowsAPI( | 181 transfer->TakeResultFromWindowsAPI( |
230 DeviceIoControl(file_.Get(), | 182 DeviceIoControl(file_.Get(), |
231 IOCTL_HID_GET_FEATURE, | 183 IOCTL_HID_GET_FEATURE, |
232 NULL, | 184 NULL, |
233 0, | 185 0, |
234 receive_buffer->data(), | 186 receive_buffer->data(), |
235 static_cast<DWORD>(receive_buffer->size()), | 187 static_cast<DWORD>(receive_buffer->size()), |
236 NULL, | 188 NULL, |
237 transfer->GetOverlapped())); | 189 transfer->GetOverlapped())); |
238 } | 190 } |
239 | 191 |
240 void HidConnectionWin::SendFeatureReport( | 192 void HidConnectionWin::PlatformSendFeatureReport( |
241 uint8_t report_id, | 193 uint8_t report_id, |
242 scoped_refptr<net::IOBufferWithSize> buffer, | 194 scoped_refptr<net::IOBufferWithSize> buffer, |
243 const IOCallback& callback) { | 195 const IOCallback& callback) { |
244 DCHECK(thread_checker_.CalledOnValidThread()); | |
245 if (device_info().feature_report_size == 0) { | |
246 // The device does not support feature reports. | |
247 callback.Run(false, 0); | |
248 return; | |
249 } | |
250 | |
251 // The Windows API always wants either a report ID (if supported) or | 196 // The Windows API always wants either a report ID (if supported) or |
252 // zero at the front of every output report. | 197 // zero at the front of every output report. |
253 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); | 198 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
254 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); | 199 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
255 output_buffer->data()[0] = report_id; | 200 output_buffer->data()[0] = report_id; |
256 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); | 201 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
257 | 202 |
258 scoped_refptr<PendingHidTransfer> transfer( | 203 scoped_refptr<PendingHidTransfer> transfer( |
259 new PendingHidTransfer(this, output_buffer, NULL, callback)); | 204 new PendingHidTransfer(this, output_buffer, NULL, callback)); |
260 transfer->TakeResultFromWindowsAPI( | 205 transfer->TakeResultFromWindowsAPI( |
261 DeviceIoControl(file_.Get(), | 206 DeviceIoControl(file_.Get(), |
262 IOCTL_HID_SET_FEATURE, | 207 IOCTL_HID_SET_FEATURE, |
263 output_buffer->data(), | 208 output_buffer->data(), |
264 static_cast<DWORD>(output_buffer->size()), | 209 static_cast<DWORD>(output_buffer->size()), |
265 NULL, | 210 NULL, |
266 0, | 211 0, |
267 NULL, | 212 NULL, |
268 transfer->GetOverlapped())); | 213 transfer->GetOverlapped())); |
269 } | 214 } |
270 | 215 |
271 void HidConnectionWin::OnTransferFinished( | 216 void HidConnectionWin::OnTransferFinished( |
272 scoped_refptr<PendingHidTransfer> transfer) { | 217 scoped_refptr<PendingHidTransfer> transfer) { |
218 transfers_.erase(transfer); | |
219 | |
273 DWORD bytes_transferred; | 220 DWORD bytes_transferred; |
274 transfers_.erase(transfer); | |
275 if (GetOverlappedResult( | 221 if (GetOverlappedResult( |
276 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { | 222 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { |
277 if (bytes_transferred == 0) | 223 if (bytes_transferred == 0) { |
278 transfer->callback_.Run(true, 0); | 224 transfer->callback_.Run(true, 0); |
279 // If this is an input transfer and the receive buffer is not the same as | 225 return; |
280 // the target buffer, we need to copy the receive buffer into the target | |
281 // buffer, discarding the first byte. This is because the target buffer's | |
282 // owner is not expecting a report ID but Windows will always provide one. | |
283 if (transfer->receive_buffer_ && | |
284 transfer->receive_buffer_ != transfer->target_buffer_) { | |
285 // Move one byte forward. | |
286 --bytes_transferred; | |
287 memcpy(transfer->target_buffer_->data(), | |
288 transfer->receive_buffer_->data() + 1, | |
289 bytes_transferred); | |
290 } | 226 } |
291 transfer->callback_.Run(true, bytes_transferred); | 227 |
228 if (transfer->receive_buffer_) { | |
Ken Rockot(use gerrit already)
2014/06/27 14:50:22
Could you please preserve the documentation that w
jracle (use Gerrit)
2014/06/27 20:07:06
Good catch. That's unfortunate I didn't keep it!
| |
229 if (!has_report_id()) { | |
230 // Assert first byte is 0x00 | |
231 DCHECK(transfer->receive_buffer_->data()[0] == kNullReportId); | |
232 | |
233 // Move one byte forward. | |
234 --bytes_transferred; | |
235 memcpy(transfer->target_buffer_->data(), | |
236 transfer->receive_buffer_->data() + 1, | |
237 bytes_transferred); | |
238 } else { | |
239 memcpy(transfer->target_buffer_->data(), | |
240 transfer->receive_buffer_->data(), | |
241 bytes_transferred); | |
242 } | |
243 } | |
244 | |
245 CompleteRead(transfer->target_buffer_, transfer->callback_); | |
292 } else { | 246 } else { |
293 transfer->callback_.Run(false, 0); | 247 transfer->callback_.Run(false, 0); |
294 } | 248 } |
295 } | 249 } |
296 | 250 |
297 void HidConnectionWin::OnTransferCanceled( | 251 void HidConnectionWin::OnTransferCanceled( |
298 scoped_refptr<PendingHidTransfer> transfer) { | 252 scoped_refptr<PendingHidTransfer> transfer) { |
299 transfers_.erase(transfer); | 253 transfers_.erase(transfer); |
300 transfer->callback_.Run(false, 0); | 254 transfer->callback_.Run(false, 0); |
301 } | 255 } |
302 | 256 |
303 } // namespace device | 257 } // namespace device |
OLD | NEW |