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 """ 17 This test script exercises power test scenarios for different scan modes. 18 This test script was designed with this setup in mind: 19 Shield box one: Android Device and Monsoon tool box 20 """ 21 22 import json 23 import os 24 25 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 26 from acts.test_utils.bt.BleEnum import ScanSettingsScanMode 27 from acts.test_utils.bt.bt_test_utils import bluetooth_enabled_check 28 from acts.test_utils.bt.bt_test_utils import disable_bluetooth 29 from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects 30 from acts.test_utils.bt.PowerBaseTest import PowerBaseTest 31 32 33 class BleScanPowerTest(PowerBaseTest): 34 # Repetitions for scan and idle 35 REPETITIONS_40 = 40 36 REPETITIONS_360 = 360 37 38 # Power measurement start time in seconds 39 SCAN_START_TIME = 60 40 # BLE scanning time in seconds 41 SCAN_TIME_60 = 60 42 SCAN_TIME_5 = 5 43 # BLE no scanning time in seconds 44 IDLE_TIME_30 = 30 45 IDLE_TIME_5 = 5 46 47 PMC_BASE_CMD = ("am broadcast -a com.android.pmc.BLESCAN --es ScanMode ") 48 # Log file name 49 LOG_FILE = "BLEPOWER.log" 50 51 def setup_class(self): 52 super(BleScanPowerTest, self).setup_class() 53 # Get power test device serial number 54 power_test_device_serial = self.user_params["PowerTestDevice"] 55 # If there are multiple devices in the shield box turn off 56 # all of them except the one for the power testing 57 if len(self.android_devices) > 1: 58 for ad in self.android_devices: 59 if ad.serial != power_test_device_serial[0]: 60 self.ad.log.info("Disable BT for %s != %s", ad.serial, 61 power_test_device_serial[0]) 62 disable_bluetooth(ad.droid) 63 64 def _measure_power_for_scan_n_log_data(self, scan_mode, scan_time, 65 idle_time, repetitions): 66 """utility function for power test with BLE scan. 67 68 Steps: 69 1. Prepare adb shell command 70 2. Send the adb shell command to PMC 71 3. PMC start first alarm to start scan 72 4. After first alarm triggered it start the second alarm to stop scan 73 5. Repeat the scan/idle cycle for the number of repetitions 74 6. Save the power usage data into log file 75 76 Args: 77 scan_mode: Scan mode 78 scan_time: Time duration for scanning 79 idle_time: Time duration for idle after scanning 80 repetitions: The number of cycles of scanning/idle 81 82 Returns: 83 None 84 """ 85 86 first_part_msg = "%s%s --es StartTime %d --es ScanTime %d" % ( 87 self.PMC_BASE_CMD, scan_mode, self.SCAN_START_TIME, scan_time) 88 msg = "%s --es NoScanTime %d --es Repetitions %d" % ( 89 first_part_msg, idle_time, repetitions) 90 91 self.ad.log.info("Send broadcast message: %s", msg) 92 self.ad.adb.shell(msg) 93 94 # Check if PMC is ready 95 if not self.check_pmc_status(self.LOG_FILE, "READY", 96 "PMC is not ready"): 97 return 98 99 # Start the power measurement 100 sample_time = (scan_time + idle_time) * repetitions 101 result = self.mon.measure_power(self.POWER_SAMPLING_RATE, sample_time, 102 self.current_test_name, 103 self.SCAN_START_TIME) 104 105 self.ad.log.info("Monsoon start_time: {}".format(result.timestamps[0])) 106 107 start_times = [] 108 end_times = [] 109 json_data = self.check_pmc_timestamps(self.LOG_FILE) 110 for timestamp in json_data: 111 start_times.append(timestamp["StartTime"]) 112 end_times.append(timestamp["EndTime"]) 113 114 self.ad.log.info("Number of test cycles: {}".format(len(start_times))) 115 116 self.save_logs_for_power_test(result, start_times, end_times, False) 117 118 @BluetoothBaseTest.bt_test_wrap 119 def test_power_for_scan_w_low_latency(self): 120 """Test power usage when scan with low latency. 121 122 Tests power usage when the device scans with low latency mode 123 for 60 seconds and then idle for 30 seconds, repeat for 60 minutes 124 where there are no advertisements. 125 126 Steps: 127 1. Prepare adb shell command 128 2. Send the adb shell command to PMC 129 3. PMC start first alarm to start scan 130 4. After first alarm triggered it start the second alarm to stop scan 131 5. Repeat the cycle for 60 minutes 132 6. Save the power usage data into log file 133 134 Expected Result: 135 power consumption results 136 137 TAGS: LE, Scanning, Power 138 Priority: 3 139 """ 140 self._measure_power_for_scan_n_log_data( 141 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value, 142 self.SCAN_TIME_60, self.IDLE_TIME_30, self.REPETITIONS_40) 143 144 @BluetoothBaseTest.bt_test_wrap 145 def test_power_for_scan_w_balanced(self): 146 """Test power usage when scan with balanced mode. 147 148 Tests power usage when the device scans with balanced mode 149 for 60 seconds and then idle for 30 seconds, repeat for 60 minutes 150 where there are no advertisements. 151 152 Steps: 153 1. Prepare adb shell command 154 2. Send the adb shell command to PMC 155 3. PMC start first alarm to start scan 156 4. After first alarm triggered it start the second alarm to stop scan 157 5. Repeat the cycle for 60 minutes 158 6. Save the power usage data into log file 159 160 Expected Result: 161 power consumption results 162 163 TAGS: LE, Scanning, Power 164 Priority: 3 165 """ 166 self._measure_power_for_scan_n_log_data( 167 ScanSettingsScanMode.SCAN_MODE_BALANCED.value, self.SCAN_TIME_60, 168 self.IDLE_TIME_30, self.REPETITIONS_40) 169 170 @BluetoothBaseTest.bt_test_wrap 171 def test_power_for_scan_w_low_power(self): 172 """Test power usage when scan with low power. 173 174 Tests power usage when the device scans with low power mode 175 for 60 seconds and then idle for 30 seconds, repeat for 60 minutes 176 where there are no advertisements. 177 178 Steps: 179 1. Prepare adb shell command 180 2. Send the adb shell command to PMC 181 3. PMC start first alarm to start scan 182 4. After first alarm triggered it start the second alarm to stop scan 183 5. Repeat the cycle for 60 minutes 184 6. Save the power usage data into log file 185 186 Expected Result: 187 power consumption results 188 189 TAGS: LE, Scanning, Power 190 Priority: 3 191 """ 192 self._measure_power_for_scan_n_log_data( 193 ScanSettingsScanMode.SCAN_MODE_LOW_POWER.value, self.SCAN_TIME_60, 194 self.IDLE_TIME_30, self.REPETITIONS_40) 195 196 @BluetoothBaseTest.bt_test_wrap 197 def test_power_for_intervaled_scans_w_balanced(self): 198 """Test power usage when intervaled scans with balanced mode 199 200 Tests power usage when the device perform multiple intervaled scans with 201 balanced mode for 5 seconds each where there are no advertisements. 202 203 Steps: 204 1. Prepare adb shell command 205 2. Send the adb shell command to PMC 206 3. PMC start first alarm to start scan 207 4. After first alarm triggered it starts the second alarm to stop scan 208 5. After second alarm triggered it starts the third alarm to start scan 209 6. Repeat the alarms until 360 scans are done 210 7. Save the power usage data into log file 211 212 Expected Result: 213 power consumption results 214 215 TAGS: LE, Scanning, Power 216 Priority: 3 217 """ 218 self._measure_power_for_scan_n_log_data( 219 ScanSettingsScanMode.SCAN_MODE_BALANCED.value, self.SCAN_TIME_5, 220 self.IDLE_TIME_5, self.REPETITIONS_360) 221 222 @BluetoothBaseTest.bt_test_wrap 223 def test_power_for_intervaled_scans_w_low_latency(self): 224 """Test power usage when intervaled scans with low latency mode 225 226 Tests power usage when the device perform multiple intervaled scans with 227 low latency mode for 5 seconds each where there are no advertisements. 228 229 Steps: 230 1. Prepare adb shell command 231 2. Send the adb shell command to PMC 232 3. PMC start first alarm to start scan 233 4. After first alarm triggered it starts the second alarm to stop scan 234 5. After second alarm triggered it starts the third alarm to start scan 235 6. Repeat the alarms until 360 scans are done 236 7. Save the power usage data into log file 237 238 Expected Result: 239 power consumption results 240 241 TAGS: LE, Scanning, Power 242 Priority: 3 243 """ 244 self._measure_power_for_scan_n_log_data( 245 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value, self.SCAN_TIME_5, 246 self.IDLE_TIME_5, self.REPETITIONS_360) 247 248 @BluetoothBaseTest.bt_test_wrap 249 def test_power_for_intervaled_scans_w_low_power(self): 250 """Test power usage when intervaled scans with low power mode 251 252 Tests power usage when the device perform multiple intervaled scans with 253 low power mode for 5 seconds each where there are no advertisements. 254 255 Steps: 256 1. Prepare adb shell command 257 2. Send the adb shell command to PMC 258 3. PMC start first alarm to start scan 259 4. After first alarm triggered it starts the second alarm to stop scan 260 5. After second alarm triggered it starts the third alarm to start scan 261 6. Repeat the alarms until 360 scans are done 262 7. Save the power usage data into log file 263 264 Expected Result: 265 power consumption results 266 267 TAGS: LE, Scanning, Power 268 Priority: 3 269 """ 270 self._measure_power_for_scan_n_log_data( 271 ScanSettingsScanMode.SCAN_MODE_LOW_POWER.value, self.SCAN_TIME_5, 272 self.IDLE_TIME_5, self.REPETITIONS_360) 273