Chromium Code Reviews| 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 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 Release(); | 91 Release(); |
| 97 } | 92 } |
| 98 | 93 |
| 99 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { | 94 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { |
| 100 watcher_.StopWatching(); | 95 watcher_.StopWatching(); |
| 101 connection_->OnTransferCanceled(this); | 96 connection_->OnTransferCanceled(this); |
| 102 } | 97 } |
| 103 | 98 |
| 104 HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info) | 99 HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info) |
| 105 : HidConnection(device_info) { | 100 : HidConnection(device_info) { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 107 file_.Set(CreateFileA(device_info.device_id.c_str(), | 101 file_.Set(CreateFileA(device_info.device_id.c_str(), |
| 108 GENERIC_WRITE | GENERIC_READ, | 102 GENERIC_WRITE | GENERIC_READ, |
| 109 FILE_SHARE_READ | FILE_SHARE_WRITE, | 103 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 110 NULL, | 104 NULL, |
| 111 OPEN_EXISTING, | 105 OPEN_EXISTING, |
| 112 FILE_FLAG_OVERLAPPED, | 106 FILE_FLAG_OVERLAPPED, |
| 113 NULL)); | 107 NULL)); |
| 114 | 108 |
| 115 if (!file_.IsValid() && | 109 if (!file_.IsValid() && |
| 116 GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) { | 110 GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) { |
| 117 file_.Set(CreateFileA(device_info.device_id.c_str(), | 111 file_.Set(CreateFileA(device_info.device_id.c_str(), |
| 118 GENERIC_READ, | 112 GENERIC_READ, |
| 119 FILE_SHARE_READ, | 113 FILE_SHARE_READ, |
| 120 NULL, | 114 NULL, |
| 121 OPEN_EXISTING, | 115 OPEN_EXISTING, |
| 122 FILE_FLAG_OVERLAPPED, | 116 FILE_FLAG_OVERLAPPED, |
| 123 NULL)); | 117 NULL)); |
| 124 } | 118 } |
| 125 } | 119 } |
| 126 | 120 |
| 127 bool HidConnectionWin::available() const { | |
| 128 return file_.IsValid(); | |
| 129 } | |
| 130 | |
| 131 HidConnectionWin::~HidConnectionWin() { | 121 HidConnectionWin::~HidConnectionWin() { |
| 132 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 133 CancelIo(file_.Get()); | 122 CancelIo(file_.Get()); |
| 134 } | 123 } |
| 135 | 124 |
| 136 void HidConnectionWin::Read(scoped_refptr<net::IOBufferWithSize> buffer, | 125 void HidConnectionWin::PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer, |
| 137 const HidConnection::IOCallback& callback) { | 126 const HidConnection::IOCallback& callback) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 127 scoped_refptr<net::IOBufferWithSize> receive_buffer = |
| 139 if (device_info().input_report_size == 0) { | 128 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 | 129 |
| 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( | 130 scoped_refptr<PendingHidTransfer> transfer( |
| 162 new PendingHidTransfer(this, buffer, receive_buffer, callback)); | 131 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
| 163 transfers_.insert(transfer); | 132 transfers_.insert(transfer); |
| 164 transfer->TakeResultFromWindowsAPI( | 133 transfer->TakeResultFromWindowsAPI( |
| 165 ReadFile(file_.Get(), | 134 ReadFile(file_.Get(), |
| 166 receive_buffer->data(), | 135 receive_buffer->data(), |
| 167 static_cast<DWORD>(receive_buffer->size()), | 136 static_cast<DWORD>(receive_buffer->size()), |
| 168 NULL, | 137 NULL, |
| 169 transfer->GetOverlapped())); | 138 transfer->GetOverlapped())); |
| 170 } | 139 } |
| 171 | 140 |
| 172 void HidConnectionWin::Write(uint8_t report_id, | 141 void HidConnectionWin::PlatformWrite( |
| 173 scoped_refptr<net::IOBufferWithSize> buffer, | 142 uint8_t report_id, |
| 174 const HidConnection::IOCallback& callback) { | 143 scoped_refptr<net::IOBufferWithSize> buffer, |
| 175 DCHECK(thread_checker_.CalledOnValidThread()); | 144 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 | 145 // The Windows API always wants either a report ID (if supported) or |
| 183 // zero at the front of every output report. | 146 // zero at the front of every output report. |
| 184 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); | 147 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 185 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); | 148 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 186 output_buffer->data()[0] = report_id; | 149 output_buffer->data()[0] = report_id; |
| 187 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); | 150 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 188 | 151 |
| 189 scoped_refptr<PendingHidTransfer> transfer( | 152 scoped_refptr<PendingHidTransfer> transfer( |
| 190 new PendingHidTransfer(this, output_buffer, NULL, callback)); | 153 new PendingHidTransfer(this, output_buffer, NULL, callback)); |
| 191 transfers_.insert(transfer); | 154 transfers_.insert(transfer); |
| 192 transfer->TakeResultFromWindowsAPI( | 155 transfer->TakeResultFromWindowsAPI( |
| 193 WriteFile(file_.Get(), | 156 WriteFile(file_.Get(), |
| 194 output_buffer->data(), | 157 output_buffer->data(), |
| 195 static_cast<DWORD>(output_buffer->size()), | 158 static_cast<DWORD>(output_buffer->size()), |
| 196 NULL, | 159 NULL, |
| 197 transfer->GetOverlapped())); | 160 transfer->GetOverlapped())); |
| 198 } | 161 } |
| 199 | 162 |
| 200 void HidConnectionWin::GetFeatureReport( | 163 void HidConnectionWin::PlatformGetFeatureReport( |
| 201 uint8_t report_id, | 164 uint8_t report_id, |
| 202 scoped_refptr<net::IOBufferWithSize> buffer, | 165 scoped_refptr<net::IOBufferWithSize> buffer, |
| 203 const IOCallback& callback) { | 166 const IOCallback& callback) { |
| 204 DCHECK(thread_checker_.CalledOnValidThread()); | 167 scoped_refptr<net::IOBufferWithSize> receive_buffer = |
| 205 if (device_info().feature_report_size == 0) { | 168 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. | 169 // The first byte of the destination buffer is the report ID being requested. |
| 225 receive_buffer->data()[0] = report_id; | 170 receive_buffer->data()[0] = report_id; |
| 171 | |
| 226 scoped_refptr<PendingHidTransfer> transfer( | 172 scoped_refptr<PendingHidTransfer> transfer( |
| 227 new PendingHidTransfer(this, buffer, receive_buffer, callback)); | 173 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
| 228 transfers_.insert(transfer); | 174 transfers_.insert(transfer); |
| 229 transfer->TakeResultFromWindowsAPI( | 175 transfer->TakeResultFromWindowsAPI( |
| 230 DeviceIoControl(file_.Get(), | 176 DeviceIoControl(file_.Get(), |
| 231 IOCTL_HID_GET_FEATURE, | 177 IOCTL_HID_GET_FEATURE, |
| 232 NULL, | 178 NULL, |
| 233 0, | 179 0, |
| 234 receive_buffer->data(), | 180 receive_buffer->data(), |
| 235 static_cast<DWORD>(receive_buffer->size()), | 181 static_cast<DWORD>(receive_buffer->size()), |
| 236 NULL, | 182 NULL, |
| 237 transfer->GetOverlapped())); | 183 transfer->GetOverlapped())); |
| 238 } | 184 } |
| 239 | 185 |
| 240 void HidConnectionWin::SendFeatureReport( | 186 void HidConnectionWin::PlatformSendFeatureReport( |
| 241 uint8_t report_id, | 187 uint8_t report_id, |
| 242 scoped_refptr<net::IOBufferWithSize> buffer, | 188 scoped_refptr<net::IOBufferWithSize> buffer, |
| 243 const IOCallback& callback) { | 189 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 | 190 // The Windows API always wants either a report ID (if supported) or |
| 252 // zero at the front of every output report. | 191 // zero at the front of every output report. |
| 253 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); | 192 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 254 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); | 193 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 255 output_buffer->data()[0] = report_id; | 194 output_buffer->data()[0] = report_id; |
| 256 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); | 195 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 257 | 196 |
| 258 scoped_refptr<PendingHidTransfer> transfer( | 197 scoped_refptr<PendingHidTransfer> transfer( |
| 259 new PendingHidTransfer(this, output_buffer, NULL, callback)); | 198 new PendingHidTransfer(this, output_buffer, NULL, callback)); |
| 260 transfer->TakeResultFromWindowsAPI( | 199 transfer->TakeResultFromWindowsAPI( |
| 261 DeviceIoControl(file_.Get(), | 200 DeviceIoControl(file_.Get(), |
| 262 IOCTL_HID_SET_FEATURE, | 201 IOCTL_HID_SET_FEATURE, |
| 263 output_buffer->data(), | 202 output_buffer->data(), |
| 264 static_cast<DWORD>(output_buffer->size()), | 203 static_cast<DWORD>(output_buffer->size()), |
| 265 NULL, | 204 NULL, |
| 266 0, | 205 0, |
| 267 NULL, | 206 NULL, |
| 268 transfer->GetOverlapped())); | 207 transfer->GetOverlapped())); |
| 269 } | 208 } |
| 270 | 209 |
| 271 void HidConnectionWin::OnTransferFinished( | 210 void HidConnectionWin::OnTransferFinished( |
| 272 scoped_refptr<PendingHidTransfer> transfer) { | 211 scoped_refptr<PendingHidTransfer> transfer) { |
| 212 transfers_.erase(transfer); | |
| 213 | |
| 273 DWORD bytes_transferred; | 214 DWORD bytes_transferred; |
| 274 transfers_.erase(transfer); | |
| 275 if (GetOverlappedResult( | 215 if (GetOverlappedResult( |
| 276 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { | 216 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { |
| 277 if (bytes_transferred == 0) | 217 if (bytes_transferred == 0) { |
| 278 transfer->callback_.Run(true, 0); | 218 transfer->callback_.Run(true, 0); |
| 279 // If this is an input transfer and the receive buffer is not the same as | 219 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 } | 220 } |
| 291 transfer->callback_.Run(true, bytes_transferred); | 221 |
| 222 if (transfer->receive_buffer_) { | |
| 223 // If owner HID top-level collection does not have report ID, we need to | |
| 224 // copy the receive buffer into the target buffer, discarding the first | |
| 225 // byte. This is because the target buffer's owner is not expecting a | |
| 226 // report ID but Windows will always provide one. | |
| 227 if (!has_report_id()) { | |
| 228 // Assert first byte is 0x00 | |
| 229 DCHECK(transfer->receive_buffer_->data()[0] == | |
|
Ken Rockot(use gerrit already)
2014/07/01 14:55:23
An assert here is pretty aggressive since we can't
jracle (use Gerrit)
2014/07/01 15:32:38
Ok, Should I pass report ID as an argument?
VLOG(
Ken Rockot(use gerrit already)
2014/07/01 15:37:14
Doesn't much matter IMO. But yes if you want to in
jracle (use Gerrit)
2014/07/01 15:42:56
OK thanks
On 2014/07/01 15:37:14, Ken Rockot wrot
| |
| 230 HidConnection::kNullReportId); | |
| 231 | |
| 232 // Move one byte forward. | |
| 233 --bytes_transferred; | |
| 234 memcpy(transfer->target_buffer_->data(), | |
| 235 transfer->receive_buffer_->data() + 1, | |
| 236 bytes_transferred); | |
| 237 } else { | |
| 238 memcpy(transfer->target_buffer_->data(), | |
| 239 transfer->receive_buffer_->data(), | |
| 240 bytes_transferred); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 CompleteRead(transfer->target_buffer_, transfer->callback_); | |
|
Ken Rockot(use gerrit already)
2014/07/01 14:55:23
Boo. So the way this is structured (not having sep
jracle (use Gerrit)
2014/07/01 15:32:38
Indeed..
Do I have to handle CompleteRead return
Ken Rockot(use gerrit already)
2014/07/01 15:37:14
Nope, not much you can do with it yet.
jracle (use Gerrit)
2014/07/01 15:42:56
got it.
On 2014/07/01 15:37:14, Ken Rockot wrote:
| |
| 292 } else { | 245 } else { |
| 293 transfer->callback_.Run(false, 0); | 246 transfer->callback_.Run(false, 0); |
| 294 } | 247 } |
| 295 } | 248 } |
| 296 | 249 |
| 297 void HidConnectionWin::OnTransferCanceled( | 250 void HidConnectionWin::OnTransferCanceled( |
| 298 scoped_refptr<PendingHidTransfer> transfer) { | 251 scoped_refptr<PendingHidTransfer> transfer) { |
| 299 transfers_.erase(transfer); | 252 transfers_.erase(transfer); |
| 300 transfer->callback_.Run(false, 0); | 253 transfer->callback_.Run(false, 0); |
| 301 } | 254 } |
| 302 | 255 |
| 303 } // namespace device | 256 } // namespace device |
| OLD | NEW |