Skip to content

Commit

Permalink
Support connection via serial number
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed Oct 9, 2022
1 parent d1d3c72 commit 553e166
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/HidApi.Net/Device.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace HidApi;

/// <summary>
Expand All @@ -15,7 +17,21 @@ public class Device : IDisposable
/// <param name="productId">product id of target device</param>
public Device(ushort vendorId, ushort productId)
{
handle = NativeMethods.Open(vendorId, productId, string.Empty);
handle = NativeMethods.Open(vendorId, productId, ref Unsafe.NullRef<byte>());

if (handle.IsInvalid)
HidException.Throw(handle);
}

/// <summary>
/// Connects to a given device.
/// </summary>
/// <param name="vendorId">Vendor id of target device</param>
/// <param name="productId">Product id of target device</param>
/// <param name="serialNumber">Serial number of target device</param>
public Device(ushort vendorId, ushort productId, string serialNumber)
{
handle = NativeMethods.Open(vendorId, productId, ref WCharT.CreateNullTerminatedString(serialNumber));

if (handle.IsInvalid)
HidException.Throw(handle);
Expand Down
2 changes: 1 addition & 1 deletion src/HidApi.Net/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static int GetInputReport(DeviceSafeHandle device, ReadOnlySpan<byte> dat
public static extern unsafe void FreeEnumeration(NativeDeviceInfo* devices);

[DllImport(Library, EntryPoint = "hid_open")]
public static extern DeviceSafeHandle Open(ushort vendorId, ushort productId, [MarshalAs(UnmanagedType.LPWStr)] string serialNumber);
public static extern DeviceSafeHandle Open(ushort vendorId, ushort productId, ref byte serialNumber);

[DllImport(Library, EntryPoint = "hid_open_path")]
public static extern DeviceSafeHandle OpenPath([MarshalAs(UnmanagedType.LPStr)] string path);
Expand Down
10 changes: 10 additions & 0 deletions src/HidApi.Net/Internal/Unicode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
using System.Runtime.InteropServices;
using System.Text;

namespace HidApi;

internal static class Unicode
{
public static ref byte CreateNullTerminatedString(string str)
{
var src = Encoding.Unicode.GetBytes(str);
var dest = new byte[src.Length + sizeof(uint)];
Array.Copy(src, dest, src.Length);

return ref MemoryMarshal.AsRef<byte>(dest);
}

public static ReadOnlySpan<byte> CreateBuffer(int size)
{
return new byte[size * sizeof(ushort)];
Expand Down
10 changes: 10 additions & 0 deletions src/HidApi.Net/Internal/Utf32.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
using System.Runtime.InteropServices;
using System.Text;

namespace HidApi;

internal static class Utf32
{
public static ref byte CreateNullTerminatedString(string str)
{
var src = Encoding.UTF32.GetBytes(str);
var dest = new byte[src.Length + sizeof(uint)];
Array.Copy(src, dest, src.Length);

return ref MemoryMarshal.AsRef<byte>(dest);
}

public static ReadOnlySpan<byte> CreateBuffer(int size)
{
return new byte[size * sizeof(uint)];
Expand Down
14 changes: 14 additions & 0 deletions src/HidApi.Net/Internal/WCharT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ public static ReadOnlySpan<byte> CreateBuffer(int size)

throw new Exception("Unsupported platform to create a buffer");
}

public static ref byte CreateNullTerminatedString(string str)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return ref Utf32.CreateNullTerminatedString(str);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return ref Unicode.CreateNullTerminatedString(str);

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return ref Utf32.CreateNullTerminatedString(str);

throw new Exception("Unsupported platform to create a string");
}
}

0 comments on commit 553e166

Please sign in to comment.