HidDevice.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. using System.Globalization;
  15. using System.Runtime.InteropServices;
  16. namespace HidSharp
  17. {
  18. /// <summary>
  19. /// Represents a USB HID class device.
  20. /// </summary>
  21. [ComVisible(true), Guid("4D8A9A1A-D5CC-414e-8356-5A025EDA098D")]
  22. public abstract class HidDevice
  23. {
  24. /// <summary>
  25. /// Makes a connection to the USB HID class device, or throws an exception if the connection cannot be made.
  26. /// </summary>
  27. /// <returns>The stream to use to communicate with the device.</returns>
  28. public abstract HidStream Open();
  29. /// <summary>
  30. /// Returns the raw report descriptor of the USB device.
  31. /// Currently this is only supported on Linux.
  32. /// </summary>
  33. /// <returns>The report descriptor.</returns>
  34. public virtual byte[] GetReportDescriptor()
  35. {
  36. throw new NotSupportedException(); // Windows without libusb can't... Linux can.
  37. }
  38. /// <summary>
  39. /// Tries to make a connection to the USB HID class device.
  40. /// </summary>
  41. /// <param name="stream">The stream to use to communicate with the device.</param>
  42. /// <returns>True if the connetion was successful.</returns>
  43. public bool TryOpen(out HidStream stream)
  44. {
  45. try
  46. {
  47. stream = Open();
  48. return true;
  49. }
  50. catch (Exception e)
  51. {
  52. #if DEBUG
  53. Console.WriteLine(e);
  54. #endif
  55. stream = null; return false;
  56. }
  57. }
  58. /// <summary>
  59. /// The operating system's name for the device.
  60. ///
  61. /// If you have multiple devices with the same Vendor ID, Product ID, Serial Number. etc.,
  62. /// this may be useful for differentiating them.
  63. /// </summary>
  64. public abstract string DevicePath
  65. {
  66. get;
  67. }
  68. /// <summary>
  69. /// The maximum input report length, including the Report ID byte.
  70. /// If the device does not use Report IDs, the first byte will always be 0.
  71. /// </summary>
  72. public abstract int MaxInputReportLength { get; }
  73. /// <summary>
  74. /// The maximum output report length, including the Report ID byte.
  75. /// If the device does not use Report IDs, use 0 for the first byte.
  76. /// </summary>
  77. public abstract int MaxOutputReportLength { get; }
  78. /// <summary>
  79. /// The maximum feature report length, including the Report ID byte.
  80. /// If the device does not use Report IDs, use 0 for the first byte.
  81. /// </summary>
  82. public abstract int MaxFeatureReportLength { get; }
  83. /// <summary>
  84. /// The manufacturer name.
  85. /// </summary>
  86. public abstract string Manufacturer
  87. {
  88. get;
  89. }
  90. /// <summary>
  91. /// The USB product ID. These are listed at: http://usb-ids.gowdy.us
  92. /// </summary>
  93. public abstract int ProductID
  94. {
  95. get;
  96. }
  97. /// <summary>
  98. /// The product name.
  99. /// </summary>
  100. public abstract string ProductName
  101. {
  102. get;
  103. }
  104. /// <summary>
  105. /// The product version.
  106. /// This is a 16-bit number encoding the major and minor versions in the upper and lower 8 bits, respectively.
  107. /// </summary>
  108. public abstract int ProductVersion
  109. {
  110. get;
  111. }
  112. /// <summary>
  113. /// The device serial number.
  114. /// </summary>
  115. public abstract string SerialNumber
  116. {
  117. get;
  118. }
  119. /// <summary>
  120. /// The USB vendor ID. These are listed at: http://usb-ids.gowdy.us
  121. /// </summary>
  122. public abstract int VendorID
  123. {
  124. get;
  125. }
  126. /// <inheritdoc />
  127. public override string ToString()
  128. {
  129. return string.Format(CultureInfo.InvariantCulture, "{0} ({1}VID {2}, PID {3}, version {4})",
  130. Manufacturer.Length > 0 || ProductName.Length > 0 ? Manufacturer.Trim() + " " + ProductName.Trim() : "(unnamed)",
  131. SerialNumber.Length > 0 ? "serial " + SerialNumber.Trim() + ", " : "", VendorID, ProductID, ProductVersion);
  132. }
  133. }
  134. }