USB device discovery problem

I need to detect a USB device when it was plugged in and unplugged, and I am writing a python program with dbus.

But it is very strange that the device will be installed three times, at least when it is connected or disconnected.

monitor code is displayed:

device = dbus.Interface(self.bus.get_object("org.freedesktop.Hal", udi), "org.freedesktop.Hal.Device") self.notify_message(device.GetProperty("info.udi")) 

then we will catch the output when I try to insert a USB device (for example, a keyboard)

 Mon Jul 4 03:47:31 2011 /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial Mon Jul 4 03:47:31 2011 /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial_if0 Mon Jul 4 03:47:31 2011 /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial_if0_logicaldev_input 

therefore, the notification notifies you three times each time it was connected or disconnected, how to show only one notification?

+4
source share
1 answer

I am not familiar with dbus, but look at the names of the devices you get:

 usb_device_413c_2003_noserial usb_device_413c_2003_noserial_if0 usb_device_413c_2003_noserial_if0_logicaldev_input 

The first device probably represents the USB device as a whole. The second device most likely represents the interface 0 of the specified device. The third device is probably an endpoint or some other function of interface 0, which may or may not be specified in device descriptors.

You get three different logical devices, although only connected to one physical device. Such things are important for people who implement composite USB devices.

To answer the question, though: if you want to receive notifications only once, then in the notification handler function, you should filter out notifications that you do not need if you look at the device name bar and decide whether you care about events or not. For example, you may decide that you do not need devices with if0 in the name so that your pseudo-code is:

 def notificationHandler(notification) if notification.name does not contain `if0` pass notification to higher level code end end 
0
source

All Articles