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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
testvhid.c
Environment:
user mode only
Author:
--*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <cfgmgr32.h>
#include <hidsdi.h>
#include "common.h"
#include <time.h>
//
// These are the default device attributes set in the driver
// which are used to identify the device.
//
#define HIDMINI_DEFAULT_PID 0xFEED
#define HIDMINI_DEFAULT_VID 0xDEED
//
// These are the device attributes returned by the mini driver in response
// to IOCTL_HID_GET_DEVICE_ATTRIBUTES.
//
#define HIDMINI_TEST_PID 0xDEEF
#define HIDMINI_TEST_VID 0xFEED
#define HIDMINI_TEST_VERSION 0x0505
USAGE g_MyUsagePage = 0xFF00;
USAGE g_MyUsage = 0x01;
#define USE_ALT_USAGE_PAGE "UseAltUsagePage"
//
// Function prototypes
//
BOOLEAN
GetFeature(
HANDLE file
);
BOOLEAN
SetFeature(
HANDLE file
);
BOOLEAN
GetInputReport(
HANDLE file
);
BOOLEAN
SetOutputReport(
HANDLE file
);
BOOLEAN
CheckIfOurDevice(
HANDLE file
);
BOOLEAN
ReadInputData(
_In_ HANDLE file
);
BOOLEAN
WriteOutputData(
_In_ HANDLE file
);
BOOLEAN
GetIndexedString(
HANDLE File
);
BOOLEAN
GetStrings(
HANDLE File
);
BOOLEAN
FindMatchingDevice(
_In_ LPGUID Guid,
_Out_ HANDLE* Handle
);
//
// Implementation
//
INT __cdecl
main(
_In_ ULONG argc,
_In_reads_(argc) PCHAR argv[]
)
{
GUID hidguid;
HANDLE file = INVALID_HANDLE_VALUE;
BOOLEAN found = FALSE;
BOOLEAN bSuccess = FALSE;
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
srand( (unsigned)time( NULL ) );
HidD_GetHidGuid(&hidguid);
found = FindMatchingDevice(&hidguid, &file);
if (found) {
printf("...sending control request to our device\n");
//
// Get/Set feature loopback
//
bSuccess = GetFeature(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = SetFeature(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = GetFeature(file);
if (bSuccess == FALSE) {
goto cleanup;
}
//
// Get/Set report loopback
//
bSuccess = GetInputReport(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = SetOutputReport(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = GetInputReport(file);
if (bSuccess == FALSE) {
goto cleanup;
}
//
// Read/Write report loopback
//
bSuccess = ReadInputData(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = WriteOutputData(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = ReadInputData(file);
if (bSuccess == FALSE) {
goto cleanup;
}
//
// Get Strings
//
bSuccess = GetIndexedString(file);
if (bSuccess == FALSE) {
goto cleanup;
}
bSuccess = GetStrings(file);
if (bSuccess == FALSE) {
goto cleanup;
}
}
else {
printf("Failure: Could not find our HID device \n");
}
cleanup:
if (found && bSuccess == FALSE) {
printf("****** Failure: one or more commands to device failed *******\n");
}
if (file != INVALID_HANDLE_VALUE) {
CloseHandle(file);
}
return (bSuccess ? 0 : 1);
}
BOOLEAN
FindMatchingDevice(
_In_ LPGUID InterfaceGuid,
_Out_ HANDLE* Handle
)
{
CONFIGRET cr = CR_SUCCESS;
PWSTR deviceInterfaceList = NULL;
ULONG deviceInterfaceListLength = 0;
PWSTR currentInterface;
BOOLEAN bRet = FALSE;
HANDLE devHandle = INVALID_HANDLE_VALUE;
if (NULL == Handle) {
printf("Error: Invalid device handle parameter\n");
return FALSE;
}
*Handle = INVALID_HANDLE_VALUE;
cr = CM_Get_Device_Interface_List_Size(
&deviceInterfaceListLength,
InterfaceGuid,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list size.\n", cr);
goto clean0;
}
if (deviceInterfaceListLength <= 1) {
bRet = FALSE;
printf("Error: No active device interfaces found.\n"
" Is the sample driver loaded?");
goto clean0;
}
deviceInterfaceList = (PWSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR));
if (deviceInterfaceList == NULL) {
printf("Error allocating memory for device interface list.\n");
goto clean0;
}
ZeroMemory(deviceInterfaceList, deviceInterfaceListLength * sizeof(WCHAR));
cr = CM_Get_Device_Interface_List(
InterfaceGuid,
NULL,
deviceInterfaceList,
deviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list.\n", cr);
goto clean0;
}
//
// Enumerate devices of this interface class
//
printf("\n....looking for our HID device (with UP=0x%04X "
"and Usage=0x%02X)\n", g_MyUsagePage, g_MyUsage);
for (currentInterface = deviceInterfaceList;
*currentInterface;
currentInterface += wcslen(currentInterface) + 1) {
devHandle = CreateFile(currentInterface,
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 == devHandle) {
printf("Warning: CreateFile failed: %d\n", GetLastError());
continue;
}
if (CheckIfOurDevice(devHandle)) {
bRet = TRUE;
*Handle = devHandle;
}
else {
CloseHandle(devHandle);
}
}
clean0:
if (deviceInterfaceList != NULL) {
free(deviceInterfaceList);
}
if (CR_SUCCESS != cr) {
bRet = FALSE;
}
return bRet;
}
BOOLEAN
CheckIfOurDevice(
HANDLE file)
{
PHIDP_PREPARSED_DATA Ppd; // The opaque parser info describing this device
HIDP_CAPS Caps; // The Capabilities of this hid device.
HIDD_ATTRIBUTES attr; // Device attributes
if (!HidD_GetAttributes(file, &attr))
{
printf("Error: HidD_GetAttributes failed \n");
return FALSE;
}
printf("Device Attributes - PID: 0x%x, VID: 0x%x \n", attr.ProductID, attr.VendorID);
if ((attr.VendorID != HIDMINI_DEFAULT_VID) || (attr.ProductID != HIDMINI_DEFAULT_PID))
{
printf("Device attributes doesn't match the sample \n");
return FALSE;
}
if (!HidD_GetPreparsedData (file, &Ppd))
{
printf("Error: HidD_GetPreparsedData failed \n");
return FALSE;
}
if (!HidP_GetCaps (Ppd, &Caps))
{
printf("Error: HidP_GetCaps failed \n");
HidD_FreePreparsedData (Ppd);
return FALSE;
}
if ((Caps.UsagePage == g_MyUsagePage) && (Caps.Usage == g_MyUsage)){
printf("Success: Found my device.. \n");
return TRUE;
}
return FALSE;
}
BOOLEAN
GetFeature(
HANDLE file
)
{
PMY_DEVICE_ATTRIBUTES myDevAttributes = NULL;
ULONG bufferSize;
PUCHAR buffer;
BOOLEAN bSuccess;
//
// Allocate memory equal to 1 byte for report ID + size of
// feature report. Buffer size for get feature should be atleast equal to
// the size of feature report.
//
bufferSize = FEATURE_REPORT_SIZE_CB + 1;
buffer = (PUCHAR) malloc (bufferSize);
if (!buffer )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(buffer, bufferSize);
//
// Fill the first byte with report ID of control collection
//
buffer[0] = CONTROL_COLLECTION_REPORT_ID;
//
// Send Hid control code thru HidD_GetFeature API
//
bSuccess = HidD_GetFeature(file, // HidDeviceObject,
buffer, // ReportBuffer,
bufferSize // ReportBufferLength
);
if (!bSuccess)
{
printf("failed HidD_GetFeature\n");
}
else
{
myDevAttributes = (PMY_DEVICE_ATTRIBUTES) (buffer + 1); // +1 to skip report id
printf("Received following feature attributes from device: \n"
" VendorID: 0x%x, \n"
" ProductID: 0x%x, \n"
" VersionNumber: 0x%x\n",
myDevAttributes->VendorID,
myDevAttributes->ProductID,
myDevAttributes->VersionNumber);
}
free(buffer);
return bSuccess;
}
BOOLEAN
SetFeature(
HANDLE file
)
{
PHIDMINI_CONTROL_INFO controlInfo = NULL;
PMY_DEVICE_ATTRIBUTES myDevAttributes = NULL;
ULONG bufferSize;
BOOLEAN bSuccess;
//
// Allocate memory equal to HIDMINI_CONTROL_INFO
//
bufferSize = sizeof(HIDMINI_CONTROL_INFO);
controlInfo = (PHIDMINI_CONTROL_INFO) malloc (bufferSize);
if (!controlInfo )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(controlInfo, bufferSize);
//
// Fill the control structure with the report ID of the control collection and
// the control code.
//
controlInfo->ReportId = CONTROL_COLLECTION_REPORT_ID;
controlInfo->ControlCode = HIDMINI_CONTROL_CODE_SET_ATTRIBUTES;
myDevAttributes = (PMY_DEVICE_ATTRIBUTES)&controlInfo->u.Attributes;
myDevAttributes->VendorID = HIDMINI_TEST_VID;
myDevAttributes->ProductID = HIDMINI_TEST_PID;
myDevAttributes->VersionNumber = HIDMINI_TEST_VERSION;
//
// Set feature
//
bSuccess = HidD_SetFeature(file, // HidDeviceObject,
controlInfo, // ReportBuffer,
bufferSize // ReportBufferLength
);
if (!bSuccess)
{
printf("failed HidD_SetFeature \n");
}
else
{
printf("Set following feature attributes on device: \n"
" VendorID: 0x%x, \n"
" ProductID: 0x%x, \n"
" VersionNumber: 0x%x\n",
myDevAttributes->VendorID,
myDevAttributes->ProductID,
myDevAttributes->VersionNumber);
}
free(controlInfo);
return bSuccess;
}
BOOLEAN
GetInputReport(
HANDLE file
)
{
ULONG bufferSize;
PUCHAR buffer;
BOOLEAN bSuccess;
//
// Allocate memory
//
bufferSize = sizeof(HIDMINI_INPUT_REPORT);
buffer = (PUCHAR) malloc (bufferSize);
if (!buffer )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(buffer, bufferSize);
//
// Fill the first byte with report ID of collection
//
buffer[0] = CONTROL_COLLECTION_REPORT_ID;
//
// Send Hid control code
//
bSuccess = HidD_GetInputReport(file, // HidDeviceObject,
buffer, // ReportBuffer,
bufferSize // ReportBufferLength
);
if (!bSuccess)
{
printf("failed HidD_GetInputReport\n");
}
else
{
printf("Received following data in input report: %d\n",
((PHIDMINI_INPUT_REPORT) buffer)->Data);
}
free(buffer);
return bSuccess;
}
BOOLEAN
SetOutputReport(
HANDLE file
)
{
ULONG bufferSize;
PHIDMINI_OUTPUT_REPORT buffer;
BOOLEAN bSuccess;
//
// Allocate memory
//
bufferSize = sizeof(HIDMINI_OUTPUT_REPORT);
buffer = (PHIDMINI_OUTPUT_REPORT) malloc (bufferSize);
if (!buffer )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(buffer, bufferSize);
//
// Fill the report
//
buffer->ReportId = CONTROL_COLLECTION_REPORT_ID;
buffer->Data = (UCHAR) (rand() % UCHAR_MAX);
//
// Send Hid control code
//
bSuccess = HidD_SetOutputReport(file, // HidDeviceObject,
buffer, // ReportBuffer,
bufferSize // ReportBufferLength
);
if (!bSuccess)
{
printf("failed HidD_SetOutputReport\n");
}
else
{
printf("Set following data in output report: %d\n",
((PHIDMINI_OUTPUT_REPORT) buffer)->Data);
}
free(buffer);
return bSuccess;
}
BOOLEAN
ReadInputData(
_In_ HANDLE file
)
{
PHIDMINI_INPUT_REPORT report;
ULONG bufferSize;
BOOL bSuccess;
DWORD bytesRead;
//
// Allocate memory
//
bufferSize = sizeof(HIDMINI_INPUT_REPORT);
report = (PHIDMINI_INPUT_REPORT) malloc (bufferSize);
if (!report )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(report, bufferSize);
report->ReportId = CONTROL_COLLECTION_REPORT_ID;
//
// get input data.
//
bSuccess = ReadFile(
file, // HANDLE hFile,
report, // LPVOID lpBuffer,
bufferSize, // DWORD nNumberOfBytesToRead,
&bytesRead, // LPDWORD lpNumberOfBytesRead,
NULL // LPOVERLAPPED lpOverlapped
);
if (!bSuccess)
{
printf("failed ReadFile \n");
}
else
{
printf("Read following byte from device: %d\n",
report->Data);
}
free(report);
return (BOOLEAN) bSuccess;
}
BOOLEAN
WriteOutputData(
_In_ HANDLE file
)
{
PHIDMINI_OUTPUT_REPORT outputReport;
ULONG outputReportSize;
BOOL bSuccess;
DWORD bytesWritten;
//
// Allocate memory for outtput report
//
outputReportSize = sizeof(HIDMINI_OUTPUT_REPORT);
outputReport = (PHIDMINI_OUTPUT_REPORT) malloc (outputReportSize);
if (!outputReport )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(outputReport, outputReportSize);
outputReport->ReportId = CONTROL_COLLECTION_REPORT_ID;
outputReport->Data = (UCHAR) (rand() % UCHAR_MAX);
//
// Wrute output data.
//
bSuccess = WriteFile(
file, // HANDLE hFile,
(PVOID) outputReport, // LPVOID lpBuffer,
outputReportSize, // DWORD nNumberOfBytesToRead,
&bytesWritten, // LPDWORD lpNumberOfBytesRead,
NULL // LPOVERLAPPED lpOverlapped
);
if (!bSuccess)
{
printf("failed WriteFile \n");
}
else
{
printf("Wrote following byte to device: %d\n",
outputReport->Data);
}
free(outputReport);
return (BOOLEAN) bSuccess;
}
BOOLEAN
GetIndexedString(
HANDLE File
)
{
BOOLEAN bSuccess;
BYTE* buffer;
ULONG bufferLength;
bufferLength = MAXIMUM_STRING_LENGTH;
buffer = malloc(bufferLength);
if (!buffer )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(buffer, bufferLength);
bSuccess = HidD_GetIndexedString(
File,
VHIDMINI_DEVICE_STRING_INDEX, // IN ULONG StringIndex,
(PVOID) buffer, //OUT PVOID Buffer,
bufferLength // IN ULONG BufferLength
) ;
if (!bSuccess)
{
printf("failed WriteFile \n");
}
else
{
printf("Indexed string: %S\n", (PWSTR) buffer);
}
free(buffer);
return bSuccess;
}
BOOLEAN
GetStrings(
HANDLE File
)
{
BOOLEAN bSuccess;
BYTE* buffer;
ULONG bufferLength;
bufferLength = MAXIMUM_STRING_LENGTH;
buffer = malloc(bufferLength);
if (!buffer )
{
printf("malloc failed\n");
return FALSE;
}
ZeroMemory(buffer, bufferLength);
bSuccess = HidD_GetProductString(
File,
(PVOID) buffer, //OUT PVOID Buffer,
bufferLength // IN ULONG BufferLength
);
if (!bSuccess)
{
printf("failed HidD_GetProductString \n");
goto exit;
}
else
{
printf("Product string: %S\n", (PWSTR) buffer);
}
ZeroMemory(buffer, bufferLength);
bSuccess = HidD_GetSerialNumberString(
File,
(PVOID) buffer, //OUT PVOID Buffer,
bufferLength // IN ULONG BufferLength
);
if (!bSuccess)
{
printf("failed HidD_GetSerialNumberString \n");
goto exit;
}
else
{
printf("Serial number string: %S\n", (PWSTR) buffer);
}
ZeroMemory(buffer, bufferLength);
bSuccess = HidD_GetManufacturerString(
File,
(PVOID) buffer, //OUT PVOID Buffer,
bufferLength // IN ULONG BufferLength
);
if (!bSuccess)
{
printf("failed HidD_GetManufacturerString \n");
goto exit;
}
else
{
printf("Manufacturer string: %S\n", (PWSTR) buffer);
}
exit:
free(buffer);
return bSuccess;
}
|