Skip to content

Commit

Permalink
Support HIDAPI 0.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed May 31, 2023
1 parent fc5355e commit daaebe4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions src/HidApi.Net/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,28 @@ public string GetIndexedString(int stringIndex, int maxLength = 128)
return WCharT.GetString(buffer);
}

/// <summary>
/// Gets the report descriptor of the device.
/// </summary>
/// <param name="bufSize">Max length of the expected data.</param>
/// <returns>The report descriptor</returns>
/// <remarks>Available since hidapi 0.14.0</remarks>
/// <exception cref="ArgumentOutOfRangeException">Raised if bufLength is less than 0</exception>
public ReadOnlySpan<byte> GetReportDescriptor(int bufSize = 4096)
{
if (bufSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufSize), bufSize, "Please provide a positive value");

var data = new byte[bufSize];
ReadOnlySpan<byte> spanData = data;
var result = NativeMethods.GetReportDescriptor(handle, spanData);

if (result == -1)
HidException.Throw(handle);

return spanData[..result];
}

/// <summary>
/// Frees all unmanaged resources.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/HidApi.Net/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public static int GetInputReport(DeviceSafeHandle device, ReadOnlySpan<byte> dat
{
return GetInputReport(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length);
}

public static int GetReportDescriptor(DeviceSafeHandle device, ReadOnlySpan<byte> buf)
{
return GetReportDescriptor(device, ref MemoryMarshal.GetReference(buf), (nuint) buf.Length);
}

[DllImport(Library, EntryPoint = "hid_init")]
public static extern int Init();
Expand Down Expand Up @@ -146,6 +151,9 @@ public static int GetInputReport(DeviceSafeHandle device, ReadOnlySpan<byte> 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);

Expand Down

0 comments on commit daaebe4

Please sign in to comment.