summaryrefslogtreecommitdiffstats
path: root/device/hid/hid_connection_win.cc
blob: 44bd8471bc1ca95d803e39e9212ab0a015e44cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/hid/hid_connection_win.h"

#include <cstring>

#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/object_watcher.h"
#include "base/win/scoped_handle.h"
#include "device/hid/hid_service.h"
#include "device/hid/hid_service_win.h"

#define INITGUID

#include <windows.h>
#include <hidclass.h>

extern "C" {
#include <hidsdi.h>
}

#include <setupapi.h>
#include <winioctl.h>

namespace device {

struct PendingHidTransfer : public base::RefCounted<PendingHidTransfer>,
                            public base::win::ObjectWatcher::Delegate,
                            public base::MessageLoop::DestructionObserver {
  PendingHidTransfer(scoped_refptr<HidConnectionWin> connection,
                     scoped_refptr<net::IOBufferWithSize> target_buffer,
                     scoped_refptr<net::IOBufferWithSize> receive_buffer,
                     HidConnection::IOCallback callback);

  void TakeResultFromWindowsAPI(BOOL result);

  OVERLAPPED* GetOverlapped() { return &overlapped_; }

  // Implements base::win::ObjectWatcher::Delegate.
  virtual void OnObjectSignaled(HANDLE object) OVERRIDE;

  // Implements base::MessageLoop::DestructionObserver
  virtual void WillDestroyCurrentMessageLoop() OVERRIDE;

  scoped_refptr<HidConnectionWin> connection_;
  scoped_refptr<net::IOBufferWithSize> target_buffer_;
  scoped_refptr<net::IOBufferWithSize> receive_buffer_;
  HidConnection::IOCallback callback_;
  OVERLAPPED overlapped_;
  base::win::ScopedHandle event_;
  base::win::ObjectWatcher watcher_;

 private:
  friend class base::RefCounted<PendingHidTransfer>;

  virtual ~PendingHidTransfer();

  DISALLOW_COPY_AND_ASSIGN(PendingHidTransfer);
};

PendingHidTransfer::PendingHidTransfer(
    scoped_refptr<HidConnectionWin> connection,
    scoped_refptr<net::IOBufferWithSize> target_buffer,
    scoped_refptr<net::IOBufferWithSize> receive_buffer,
    HidConnection::IOCallback callback)
    : connection_(connection),
      target_buffer_(target_buffer),
      receive_buffer_(receive_buffer),
      callback_(callback),
      event_(CreateEvent(NULL, FALSE, FALSE, NULL)) {
  memset(&overlapped_, 0, sizeof(OVERLAPPED));
  overlapped_.hEvent = event_.Get();
}

PendingHidTransfer::~PendingHidTransfer() {
  base::MessageLoop::current()->RemoveDestructionObserver(this);
}

void PendingHidTransfer::TakeResultFromWindowsAPI(BOOL result) {
  if (result || GetLastError() != ERROR_IO_PENDING) {
    connection_->OnTransferFinished(this);
  } else {
    base::MessageLoop::current()->AddDestructionObserver(this);
    AddRef();
    watcher_.StartWatching(event_.Get(), this);
  }
}

void PendingHidTransfer::OnObjectSignaled(HANDLE event_handle) {
  connection_->OnTransferFinished(this);
  Release();
}

void PendingHidTransfer::WillDestroyCurrentMessageLoop() {
  watcher_.StopWatching();
  connection_->OnTransferCanceled(this);
}

HidConnectionWin::HidConnectionWin(const HidDeviceInfo& device_info)
    : HidConnection(device_info) {
  DCHECK(thread_checker_.CalledOnValidThread());
  file_.Set(CreateFileA(device_info.device_id.c_str(),
                        GENERIC_WRITE | GENERIC_READ,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED,
                        NULL));
}

bool HidConnectionWin::available() const {
  return file_.IsValid();
}

HidConnectionWin::~HidConnectionWin() {
  DCHECK(thread_checker_.CalledOnValidThread());
  CancelIo(file_.Get());
}

void HidConnectionWin::Read(scoped_refptr<net::IOBufferWithSize> buffer,
                            const HidConnection::IOCallback& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (device_info().input_report_size == 0) {
    // The device does not support input reports.
    callback.Run(false, 0);
    return;
  }

  if (buffer->size() < device_info().input_report_size) {
    callback.Run(false, 0);
    return;
  }

  // If the device doesn't support report IDs, the caller should not be
  // expecting one; however, Windows will always expect enough space for one,
  // so we need to use a buffer with one extra byte of space in this case.
  scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer);
  if (!device_info().has_report_id)
    receive_buffer = new net::IOBufferWithSize(buffer->size() + 1);

  scoped_refptr<PendingHidTransfer> transfer(
      new PendingHidTransfer(this, buffer, receive_buffer, callback));
  transfers_.insert(transfer);
  transfer->TakeResultFromWindowsAPI(
      ReadFile(file_.Get(),
               receive_buffer->data(),
               static_cast<DWORD>(receive_buffer->size()),
               NULL,
               transfer->GetOverlapped()));
}

