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.
You can use libudev
or parse udevadm
output as @Ambroz Bizjak suggested. Although, I advise against adding an additional process (stdbuf
) and language (NCD
), just to parse udevadm's output.
A step between plain libudev and parsing output is modifying the udevadm sources. This solution reduces the needed resources and skips the parsing process altogether. When you look at the udev package, you will find the sources for udevd and udevadm in the udev
directory.
There, you have the main routine in udevadm.c
and the source for udevadm monitor
in udevadm-monitor.c
. Every event received will be printed through print_device()
. This is where you insert your code.
If you're tight on memory, you can strip off unneeded code for control
, info
, settle
, test-builtin
, test
and trigger
. On my system (Ubuntu 12.04), this reduces the size of udevadm by about 75%.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With