-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHid.cs
739 lines (595 loc) · 20.6 KB
/
Hid.cs
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
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GenericHid
{
/// <summary>
/// Supports Windows API functions for accessing HID-class USB devices.
/// Includes routines for retrieving information about the configuring a HID and
/// sending and receiving reports via control and interrupt transfers.
/// </summary>
internal sealed partial class Hid
{
// Used in error messages.
private const String ModuleName = "Hid";
internal NativeMethods.HIDP_CAPS Capabilities;
internal NativeMethods.HIDD_ATTRIBUTES DeviceAttributes;
// For viewing results of API calls in debug.write statements:
internal static Debugging MyDebugging = new Debugging();
/// <summary>
/// Provides a central mechanism for exception handling.
/// Displays a message box that describes the exception.
/// </summary>
///
/// <param name="moduleName"> the module where the exception occurred. </param>
/// <param name="e"> the exception </param>
internal static void DisplayException(String moduleName, Exception e)
{
// Create an error message.
String message = "Exception: " + e.Message + Environment.NewLine + "Module: " + moduleName + Environment.NewLine + "Method: " + e.TargetSite.Name;
const String caption = "Unexpected Exception";
MessageBox.Show(message, caption, MessageBoxButtons.OK);
Debug.Write(message);
// Get the last error and display it.
Int32 error = Marshal.GetLastWin32Error();
Debug.WriteLine("The last Win32 Error was: " + error);
}
/// <summary>
/// Remove any Input reports waiting in the buffer.
/// </summary>
/// <param name="hidHandle"> a handle to a device. </param>
/// <returns>
/// True on success, False on failure.
/// </returns>
internal Boolean FlushQueue(SafeFileHandle hidHandle)
{
try
{
// ***
// API function: HidD_FlushQueue
// Purpose: Removes any Input reports waiting in the buffer.
// Accepts: a handle to the device.
// Returns: True on success, False on failure.
// ***
Boolean success = NativeMethods.HidD_FlushQueue(hidHandle);
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Get HID attributes.
/// </summary>
/// <param name="hidHandle"> HID handle retrieved with CreateFile </param>
/// <param name="deviceAttributes"> HID attributes structure </param>
/// <returns> true on success </returns>
internal Boolean GetAttributes(SafeFileHandle hidHandle, ref NativeMethods.HIDD_ATTRIBUTES deviceAttributes)
{
Boolean success;
try
{
// ***
// API function:
// HidD_GetAttributes
// Purpose:
// Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID,
// Product ID, and Product Version Number for a device.
// Accepts:
// A handle returned by CreateFile.
// A pointer to receive a HIDD_ATTRIBUTES structure.
// Returns:
// True on success, False on failure.
// ***
success = NativeMethods.HidD_GetAttributes(hidHandle, ref deviceAttributes);
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
return success;
}
/// <summary>
/// Retrieves a structure with information about a device's capabilities.
/// </summary>
///
/// <param name="hidHandle"> a handle to a device. </param>
///
/// <returns>
/// An HIDP_CAPS structure.
/// </returns>
internal NativeMethods.HIDP_CAPS GetDeviceCapabilities(SafeFileHandle hidHandle)
{
var preparsedData = new IntPtr();
try
{
// ***
// API function: HidD_GetPreparsedData
// Purpose: retrieves a pointer to a buffer containing information about the device's capabilities.
// HidP_GetCaps and other API functions require a pointer to the buffer.
// Requires:
// A handle returned by CreateFile.
// A pointer to a buffer.
// Returns:
// True on success, False on failure.
// ***
NativeMethods.HidD_GetPreparsedData(hidHandle, ref preparsedData);
// ***
// API function: HidP_GetCaps
// Purpose: find out a device's capabilities.
// For standard devices such as joysticks, you can find out the specific
// capabilities of the device.
// For a custom device where the software knows what the device is capable of,
// this call may be unneeded.
// Accepts:
// A pointer returned by HidD_GetPreparsedData
// A pointer to a HIDP_CAPS structure.
// Returns: True on success, False on failure.
// ***
Int32 result = NativeMethods.HidP_GetCaps(preparsedData, ref Capabilities);
if ((result != 0))
{
Debug.WriteLine("");
Debug.WriteLine(" Usage: " + Convert.ToString(Capabilities.Usage, 16));
Debug.WriteLine(" Usage Page: " + Convert.ToString(Capabilities.UsagePage, 16));
Debug.WriteLine(" Input Report Byte Length: " + Capabilities.InputReportByteLength);
Debug.WriteLine(" Output Report Byte Length: " + Capabilities.OutputReportByteLength);
Debug.WriteLine(" Feature Report Byte Length: " + Capabilities.FeatureReportByteLength);
Debug.WriteLine(" Number of Link Collection Nodes: " + Capabilities.NumberLinkCollectionNodes);
Debug.WriteLine(" Number of Input Button Caps: " + Capabilities.NumberInputButtonCaps);
Debug.WriteLine(" Number of Input Value Caps: " + Capabilities.NumberInputValueCaps);
Debug.WriteLine(" Number of Input Data Indices: " + Capabilities.NumberInputDataIndices);
Debug.WriteLine(" Number of Output Button Caps: " + Capabilities.NumberOutputButtonCaps);
Debug.WriteLine(" Number of Output Value Caps: " + Capabilities.NumberOutputValueCaps);
Debug.WriteLine(" Number of Output Data Indices: " + Capabilities.NumberOutputDataIndices);
Debug.WriteLine(" Number of Feature Button Caps: " + Capabilities.NumberFeatureButtonCaps);
Debug.WriteLine(" Number of Feature Value Caps: " + Capabilities.NumberFeatureValueCaps);
Debug.WriteLine(" Number of Feature Data Indices: " + Capabilities.NumberFeatureDataIndices);
// ***
// API function: HidP_GetValueCaps
// Purpose: retrieves a buffer containing an array of HidP_ValueCaps structures.
// Each structure defines the capabilities of one value.
// This application doesn't use this data.
// Accepts:
// A report type enumerator from hidpi.h,
// A pointer to a buffer for the returned array,
// The NumberInputValueCaps member of the device's HidP_Caps structure,
// A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
// Returns: True on success, False on failure.
// ***
Int32 vcSize = Capabilities.NumberInputValueCaps;
var valueCaps = new Byte[vcSize];
NativeMethods.HidP_GetValueCaps(NativeMethods.HidP_Input, valueCaps, ref vcSize, preparsedData);
// (To use this data, copy the ValueCaps byte array into an array of structures.)
}
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
finally
{
// ***
// API function: HidD_FreePreparsedData
// Purpose: frees the buffer reserved by HidD_GetPreparsedData.
// Accepts: A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
// Returns: True on success, False on failure.
// ***
if (preparsedData != IntPtr.Zero)
{
NativeMethods.HidD_FreePreparsedData(preparsedData);
}
}
return Capabilities;
}
/// <summary>
/// reads a Feature report from the device.
/// </summary>
///
/// <param name="hidHandle"> the handle for learning about the device and exchanging Feature reports. </param>
/// <param name="inFeatureReportBuffer"> contains the requested report.</param>
internal Boolean GetFeatureReport(SafeFileHandle hidHandle, ref Byte[] inFeatureReportBuffer)
{
try
{
Boolean success = false;
// ***
// API function: HidD_GetFeature
// Attempts to read a Feature report from the device.
// Requires:
// A handle to a HID
// A pointer to a buffer containing the report ID and report
// The size of the buffer.
// Returns: true on success, false on failure.
// ***
if (!hidHandle.IsInvalid && !hidHandle.IsClosed)
{
success = NativeMethods.HidD_GetFeature(hidHandle, inFeatureReportBuffer, inFeatureReportBuffer.Length);
Debug.Print("HidD_GetFeature success = " + success);
}
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Get the HID-class GUID
/// </summary>
/// <returns> the GUID </returns>
internal Guid GetHidGuid()
{
Guid hidGuid = Guid.Empty;
try
{
// ***
// API function: 'HidD_GetHidGuid
// Purpose: Retrieves the interface class GUID for the HID class.
// Accepts: A System.Guid object for storing the GUID.
// ***
NativeMethods.HidD_GetHidGuid(ref hidGuid);
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
return hidGuid;
}
/// <summary>
/// Creates a 32-bit Usage from the Usage Page and Usage ID.
/// Determines whether the Usage is a system mouse or keyboard.
/// Can be modified to detect other Usages.
/// </summary>
///
/// <param name="myCapabilities"> a HIDP_CAPS structure retrieved with HidP_GetCaps. </param>
///
/// <returns>
/// A String describing the Usage.
/// </returns>
internal String GetHidUsage(NativeMethods.HIDP_CAPS myCapabilities)
{
String usageDescription = "";
try
{
// Create32-bit Usage from Usage Page and Usage ID.
Int32 usage = myCapabilities.UsagePage * 256 + myCapabilities.Usage;
if (usage == Convert.ToInt32(0X102))
{
usageDescription = "mouse";
}
if (usage == Convert.ToInt32(0X106))
{
usageDescription = "keyboard";
}
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
return usageDescription;
}
/// <summary>
/// reads an Input report from the device using a control transfer.
/// </summary>
/// <param name="hidHandle"> the handle for learning about the device and exchanging Feature reports. </param>
/// <param name="inputReportBuffer"> contains the requested report. </param>
internal Boolean GetInputReportViaControlTransfer(SafeFileHandle hidHandle, ref Byte[] inputReportBuffer)
{
var success = false;
try
{
// ***
// API function: HidD_GetInputReport
// Purpose: Attempts to read an Input report from the device using a control transfer.
// Requires:
// A handle to a HID
// A pointer to a buffer containing the report ID and report
// The size of the buffer.
// Returns: true on success, false on failure.
// ***
if (!hidHandle.IsInvalid && !hidHandle.IsClosed)
{
success = NativeMethods.HidD_GetInputReport(hidHandle, inputReportBuffer, inputReportBuffer.Length + 1);
Debug.Print("HidD_GetInputReport success = " + success);
}
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Reads an Input report from the device using an interrupt transfer.
/// </summary>
///
/// <param name="deviceData"> the Filestream for writing data. </param>
/// <param name="inputReportBuffer"> contains the report ID and report data. </param>
/// <returns>
/// True on success. False on failure.
/// </returns>
internal async Task<Int32> GetInputReportViaInterruptTransfer(FileStream deviceData, Byte[] inputReportBuffer, CancellationTokenSource cts)
{
try
{
Int32 bytesRead = 0;
// Begin reading an Input report.
Task<Int32> t = deviceData.ReadAsync(inputReportBuffer, 0, inputReportBuffer.Length, cts.Token);
bytesRead = await t;
// Gets to here only if the read operation completed before a timeout.
Debug.Print("Asynchronous read completed. Bytes read = " + Convert.ToString(bytesRead));
// The operation has one of these completion states:
switch (t.Status)
{
case TaskStatus.RanToCompletion:
Debug.Print("Input report received from device");
break;
case TaskStatus.Canceled:
Debug.Print("Task canceled");
break;
case TaskStatus.Faulted:
Debug.Print("Unhandled exception");
break;
}
return bytesRead;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Retrieves the number of Input reports the HID driver will store.
/// </summary>
///
/// <param name="hidDeviceObject"> a handle to a device </param>
/// <param name="numberOfInputBuffers"> an integer to hold the returned value. </param>
///
/// <returns>
/// True on success, False on failure.
/// </returns>
internal Boolean GetNumberOfInputBuffers(SafeFileHandle hidDeviceObject, ref Int32 numberOfInputBuffers)
{
try
{
// ***
// API function: HidD_GetNumInputBuffers
// Purpose: retrieves the number of Input reports the host can store.
// Not supported by Windows 98 Gold.
// If the buffer is full and another report arrives, the host drops the
// ldest report.
// Accepts: a handle to a device and an integer to hold the number of buffers.
// Returns: True on success, False on failure.
// ***
Boolean success = NativeMethods.HidD_GetNumInputBuffers(hidDeviceObject, ref numberOfInputBuffers);
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Timeout if read or write via interrupt transfer doesn't return.
/// </summary>
internal void OnTimeout()
{
try
{
// No action required.
Debug.Print("timeout");
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Attempts to open a handle to a HID.
/// </summary>
/// <param name="devicePathName"> device path name returned by SetupDiGetDeviceInterfaceDetail </param>
/// <param name="readAndWrite"> true if requesting read/write access for Input and Output reports </param>
/// <returns> hidHandle - a handle to the HID </returns>
internal SafeFileHandle OpenHandle(String devicePathName, Boolean readAndWrite)
{
SafeFileHandle hidHandle;
try
{
if (readAndWrite)
{
// ***
// API function:
// CreateFile
// Purpose:
// Retrieves a handle to a device.
// Accepts:
// A device path name returned by SetupDiGetDeviceInterfaceDetail
// The type of access requested (read/write).
// FILE_SHARE attributes to allow other processes to access the device while this handle is open.
// A Security structure or IntPtr.Zero.
// A creation disposition value. Use OPEN_EXISTING for devices.
// Flags and attributes for files. Not used for devices.
// Handle to a template file. Not used.
// Returns: a handle without read or write access.
// This enables obtaining information about all HIDs, even system
// keyboards and mice.
// Separate handles are used for reading and writing.
// ***
hidHandle = FileIo.CreateFile(devicePathName, FileIo.GenericRead | FileIo.GenericWrite, FileIo.FileShareRead | FileIo.FileShareWrite, IntPtr.Zero, FileIo.OpenExisting, 0, IntPtr.Zero);
}
else
{
hidHandle = FileIo.CreateFile(devicePathName, 0, FileIo.FileShareRead | FileIo.FileShareWrite, IntPtr.Zero, FileIo.OpenExisting, 0, IntPtr.Zero);
}
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
return hidHandle;
}
/// <summary>
/// Writes a Feature report to the device.
/// </summary>
///
/// <param name="outFeatureReportBuffer"> contains the report ID and report data. </param>
/// <param name="hidHandle"> handle to the device. </param>
///
/// <returns>
/// True on success. False on failure.
/// </returns>
internal Boolean SendFeatureReport(SafeFileHandle hidHandle, Byte[] outFeatureReportBuffer)
{
try
{
// ***
// API function: HidD_SetFeature
// Purpose: Attempts to send a Feature report to the device.
// Accepts:
// A handle to a HID
// A pointer to a buffer containing the report ID and report
// The size of the buffer.
// Returns: true on success, false on failure.
// ***
Boolean success = NativeMethods.HidD_SetFeature(hidHandle, outFeatureReportBuffer, outFeatureReportBuffer.Length);
Debug.Print("HidD_SetFeature success = " + success);
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Writes an Output report to the device using a control transfer.
/// </summary>
///
/// <param name="outputReportBuffer"> contains the report ID and report data. </param>
/// <param name="hidHandle"> handle to the device. </param>
///
/// <returns>
/// True on success. False on failure.
/// </returns>
internal Boolean SendOutputReportViaControlTransfer(SafeFileHandle hidHandle, Byte[] outputReportBuffer)
{
try
{
// ***
// API function: HidD_SetOutputReport
// Purpose:
// Attempts to send an Output report to the device using a control transfer.
// Accepts:
// A handle to a HID
// A pointer to a buffer containing the report ID and report
// The size of the buffer.
// Returns: true on success, false on failure.
// ***
Boolean success = NativeMethods.HidD_SetOutputReport(hidHandle, outputReportBuffer, outputReportBuffer.Length + 1);
Debug.Print("HidD_SetOutputReport success = " + success);
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// Writes an Output report to the device using an interrupt transfer.
/// </summary>
///
/// <param name="fileStreamDeviceData"> the Filestream for writing data. </param>
/// <param name="hidHandle"> SafeFileHandle to the device. </param>
/// <param name="outputReportBuffer"> contains the report ID and report data. </param>
/// <param name="cts"> CancellationTokenSource </param>
///
/// <returns>
/// 1 on success. 0 on failure.
/// </returns>
internal async Task<Boolean> SendOutputReportViaInterruptTransferAsync
(FileStream fileStreamDeviceData, SafeFileHandle hidHandle, Byte[] outputReportBuffer, CancellationTokenSource cts)
{
try
{
var success = false;
// Begin writing the Output report.
Task t = fileStreamDeviceData.WriteAsync(outputReportBuffer, 0, outputReportBuffer.Length, cts.Token);
await t;
// Gets to here only if the write operation completed before a timeout.
Debug.Print("Asynchronous write completed");
// The operation has one of these completion states:
switch (t.Status)
{
case TaskStatus.RanToCompletion:
success = true;
Debug.Print("Output report written to device");
break;
case TaskStatus.Canceled:
Debug.Print("Task canceled");
break;
case TaskStatus.Faulted:
Debug.Print("Unhandled exception");
break;
}
return success;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
/// <summary>
/// sets the number of input reports the host HID driver store.
/// </summary>
///
/// <param name="hidDeviceObject"> a handle to the device.</param>
/// <param name="numberBuffers"> the requested number of input reports. </param>
///
/// <returns>
/// True on success. False on failure.
/// </returns>
internal Boolean SetNumberOfInputBuffers(SafeFileHandle hidDeviceObject, Int32 numberBuffers)
{
try
{
// ***
// API function: HidD_SetNumInputBuffers
// Purpose: Sets the number of Input reports the host can store.
// If the buffer is full and another report arrives, the host drops the
// oldest report.
// Requires:
// A handle to a HID
// An integer to hold the number of buffers.
// Returns: true on success, false on failure.
// ***
NativeMethods.HidD_SetNumInputBuffers(hidDeviceObject, numberBuffers);
return true;
}
catch (Exception ex)
{
DisplayException(ModuleName, ex);
throw;
}
}
}
}