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