问题描述:

I need detect an usb device when it had been plugged and unplugged, and I write a python programme with dbus.

But it is very odd the device would be mounted three times at least when it is plugged or unplugged

the monitor code is following:

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

"org.freedesktop.Hal.Device")

self.notify_message(device.GetProperty("info.udi"))

then we catch the output when I try to insert an usb device(eg. 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

so, the notify shows three times everytime it was plugged or unplugged

how to show only once notification?

网友答案:

I am not familiar with dbus, but look at the device names you are getting:

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 Interface 0 of said device. The third device probably represents an endpoint or some other feature of Interface 0 which may or may not be specified in the device's descriptors.

You get three different logical devices even though only plugged in one physical device. This sort of thing is important for people who implement composite USB devices.

To answer the question, though: If you only want to be notified once, then in your notification handler function you should filter out the notifications you don't care about by looking at the device name string and deciding whether you care about the event or not. For example, you might decide that you don't care about devices with if0 in the name so your pseudo code would be:

def notificationHandler(notification)
  if notification.name does not contain `if0`
    pass notification to higher level code
  end
end
相关阅读:
Top