• 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
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    def __init__(self, controllers):
27        if not hasattr(self, 'android_devices'):
28            super(AwareBaseTest, self).__init__(controllers)
29
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_test(self):
39        required_params = ("aware_default_power_mode", )
40        self.unpack_userparams(required_params)
41
42        for ad in self.android_devices:
43            asserts.skip_if(
44                not ad.droid.doesDeviceSupportWifiAwareFeature(),
45                "Device under test does not support Wi-Fi Aware - skipping test"
46            )
47            wutils.wifi_toggle_state(ad, True)
48            ad.droid.wifiP2pClose()
49            utils.set_location_service(ad, True)
50            aware_avail = ad.droid.wifiIsAwareAvailable()
51            if not aware_avail:
52                self.log.info('Aware not available. Waiting ...')
53                autils.wait_for_event(ad,
54                                      aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)
55            ad.ed.clear_all_events()
56            ad.aware_capabilities = autils.get_aware_capabilities(ad)
57            self.reset_device_parameters(ad)
58            self.reset_device_statistics(ad)
59            self.set_power_mode_parameters(ad)
60            ad.droid.wifiSetCountryCode(wutils.WifiEnums.CountryCode.US)
61            autils.configure_ndp_allow_any_override(ad, True)
62            # set randomization interval to 0 (disable) to reduce likelihood of
63            # interference in tests
64            autils.configure_mac_random_interval(ad, 0)
65
66    def teardown_test(self):
67        for ad in self.android_devices:
68            if not ad.droid.doesDeviceSupportWifiAwareFeature():
69                return
70            ad.droid.wifiP2pClose()
71            ad.droid.wifiAwareDestroyAll()
72            self.reset_device_parameters(ad)
73            autils.validate_forbidden_callbacks(ad)
74
75    def reset_device_parameters(self, ad):
76        """Reset device configurations which may have been set by tests. Should be
77    done before tests start (in case previous one was killed without tearing
78    down) and after they end (to leave device in usable state).
79
80    Args:
81      ad: device to be reset
82    """
83        ad.adb.shell("cmd wifiaware reset")
84
85    def reset_device_statistics(self, ad):
86        """Reset device statistics.
87
88    Args:
89        ad: device to be reset
90    """
91        ad.adb.shell("cmd wifiaware native_cb get_cb_count --reset")
92
93    def set_power_mode_parameters(self, ad):
94        """Set the power configuration DW parameters for the device based on any
95    configuration overrides (if provided)"""
96        if self.aware_default_power_mode == "INTERACTIVE":
97            autils.config_settings_high_power(ad)
98        elif self.aware_default_power_mode == "NON_INTERACTIVE":
99            autils.config_settings_low_power(ad)
100        else:
101            asserts.assert_false(
102                "The 'aware_default_power_mode' configuration must be INTERACTIVE or "
103                "NON_INTERACTIVE")
104
105    def get_next_msg_id(self):
106        """Increment the message ID and returns the new value. Guarantees that
107    each call to the method returns a unique value.
108
109    Returns: a new message id value.
110    """
111        self.msg_id = self.msg_id + 1
112        return self.msg_id
113
114    def on_fail(self, test_name, begin_time):
115        for ad in self.android_devices:
116            ad.take_bug_report(test_name, begin_time)
117            ad.cat_adb_log(test_name, begin_time)
118