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