Update TTFF test procedure for BCM DUT

Bug: b/197570910
Test: Manual test
Change-Id: I023bc89e83edf37439df00ff09f0db1be30d055b
diff --git a/acts_tests/acts_contrib/test_utils/gnss/LabTtffTestBase.py b/acts_tests/acts_contrib/test_utils/gnss/LabTtffTestBase.py
index 191f3dd..28207f9 100644
--- a/acts_tests/acts_contrib/test_utils/gnss/LabTtffTestBase.py
+++ b/acts_tests/acts_contrib/test_utils/gnss/LabTtffTestBase.py
@@ -28,10 +28,9 @@
 from acts_contrib.test_utils.gnss import dut_log_test_utils as diaglog
 from acts_contrib.test_utils.gnss import gnss_test_utils as gutils
 from acts_contrib.test_utils.gnss import gnss_testlog_utils as glogutils
-
-# TODO: b/189387425 define the common constants in new gnss_defines.py for GNSS tests.
-DEVICE_GPSLOG_FOLDER = '/sdcard/Android/data/com.android.gpstool/files/'
-GPS_PKG_NAME = 'com.android.gpstool'
+from acts_contrib.test_utils.gnss.gnss_defines import DEVICE_GPSLOG_FOLDER
+from acts_contrib.test_utils.gnss.gnss_defines import GPS_PKG_NAME
+from acts_contrib.test_utils.gnss.gnss_defines import BCM_GPS_XML_PATH
 
 
 class LabTtffTestBase(BaseTestClass):
@@ -63,6 +62,7 @@
         self.gnss_simulator = None
         self.rockbottom_script = None
         self.gnss_log_path = self.log_path
+        self.gps_xml_bk_path = BCM_GPS_XML_PATH + '.bk'
 
     def setup_class(self):
         super().setup_class()
@@ -139,6 +139,17 @@
         utils.set_location_service(self.dut, True)
         gutils.reinstall_package_apk(self.dut, GPS_PKG_NAME,
                                      self.gtw_gpstool_app)
+
+        # For BCM DUTs, delete gldata.sto and set IgnoreRomAlm="true" based on b/196936791#comment20
+        if self.diag_option == "BCM":
+            gutils.remount_device(self.dut)
+            # Backup gps.xml
+            copy_cmd = "cp {} {}".format(BCM_GPS_XML_PATH, self.gps_xml_bk_path)
+            self.dut.adb.shell(copy_cmd)
+            gutils.delete_bcm_nvmem_sto_file(self.dut)
+            gutils.bcm_gps_ignore_rom_alm(self.dut)
+            # Reboot DUT to apply the setting
+            gutils.reboot(self.dut)
         self.gnss_simulator.connect()
 
     def dut_rockbottom(self):
@@ -157,6 +168,18 @@
         # Restart SL4A
         self.dut.start_services()
 
+    def teardown_test(self):
+        """Teardown settings for the test class"""
+        super().teardown_test()
+        # Restore the gps.xml everytime after the test.
+        if self.diag_option == "BCM":
+            # Restore gps.xml
+            rm_cmd = "rm -rf {}".format(BCM_GPS_XML_PATH)
+            restore_cmd = "mv {} {}".format(self.gps_xml_bk_path,
+                                            BCM_GPS_XML_PATH)
+            self.dut.adb.shell(rm_cmd)
+            self.dut.adb.shell(restore_cmd)
+
     def teardown_class(self):
         """ Executed after completing all selected test cases."""
         self.clear_gps_log()
