forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhclient.c
3932 lines (3235 loc) · 124 KB
/
hclient.c
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft 1998, All Rights Reserved
Module Name:
hclient.c
Abstract:
This module contains the code for handling HClient's main dialog box and
for performing/calling the appropriate other routines.
Environment:
User mode
Revision History:
Nov-97 : Created
--*/
#define __HCLIENT_C__
#define LOG_FILE_NAME NULL
//****************************************************************************
// HClient include files
//****************************************************************************
#include <windows.h>
#include <stdlib.h>
#include <wtypes.h>
#include <math.h>
#include <assert.h>
#include <dbt.h>
#include <Shellapi.h>
#include <fcntl.h>
#include <io.h>
#include "hidsdi.h"
#include "hid.h"
#include "resource.h"
#include "hclient.h"
#include "buffers.h"
#include "ecdisp.h"
#include "list.h"
#include <strsafe.h>
#pragma warning(disable:4057) // indirection to slightly different base types
#pragma warning(disable:4127) // conditional expression in constant //while (1)
#pragma warning(disable:4702) // unreachable code //ExitThead
#pragma warning(disable:28146) // Warning is meant for kernel mode drivers
#pragma prefast(disable:17001, "By design, sample files are not internationalized.")
#pragma prefast(disable:38030, "By design, sample files are not internationalized.")
//****************************************************************************
// Local display macro definitions
//****************************************************************************
#define INPUT_BUTTON 1
#define INPUT_VALUE 2
#define OUTPUT_BUTTON 3
#define OUTPUT_VALUE 4
#define FEATURE_BUTTON 5
#define FEATURE_VALUE 6
#define HID_CAPS 7
#define DEVICE_ATTRIBUTES 8
#define MAX_LB_ITEMS 200
#define MAX_WRITE_ELEMENTS 100
#define MAX_OUTPUT_ELEMENTS 50
#define CONTROL_COUNT 9
#define MAX_LABEL 128
#define MAX_VALUE 128
#define SMALL_BUFF 128
//****************************************************************************
// Macro definition to get device block from the main dialog box procedure
//****************************************************************************
#define GET_CURRENT_DEVICE(hDlg, pDevice) \
{ \
pDevice = NULL; \
iIndex = (INT) SendDlgItemMessage(hDlg, \
IDC_DEVICES, \
CB_GETCURSEL, \
0, \
0); \
if (CB_ERR != iIndex) { \
pDevice = (PHID_DEVICE) SendDlgItemMessage(hDlg, \
IDC_DEVICES, \
CB_GETITEMDATA, \
iIndex, \
0); \
} \
}
//****************************************************************************
// Data types local to the HClient display routines
//****************************************************************************
typedef struct rWriteDataStruct_type
{
char szLabel[MAX_LABEL];
char szValue[MAX_VALUE];
} rWriteDataStruct, *prWriteDataStruct;
typedef struct rGetWriteDataParams_type
{
prWriteDataStruct prItems;
int iCount;
} rGetWriteDataParams, *prGetWriteDataParams;
typedef struct _DEVICE_LIST_NODE
{
LIST_NODE_HDR Hdr;
HDEVNOTIFY NotificationHandle;
HID_DEVICE HidDeviceInfo;
BOOL DeviceOpened;
} DEVICE_LIST_NODE, *PDEVICE_LIST_NODE;
//****************************************************************************
// Global program variables
//****************************************************************************
//
// Pointers to the HID.DLL functions that were added into the Win98 OSR and
// Windows 2000 but we're not included in the original implementation of
// HID.DLL in Windows 98. By getting pointers to these functions instead of
// statically linking with them, we can avoid the link error that would
// occur when this runs on Windows 98. The typedefs to make this easier to
// declare are also included below.
//
PGETEXTATTRIB __encoded_pointer pfnHidP_GetExtendedAttributes = NULL;
PINITREPORT __encoded_pointer pfnHidP_InitializeReportForID = NULL;
//****************************************************************************
// Global module variables
//****************************************************************************
static HINSTANCE hGInstance; //global application instance handle
static HANDLE HIDDLLModuleHandle = NULL;
//
// Variables for handling the two different types of devices that can be loaded
// into the system. PhysicalDeviceList contains all the actual HID devices
// attached via the USB bus.
//
static LIST PhysicalDeviceList;
//****************************************************************************
// Local data routine declarations
//****************************************************************************
VOID
vReadDataFromControls(
HWND hDlg,
prWriteDataStruct prData,
int iOffset,
int iCount
);
INT_PTR CALLBACK
bGetDataDlgProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
);
INT_PTR CALLBACK
bMainDlgProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
);
INT_PTR CALLBACK
bFeatureDlgProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
);
INT_PTR CALLBACK
bReadDlgProc(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam
);
VOID
vLoadItemTypes(
HWND hItemTypes
);
BOOL
bGetData(
prWriteDataStruct,
int iCount,
HWND hParent,
_In_ LPSTR pszDialogName
);
VOID
vLoadDevices(
HWND hDeviceCombo
);
VOID
vFreeDeviceList(
PHID_DEVICE DeviceList,
ULONG nDevices
);
VOID
vDisplayInputButtons(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vDisplayInputValues(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vDisplayOutputButtons(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vDisplayOutputValues(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vDisplayFeatureButtons(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vDisplayFeatureValues(
PHID_DEVICE pDevice,
HWND hControl
);
VOID
vWriteDataToControls(
HWND hDlg,
prWriteDataStruct prData,
int iOffset,
int iCount
);
int
iPrepareDataFields(
PHID_DATA pData,
ULONG ulDataLength,
rWriteDataStruct rWriteData[],
int iMaxElements
);
BOOL
bParseData(
PHID_DATA pData,
rWriteDataStruct rWriteData[],
INT iCount,
INT *piErrorLine
);
BOOL
bSetButtonUsages(
PHID_DATA pCap,
_In_ LPSTR pszInputString
);
VOID
BuildReportIDList(
IN PHIDP_BUTTON_CAPS phidButtonCaps,
IN USHORT nButtonCaps,
IN PHIDP_VALUE_CAPS phidValueCaps,
IN USHORT nValueCaps,
OUT UCHAR **ppReportIDList,
OUT INT *nReportIDs
);
VOID
ReportToString(
PHID_DATA pData,
_Inout_updates_bytes_(iBuffSize) LPSTR szBuff,
UINT iBuffSize
);
BOOL
RegisterHidDevice(
IN HWND WindowHandle,
IN PDEVICE_LIST_NODE DeviceNode
);
VOID
DestroyDeviceListCallback(
IN PLIST_NODE_HDR ListNode
);
//****************************************************************************
// Function Definitions
//****************************************************************************
void
DumpDeviceAttr(
_In_ PHIDD_ATTRIBUTES attr)
{
printf("Vendor ID: 0x%x\n", attr->VendorID);
printf("Product ID: 0x%x\n", attr->ProductID);
printf("Version Number 0x%x\n", attr->VersionNumber);
printf("\n");
}
void
DumpButtonInfo(
_In_ PHIDP_BUTTON_CAPS pButton)
{
ASSERT(pButton);
printf("Report ID: 0x%x\n", pButton->ReportID);
printf("Usage Page: 0x%x\n", pButton->UsagePage);
printf("Alias: %s\n", pButton -> IsAlias ? "TRUE" : "FALSE");
printf("Link Collection: %hu\n", pButton -> LinkCollection);
printf("Link Usage Page: 0x%x\n", pButton -> LinkUsagePage);
printf("Link Usage: 0x%x\n", pButton -> LinkUsage);
if (pButton->IsRange)
{
printf("Usage Min: 0x%x, Usage Max: 0x%x\n",
pButton->Range.UsageMin,
pButton->Range.UsageMax);
}
else
{
printf("Usage: 0x%x\n",pButton->NotRange.Usage);
}
if (pButton->IsRange)
{
printf("Data Index Min: 0x%x, Data Index Max: 0x%x\n",
pButton->Range.DataIndexMin,
pButton->Range.DataIndexMax);
}
else
{
printf("DataIndex: 0x%x\n",pButton->NotRange.DataIndex);
}
if (pButton->IsStringRange)
{
printf("String Min: 0x%x, String Max: 0x%x\n",
pButton->Range.StringMin,
pButton->Range.StringMax);
}
else
{
printf("String Index: 0x%x\n",pButton->NotRange.StringIndex);
}
if (pButton->IsDesignatorRange)
{
printf("Designator Min: 0x%x, Designator Max: 0x%x\n",
pButton->Range.DesignatorMin,
pButton->Range.DesignatorMax);
}
else
{
printf("Designator Index: 0x%x\n",
pButton->NotRange.DesignatorIndex);
}
printf("Absolute: %s\n",(pButton->IsAbsolute)?"Yes":"No");
printf("\n");
}
void
DumpValueInfo(
_In_ PHIDP_VALUE_CAPS pValue)
{
ASSERT(pValue);
printf("Report ID 0x%x\n", pValue->ReportID);
printf("Usage Page: 0x%x\n", pValue->UsagePage);
printf("Bit size: 0x%x\n", pValue->BitSize);
printf("Report Count: 0x%x\n", pValue->ReportCount);
printf("Unit Exponent: 0x%x\n", pValue->UnitsExp);
printf("Has Null: 0x%x\n", pValue->HasNull);
printf("Alias: %s\n", pValue->IsAlias ? "TRUE" : "FALSE");
if (pValue->IsRange)
{
printf("Usage Min: 0x%x, Usage Max 0x%x\n",
pValue->Range.UsageMin,
pValue->Range.UsageMax);
}
else
{
printf("Usage: 0x%x\n", pValue -> NotRange.Usage);
}
if (pValue->IsRange)
{
printf("Data Index Min: 0x%x, Data Index Max: 0x%x\n",
pValue->Range.DataIndexMin,
pValue->Range.DataIndexMax);
}
else
{
printf("DataIndex: 0x%x\n", pValue->NotRange.DataIndex);
}
printf("Physical Minimum: %d, Physical Maximum: %d\n",
pValue->PhysicalMin,
pValue->PhysicalMax);
printf("Logical Minimum: 0x%x, Logical Maximum: 0x%x\n",
pValue->LogicalMin,
pValue->LogicalMax);
if (pValue->IsStringRange)
{
printf("String Min: 0x%x String Max 0x%x\n",
pValue->Range.StringMin,
pValue->Range.StringMax);
}
else
{
printf("String Index: 0x%x\n",pValue->NotRange.StringIndex);
}
if (pValue->IsDesignatorRange)
{
printf("Designator Minimum: 0x%x, Max: 0x%x\n",
pValue->Range.DesignatorMin,
pValue->Range.DesignatorMax);
}
else
{
printf("Designator Index: 0x%x\n",pValue->NotRange.DesignatorIndex);
}
printf("Absolute: %s\n", pValue->IsAbsolute? "Yes" : "No");
printf("\n");
}
BOOL
CLM_AttachConsole()
{
HANDLE stdOut = INVALID_HANDLE_VALUE;
int osfStdOut = -1;
FILE *cStdOut = NULL;
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE != stdOut)
{
osfStdOut = _open_osfhandle((intptr_t)stdOut, _O_TEXT);
if (-1 != osfStdOut)
{
cStdOut = _fdopen(osfStdOut, "w");
}
}
}
if (NULL == cStdOut)
{
MessageBox(NULL, "Failed attaching the console", NULL, MB_OK);
return FALSE;
}
else
{
*stdout = *cStdOut;
printf("\n");
return TRUE;
}
}
void
CLM_ShowDeviceInfo(
_In_ PHID_DEVICE pDevice)
{
ULONG iIndex;
// Device Attributes
printf("Device Attributes:\n");
printf("------------------\n");
DumpDeviceAttr(&pDevice->Attributes);
// HID CAPS
printf("HID CAPS:\n");
printf("---------\n");
printf("Usage Page: 0x%x\n", pDevice->Caps.UsagePage);
printf("Input report byte length: %d\n", pDevice->Caps.InputReportByteLength);
printf("Output report byte length: %d\n", pDevice->Caps.OutputReportByteLength);
printf("Feature report byte length: %d\n", pDevice->Caps.FeatureReportByteLength);
printf("Number of collection nodes: %d\n", pDevice->Caps.NumberLinkCollectionNodes);
printf("\n");
// Input Buttons
if (0 == pDevice->Caps.NumberInputButtonCaps)
{
printf("No input buttons\n");
printf("----------------\n");
printf("\n");
}
else {
for (iIndex = 0; iIndex < pDevice->Caps.NumberInputButtonCaps; iIndex ++)
{
PHIDP_BUTTON_CAPS pButton;
printf("Input Button #%d:\n", iIndex+1);
printf("----------------\n");
pButton = &pDevice->InputButtonCaps[iIndex];
DumpButtonInfo(pButton);
}
}
// Output Buttons
if (0 == pDevice->Caps.NumberOutputButtonCaps)
{
printf("No output buttons\n");
printf("-----------------\n");
printf("\n");
}
else
{
for (iIndex = 0; iIndex < pDevice->Caps.NumberOutputButtonCaps; iIndex ++)
{
PHIDP_BUTTON_CAPS pButton;
printf("Output Button #%d:\n", iIndex+1);
printf("-----------------\n");
pButton = &pDevice->OutputButtonCaps[iIndex];
DumpButtonInfo(pButton);
}
}
// Feature Buttons
if (0 == pDevice->Caps.NumberFeatureButtonCaps)
{
printf("No feature buttons\n");
printf("------------------\n");
printf("\n");
}
else
{
for (iIndex = 0; iIndex < pDevice->Caps.NumberFeatureButtonCaps; iIndex ++)
{
PHIDP_BUTTON_CAPS pButton;
printf("Feature Button #%d:\n", iIndex+1);
printf("------------------\n");
pButton = &pDevice->FeatureButtonCaps[iIndex];
DumpButtonInfo(pButton);
}
}
// Input Values
if (0 == pDevice->Caps.NumberInputValueCaps)
{
printf("No input values\n");
printf("---------------\n");
printf("\n");
}
else
{
for (iIndex = 0; iIndex < pDevice->Caps.NumberInputValueCaps; iIndex ++)
{
PHIDP_VALUE_CAPS pValue;
printf("Input Value #%d:\n", iIndex+1);
printf("---------------\n");
pValue = &pDevice->InputValueCaps[iIndex];
DumpValueInfo(pValue);
}
}
// Output Values
if (0 == pDevice->Caps.NumberOutputValueCaps)
{
printf("No output values\n");
printf("----------------\n");
printf("\n");
}
else
{
for (iIndex = 0; iIndex < pDevice->Caps.NumberOutputValueCaps; iIndex ++)
{
PHIDP_VALUE_CAPS pValue;
printf("Output Value #%d:\n", iIndex+1);
printf("----------------\n");
pValue = &pDevice->OutputValueCaps[iIndex];
DumpValueInfo(pValue);
}
}
// Feature Values
if (0 == pDevice->Caps.NumberFeatureValueCaps)
{
printf("No feature values\n");
printf("-----------------\n");
printf("\n");
}
else
{
for (iIndex = 0; iIndex < pDevice->Caps.NumberFeatureValueCaps; iIndex ++)
{
PHIDP_VALUE_CAPS pValue;
printf("Feature Value #%d:\n", iIndex+1);
printf("-----------------\n");
pValue = &pDevice->FeatureValueCaps[iIndex];
DumpValueInfo(pValue);
}
}
}
void
CLM_PrintInputReport(
_In_ PHID_DEVICE pDevice
)
{
ULONG i;
CHAR szTempBuff[1024] = {0};
printf("-------------------------------\n");
printf("Raw Report Data: ");
for (i = 0; i < pDevice->Caps.InputReportByteLength; i++) {
printf("%02X ", (BYTE)pDevice->InputReportBuffer[i]);
}
printf("\n");
if (FALSE == UnpackReport (pDevice->InputReportBuffer,
pDevice->Caps.InputReportByteLength,
HidP_Input,
pDevice->InputData,
pDevice->InputDataLength,
pDevice->Ppd))
{
printf("Failed parsing the report data\n");
}
else {
printf("Parsed Data:\n");
for (i = 0; i< pDevice->InputDataLength; i++)
{
ReportToString(&pDevice->InputData[i], szTempBuff, sizeof(szTempBuff));
printf("HID_DATA[%d]: %s\n", i, szTempBuff);
}
}
printf("-------------------------------\n");
}
void
CLM_SyncRead(
_In_ PHID_DEVICE pDevice,
_In_ BYTE reportID,
_In_ ULONG msecToSleep,
_In_ ULONG numReads)
{
HID_DEVICE syncDevice;
ULONG numReadsDone = 0;
RtlZeroMemory(&syncDevice, sizeof(syncDevice));
if (!OpenHidDevice(pDevice->DevicePath,
TRUE,
FALSE,
FALSE,
FALSE,
&syncDevice))
{
printf("Failed opening the device for synchronous read.\n");
goto Done;
}
printf("Synchronous read started...\n");
while (INFINITE_READS == numReads || numReadsDone < numReads)
{
syncDevice.InputReportBuffer[0] = reportID;
if (FALSE == HidD_GetInputReport(syncDevice.HidDevice,
syncDevice.InputReportBuffer,
syncDevice.Caps.InputReportByteLength ))
{
printf("HidD_GetInputReport() failed. Error: 0x%X\n", GetLastError());
break;
}
numReadsDone++;
printf("Read #%d:\n",numReadsDone);
CLM_PrintInputReport(&syncDevice);
if (msecToSleep > 0)
{
Sleep(msecToSleep);
}
}
printf("Synchronous read stopped.\n");
Done:
CloseHidDevice(&syncDevice);
}
void
CLM_AsyncRead(
_In_ PHID_DEVICE pDevice,
_In_ ULONG numReads)
{
HID_DEVICE asyncDevice;
READ_THREAD_CONTEXT readContext;
HANDLE readThread;
if (!OpenHidDevice(pDevice->DevicePath,
TRUE,
FALSE,
FALSE,
FALSE,
&asyncDevice))
{
printf("Failed opening the device for asynchronous read.\n");
return;
}
readContext.HidDevice = &asyncDevice;
readContext.TerminateThread = FALSE;
readContext.NumberOfReads = numReads;
readContext.DisplayEvent = NULL;
readContext.DisplayWindow = NULL;
readThread = CreateThread( NULL,
0,
AsynchReadThreadProc,
&readContext,
CREATE_SUSPENDED,
NULL);
if (NULL == readThread)
{
printf("Failed creating the thread for asynchronous read. Error: 0x%x",
GetLastError());
}
else {
printf("Asychronous read started.\n");
ResumeThread(readThread);
WaitForSingleObject(readThread, INFINITE);
printf("Asychronous read stopped.\n");
}
if (readThread != NULL)
{
CloseHandle(readThread);
}
CloseHidDevice(&asyncDevice);
}
void
RunInCommandLineMode(
_In_reads_(nArgs) LPWSTR *szArgList,
_In_ int nArgs)
{
DWORD nArgCmd = 0;
BOOL bPrintHelp = FALSE;
BOOL bConsoleAttached = FALSE;
PHID_DEVICE deviceList = NULL;
ULONG numDevices = 0;
ULONG nDevice = 0;
ULONG numReads = 0;
ULONG i;
ULONG reportID = 0;
ULONG msecToSleep = 0;
if (nArgs < 2)
{
ASSERT(FALSE);
return;
}
nArgCmd = wcstoul(szArgList[1], NULL, 10);
bConsoleAttached = CLM_AttachConsole();
if (!bConsoleAttached)
{
goto Done;
}
FindKnownHidDevices(&deviceList,&numDevices);
switch (nArgCmd)
{
case 1:
for (i = 0; i < numDevices; i++)
{
if (INVALID_HANDLE_VALUE != deviceList[i].HidDevice)
{
printf("Device #%d: VID: 0x%04X PID: 0x%04X UsagePage: 0x%X Usage: 0x%X\n",
i,
deviceList[i].Attributes.VendorID,
deviceList[i].Attributes.ProductID,
deviceList[i].Caps.UsagePage,
deviceList[i].Caps.Usage);
}
else
{
printf("Device #%d: %s (error opening the device)\n",
i,
(NULL != deviceList[i].DevicePath) ? deviceList[i].DevicePath : "");
}
}
break;
case 2:
if (nArgs < 3)
{
bPrintHelp = TRUE;
break;
}
// Get the device ID argument
nDevice = wcstoul(szArgList[2], NULL, 10);
if (nDevice >= numDevices)
{
bPrintHelp = TRUE;
break;
}
if (INVALID_HANDLE_VALUE == deviceList[nDevice].HidDevice)
{
printf("An error occurred while opening device #%d.\n", nDevice);
break;
}
CLM_ShowDeviceInfo(&deviceList[nDevice]);
break;
case 3:
if (nArgs < 4)
{
bPrintHelp = TRUE;
break;
}
// szArgList[2]: device_num
nDevice = wcstoul(szArgList[2], NULL, 10);
if (nDevice >= numDevices)
{
bPrintHelp = TRUE;
break;
}
if (INVALID_HANDLE_VALUE == deviceList[nDevice].HidDevice)
{
printf("An error occurred while opening device #%d.\n", nDevice);
break;
}
// szArgList[3]: report_ID
reportID = wcstoul(szArgList[3], NULL, 10);
if (reportID>255) {
bPrintHelp = TRUE;
break;
}
// szArgList[4]: msec_to_sleep
if (nArgs >= 5)
{
msecToSleep = wcstoul(szArgList[4], NULL, 10);
}
// szArgList[5]: num_reads
if (nArgs >= 6)
{
numReads = wcstoul(szArgList[5], NULL, 10);
if (0 == numReads)
{
numReads = 1;
}
}
else
{
numReads = INFINITE_READS;
}
CLM_SyncRead(&deviceList[nDevice], (BYTE)reportID, msecToSleep, numReads);
break;
case 4:
if (nArgs < 3)
{
bPrintHelp = TRUE;
break;
}
// szArgList[2]: device_num
nDevice = wcstoul(szArgList[2], NULL, 10);
if (nDevice >= numDevices)
{
bPrintHelp = TRUE;
break;
}
if (INVALID_HANDLE_VALUE == deviceList[nDevice].HidDevice)
{
printf("An error occurred while opening device #%d.\n", nDevice);
break;
}
// szArgList[3]: num_reads
if (nArgs >= 4)
{
numReads = wcstoul(szArgList[3], NULL, 10);
if (0 == numReads)
{
numReads = 1;
}
}
else
{
numReads = INFINITE_READS;
}
CLM_AsyncRead(&deviceList[nDevice], numReads);
break;
default:
bPrintHelp = TRUE;
}
if (bPrintHelp)
{
printf("Syntax: HCLIENT.EXE <action> [arguments]\n"
"\n"
" action: 1 - List HID devices.\n"
"\n"
" 2 - Show device info.\n"
" arguments: device_num\n"
"\n"
" 3 - Do sync reads by calling HidD_GetInputReport.\n"
" arguments: device_num report_id [msec_to_sleep] [num_of_reads]\n"
"\n"
" 4 - Do async reads by calling ReadFile.\n"
" arguments: device_num [num_of_reads]\n");
}
printf("Press enter to exit.\n");
Done:
if (NULL != deviceList)
{
free(deviceList);
deviceList = NULL;
}
if (bConsoleAttached)
{
FreeConsole();
bConsoleAttached = FALSE;
}
}