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