Cross-platform USB with ASP.NET Core

Overview

I ported the web application to .NET Core, and I found that it is able to communicate with the microcontroller via USB when working on Windows, but not when working on Linux. I'm trying to work:

  • Why is this so (is this a bug in the libraries I use, or did I misconfigure the Linux environment?)

  • How can I enable or work around it (alternative libraries, changes in the environment, etc.)

If someone managed to create an application based on .NET Core that can communicate via USB in an agnostic platform, I would be very grateful for your input.

Environment

For reference in the remainder of this post, these are some details about my current environment:

  • Windows 7 64-bit host machine
  • Dotnet Version 1.0.0-preview2-003131
  • Docker version 1.12.0, build 8eab29e
  • Docker Toolbox (I believe v1.12.3)
  • Virtualbox v5.0.30 r112061

More details

I used to have a .NET Framework -based web service that I used to control the microcontroller. Based on the .NET Framework, it was limited to work on Windows. To get cross-platform support, I started porting the application to .NET Core . This task is now more or less complete - for example, now I can create and run my application as a Docker service, which, it seems to me, will be of great benefit.

My application is divided into several projects, but for the purposes of this post, I only address two:

  • An ASP.NET Core Web service project that adapts the core functions of an application to control through web communication. 1 It binds everything else together and is what I actually execute using dotnet <<application.dll>> .

  • A USB communication service project that handles communication with my USB device. 2 He currently uses the CoreCompat.LibUsbDotNet library (v2.2.8-r101), which acts as a cross-platform C # adapter through the WinUSB and libusb libraries for Windows and Unix, respectively. This project is distributed as a Nuget package that is consumed by a web service, and that is the problem.

The application works fine when I run it directly under Windows. However, if I try to run it on a Linux virtual machine or as a Docker service, the web service cannot be initialized with an error stating that libusb-1.0 library could not be found. 3

Based on this error message, I tried to check the environment of both the Linux virtual machines and the Docker containers on which I tried to run the application. 4

After installing my USB device in the Docker virtual machine and launching the container based on my image of the web application in privileged mode, I can confirm that it sees my USB device:

 root@19e8929e1814 :/app# lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 002: ID 2b87:0001 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 

I also confirm that libusb-1.0 installed 5 and checks that it is accessible from the ldconfig cache:

 root@19e8929e1814 :/app# ldconfig -p | grep libusb libusb-1.0.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-1.0.so.0 libusb-1.0.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-1.0.so libusb-0.1.so.4 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-0.1.so.4 libusb-0.1.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4 

Based on this, I am not sure why CoreCompat.LibUsbDotNet throws an exception due to the inability to find libusb-1.0 and can only assume that this is an error in the library.

Has anyone managed to successfully use this library to communicate in a Unix environment? Alternatively, did anyone find another way to communicate with USB devices in the agnostic platform for the .NET Core application?

References

[1] project.json for a web service project

 { "dependencies": { "<<Company>>.Communications.<<Product>>Usb": "0.4.9", "<<Company>>.<<Product>>Web.Core": { "target": "project" }, "<<Company>>.<<Product>>WebComponentPackage": "0.4.9-beta0002", "Autofac": "4.2.1", "Common.Logging": "3.4.0-Beta2", "Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "Microsoft.AspNetCore.StaticFiles": "1.1.0", "Microsoft.AspNetCore.WebSockets.Server": "0.1.0", "Microsoft.Extensions.DependencyModel": "1.1.0-preview1-001100", "Microsoft.Extensions.Logging.Console": "1.1.0", "Microsoft.NETCore.App": { "version": "1.1.0", "type": "platform" }, "Serilog.Enrichers.Thread": "3.0.0", "Serilog.Sinks.Literate": "2.1.0-dev-00034", "Serilog.Sinks.RollingFile": "3.3.0", "System.Runtime.Loader": "4.3.0", "Thrower": "3.0.4" }, "tools": { }, "frameworks": { "netcoreapp1.1": { "imports": [ "dotnet5.6", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, "publishOptions": { "include": [ "wwwroot", "web.config" ] }, "scripts": { "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } } 

[2] project.json for a USB communication project

 { "version": "0.4.9-*", "description": "Provides a type of communication service that facilitates communication between .NET applications and <<Product>> over USB.", "authors": ["<<Author>>"], "dependencies": { "Microsoft.Composition": "1.0.30", "Microsoft.NETCore.Portable.Compatibility": "1.0.1", "Thrower": "3.0.4", "CoreCompat.LibUsbDotNet": "2.2.8-r101", "Common.Logging": "3.4.0-Beta2", "<<Company>>.Message": "0.4.3", "NETStandard.Library": "1.6.1", "<<Company>>.Communications.Core": "0.4.5", "Serilog": "2.3.0" }, "packOptions": { "owners": ["<<Company>>"], "repository": { "type": "git", "url": "https://bitbucket.org/<<Company>>-dev/<<Product>>usb" } }, "frameworks": { "netstandard1.4": { "imports": [ "dnxcore50", "portable-net45+win8" ] } }, "buildOptions": { "xmlDoc": true } } 

