Hello I am working on an embedded linux device with a usb port that uses the g_ether driver for usb networking.
When the usb plug is connected the dmesg output is:
g_ether gadget: full speed config #2: RNDIS
When the usb cable is unplugged no message is written to dmesg.
Using C how can I listen for the connect/disconnect events?
The embedded linux OS does not have any extras. There is no dbus daemon or hotplug helper script. I am not even sure if these would of been helpful.
If you want everything in your single process, you’ll have to use libudev to either get events from
udevdor directly from the kernel.Seeing that it might be a problem to use libudev in your application (lack of documentation?), an alternative is to use the udevadm program, which can:
udevd(udevadm monitor --udev --property),udevadm monitor --kernel --property), andudevadm info --query all --export-db)udevadmis part of the udev package, but shouldn’t needudevdif you only use it to report kernel events. You can use it by having your process spawn it and parse its standard output (but you’ll have to launch it via stdbuf-o L).Either way, it’ll probably be a lot of work. I’ve already implemented a lot of this in my NCD programming language, including monitoring of USB devices. You might want to take a look at NCD; it’s useful for a lot of configuration tasks, and handles hotplugging well. For example, this NCD program will print USB device events to standard output:
This will make NCD print something like that (with an initial
addedevent for any USB device that was already plugged in):You can also use NCD just for this, and parse this standard output – which is much easier to work with than messing with udevadm directly.
Note that NCD itself uses
udevadm, and it does require udevd to be running; but why is that a problem anyway? (with some work this dependency could be removed)