Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HIDAPI 0.14.0 #38

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/HidApi.Net/HidApi.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>0.3.0</VersionPrefix>
<VersionPrefix>0.4.0</VersionPrefix>

<RootNamespace>HidApi</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
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 @@ -89,6 +89,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