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 time 18import acts_contrib.test_utils.bt.bt_test_utils as btutils 19import acts_contrib.test_utils.power.PowerBTBaseTest as PBtBT 20from acts import asserts 21from acts_contrib.test_utils.bt import BtEnum 22from acts.libs.proc import job 23 24DEFAULT_ADB_TIMEOUT = 60 25EXTRA_PLAY_TIME = 10 26GET_PROPERTY_HARDWARE_PLATFORM = 'getprop ro.boot.hardware.platform' 27PL_MAP = { 28 '10': 'EPA_BF', 29 '9': 'EPA_DIV', 30 '8': 'IPA_BF', 31 '7': 'IPA_DIV', 32} 33 34class PowerBTa2dpTest(PBtBT.PowerBTBaseTest): 35 def __init__(self, configs): 36 super().__init__(configs) 37 req_params = ['codecs', 'tx_power_levels', 'atten_pl_settings'] 38 self.unpack_userparams(req_params) 39 # Loop all codecs and tx power levels 40 for codec_config in self.codecs: 41 for tpl in self.tx_power_levels: 42 self.generate_test_case(codec_config, tpl) 43 44 def setup_test(self): 45 super().setup_test() 46 btutils.connect_phone_to_headset(self.dut, self.bt_device, 60) 47 vol = self.dut.droid.getMaxMediaVolume() * self.volume 48 self.dut.droid.setMediaVolume(0) 49 time.sleep(1) 50 self.dut.droid.setMediaVolume(int(vol)) 51 52 def generate_test_case(self, codec_config, tpl): 53 def test_case_fn(): 54 self.measure_a2dp_power(codec_config, tpl) 55 56 power_level = 'PL{}'.format(tpl) 57 58 # If the device is P21 and later, generate tests with different name. 59 platform = self._get_hardware_platform_at_init_stage() 60 self.log.info('Hardware Platform is: {}'.format(platform)) 61 if platform.startswith('gs'): 62 power_level = PL_MAP[str(tpl)] 63 self.log.info('The device is P21 or later, use name {}'.format( 64 power_level)) 65 66 test_case_name = ('test_BTa2dp_{}_codec_at_{}'.format( 67 codec_config['codec_type'], power_level)) 68 setattr(self, test_case_name, test_case_fn) 69 70 def _get_hardware_platform_at_init_stage(self): 71 72 # At __init__ stage the android devices are not registered. Thus, run 73 # adb command with device sn directly. 74 sn = self.controller_configs['AndroidDevice'][0] 75 cmd = 'adb -s {} shell {}'.format(sn, GET_PROPERTY_HARDWARE_PLATFORM) 76 result = job.run(cmd, ignore_status=True, timeout=DEFAULT_ADB_TIMEOUT) 77 ret, out, err = result.exit_status, result.stdout, result.stderr 78 self.log.info('get platform ret: {}, out: {}, err: {}'.format( 79 ret, out, err)) 80 return out 81 82 def measure_a2dp_power(self, codec_config, tpl): 83 84 current_codec = self.dut.droid.bluetoothA2dpGetCurrentCodecConfig() 85 current_codec_type = BtEnum.BluetoothA2dpCodecType( 86 current_codec['codecType']).name 87 if current_codec_type != codec_config['codec_type']: 88 codec_set = btutils.set_bluetooth_codec(self.dut, **codec_config) 89 asserts.assert_true(codec_set, 'Codec configuration failed.') 90 else: 91 self.log.info('Current Codec is {}, no need to change'.format( 92 current_codec_type)) 93 # Start music playing 94 self.media.play() 95 time.sleep(EXTRA_PLAY_TIME) 96 97 # Set attenuation so BT tx at desired power level 98 self.log.info('Current Attenuation {} dB'.format( 99 self.attenuator.get_atten())) 100 tpl = 'PL' + str(tpl) 101 PBtBT.ramp_attenuation(self.attenuator, self.atten_pl_settings[tpl][0], 102 attenuation_step_max=1, time_wait_in_between=1) 103 self.log.info('Setting Attenuator to {} dB'.format( 104 self.atten_pl_settings[tpl][0])) 105 106 self.log.info('Running A2DP with codec {} at {}'.format( 107 codec_config['codec_type'], tpl)) 108 self.dut.droid.goToSleepNow() 109 self.measure_power_and_validate() 110