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
17from acts import asserts
18from acts import utils
19from acts.base_test import BaseTestClass
20from acts.test_utils.wifi import wifi_test_utils as wutils
21from acts.test_utils.wifi.rtt import rtt_const as rconsts
22from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
23
24
25class RttBaseTest(BaseTestClass):
26    def setup_test(self):
27        required_params = ("lci_reference", "lcr_reference",
28                           "rtt_reference_distance_mm",
29                           "stress_test_min_iteration_count",
30                           "stress_test_target_run_time_sec")
31        self.unpack_userparams(required_params)
32
33        # can be moved to JSON config file
34        self.rtt_reference_distance_margin_mm = 2000
35        self.rtt_max_failure_rate_two_sided_rtt_percentage = 20
36        self.rtt_max_failure_rate_one_sided_rtt_percentage = 50
37        self.rtt_max_margin_exceeded_rate_two_sided_rtt_percentage = 10
38        self.rtt_max_margin_exceeded_rate_one_sided_rtt_percentage = 50
39        self.rtt_min_expected_rssi_dbm = -100
40
41        for ad in self.android_devices:
42            utils.set_location_service(ad, True)
43            asserts.skip_if(
44                not ad.droid.doesDeviceSupportWifiRttFeature(),
45                "Device under test does not support Wi-Fi RTT - skipping test")
46            wutils.wifi_toggle_state(ad, True)
47            rtt_avail = ad.droid.wifiIsRttAvailable()
48            if not rtt_avail:
49                self.log.info('RTT not available. Waiting ...')
50                rutils.wait_for_event(ad, rconsts.BROADCAST_WIFI_RTT_AVAILABLE)
51            ad.ed.clear_all_events()
52            rutils.config_privilege_override(ad, False)
53            wutils.set_wifi_country_code(ad, wutils.WifiEnums.CountryCode.US)
54            ad.rtt_capabilities = rutils.get_rtt_capabilities(ad)
55
56    def teardown_test(self):
57        for ad in self.android_devices:
58            if not ad.droid.doesDeviceSupportWifiRttFeature():
59                return
60
61            # clean-up queue from the System Service UID
62            ad.droid.wifiRttCancelRanging([1000])
63
64    def on_fail(self, test_name, begin_time):
65        for ad in self.android_devices:
66            ad.take_bug_report(test_name, begin_time)
67            ad.cat_adb_log(test_name, begin_time)
68