Add a program to measure the call setup time from Android to PC
[SaveMySugar/python3-savemysugar.git] / src / measure_call_setup_time_from_android.py
1 #!/usr/bin/env python3
2 #
3 # measure_call_setup_time_from_android - call setup time from Android to PC
4 #
5 # Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # NOTE: since there is no easy way to hangup an Android call from this program,
21 # so the suggested way to use it is to set an high value for the "Maximum call
22 # setup time" (e.g. 17) and then send a message from the Android app while
23 # measuring the times with this program.
24
25 from savemysugar.cumulative_average import cumulative_average
26 from savemysugar.Modem import Modem
27 import subprocess
28 import time
29
30
31 class Ring(object):
32     time = -1
33
34
35 def on_ring():
36     if Ring.time == -1:
37         Ring.time = time.time()
38         raise StopIteration("Got the ring we were waiting for")
39
40
41 def measure_call_setup_time_from_android(incoming_port):
42     incoming_modem = Modem(incoming_port)
43     incoming_modem.register_callback("RING", on_ring)
44
45     min_call_setup_time = 10000
46     max_call_setup_time = -1
47
48     avg_call_setup_time = 0
49
50     subprocess.check_call("adb logcat -c".split())
51     logcat = subprocess.Popen("adb logcat -s SaveMySugar:*".split(),
52                               stdout=subprocess.PIPE)
53
54     i = 0
55     while True:
56         print()
57         print("Call: %s" % (i + 1))
58
59         # When an outgoing call is placed, the SaveMySugar Android App
60         # outputs a line like this:
61         # D/SaveMySugar( 1604): 1451818183.802000 sendSymbol Dial and wait ...
62         for line in logcat.stdout:
63             line_str = line.decode('UTF-8')
64             if "sendSymbol Dial" in line_str:
65                 dial_time = time.time()
66                 break
67
68         # Wait for the receiver to get a ring
69         try:
70             # flush to discard unprocessed RING notifications this is necessary
71             # in this case because we don't hang up the transmitter explicitly,
72             # so it may happen that it sends multiple rings for each call.
73             incoming_modem.flush()
74             incoming_modem.get_response_loop()
75         except StopIteration:
76             pass
77
78         call_setup_time = Ring.time - dial_time
79         min_call_setup_time = min(min_call_setup_time, call_setup_time)
80         max_call_setup_time = max(max_call_setup_time, call_setup_time)
81         avg_call_setup_time = cumulative_average(avg_call_setup_time,
82                                                  i + 1,
83                                                  call_setup_time)
84         print("Call setup time: %f" % call_setup_time)
85         print("Min call setup time: %f" % min_call_setup_time)
86         print("Max call setup time: %f" % max_call_setup_time)
87         print("Average call setup time: %f" % avg_call_setup_time)
88         print()
89
90         Ring.time = -1
91         i += 1
92
93
94 def main():
95     import sys
96     if len(sys.argv) != 2:
97         print("usage: %s" % sys.argv[0], "<incoming serial port>")
98         sys.exit(1)
99
100     measure_call_setup_time_from_android(sys.argv[1])
101
102
103 if __name__ == "__main__":
104     main()