DBus Interface Properties

How to get a list of available DBus interface properties?

I am writing a script that will track certain types of USB device connections. The way to distinguish the tracking connections from all usb connections, I think, is to check the properties of the signal interfaces that DBus sends over the USB connection. I would like to get a list of all such properties in order to select the appropriate one.

My code is:

    import sys
    import dbus
    from dbus.mainloop.glib import DBusGMainLoop
    import gobject

    def deviceAdded(udi):
        device = bus.get_object("org.freedesktop.Hal", udi)
        device_if = dbus.Interface(device, 'org.freedesktop.Hal.Device')
        if device_if.GetPropertyString('info.subsystem') == 'usb_device':
            #
            # Properties can be accesed like this:
            # vendor_id = device_if.GetPropertyInteger('usb_device.vendor_id')
            # 
            # how to get the list of all properties?
            #
            # do something

    def deviceRemoved(udi):
        # do something
        pass

    if __name__ == "__main__":
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()

    bus.add_signal_receiver( 
        deviceAdded,
        'DeviceAdded',
        'org.freedesktop.Hal.Manager',
        'org.freedesktop.Hal',
        '/org/freedesktop/Hal/Manager')

    bus.add_signal_receiver( 
        deviceRemoved,
        'DeviceRemoved',
        'org.freedesktop.Hal.Manager',
        'org.freedesktop.Hal',
        '/org/freedesktop/Hal/Manager')

    loop = gobject.MainLoop()

    try:
        loop.run()
    except KeyboardInterrupt:
        print "usb-device-tracker: keyboad interrupt received, shutting down"
        loop.quit()
        sys.exit(0)
+5
source share
3 answers

First of all, check the documentation and sources of documentation, they are always your friend.

import dbus
bus = dbus.SystemBus()
dev = bus.get_object("org.freedesktop.Hal", u'/org/freedesktop/Hal/devices/computer_logicaldev_input')
iface = dbus.Interface(dev, 'org.freedesktop.Hal.Device')
props = iface.GetAllProperties()
print "\n".join(("%s: %s" % (k, props[k]) for k in props))

As a last resort, you can always find the properties that interest you with the lshal command.

+2

GetAll org.freedesktop.DBus.Properties.

+2

I recently ran into the same problem (not Hal). I'm not sure if this is universal, but can (at least very often) be obtained via the interface org.freedesktop.DBus.Properties(as @daf suggested).

bus = dbus.SystemBus()
device = bus.get_object(...)

your_interface = 'org.freedesktop.Hal.Device' # for this example
props_iface = dbus.Interface(device, 'org.freedesktop.DBus.Properties')
properties = props_iface.GetAll(your_interface) #properties is a dbus.Dictionary
0
source

All Articles