DA: Skip initializing failed_bssids list when eapol failure case
[platform/upstream/connman.git] / test / test-counter
1 #!/usr/bin/python
2
3 from gi.repository import GLib
4
5 import sys
6
7 import dbus
8 import dbus.service
9 import dbus.mainloop.glib
10
11 def make_bytes_readable(bytes):
12         SUFFIXES = [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ]
13         size = 1024
14
15         if bytes < size:
16                 return ''
17
18         for suffix in SUFFIXES:
19                 if bytes > size * 1024:
20                         size = size * 1024
21                         continue
22
23                 return '%.1f %s' % (bytes / float(size), suffix)
24
25         return ''
26
27 def print_stats(stats):
28         keys = list(stats.keys())
29         keys.sort()
30
31         for key in keys:
32                 val = int(stats[key])
33                 str = "    %s = %s" % (key, val)
34
35                 if key in ["RX.Bytes", "TX.Bytes"]:
36                         hstr = make_bytes_readable(val)
37                         if hstr:
38                                 str = "%s (%s)" % (str, hstr)
39
40                 print(str)
41
42 class Counter(dbus.service.Object):
43         @dbus.service.method("net.connman.Counter",
44                                 in_signature='', out_signature='')
45         def Release(self):
46                 print("Release")
47                 mainloop.quit()
48
49         @dbus.service.method("net.connman.Counter",
50                                 in_signature='oa{sv}a{sv}', out_signature='')
51         def Usage(self, path, home, roaming):
52                 print("%s" % (path))
53
54                 if len(home) > 0:
55                         print("  Home")
56                         print_stats(home)
57                 if len(roaming) > 0:
58                         print("  Roaming")
59                         print_stats(roaming)
60
61 if __name__ == '__main__':
62         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
63
64         bus = dbus.SystemBus()
65         manager = dbus.Interface(bus.get_object('net.connman', "/"),
66                                         'net.connman.Manager')
67
68         period = 2
69         if len(sys.argv) > 1:
70                 period = sys.argv[1]
71
72         path = "/test/counter%s" % period
73         object = Counter(bus, path)
74
75         manager.RegisterCounter(path, dbus.UInt32(10), dbus.UInt32(period))
76
77         mainloop = GLib.MainLoop()
78         mainloop.run()
79
80         #manager.UnregisterCounter(path)