WinHidDevice.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #region License
  2. /* Copyright 2010, 2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. #pragma warning disable 618
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Threading;
  18. namespace HidSharp.Platform.Windows
  19. {
  20. sealed class WinHidDevice : HidDevice
  21. {
  22. string _path;
  23. string _manufacturer;
  24. string _productName;
  25. string _serialNumber;
  26. int _vid, _pid, _version;
  27. int _maxInput, _maxOutput, _maxFeature;
  28. object _completeSync = new object();
  29. volatile bool _complete;
  30. internal WinHidDevice(string path)
  31. {
  32. _path = path;
  33. }
  34. void WaitForCompletion()
  35. {
  36. lock (_completeSync)
  37. {
  38. while (!_complete) { Monitor.Wait(_completeSync); }
  39. }
  40. }
  41. public override HidStream Open()
  42. {
  43. WaitForCompletion();
  44. var stream = new WinHidStream();
  45. try { stream.Init(_path, this); return stream; }
  46. catch { stream.Close(); throw; }
  47. }
  48. internal bool GetInfo(IntPtr handle)
  49. {
  50. NativeMethods.HIDD_ATTRIBUTES attributes = new NativeMethods.HIDD_ATTRIBUTES();
  51. attributes.Size = Marshal.SizeOf(attributes);
  52. if (!NativeMethods.HidD_GetAttributes(handle, ref attributes)) { return false; }
  53. _pid = attributes.ProductID;
  54. _vid = attributes.VendorID;
  55. _version = attributes.VersionNumber;
  56. return true;
  57. }
  58. internal void GetInfoComplete(IntPtr handle)
  59. {
  60. try
  61. {
  62. char[] buffer = new char[128];
  63. _manufacturer = NativeMethods.HidD_GetManufacturerString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  64. _productName = NativeMethods.HidD_GetProductString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  65. _serialNumber = NativeMethods.HidD_GetSerialNumberString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  66. IntPtr preparsed;
  67. if (NativeMethods.HidD_GetPreparsedData(handle, out preparsed))
  68. {
  69. NativeMethods.HIDP_CAPS caps;
  70. int statusCaps = NativeMethods.HidP_GetCaps(preparsed, out caps);
  71. if (statusCaps == NativeMethods.HIDP_STATUS_SUCCESS)
  72. {
  73. _maxInput = caps.InputReportByteLength;
  74. _maxOutput = caps.OutputReportByteLength;
  75. _maxFeature = caps.FeatureReportByteLength;
  76. }
  77. NativeMethods.HidD_FreePreparsedData(preparsed);
  78. }
  79. }
  80. finally
  81. {
  82. NativeMethods.CloseHandle(handle);
  83. }
  84. lock (_completeSync) { _complete = true; Monitor.PulseAll(_completeSync); }
  85. }
  86. public override string DevicePath
  87. {
  88. get { return _path; }
  89. }
  90. public override int MaxInputReportLength
  91. {
  92. get { WaitForCompletion(); return _maxInput; }
  93. }
  94. public override int MaxOutputReportLength
  95. {
  96. get { WaitForCompletion(); return _maxOutput; }
  97. }
  98. public override int MaxFeatureReportLength
  99. {
  100. get { WaitForCompletion(); return _maxFeature; }
  101. }
  102. public override string Manufacturer
  103. {
  104. get { WaitForCompletion(); return _manufacturer; }
  105. }
  106. public override int ProductID
  107. {
  108. get { return _pid; }
  109. }
  110. public override string ProductName
  111. {
  112. get { WaitForCompletion(); return _productName; }
  113. }
  114. public override int ProductVersion
  115. {
  116. get { return _version; }
  117. }
  118. public override string SerialNumber
  119. {
  120. get { WaitForCompletion(); return _serialNumber; }
  121. }
  122. public override int VendorID
  123. {
  124. get { return _vid; }
  125. }
  126. }
  127. }