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
|
#!/usr/bin/python
import sys
import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
def make_bytes_readable(bytes):
SUFFIXES = [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ]
size = 1024
if bytes < size:
return ''
for suffix in SUFFIXES:
if bytes > size * 1024:
size = size * 1024
continue
return '%.1f %s' % (bytes / float(size), suffix)
return ''
def print_stats(stats):
keys = list(stats.keys())
keys.sort()
for key in keys:
val = int(stats[key])
str = " %s = %s" % (key, val)
if key in ["RX.Bytes", "TX.Bytes"]:
hstr = make_bytes_readable(val)
if hstr:
str = "%s (%s)" % (str, hstr)
print(str)
class Counter(dbus.service.Object):
@dbus.service.method("net.connman.Counter",
in_signature='', out_signature='')
def Release(self):
print("Release")
mainloop.quit()
@dbus.service.method("net.connman.Counter",
in_signature='oa{sv}a{sv}', out_signature='')
def Usage(self, path, home, roaming):
print("%s" % (path))
if len(home) > 0:
print(" Home")
print_stats(home)
if len(roaming) > 0:
print(" Roaming")
print_stats(roaming)
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object('net.connman', "/"),
'net.connman.Manager')
period = 2
if len(sys.argv) > 1:
period = sys.argv[1]
path = "/test/counter%s" % period
object = Counter(bus, path)
manager.RegisterCounter(path, dbus.UInt32(10), dbus.UInt32(period))
mainloop = gobject.MainLoop()
mainloop.run()
#manager.UnregisterCounter(path)
|