1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - Google
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
17import os
18
19from acts import asserts
20from acts import utils
21from acts.base_test import BaseTestClass
22from acts.keys import Config
23from acts_contrib.test_utils.net import net_test_utils as nutils
24from acts_contrib.test_utils.wifi import wifi_test_utils as wutils
25from acts_contrib.test_utils.wifi.rtt import rtt_const as rconsts
26from acts_contrib.test_utils.wifi.rtt import rtt_test_utils as rutils
27
28
29class RttBaseTest(BaseTestClass):
30
31    def setup_class(self):
32        opt_param = ["ranging_role_concurrency_flexible_models"]
33        self.unpack_userparams(opt_param_names=opt_param)
34
35    def setup_test(self):
36        required_params = ("lci_reference", "lcr_reference",
37                           "rtt_reference_distance_mm",
38                           "stress_test_min_iteration_count",
39                           "stress_test_target_run_time_sec")
40        self.unpack_userparams(required_params)
41
42        # can be moved to JSON config file
43        self.rtt_reference_distance_margin_mm = 2000
44        self.rtt_max_failure_rate_two_sided_rtt_percentage = 20
45        self.rtt_max_failure_rate_one_sided_rtt_percentage = 50
46        self.rtt_max_margin_exceeded_rate_two_sided_rtt_percentage = 10
47        self.rtt_max_margin_exceeded_rate_one_sided_rtt_percentage = 50
48        self.rtt_min_expected_rssi_dbm = -100
49
50        wutils.start_all_wlan_logs(self.android_devices)
51        self.tcpdump_proc = []
52        if hasattr(self, "android_devices"):
53            for ad in self.android_devices:
54                proc = nutils.start_tcpdump(ad, self.test_name)
55                self.tcpdump_proc.append((ad, proc))
56
57        for ad in self.android_devices:
58            utils.set_location_service(ad, True)
59            ad.droid.wifiEnableVerboseLogging(1)
60            asserts.skip_if(
61                not ad.droid.doesDeviceSupportWifiRttFeature(),
62                "Device under test does not support Wi-Fi RTT - skipping test")
63            wutils.wifi_toggle_state(ad, True)
64            rtt_avail = ad.droid.wifiIsRttAvailable()
65            if not rtt_avail:
66                self.log.info('RTT not available. Waiting ...')
67                rutils.wait_for_event(ad, rconsts.BROADCAST_WIFI_RTT_AVAILABLE)
68            ad.ed.clear_all_events()
69            rutils.config_privilege_override(ad, False)
70            wutils.set_wifi_country_code(ad, wutils.WifiEnums.CountryCode.US)
71            ad.rtt_capabilities = rutils.get_rtt_capabilities(ad)
72
73    def teardown_test(self):
74        wutils.stop_all_wlan_logs(self.android_devices)
75        for proc in self.tcpdump_proc:
76            nutils.stop_tcpdump(
77                    proc[0], proc[1], self.test_name, pull_dump=False)
78        self.tcpdump_proc = []
79        for ad in self.android_devices:
80            if not ad.droid.doesDeviceSupportWifiRttFeature():
81                return
82
83            # clean-up queue from the System Service UID
84            ad.droid.wifiRttCancelRanging([1000])
85
86    def on_fail(self, test_name, begin_time):
87        for ad in self.android_devices:
88            ad.take_bug_report(test_name, begin_time)
89            ad.cat_adb_log(test_name, begin_time)
90            wutils.get_ssrdumps(ad)
91        wutils.stop_all_wlan_logs(self.android_devices)
92        for ad in self.android_devices:
93            wutils.get_wlan_logs(ad)
94        for proc in self.tcpdump_proc:
95            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
96        self.tcpdump_proc = []
97