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