3 # measure_call_setup_time_from_android - call setup time from Android to PC
5 # Copyright (C) 2015 Antonio Ospite <ao2@ao2.it>
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.
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.
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/>.
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.
28 from savemysugar.cumulative_average import cumulative_average
29 from savemysugar.Modem import Modem
38 Ring.time = time.time()
39 raise StopIteration("Got the ring we were waiting for")
42 def measure_call_setup_time_from_android(incoming_port):
43 incoming_modem = Modem(incoming_port)
44 incoming_modem.register_callback("RING", on_ring)
46 min_call_setup_time = 10000
47 max_call_setup_time = -1
49 avg_call_setup_time = 0
51 subprocess.check_call("adb logcat -c".split())
52 logcat = subprocess.Popen("adb logcat -s SaveMySugar:*".split(),
53 stdout=subprocess.PIPE)
58 print("Call: %s" % (i + 1))
60 # When an outgoing call is placed, the SaveMySugar Android App
61 # outputs a line like this:
62 # D/SaveMySugar( 1604): 1451818183.802000 sendSymbol Dial and wait ...
63 for line in logcat.stdout:
64 line_str = line.decode('UTF-8')
65 if "sendSymbol Dial" in line_str:
66 dial_time = time.time()
69 # Wait for the receiver to get a ring
71 # flush to discard unprocessed RING notifications this is necessary
72 # in this case because we don't hang up the transmitter explicitly,
73 # so it may happen that it sends multiple rings for each call.
74 incoming_modem.flush()
75 incoming_modem.get_response_loop()
79 call_setup_time = Ring.time - dial_time
80 min_call_setup_time = min(min_call_setup_time, call_setup_time)
81 max_call_setup_time = max(max_call_setup_time, call_setup_time)
82 avg_call_setup_time = cumulative_average(avg_call_setup_time,
85 print("Call setup time: %f" % call_setup_time)
86 print("Min call setup time: %f" % min_call_setup_time)
87 print("Max call setup time: %f" % max_call_setup_time)
88 print("Average call setup time: %f" % avg_call_setup_time)
97 if len(sys.argv) != 2:
98 print("usage: %s" % sys.argv[0], "<incoming serial port>")
101 measure_call_setup_time_from_android(sys.argv[1])
104 if __name__ == "__main__":