1#!/usr/bin/python3.4
2#
3#   Copyright 2018 - 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
17import queue
18import time
19
20from acts import asserts
21from acts.test_utils.wifi.aware import aware_const as aconsts
22from acts.test_utils.wifi.aware import aware_test_utils as autils
23from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest
24from acts.test_utils.wifi.rtt import rtt_const as rconsts
25from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
26from acts.test_utils.wifi.rtt.RttBaseTest import RttBaseTest
27
28
29class StressRangeAwareTest(AwareBaseTest, RttBaseTest):
30    """Test class for stress testing of RTT ranging to Wi-Fi Aware peers."""
31    SERVICE_NAME = "GoogleTestServiceXY"
32
33    def setup_test(self):
34        """Manual setup here due to multiple inheritance: explicitly execute the
35    setup method from both parents."""
36        AwareBaseTest.setup_test(self)
37        RttBaseTest.setup_test(self)
38
39    def teardown_test(self):
40        """Manual teardown here due to multiple inheritance: explicitly execute the
41    teardown method from both parents."""
42        AwareBaseTest.teardown_test(self)
43        RttBaseTest.teardown_test(self)
44
45    #############################################################################
46
47    def run_rtt_discovery(self, init_dut, resp_mac=None, resp_peer_id=None):
48        """Perform single RTT measurement, using Aware, from the Initiator DUT to
49    a Responder. The RTT Responder can be specified using its MAC address
50    (obtained using out- of-band discovery) or its Peer ID (using Aware
51    discovery).
52
53    Args:
54      init_dut: RTT Initiator device
55      resp_mac: MAC address of the RTT Responder device
56      resp_peer_id: Peer ID of the RTT Responder device
57    """
58        asserts.assert_true(
59            resp_mac is not None or resp_peer_id is not None,
60            "One of the Responder specifications (MAC or Peer ID)"
61            " must be provided!")
62        if resp_mac is not None:
63            id = init_dut.droid.wifiRttStartRangingToAwarePeerMac(resp_mac)
64        else:
65            id = init_dut.droid.wifiRttStartRangingToAwarePeerId(resp_peer_id)
66        try:
67            event = init_dut.ed.pop_event(
68                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT, id),
69                rutils.EVENT_TIMEOUT)
70            result = event["data"][rconsts.EVENT_CB_RANGING_KEY_RESULTS][0]
71            if resp_mac is not None:
72                rutils.validate_aware_mac_result(result, resp_mac, "DUT")
73            else:
74                rutils.validate_aware_peer_id_result(result, resp_peer_id,
75                                                     "DUT")
76            return result
77        except queue.Empty:
78            return None
79
80    def test_stress_rtt_ib_discovery_set(self):
81        """Perform a set of RTT measurements, using in-band (Aware) discovery, and
82    switching Initiator and Responder roles repeatedly.
83
84    Stress test: repeat ranging operations. Verify rate of success and
85    stability of results.
86    """
87        p_dut = self.android_devices[0]
88        s_dut = self.android_devices[1]
89
90        (p_id, s_id, p_disc_id, s_disc_id, peer_id_on_sub,
91         peer_id_on_pub) = autils.create_discovery_pair(
92             p_dut,
93             s_dut,
94             p_config=autils.add_ranging_to_pub(
95                 autils.create_discovery_config(
96                     self.SERVICE_NAME, aconsts.PUBLISH_TYPE_UNSOLICITED),
97                 True),
98             s_config=autils.add_ranging_to_pub(
99                 autils.create_discovery_config(
100                     self.SERVICE_NAME, aconsts.SUBSCRIBE_TYPE_PASSIVE), True),
101             device_startup_offset=self.device_startup_offset,
102             msg_id=self.get_next_msg_id())
103
104        results = []
105        start_clock = time.time()
106        iterations_done = 0
107        run_time = 0
108        while iterations_done < self.stress_test_min_iteration_count or (
109                self.stress_test_target_run_time_sec != 0
110                and run_time < self.stress_test_target_run_time_sec):
111            results.append(
112                self.run_rtt_discovery(p_dut, resp_peer_id=peer_id_on_pub))
113            results.append(
114                self.run_rtt_discovery(s_dut, resp_peer_id=peer_id_on_sub))
115
116            iterations_done = iterations_done + 1
117            run_time = time.time() - start_clock
118
119        stats = rutils.extract_stats(
120            results,
121            self.rtt_reference_distance_mm,
122            self.rtt_reference_distance_margin_mm,
123            self.rtt_min_expected_rssi_dbm,
124            summary_only=True)
125        self.log.debug("Stats: %s", stats)
126        asserts.assert_true(
127            stats['num_no_results'] == 0,
128            "Missing (timed-out) results",
129            extras=stats)
130        asserts.assert_false(
131            stats['any_lci_mismatch'], "LCI mismatch", extras=stats)
132        asserts.assert_false(
133            stats['any_lcr_mismatch'], "LCR mismatch", extras=stats)
134        asserts.assert_equal(
135            stats['num_invalid_rssi'], 0, "Invalid RSSI", extras=stats)
136        asserts.assert_true(
137            stats['num_failures'] <=
138            self.rtt_max_failure_rate_two_sided_rtt_percentage *
139            stats['num_results'] / 100,
140            "Failure rate is too high",
141            extras=stats)
142        asserts.assert_true(
143            stats['num_range_out_of_margin'] <=
144            self.rtt_max_margin_exceeded_rate_two_sided_rtt_percentage *
145            stats['num_success_results'] / 100,
146            "Results exceeding error margin rate is too high",
147            extras=stats)
148