From f02dde1249224a4a0c30772ed8a110a12e9d212d Mon Sep 17 00:00:00 2001 From: badcel <1218031+badcel@users.noreply.github.com> Date: Wed, 31 May 2023 22:51:20 +0200 Subject: [PATCH] Support HIDAPI 0.14.0 --- readme.md | 2 +- src/HidApi.Net/Device.cs | 22 ++++++++++++++++++++++ src/HidApi.Net/Internal/NativeMethods.cs | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 85592ba..64ebedb 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ Welcome to HidApi.Net a modern .NET 6 cross platform C# binding for the C [HIDAPI] library. Supported platforms are Linux, OSX and Windows. -Supported HIDAPI version: 0.13 +Supported HIDAPI version: Up to 0.14 ## Use To use the library please reference the [nuget package](https://www.nuget.org/packages/HidApi.Net/) in your project. Additionally it is required to either ensure that [HIDAPI] is available on the host system or is distributed as part of your application. diff --git a/src/HidApi.Net/Device.cs b/src/HidApi.Net/Device.cs index f0657ed..f87ccc7 100644 --- a/src/HidApi.Net/Device.cs +++ b/src/HidApi.Net/Device.cs @@ -271,6 +271,28 @@ public string GetIndexedString(int stringIndex, int maxLength = 128) return WCharT.GetString(buffer); } + /// + /// Gets the report descriptor of the device. + /// + /// Max length of the expected data. + /// The report descriptor + /// Available since hidapi 0.14.0 + /// Raised if bufLength is less than 0 + public ReadOnlySpan GetReportDescriptor(int bufSize = 4096) + { + if (bufSize < 0) + throw new ArgumentOutOfRangeException(nameof(bufSize), bufSize, "Please provide a positive value"); + + var data = new byte[bufSize]; + ReadOnlySpan spanData = data; + var result = NativeMethods.GetReportDescriptor(handle, spanData); + + if (result == -1) + HidException.Throw(handle); + + return spanData[..result]; + } + /// /// Frees all unmanaged resources. /// diff --git a/src/HidApi.Net/Internal/NativeMethods.cs b/src/HidApi.Net/Internal/NativeMethods.cs index 08464a9..3fbf878 100644 --- a/src/HidApi.Net/Internal/NativeMethods.cs +++ b/src/HidApi.Net/Internal/NativeMethods.cs @@ -89,6 +89,11 @@ public static int GetInputReport(DeviceSafeHandle device, ReadOnlySpan dat return GetInputReport(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length); } + public static int GetReportDescriptor(DeviceSafeHandle device, ReadOnlySpan buf) + { + return GetReportDescriptor(device, ref MemoryMarshal.GetReference(buf), (nuint) buf.Length); + } + [DllImport(Library, EntryPoint = "hid_init")] public static extern int Init(); @@ -146,6 +151,9 @@ public static int GetInputReport(DeviceSafeHandle device, ReadOnlySpan dat [DllImport(Library, EntryPoint = "hid_get_indexed_string")] private static extern int GetIndexedString(DeviceSafeHandle device, int stringIndex, ref byte buffer, nuint maxLength); + [DllImport(Library, EntryPoint = "hid_get_report_descriptor")] + private static extern int GetReportDescriptor(DeviceSafeHandle device, ref byte buf, nuint bufSize); + [DllImport(Library, EntryPoint = "hid_error")] public static extern unsafe byte* Error(DeviceSafeHandle device);