1#!/usr/bin/env python3 2# 3# Copyright (C) 2017 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 different Bluetooth 5 specific scan scenarios. 18It is expected that the second AndroidDevice is able to advertise. 19 20This test script was designed with this setup in mind: 21Shield box one: Android Device, Android Device 22""" 23 24from queue import Empty 25 26from acts.asserts import assert_equal 27from acts.asserts import assert_true 28from acts.test_decorators import test_tracker_info 29from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 30from acts.test_utils.bt.bt_constants import ble_scan_settings_modes 31from acts.test_utils.bt.bt_constants import advertising_set_started 32from acts.test_utils.bt.bt_constants import advertising_set_stopped 33from acts.test_utils.bt.bt_constants import advertising_set_enabled 34from acts.test_utils.bt.bt_constants import advertising_set_data_set 35from acts.test_utils.bt.bt_constants import advertising_set_scan_response_set 36from acts.test_utils.bt.bt_constants import advertising_set_parameters_update 37from acts.test_utils.bt.bt_constants import advertising_set_periodic_parameters_updated 38from acts.test_utils.bt.bt_constants import advertising_set_periodic_data_set 39from acts.test_utils.bt.bt_constants import advertising_set_periodic_enable 40from acts.test_utils.bt.bt_test_utils import reset_bluetooth 41from acts import signals 42 43 44class AdvertisingSetTest(BluetoothBaseTest): 45 default_timeout = 10 46 report_delay = 2000 47 scan_callbacks = [] 48 adv_callbacks = [] 49 active_scan_callback_list = [] 50 big_adv_data = { 51 "includeDeviceName": True, 52 "manufacturerData": [0x0123, "00112233445566778899AABBCCDDEE"], 53 "manufacturerData2": 54 [0x2540, [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xFF]], 55 "serviceData": [ 56 "b19d42dc-58ba-4b20-b6c1-6628e7d21de4", 57 "00112233445566778899AABBCCDDEE" 58 ], 59 "serviceData2": [ 60 "000042dc-58ba-4b20-b6c1-6628e7d21de4", 61 [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xFF] 62 ] 63 } 64 65 def setup_class(self): 66 super(AdvertisingSetTest, self).setup_class() 67 self.adv_ad = self.android_devices[0] 68 69 if not self.adv_ad.droid.bluetoothIsLeExtendedAdvertisingSupported(): 70 raise signals.TestAbortClass( 71 "Advertiser does not support LE Extended Advertising") 72 73 def teardown_test(self): 74 self.active_scan_callback_list = [] 75 76 def on_exception(self, test_name, begin_time): 77 reset_bluetooth(self.android_devices) 78 79 @BluetoothBaseTest.bt_test_wrap 80 @test_tracker_info(uuid='58182f7e-443f-47fb-b718-1be3ac850287') 81 def test_reenabling(self): 82 """Test re-enabling an Advertising Set 83 84 Test GATT notify characteristic value. 85 86 Steps: 87 1. Start an advertising set that only lasts for a few seconds 88 2. Restart advertising set 89 3. Repeat steps 1-2 90 91 Expected Result: 92 Verify that advertising set re-enables 93 94 Returns: 95 Pass if True 96 Fail if False 97 98 TAGS: LE, GATT, Characteristic 99 Priority: 1 100 """ 101 adv_callback = self.adv_ad.droid.bleAdvSetGenCallback() 102 duration = 0 103 max_ext_adv_events = 0 104 self.adv_ad.droid.bleAdvSetStartAdvertisingSet({ 105 "connectable": False, 106 "legacyMode": False, 107 "primaryPhy": "PHY_LE_1M", 108 "secondaryPhy": "PHY_LE_1M", 109 "interval": 320 110 }, self.big_adv_data, None, None, None, duration, max_ext_adv_events, 111 adv_callback) 112 113 set_started_evt = self.adv_ad.ed.pop_event( 114 advertising_set_started.format(adv_callback), self.default_timeout) 115 set_id = set_started_evt['data']['setId'] 116 assert_equal(0, set_started_evt['data']['status']) 117 assert_equal(0, set_started_evt['data']['status']) 118 119 self.log.info("Successfully started set " + str(set_id)) 120 121 self.adv_ad.droid.bleAdvSetEnableAdvertising(set_id, False, duration, 122 max_ext_adv_events) 123 enable_evt = self.adv_ad.ed.pop_event( 124 advertising_set_enabled.format(adv_callback), self.default_timeout) 125 assert_equal(set_id, enable_evt['data']['setId']) 126 assert_equal(False, enable_evt['data']['enable']) 127 assert_equal(0, enable_evt['data']['status']) 128 self.log.info("Successfully disabled advertising set " + str(set_id)) 129 130 self.log.info("Enabling advertising for 2 seconds... ") 131 duration_200 = 200 132 self.adv_ad.droid.bleAdvSetEnableAdvertising( 133 set_id, True, duration_200, max_ext_adv_events) 134 enable_evt = self.adv_ad.ed.pop_event( 135 advertising_set_enabled.format(adv_callback), self.default_timeout) 136 assert_equal(set_id, enable_evt['data']['setId']) 137 assert_equal(True, enable_evt['data']['enable']) 138 assert_equal(0, enable_evt['data']['status']) 139 self.log.info("Enabled. Waiting for disable event ~2s ...") 140 141 enable_evt = self.adv_ad.ed.pop_event( 142 advertising_set_enabled.format(adv_callback), self.default_timeout) 143 assert_equal(set_id, enable_evt['data']['setId']) 144 assert_equal(False, enable_evt['data']['enable']) 145 assert_equal(0, enable_evt['data']['status']) 146 self.log.info("Disable event received. Now trying to set data...") 147 148 self.adv_ad.droid.bleAdvSetSetAdvertisingData( 149 set_id, 150 {"manufacturerData": [0x0123, "00112233445566778899AABBCCDDEE"]}) 151 data_set_evt = self.adv_ad.ed.pop_event( 152 advertising_set_data_set.format(adv_callback), 153 self.default_timeout) 154 assert_equal(set_id, data_set_evt['data']['setId']) 155 assert_equal(0, data_set_evt['data']['status']) 156 self.log.info("Data changed successfully.") 157 158 max_len = self.adv_ad.droid.bluetoothGetLeMaximumAdvertisingDataLength( 159 ) 160 161 self.log.info("Will try to set data to maximum possible length") 162 data_len = max_len - 4 163 test_fill = '01' * data_len 164 self.adv_ad.droid.bleAdvSetSetAdvertisingData( 165 set_id, {"manufacturerData": [0x0123, test_fill]}) 166 data_set_evt = self.adv_ad.ed.pop_event( 167 advertising_set_data_set.format(adv_callback), 168 self.default_timeout) 169 assert_equal(set_id, data_set_evt['data']['setId']) 170 assert_equal(0, data_set_evt['data']['status']) 171 self.log.info("Data changed successfully.") 172 173 if max_len < 1650: 174 self.log.info("Will try to set data to more than maximum length") 175 data_len = max_len - 4 + 1 176 test_fill = '01' * data_len 177 self.adv_ad.droid.bleAdvSetSetAdvertisingData( 178 set_id, {"manufacturerData": [0x0123, test_fill]}) 179 data_set_evt = self.adv_ad.ed.pop_event( 180 advertising_set_data_set.format(adv_callback), 181 self.default_timeout) 182 assert_equal(set_id, data_set_evt['data']['setId']) 183 #TODO(jpawlowski): make nicer error fot this case 184 assert_true(data_set_evt['data']['status'] != 0, 185 "Setting data should fail because data too long.") 186 187 self.log.info("Data change failed as expected.") 188 189 self.adv_ad.droid.bleAdvSetStopAdvertisingSet(adv_callback) 190 try: 191 self.adv_ad.ed.pop_event( 192 advertising_set_stopped.format(adv_callback), 193 self.default_timeout) 194 except Empty: 195 self.log.error("Failed to find advertising set stopped event.") 196 return False 197 return True 198