a862fead24441c0800e85b67767f5c990907e37b
[SaveMySugar/python3-savemysugar.git] / src / measure_call_setup_time_to_android.py
1 #!/usr/bin/env python3
2 #
3 # measure_call_setup_time_to_android - call setup time from PC to Android
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 from savemysugar.cumulative_average import cumulative_average
21 from savemysugar.Modem import Modem
22 import subprocess
23 import time
24
25
26 def measure_call_setup_time_to_android(outgoing_port, destination_number):
27     outgoing_modem = Modem(outgoing_port)
28
29     min_call_time = 10000
30     max_call_time = -1
31     min_call_setup_time = 10000
32     max_call_setup_time = -1
33
34     avg_call_setup_time = 0
35
36     subprocess.check_call("adb logcat -c".split())
37     logcat = subprocess.Popen("adb logcat -s SaveMySugar:*".split(),
38                               stdout=subprocess.PIPE)
39
40     for i in range(10):
41         print()
42         print("Call: %s" % (i + 1))
43         outgoing_modem.send_command("ATDT" + destination_number + ";")
44         dial_time = time.time()
45
46         # Wait for the receiver to get a ring before terminating the call.
47         #
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()
55                 break
56
57         outgoing_modem.send_command("ATH")
58         outgoing_modem.get_response()
59
60         hangup_time = time.time()
61
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,
66                                                  i + 1,
67                                                  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)
72         print()
73
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)
80         print()
81
82         ring_time = -1
83
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)
91
92
93 def main():
94     import sys
95     if len(sys.argv) != 3:
96         print("usage: %s" % sys.argv[0],
97               "<outgoing serial port>", "<destination number>")
98         sys.exit(1)
99
100     measure_call_setup_time_to_android(sys.argv[1], sys.argv[2])
101
102
103 if __name__ == "__main__":
104     main()