• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - 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 os
18import time
19import acts_contrib.test_utils.bt.bt_power_test_utils as btputils
20import acts_contrib.test_utils.bt.bt_test_utils as btutils
21import acts_contrib.test_utils.power.PowerBaseTest as PBT
22from acts_contrib.test_utils.abstract_devices.bluetooth_handsfree_abstract_device import BluetoothHandsfreeAbstractDeviceFactory as bt_factory
23from math import copysign
24
25BLE_LOCATION_SCAN_DISABLE = 'settings put secure location_mode 0'
26PHONE_MUSIC_FILE_DIRECTORY = '/sdcard/Music'
27INIT_ATTEN = [0]
28
29
30def ramp_attenuation(obj_atten, attenuation_target, attenuation_step_max=20,
31                    time_wait_in_between=5 ):
32    """Ramp the attenuation up or down for BT tests.
33
34    Ramp the attenuation slowly so it won't have dramatic signal drop to affect
35    Link.
36
37    Args:
38        obj_atten: attenuator object, a single port attenuator
39        attenuation_target: target attenuation level to reach to.
40        attenuation_step_max: max step for attenuation set
41        time_wait_in_between: wait time between attenuation changes
42    """
43    sign = lambda x: copysign(1, x)
44    attenuation_delta = obj_atten.get_atten() - attenuation_target
45    while abs(attenuation_delta) > attenuation_step_max:
46        attenuation_intermediate = obj_atten.get_atten(
47        ) - sign(attenuation_delta) * attenuation_step_max
48        obj_atten.set_atten(attenuation_intermediate)
49        time.sleep(time_wait_in_between)
50        attenuation_delta = obj_atten.get_atten() - attenuation_target
51    obj_atten.set_atten(attenuation_target)
52    time.sleep(time_wait_in_between)
53
54
55class PowerBTBaseTest(PBT.PowerBaseTest):
56    """Base class for BT power related tests.
57
58    Inherited from the PowerBaseTest class
59    """
60    def setup_class(self):
61
62        super().setup_class()
63        # Get music file and push it to the phone
64        music_files = self.user_params.get('music_files', [])
65        if music_files:
66            music_src = music_files[0]
67            music_dest = PHONE_MUSIC_FILE_DIRECTORY
68            success = self.dut.push_system_file(music_src, music_dest)
69            if success:
70                self.music_file = os.path.join(PHONE_MUSIC_FILE_DIRECTORY,
71                                               os.path.basename(music_src))
72            # Initialize media_control class
73            self.media = btputils.MediaControl(self.dut, self.music_file)
74        # Set Attenuator to the initial attenuation
75        if hasattr(self, 'attenuators'):
76            self.set_attenuation(INIT_ATTEN)
77            self.attenuator = self.attenuators[0]
78        # Create the BTOE(Bluetooth-Other-End) device object
79        bt_devices = self.user_params.get('bt_devices', [])
80        if bt_devices:
81            attr, idx = bt_devices.split(':')
82            self.bt_device_controller = getattr(self, attr)[int(idx)]
83            self.bt_device = bt_factory().generate(self.bt_device_controller)
84        else:
85            self.log.error('No BT devices config is provided!')
86        # Turn off screen as all tests will be screen off
87        self.dut.droid.goToSleepNow()
88
89    def setup_test(self):
90
91        super().setup_test()
92        self.unpack_userparams(volume=0.9)
93        # Reset BT to factory defaults
94        self.dut.droid.bluetoothFactoryReset()
95        self.bt_device.reset()
96        self.bt_device.power_on()
97        btutils.enable_bluetooth(self.dut.droid, self.dut.ed)
98
99    def teardown_test(self):
100        """Tear down necessary objects after test case is finished.
101
102        Bring down the AP interface, delete the bridge interface, stop the
103        packet sender, and reset the ethernet interface for the packet sender
104        """
105        super().teardown_test()
106        self.dut.droid.bluetoothFactoryReset()
107        self.dut.adb.shell(BLE_LOCATION_SCAN_DISABLE)
108        if hasattr(self, 'media'):
109            self.media.stop()
110        # Set Attenuator to the initial attenuation
111        if hasattr(self, 'attenuators'):
112            self.set_attenuation(INIT_ATTEN)
113        self.bt_device.reset()
114        self.bt_device.power_off()
115        btutils.disable_bluetooth(self.dut.droid)
116
117    def teardown_class(self):
118        """Clean up the test class after tests finish running
119
120        """
121        super().teardown_class()
122        self.dut.droid.bluetoothFactoryReset()
123        self.dut.adb.shell(BLE_LOCATION_SCAN_DISABLE)
124        self.bt_device.reset()
125        self.bt_device.power_off()
126        btutils.disable_bluetooth(self.dut.droid)
127