Add configuration file for DBus access
[platform/upstream/connman.git] / test / monitor-vpn
1 #!/usr/bin/python
2
3 import gobject
4
5 import dbus
6 import dbus.mainloop.glib
7
8 from dbus.lowlevel import MethodCallMessage, HANDLER_RESULT_NOT_YET_HANDLED
9
10 def extract_list(list):
11         val = "["
12         for i in list:
13                 if type(i).__name__ == 'Dictionary':
14                         val += extract_values(i)
15                 elif type(i).__name__ == 'Struct':
16                         val += extract_list(i)
17                 else:
18                         val += " " + str(i)
19         val += "]"
20         return val
21
22 def extract_values(values):
23         val = "{"
24         for key in values.keys():
25                 val += " " + key + "="
26                 if key in ["ProtocolFamily"]:
27                         val += "%s" % (int(values[key]))
28                 elif key in ["IPv4", "IPv6"]:
29                         val += extract_values(values[key])
30                 else:
31                         val += str(values[key])
32         val += " }"
33         return val
34
35 def extract(name, value):
36         val = None
37
38         if name in ["Index"]:
39                 val = int(value)
40         elif name in ["IPv4", "IPv6" ]:
41                 val = extract_values(value)
42         elif name in ["UserRoutes", "ServerRoutes", "Nameservers"]:
43                 val = extract_list(value)
44         else:
45                 val = str(value)
46
47         return val
48
49 def property_changed(name, value, path, interface):
50         iface = interface[interface.rfind(".") + 1:]
51         val = extract(name, value)
52
53         print "{%s} [%s] %s = %s" % (iface, path, name, val)
54
55 def message_filter(connection, message):
56         if not isinstance(message, MethodCallMessage):
57                 return HANDLER_RESULT_NOT_YET_HANDLED
58
59         interface = message.get_interface()
60         path = message.get_path()
61         name = message.get_member()
62         args = message.get_args_list()
63
64         property_changed(name, args, path, interface)
65
66 if __name__ == '__main__':
67         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
68
69         bus = dbus.SystemBus()
70
71         bus.add_signal_receiver(property_changed,
72                                         bus_name="net.connman.vpn",
73                                         signal_name = "PropertyChanged",
74                                                 path_keyword="path",
75                                                 interface_keyword="interface")
76
77         bus.add_message_filter(message_filter)
78
79         mainloop = gobject.MainLoop()
80
81         mainloop.run()