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