| 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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/win/object_watcher.h" |
| 13 #include "base/win/scoped_handle.h" |
| 12 #include "device/hid/hid_service.h" | 14 #include "device/hid/hid_service.h" |
| 13 #include "device/hid/hid_service_win.h" | 15 #include "device/hid/hid_service_win.h" |
| 14 #include "net/base/io_buffer.h" | |
| 15 | |
| 16 #if defined(OS_WIN) | |
| 17 | 16 |
| 18 #define INITGUID | 17 #define INITGUID |
| 19 | 18 |
| 20 #include <windows.h> | 19 #include <windows.h> |
| 21 #include <hidclass.h> | 20 #include <hidclass.h> |
| 22 | 21 |
| 23 extern "C" { | 22 extern "C" { |
| 24 | |
| 25 #include <hidsdi.h> | 23 #include <hidsdi.h> |
| 26 | |
| 27 } | 24 } |
| 28 | 25 |
| 29 #include <setupapi.h> | 26 #include <setupapi.h> |
| 30 #include <winioctl.h> | 27 #include <winioctl.h> |
| 31 #include "base/win/scoped_handle.h" | |
| 32 | |
| 33 #endif // defined(OS_WIN) | |
| 34 | 28 |
| 35 namespace device { | 29 namespace device { |
| 36 | 30 |
| 37 HidConnectionWin::PendingTransfer::PendingTransfer( | 31 struct PendingHidTransfer : public base::RefCounted<PendingHidTransfer>, |
| 38 scoped_refptr<HidConnectionWin> conn, | 32 public base::win::ObjectWatcher::Delegate, |
| 39 scoped_refptr<net::IOBuffer> target, | 33 public base::MessageLoop::DestructionObserver { |
| 40 scoped_refptr<net::IOBuffer> receiving, | 34 PendingHidTransfer(scoped_refptr<HidConnectionWin> connection, |
| 41 bool is_input, | 35 scoped_refptr<net::IOBufferWithSize> target_buffer, |
| 42 IOCallback callback) | 36 scoped_refptr<net::IOBufferWithSize> receive_buffer, |
| 43 : conn_(conn), | 37 HidConnection::IOCallback callback); |
| 44 is_input_(is_input), | 38 |
| 45 target_(target), | 39 void TakeResultFromWindowsAPI(BOOL result); |
| 46 receiving_(receiving), | 40 |
| 41 OVERLAPPED* GetOverlapped() { return &overlapped_; } |
| 42 |
| 43 // Implements base::win::ObjectWatcher::Delegate. |
| 44 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 45 |
| 46 // Implements base::MessageLoop::DestructionObserver |
| 47 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 48 |
| 49 scoped_refptr<HidConnectionWin> connection_; |
| 50 scoped_refptr<net::IOBufferWithSize> target_buffer_; |
| 51 scoped_refptr<net::IOBufferWithSize> receive_buffer_; |
| 52 HidConnection::IOCallback callback_; |
| 53 OVERLAPPED overlapped_; |
| 54 base::win::ScopedHandle event_; |
| 55 base::win::ObjectWatcher watcher_; |
| 56 |
| 57 private: |
| 58 friend class base::RefCounted<PendingHidTransfer>; |
| 59 |
| 60 virtual ~PendingHidTransfer(); |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(PendingHidTransfer); |
| 63 }; |
| 64 |
| 65 PendingHidTransfer::PendingHidTransfer( |
| 66 scoped_refptr<HidConnectionWin> connection, |
| 67 scoped_refptr<net::IOBufferWithSize> target_buffer, |
| 68 scoped_refptr<net::IOBufferWithSize> receive_buffer, |
| 69 HidConnection::IOCallback callback) |
| 70 : connection_(connection), |
| 71 target_buffer_(target_buffer), |
| 72 receive_buffer_(receive_buffer), |
| 47 callback_(callback), | 73 callback_(callback), |
| 48 event_(CreateEvent(NULL, FALSE, FALSE, NULL)) { | 74 event_(CreateEvent(NULL, FALSE, FALSE, NULL)) { |
| 49 memset(&overlapped_, 0, sizeof(OVERLAPPED)); | 75 memset(&overlapped_, 0, sizeof(OVERLAPPED)); |
| 50 overlapped_.hEvent = event_.Get(); | 76 overlapped_.hEvent = event_.Get(); |
| 51 } | 77 } |
| 52 HidConnectionWin::PendingTransfer::~PendingTransfer() { | 78 |
| 79 PendingHidTransfer::~PendingHidTransfer() { |
| 53 base::MessageLoop::current()->RemoveDestructionObserver(this); | 80 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 54 } | 81 } |
| 55 | 82 |
| 56 void HidConnectionWin::PendingTransfer::TakeResultFromWindowsAPI(BOOL result) { | 83 void PendingHidTransfer::TakeResultFromWindowsAPI(BOOL result) { |
| 57 if (result || GetLastError() != ERROR_IO_PENDING) { | 84 if (result || GetLastError() != ERROR_IO_PENDING) { |
| 58 conn_->OnTransferFinished(this); | 85 connection_->OnTransferFinished(this); |
| 59 } else { | 86 } else { |
| 60 base::MessageLoop::current()->AddDestructionObserver(this); | 87 base::MessageLoop::current()->AddDestructionObserver(this); |
| 61 AddRef(); | 88 AddRef(); |
| 62 watcher_.StartWatching(event_.Get(), this); | 89 watcher_.StartWatching(event_.Get(), this); |
| 63 } | 90 } |
| 64 } | 91 } |
| 65 | 92 |
| 66 void HidConnectionWin::PendingTransfer::OnObjectSignaled(HANDLE event_handle) { | 93 void PendingHidTransfer::OnObjectSignaled(HANDLE event_handle) { |
| 67 conn_->OnTransferFinished(this); | 94 connection_->OnTransferFinished(this); |
| 68 Release(); | 95 Release(); |
| 69 } | 96 } |
| 70 | 97 |
| 71 void HidConnectionWin::PendingTransfer::WillDestroyCurrentMessageLoop() { | 98 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { |
| 72 watcher_.StopWatching(); | 99 watcher_.StopWatching(); |
| 73 conn_->OnTransferCanceled(this); | 100 connection_->OnTransferCanceled(this); |
| 74 } | 101 } |
| 75 | 102 |
| 76 void HidConnectionWin::OnTransferFinished( | 103 HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info) |
| 77 scoped_refptr<PendingTransfer> transfer) { | 104 : HidConnection(device_info) { |
| 78 DWORD bytes_transfered; | |
| 79 transfers_.erase(transfer); | |
| 80 if (GetOverlappedResult(file_, | |
| 81 transfer->GetOverlapped(), | |
| 82 &bytes_transfered, | |
| 83 FALSE)) { | |
| 84 if (transfer->is_input_ && !device_info_.has_report_id) { | |
| 85 // Move one byte forward. | |
| 86 --bytes_transfered; | |
| 87 memcpy(transfer->target_->data(), | |
| 88 transfer->receiving_->data() + 1, | |
| 89 bytes_transfered); | |
| 90 } | |
| 91 transfer->callback_.Run(true, bytes_transfered); | |
| 92 } else { | |
| 93 transfer->callback_.Run(false, 0); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void HidConnectionWin::OnTransferCanceled( | |
| 98 scoped_refptr<PendingTransfer> transfer) { | |
| 99 transfers_.erase(transfer); | |
| 100 transfer->callback_.Run(false, 0); | |
| 101 } | |
| 102 | |
| 103 HidConnectionWin::HidConnectionWin(HidDeviceInfo device_info) | |
| 104 : HidConnection(device_info), | |
| 105 available_(false) { | |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 file_.Set(CreateFileA(device_info.device_id.c_str(), | 106 file_.Set(CreateFileA(device_info.device_id.c_str(), |
| 108 GENERIC_WRITE | GENERIC_READ, | 107 GENERIC_WRITE | GENERIC_READ, |
| 109 FILE_SHARE_READ | FILE_SHARE_WRITE, | 108 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 110 NULL, | 109 NULL, |
| 111 OPEN_EXISTING, | 110 OPEN_EXISTING, |
| 112 FILE_FLAG_OVERLAPPED, | 111 FILE_FLAG_OVERLAPPED, |
| 113 NULL)); | 112 NULL)); |
| 114 available_ = file_.IsValid(); | |
| 115 } | 113 } |
| 116 | 114 |
| 115 bool HidConnectionWin::available() const { return file_.IsValid(); } |
| 116 |
| 117 HidConnectionWin::~HidConnectionWin() { | 117 HidConnectionWin::~HidConnectionWin() { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 CancelIo(file_.Get()); | 119 CancelIo(file_.Get()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 void HidConnectionWin::Read(scoped_refptr<net::IOBuffer> buffer, | 122 void HidConnectionWin::Read(scoped_refptr<net::IOBufferWithSize> buffer, |
| 123 size_t size, | |
| 124 const HidConnection::IOCallback& callback) { | 123 const HidConnection::IOCallback& callback) { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 size_t report_size = device_info_.input_report_size; | 125 if (device_info().input_report_size == 0) { |
| 127 if (report_size == 0) { | 126 // The device does not support input reports. |
| 128 // The device does not supoort input reports. | |
| 129 callback.Run(false, 0); | 127 callback.Run(false, 0); |
| 130 return; | 128 return; |
| 131 } | 129 } |
| 132 | 130 |
| 133 if (size + !device_info_.has_report_id < report_size) { | 131 if (buffer->size() < device_info().input_report_size) { |
| 134 // Buffer too short. | |
| 135 callback.Run(false, 0); | 132 callback.Run(false, 0); |
| 136 return; | 133 return; |
| 137 } | 134 } |
| 138 | 135 |
| 139 scoped_refptr<net::IOBuffer> expanded_buffer; | 136 // If the device doesn't support report IDs, the caller should not be |
| 140 if (!device_info_.has_report_id) { | 137 // expecting one; however, Windows will always expect enough space for one, |
| 141 ++size; | 138 // so we need to use a buffer with one extra byte of space in this case. |
| 142 expanded_buffer = new net::IOBuffer(static_cast<int>(size)); | 139 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); |
| 143 } | 140 if (!device_info().has_report_id) |
| 141 receive_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 144 | 142 |
| 145 scoped_refptr<PendingTransfer> transfer( | 143 scoped_refptr<PendingHidTransfer> transfer( |
| 146 new PendingTransfer(this, buffer, expanded_buffer, true, callback)); | 144 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
| 147 transfers_.insert(transfer); | 145 transfers_.insert(transfer); |
| 148 transfer->TakeResultFromWindowsAPI(ReadFile(file_.Get(), | 146 transfer->TakeResultFromWindowsAPI( |
| 149 device_info_.has_report_id ? | 147 ReadFile(file_.Get(), |
| 150 buffer->data() : | 148 receive_buffer->data(), |
| 151 expanded_buffer->data(), | 149 static_cast<DWORD>(receive_buffer->size()), |
| 152 static_cast<DWORD>(size), | 150 NULL, |
| 153 NULL, | 151 transfer->GetOverlapped())); |
| 154 transfer->GetOverlapped())); | |
| 155 } | 152 } |
| 156 | 153 |
| 157 void HidConnectionWin::Write(scoped_refptr<net::IOBuffer> buffer, | 154 void HidConnectionWin::Write(uint8_t report_id, |
| 158 size_t size, | 155 scoped_refptr<net::IOBufferWithSize> buffer, |
| 159 const HidConnection::IOCallback& callback) { | 156 const HidConnection::IOCallback& callback) { |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 161 size_t report_size = device_info_.output_report_size; | 158 if (device_info().output_report_size == 0) { |
| 162 if (report_size == 0) { | 159 // The device does not support output reports. |
| 163 // The device does not supoort output reports. | |
| 164 callback.Run(false, 0); | 160 callback.Run(false, 0); |
| 165 return; | 161 return; |
| 166 } | 162 } |
| 167 | 163 |
| 168 if (size + !device_info_.has_report_id > report_size) { | 164 // The Windows API always wants either a report ID (if supported) or |
| 169 // Size of report too long. | 165 // zero at the front of every output report. |
| 166 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 167 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 168 output_buffer->data()[0] = report_id; |
| 169 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 170 |
| 171 scoped_refptr<PendingHidTransfer> transfer( |
| 172 new PendingHidTransfer(this, buffer, NULL, callback)); |
| 173 transfers_.insert(transfer); |
| 174 transfer->TakeResultFromWindowsAPI( |
| 175 WriteFile(file_.Get(), |
| 176 output_buffer->data(), |
| 177 static_cast<DWORD>(output_buffer->size()), |
| 178 NULL, |
| 179 transfer->GetOverlapped())); |
| 180 } |
| 181 |
| 182 void HidConnectionWin::GetFeatureReport( |
| 183 uint8_t report_id, |
| 184 scoped_refptr<net::IOBufferWithSize> buffer, |
| 185 const IOCallback& callback) { |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); |
| 187 if (device_info().feature_report_size == 0) { |
| 188 // The device does not support feature reports. |
| 170 callback.Run(false, 0); | 189 callback.Run(false, 0); |
| 171 return; | 190 return; |
| 172 } | 191 } |
| 173 | 192 |
| 174 scoped_refptr<net::IOBuffer> expanded_buffer; | 193 if (buffer->size() < device_info().feature_report_size) { |
| 175 if (!device_info_.has_report_id) { | |
| 176 expanded_buffer = new net::IOBuffer( | |
| 177 static_cast<int>(device_info_.output_report_size)); | |
| 178 memset(expanded_buffer->data(), 0, device_info_.output_report_size); | |
| 179 memcpy(expanded_buffer->data() + 1, | |
| 180 buffer->data(), | |
| 181 size); | |
| 182 size++; | |
| 183 } | |
| 184 | |
| 185 scoped_refptr<PendingTransfer> transfer( | |
| 186 new PendingTransfer(this, buffer, expanded_buffer, false, callback)); | |
| 187 transfers_.insert(transfer); | |
| 188 transfer->TakeResultFromWindowsAPI( | |
| 189 WriteFile(file_.Get(), | |
| 190 device_info_.has_report_id ? | |
| 191 buffer->data() : expanded_buffer->data(), | |
| 192 static_cast<DWORD>(device_info_.output_report_size), | |
| 193 NULL, | |
| 194 transfer->GetOverlapped())); | |
| 195 } | |
| 196 | |
| 197 void HidConnectionWin::GetFeatureReport(scoped_refptr<net::IOBuffer> buffer, | |
| 198 size_t size, | |
| 199 const IOCallback& callback) { | |
| 200 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 201 size_t report_size = device_info_.feature_report_size; | |
| 202 if (report_size == 0) { | |
| 203 // The device does not supoort input reports. | |
| 204 callback.Run(false, 0); | 194 callback.Run(false, 0); |
| 205 return; | 195 return; |
| 206 } | 196 } |
| 207 | 197 |
| 208 if (size + !device_info_.has_report_id < report_size) { | 198 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); |
| 209 // Buffer too short. | 199 if (!device_info().has_report_id) |
| 210 callback.Run(false, 0); | 200 receive_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 211 return; | |
| 212 } | |
| 213 | 201 |
| 214 scoped_refptr<net::IOBuffer> expanded_buffer; | 202 // The first byte of the destination buffer is the report ID being requested. |
| 215 if (!device_info_.has_report_id) { | 203 receive_buffer->data()[0] = report_id; |
| 216 ++size; | 204 scoped_refptr<PendingHidTransfer> transfer( |
| 217 expanded_buffer = new net::IOBuffer(static_cast<int>(size)); | 205 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
| 218 } | |
| 219 | |
| 220 scoped_refptr<PendingTransfer> transfer( | |
| 221 new PendingTransfer(this, buffer, expanded_buffer, true, callback)); | |
| 222 transfers_.insert(transfer); | 206 transfers_.insert(transfer); |
| 223 transfer->TakeResultFromWindowsAPI( | 207 transfer->TakeResultFromWindowsAPI( |
| 224 DeviceIoControl(file_.Get(), | 208 DeviceIoControl(file_.Get(), |
| 225 IOCTL_HID_GET_FEATURE, | 209 IOCTL_HID_GET_FEATURE, |
| 226 NULL, | 210 NULL, |
| 227 0, | 211 0, |
| 228 device_info_.has_report_id ? | 212 receive_buffer->data(), |
| 229 buffer->data() : | 213 static_cast<DWORD>(receive_buffer->size()), |
| 230 expanded_buffer->data(), | |
| 231 static_cast<DWORD>(size), | |
| 232 NULL, | 214 NULL, |
| 233 transfer->GetOverlapped())); | 215 transfer->GetOverlapped())); |
| 234 } | 216 } |
| 235 | 217 |
| 236 void HidConnectionWin::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 218 void HidConnectionWin::SendFeatureReport( |
| 237 size_t size, | 219 uint8_t report_id, |
| 238 const IOCallback& callback) { | 220 scoped_refptr<net::IOBufferWithSize> buffer, |
| 221 const IOCallback& callback) { |
| 239 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
| 240 size_t report_size = device_info_.feature_report_size; | 223 if (device_info().feature_report_size == 0) { |
| 241 if (report_size == 0) { | 224 // The device does not support feature reports. |
| 242 // The device does not supoort output reports. | |
| 243 callback.Run(false, 0); | 225 callback.Run(false, 0); |
| 244 return; | 226 return; |
| 245 } | 227 } |
| 246 | 228 |
| 247 if (size + !device_info_.has_report_id > report_size) { | 229 if (buffer->size() < device_info().feature_report_size) { |
| 248 // Size of report too long. | |
| 249 callback.Run(false, 0); | 230 callback.Run(false, 0); |
| 250 return; | 231 return; |
| 251 } | 232 } |
| 252 | 233 |
| 253 scoped_refptr<net::IOBuffer> expanded_buffer; | 234 // The Windows API always wants either a report ID (if supported) or |
| 254 if (!device_info_.has_report_id) { | 235 // zero at the front of every output report. |
| 255 expanded_buffer = new net::IOBuffer( | 236 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 256 static_cast<int>(device_info_.feature_report_size)); | 237 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 257 memset(expanded_buffer->data(), 0, device_info_.feature_report_size); | 238 output_buffer->data()[0] = report_id; |
| 258 memcpy(expanded_buffer->data() + 1, | 239 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 259 buffer->data(), | |
| 260 size); | |
| 261 size++; | |
| 262 } | |
| 263 | 240 |
| 264 scoped_refptr<PendingTransfer> transfer( | 241 scoped_refptr<PendingHidTransfer> transfer( |
| 265 new PendingTransfer(this, buffer, expanded_buffer, false, callback)); | 242 new PendingHidTransfer(this, buffer, NULL, callback)); |
| 266 transfer->TakeResultFromWindowsAPI( | 243 transfer->TakeResultFromWindowsAPI( |
| 267 DeviceIoControl(file_.Get(), | 244 DeviceIoControl(file_.Get(), |
| 268 IOCTL_HID_SET_FEATURE, | 245 IOCTL_HID_SET_FEATURE, |
| 269 device_info_.has_report_id ? | 246 output_buffer->data(), |
| 270 buffer->data() : | 247 static_cast<DWORD>(output_buffer->size()), |
| 271 expanded_buffer->data(), | |
| 272 static_cast<DWORD>(device_info_.output_report_size), | |
| 273 NULL, | 248 NULL, |
| 274 0, | 249 0, |
| 275 NULL, | 250 NULL, |
| 276 transfer->GetOverlapped())); | 251 transfer->GetOverlapped())); |
| 277 } | 252 } |
| 278 | 253 |
| 254 void HidConnectionWin::OnTransferFinished( |
| 255 scoped_refptr<PendingHidTransfer> transfer) { |
| 256 DWORD bytes_transferred; |
| 257 transfers_.erase(transfer); |
| 258 if (GetOverlappedResult( |
| 259 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { |
| 260 if (bytes_transferred == 0) |
| 261 transfer->callback_.Run(true, 0); |
| 262 // If this is an input transfer and the receive buffer is not the same as |
| 263 // the target buffer, we need to copy the receive buffer into the target |
| 264 // buffer, discarding the first byte. This is because the target buffer's |
| 265 // owner is not expecting a report ID but Windows will always provide one. |
| 266 if (transfer->receive_buffer_ && |
| 267 transfer->receive_buffer_ != transfer->target_buffer_) { |
| 268 // Move one byte forward. |
| 269 --bytes_transferred; |
| 270 memcpy(transfer->target_buffer_->data(), |
| 271 transfer->receive_buffer_->data() + 1, |
| 272 bytes_transferred); |
| 273 } |
| 274 transfer->callback_.Run(true, bytes_transferred); |
| 275 } else { |
| 276 transfer->callback_.Run(false, 0); |
| 277 } |
| 278 } |
| 279 |
| 280 void HidConnectionWin::OnTransferCanceled( |
| 281 scoped_refptr<PendingHidTransfer> transfer) { |
| 282 transfers_.erase(transfer); |
| 283 transfer->callback_.Run(false, 0); |
| 284 } |
| 285 |
| 279 } // namespace device | 286 } // namespace device |
| OLD | NEW |