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.
 
  25 from savemysugar.cumulative_average import cumulative_average
 
  26 from savemysugar.Modem import Modem
 
  37         Ring.time = time.time()
 
  38         raise StopIteration("Got the ring we were waiting for")
 
  41 def measure_call_setup_time_from_android(incoming_port):
 
  42     incoming_modem = Modem(incoming_port)
 
  43     incoming_modem.register_callback("RING", on_ring)
 
  45     min_call_setup_time = 10000
 
  46     max_call_setup_time = -1
 
  48     avg_call_setup_time = 0
 
  50     subprocess.check_call("adb logcat -c".split())
 
  51     logcat = subprocess.Popen("adb logcat -s SaveMySugar:*".split(),
 
  52                               stdout=subprocess.PIPE)
 
  57         print("Call: %s" % (i + 1))
 
  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()
 
  68         # Wait for the receiver to get a ring
 
  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()
 
  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,
 
  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)
 
  96     if len(sys.argv) != 2:
 
  97         print("usage: %s" % sys.argv[0], "<incoming serial port>")
 
 100     measure_call_setup_time_from_android(sys.argv[1])
 
 103 if __name__ == "__main__":