WinHidStream.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #region License
  2. /* Copyright 2012-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. using System;
  15. using System.ComponentModel;
  16. using System.IO;
  17. using System.Threading;
  18. namespace HidSharp.Platform.Windows
  19. {
  20. class WinHidStream : HidStream
  21. {
  22. object _readSync = new object(), _writeSync = new object();
  23. byte[] _readBuffer, _writeBuffer;
  24. IntPtr _handle, _closeEventHandle;
  25. WinHidDevice _device;
  26. internal WinHidStream()
  27. {
  28. _closeEventHandle = NativeMethods.CreateManualResetEventOrThrow();
  29. }
  30. ~WinHidStream()
  31. {
  32. Close();
  33. NativeMethods.CloseHandle(_closeEventHandle);
  34. }
  35. internal void Init(string path, WinHidDevice device)
  36. {
  37. IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.All);
  38. if (handle == (IntPtr)(-1)) { throw new IOException("Unable to open HID class device."); }
  39. _device = device;
  40. _handle = handle;
  41. HandleInitAndOpen();
  42. }
  43. protected override void Dispose(bool disposing)
  44. {
  45. base.Dispose(disposing);
  46. if (!HandleClose()) { return; }
  47. NativeMethods.SetEvent(_closeEventHandle);
  48. HandleRelease();
  49. }
  50. internal override void HandleFree()
  51. {
  52. NativeMethods.CloseHandle(ref _handle);
  53. NativeMethods.CloseHandle(ref _closeEventHandle);
  54. }
  55. public unsafe override void GetFeature(byte[] buffer, int offset, int count)
  56. {
  57. HandleAcquireIfOpenOrFail();
  58. try
  59. {
  60. fixed (byte* ptr = buffer)
  61. {
  62. if (!NativeMethods.HidD_GetFeature(_handle, ptr + offset, count))
  63. { throw new IOException("GetFeature failed.", new Win32Exception()); }
  64. }
  65. }
  66. finally
  67. {
  68. HandleRelease();
  69. }
  70. }
  71. // Buffer needs to be big enough for the largest report, plus a byte
  72. // for the Report ID.
  73. public unsafe override int Read(byte[] buffer, int offset, int count)
  74. {
  75. uint bytesTransferred;
  76. IntPtr @event = NativeMethods.CreateManualResetEventOrThrow();
  77. HandleAcquireIfOpenOrFail();
  78. try
  79. {
  80. lock (_readSync)
  81. {
  82. int maxIn = _device.MaxInputReportLength;
  83. Array.Resize(ref _readBuffer, maxIn); if (count > maxIn) { count = maxIn; }
  84. fixed (byte* ptr = _readBuffer)
  85. {
  86. var overlapped = stackalloc NativeOverlapped[1];
  87. overlapped[0].EventHandle = @event;
  88. NativeMethods.OverlappedOperation(_handle, @event, ReadTimeout, _closeEventHandle,
  89. NativeMethods.ReadFile(_handle, ptr, maxIn, IntPtr.Zero, overlapped),
  90. overlapped, out bytesTransferred);
  91. if (count > (int)bytesTransferred) { count = (int)bytesTransferred; }
  92. Array.Copy(_readBuffer, 0, buffer, offset, count);
  93. return count;
  94. }
  95. }
  96. }
  97. finally
  98. {
  99. HandleRelease();
  100. NativeMethods.CloseHandle(@event);
  101. }
  102. }
  103. public unsafe override void SetFeature(byte[] buffer, int offset, int count)
  104. {
  105. HandleAcquireIfOpenOrFail();
  106. try
  107. {
  108. fixed (byte* ptr = buffer)
  109. {
  110. if (!NativeMethods.HidD_SetFeature(_handle, ptr + offset, count))
  111. { throw new IOException("SetFeature failed.", new Win32Exception()); }
  112. }
  113. }
  114. finally
  115. {
  116. HandleRelease();
  117. }
  118. }
  119. public unsafe override void Write(byte[] buffer, int offset, int count)
  120. {
  121. uint bytesTransferred;
  122. IntPtr @event = NativeMethods.CreateManualResetEventOrThrow();
  123. HandleAcquireIfOpenOrFail();
  124. try
  125. {
  126. lock (_writeSync)
  127. {
  128. int maxOut = _device.MaxOutputReportLength;
  129. Array.Resize(ref _writeBuffer, maxOut); if (count > maxOut) { count = maxOut; }
  130. Array.Copy(buffer, offset, _writeBuffer, 0, count); count = maxOut;
  131. fixed (byte* ptr = _writeBuffer)
  132. {
  133. int offset0 = 0;
  134. while (count > 0)
  135. {
  136. var overlapped = stackalloc NativeOverlapped[1];
  137. overlapped[0].EventHandle = @event;
  138. NativeMethods.OverlappedOperation(_handle, @event, WriteTimeout, _closeEventHandle,
  139. NativeMethods.WriteFile(_handle, ptr + offset0, count, IntPtr.Zero, overlapped),
  140. overlapped, out bytesTransferred);
  141. count -= (int)bytesTransferred; offset0 += (int)bytesTransferred;
  142. }
  143. }
  144. }
  145. }
  146. finally
  147. {
  148. HandleRelease();
  149. NativeMethods.CloseHandle(@event);
  150. }
  151. }
  152. public override HidDevice Device
  153. {
  154. get { return _device; }
  155. }
  156. }
  157. }