summaryrefslogtreecommitdiff
path: root/test/monitor-connman
blob: 4d44519a1219db246352f26e5558516e75680c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python

import gobject

import dbus
import dbus.mainloop.glib

from dbus.lowlevel import MethodCallMessage, HANDLER_RESULT_NOT_YET_HANDLED

def extract_list(list):
	val = "["
	for i in list:
		if type(i).__name__ == 'Dictionary':
			val = extract_values(i)
		else:
			val += " " + str(i)
	val += " ]"
	return val

def extract_values(values):
	val = "{"
	for key in values.keys():
		val += " " + key + "="
		if key in ["PrefixLength"]:
			val += "%s" % (int(values[key]))
		elif key in ["IPv4", "IPv6", "Ethernet" ]:
			val += extract_values(values[key])
		elif key in [ "AllowedBearers" ]:
			val += extract_list(values[key])
		else:
			if key in ["Servers", "Excludes"]:
				val += extract_list(values[key])
			else:
				val += str(values[key])
	val += " }"
	return val

def extract(name, value):
	val = None

	if name in ["Strength", "Priority"]:
		val = int(value)
	elif name in ["IPv4", "IPv6", "Ethernet", "Proxy" ]:
		val = extract_values(value)
	elif name in ["Nameservers", "Domains", "Services",
		      "Update", "Technologies", "AvailableTechnologies",
		      "EnabledTechnologies", "ConnectedTechnologies" ]:
		val = extract_list(value)
	else:
		val = str(value)

	return val

def property_changed(name, value, path, interface):
	iface = interface[interface.rfind(".") + 1:]
	val = extract(name, value)

	print "{%s} [%s] %s = %s" % (iface, path, name, val)

def message_filter(connection, message):
	if not isinstance(message, MethodCallMessage):
		return HANDLER_RESULT_NOT_YET_HANDLED

	interface = message.get_interface()
	path = message.get_path()
	name = message.get_member()
	args = message.get_args_list()

	property_changed(name, args, path, interface)

if __name__ == '__main__':
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()

	bus.add_signal_receiver(property_changed,
					bus_name="net.connman",
					signal_name = "PropertyChanged",
						path_keyword="path",
						interface_keyword="interface")

	bus.add_match_string("member=Change,interface=net.connman.Session")
	bus.add_match_string("member=Update,interface=net.connman.Notification")
	bus.add_message_filter(message_filter)

	mainloop = gobject.MainLoop()

	mainloop.run()