from modem import AK110port, WvdialTest
import unittest
import os
import config

COMMAND_DEVICE = "/dev/ttyAK110_AT"

WVDIAL_CONF = """[Dialer Defaults]
Modem Type = Analog Modem
ISDN = 0
New PPPD = yes
Stupid Mode = 1
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","%(apn)s"
Init4 = AT$QCPDPP=1,3,"%(password)s","%(user)s"
Modem = /dev/ttyAK110_MODEM
Phone = *99***1#
Password = %(password)s
Username = %(user)s
"""

class AK130_AK110_TestCase(unittest.TestCase):
	def __log(self, msg):
		if config.logfile:
			config.logfile.write(msg)

	def setUp(self):
		self.__log("---- %s start ----\n" % self._testMethodName)

	def tearDown(self):
		self.__log("---- %s finish ----\n" % self._testMethodName)

	def test_dialup(self):
		conf = WVDIAL_CONF % {
				"apn":config.MOBILE_APN,
				"user":config.MOBILE_USER,
				"password":config.MOBILE_PASSWORD}
		wvdial = WvdialTest()
		result = wvdial.start(conf, config.logfile)
		self.assertTrue(result, "wvdial fail")

	def test_modem_device(self):
		self.assertTrue(os.path.exists(COMMAND_DEVICE))
		self.__mdm = AK110port(COMMAND_DEVICE, log=config.logfile)
		imei = self.__mdm.get_imei()
		self.assertIsNotNone(imei)
		imsi = self.__mdm.get_imsi()
		self.assertIsNotNone(imsi)
		info = self.__mdm.get_info()
		self.assertTrue(info["Revision"])
		rssi = self.__mdm.get_rssi()
		self.assertTrue(rssi > 10, "RSSI is low")
		self.__mdm.close()

		self.__log("IMEI: %s\n" % imei)
		self.__log("IMSI: %s\n" % imsi)
		self.__log("Revision: %s\n" % info["Revision"])
		self.__log("RSSI: %d\n" % rssi)


