-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhidio.cpp
More file actions
350 lines (292 loc) · 9.24 KB
/
Copy pathhidio.cpp
File metadata and controls
350 lines (292 loc) · 9.24 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "hidio.h"
#include <cstdint>
#include <cassert>
#include <type_traits>
#include <memory>
#include <vector>
#include <string>
#include <functional>
//#define WITH_HIDAPI
#if !defined(_WIN32) || defined(WITH_HIDAPI)
#include <hidapi/hidapi.h>
#pragma comment(lib, "hidapi.lib")
namespace hidio
{
std::vector<device_info> enumerate_devices(uint16_t vendorId, uint16_t productId)
{
std::vector<device_info> result;
if (auto* device_list = ::hid_enumerate(vendorId, productId))
{
for (auto i = device_list; i; i = i->next)
{
device_info t{};
t.device_path = i->path;
t.manufacture_string = i->manufacturer_string;
t.product_string = i->product_string;
t.serial_number = i->serial_number;
result.push_back(t);
}
hid_free_enumeration(device_list);
}
return result;
}
std::unique_ptr<device> device::open(const char* device_path)
{
struct impl final : device
{
std::string device_name_{};
hid_device* device_{};
impl(const char* device_path) : device_name_(device_path), device_(hid_open_path(device_path)) { }
[[nodiscard]] bool is_open() const { return device_; }
[[nodiscard]] const char* device_path() const override { return device_name_.c_str(); }
[[nodiscard]] int write(const void* data, size_t length) override { return hid_write(device_, static_cast<const unsigned char*>(data), length); }
[[nodiscard]] int read(void* buffer, size_t length, int timeout_milliseconds) override { return hid_read_timeout(device_, static_cast<unsigned char*>(buffer), length, timeout_milliseconds); }
~impl() override { hid_close(device_); }
};
if (auto d = std::make_unique<impl>(device_path); d->is_open())
return std::move(d);
else
return nullptr;
}
void set_thread_priority_realtime()
{
}
}
#else
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <SetupAPI.h>
#include <hidsdi.h>
#pragma comment(lib, "hid.lib")
#pragma comment(lib, "setupapi.lib")
namespace hidio
{
std::vector<device_info> enumerate_devices(uint16_t vendorId, uint16_t productId)
{
std::vector<device_info> result;
constexpr GUID GUID_DEVINTERFACE_HID = {0x4D1E55B2L, 0xF16F, 0x11CF, {0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}};
auto dev_info = std::shared_ptr<std::remove_pointer_t<HDEVINFO>>(
SetupDiGetClassDevsA(&GUID_DEVINTERFACE_HID, nullptr, nullptr, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT),
&SetupDiDestroyDeviceInfoList);
SP_DEVICE_INTERFACE_DATA id{};
id.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD index = 0;
SetupDiEnumDeviceInterfaces(dev_info.get(), nullptr, &GUID_DEVINTERFACE_HID, index, &id);
index++)
{
DWORD size = 0;
// Get Device Path
uint8_t buffer[sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A) + 256]{};
auto detail_data = reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>(buffer);
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
if (!SetupDiGetDeviceInterfaceDetailA(dev_info.get(), &id, detail_data, sizeof(buffer), &size, nullptr))
{
continue;
}
// Open Device
auto device_handle = std::shared_ptr<std::remove_pointer_t<HANDLE>>(
CreateFileA(
detail_data->DevicePath,
0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0),
&CloseHandle);
if (device_handle.get() == INVALID_HANDLE_VALUE)
{
continue;
}
// Filter by vid/pid.
HIDD_ATTRIBUTES attr{};
if (!HidD_GetAttributes(device_handle.get(), &attr)
|| attr.VendorID != vendorId
|| attr.ProductID != productId)
{
continue;
}
// Collect Device Info
device_info t{};
t.device_path = detail_data->DevicePath;
if (wchar_t buf[256]{};
HidD_GetManufacturerString(device_handle.get(), buf, sizeof(buf)))
{
t.manufacture_string = buf;
}
if (wchar_t buf[256]{};
HidD_GetProductString(device_handle.get(), buf, sizeof(buf)))
{
t.product_string = buf;
}
if (wchar_t buf[256]{};
HidD_GetSerialNumberString(device_handle.get(), buf, sizeof(buf)))
{
t.serial_number = buf;
}
result.push_back(t);
}
return result;
}
std::unique_ptr<device> device::open(const char* device_path)
{
struct impl final : device
{
using win32_handle = std::unique_ptr<std::remove_pointer_t<HANDLE>, decltype(&CloseHandle)>;
std::string device_path_{};
win32_handle device_{nullptr, &CloseHandle};
OVERLAPPED reading_overlapped_{};
bool previous_read_is_incomplete_ = false;
win32_handle read_operation_completed_{CreateEventA(nullptr, true, false, nullptr), &CloseHandle};
std::vector<uint8_t> inputBuffer_{};
std::vector<uint8_t> outputBuffer_{};
static win32_handle open_device(const char* device_path, size_t* input_report_length, size_t* output_report_length, size_t* feature_report_length)
{
auto dev = win32_handle(
CreateFileA(
device_path,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL), &CloseHandle);
if (dev.get() == INVALID_HANDLE_VALUE) return {nullptr, CloseHandle};
if (!HidD_SetNumInputBuffers(dev.get(), 64)) return {nullptr, CloseHandle};
// Collect device caps
if (auto data = std::unique_ptr<std::remove_pointer_t<PHIDP_PREPARSED_DATA>, decltype(&HidD_FreePreparsedData)>(
[&]
{
PHIDP_PREPARSED_DATA t = nullptr;
return HidD_GetPreparsedData(dev.get(), &t) ? t : nullptr;
}(), &HidD_FreePreparsedData))
{
if (HIDP_CAPS caps{}; HidP_GetCaps(data.get(), &caps) == HIDP_STATUS_SUCCESS)
{
if (input_report_length) *input_report_length = caps.InputReportByteLength;
if (output_report_length) *output_report_length = caps.OutputReportByteLength;
if (feature_report_length) *feature_report_length = caps.FeatureReportByteLength;
}
else return {nullptr, CloseHandle};
}
else return {nullptr, CloseHandle};
return std::move(dev);
}
impl(const char* device_path)
: device_path_(device_path)
{
size_t input_report_length{};
size_t output_report_length{};
size_t feature_report_length{};
device_ = open_device(device_path, &input_report_length, &output_report_length, &feature_report_length);
inputBuffer_.resize(input_report_length);
outputBuffer_.resize(output_report_length);
}
[[nodiscard]] bool is_open() const
{
return device_.get() != nullptr;
}
[[nodiscard]] const char* device_path() const override
{
return device_path_.c_str();
}
void reset_io()
{
CancelIo(device_.get());
previous_read_is_incomplete_ = false;
device_.reset();
device_ = open_device(device_path(), nullptr, nullptr, nullptr);
}
[[nodiscard]] int write(const void* data, size_t length) override
{
// if data is shorter than minimum length, zero-extend it.
if (length < outputBuffer_.size())
{
memcpy(outputBuffer_.data(), data, length);
memset(outputBuffer_.data() + length, 0, outputBuffer_.size() - length);
data = outputBuffer_.data();
length = outputBuffer_.size();
}
OVERLAPPED writing_overlapped_{};
if (BOOL r = WriteFile(device_.get(), data, static_cast<DWORD>(length), nullptr, &writing_overlapped_);
r || GetLastError() == ERROR_IO_PENDING)
{
/* OK */
}
else
{
reset_io();
return -1; // failed.
}
DWORD wrote{};
if (GetOverlappedResult(device_.get(), &writing_overlapped_, &wrote, true))
{
return static_cast<int>(wrote); // complete
}
reset_io();
return -1; // failed.
}
[[nodiscard]] int read(void* buffer, size_t length, int timeout_milliseconds) override
{
if (!previous_read_is_incomplete_)
{
previous_read_is_incomplete_ = true;
memset(inputBuffer_.data(), 0, inputBuffer_.size());
ResetEvent(read_operation_completed_.get());
reading_overlapped_ = OVERLAPPED{};
reading_overlapped_.hEvent = read_operation_completed_.get();
if (BOOL r = ReadFile(device_.get(), inputBuffer_.data(), static_cast<DWORD>(inputBuffer_.size()), nullptr, &reading_overlapped_);
r || GetLastError() == ERROR_IO_PENDING)
{
/* OK */
}
else
{
reset_io();
return -1; // failed.
}
}
// Wait reading operation complete
if (auto wait_result = WaitForSingleObject(
read_operation_completed_.get(),
timeout_milliseconds >= 0 ? timeout_milliseconds : INFINITE);
wait_result == WAIT_OBJECT_0)
{
previous_read_is_incomplete_ = false;
DWORD read{};
if (GetOverlappedResult(device_.get(), &reading_overlapped_, &read, true))
{
const uint8_t* src = inputBuffer_.data();
if (*src == 0x00)
{
src++;
read--;
}
if (read <= length)
{
memcpy(buffer, src, read);
return static_cast<int>(read); // complete
}
}
}
else if (wait_result == WAIT_TIMEOUT)
{
return 0; // reading operation is continued in background
}
reset_io();
return -1; // failed
}
~impl() override
{
reset_io();
device_.reset();
}
};
if (auto d = std::make_unique<impl>(device_path); d->is_open())
return {std::move(d)};
else
return nullptr;
}
void set_thread_priority_to_realtime()
{
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
}
}
#endif