void HidConnectionWin::Write(uint8_t report_id,
                             scoped_refptr<net::IOBufferWithSize> buffer,
                             const HidConnection::IOCallback& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (device_info().output_report_size == 0) {
    // The device does not support output reports.
    callback.Run(false, 0);
    return;
  }

  // The Windows API always wants either a report ID (if supported) or
  // zero at the front of every output report.
  scoped_refptr<net::IOBufferWithSize> output_buffer(buffer);
  output_buffer = new net::IOBufferWithSize(buffer->size() + 1);
  output_buffer->data()[0] = report_id;
  memcpy(output_buffer->data() + 1, buffer->data(), buffer->size());

  scoped_refptr<PendingHidTransfer> transfer(
      new PendingHidTransfer(this, buffer, NULL, callback));
  transfers_.insert(transfer);
  transfer->TakeResultFromWindowsAPI(
      WriteFile(file_.Get(),
                output_buffer->data(),
                static_cast<DWORD>(output_buffer->size()),
                NULL,
                transfer->GetOverlapped()));
}

void HidConnectionWin::GetFeatureReport(
    uint8_t report_id,
    scoped_refptr<net::IOBufferWithSize> buffer,
    const IOCallback& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (device_info().feature_report_size == 0) {
    // The device does not support feature reports.
    callback.Run(false, 0);
    return;
  }

  if (buffer->size() < device_info().feature_report_size) {
    callback.Run(false, 0);
    return;
  }

  scoped_refptr<net::IOBufferWithSize> receive_buffer(buffer);
  if (!device_info().has_report_id)
    receive_buffer = new net::IOBufferWithSize(buffer->size() + 1);

  // The first byte of the destination buffer is the report ID being requested.
  receive_buffer->data()[0] = report_id;
  scoped_refptr<PendingHidTransfer> transfer(
      new PendingHidTransfer(this, buffer, receive_buffer, callback));
  transfers_.insert(transfer);
  transfer->TakeResultFromWindowsAPI(
      DeviceIoControl(file_.Get(),
                      IOCTL_HID_GET_FEATURE,
                      NULL,
                      0,
                      receive_buffer->data(),
                      static_cast<DWORD>(receive_buffer->size()),
                      NULL,
                      transfer->GetOverlapped()));
}

void HidConnectionWin::SendFeatureReport(
    uint8_t report_id,
    scoped_refptr<net::IOBufferWithSize> buffer,
    const IOCallback& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (device_info().feature_report_size == 0) {
    // The device does not support feature reports.
    callback.Run(false, 0);
    return;
  }

  if (buffer->size() < device_info().feature_report_size) {
    callback.Run(false, 0);
    return;
  }

  // The Windows API always wants either a report ID (if supported) or
  // zero at the front of every output report.
  scoped_refptr<net::IOBufferWithSize> output_buffer(buffer);
  output_buffer = new net::IOBufferWithSize(buffer->size() + 1);
  output_buffer->data()[0] = report_id;
  memcpy(output_buffer->data() + 1, buffer->data(), buffer->size());

  scoped_refptr<PendingHidTransfer> transfer(
      new PendingHidTransfer(this, buffer, NULL, callback));
  transfer->TakeResultFromWindowsAPI(
      DeviceIoControl(file_.Get(),
                      IOCTL_HID_SET_FEATURE,
                      output_buffer->data(),
                      static_cast<DWORD>(output_buffer->size()),
                      NULL,
                      0,
                      NULL,
                      transfer->GetOverlapped()));
}

void HidConnectionWin::OnTransferFinished(
    scoped_refptr<PendingHidTransfer> transfer) {
  DWORD bytes_transferred;
  transfers_.erase(transfer);
  if (GetOverlappedResult(
          file_, transfer->GetOverlapped(), &bytes_transferred, FALSE)) {
    if (bytes_transferred == 0)
      transfer->callback_.Run(true, 0);
    // If this is an input transfer and the receive buffer is not the same as
    // the target buffer, we need to copy the receive buffer into the target
    // buffer, discarding the first byte. This is because the target buffer's
    // owner is not expecting a report ID but Windows will always provide one.
    if (transfer->receive_buffer_ &&
        transfer->receive_buffer_ != transfer->target_buffer_) {
      // Move one byte forward.
      --bytes_transferred;
      memcpy(transfer->target_buffer_->data(),
             transfer->receive_buffer_->data() + 1,
             bytes_transferred);
    }
    transfer->callback_.Run(true, bytes_transferred);
  } else {
    transfer->callback_.Run(false, 0);
  }
}

void HidConnectionWin::OnTransferCanceled(
    scoped_refptr<PendingHidTransfer> transfer) {
  transfers_.erase(transfer);
  transfer->callback_.Run(false, 0);
}

}  // namespace device