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 # As a temporary fix, directly set the test with tpl 10 43 if tpl != 10: 44 self.generate_test_case(codec_config, tpl) 45 46 def setup_test(self): 47 super().setup_test() 48 btutils.connect_phone_to_headset(self.dut, self.bt_device, 60) 49 vol = self.dut.droid.getMaxMediaVolume() * self.volume 50 self.dut.droid.setMediaVolume(0) 51 time.sleep(1) 52 self.dut.droid.setMediaVolume(int(vol)) 53 54 def generate_test_case(self, codec_config, tpl): 55 def test_case_fn(): 56 self.measure_a2dp_power(codec_config, tpl) 57 58 power_level = 'PL{}'.format(tpl) 59 60 # If the device is P21 and later, generate tests with different name. 61 platform = self._get_hardware_platform_at_init_stage() 62 self.log.info('Hardware Platform is: {}'.format(platform)) 63 if platform.startswith('gs'): 64 power_level = PL_MAP[str(tpl)] 65 self.log.info('The device is P21 or later, use name {}'.format( 66 power_level)) 67 68 test_case_name = ('test_BTa2dp_{}_codec_at_{}'.format( 69 codec_config['codec_type'], power_level)) 70 setattr(self, test_case_name, test_case_fn) 71 72 def _get_hardware_platform_at_init_stage(self): 73 74 # At __init__ stage the android devices are not registered. Thus, run 75 # adb command with device sn directly. 76 sn = self.controller_configs['AndroidDevice'][0] 77 cmd = 'adb -s {} shell {}'.format(sn, GET_PROPERTY_HARDWARE_PLATFORM) 78 result = job.run(cmd, ignore_status=True, timeout=DEFAULT_ADB_TIMEOUT) 79 ret, out, err = result.exit_status, result.stdout, result.stderr 80 self.log.info('get platform ret: {}, out: {}, err: {}'.format( 81 ret, out, err)) 82 return out 83 84 def measure_a2dp_power(self, codec_config, tpl): 85 86 current_codec = self.dut.droid.bluetoothA2dpGetCurrentCodecConfig() 87 current_codec_type = BtEnum.BluetoothA2dpCodecType( 88 current_codec['codecType']).name 89 if current_codec_type != codec_config['codec_type']: 90 codec_set = btutils.set_bluetooth_codec(self.dut, **codec_config) 91 asserts.assert_true(codec_set, 'Codec configuration failed.') 92 else: 93 self.log.info('Current Codec is {}, no need to change'.format( 94 current_codec_type)) 95 # Start music playing 96 self.media.play() 97 time.sleep(EXTRA_PLAY_TIME) 98 99 # Set attenuation so BT tx at desired power level 100 self.log.info('Current Attenuation {} dB'.format( 101 self.attenuator.get_atten())) 102 tpl = 'PL' + str(tpl) 103 PBtBT.ramp_attenuation(self.attenuator, self.atten_pl_settings[tpl][0], 104 attenuation_step_max=1, time_wait_in_between=1) 105 self.log.info('Setting Attenuator to {} dB'.format( 106 self.atten_pl_settings[tpl][0])) 107 108 self.log.info('Running A2DP with codec {} at {}'.format( 109 codec_config['codec_type'], tpl)) 110 self.dut.droid.goToSleepNow() 111 self.measure_power_and_validate() 112 113 def test_BTa2dp_AAC_codec_at_EPA_BF(self): 114 self.measure_a2dp_power(self.codecs[0], 10) 115 116 def test_BTa2dp_SBC_codec_at_EPA_BF(self): 117 self.measure_a2dp_power(self.codecs[1], 10)