1#!/usr/bin/env python3 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""" 17Test script to test the pairing scenarios and setting priorities. 18""" 19 20import time 21 22from acts.test_decorators import test_tracker_info 23from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 24from acts.base_test import BaseTestClass 25from acts_contrib.test_utils.bt import bt_test_utils 26from acts_contrib.test_utils.car import car_bt_utils 27from acts_contrib.test_utils.bt import BtEnum 28 29# Timed wait between Bonding happens and Android actually gets the list of 30# supported services (and subsequently updates the priorities) 31BOND_TO_SDP_WAIT = 3 32UNBOND_TIMEOUT = 3 33 34 35class BtCarPairingTest(BluetoothBaseTest): 36 def setup_class(self): 37 super().setup_class() 38 self.car = self.android_devices[0] 39 self.ph = self.android_devices[1] 40 41 def teardown_test(self): 42 for ad in self.android_devices: 43 bt_test_utils.clear_bonded_devices(ad) 44 # Give the stack time to unbond. 45 time.sleep(UNBOND_TIMEOUT) 46 47 @test_tracker_info(uuid='f56e915-eef7-45cd-b5a6-771f6ef72602') 48 @BluetoothBaseTest.bt_test_wrap 49 def test_simple_pairing(self): 50 """ 51 Tests if after first pairing the remote device has the default 52 priorities for A2DP and HFP. 53 54 Steps: 55 1. Pair the devices (do not connect) 56 2. Check the priorities. 57 58 Returns: 59 Pass if True 60 Fail if False 61 62 Priority: 0 63 """ 64 # Pair the devices. 65 if not bt_test_utils.pair_pri_to_sec( 66 self.car, self.ph, attempts=1, auto_confirm=False): 67 self.log.error("cannot pair") 68 return False 69 70 # Sleep because priorities are not event driven. 71 time.sleep(BOND_TO_SDP_WAIT) 72 73 # Check that the default priority for HFP and A2DP is ON. 74 ph_hfp_p = self.car.droid.bluetoothHfpClientGetPriority( 75 self.ph.droid.bluetoothGetLocalAddress()) 76 if ph_hfp_p != BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value: 77 self.log.error("hfp {} priority {} expected {}".format( 78 self.ph.serial, ph_hfp_p, 79 BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value)) 80 return False 81 82 ph_a2dp_p = self.car.droid.bluetoothA2dpSinkGetPriority( 83 self.ph.droid.bluetoothGetLocalAddress()) 84 if ph_a2dp_p != BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value: 85 self.log.error("a2dp {} priority {} expected {}".format( 86 self.ph.serial, ph_a2dp_p, 87 BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value)) 88 return False 89 90 ph_pbap_p = self.car.droid.bluetoothPbapClientGetPriority( 91 self.ph.droid.bluetoothGetLocalAddress()) 92 if ph_pbap_p != BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value: 93 self.log.error("pbap {} priority {} expected {}".format( 94 self.ph.serial, ph_pbap_p, 95 BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value)) 96 return False 97 return True 98 99 @test_tracker_info(uuid='be4db211-10a0-479a-8958-dff0ccadca1a') 100 @BluetoothBaseTest.bt_test_wrap 101 def test_repairing(self): 102 """ 103 Tests that even if we modify the priorities, on unpair and pair 104 we will reset the priorities. 105 106 Steps: 107 1. Pair the devices (do not connect) 108 2. Unset the priorities for HFP and A2DP 109 3. Pair again 110 4. Check the priorities, they should be set to default. 111 112 Returns: 113 Pass if True 114 Fail if False 115 116 Priority: 0 117 """ 118 # Pair the devices. 119 self.log.info("Pairing the devices ...") 120 if not bt_test_utils.pair_pri_to_sec( 121 self.car, self.ph, attempts=1, auto_confirm=False): 122 self.log.error("Failed to pair devices.") 123 return False 124 125 # Timed wait for the profile priorities to propagate. 126 time.sleep(BOND_TO_SDP_WAIT) 127 128 # Set the priority to OFF for ALL car profiles. 129 self.car.log.info("Set priorities off ...") 130 car_bt_utils.set_car_profile_priorities_off(self.car, self.ph) 131 132 # Now unpair the devices. 133 self.log.info("Resetting the devices ...") 134 for ad in self.android_devices: 135 bt_test_utils.clear_bonded_devices(ad) 136 # Give the stack time to unbond. 137 time.sleep(UNBOND_TIMEOUT) 138 139 # Pair them again! 140 self.log.info("Pairing them again ...") 141 if not bt_test_utils.pair_pri_to_sec( 142 self.car, self.ph, attempts=1, auto_confirm=False): 143 self.log.error("Faild to pair devices.") 144 return False 145 146 # Timed wait for the profile priorities to propagate. 147 time.sleep(BOND_TO_SDP_WAIT) 148 149 # Check the default priorities. 150 ph_hfp_p = self.car.droid.bluetoothHfpClientGetPriority( 151 self.ph.droid.bluetoothGetLocalAddress()) 152 if ph_hfp_p != BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value: 153 self.hf.log.error("HFP priority found: {}, expected: {}.".format( 154 ph_hfp_p, BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value)) 155 return False 156 157 ph_a2dp_p = self.car.droid.bluetoothA2dpSinkGetPriority( 158 self.ph.droid.bluetoothGetLocalAddress()) 159 if ph_a2dp_p != BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value: 160 self.ph.log.error("A2DP priority found: {}, expected {}.".format( 161 ph_a2dp_p, BtEnum.BluetoothPriorityLevel.PRIORITY_ON.value)) 162 return False 163 164 return True 165