• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
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.aware import aware_const as aconsts
26from acts_contrib.test_utils.wifi.aware import aware_test_utils as autils
27
28
29class AwareBaseTest(BaseTestClass):
30    # message ID counter to make sure all uses are unique
31    msg_id = 0
32
33    # offset (in seconds) to separate the start-up of multiple devices.
34    # De-synchronizes the start-up time so that they don't start and stop scanning
35    # at the same time - which can lead to very long clustering times.
36    device_startup_offset = 2
37
38    def setup_class(self):
39        opt_param = ["ranging_role_concurrency_flexible_models"]
40        self.unpack_userparams(opt_param_names=opt_param)
41
42    def setup_test(self):
43        required_params = ("aware_default_power_mode",
44                           "dbs_supported_models",)
45        self.unpack_userparams(required_params)
46
47        wutils.start_all_wlan_logs(self.android_devices)
48        self.tcpdump_proc = []
49        if hasattr(self, "android_devices"):
50            for ad in self.android_devices:
51                proc = nutils.start_tcpdump(ad, self.test_name)
52                self.tcpdump_proc.append((ad, proc))
53
54        for ad in self.android_devices:
55            ad.droid.wifiEnableVerboseLogging(1)
56            asserts.skip_if(
57                not ad.droid.doesDeviceSupportWifiAwareFeature(),
58                "Device under test does not support Wi-Fi Aware - skipping test"
59            )
60            aware_avail = ad.droid.wifiIsAwareAvailable()
61            ad.droid.wifiP2pClose()
62            wutils.wifi_toggle_state(ad, True)
63            utils.set_location_service(ad, True)
64            if not aware_avail:
65                self.log.info('Aware not available. Waiting ...')
66                autils.wait_for_event(ad,
67                                      aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)
68            ad.aware_capabilities = autils.get_aware_capabilities(ad)
69            self.reset_device_parameters(ad)
70            self.reset_device_statistics(ad)
71            self.set_power_mode_parameters(ad)
72            wutils.set_wifi_country_code(ad, wutils.WifiEnums.CountryCode.US)
73            autils.configure_ndp_allow_any_override(ad, True)
74            # set randomization interval to 0 (disable) to reduce likelihood of
75            # interference in tests
76            autils.configure_mac_random_interval(ad, 0)
77            ad.ed.clear_all_events()
78
79    def teardown_test(self):
80        wutils.stop_all_wlan_logs(self.android_devices)
81        for proc in self.tcpdump_proc:
82            nutils.stop_tcpdump(
83                    proc[0], proc[1], self.test_name, pull_dump=False)
84        self.tcpdump_proc = []
85        for ad in self.android_devices:
86            if not ad.droid.doesDeviceSupportWifiAwareFeature():
87                return
88            ad.droid.wifiP2pClose()
89            ad.droid.wifiAwareDestroyAll()
90            self.reset_device_parameters(ad)
91            autils.validate_forbidden_callbacks(ad)
92
93    def reset_device_parameters(self, ad):
94        """Reset device configurations which may have been set by tests. Should be
95    done before tests start (in case previous one was killed without tearing
96    down) and after they end (to leave device in usable state).
97
98    Args:
99      ad: device to be reset
100    """
101        ad.adb.shell("cmd wifiaware reset")
102
103    def reset_device_statistics(self, ad):
104        """Reset device statistics.
105
106    Args:
107        ad: device to be reset
108    """
109        ad.adb.shell("cmd wifiaware native_cb get_cb_count --reset")
110
111    def set_power_mode_parameters(self, ad):
112        """Set the power configuration DW parameters for the device based on any
113    configuration overrides (if provided)"""
114        if self.aware_default_power_mode == "INTERACTIVE":
115            autils.config_settings_high_power(ad)
116        elif self.aware_default_power_mode == "NON_INTERACTIVE":
117            autils.config_settings_low_power(ad)
118        else:
119            asserts.assert_false(
120                "The 'aware_default_power_mode' configuration must be INTERACTIVE or "
121                "NON_INTERACTIVE")
122
123    def get_next_msg_id(self):
124        """Increment the message ID and returns the new value. Guarantees that
125    each call to the method returns a unique value.
126
127    Returns: a new message id value.
128    """
129        self.msg_id = self.msg_id + 1
130        return self.msg_id
131
132    def on_fail(self, test_name, begin_time):
133        for ad in self.android_devices:
134            ad.take_bug_report(test_name, begin_time)
135            ad.cat_adb_log(test_name, begin_time)
136            wutils.get_ssrdumps(ad)
137        wutils.stop_all_wlan_logs(self.android_devices)
138        for ad in self.android_devices:
139            wutils.get_wlan_logs(ad)
140        for proc in self.tcpdump_proc:
141            nutils.stop_tcpdump(proc[0], proc[1], self.test_name)
142        self.tcpdump_proc = []
143