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.Runtime.InteropServices;
  16. namespace HidSharp.Platform.Windows
  17. {
  18. internal sealed class WinHidDevice : HidDevice
  19. {
  20. private string _path;
  21. private string _manufacturer;
  22. private string _productName;
  23. private string _serialNumber;
  24. private int _vid, _pid, _version;
  25. private int _maxInput, _maxOutput, _maxFeature;
  26. private object _completeSync = new object();
  27. private volatile bool _complete;
  28. internal WinHidDevice(string path)
  29. {
  30. _path = path;
  31. }
  32. private void WaitForCompletion()
  33. {
  34. lock (_completeSync)
  35. {
  36. while (!_complete) { Monitor.Wait(_completeSync); }
  37. }
  38. }
  39. public override HidStream Open()
  40. {
  41. WaitForCompletion();
  42. var stream = new WinHidStream();
  43. try { stream.Init(_path, this); return stream; }
  44. catch { stream.Close(); throw; }
  45. }
  46. internal bool GetInfo(IntPtr handle)
  47. {
  48. NativeMethods.HIDD_ATTRIBUTES attributes = new NativeMethods.HIDD_ATTRIBUTES();
  49. attributes.Size = Marshal.SizeOf(attributes);
  50. if (!NativeMethods.HidD_GetAttributes(handle, ref attributes)) { return false; }
  51. _pid = attributes.ProductID;
  52. _vid = attributes.VendorID;
  53. _version = attributes.VersionNumber;
  54. return true;
  55. }
  56. internal void GetInfoComplete(IntPtr handle)
  57. {
  58. try
  59. {
  60. char[] buffer = new char[128];
  61. _manufacturer = NativeMethods.HidD_GetManufacturerString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  62. _productName = NativeMethods.HidD_GetProductString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  63. _serialNumber = NativeMethods.HidD_GetSerialNumberString(handle, buffer, 256) ? NativeMethods.NTString(buffer) : "";
  64. IntPtr preparsed;
  65. if (NativeMethods.HidD_GetPreparsedData(handle, out preparsed))
  66. {
  67. NativeMethods.HIDP_CAPS caps;
  68. int statusCaps = NativeMethods.HidP_GetCaps(preparsed, out caps);
  69. if (statusCaps == NativeMethods.HIDP_STATUS_SUCCESS)
  70. {
  71. _maxInput = caps.InputReportByteLength;
  72. _maxOutput = caps.OutputReportByteLength;
  73. _maxFeature = caps.FeatureReportByteLength;
  74. }
  75. NativeMethods.HidD_FreePreparsedData(preparsed);
  76. }
  77. }
  78. finally
  79. {
  80. NativeMethods.CloseHandle(handle);
  81. }
  82. lock (_completeSync) { _complete = true; Monitor.PulseAll(_completeSync); }
  83. }
  84. public override string DevicePath
  85. {
  86. get { return _path; }
  87. }
  88. public override int MaxInputReportLength
  89. {
  90. get { WaitForCompletion(); return _maxInput; }
  91. }
  92. public override int MaxOutputReportLength
  93. {
  94. get { WaitForCompletion(); return _maxOutput; }
  95. }
  96. public override int MaxFeatureReportLength
  97. {
  98. get { WaitForCompletion(); return _maxFeature; }
  99. }
  100. public override string Manufacturer
  101. {
  102. get { WaitForCompletion(); return _manufacturer; }
  103. }
  104. public override int ProductID
  105. {
  106. get { return _pid; }
  107. }
  108. public override string ProductName
  109. {
  110. get { WaitForCompletion(); return _productName; }
  111. }
  112. public override int ProductVersion
  113. {
  114. get { return _version; }
  115. }
  116. public override string SerialNumber
  117. {
  118. get { WaitForCompletion(); return _serialNumber; }
  119. }
  120. public override int VendorID
  121. {
  122. get { return _vid; }
  123. }
  124. }
  125. }