#!/usr/bin/python3
#
#  oFono - Open Source Telephony - RIL Modem test
#
#  Copyright (C) 2014 Canonical Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# This test ensures that basic modem information is available
# when the modem is online and has a valid, unlocked SIM present.
#
# *** REQUIRED SETUP ***
#
# 1. /etc/host:
# This script uses telnetlib to access the emulator console.  In
# order for this to work, the emulator needs to be configured so
# that it can access 'localhost' on the host machine.  This is 
# done by modifying /etc/hosts in the emulator to define 'localhost'
# as 10.0.2.2, which is a special alias to the host loopback interface.
# Without this change, it's impossible for this script to access the
# emulator console.
#
# 2. Modem online
# This script must be run *after* the modem has been brought online,
# otherwise it will fail.  This is currently done by telepathy-ofono,
# which isn't being started in the current touch emulator, however it
# will soon be the responsibility of urfkill.
#
#  *** TODO ***
#
# 1. This test calls HangupAll() which is an asynchronous operation.
# There's no getting around that fact that a timer needs to be used
# to delay querying the calls after the hangup.  The code currently
# uses a time.sleep(5), however it could be changed to listen for a
# CallRemoved signal.  A timer would still be required to catch the
# scenario where the signal was never received.

import dbus
import sys
import time
import telnetlib

def create_incoming_call(number, port):

	tn = telnetlib.Telnet("localhost", 5554)
	tn.read_until("OK")

	tn.write("gsm call " + number + "\n")
	print("incoming call created")

	tn.read_until("OK")
	tn.write("exit")

if __name__ == "__main__":

	if len(sys.argv) == 2:
		port = sys.argv[1]
	else:
		port = 5554

	create_incoming_call("6663334444", port)

	bus = dbus.SystemBus()

	manager = dbus.Interface(bus.get_object('org.ofono', '/'),
				'org.ofono.Manager')
	modems = manager.GetModems()

	for path, properties in modems:
		print("[ %s ]" % (path))

		assert "org.ofono.VoiceCallManager" in properties["Interfaces"]

		mgr = dbus.Interface(bus.get_object('org.ofono', path),
				'org.ofono.VoiceCallManager')
		calls = mgr.GetCalls()

		assert len(calls) > 0, "No incoming call present!"

		mgr.HangupAll()

		time.sleep(5)

		calls = mgr.GetCalls()

		assert len(calls) == 0, "HangupAll() failed, calls still exist!"
