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 utils
18from acts import asserts
19from acts import signals
20from acts.base_test import BaseTestClass
21from acts.test_decorators import test_tracker_info
22from acts.utils import get_current_epoch_time
23from acts.test_utils.wifi.wifi_test_utils import wifi_toggle_state
24from acts.test_utils.tel.tel_test_utils import start_qxdm_logger
25from acts.test_utils.tel.tel_test_utils import stop_qxdm_logger
26from acts.test_utils.tel.tel_test_utils import verify_internet_connection
27from acts.test_utils.tel.tel_test_utils import abort_all_tests
28from acts.test_utils.gnss.gnss_test_utils import get_baseband_and_gms_version
29from acts.test_utils.gnss.gnss_test_utils import _init_device
30from acts.test_utils.gnss.gnss_test_utils import check_location_service
31from acts.test_utils.gnss.gnss_test_utils import clear_logd_gnss_qxdm_log
32from acts.test_utils.gnss.gnss_test_utils import set_mobile_data
33from acts.test_utils.gnss.gnss_test_utils import get_gnss_qxdm_log
34from acts.test_utils.gnss.gnss_test_utils import set_wifi_and_bt_scanning
35from acts.test_utils.gnss.gnss_test_utils import process_gnss_by_gtw_gpstool
36from acts.test_utils.gnss.gnss_test_utils import start_ttff_by_gtw_gpstool
37from acts.test_utils.gnss.gnss_test_utils import process_ttff_by_gtw_gpstool
38from acts.test_utils.gnss.gnss_test_utils import check_ttff_data
39from acts.test_utils.gnss.gnss_test_utils import set_attenuator_gnss_signal
40from acts.test_utils.gnss.gnss_test_utils import connect_to_wifi_network
41from acts.test_utils.gnss.gnss_test_utils import gnss_tracking_via_gtw_gpstool
42from acts.test_utils.gnss.gnss_test_utils import parse_gtw_gpstool_log
43from acts.test_utils.tel.tel_test_utils import start_adb_tcpdump
44from acts.test_utils.tel.tel_test_utils import stop_adb_tcpdump
45from acts.test_utils.tel.tel_test_utils import get_tcpdump_log
46
47
48class FlpTtffTest(BaseTestClass):
49    """ FLP TTFF Tests"""
50    def setup_class(self):
51        super().setup_class()
52        self.ad = self.android_devices[0]
53        req_params = ["pixel_lab_network", "standalone_cs_criteria",
54                      "qdsp6m_path", "flp_ttff_max_threshold",
55                      "pixel_lab_location", "default_gnss_signal_attenuation",
56                      "weak_gnss_signal_attenuation", "ttff_test_cycle",
57                      "collect_logs"]
58        self.unpack_userparams(req_param_names=req_params)
59        self.ssid_map = {}
60        for network in self.pixel_lab_network:
61            SSID = network['SSID']
62            self.ssid_map[SSID] = network
63        if int(self.ad.adb.shell("settings get global airplane_mode_on")) != 0:
64            self.ad.log.info("Force airplane mode off")
65            force_airplane_mode(self.ad, False)
66        _init_device(self.ad)
67
68    def setup_test(self):
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
92    def on_pass(self, test_name, begin_time):
93        if self.collect_logs:
94            self.ad.take_bug_report(test_name, begin_time)
95            get_gnss_qxdm_log(self.ad, self.qdsp6m_path)
96            get_tcpdump_log(self.ad, test_name, begin_time)
97
98    def on_fail(self, test_name, begin_time):
99        if self.collect_logs:
100            self.ad.take_bug_report(test_name, begin_time)
101            get_gnss_qxdm_log(self.ad, self.qdsp6m_path)
102            get_tcpdump_log(self.ad, test_name, begin_time)
103
104    """ Helper Functions """
105
106    def flp_ttff_hs_and_cs(self, criteria, location):
107        flp_results = []
108        ttff = {"hs": "Hot Start", "cs": "Cold Start"}
109        for mode in ttff.keys():
110            begin_time = get_current_epoch_time()
111            process_gnss_by_gtw_gpstool(
112                self.ad, self.standalone_cs_criteria, type="flp")
113            start_ttff_by_gtw_gpstool(
114                self.ad, ttff_mode=mode, iteration=self.ttff_test_cycle)
115            ttff_data = process_ttff_by_gtw_gpstool(
116                self.ad, begin_time, location, type="flp")
117            result = check_ttff_data(self.ad, ttff_data, ttff[mode], criteria)
118            flp_results.append(result)
119        asserts.assert_true(
120            all(flp_results), "FLP TTFF fails to reach designated criteria")
121
122    def start_qxdm_and_tcpdump_log(self):
123        """Start QXDM and adb tcpdump if collect_logs is True."""
124        if self.collect_logs:
125            start_qxdm_logger(self.ad, get_current_epoch_time())
126            start_adb_tcpdump(self.ad)
127
128    """ Test Cases """
129
130    @test_tracker_info(uuid="c11ada6a-d7ad-4dc8-9d4a-0ae3cb9dfa8e")
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                                      type="flp", testtime=60)
144        parse_gtw_gpstool_log(self.ad, self.pixel_lab_location, type="flp")
145
146    @test_tracker_info(uuid="8bc4e82d-fdce-4ee8-af8c-5e4a925b5360")
147    def test_flp_ttff_strong_signal_wifiscan_on_wifi_connect(self):
148        """Verify FLP TTFF Hot Start and Cold Start under strong GNSS signals
149        with WiFi scanning on and connected.
150
151        Steps:
152            1. Enable WiFi scanning in location setting.
153            2. Connect to WiFi AP.
154            3. TTFF Hot Start for 10 iteration.
155            4. TTFF Cold Start for 10 iteration.
156
157        Expected Results:
158            Both FLP TTFF Hot Start and Cold Start results should be within
159            flp_ttff_max_threshold.
160        """
161        self.start_qxdm_and_tcpdump_log()
162        set_wifi_and_bt_scanning(self.ad, True)
163        wifi_toggle_state(self.ad, True)
164        connect_to_wifi_network(
165            self.ad, self.ssid_map[self.pixel_lab_network[0]["SSID"]])
166        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
167                                self.pixel_lab_location)
168
169    @test_tracker_info(uuid="adc1a0c7-3635-420d-9481-0f5816c58334")
170    def test_flp_ttff_strong_signal_wifiscan_on_wifi_not_connect(self):
171        """Verify FLP TTFF Hot Start and Cold Start under strong GNSS signals
172        with WiFi scanning on and not connected.
173
174        Steps:
175            1. Enable WiFi scanning in location setting.
176            2. WiFi is not connected.
177            3. TTFF Hot Start for 10 iteration.
178            4. TTFF Cold Start for 10 iteration.
179
180        Expected Results:
181            Both FLP TTFF Hot Start and Cold Start results should be within
182            flp_ttff_max_threshold.
183        """
184        self.start_qxdm_and_tcpdump_log()
185        set_wifi_and_bt_scanning(self.ad, True)
186        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
187                                self.pixel_lab_location)
188
189    @test_tracker_info(uuid="3ec3cee2-b881-4c61-9df1-b6b81fcd4527")
190    def test_flp_ttff_strong_signal_wifiscan_off(self):
191        """Verify FLP TTFF Hot Start and Cold Start with WiFi scanning OFF
192           under strong GNSS signals.
193
194        Steps:
195            1. Disable WiFi scanning in location setting.
196            2. TTFF Hot Start for 10 iteration.
197            3. TTFF Cold Start for 10 iteration.
198
199        Expected Results:
200            Both FLP TTFF Hot Start and Cold Start results should be within
201            flp_ttff_max_threshold.
202        """
203        self.start_qxdm_and_tcpdump_log()
204        set_wifi_and_bt_scanning(self.ad, False)
205        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
206                                self.pixel_lab_location)
207
208    @test_tracker_info(uuid="03c0d34f-8312-48d5-8753-93b09151233a")
209    def test_flp_ttff_weak_signal_wifiscan_on_wifi_connect(self):
210        """Verify FLP TTFF Hot Start and Cold Start under Weak GNSS signals
211        with WiFi scanning on and connected
212
213        Steps:
214            1. Set attenuation value to weak GNSS signal.
215            2. Enable WiFi scanning in location setting.
216            3. Connect to WiFi AP.
217            4. TTFF Hot Start for 10 iteration.
218            5. TTFF Cold Start for 10 iteration.
219
220        Expected Results:
221            Both FLP TTFF Hot Start and Cold Start results should be within
222            flp_ttff_max_threshold.
223        """
224        set_attenuator_gnss_signal(self.ad, self.attenuators,
225                                   self.weak_gnss_signal_attenuation)
226        self.start_qxdm_and_tcpdump_log()
227        set_wifi_and_bt_scanning(self.ad, True)
228        wifi_toggle_state(self.ad, True)
229        connect_to_wifi_network(
230            self.ad, self.ssid_map[self.pixel_lab_network[0]["SSID"]])
231        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
232                                self.pixel_lab_location)
233
234    @test_tracker_info(uuid="13daf7b3-5ac5-4107-b3dc-a3a8b5589fed")
235    def test_flp_ttff_weak_signal_wifiscan_on_wifi_not_connect(self):
236        """Verify FLP TTFF Hot Start and Cold Start under Weak GNSS signals
237        with WiFi scanning on and not connected.
238
239        Steps:
240            1. Set attenuation value to weak GNSS signal.
241            2. Enable WiFi scanning in location setting.
242            3. WiFi is not connected.
243            4. TTFF Hot Start for 10 iteration.
244            5. TTFF Cold Start for 10 iteration.
245
246        Expected Results:
247            Both FLP TTFF Hot Start and Cold Start results should be within
248            flp_ttff_max_threshold.
249        """
250        set_attenuator_gnss_signal(self.ad, self.attenuators,
251                                   self.weak_gnss_signal_attenuation)
252        self.start_qxdm_and_tcpdump_log()
253        set_wifi_and_bt_scanning(self.ad, True)
254        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
255                                self.pixel_lab_location)
256
257    @test_tracker_info(uuid="1831f80f-099f-46d2-b484-f332046d5a4d")
258    def test_flp_ttff_weak_signal_wifiscan_off(self):
259        """Verify FLP TTFF Hot Start and Cold Start with WiFi scanning OFF
260           under weak GNSS signals.
261
262        Steps:
263            1. Set attenuation value to weak GNSS signal.
264            2. Disable WiFi scanning in location setting.
265            3. TTFF Hot Start for 10 iteration.
266            4. TTFF Cold Start for 10 iteration.
267
268        Expected Results:
269            Both FLP TTFF Hot Start and Cold Start results should be within
270            flp_ttff_max_threshold.
271        """
272        set_attenuator_gnss_signal(self.ad, self.attenuators,
273                                   self.weak_gnss_signal_attenuation)
274        self.start_qxdm_and_tcpdump_log()
275        set_wifi_and_bt_scanning(self.ad, False)
276        self.flp_ttff_hs_and_cs(self.flp_ttff_max_threshold,
277                                self.pixel_lab_location)
278