diff --git a/acts_tests/acts_contrib/test_utils/gnss/gnss_defines.py b/acts_tests/acts_contrib/test_utils/gnss/gnss_defines.py
new file mode 100644
index 0000000..24eef0f
--- /dev/null
+++ b/acts_tests/acts_contrib/test_utils/gnss/gnss_defines.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+#
+#   Copyright 2021 - The Android Open Source Project
+#
+#   Licensed under the Apache License, Version 2.0 (the 'License');
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an 'AS IS' BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+DEVICE_GPSLOG_FOLDER = '/sdcard/Android/data/com.android.gpstool/files/'
+GPS_PKG_NAME = 'com.android.gpstool'
+BCM_GPS_XML_PATH = '/vendor/etc/gnss/gps.xml'
+BCM_NVME_STO_PATH = '/data/vendor/gps/gldata.sto'
\ No newline at end of file
diff --git a/acts_tests/acts_contrib/test_utils/gnss/gnss_test_utils.py b/acts_tests/acts_contrib/test_utils/gnss/gnss_test_utils.py
index 61a843c..5d8d24b 100644
--- a/acts_tests/acts_contrib/test_utils/gnss/gnss_test_utils.py
+++ b/acts_tests/acts_contrib/test_utils/gnss/gnss_test_utils.py
@@ -41,6 +41,8 @@
 from acts_contrib.test_utils.instrumentation.device.command.instrumentation_command_builder import InstrumentationTestCommandBuilder
 from acts.utils import get_current_epoch_time
 from acts.utils import epoch_to_human_time
+from acts_contrib.test_utils.gnss.gnss_defines import BCM_GPS_XML_PATH
+from acts_contrib.test_utils.gnss.gnss_defines import BCM_NVME_STO_PATH
 
 WifiEnums = wutils.WifiEnums
 PULL_TIMEOUT = 300
@@ -2288,3 +2290,68 @@
         ad.log.info("Click button %s" % matching_text)
     else:
         ad.log.error("No button named %s" % matching_text)
+
+
+def delete_bcm_nvmem_sto_file(ad):
+    """Delete BCM's NVMEM ephemeris gldata.sto.
+
+    Args:
+        ad: An AndroidDevice object.
+    """
+    remount_device(ad)
+    rm_cmd = "rm -rf {}".format(BCM_NVME_STO_PATH)
+    status = ad.adb.shell(rm_cmd)
+    ad.log.info("Delete BCM's NVMEM ephemeris files.\n%s" % status)
+
+
+def bcm_gps_xml_add_option(ad,
+                           search_line=None,
+                           append_txt=None,
+                           gps_xml_path=BCM_GPS_XML_PATH):
+    """Append parameter setting in gps.xml for BCM solution
+
+    Args:
+        ad: An AndroidDevice object.
+        search_line: Pattern matching of target
+        line for appending new line data.
+        append_txt: New line that will be appended after the search_line.
+        gps_xml_path: gps.xml file location of DUT
+    """
+    remount_device(ad)
+    #Update gps.xml
+    if not search_line or not append_txt:
+        ad.log.info("Nothing for update.")
+    else:
+        tmp_log_path = tempfile.mkdtemp()
+        ad.pull_files(gps_xml_path, tmp_log_path)
+        gps_xml_tmp_path = os.path.join(tmp_log_path, "gps.xml")
+        gps_xml_file = open(gps_xml_tmp_path, "r")
+        lines = gps_xml_file.readlines()
+        gps_xml_file.close()
+        fout = open(gps_xml_tmp_path, "w")
+        append_txt_tag = append_txt.strip()
+        for line in lines:
+            if append_txt_tag in line:
+                ad.log.info('{} is already in the file. Skip'.format(append_txt))
+                continue
+            fout.write(line)
+            if search_line in line:
+                fout.write(append_txt)
+                ad.log.info("Update new line: '{}' in gps.xml.".format(append_txt))
+        fout.close()
+
+        # Update gps.xml with gps_new.xml
+        ad.push_system_file(gps_xml_tmp_path, gps_xml_path)
+
+        # remove temp folder
+        shutil.rmtree(tmp_log_path, ignore_errors=True)
+
+
+def bcm_gps_ignore_rom_alm(ad):
+    """ Update BCM gps.xml with ignoreRomAlm="True"
+    Args:
+        ad: An AndroidDevice object.
+    """
+    search_line_tag = '<gll\n'
+    append_line_str = '       IgnoreRomAlm=\"true\"\n'
+    bcm_gps_xml_add_option(ad, search_line_tag, append_line_str)