monitor-iwd (3375B)
1 #!/usr/bin/python3 2 3 from gi.repository import GLib 4 5 import dbus 6 import dbus.mainloop.glib 7 8 _dbus2py = { 9 dbus.String : str, 10 dbus.UInt32 : int, 11 dbus.Int32 : int, 12 dbus.Int16 : int, 13 dbus.UInt16 : int, 14 dbus.UInt64 : int, 15 dbus.Int64 : int, 16 dbus.Byte : int, 17 dbus.Boolean : bool, 18 dbus.ByteArray : str, 19 dbus.ObjectPath : str 20 } 21 22 def dbus2py(d): 23 t = type(d) 24 if t in _dbus2py: 25 return _dbus2py[t](d) 26 if t is dbus.Dictionary: 27 return dict([(dbus2py(k), dbus2py(v)) for k, v in d.items()]) 28 if t is dbus.Array and d.signature == "y": 29 return "".join([chr(b) for b in d]) 30 if t is dbus.Array or t is list: 31 return [dbus2py(v) for v in d] 32 if t is dbus.Struct or t is tuple: 33 return tuple([dbus2py(v) for v in d]) 34 return d 35 36 def pretty(d): 37 d = dbus2py(d) 38 t = type(d) 39 40 if t in (dict, tuple, list) and len(d) > 0: 41 if t is dict: 42 d = ", ".join(["%s = %s" % (k, pretty(v)) for k, v in d.items()]) 43 return "{ %s }" % d 44 45 d = " ".join([pretty(e) for e in d]) 46 47 if t is tuple: 48 return "( %s )" % d 49 50 if t is str: 51 return "%s" % d 52 53 return str(d) 54 55 def properties_changed(interface, changed, invalidated, path): 56 iface = interface[interface.rfind(".") + 1:] 57 for name, value in changed.items(): 58 print("{%s} [%s] %s = %s" % (iface, path, name, pretty(value))) 59 60 relevant_ifaces = [ "net.connman.iwd.Device", 61 "net.connman.iwd.Adapter", 62 "net.connman.iwd.SimpleConfiguration", 63 "net.connman.iwd.AccessPoint", 64 "net.connman.iwd.AdHoc", 65 "net.connman.iwd.Network", 66 "net.connman.iwd.AgentManager", 67 "net.connman.iwd.KnownNetwork", 68 "net.connman.iwd.Station", 69 "net.connman.iwd.AccessPointDiagnostic", 70 "net.connman.iwd.StationDiagnostic"] 71 72 def interfaces_added(path, interfaces): 73 for iface, props in interfaces.items(): 74 if not(iface in relevant_ifaces): 75 continue 76 77 print("{Added %s} [%s]" % (iface, path)) 78 79 for name, value in props.items(): 80 print(" %s = %s" % (name, pretty(value))) 81 82 def interfaces_removed(path, interfaces): 83 for iface in interfaces: 84 if not(iface in relevant_ifaces): 85 continue 86 87 print("{Removed %s} [%s]" % (iface, path)) 88 89 if __name__ == '__main__': 90 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 91 92 bus = dbus.SystemBus() 93 94 bus.add_signal_receiver(properties_changed, 95 bus_name="net.connman.iwd", 96 dbus_interface="org.freedesktop.DBus.Properties", 97 signal_name="PropertiesChanged", 98 path_keyword="path") 99 100 bus.add_signal_receiver(interfaces_added, bus_name="net.connman.iwd", 101 dbus_interface="org.freedesktop.DBus.ObjectManager", 102 signal_name="InterfacesAdded") 103 104 bus.add_signal_receiver(interfaces_removed, bus_name="net.connman.iwd", 105 dbus_interface="org.freedesktop.DBus.ObjectManager", 106 signal_name="InterfacesRemoved") 107 108 mainloop = GLib.MainLoop() 109 mainloop.run()