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