| 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(); | 113 } |
| 114 |
| 115 bool HidConnectionWin::available() const { |
| 116 return file_.IsValid(); |
| 115 } | 117 } |
| 116 | 118 |
| 117 HidConnectionWin::~HidConnectionWin() { | 119 HidConnectionWin::~HidConnectionWin() { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 CancelIo(file_.Get()); | 121 CancelIo(file_.Get()); |
| 120 } | 122 } |
| 121 | 123 |
| 122 void HidConnectionWin::Read(scoped_refptr<net::IOBuffer> buffer, | 124 void HidConnectionWin::Read(scoped_refptr<net::IOBufferWithSize> buffer, |
| 123 size_t size, | |
| 124 const HidConnection::IOCallback& callback) { | 125 const HidConnection::IOCallback& callback) { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 size_t report_size = device_info_.input_report_size; | 127 if (device_info().input_report_size == 0) { |
| 127 if (report_size == 0) { | 128 // The device does not support input reports. |
| 128 // The device does not supoort input reports. | |
| 129 callback.Run(false, 0); | 129 callback.Run(false, 0); |
| 130 return; | 130 return; |
| 131 } | 131 } |
| 132 | 132 |
| 133 if (size + !device_info_.has_report_id < report_size) { | 133 if (buffer->size() < device_info().input_report_size) { |
| 134 // Buffer too short. | |
| 135 callback.Run(false, 0); | 134 callback.Run(false, 0); |
| 136 return; | 135 return; |
| 137 } | 136 } |
| 138 | 137 |
| 139 scoped_refptr<net::IOBuffer> expanded_buffer; | 138 // If the device doesn't support report IDs, the caller should not be |
| 140 if (!device_info_.has_report_id) { | 139 // expecting one; however, Windows will always expect enough space for one, |
| 141 ++size; | 140 // 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)); | 141 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); |
| 143 } | 142 if (!device_info().has_report_id) |
| 143 receive_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 144 | 144 |
| 145 scoped_refptr<PendingTransfer> transfer( | 145 scoped_refptr<PendingHidTransfer> transfer( |
| 146 new PendingTransfer(this, buffer, expanded_buffer, true, callback)); | 146 new PendingHidTransfer(this, buffer, receive_buffer, callback)); |
| 147 transfers_.insert(transfer); | 147 transfers_.insert(transfer); |
| 148 transfer->TakeResultFromWindowsAPI(ReadFile(file_.Get(), | 148 transfer->TakeResultFromWindowsAPI( |
| 149 device_info_.has_report_id ? | 149 ReadFile(file_.Get(), |
| 150 buffer->data() : | 150 receive_buffer->data(), |
| 151 expanded_buffer->data(), | 151 static_cast<DWORD>(receive_buffer->size()), |
| 152 static_cast<DWORD>(size), | 152 NULL, |
| 153 NULL, | 153 transfer->GetOverlapped())); |
| 154 transfer->GetOverlapped())); | |
| 155 } | 154 } |
| 156 | 155 |
| 157 void HidConnectionWin::Write(scoped_refptr<net::IOBuffer> buffer, | 156 void HidConnectionWin::Write(uint8_t report_id, |
| 158 size_t size, | 157 scoped_refptr<net::IOBufferWithSize> buffer, |
| 159 const HidConnection::IOCallback& callback) { | 158 const HidConnection::IOCallback& callback) { |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(thread_checker_.CalledOnValidThread()); |
| 161 size_t report_size = device_info_.output_report_size; | 160 if (device_info().output_report_size == 0) { |
| 162 if (report_size == 0) { | 161 // The device does not support output reports. |
| 163 // The device does not supoort output reports. | |
| 164 callback.Run(false, 0); | 162 callback.Run(false, 0); |
| 165 return; | 163 return; |
| 166 } | 164 } |
| 167 | 165 |
| 168 if (size + !device_info_.has_report_id > report_size) { | 166 // The Windows API always wants either a report ID (if supported) or |
| 169 // Size of report too long. | 167 // zero at the front of every output report. |
| 168 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 169 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 170 output_buffer->data()[0] = report_id; |
| 171 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 172 |
| 173 scoped_refptr<PendingHidTransfer> transfer( |
| 174 new PendingHidTransfer(this, buffer, NULL, callback)); |
| 175 transfers_.insert(transfer); |
| 176 transfer->TakeResultFromWindowsAPI( |
| 177 WriteFile(file_.Get(), |
| 178 output_buffer->data(), |
| 179 static_cast<DWORD>(output_buffer->size()), |
| 180 NULL, |
| 181 transfer->GetOverlapped())); |
| 182 } |
| 183 |
| 184 void HidConnectionWin::GetFeatureReport( |
| 185 uint8_t report_id, |
| 186 scoped_refptr<net::IOBufferWithSize> buffer, |
| 187 const IOCallback& callback) { |
| 188 DCHECK(thread_checker_.CalledOnValidThread()); |
| 189 if (device_info().feature_report_size == 0) { |
| 190 // The device does not support feature reports. |
| 170 callback.Run(false, 0); | 191 callback.Run(false, 0); |
| 171 return; | 192 return; |
| 172 } | 193 } |
| 173 | 194 |
| 174 scoped_refptr<net::IOBuffer> expanded_buffer; | 195 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); | 196 callback.Run(false, 0); |
| 205 return; | 197 return; |
| 206 } | 198 } |
| 207 | 199 |
| 208 if (size + !device_info_.has_report_id < report_size) { | 200 scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer); |
| 209 // Buffer too short. | 201 if (!device_info().has_report_id) |
| 210 callback.Run(false, 0); | 202 receive_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 211 return; | |
| 212 } | |
| 213 | 203 |
| 214 scoped_refptr<net::IOBuffer> expanded_buffer; | 204 // The first byte of the destination buffer is the report ID being requested. |
| 215 if (!device_info_.has_report_id) { | 205 receive_buffer->data()[0] = report_id; |
| 216 ++size; | 206 scoped_refptr<PendingHidTransfer> transfer( |
| 217 expanded_buffer = new net::IOBuffer(static_cast<int>(size)); | 207 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); | 208 transfers_.insert(transfer); |
| 223 transfer->TakeResultFromWindowsAPI( | 209 transfer->TakeResultFromWindowsAPI( |
| 224 DeviceIoControl(file_.Get(), | 210 DeviceIoControl(file_.Get(), |
| 225 IOCTL_HID_GET_FEATURE, | 211 IOCTL_HID_GET_FEATURE, |
| 226 NULL, | 212 NULL, |
| 227 0, | 213 0, |
| 228 device_info_.has_report_id ? | 214 receive_buffer->data(), |
| 229 buffer->data() : | 215 static_cast<DWORD>(receive_buffer->size()), |
| 230 expanded_buffer->data(), | |
| 231 static_cast<DWORD>(size), | |
| 232 NULL, | 216 NULL, |
| 233 transfer->GetOverlapped())); | 217 transfer->GetOverlapped())); |
| 234 } | 218 } |
| 235 | 219 |
| 236 void HidConnectionWin::SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 220 void HidConnectionWin::SendFeatureReport( |
| 237 size_t size, | 221 uint8_t report_id, |
| 238 const IOCallback& callback) { | 222 scoped_refptr<net::IOBufferWithSize> buffer, |
| 223 const IOCallback& callback) { |
| 239 DCHECK(thread_checker_.CalledOnValidThread()); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 240 size_t report_size = device_info_.feature_report_size; | 225 if (device_info().feature_report_size == 0) { |
| 241 if (report_size == 0) { | 226 // The device does not support feature reports. |
| 242 // The device does not supoort output reports. | |
| 243 callback.Run(false, 0); | 227 callback.Run(false, 0); |
| 244 return; | 228 return; |
| 245 } | 229 } |
| 246 | 230 |
| 247 if (size + !device_info_.has_report_id > report_size) { | 231 if (buffer->size() < device_info().feature_report_size) { |
| 248 // Size of report too long. | |
| 249 callback.Run(false, 0); | 232 callback.Run(false, 0); |
| 250 return; | 233 return; |
| 251 } | 234 } |
| 252 | 235 |
| 253 scoped_refptr<net::IOBuffer> expanded_buffer; | 236 // The Windows API always wants either a report ID (if supported) or |
| 254 if (!device_info_.has_report_id) { | 237 // zero at the front of every output report. |
| 255 expanded_buffer = new net::IOBuffer( | 238 scoped_refptr<net::IOBufferWithSize> output_buffer(buffer); |
| 256 static_cast<int>(device_info_.feature_report_size)); | 239 output_buffer = new net::IOBufferWithSize(buffer->size() + 1); |
| 257 memset(expanded_buffer->data(), 0, device_info_.feature_report_size); | 240 output_buffer->data()[0] = report_id; |
| 258 memcpy(expanded_buffer->data() + 1, | 241 memcpy(output_buffer->data() + 1, buffer->data(), buffer->size()); |
| 259 buffer->data(), | |
| 260 size); | |
| 261 size++; | |
| 262 } | |
| 263 | 242 |
| 264 scoped_refptr<PendingTransfer> transfer( | 243 scoped_refptr<PendingHidTransfer> transfer( |
| 265 new PendingTransfer(this, buffer, expanded_buffer, false, callback)); | 244 new PendingHidTransfer(this, buffer, NULL, callback)); |
| 266 transfer->TakeResultFromWindowsAPI( | 245 transfer->TakeResultFromWindowsAPI( |
| 267 DeviceIoControl(file_.Get(), | 246 DeviceIoControl(file_.Get(), |
| 268 IOCTL_HID_SET_FEATURE, | 247 IOCTL_HID_SET_FEATURE, |
| 269 device_info_.has_report_id ? | 248 output_buffer->data(), |
| 270 buffer->data() : | 249 static_cast<DWORD>(output_buffer->size()), |
| 271 expanded_buffer->data(), | |
| 272 static_cast<DWORD>(device_info_.output_report_size), | |
| 273 NULL, | 250 NULL, |
| 274 0, | 251 0, |
| 275 NULL, | 252 NULL, |
| 276 transfer->GetOverlapped())); | 253 transfer->GetOverlapped())); |
| 277 } | 254 } |
| 278 | 255 |
| 256 void HidConnectionWin::OnTransferFinished( |
| 257 scoped_refptr<PendingHidTransfer> transfer) { |
| 258 DWORD bytes_transferred; |
| 259 transfers_.erase(transfer); |
| 260 if (GetOverlappedResult( |
| 261 file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) { |
| 262 if (bytes_transferred == 0) |
| 263 transfer->callback_.Run(true, 0); |
| 264 // If this is an input transfer and the receive buffer is not the same as |
| 265 // the target buffer, we need to copy the receive buffer into the target |
| 266 // buffer, discarding the first byte. This is because the target buffer's |
| 267 // owner is not expecting a report ID but Windows will always provide one. |
| 268 if (transfer->receive_buffer_ && |
| 269 transfer->receive_buffer_ != transfer->target_buffer_) { |
| 270 // Move one byte forward. |
| 271 --bytes_transferred; |
| 272 memcpy(transfer->target_buffer_->data(), |
| 273 transfer->receive_buffer_->data() + 1, |
| 274 bytes_transferred); |
| 275 } |
| 276 transfer->callback_.Run(true, bytes_transferred); |
| 277 } else { |
| 278 transfer->callback_.Run(false, 0); |
| 279 } |
| 280 } |
| 281 |
| 282 void HidConnectionWin::OnTransferCanceled( |
| 283 scoped_refptr<PendingHidTransfer> transfer) { |
| 284 transfers_.erase(transfer); |
| 285 transfer->callback_.Run(false, 0); |
| 286 } |
| 287 |
| 279 } // namespace device | 288 } // namespace device |
| OLD | NEW |