1import time
2import os
3import tempfile
4
5from acts import utils
6from acts import signals
7from acts.base_test import BaseTestClass
8from acts_contrib.test_utils.tel.tel_defines import EventSmsSentSuccess
9from acts_contrib.test_utils.tel.tel_test_utils import get_iccid_by_adb
10from acts_contrib.test_utils.tel.tel_test_utils import is_sim_ready_by_adb
11
12
13class GnssSimInventoryTest(BaseTestClass):
14    """ GNSS SIM Inventory Tests"""
15    def setup_class(self):
16        super().setup_class()
17        self.ad = self.android_devices[0]
18        req_params = ["sim_inventory_recipient", "sim_inventory_ldap"]
19        self.unpack_userparams(req_param_names=req_params)
20
21    def check_device_status(self):
22        if int(self.ad.adb.shell("settings get global airplane_mode_on")) != 0:
23            self.ad.log.info("Force airplane mode off")
24            utils.force_airplane_mode(self.ad, False)
25        if not is_sim_ready_by_adb(self.ad.log, self.ad):
26            raise signals.TestFailure("SIM card is not loaded and ready.")
27
28    def get_imsi(self):
29        self.ad.log.info("Get imsi from netpolicy.xml")
30        tmp_path = tempfile.mkdtemp()
31        self.ad.pull_files("/data/system/netpolicy.xml", tmp_path)
32        netpolicy_path = os.path.join(tmp_path, "netpolicy.xml")
33        with open(netpolicy_path, "r", encoding="utf-8") as file:
34            for line in file.readlines():
35                if "subscriberId" in line:
36                    imsi = line.split(" ")[2].split("=")[-1].strip('"')
37                    return imsi
38        raise signals.TestFailure("Fail to get imsi")
39
40    def get_iccid(self):
41        iccid = str(get_iccid_by_adb(self.ad))
42        if not isinstance(iccid, int):
43            self.ad.log.info("Unable to get iccid via adb. Changed to isub.")
44            iccid = str(self.ad.adb.shell(
45                "dumpsys isub | grep iccid")).split(" ")[4].strip(",")
46            return iccid
47        raise signals.TestFailure("Fail to get iccid")
48
49    def test_gnss_sim_inventory(self):
50        self.check_device_status()
51        sms_message = "imsi: %s, iccid: %s, ldap: %s, model: %s, sn: %s" % (
52            self.get_imsi(), self.get_iccid(), self.sim_inventory_ldap,
53            self.ad.model, self.ad.serial)
54        self.ad.log.info(sms_message)
55        try:
56            self.ad.log.info("Send SMS by SL4A.")
57            self.ad.droid.smsSendTextMessage(self.sim_inventory_recipient,
58                                             sms_message, True)
59            self.ad.ed.pop_event(EventSmsSentSuccess, 10)
60        except Exception as e:
61            raise signals.TestFailure(e)
62