1#!/usr/bin/python3.4
2#
3#   Copyright 2017 - 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 random
18import time
19
20from acts import asserts
21from acts.test_decorators import test_tracker_info
22from acts.test_utils.wifi.rtt import rtt_const as rconsts
23from acts.test_utils.wifi.rtt import rtt_test_utils as rutils
24from acts.test_utils.wifi.rtt.RttBaseTest import RttBaseTest
25
26
27class RttRequestManagementTest(RttBaseTest):
28    """Test class for RTT request management flows."""
29
30    SPAMMING_LIMIT = 20
31
32    #############################################################################
33
34    @test_tracker_info(uuid="29ff4a02-2952-47df-bf56-64f30c963093")
35    def test_cancel_ranging(self):
36        """Request a 'large' number of range operations with various UIDs (using the
37    work-source API), then cancel some of them.
38
39    We can't guarantee a reaction time - it is possible that a cancelled test
40    was already finished and it's results dispatched back. The test therefore
41    stacks the request queue. The sequence is:
42
43    - Request:
44      - 50 tests @ UIDs = {uid1, uid2, uid3}
45      - 2 tests @ UIDs = {uid2, uid3}
46      - 1 test2 @ UIDs = {uid1, uid2, uid3}
47    - Cancel UIDs = {uid2, uid3}
48
49    Expect to receive only 51 results.
50    """
51        dut = self.android_devices[0]
52        max_peers = dut.droid.wifiRttMaxPeersInRequest()
53
54        all_uids = [1000, 20,
55                    30]  # 1000 = System Server (makes requests foreground)
56        some_uids = [20, 30]
57
58        aps = rutils.select_best_scan_results(
59            rutils.scan_with_rtt_support_constraint(dut, True, repeat=10),
60            select_count=1)
61        dut.log.info("RTT Supporting APs=%s", aps)
62
63        asserts.assert_true(
64            len(aps) > 0, "Need at least one AP which supports 802.11mc!")
65        if len(aps) > max_peers:
66            aps = aps[0:max_peers]
67
68        group1_ids = []
69        group2_ids = []
70        group3_ids = []
71
72        # step 1: request <spam_limit> ranging operations on [uid1, uid2, uid3]
73        for i in range(self.SPAMMING_LIMIT):
74            group1_ids.append(
75                dut.droid.wifiRttStartRangingToAccessPoints(aps, all_uids))
76
77        # step 2: request 2 ranging operations on [uid2, uid3]
78        for i in range(2):
79            group2_ids.append(
80                dut.droid.wifiRttStartRangingToAccessPoints(aps, some_uids))
81
82        # step 3: request 1 ranging operation on [uid1, uid2, uid3]
83        for i in range(1):
84            group3_ids.append(
85                dut.droid.wifiRttStartRangingToAccessPoints(aps, all_uids))
86
87        # step 4: cancel ranging requests on [uid2, uid3]
88        dut.droid.wifiRttCancelRanging(some_uids)
89
90        # collect results
91        for i in range(len(group1_ids)):
92            rutils.wait_for_event(
93                dut,
94                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
95                                      group1_ids[i]))
96        time.sleep(
97            rutils.EVENT_TIMEOUT)  # optimize time-outs below to single one
98        for i in range(len(group2_ids)):
99            rutils.fail_on_event(
100                dut,
101                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
102                                      group2_ids[i]), 0)
103        for i in range(len(group3_ids)):
104            rutils.wait_for_event(
105                dut,
106                rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
107                                      group3_ids[i]))
108
109    @test_tracker_info(uuid="48297480-c026-4780-8c13-476e7bea440c")
110    def test_throttling(self):
111        """Request sequential range operations using a bogus UID (which will
112    translate as a throttled process) and similarly using the ACTS/sl4a as
113    the source (a foreground/unthrottled process)."""
114        dut = self.android_devices[0]
115        max_peers = dut.droid.wifiRttMaxPeersInRequest()
116
117        # Need to use a random number since the system keeps states and so the
118        # background uid will be throttled on the next run of this script
119        fake_uid = [random.randint(10, 9999)]
120
121        aps = rutils.select_best_scan_results(
122            rutils.scan_with_rtt_support_constraint(dut, True, repeat=10),
123            select_count=1)
124        dut.log.info("RTT Supporting APs=%s", aps)
125
126        asserts.assert_true(
127            len(aps) > 0, "Need at least one AP which supports 802.11mc!")
128        if len(aps) > max_peers:
129            aps = aps[0:max_peers]
130
131        id1 = dut.droid.wifiRttStartRangingToAccessPoints(aps)  # as ACTS/sl4a
132        id2 = dut.droid.wifiRttStartRangingToAccessPoints(aps, fake_uid)
133        id3 = dut.droid.wifiRttStartRangingToAccessPoints(aps, fake_uid)
134        id4 = dut.droid.wifiRttStartRangingToAccessPoints(aps)  # as ACTS/sl4a
135
136        rutils.wait_for_event(
137            dut, rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
138                                       id1))
139        rutils.wait_for_event(
140            dut, rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
141                                       id2))
142        rutils.wait_for_event(
143            dut, rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_FAIL, id3))
144        rutils.wait_for_event(
145            dut, rutils.decorate_event(rconsts.EVENT_CB_RANGING_ON_RESULT,
146                                       id4))
147