WinHidStream.cs 6.3 KB

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