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

Add Read overloads for passing in buffer #102

Merged
merged 1 commit into from
Aug 12, 2024
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
30 changes: 10 additions & 20 deletions src/HidApi.Net/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,34 @@ public void Write(ReadOnlySpan<byte> data)
/// <summary>
/// Returns an input report.
/// </summary>
/// <param name="maxLength">Max length of the expected data. The value can be greater than the actual report.</param>
/// <param name="buffer">Buffer to write the data into</param>
/// <param name="milliseconds">timeout in milliseconds. -1 for blocking mode.</param>
/// <returns>The received data of the HID device. If the timeout is exceeded an empty result is returned.</returns>
/// <exception cref="ArgumentOutOfRangeException">Raised if maxlength is smaller than 0</exception>
/// <returns>The length of the received data in bytes. If the timeout is exceeded 0 is returned.</returns>
/// <exception cref="HidException">Raised on failure</exception>
public ReadOnlySpan<byte> ReadTimeout(int maxLength, int milliseconds)
public int ReadTimeout(Span<byte> buffer, int milliseconds)
{
if (maxLength < 0)
throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value");

ReadOnlySpan<byte> data = new byte[maxLength];
var result = NativeMethods.ReadTimeOut(handle, data, milliseconds);
var result = NativeMethods.ReadTimeOut(handle, buffer, milliseconds);

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

return data[..result];
return result;
}

/// <summary>
/// Returns an input report.
/// </summary>
/// <param name="maxLength">Max length of the expected data. The value can be greater than the actual report.</param>
/// <returns>The received data of the HID device. If non-blocking mode is enabled and no data is available an empty result will be returned.</returns>
/// <exception cref="ArgumentOutOfRangeException">Raised if maxlength is smaller than 0</exception>
/// <param name="buffer">Buffer to write the data into</param>
/// <returns>The length of the received data in bytes. If non-blocking mode is enabled and no data is available 0 will be returned.</returns>
/// <exception cref="HidException">Raised on failure</exception>
public ReadOnlySpan<byte> Read(int maxLength)
public int Read(Span<byte> buffer)
{
if (maxLength < 0)
throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value");

ReadOnlySpan<byte> data = new byte[maxLength];
var result = NativeMethods.Read(handle, data);
var result = NativeMethods.Read(handle, buffer);

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

return data[..result];
return result;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/HidApi.Net/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public static int Write(DeviceSafeHandle device, ReadOnlySpan<byte> data)
return Write(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length);
}

public static int ReadTimeOut(DeviceSafeHandle device, ReadOnlySpan<byte> data, int milliseconds)
public static int ReadTimeOut(DeviceSafeHandle device, Span<byte> data, int milliseconds)
{
return ReadTimeOut(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length, milliseconds);
}

public static int Read(DeviceSafeHandle device, ReadOnlySpan<byte> data)
public static int Read(DeviceSafeHandle device, Span<byte> data)
{
return Read(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length);
}
Expand Down