[3] USB communication failure exception

 Unhandled Exception: System.DllNotFoundException: libusb-1.0 library not found. This is often an indication that libusb-1.0 was installed to '/usr/local/lib' and mono.net is not looking for it there. To resolve this, add the path '/usr/local/lib' to '/etc/ld.so.conf' and run 'ldconfig' as root. (http://www.mono-project.co m/DllNotFoundException) ---> System.DllNotFoundException: Unable to load DLL 'libusb-1.0.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at MonoLibUsb.MonoUsbApi.Init(IntPtr& pContext) at MonoLibUsb.MonoUsbSessionHandle..ctor() --- End of inner exception stack trace --- at MonoLibUsb.MonoUsbSessionHandle..ctor() at MonoLibUsb.MonoUsbEventHandler.Init(UnixNativeTimeval unixNativeTimeval) at MonoLibUsb.MonoUsbEventHandler.Init() at MonoLibUsb.MonoUsbApi.InitAndStart() at LibUsbDotNet.LudnMonoLibUsb.MonoUsbDevice.get_MonoUsbDeviceList() at LibUsbDotNet.Main.LegacyUsbRegistry.get_DeviceList() at LibUsbDotNet.UsbDevice.get_AllLibUsbDevices() at LibUsbDotNet.UsbDevice.get_AllDevices() at LibUsbDotNet.UsbDevice.OpenUsbDevice(Predicate`1 findDevicePredicate) at LibUsbDotNet.UsbDevice.OpenUsbDevice(UsbDeviceFinder usbDeviceFinder) at <<Company>>.Communications.<<Product>>Usb.UsbCommunicationService.Start() at <<Company>>.<<Product>>Web.Core.DeviceController.Initialize() at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Startup.ConfigureServices(IServiceCollection services) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Program.Main(String[] args) 

[4] Dockerfile Web Service

 FROM microsoft/aspnetcore:1.1.0 ENTRYPOINT ["dotnet", "bin/Debug/netcoreapp1.1/publish/<<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.dll"] ARG source=. WORKDIR /app EXPOSE 80 COPY $source . RUN apt-get update RUN apt-get install -y libusb-1.0-0-dev usbutils 

[5] Output of find / -name libusb* in the Docker container

 root@19e8929e1814 :/app# find / -name libusb* /lib/x86_64-linux-gnu/libusb-1.0.so.0 /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0 /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4 /lib/x86_64-linux-gnu/libusb-0.1.so.4 /usr/lib/x86_64-linux-gnu/libusb-1.0.a /usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc /usr/lib/x86_64-linux-gnu/libusb-1.0.so /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4 /usr/include/libusb-1.0 /usr/include/libusb-1.0/libusb.h /usr/share/doc/libusb-1.0-0 /usr/share/doc/libusb-1.0-doc /usr/share/doc/libusb-1.0-doc/html/libusb_8h_source.html /usr/share/doc/libusb-1.0-0-dev /usr/share/doc/libusb-0.1-4 /usr/share/doc-base/libusb-1.0-doc /var/lib/dpkg/info/libusb-1.0-0-dev:amd64.md5sums /var/lib/dpkg/info/libusb-1.0-doc.list /var/lib/dpkg/info/libusb-1.0-0:amd64.md5sums /var/lib/dpkg/info/libusb-1.0-0:amd64.symbols /var/lib/dpkg/info/libusb-1.0-0:amd64.shlibs /var/lib/dpkg/info/libusb-1.0-0:amd64.list /var/lib/dpkg/info/libusb-1.0-0:amd64.postrm /var/lib/dpkg/info/libusb-1.0-0:amd64.postinst /var/lib/dpkg/info/libusb-1.0-doc.md5sums /var/lib/dpkg/info/libusb-1.0-0-dev:amd64.list /var/lib/dpkg/info/libusb-0.1-4:amd64.postrm /var/lib/dpkg/info/libusb-0.1-4:amd64.shlibs /var/lib/dpkg/info/libusb-0.1-4:amd64.symbols /var/lib/dpkg/info/libusb-0.1-4:amd64.postinst /var/lib/dpkg/info/libusb-0.1-4:amd64.list /var/lib/dpkg/info/libusb-0.1-4:amd64.md5sums 
+6
source share
1 answer

Associate the actual shared library with the location of the shared .Net Core libraries. I managed to load the library with:

 ln -s /lib/x86_64-linux-gnu/libusb-1.0.so.0 /opt/dotnet/shared/Microsoft.NETCore.App/1.1.0/libusb-1.0.dll 

I have not yet received a list of devices.

See this issue on Github .

+8
source

Source: https://habr.com/ru/post/1014007/


All Articles