WinHidStream.cs 6.1 KB

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