1#!/usr/bin/env python3.5
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from acts import asserts
18from acts import signals
19from acts.base_test import BaseTestClass
20from acts.utils import get_current_epoch_time
21from acts_contrib.test_utils.wifi.wifi_test_utils import wifi_toggle_state
22from acts_contrib.test_utils.tel.tel_logging_utils import start_qxdm_logger
23from acts_contrib.test_utils.tel.tel_logging_utils import stop_qxdm_logger
24from acts_contrib.test_utils.tel.tel_logging_utils import start_adb_tcpdump
25from acts_contrib.test_utils.tel.tel_logging_utils import stop_adb_tcpdump
26from acts_contrib.test_utils.tel.tel_logging_utils import get_tcpdump_log
27from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
28from acts_contrib.test_utils.gnss.gnss_test_utils import get_baseband_and_gms_version
29from acts_contrib.test_utils.gnss.gnss_test_utils import _init_device
30from acts_contrib.test_utils.gnss.gnss_test_utils import clear_logd_gnss_qxdm_log
31from acts_contrib.test_utils.gnss.gnss_test_utils import set_mobile_data
32from acts_contrib.test_utils.gnss.gnss_test_utils import get_gnss_qxdm_log
33from acts_contrib.test_utils.gnss.gnss_test_utils import set_wifi_and_bt_scanning
34from acts_contrib.test_utils.gnss.gnss_test_utils import process_gnss_by_gtw_gpstool
35from acts_contrib.test_utils.gnss.gnss_test_utils import start_ttff_by_gtw_gpstool
36from acts_contrib.test_utils.gnss.gnss_test_utils import process_ttff_by_gtw_gpstool
37from acts_contrib.test_utils.gnss.gnss_test_utils import check_ttff_data
38from acts_contrib.test_utils.gnss.gnss_test_utils import set_attenuator_gnss_signal
39from acts_contrib.test_utils.gnss.gnss_test_utils import connect_to_wifi_network
40from acts_contrib.test_utils.gnss.gnss_test_utils import gnss_tracking_via_gtw_gpstool
41from acts_contrib.test_utils.gnss.gnss_test_utils import parse_gtw_gpstool_log
42from acts_contrib.test_utils.gnss.gnss_test_utils import log_current_epoch_time
43from acts_contrib.test_utils.gnss.testtracker_util import log_testtracker_uuid
44
45
46class FlpTtffTest(BaseTestClass):
47    """ FLP TTFF Tests"""
48    def setup_class(self):
49        super().setup_class()
50        self.ad = self.android_devices[0]
51        req_params = ["pixel_lab_network", "standalone_cs_criteria",
52                      "qdsp6m_path", "flp_ttff_max_threshold",
53                      "pixel_lab_location", "default_gnss_signal_attenuation",
54                      "weak_gnss_signal_attenuation", "ttff_test_cycle",
55                      "collect_logs"]
56        self.unpack_userparams(req_param_names=req_params)
57        self.ssid_map = {}
58        for network in self.pixel_lab_network:
59            SSID = network['SSID']
60            self.ssid_map[SSID] = network
61        if int(self.ad.adb.shell("settings get global airplane_mode_on")) != 0:
62            self.ad.log.info("Force airplane mode off")
63            force_airplane_mode(self.ad, False)
64        _init_device(self.ad)
65
66    def setup_test(self):
67        log_current_epoch_time(self.ad, "test_start_time")
68        log_testtracker_uuid(self.ad, self.current_test_name)
69        get_baseband_and_gms_version(self.ad)
70        if self.collect_logs:
71            clear_logd_gnss_qxdm_log(self.ad)
72            set_attenuator_gnss_signal(self.ad, self.attenuators,
73                                       self.default_gnss_signal_attenuation)
74        if not verify_internet_connection(self.ad.log, self.ad, retries=3,
75                                          expected_state=True):
76            raise signals.TestFailure("Fail to connect to LTE network.")
77
78    def teardown_test(self):
79        if self.collect_logs:
80            stop_qxdm_logger(self.ad)
81            stop_adb_tcpdump(self.ad)
82            set_attenuator_gnss_signal(self.ad, self.attenuators,
83                                       self.default_gnss_signal_attenuation)
84        if int(self.ad.adb.shell("settings get global mobile_data")) != 1:
85            set_mobile_data(self.ad, True)
86        if int(self.ad.adb.shell(
87            "settings get global wifi_scan_always_enabled")) != 1:
88            set_wifi_and_bt_scanning(self.ad, True)
89        if self.ad.droid.wifiCheckState():
90            wifi_toggle_state(self.ad, False)
91        log_current_epoch_time(self.ad, "test_end_time")
92
93    def on_pass(self, test_name, begin_time):
94        if self.collect_logs:
95            self.ad.take_bug_report(test_name, begin_time)
96            get_gnss_qxdm_log(self.ad, self.qdsp6m_path)
97            get_tcpdump_log(self.ad, test_name, begin_time)
98
99    def on_fail(self, test_name, begin_time):
100        if self.collect_logs:
101            self.ad.take_bug_report(test_name, begin_time)
102            get_gnss_qxdm_log(self.ad, self.qdsp6m_path)
103            get_tcpdump_log(self.ad, test_name, begin_time)
104
105    """ Helper Functions """
106
107    def flp_ttff_hs_and_cs(self, criteria, location):
108        flp_results = []
109        ttff = {"hs": "Hot Start", "cs": "Cold Start"}
110        for mode in ttff.keys():
111            begin_time = get_current_epoch_time()
112            process_gnss_by_gtw_gpstool(
113                self.ad, self.standalone_cs_criteria, api_type="flp")
114            start_ttff_by_gtw_gpstool(
115                self.ad, ttff_mode=mode, iteration=self.ttff_test_cycle)
116            ttff_data = process_ttff_by_gtw_gpstool(
117                self.ad, begin_time, location, api_type="flp")
118            result = check_ttff_data(self.ad, ttff_data, ttff[mode], criteria)
119            flp_results.append(result)
120        asserts.assert_true(
121            all(flp_results), "FLP TTFF fails to reach designated criteria")
122
123    def start_qxdm_and_tcpdump_log(self):
124        """Start QXDM and adb tcpdump if collect_logs is True."""
125        if self.collect_logs:
126            start_qxdm_logger(self.ad, get_current_epoch_time())
127            start_adb_tcpdump(self.ad)
128
129    """ Test Cases """
130
131    def test_flp_one_hour_tracking(self):
132        """Verify FLP tracking performance of position error.
133
134        Steps:
135            1. Launch GTW_GPSTool.
136            2. FLP tracking for 60 minutes.
137
138        Expected Results:
139            DUT could finish 60 minutes test and output track data.
140        """
141        self.start_qxdm_and_tcpdump_log()
142        gnss_tracking_via_gtw_gpstool(self.ad, self.standalone_cs_criteria,
143                                      api_type="flp", testtime=60)
144        parse_gtw_gpstool_log(self.ad, self.pixel_lab_location, api_type="flp")
145
146    def test_flp_ttff_strong_signal_wifiscan_on_wifi_connect(self):
147        """Verify FLP TTFF Hot Start and Cold Start under strong GNSS signals
148        with WiFi scanning on and connected.
149
150        Steps:
151            1. Enable WiFi scanning in location setting.
152            2. Connect to WiFi AP.
153            3. TTFF Hot Start for 10 iteration.
154            4. TTFF Cold Start for 10 iteration.
155
156        Expected Results:
157            Both FLP TTFF Hot Start and Cold Start results should be within
158            flp_ttff_max_threshold.
159        """
160        self.start_qxdm_and_tcpdump_log()
161        set_wifi_and_bt_scanning(self.ad, True)
162        wifi_toggle_state(self.ad, True)
163        connect_to_wifi_network(
164            self.ad, self.ssid_map[self.pixel_lab_network[0]["SSID"]])
165        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
166                                self.pixel_lab_location)
167
168    def test_flp_ttff_strong_signal_wifiscan_on_wifi_not_connect(self):
169        """Verify FLP TTFF Hot Start and Cold Start under strong GNSS signals
170        with WiFi scanning on and not connected.
171
172        Steps:
173            1. Enable WiFi scanning in location setting.
174            2. WiFi is not connected.
175            3. TTFF Hot Start for 10 iteration.
176            4. TTFF Cold Start for 10 iteration.
177
178        Expected Results:
179            Both FLP TTFF Hot Start and Cold Start results should be within
180            flp_ttff_max_threshold.
181        """
182        self.start_qxdm_and_tcpdump_log()
183        set_wifi_and_bt_scanning(self.ad, True)
184        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
185                                self.pixel_lab_location)
186
187    def test_flp_ttff_strong_signal_wifiscan_off(self):
188        """Verify FLP TTFF Hot Start and Cold Start with WiFi scanning OFF
189           under strong GNSS signals.
190
191        Steps:
192            1. Disable WiFi scanning in location setting.
193            2. TTFF Hot Start for 10 iteration.
194            3. TTFF Cold Start for 10 iteration.
195
196        Expected Results:
197            Both FLP TTFF Hot Start and Cold Start results should be within
198            flp_ttff_max_threshold.
199        """
200        self.start_qxdm_and_tcpdump_log()
201        set_wifi_and_bt_scanning(self.ad, False)
202        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
203                                self.pixel_lab_location)
204
205    def test_flp_ttff_weak_signal_wifiscan_on_wifi_connect(self):
206        """Verify FLP TTFF Hot Start and Cold Start under Weak GNSS signals
207        with WiFi scanning on and connected
208
209        Steps:
210            1. Set attenuation value to weak GNSS signal.
211            2. Enable WiFi scanning in location setting.
212            3. Connect to WiFi AP.
213            4. TTFF Hot Start for 10 iteration.
214            5. TTFF Cold Start for 10 iteration.
215
216        Expected Results:
217            Both FLP TTFF Hot Start and Cold Start results should be within
218            flp_ttff_max_threshold.
219        """
220        set_attenuator_gnss_signal(self.ad, self.attenuators,
221                                   self.weak_gnss_signal_attenuation)
222        self.start_qxdm_and_tcpdump_log()
223        set_wifi_and_bt_scanning(self.ad, True)
224        wifi_toggle_state(self.ad, True)
225        connect_to_wifi_network(
226            self.ad, self.ssid_map[self.pixel_lab_network[0]["SSID"]])
227        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
228                                self.pixel_lab_location)
229
230    def test_flp_ttff_weak_signal_wifiscan_on_wifi_not_connect(self):
231        """Verify FLP TTFF Hot Start and Cold Start under Weak GNSS signals
232        with WiFi scanning on and not connected.
233
234        Steps:
235            1. Set attenuation value to weak GNSS signal.
236            2. Enable WiFi scanning in location setting.
237            3. WiFi is not connected.
238            4. TTFF Hot Start for 10 iteration.
239            5. TTFF Cold Start for 10 iteration.
240
241        Expected Results:
242            Both FLP TTFF Hot Start and Cold Start results should be within
243            flp_ttff_max_threshold.
244        """
245        set_attenuator_gnss_signal(self.ad, self.attenuators,
246                                   self.weak_gnss_signal_attenuation)
247        self.start_qxdm_and_tcpdump_log()
248        set_wifi_and_bt_scanning(self.ad, True)
249        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
250                                self.pixel_lab_location)
251
252    def test_flp_ttff_weak_signal_wifiscan_off(self):
253        """Verify FLP TTFF Hot Start and Cold Start with WiFi scanning OFF
254           under weak GNSS signals.
255
256        Steps:
257            1. Set attenuation value to weak GNSS signal.
258            2. Disable WiFi scanning in location setting.
259            3. TTFF Hot Start for 10 iteration.
260            4. TTFF Cold Start for 10 iteration.
261
262        Expected Results:
263            Both FLP TTFF Hot Start and Cold Start results should be within
264            flp_ttff_max_threshold.
265        """
266        set_attenuator_gnss_signal(self.ad, self.attenuators,
267                                   self.weak_gnss_signal_attenuation)
268        self.start_qxdm_and_tcpdump_log()
269        set_wifi_and_bt_scanning(self.ad, False)
270        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
271                                self.pixel_lab_location)
272