def test_restart_advertise_callback_after_bt_toggle(self): """Test starting an advertisement on a cleared out callback. Test that a single device resets its callbacks when the bluetooth state is reset. Steps: 1. Setup the scanning android device. 2. Setup the advertiser android device. 3. Call start ble advertising. 4. Toggle bluetooth on and off. 5. Call start ble advertising on the same callback. Expected Result: Starting an advertisement on a callback id after toggling bluetooth should fail. Returns: Pass if True Fail if False TAGS: LE, Advertising, Concurrency Priority: 1 """ test_result = True advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.error("Test failed with Empty error: {}".format(error)) test_result = False except concurrent.futures._base.TimeoutError as error: self.log.debug( "Test failed, filtering callback onSuccess never occurred: {}". format(error)) test_result = reset_bluetooth([self.android_devices[1]]) if not test_result: return test_result self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.error("Test failed with Empty error: {}".format(error)) test_result = False except concurrent.futures._base.TimeoutError as error: self.log.debug( "Test failed, filtering callback onSuccess never occurred: {}". format(error)) return test_result
def test_start_two_advertisements_on_same_callback(self): """Test invalid advertisement scenario. Test that a single device cannot have two advertisements start on the same callback. Steps: 1. Setup the scanning android device. 2. Setup the advertiser android device. 3. Call start ble advertising on the same callback. Expected Result: The second call of start advertising on the same callback should fail. Returns: Pass if True Fail if False TAGS: LE, Advertising, Concurrency Priority: 1 """ test_result = True advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.error("Test failed with Empty error: {}".format(error)) return False except concurrent.futures._base.TimeoutError as error: self.log.debug( "Test failed, filtering callback onSuccess never occurred: {}". format(error)) try: self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) test_result = False except Empty as error: self.log.debug("Test passed with Empty error: {}".format(error)) except concurrent.futures._base.TimeoutError as error: self.log.debug( "Test passed, filtering callback onSuccess never occurred: {}". format(error)) return test_result
def _verify_ble_adv_started(self, advertise_callback): """Helper for verifying if an advertisment started or not""" regex = "({}|{})".format(adv_succ.format(advertise_callback), adv_fail.format(advertise_callback)) try: event = self.dut.ed.pop_events(regex, 5, small_timeout) except Empty: self.dut.log.error("Failed to get success or failed event.") return if event[0]["name"] == adv_succ.format(advertise_callback): self.dut.log.info("Advertisement started successfully.") return True else: self.dut.log.info("Advertisement failed to start.") return False
def test_start_ble_advertise(self): """Test to demonstrate how to start an LE advertisement Test that shows the steps to start a new ble scan. Steps: 1. Create a advertise data object 2. Create a advertise settings object. 3. Create a advertise callback object. 4. Start an LE advertising using the objects created in steps 1-3. 5. Find the onSuccess advertisement event. Expected Result: Advertisement is successfully advertising. Returns: Pass if True Fail if False TAGS: LE, Advertising Priority: 4 """ advertise_data = self.adv_droid.bleBuildAdvertiseData() advertise_settings = self.adv_droid.bleBuildAdvertiseSettings() advertise_callback = self.adv_droid.bleGenBleAdvertiseCallback() self.adv_droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) self.adv_ed.pop_event(adv_succ.format(advertise_callback)) return True
def test_timeout(self): """Test starting advertiser with timeout. Test that when a timeout is used, the advertiser is cleaned properly, and next one can be started. Steps: 1. Setup the advertiser android device with 4 second timeout. 2. Call start ble advertising. 3. Wait 5 seconds, to make sure advertiser times out. 4. Repeat steps 1-4 four times. Expected Result: Starting the advertising should succeed each time. Returns: Pass if True Fail if False TAGS: LE, Advertising, Concurrency Priority: 1 """ advertise_timeout_s = 4 num_iterations = 4 test_result = True for i in range(0, num_iterations): advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleSetAdvertiseSettingsTimeout( advertise_timeout_s * 1000) self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.error( "Test failed with Empty error: {}".format(error)) test_result = False except concurrent.futures._base.TimeoutError as error: self.log.debug( "Test failed, filtering callback onSuccess never occurred: {}" .format(error)) if not test_result: return test_result time.sleep(advertise_timeout_s + 1) return test_result
def test_toggle_advertiser_bt_state(self): """Test forcing stopping advertisements. Test that a single device resets its callbacks when the bluetooth state is reset. There should be no advertisements. Steps: 1. Setup the scanning android device. 2. Setup the advertiser android device. 3. Call start ble advertising. 4. Toggle bluetooth on and off. 5. Scan for any advertisements. Expected Result: No advertisements should be found after toggling Bluetooth on the advertising device. Returns: Pass if True Fail if False TAGS: LE, Advertising, Concurrency Priority: 2 """ test_result = True self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleStartBleAdvertising(advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event(adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.error("Test failed with Empty error: {}".format(error)) return False except concurrent.futures._base.TimeoutError as error: self.log.error( "Test failed, filtering callback onSuccess never occurred: {}". format(error)) self.scn_ad.droid.bleSetScanSettingsScanMode( ble_scan_settings_modes['low_latency']) self.scn_ad.droid.bleSetScanFilterDeviceName( self.adv_ad.droid.bluetoothGetLocalName()) filter_list, scan_settings, scan_callback = generate_ble_scan_objects( self.scn_ad.droid) self.scn_ad.droid.bleBuildScanFilter(filter_list) self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, scan_callback) try: self.scn_ad.ed.pop_event(scan_result.format(scan_callback), self.default_timeout) except Empty as error: self.log.error("Test failed with: {}".format(error)) return False self.scn_ad.droid.bleStopBleScan(scan_callback) test_result = reset_bluetooth([self.android_devices[1]]) self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, scan_callback) if not test_result: return False try: expected_event = scan_result.format(scan_callback) event = self.scn_ad.ed.pop_event(expected_event, self.default_timeout) self.log.error("Event {} not expected. Found: {}".format( expected_event, event)) return False except Empty as error: self.log.debug("Test passed with: {}".format(error)) self.scn_ad.droid.bleStopBleScan(scan_callback) self.adv_ad.droid.bleStopBleAdvertising(advertise_callback) return True
def test_max_concurrent_ble_scans(self): """Test max LE scans. Test that a single device can have max scans concurrently scanning. Steps: 1. Initialize scanner 2. Initialize advertiser 3. Start advertising on the device from step 2 4. Create max ble scan callbacks 5. Start ble scan on each callback 6. Verify that each callback triggers 7. Stop all scans and advertisements Expected Result: All scanning instances should start without errors and the advertisement should be found on each scan instance. Returns: Pass if True Fail if False TAGS: LE, Scanning, Concurrency Priority: 0 """ test_result = True self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) self.scn_ad.droid.bleSetScanSettingsCallbackType( ble_scan_settings_callback_types['all_matches']) self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[ 'low_latency']) self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode( ble_advertise_settings_modes['low_latency']) advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleSetAdvertiseSettingsIsConnectable(False) self.adv_ad.droid.bleStartBleAdvertising( advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event( adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.exception("Test failed with Empty error: {}".format( error)) test_result = False except concurrent.futures._base.TimeoutError as error: self.log.exception( "Test failed callback onSuccess never occurred: " "{}".format(error)) test_result = False if not test_result: return test_result filter_list = self.scn_ad.droid.bleGenFilterList() self.scn_ad.droid.bleSetScanFilterDeviceName( self.adv_ad.droid.bluetoothGetLocalName()) self.scn_ad.droid.bleBuildScanFilter(filter_list) scan_settings = self.scn_ad.droid.bleBuildScanSetting() scan_callback_list = [] for i in range(self.max_concurrent_scans): self.log.debug("Concurrent Ble Scan iteration {}".format(i + 1)) scan_callback = self.scn_ad.droid.bleGenScanCallback() scan_callback_list.append(scan_callback) self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, scan_callback) try: self.scn_ad.ed.pop_event( scan_result.format(scan_callback), self.default_timeout) self.log.info("Found scan event successfully. Iteration {} " "successful.".format(i)) except Exception: self.log.info("Failed to find a scan result for callback {}" .format(scan_callback)) test_result = False break for callback in scan_callback_list: self.scn_ad.droid.bleStopBleScan(callback) self.adv_ad.droid.bleStopBleAdvertising(advertise_callback) if not test_result: return test_result self.log.info("Waiting for scan callbacks to stop completely.") # Wait for all scan callbacks to stop. There is no confirmation # otherwise. time.sleep(10) return test_result
def test_max_concurrent_ble_scans_verify_scans_stop_independently(self): """Test max LE scans variant. Test that a single device can have max scans concurrently scanning. Steps: 1. Initialize scanner 2. Initialize advertiser 3. Create max ble scan callbacks 4. Start ble scan on each callback 5. Start advertising on the device from step 2 6. Verify that the first callback triggers 7. Stop the scan and repeat steps 6 and 7 until all scans stopped Expected Result: All scanning instances should start without errors and the advertisement should be found on each scan instance. All scanning instances should stop successfully. Returns: Pass if True Fail if False TAGS: LE, Scanning, Concurrency Priority: 1 """ self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) self.scn_ad.droid.bleSetScanSettingsCallbackType( ble_scan_settings_callback_types['all_matches']) self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[ 'low_latency']) self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode( ble_advertise_settings_modes['low_latency']) advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) filter_list = self.scn_ad.droid.bleGenFilterList() self.scn_ad.droid.bleSetScanFilterDeviceName( self.adv_ad.droid.bluetoothGetLocalName()) self.scn_ad.droid.bleBuildScanFilter(filter_list) scan_settings = self.scn_ad.droid.bleBuildScanSetting() scan_callback_list = [] for i in range(self.max_concurrent_scans): self.log.debug("Concurrent Ble Scan iteration {}".format(i + 1)) scan_callback = self.scn_ad.droid.bleGenScanCallback() scan_callback_list.append(scan_callback) self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, scan_callback) self.adv_ad.droid.bleStartBleAdvertising( advertise_callback, advertise_data, advertise_settings) try: self.adv_ad.ed.pop_event( adv_succ.format(advertise_callback), self.default_timeout) except Empty as error: self.log.exception("Test failed with Empty error: {}".format( error)) return False except concurrent.futures._base.TimeoutError as error: self.log.exception( "Test failed, filtering callback onSuccess never" " occurred: {}".format(error)) return False i = 0 for callback in scan_callback_list: expected_scan_event_name = scan_result.format(scan_callback) try: self.scn_ad.ed.pop_event(expected_scan_event_name, self.default_timeout) self.log.info( "Found scan event successfully. Iteration {} successful.". format(i)) i += 1 except Exception: self.log.info("Failed to find a scan result for callback {}". format(scan_callback)) return False self.scn_ad.droid.bleStopBleScan(callback) self.adv_ad.droid.bleStopBleAdvertising(advertise_callback) return True
def test_filter_manufacturer_id_bounds(self): """Test scan filtering of lower and upper bounds of allowed manu data This test will setup one Android device as an advertiser and the second as the scanner and test the upper and lower bounds of manufacturer data filtering Steps: 1. Start an advertisement with manufacturer id set to 0x004c 2. Start a generic scanner. 3. Find the advertisement and extract the mac address. 4. Stop the first scanner. 5. Create a new scanner with scan filter with a mac address filter of what was found in step 3. 6. Start the scanner. Expected Result: Verify that the advertisement was found in the second scan instance. Returns: Pass if True Fail if False TAGS: LE, Advertising, Filtering, Scanning Priority: 1 """ manufacturer_id_list = [0, 1, 65534, 65535] for manufacturer_id in manufacturer_id_list: self.adv_ad.droid.bleAddAdvertiseDataManufacturerId( manufacturer_id, [0x01]) self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode( ble_advertise_settings_modes['low_latency']) advertise_callback, advertise_data, advertise_settings = ( generate_ble_advertise_objects(self.adv_ad.droid)) self.adv_ad.droid.bleStartBleAdvertising( advertise_callback, advertise_data, advertise_settings) expected_event = adv_succ.format(advertise_callback) try: self.adv_ad.ed.pop_event(expected_event) except Empty: self.log.info("Failed to start advertisement.") return False self.scn_ad.droid.bleSetScanSettingsScanMode( ble_scan_settings_modes['low_latency']) self.scn_ad.droid.bleSetScanFilterManufacturerData( manufacturer_id, [0x01]) filter_list = self.scn_ad.droid.bleGenFilterList() scan_settings = self.scn_ad.droid.bleBuildScanSetting() scan_filter = self.scn_ad.droid.bleBuildScanFilter(filter_list) scan_callback = self.scn_ad.droid.bleGenScanCallback() self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, scan_callback) expected_event_name = scan_result.format(scan_callback) event = None try: event = self.scn_ad.ed.pop_event(expected_event_name, self.default_timeout) except Empty: self.log.error("Unable to find beacon advertisement.") return False found_manufacturer_id = json.loads( event['data']['Result']['manufacturerIdList']) if found_manufacturer_id[0] != manufacturer_id: self.log.error( "Manufacturer id mismatch. Found {}, Expected {}".format( found_manufacturer_id, manufacturer_id)) return False self.scn_ad.droid.bleStopBleScan(scan_callback) self.adv_ad.droid.bleStopBleAdvertising(advertise_callback) return True