/SampleTOAPIA/USBHIDTest/USBHIDTest/Extensions.cs

https://github.com/Wiladams/NewTOAPIA · C# · 101 lines · 9 code · 3 blank · 89 comment · 0 complexity · 1c7f4755fd2695f5388c98e9b9bd2860 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Permissions;
  4. namespace USBHIDTest
  5. {
  6. public static class Extensions
  7. {
  8. //[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  9. //internal static bool InitRegisters(this USBDevice device)
  10. //{
  11. // var result = false;
  12. // var registers = device.ReadRegisters(ReportType.Entire);
  13. // result = registers != default(ushort[]);
  14. // if (result) device.Registers = registers;
  15. // return result;
  16. //}
  17. //[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
  18. //internal static ushort[] ReadRegisters(this USBDevice device, ReportType report)
  19. //{
  20. // var result = default(ushort[]);
  21. // if (!device.IsInvalid)
  22. // {
  23. // var buffer = new byte[device.FeatureReportLength];
  24. // buffer[0] = (byte)report;
  25. // var reportLength = 0;
  26. // switch (report)
  27. // {
  28. // case ReportType.StatusRssi:
  29. // case ReportType.ReadChan:
  30. // reportLength = 1;
  31. // break;
  32. // case ReportType.RDS: reportLength = Native.RDS_REGISTER_NUM; break;
  33. // case ReportType.Entire: reportLength = Native.FMRADIO_REGISTER_NUM; break;
  34. // case ReportType.Scratch: reportLength = Native.SCRATCH_PAGE_SIZE; break;
  35. // }
  36. // result = new ushort[reportLength];
  37. // if (reportLength <= (device.FeatureReportLength - 1) / Native.FMRADIO_REGISTER_SIZE)
  38. // {
  39. // if (report >= ReportType.DeviceID && report <= ReportType.Entire)
  40. // {
  41. // var hEPBuffer = Marshal.AllocHGlobal(buffer.Length);
  42. // Marshal.Copy(buffer, 0, hEPBuffer, buffer.Length);
  43. // if (USBDevice.Native.HidD_GetFeature(device.DangerousGetHandle(), hEPBuffer, (uint)buffer.Length))
  44. // {
  45. // Marshal.Copy(hEPBuffer, buffer, 0, buffer.Length);
  46. // for (byte i = 0; i < result.Length; i++)
  47. // {
  48. // result[i] = (ushort)((buffer[(i * 2) + 1] << 8) | buffer[(i * 2) + 2]);
  49. // }
  50. // }
  51. // Marshal.FreeHGlobal(hEPBuffer);
  52. // }
  53. // else if (report == ReportType.RDS)
  54. // {
  55. // buffer = new byte[Native.RDS_REPORT_SIZE];
  56. // buffer[0] = (byte)ReportType.RDS;
  57. // uint bytesRead;
  58. // Native.OVERLAPPED o = new Native.OVERLAPPED();
  59. // o.hEvent = Native.CreateEventW(IntPtr.Zero, false, false, string.Empty);
  60. // IntPtr hOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(o));
  61. // Marshal.StructureToPtr(o, hOverlapped, true);
  62. // if (!Native.ReadFile(device.DangerousGetHandle(), buffer, (uint)buffer.Length, out bytesRead, hOverlapped))
  63. // {
  64. // var error = Marshal.GetLastWin32Error();
  65. // if (error == Native.ERROR_IO_PENDING)
  66. // {
  67. // if (Native.WaitForSingleObject(o.hEvent, 3000))
  68. // {
  69. // Native.GetOverlappedResult(device.DangerousGetHandle(), ref o, out bytesRead, false);
  70. // }
  71. // }
  72. // }
  73. // Marshal.FreeHGlobal(hOverlapped);
  74. // for (byte i = 0; i < result.Length; i++)
  75. // {
  76. // result[i] = (ushort)((buffer[(i * 2) + 1] << 8) | buffer[(i * 2) + 2]);
  77. // }
  78. // }
  79. // }
  80. // else if (reportLength <= (device.FeatureReportLength - 1))
  81. // {
  82. // var hEPBuffer = Marshal.AllocHGlobal(device.FeatureReportLength);
  83. // Marshal.Copy(buffer, 0, hEPBuffer, buffer.Length);
  84. // if (USBDevice.Native.HidD_GetFeature(device.DangerousGetHandle(), hEPBuffer, device.FeatureReportLength))
  85. // {
  86. // Marshal.Copy(hEPBuffer, buffer, 0, buffer.Length);
  87. // for (byte i = 0; i < result.Length; i++)
  88. // {
  89. // result[i] = (ushort)buffer[i + 1];
  90. // }
  91. // }
  92. // Marshal.FreeHGlobal(hEPBuffer);
  93. // }
  94. // }
  95. // return result;
  96. //}
  97. }
  98. }