Skip to content

Repository files navigation

GitHub license tests/main CodeQL

Smdn.IO.UsbHid.Abstractions

NuGet Smdn.IO.UsbHid.Abstractions

Provides a unified and abstracted API for interacting with USB HID devices, specifically focusing on HID report I/O operations.

This library provides a unified interface for interacting with USB HID devices in .NET. It defines core abstractions including IUsbHidService, IUsbHidDevice, and IUsbHidEndPoint to represent device discovery and report I/O operations.

The design decouples application logic from specific hardware access libraries through a dependency injection-friendly structure using UsbHidServiceBuilder. Extension methods are included to simplify common tasks such as finding specific devices by VID/PID and opening endpoints. It serves as the common foundation for the Smdn.IO.UsbHid.* libraries, ensuring a consistent API regardless of the chosen backend.

Smdn.IO.UsbHid.Providers.HidSharp

NuGet Smdn.IO.UsbHid.Providers.HidSharp

A backend provider for Smdn.IO.UsbHid.* that utilizes the HIDSharp library for hardware communication.

It maps the common interfaces to HIDSharp's HidDevice and HidStream, and it supports standard DI registration via the AddHidSharpUsbHid extension methods. The implementation allows for the integration of Polly resilience pipelines for the endpoint opening process through the HidSharpUsbHidServiceBuilder. While adhering to the unified API, the underlying HidDevice instance remains accessible via the IUsbHidDevice.UnderlyingDevice property for library-specific operations.

Smdn.IO.UsbHid.Providers.LibUsbDotNet

NuGet Smdn.IO.UsbHid.Providers.LibUsbDotNet

A backend provider for Smdn.IO.UsbHid.* that utilizes the LibUsbDotNet library for hardware communication.

It offers specific configuration through LibUsbDotNetOptions, allowing users to define timeouts and buffer sizes for HID report transfers. The library maps IUsbHidDevice to UsbDevice and handles HID reports using LibUsbDotNet's endpoint reader and writer classes. Similar to other providers, it supports DI-based service registration and the configuration of Polly resilience pipelines for device access.

The LibUsbDotNet NuGet package does not include the native library libusb-1.0. To use this backend provider, you need to either install it on your system or manually copy it to the output directory of the executable. For more details, please refer to the LibUsbDotNet documentation.

Smdn.IO.UsbHid.Providers.LibUsbDotNetV3

NuGet Smdn.IO.UsbHid.Providers.LibUsbDotNetV3

A backend provider for Smdn.IO.UsbHid.* that utilizes the LibUsbDotNet version 3 library.

Smdn.IO.UsbHid.Providers.LibUsbDotNet uses the LibUsbDotNet, version 2, whereas Smdn.IO.UsbHid.Providers.LibUsbDotNetV3 uses the LibUsbDotNet, version 3.

Getting started and usage examples

More examples can be found in following examples directory.

Troubleshooting

DllImport resolving

LibUsbDotNet do DllImport-ing a shared library with the filename libusb-1.0.so.0.

If the libusb's .so filename installed on your system is different from that, use the NativeLibrary.SetDllImportResolver() to load installed .so file like below.

$ find /lib/ -name "libusb-*.so*"
/lib/x86_64-linux-gnu/libusb-1.0.so.x.y.z
/lib/i386-linux-gnu/libusb-1.0.so.x.y.z
using System.Runtime.InteropServices;

static void Main() {
  // libusb.so filename which is installed on your system
  const string fileNameLibUsb = "libusb-1.0.so.x.y.z";

  NativeLibrary.SetDllImportResolver(
    typeof(global::LibUsbDotNet.LibUsb.UsbDevice).Assembly,
    (libraryName, assembly, searchPath) => {
      if (string.Equals(libraryName, "libusb-1.0.so.0", StringComparison.OrdinalIgnoreCase)) {
        if (NativeLibrary.TryLoad(fileNameLibUsb, out var handleOfLibUsb))
          return handleOfLibUsb;
      }

      return IntPtr.Zero;
    }
  );

  // your codes here
    ︙
    ︙
}

For contributors

Contributions are appreciated!

If there's a feature you would like to add or a bug you would like to fix, please read Contribution guidelines and create an Issue or Pull Request.

IssueやPull Requestを送る際は、Contribution guidelinesをご覧頂ください。 可能なら英語が望ましいですが、日本語で構いません。

Notice

License

This project is licensed under the terms of the MIT License.

Credits

This project uses the following components. See ThirdPartyNotices.md for detail.

About

A .NET library providing a backend-agnostic API for USB HID device operations, enabling the selection of backends like HidSharp or LibUsbDotNet through dependency injection.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages