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
|
// Copyright 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_service_win.h"
#include <cstdlib>
#include "base/files/file.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h"
#include "device/hid/hid_connection_win.h"
#include "device/hid/hid_device_info.h"
#include "net/base/io_buffer.h"
#if defined(OS_WIN)
#define INITGUID
#include <windows.h>
#include <hidclass.h>
extern "C" {
#include <hidsdi.h>
#include <hidpi.h>
}
#include <setupapi.h>
#include <winioctl.h>
#include "base/win/scoped_handle.h"
#endif // defined(OS_WIN)
// Setup API is required to enumerate HID devices.
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")
namespace device {
namespace {
const char kHIDClass[] = "HIDClass";
} // namespace
HidServiceWin::HidServiceWin() {
Enumerate();
}
HidServiceWin::~HidServiceWin() {}
void HidServiceWin::Enumerate() {
BOOL res;
HDEVINFO device_info_set;
SP_DEVINFO_DATA devinfo_data;
SP_DEVICE_INTERFACE_DATA device_interface_data;
memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA));
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
device_info_set = SetupDiGetClassDevs(
&GUID_DEVINTERFACE_HID,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (device_info_set == INVALID_HANDLE_VALUE)
return;
for (int device_index = 0;
SetupDiEnumDeviceInterfaces(device_info_set,
NULL,
&GUID_DEVINTERFACE_HID,
device_index,
&device_interface_data);
++device_index) {
DWORD required_size = 0;
// Determime the required size of detail struct.
SetupDiGetDeviceInterfaceDetailA(device_info_set,
&device_interface_data,
NULL,
0,
&required_size,
NULL);
scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA_A, base::FreeDeleter>
device_interface_detail_data(
static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>(
malloc(required_size)));
device_interface_detail_data->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
// Get the detailed data for this device.
res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
&device_interface_data,
device_interface_detail_data.get(),
required_size,
NULL,
NULL);
if (!res)
continue;
// Enumerate device info. Looking for Setup Class "HIDClass".
for (DWORD i = 0;
SetupDiEnumDeviceInfo(device_info_set, i, &devinfo_data);
i++) {
char class_name[256] = {0};
res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
&devinfo_data,
SPDRP_CLASS,
NULL,
(PBYTE) class_name,
sizeof(class_name) - 1,
NULL);
if (!res)
break;
if (memcmp(class_name, kHIDClass, sizeof(kHIDClass)) == 0) {
char driver_name[256] = {0};
// Get bounded driver.
res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
&devinfo_data,
SPDRP_DRIVER,
NULL,
(PBYTE) driver_name,
sizeof(driver_name) - 1,
NULL);
if (res) {
// Found the driver.
break;
}
}
}
if (!res)
continue;
PlatformAddDevice(device_interface_detail_data->DevicePath);
}
}
void HidServiceWin::PlatformAddDevice(const std::string& device_path) {
HidDeviceInfo device_info;
device_info.device_id = device_path;
// Try to open the device.
base::win::ScopedHandle device_handle(
CreateFileA(device_path.c_str(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0));
if (!device_handle.IsValid() &&
GetLastError() == base::File::FILE_ERROR_ACCESS_DENIED) {
base::win::ScopedHandle device_handle(
CreateFileA(device_path.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0));
if (!device_handle.IsValid())
return;
}
// Get VID/PID pair.
HIDD_ATTRIBUTES attrib = {0};
attrib.Size = sizeof(HIDD_ATTRIBUTES);
if (!HidD_GetAttributes(device_handle.Get(), &attrib))
return;
device_info.vendor_id = attrib.VendorID;
device_info.product_id = attrib.ProductID;
for (ULONG i = 32;
HidD_SetNumInputBuffers(device_handle.Get(), i);
i <<= 1);
// Get usage and usage page (optional).
PHIDP_PREPARSED_DATA preparsed_data;
if (HidD_GetPreparsedData(device_handle.Get(), &preparsed_data) &&
preparsed_data) {
HIDP_CAPS capabilities;
if (HidP_GetCaps(preparsed_data, &capabilities) == HIDP_STATUS_SUCCESS) {
device_info.input_report_size = capabilities.InputReportByteLength;
device_info.output_report_size = capabilities.OutputReportByteLength;
device_info.feature_report_size = capabilities.FeatureReportByteLength;
device_info.usages.push_back(HidUsageAndPage(
capabilities.Usage,
static_cast<HidUsageAndPage::Page>(capabilities.UsagePage)));
}
// Detect if the device supports report ids.
if (capabilities.NumberInputValueCaps > 0) {
scoped_ptr<HIDP_VALUE_CAPS[]> value_caps(
new HIDP_VALUE_CAPS[capabilities.NumberInputValueCaps]);
USHORT value_caps_length = capabilities.NumberInputValueCaps;
if (HidP_GetValueCaps(HidP_Input, &value_caps[0], &value_caps_length,
preparsed_data) == HIDP_STATUS_SUCCESS) {
device_info.has_report_id = (value_caps[0].ReportID != 0);
}
}
if (!device_info.has_report_id && capabilities.NumberInputButtonCaps > 0)
{
scoped_ptr<HIDP_BUTTON_CAPS[]> button_caps(
new HIDP_BUTTON_CAPS[capabilities.NumberInputButtonCaps]);
USHORT button_caps_length = capabilities.NumberInputButtonCaps;
if (HidP_GetButtonCaps(HidP_Input,
&button_caps[0],
&button_caps_length,
preparsed_data) == HIDP_STATUS_SUCCESS) {
device_info.has_report_id = (button_caps[0].ReportID != 0);
}
}
HidD_FreePreparsedData(preparsed_data);
}
AddDevice(device_info);
}
void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) {
RemoveDevice(device_path);
}
void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) {
Enumerate();
HidService::GetDevices(devices);
}
scoped_refptr<HidConnection> HidServiceWin::Connect(
const HidDeviceId& device_id) {
HidDeviceInfo device_info;
if (!GetDeviceInfo(device_id, &device_info))
return NULL;
scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info));
if (!connection->available()) {
PLOG(ERROR) << "Failed to open device.";
return NULL;
}
return connection;
}
} // namespace device
|