forked from coolstar/crosecservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
469 lines (378 loc) · 9.11 KB
/
client.cpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <windows.h>
#include <hidsdi.h>
#include <setupapi.h>
#include <stdio.h>
#include <stdlib.h>
#include "croskbhid.h"
#if __GNUC__
#define __in
#define __in_ecount(x)
typedef void* PVOID;
typedef PVOID HDEVINFO;
WINHIDSDI BOOL WINAPI HidD_SetOutputReport(HANDLE, PVOID, ULONG);
#endif
typedef struct _croskbhid_client_t
{
HANDLE hKeyboard;
HANDLE hSettings;
} croskbhid_client_t;
//
// Function prototypes
//
HANDLE
SearchMatchingHwID(
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage
);
HANDLE
OpenDeviceInterface(
HDEVINFO HardwareDeviceInfo,
PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage
);
BOOLEAN
CheckIfOurDevice(
HANDLE file,
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage
);
BOOL
HidOutput(
BOOL useSetOutputReport,
HANDLE file,
PCHAR buffer,
ULONG bufferSize
);
//
// Copied this structure from hidport.h
//
typedef struct _HID_DEVICE_ATTRIBUTES {
ULONG Size;
//
// sizeof (struct _HID_DEVICE_ATTRIBUTES)
//
//
// Vendor ids of this hid device
//
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
USHORT Reserved[11];
} HID_DEVICE_ATTRIBUTES, * PHID_DEVICE_ATTRIBUTES;
static USHORT TpVendorID = 0;
USHORT getVendorID() {
return TpVendorID;
}
//
// Implementation
//
pcroskbhid_client croskbhid_alloc(void)
{
return (pcroskbhid_client)malloc(sizeof(croskbhid_client_t));
}
void croskbhid_free(pcroskbhid_client croskbhid)
{
free(croskbhid);
}
BOOL croskbhid_connect(pcroskbhid_client croskbhid)
{
//
// Find the HID devices
//
croskbhid->hKeyboard = SearchMatchingHwID(CROSKBHIDREMAPPER_VID, CROSKBHIDREMAPPER_PID, 0x000C, 0x0001);
if (croskbhid->hKeyboard == INVALID_HANDLE_VALUE || croskbhid->hKeyboard == NULL)
{
croskbhid_disconnect(croskbhid);
return FALSE;
}
//
// Set the buffer count to 10 on the setting HID
//
if (!HidD_SetNumInputBuffers(croskbhid->hKeyboard, 10))
{
printf("failed HidD_SetNumInputBuffers %d\n", GetLastError());
croskbhid_disconnect(croskbhid);
return FALSE;
}
return TRUE;
}
BOOL croskblight_connect(pcroskbhid_client croskbhid) {
croskbhid->hSettings = SearchMatchingHwID(CROSKBLIGHT_VID, CROSKBLIGHT_PID, 0xff00, 0x0001);
if (croskbhid->hSettings == INVALID_HANDLE_VALUE || croskbhid->hSettings == NULL)
{
croskbhid->hSettings = NULL;
return FALSE;
}
if (!HidD_SetNumInputBuffers(croskbhid->hSettings, 10))
{
printf("failed HidD_SetNumInputBuffers %d\n", GetLastError());
croskbhid_disconnect(croskbhid);
return FALSE;
}
return TRUE;
}
void croskblight_disconnect(pcroskbhid_client croskbhid)
{
if (croskbhid->hSettings != NULL)
CloseHandle(croskbhid->hSettings);
croskbhid->hSettings = NULL;
}
void croskbhid_disconnect(pcroskbhid_client croskbhid)
{
if (croskbhid->hKeyboard != NULL)
CloseHandle(croskbhid->hKeyboard);
croskbhid->hKeyboard = NULL;
if (croskbhid->hSettings != NULL)
CloseHandle(croskbhid->hSettings);
croskbhid->hSettings = NULL;
}
BOOL croskbhid_read_keyboard(pcroskbhid_client vmulti, CrosKBHIDRemapperMediaReport* pReport)
{
ULONG bytesRead;
//
// Read the report
//
if (!ReadFile(vmulti->hKeyboard, pReport, sizeof(CrosKBHIDRemapperMediaReport), &bytesRead, NULL))
{
return FALSE;
}
return TRUE;
}
BOOL croskbhid_write_keyboard(pcroskbhid_client croskbhid, CrosKBHIDRemapperMediaReport* pReport)
{
ULONG bytesWritten;
//
// Write the report
//
if (!WriteFile(croskbhid->hKeyboard, pReport, sizeof(CrosKBHIDRemapperMediaReport), &bytesWritten, NULL))
{
return FALSE;
}
return TRUE;
}
BOOL croskblight_read_message(pcroskbhid_client vmulti, CrosKBLightSettingsReport* pReport)
{
ULONG bytesRead;
//
// Read the report
//
if (!ReadFile(vmulti->hSettings, pReport, sizeof(CrosKBLightSettingsReport), &bytesRead, NULL))
{
return FALSE;
}
return TRUE;
}
BOOL croskblight_write_message(pcroskbhid_client croskbhid, CrosKBLightSettingsReport* pReport)
{
ULONG bytesWritten;
//
// Write the report
//
if (!WriteFile(croskbhid->hSettings, pReport, sizeof(CrosKBLightSettingsReport), &bytesWritten, NULL))
{
return FALSE;
}
return TRUE;
}
HANDLE
SearchMatchingHwID(
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage
)
{
HDEVINFO hardwareDeviceInfo;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
SP_DEVINFO_DATA devInfoData;
GUID hidguid;
int i;
HidD_GetHidGuid(&hidguid);
hardwareDeviceInfo =
SetupDiGetClassDevs((LPGUID)&hidguid,
NULL,
NULL, // Define no
(DIGCF_PRESENT |
DIGCF_INTERFACEDEVICE));
if (INVALID_HANDLE_VALUE == hardwareDeviceInfo)
{
printf("SetupDiGetClassDevs failed: %x\n", GetLastError());
return INVALID_HANDLE_VALUE;
}
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//
// Enumerate devices of this interface class
//
printf("\n....looking for our HID device (with UP=0x%x "
"and Usage=0x%x)\n", myUsagePage, myUsage);
for (i = 0; SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
0, // No care about specific PDOs
(LPGUID)&hidguid,
i, //
&deviceInterfaceData);
i++)
{
//
// Open the device interface and Check if it is our device
// by matching the Usage page and Usage from Hid_Caps.
// If this is our device then send the hid request.
//
HANDLE file = OpenDeviceInterface(hardwareDeviceInfo, &deviceInterfaceData, vendorID, productID, myUsagePage, myUsage);
if (file != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
return file;
}
//
//device was not found so loop around.
//
}
printf("Failure: Could not find our HID device \n");
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
return INVALID_HANDLE_VALUE;
}
HANDLE
OpenDeviceInterface(
HDEVINFO hardwareDeviceInfo,
PSP_DEVICE_INTERFACE_DATA deviceInterfaceData,
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage
)
{
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
DWORD predictedLength = 0;
DWORD requiredLength = 0;
HANDLE file = INVALID_HANDLE_VALUE;
SetupDiGetDeviceInterfaceDetail(
hardwareDeviceInfo,
deviceInterfaceData,
NULL, // probing so no output buffer yet
0, // probing so output buffer length of zero
&requiredLength,
NULL
); // not interested in the specific dev-node
predictedLength = requiredLength;
deviceInterfaceDetailData =
(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(predictedLength);
if (!deviceInterfaceDetailData)
{
printf("Error: OpenDeviceInterface: malloc failed\n");
goto cleanup;
}
deviceInterfaceDetailData->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(
hardwareDeviceInfo,
deviceInterfaceData,
deviceInterfaceDetailData,
predictedLength,
&requiredLength,
NULL))
{
printf("Error: SetupDiGetInterfaceDeviceDetail failed\n");
free(deviceInterfaceDetailData);
goto cleanup;
}
file = CreateFile(deviceInterfaceDetailData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file
if (INVALID_HANDLE_VALUE == file) {
printf("Error: CreateFile failed: %d\n", GetLastError());
goto cleanup;
}
if (CheckIfOurDevice(file, vendorID, productID, myUsagePage, myUsage)) {
goto cleanup;
}
CloseHandle(file);
file = INVALID_HANDLE_VALUE;
cleanup:
free(deviceInterfaceDetailData);
return file;
}
BOOLEAN
CheckIfOurDevice(
HANDLE file,
USHORT vendorID,
USHORT productID,
USAGE myUsagePage,
USAGE myUsage)
{
PHIDP_PREPARSED_DATA Ppd = NULL; // The opaque parser info describing this device
HIDD_ATTRIBUTES Attributes; // The Attributes of this hid device.
HIDP_CAPS Caps; // The Capabilities of this hid device.
BOOLEAN result = FALSE;
if (!HidD_GetPreparsedData(file, &Ppd))
{
printf("Error: HidD_GetPreparsedData failed \n");
goto cleanup;
}
if (!HidD_GetAttributes(file, &Attributes))
{
printf("Error: HidD_GetAttributes failed \n");
goto cleanup;
}
if (Attributes.VendorID == vendorID && Attributes.ProductID == productID)
{
TpVendorID = Attributes.VendorID;
if (!HidP_GetCaps(Ppd, &Caps))
{
printf("Error: HidP_GetCaps failed \n");
goto cleanup;
}
if ((Caps.UsagePage == myUsagePage) && (Caps.Usage == myUsage))
{
printf("Success: Found my device.. \n");
result = TRUE;
}
}
cleanup:
if (Ppd != NULL)
{
HidD_FreePreparsedData(Ppd);
}
return result;
}
BOOL
HidOutput(
BOOL useSetOutputReport,
HANDLE file,
PCHAR buffer,
ULONG bufferSize
)
{
ULONG bytesWritten;
if (useSetOutputReport)
{
//
// Send Hid report thru HidD_SetOutputReport API
//
if (!HidD_SetOutputReport(file, buffer, bufferSize))
{
printf("failed HidD_SetOutputReport %d\n", GetLastError());
return FALSE;
}
}
else
{
if (!WriteFile(file, buffer, bufferSize, &bytesWritten, NULL))
{
printf("failed WriteFile %d\n", GetLastError());
return FALSE;
}
}
return TRUE;
}