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 different onLost/onFound scenarios. 18 """ 19 20 from queue import Empty 21 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 22 from acts.test_utils.bt.BleEnum import AdvertiseSettingsAdvertiseMode 23 from acts.test_utils.bt.BleEnum import ScanSettingsCallbackType 24 from acts.test_utils.bt.BleEnum import ScanSettingsMatchMode 25 from acts.test_utils.bt.BleEnum import ScanSettingsMatchNum 26 from acts.test_utils.bt.BleEnum import ScanSettingsScanMode 27 from acts.test_utils.bt.bt_test_utils import adv_succ 28 from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers 29 from acts.test_utils.bt.bt_test_utils import log_energy_info 30 from acts.test_utils.bt.bt_test_utils import reset_bluetooth 31 from acts.test_utils.bt.bt_test_utils import scan_result 32 33 34 class BleOnLostOnFoundTest(BluetoothBaseTest): 35 default_timeout = 10 36 max_scan_instances = 28 37 active_scan_callback_list = [] 38 active_adv_callback_list = [] 39 40 def __init__(self, controllers): 41 BluetoothBaseTest.__init__(self, controllers) 42 self.scn_ad = self.android_devices[0] 43 self.adv_ad = self.android_devices[1] 44 45 def teardown_test(self): 46 self.log.info(log_energy_info(self.android_devices, "End")) 47 cleanup_scanners_and_advertisers( 48 self.scn_ad, self.active_adv_callback_list, self.adv_ad, 49 self.active_adv_callback_list) 50 self.active_adv_callback_list = [] 51 self.active_scan_callback_list = [] 52 53 def on_exception(self, test_name, begin_time): 54 reset_bluetooth(self.android_devices) 55 56 def _start_generic_advertisement_include_device_name(self): 57 self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) 58 self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode( 59 AdvertiseSettingsAdvertiseMode.ADVERTISE_MODE_LOW_LATENCY.value) 60 advertise_data = self.adv_ad.droid.bleBuildAdvertiseData() 61 advertise_settings = self.adv_ad.droid.bleBuildAdvertiseSettings() 62 advertise_callback = self.adv_ad.droid.bleGenBleAdvertiseCallback() 63 self.adv_ad.droid.bleStartBleAdvertising( 64 advertise_callback, advertise_data, advertise_settings) 65 self.adv_ad.ed.pop_event( 66 adv_succ.format(advertise_callback), self.default_timeout) 67 self.active_adv_callback_list.append(advertise_callback) 68 return advertise_callback 69 70 def _verify_no_events_found(self, event_name): 71 try: 72 self.scn_ad.ed.pop_event(event_name, self.default_timeout) 73 self.log.error("Found an event when none was expected.") 74 return False 75 except Empty: 76 self.log.info("No scan result found as expected.") 77 return True 78 79 @BluetoothBaseTest.bt_test_wrap 80 def test_onlost_onfound_defaults(self): 81 """Test generic onlost/onfound defaults. 82 83 Tests basic onFound/onLost functionality. 84 85 Steps: 86 1. Setup dut0 scanner and start scan with this setup: 87 Scan Mode: SCAN_MODE_LOW_LATENCY 88 Callback Type: CALLBACK_TYPE_FOUND_AND_LOST 89 Match Mode: AGGRESSIVE 90 Num of Matches: MATCH_NUM_ONE_ADVERTISEMENT 91 Filter: Device name of dut1 92 2. Start an advertisement on dut1, include device name 93 3. Find an onFound event 94 4. Stop the advertisement on dut1 95 5. Find an onLost event 96 97 Expected Result: 98 Find an onLost and an onFound event successfully. 99 100 Returns: 101 Pass if True 102 Fail if False 103 104 TAGS: LE, Advertising, Scanning, onLost, onFound 105 Priority: 0 106 """ 107 filter_list = self.scn_ad.droid.bleGenFilterList() 108 self.scn_ad.droid.bleSetScanFilterDeviceName( 109 self.adv_ad.droid.bluetoothGetLocalName()) 110 self.scn_ad.droid.bleSetScanSettingsScanMode( 111 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value) 112 self.scn_ad.droid.bleSetScanSettingsCallbackType( 113 ScanSettingsCallbackType.CALLBACK_TYPE_FOUND_AND_LOST.value) 114 self.scn_ad.droid.bleSetScanSettingsMatchMode( 115 ScanSettingsMatchMode.AGGRESIVE.value) 116 self.scn_ad.droid.bleSetScanSettingsNumOfMatches( 117 ScanSettingsMatchNum.MATCH_NUM_ONE_ADVERTISEMENT.value) 118 scan_settings = self.scn_ad.droid.bleBuildScanSetting() 119 scan_callback = self.scn_ad.droid.bleGenScanCallback() 120 self.scn_ad.droid.bleBuildScanFilter(filter_list) 121 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, 122 scan_callback) 123 self.active_scan_callback_list.append(scan_callback) 124 adv_callback = self._start_generic_advertisement_include_device_name() 125 event = self.scn_ad.ed.pop_event( 126 scan_result.format(scan_callback), self.default_timeout * 3) 127 found_callback_type = event['data']['CallbackType'] 128 if event['data'][ 129 'CallbackType'] != ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value: 130 self.log.info( 131 "Found Callbacreset_bluetoothkType:{}, Expected CallbackType:{}".format( 132 found_callback_type, 133 ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value)) 134 return False 135 self.adv_ad.droid.bleStopBleAdvertising(adv_callback) 136 event = self.scn_ad.ed.pop_event( 137 scan_result.format(scan_callback), self.default_timeout * 4) 138 found_callback_type = event['data']['CallbackType'] 139 if found_callback_type != ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value: 140 self.log.info( 141 "Found CallbackType:{}, Expected CallbackType:{}".format( 142 found_callback_type, 143 ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value)) 144 return False 145 return True 146 147 @BluetoothBaseTest.bt_test_wrap 148 def test_onlost_onfound_match_mode_sticky(self): 149 """Test generic onlost/onfound in sticky mode. 150 151 Tests basic onFound/onLost functionality. 152 153 Steps: 154 1. Setup dut0 scanner and start scan with this setup: 155 Scan Mode: SCAN_MODE_LOW_LATENCY 156 Callback Type: CALLBACK_TYPE_FOUND_AND_LOST 157 Match Mode: STICKY 158 Num of Matches: MATCH_NUM_ONE_ADVERTISEMENT 159 Filter: Device name of dut1 160 2. Start an advertisement on dut1, include device name 161 3. Find an onFound event 162 4. Stop the advertisement on dut1 163 5. Find an onLost event 164 165 Expected Result: 166 Find an onLost and an onFound event successfully. 167 168 Returns: 169 Pass if True 170 Fail if False 171 172 TAGS: LE, Advertising, Scanning, onLost, onFound 173 Priority: 1 174 """ 175 filter_list = self.scn_ad.droid.bleGenFilterList() 176 self.scn_ad.droid.bleSetScanFilterDeviceName( 177 self.adv_ad.droid.bluetoothGetLocalName()) 178 self.scn_ad.droid.bleSetScanSettingsScanMode( 179 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value) 180 self.scn_ad.droid.bleSetScanSettingsCallbackType( 181 ScanSettingsCallbackType.CALLBACK_TYPE_FOUND_AND_LOST.value) 182 self.scn_ad.droid.bleSetScanSettingsMatchMode( 183 ScanSettingsMatchMode.STICKY.value) 184 self.scn_ad.droid.bleSetScanSettingsNumOfMatches( 185 ScanSettingsMatchNum.MATCH_NUM_ONE_ADVERTISEMENT.value) 186 scan_settings = self.scn_ad.droid.bleBuildScanSetting() 187 scan_callback = self.scn_ad.droid.bleGenScanCallback() 188 self.scn_ad.droid.bleBuildScanFilter(filter_list) 189 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, 190 scan_callback) 191 self.active_scan_callback_list.append(scan_callback) 192 adv_callback = self._start_generic_advertisement_include_device_name() 193 event = self.scn_ad.ed.pop_event( 194 scan_result.format(scan_callback), self.default_timeout * 3) 195 found_callback_type = event['data']['CallbackType'] 196 if event['data'][ 197 'CallbackType'] != ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value: 198 self.log.info( 199 "Found CallbackType:{}, Expected CallbackType:{}".format( 200 found_callback_type, 201 ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value)) 202 return False 203 self.adv_ad.droid.bleStopBleAdvertising(adv_callback) 204 event = self.scn_ad.ed.pop_event( 205 scan_result.format(scan_callback), self.default_timeout * 4) 206 found_callback_type = event['data']['CallbackType'] 207 if found_callback_type != ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value: 208 self.log.info( 209 "Found CallbackType:{}, Expected CallbackType:{}".format( 210 found_callback_type, 211 ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value)) 212 return False 213 return True 214 215 @BluetoothBaseTest.bt_test_wrap 216 def test_onlost_onfound_match_num_few(self): 217 """Test generic onlost/onfound num few. 218 219 Tests basic onFound/onLost functionality. 220 221 Steps: 222 1. Setup dut0 scanner and start scan with this setup: 223 Scan Mode: SCAN_MODE_LOW_LATENCY 224 Callback Type: CALLBACK_TYPE_FOUND_AND_LOST 225 Match Mode: AGGRESSIVE 226 Num of Matches: MATCH_NUM_FEW_ADVERTISEMENT 227 Filter: Device name of dut1 228 2. Start an advertisement on dut1, include device name 229 3. Find an onFound event 230 4. Stop the advertisement on dut1 231 5. Find an onLost event 232 233 Expected Result: 234 Find an onLost and an onFound event successfully. 235 236 Returns: 237 Pass if True 238 Fail if False 239 240 TAGS: LE, Advertising, Scanning, onLost, onFound 241 Priority: 1 242 """ 243 filter_list = self.scn_ad.droid.bleGenFilterList() 244 self.scn_ad.droid.bleSetScanFilterDeviceName( 245 self.adv_ad.droid.bluetoothGetLocalName()) 246 self.scn_ad.droid.bleSetScanSettingsScanMode( 247 ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value) 248 self.scn_ad.droid.bleSetScanSettingsCallbackType( 249 ScanSettingsCallbackType.CALLBACK_TYPE_FOUND_AND_LOST.value) 250 self.scn_ad.droid.bleSetScanSettingsMatchMode( 251 ScanSettingsMatchMode.AGGRESIVE.value) 252 self.scn_ad.droid.bleSetScanSettingsNumOfMatches( 253 ScanSettingsMatchNum.MATCH_NUM_FEW_ADVERTISEMENT.value) 254 scan_settings = self.scn_ad.droid.bleBuildScanSetting() 255 scan_callback = self.scn_ad.droid.bleGenScanCallback() 256 self.scn_ad.droid.bleBuildScanFilter(filter_list) 257 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, 258 scan_callback) 259 self.active_scan_callback_list.append(scan_callback) 260 adv_callback = self._start_generic_advertisement_include_device_name() 261 event = self.scn_ad.ed.pop_event( 262 scan_result.format(scan_callback), self.default_timeout * 3) 263 found_callback_type = event['data']['CallbackType'] 264 if event['data'][ 265 'CallbackType'] != ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value: 266 self.log.info( 267 "Found CallbackType:{}, Expected CallbackType:{}".format( 268 found_callback_type, 269 ScanSettingsCallbackType.CALLBACK_TYPE_FIRST_MATCH.value)) 270 return False 271 self.adv_ad.droid.bleStopBleAdvertising(adv_callback) 272 event = self.scn_ad.ed.pop_event( 273 scan_result.format(scan_callback), self.default_timeout * 4) 274 found_callback_type = event['data']['CallbackType'] 275 if found_callback_type != ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value: 276 self.log.info( 277 "Found CallbackType:{}, Expected CallbackType:{}".format( 278 found_callback_type, 279 ScanSettingsCallbackType.CALLBACK_TYPE_MATCH_LOST.value)) 280 return False 281 return True 282