1#/usr/bin/env python3.4 2# 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy of 7# 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, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations under 15# the License. 16""" 17This test script exercises power test scenarios for different scan modes. 18This test script was designed with this setup in mind: 19Shield box one: Android Device and Monsoon tool box 20""" 21 22import json 23import os 24 25from acts import asserts 26from acts.controllers import monsoon 27from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 28from acts.test_utils.bt.BleEnum import ScanSettingsScanMode 29from acts.test_utils.bt.bt_test_utils import bluetooth_enabled_check 30from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects 31from acts.test_utils.tel.tel_test_utils import set_phone_screen_on 32from acts.test_utils.wifi import wifi_test_utils as wutils 33from acts.utils import create_dir 34from acts.utils import force_airplane_mode 35from acts.utils import get_current_human_time 36from acts.utils import set_location_service 37from acts.utils import set_adaptive_brightness 38from acts.utils import set_ambient_display 39from acts.utils import set_auto_rotate 40from acts.utils import sync_device_time 41 42# Monsoon output Voltage in V 43MONSOON_OUTPUT_VOLTAGE = 4.2 44# Monsoon output max current in A 45MONSOON_MAX_CURRENT = 7.8 46 47# Power mesaurement sampling rate in Hz 48BLE_SCAN_POWER_SAMPLING_RATE = 20 49# Power mesaurement sample duration in seconds 50BLE_SCAN_POWER_SAMPLE_TIME = 60 51# Power mesaurement start time in seconds 52BLE_SCAN_START_TIME = 30 53# BLE scanning time in seconds 54BLE_SCAN_DURATION = 5 55# BLE no scanning time in seconds 56BLE_NOSCAN_DURATION = 5 57 58start_pmc_cmd = ("am start -n com.android.pmc/com.android.pmc." 59 "PMCMainActivity") 60pmc_base_cmd = ("am broadcast -a com.android.pmc.BLESCAN --es ScanMode ") 61 62 63class BleScanPowerTest(BluetoothBaseTest): 64 SCREEN_TIME_OFF = 10 65 66 def setup_class(self): 67 self.ad = self.android_devices[0] 68 self.mon = self.monsoons[0] 69 self.mon.set_voltage(MONSOON_OUTPUT_VOLTAGE) 70 self.mon.set_max_current(MONSOON_MAX_CURRENT) 71 # Monsoon phone 72 self.mon.attach_device(self.ad) 73 self.monsoon_log_path = os.path.join(self.log_path, "MonsoonLog") 74 create_dir(self.monsoon_log_path) 75 76 asserts.assert_true( 77 self.mon.usb("auto"), 78 "Failed to turn USB mode to auto on monsoon.") 79 80 sync_device_time(self.ad) 81 82 asserts.assert_true( 83 force_airplane_mode(self.ad, True), 84 "Can not turn on airplane mode on: %s" % self.ad.serial) 85 asserts.assert_true( 86 bluetooth_enabled_check(self.ad), 87 "Failed to set Bluetooth state to enabled") 88 set_location_service(self.ad, False) 89 set_adaptive_brightness(self.ad, False) 90 set_ambient_display(self.ad, False) 91 self.ad.adb.shell("settings put system screen_brightness 0") 92 set_auto_rotate(self.ad, False) 93 set_phone_screen_on(self.log, self.ad, self.SCREEN_TIME_OFF) 94 95 # Start pmc app. 96 self.ad.adb.shell(start_pmc_cmd) 97 self.ad.adb.shell("setprop log.tag.PMC VERBOSE") 98 wutils.wifi_toggle_state(self.ad, False) 99 100 def _save_logs_for_power_test(self, monsoon_result): 101 """utility function to save power data into log file. 102 103 "whether monsoon_log_for_power_test" needs to be set 104 in config file in order to save power data into a file 105 If bug_report_for_power_test is defined in test config file 106 it will also save a bug report. 107 108 Steps: 109 1. Save power data into a file if being configed. 110 2. Create a bug report if being configed 111 112 Args: 113 monsoon_result: power data object 114 115 Returns: 116 If success, return None. 117 if fail, exception will be thrown 118 """ 119 current_time = get_current_human_time() 120 file_name = "%s_%s" % (self.current_test_name, current_time) 121 monsoon_result.save_to_text_file( 122 [monsoon_result], os.path.join(self.monsoon_log_path, file_name)) 123 124 self.ad.take_bug_report(self.current_test_name, current_time) 125 126 def _test_power_for_scan(self, scan_mode): 127 """utility function for power test with BLE scan. 128 129 Steps: 130 1. Prepare adb shell command 131 2. Send the adb shell command to PMC 132 3. PMC start first alarm to start scan 133 4. After first alarm triggered it start the second alarm to stop scan 134 5. Save the power usage data into log file 135 136 Args: 137 scan_mode: scan mode 138 139 Returns: 140 If success, return None. 141 if fail, error will be logged. 142 """ 143 144 msg = "%s%s --es StartTime %d --es ScanTime %d" % ( 145 pmc_base_cmd, scan_mode, BLE_SCAN_START_TIME, 146 BLE_SCAN_POWER_SAMPLE_TIME) 147 self.ad.log.info("Send broadcast message: %s", msg) 148 self.ad.adb.shell(msg) 149 # Start the power measurement 150 result = self.mon.measure_power( 151 BLE_SCAN_POWER_SAMPLING_RATE, BLE_SCAN_POWER_SAMPLE_TIME, 152 self.current_test_name, BLE_SCAN_START_TIME) 153 154 self._save_logs_for_power_test(result) 155 156 @BluetoothBaseTest.bt_test_wrap 157 def test_power_for_scan_w_low_latency(self): 158 """Test power usage when scan with low latency. 159 160 Tests power usage when the device scans with low latency mode 161 for 60 seconds where there are no advertisements. 162 163 Steps: 164 1. Prepare adb shell command 165 2. Send the adb shell command to PMC 166 3. PMC start first alarm to start scan 167 4. After first alarm triggered it start the second alarm to stop scan 168 5. Save the power usage data into log file 169 170 Expected Result: 171 power consumption results 172 173 TAGS: LE, Scanning, Power 174 Priority: 3 175 """ 176 self._test_power_for_scan( 177 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value) 178 179 @BluetoothBaseTest.bt_test_wrap 180 def test_power_for_scan_w_balanced(self): 181 """Test power usage when scan with balanced mode. 182 183 Tests power usage when the device scans with balanced mode 184 for 60 seconds where there are no advertisements. 185 186 Steps: 187 1. Prepare adb shell command 188 2. Send the adb shell command to PMC 189 3. PMC start first alarm to start scan 190 4. After first alarm triggered it start the second alarm to stop scan 191 5. Save the power usage data into log file 192 193 Expected Result: 194 power consumption results 195 196 TAGS: LE, Scanning, Power 197 Priority: 3 198 """ 199 self._test_power_for_scan( 200 ScanSettingsScanMode.SCAN_MODE_BALANCED.value) 201 202 @BluetoothBaseTest.bt_test_wrap 203 def test_power_for_scan_w_low_power(self): 204 """Test power usage when scan with low power. 205 206 Tests power usage when the device scans with low power mode 207 for 60 seconds where there are no advertisements. 208 209 Steps: 210 1. Prepare adb shell command 211 2. Send the adb shell command to PMC 212 3. PMC start first alarm to start scan 213 4. After first alarm triggered it start the second alarm to stop scan 214 5. Save the power usage data into log file 215 216 Expected Result: 217 power consumption results 218 219 TAGS: LE, Scanning, Power 220 Priority: 3 221 """ 222 self._test_power_for_scan( 223 ScanSettingsScanMode.SCAN_MODE_LOW_POWER.value) 224 225 @BluetoothBaseTest.bt_test_wrap 226 def test_power_for_intervaled_scans_w_balanced(self): 227 """Test power usage when 12 intervaled scans with balanced mode 228 229 Tests power usage when the device perform 12 intervaled scans with 230 balanced mode for 5 seconds each where there are no advertisements. 231 232 Steps: 233 1. Prepare adb shell command 234 2. Send the adb shell command to PMC 235 3. PMC start first alarm to start scan 236 4. After first alarm triggered it starts the second alarm to stop scan 237 5. After second alarm triggered it starts the third alarm to start scan 238 6. Repeat the alarms until 12 scans are done 239 240 Expected Result: 241 power consumption results 242 243 TAGS: LE, Scanning, Power 244 Priority: 3 245 """ 246 msg1 = "%s%s --es StartTime %d --es ScanTime %d" % ( 247 pmc_base_cmd, ScanSettingsScanMode.SCAN_MODE_BALANCED.value, 248 BLE_SCAN_START_TIME, BLE_SCAN_DURATION) 249 msg = "%s --es NoScanTime %d --es Repetitions %d" % ( 250 msg1, BLE_NOSCAN_DURATION, 12) 251 252 self.ad.log.info("Send broadcast message: %s", msg) 253 self.ad.adb.shell(msg) 254 # Start the power measurement 255 result = self.mon.measure_power( 256 BLE_SCAN_POWER_SAMPLING_RATE, 257 (BLE_SCAN_DURATION + BLE_NOSCAN_DURATION) * 12, 258 self.current_test_name, BLE_SCAN_START_TIME) 259 260 self._save_logs_for_power_test(result) 261