3 # measure_call_setup_time_to_android - call setup time from PC to Android
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 from savemysugar.cumulative_average import cumulative_average
21 from savemysugar.Modem import Modem
26 def measure_call_setup_time_to_android(outgoing_port, destination_number):
27 outgoing_modem = Modem(outgoing_port)
31 min_call_setup_time = 10000
32 max_call_setup_time = -1
34 avg_call_setup_time = 0
36 subprocess.check_call("adb logcat -c".split())
37 logcat = subprocess.Popen("adb logcat -s SaveMySugar:*".split(),
38 stdout=subprocess.PIPE)
42 print("Call: %s" % (i + 1))
43 outgoing_modem.send_command("ATDT" + destination_number + ";")
44 dial_time = time.time()
46 # Wait for the receiver to get a ring before terminating the call.
48 # When an incoming ring is detected, the SaveMySugar Android App
49 # outputs a line like this:
50 # D/SaveMySugar( 5555): 1450468488.997000 CALL_STATE_RINGING +39XX...
51 for line in logcat.stdout:
52 line_str = line.decode('UTF-8')
53 if "CALL_STATE_RINGING" in line_str:
54 ring_time = time.time()
57 outgoing_modem.send_command("ATH")
58 outgoing_modem.get_response()
60 hangup_time = time.time()
62 call_setup_time = ring_time - dial_time
63 min_call_setup_time = min(min_call_setup_time, call_setup_time)
64 max_call_setup_time = max(max_call_setup_time, call_setup_time)
65 avg_call_setup_time = cumulative_average(avg_call_setup_time,
68 print("Call setup time: %f" % call_setup_time)
69 print("Min call setup time: %f" % min_call_setup_time)
70 print("Max call setup time: %f" % max_call_setup_time)
71 print("Average call setup time: %f" % avg_call_setup_time)
74 call_time = hangup_time - dial_time
75 min_call_time = min(min_call_time, call_time)
76 max_call_time = max(max_call_time, call_time)
77 print("Total call time: %f" % call_time)
78 print("Min call time: %f" % min_call_time)
79 print("Max call time: %f" % max_call_time)
84 # When dialing with an analog modem I noticed that if calls are
85 # separated one from another they take less time to set up, this
86 # may be due to how an analog modem works: getting it on-hook and
87 # off-hook takes quite some time.
88 inter_call_sleep = 5.2
89 print("Sleeping %.2f sec before the next call..." % inter_call_sleep)
90 time.sleep(inter_call_sleep)
95 if len(sys.argv) != 3:
96 print("usage: %s" % sys.argv[0],
97 "<outgoing serial port>", "<destination number>")
100 measure_call_setup_time_to_android(sys.argv[1], sys.argv[2])
103 if __name__ == "__main__":