Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get device path using libusb in Linux

Tags:

c#

linux

usb

libusb

I was looking for a cross platform way of getting usb arrival and removal events in C# and i found "LibUsbDotNet C# USB Library" (http://sourceforge.net/projects/libusbdotnet/?source=navbar).

It works as it should but in Linux it seems that i can't get device mount point (path). In Linux it uses "libusb" library which does not have a method for getting the device path.

Here is a simple code sample that detects the device events:

internal class DeviceNotification
{
    public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();

    private static void Main(string[] args)
    {
        // Hook the device notifier event
        UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;

        // Exit on and key pressed.
        Console.Clear();            
        Console.WriteLine();
        Console.WriteLine("Waiting for system level device events..");
        Console.Write("[Press any key to exit]");

        while (!Console.KeyAvailable)
            Application.DoEvents();

        UsbDeviceNotifier.Enabled = false;  // Disable the device notifier

        // Unhook the device notifier event
        UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
    }

    private static void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
    {
        // A Device system-level event has occured

        Console.SetCursorPosition(0,Console.CursorTop);

        Console.WriteLine(e.ToString()); // Dump the event info to output.

        Console.WriteLine();
        Console.Write("[Press any key to exit]");
    }
}

and here is a sample of the output:

[DeviceType:DeviceInterface] [EventType:DeviceArrival] Name:usbdev1.17 BusNumber:1 DeviceAddress:17 Length:18 DescriptorType:Device BcdUsb:0x0200 Class:PerInterface SubClass:0x00 Protocol:0x00 MaxPacketSize0:64 VendorID:0x059F ProductID:0x1014 BcdDevice:0x0000 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1

[Press any key to exit][DeviceType:DeviceInterface] [EventType:DeviceRemoveComplete] Name:usbdev1.17 BusNumber:1 DeviceAddress:17 Length:18 DescriptorType:Device BcdUsb:0x0200 Class:PerInterface SubClass:0x00 Protocol:0x00 MaxPacketSize0:64 VendorID:0x059F ProductID:0x1014 BcdDevice:0x0000 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1

My question is how can I get the path of the device attached or removed, or how can I bind the info returned by libusb with an actual device path?

like image 904
LAS Avatar asked Nov 12 '22 11:11

LAS


1 Answers

You need to use UDev instead of libusb. Libusb merely tells you about what USB devices are on the system, but won't tell you anything about where they're mounted. UDev handles mounting them.

There is libudev and the documentation should be here: http://www.freedesktop.org/software/systemd/libudev/ but it appears to be down at the moment. Here's a tutorial on libudev: Tutorial: How to use libudev and SysFS in Linux

There is also a GLib based wrapper for libudev, docs here: http://ftp.osuosl.org/pub/linux/utils/kernel/hotplug/gudev/ and there appears to be a c# wrapper for libgudev.

But finally you may find using GLib's GIO easier than getting down to the udev level: Volumes and Drives API reference.

like image 98
iain Avatar answered Nov 15 '22 04:11

iain