Search code examples
pythondbus

D-Bus method GetAll not found?


As far as I'm aware, org.freedesktop.DBus.Properties.GetAll should work to get properties of an interface. For some reason, this doesn't seem to work on org.freedesktop.NetworkManager.Connections.Active. Any suggestions on how to make this code work?


The code:

import dbus
from gi.repository import GObject
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
loop = GObject.MainLoop()


def handle_nm_change(o):
    if 'ActiveConnections' in o:
        # This is a connection change, let's see if it's in our SSID list
        # First, get the ActiveConnection that changed:
        for c in o['ActiveConnections']:
            # Get the active connection
            dev = system_bus.get_object('org.freedesktop.NetworkManager', c)
            # Attempt to get the properties of the connection.
            devprops_iface = dbus.Interface(dev, dbus_interface='org.freedesktop.DBus.Properties')
            devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
            # if not devprops['Default']:
            #     ii = input('Device not default: ' + c)
            #     if ii == 'n':
            #         exit(0)
            appath = devprops['SpecificObject']
            if appath.startswith('/org/freedesktop/NetworkManager/AccessPoint'):
                ap = system_bus.get_object('org.freedesktop.NetworkManager', appath)
                ssid = ap.Get('org.freedesktop.NetworkManager.AccessPoint', 'Ssid',
                              dbus_interface=dbus.PROPERTIES_IFACE
                              )
                print(ssid)


if __name__ == '__main__':
    system_bus.add_signal_receiver(handle_nm_change,
                                   'PropertiesChanged',
                                   'org.freedesktop.NetworkManager'
                                   )
    loop.run()

The error:

ERROR:dbus.connection:Exception in handler for D-Bus signal:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 230, in maybe_handle_message
    self._handler(*args, **kwargs)
  File "so-mockup.py", line 18, in handle_nm_change
    devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist

Solution

  • Signal 'PropertiesChanged' is sent also when a connection is deactivated. Then the object path for the "deactivated" connection does not exist anymore. That's why you are receiving the UnknownMethod exception.

    Before getting properties of the ActiveConnection make sure it still exists. Try the changes below:

        # Get ActiveConnection upon receiving a PropertiesChanged signal
        nm = system_bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
        nm_iface = dbus.Interface(nm, dbus_interface='org.freedesktop.DBus.Properties')
        nms = nm_iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
    
        # Check current active connections
        for ac in nms:
                print("ActiveConnection:  "+ac)
    
        # This is a connection change, let's see if it's in our SSID list
        # First, get the ActiveConnection that changed:
        for c in o['ActiveConnections']:
        # Do whatever if c is in "ActiveConnections"
            if c in nms: