Report.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #region License
  2. /* Copyright 2011 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.Collections.Generic;
  15. namespace HidSharp.ReportDescriptors.Parser
  16. {
  17. public delegate void ReportScanCallback
  18. (byte[] buffer, int bitOffset, ReportSegment segment);
  19. /// <summary>
  20. /// Reads and writes HID reports.
  21. /// </summary>
  22. public class Report
  23. {
  24. internal List<ReportSegment> _segments;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="Report"/> class.
  27. /// </summary>
  28. public Report()
  29. {
  30. _segments = new List<ReportSegment>();
  31. }
  32. /// <summary>
  33. /// Resets the instance to its initial state.
  34. /// </summary>
  35. public void Clear()
  36. {
  37. List<ReportSegment> segments = new List<ReportSegment>(_segments);
  38. foreach (ReportSegment segment in segments) { segment.Report = null; }
  39. ID = 0; Type = 0;
  40. }
  41. /// <summary>
  42. /// Reads a HID report, calling back a provided function for each segment.
  43. /// </summary>
  44. /// <param name="buffer">The buffer containing the report.</param>
  45. /// <param name="offset">The offset to begin reading the report at.</param>
  46. /// <param name="callback">
  47. /// This callback will be called for each report segment.
  48. /// Use this to read every value you need.
  49. /// </param>
  50. public void Scan(byte[] buffer, int offset, ReportScanCallback callback)
  51. {
  52. int bitOffset = offset * 8;
  53. foreach (ReportSegment segment in Segments)
  54. {
  55. callback(buffer, bitOffset, segment);
  56. bitOffset += segment.BitCount;
  57. }
  58. }
  59. /// <summary>
  60. /// Writes a HID report, calling back a provided function for each segment.
  61. /// </summary>
  62. /// <param name="callback">
  63. /// This callback will be called for each report segment.
  64. /// Write to each segment to write a complete HID report.
  65. /// </param>
  66. public byte[] Write(ReportScanCallback callback)
  67. {
  68. byte[] buffer = new byte[1 + Length];
  69. buffer[0] = ID; Scan(buffer, 1, callback);
  70. return buffer;
  71. }
  72. /// <summary>
  73. /// The Report ID.
  74. /// </summary>
  75. public byte ID
  76. {
  77. get;
  78. set;
  79. }
  80. /// <summary>
  81. /// The length of this particular report.
  82. /// The Report ID is not included in this length.
  83. /// </summary>
  84. public int Length
  85. {
  86. get
  87. {
  88. int bits = 0;
  89. foreach (ReportSegment segment in _segments) { bits += segment.BitCount; }
  90. return (bits + 7) / 8;
  91. }
  92. }
  93. public IEnumerable<ReportSegment> Segments
  94. {
  95. get { foreach (ReportSegment segment in _segments) { yield return segment; } }
  96. }
  97. public ReportType Type
  98. {
  99. get;
  100. set;
  101. }
  102. }
  103. }