Esempio n. 1
0
def connect_dev_to_headset(pri_droid, dev_to_connect, profiles_set):
    """Connects primary android device to headset.

    Args:
        pri_droid: Android device initiating connection.
        dev_to_connect: Third party headset mac address.
        profiles_set: Profiles to be connected.

    Returns:
        True if Pass
        False if Fail
    """
    supported_profiles = bt_profile_constants.values()
    for profile in profiles_set:
        if profile not in supported_profiles:
            pri_droid.log.info("Profile {} is not supported list {}".format(
                profile, supported_profiles))
            return False

    paired = False
    for paired_device in pri_droid.droid.bluetoothGetBondedDevices():
        if paired_device['address'] == dev_to_connect:
            paired = True
            break

    if not paired:
        pri_droid.log.info("{} not paired to {}".format(pri_droid.serial,
                                                        dev_to_connect))
        return False

    end_time = time.time() + 10
    profile_connected = set()
    sec_addr = dev_to_connect
    pri_droid.log.info("Profiles to be connected {}".format(profiles_set))

    while (time.time() < end_time and
           not profile_connected.issuperset(profiles_set)):
        if (bt_profile_constants['headset_client'] not in profile_connected and
                bt_profile_constants['headset_client'] in profiles_set):
            if is_hfp_client_device_connected(pri_droid, sec_addr):
                profile_connected.add(bt_profile_constants['headset_client'])
        if (bt_profile_constants['headset'] not in profile_connected and
                bt_profile_constants['headset'] in profiles_set):
            profile_connected.add(bt_profile_constants['headset'])
        if (bt_profile_constants['a2dp'] not in profile_connected and
                bt_profile_constants['a2dp'] in profiles_set):
            if is_a2dp_src_device_connected(pri_droid, sec_addr):
                profile_connected.add(bt_profile_constants['a2dp'])
        if (bt_profile_constants['a2dp_sink'] not in profile_connected and
                bt_profile_constants['a2dp_sink'] in profiles_set):
            if is_a2dp_snk_device_connected(pri_droid, sec_addr):
                profile_connected.add(bt_profile_constants['a2dp_sink'])
        if (bt_profile_constants['map_mce'] not in profile_connected and
                bt_profile_constants['map_mce'] in profiles_set):
            if is_map_mce_device_connected(pri_droid, sec_addr):
                profile_connected.add(bt_profile_constants['map_mce'])
        if (bt_profile_constants['map'] not in profile_connected and
                bt_profile_constants['map'] in profiles_set):
            if is_map_mse_device_connected(pri_droid, sec_addr):
                profile_connected.add(bt_profile_constants['map'])
        time.sleep(0.1)

    while not profile_connected.issuperset(profiles_set):
        try:
            time.sleep(10)
            profile_event = pri_droid.ed.pop_event(
                bluetooth_profile_connection_state_changed,
                bt_default_timeout + 10)
            pri_droid.log.info("Got event {}".format(profile_event))
        except Exception:
            pri_droid.log.error("Did not get {} profiles left {}".format(
                bluetooth_profile_connection_state_changed, profile_connected))
            return False
        profile = profile_event['data']['profile']
        state = profile_event['data']['state']
        device_addr = profile_event['data']['addr']
        if state == bt_profile_states['connected'] and (
                device_addr == dev_to_connect):
            profile_connected.add(profile)
        pri_droid.log.info(
            "Profiles connected until now {}".format(profile_connected))
    return True
Esempio n. 2
0
    def test_call_transfer_connect_disconnect_connect(self):
        """
        Test that when we go from connect -> disconnect -> connect on an active
        call then the call is restored on HF.

        Precondition:
        1. AG & HF are paired

        Steps:
        0. Connect AG & HF
        1. Make a call from HF role
        2. Accept from RE role and transition the call to Active
        3. Disconnect AG & HF
        4. Verify that we don't have any calls on HF
        5. Connect AG & HF
        6. Verify that HF gets the call back.

        Returns:
          Pass if True
          Fail if False

        Priority: 1
        """
        # Now connect the devices.
        if not bt_test_utils.connect_pri_to_sec(
                self.hf, self.ag,
                set([BtEnum.BluetoothProfile.HEADSET_CLIENT.value])):
            self.log.error("Could not connect HF and AG {} {}".format(
                self.hf.serial, self.ag.serial))
            return False

        # make a call on HF
        if not car_telecom_utils.dial_number(self.log, self.hf,
                                             self.re_phone_number):
            self.hf.log.error("HF not in dialing state.")
            return False

        # Wait for HF, AG to be dialing and RE to be ringing
        ret = True
        ret &= car_telecom_utils.wait_for_dialing(self.log, self.hf)
        #uncomment once sl4a code has been merged.
        ret &= car_telecom_utils.wait_for_dialing(self.log, self.ag)
        ret &= car_telecom_utils.wait_for_ringing(self.log, self.re)

        if not ret:
            self.log.error("Outgoing call did not get established")
            return False

        # Accept call on RE.
        if not wait_and_answer_call(self.log, self.re):
            self.re.log.error("Failed to accept call on re.")
            return False

        ret &= car_telecom_utils.wait_for_active(self.log, self.hf)
        ret &= car_telecom_utils.wait_for_active(self.log, self.ag)
        ret &= car_telecom_utils.wait_for_active(self.log, self.re)

        if not ret:
            self.log.error("Outgoing call did not transition to active")
            return False

        # Disconnect HF & AG
        self.hf.droid.bluetoothDisconnectConnected(
            self.ag.droid.bluetoothGetLocalAddress())

        # We use the proxy of the Call going away as HF disconnected
        if not car_telecom_utils.wait_for_not_in_call(self.log, self.hf):
            self.hf.log.error("HF still in call after disconnection.")
            return False

        # Now connect the devices.
        if not bt_test_utils.connect_pri_to_sec(
                self.hf, self.ag,
                set([BtEnum.BluetoothProfile.HEADSET_CLIENT.value])):
            self.log.error("Could not connect HF and AG {} {}".format(
                self.hf.serial, self.ag.serial))
            # Additional profile connection check for b/
            if not bt_test_utils.is_hfp_client_device_connected(
                    self.hf, self.ag.droid.bluetoothGetLocalAddress()):
                self.hf.log.info(
                    "HFP Client connected even though connection state changed "
                    + " event not found")
                return False

        # Check that HF is in active state
        if not car_telecom_utils.wait_for_active(self.log, self.hf):
            self.hf.log.error("HF not in Active state.")
            return False

        # Hangup the call and check all devices are clean
        self.hf.droid.telecomEndCall()
        ret &= car_telecom_utils.wait_for_not_in_call(self.log, self.hf)
        ret &= car_telecom_utils.wait_for_not_in_call(self.log, self.ag)
        ret &= car_telecom_utils.wait_for_not_in_call(self.log, self.re)

        return ret
    def test_connect_disconnect_paired(self):
        """
        Tests if we can connect two devices over Headset, A2dp and then disconnect them with success

        Precondition:
        1. Devices are paired.

        Steps:
        1. Initiate connection over A2dp Sink and Headset client profiles.
        2. Check if the connection succeeded.

        Returns:
          Pass if True
          Fail if False

        Priority: 0
        """

        NUM_TEST_RUNS = 2
        failure = 0
        addr = self.ph.droid.bluetoothGetLocalAddress()
        for i in range(NUM_TEST_RUNS):
            self.log.info("Running test [" + str(i) + "/" + str(NUM_TEST_RUNS)
                          + "]")
            success = bt_test_utils.connect_pri_to_sec(self.car, self.ph, set(
                [BtEnum.BluetoothProfile.HEADSET_CLIENT.value,
                 BtEnum.BluetoothProfile.A2DP_SINK.value]))

            # Check if we got connected.
            if not success:
                self.car.log.info("Not all profiles connected.")
                if (bt_test_utils.is_hfp_client_device_connected(self.car,
                                                                 addr) and
                        bt_test_utils.is_a2dp_snk_device_connected(self.car,
                                                                   addr)):
                    self.car.log.info(
                        "HFP Client or A2DP SRC connected successfully.")
                else:
                    failure = failure + 1
                continue

            # Disconnect the devices.
            success = bt_test_utils.disconnect_pri_from_sec(
                self.car, self.ph,
                [BtEnum.BluetoothProfile.HEADSET_CLIENT.value,
                 BtEnum.BluetoothProfile.A2DP_SINK.value])

            if success is False:
                self.car.log.info("Disconnect failed.")
                if (bt_test_utils.is_hfp_client_device_connected(self.car,
                                                                 addr) or
                        bt_test_utils.is_a2dp_snk_device_connected(self.car,
                                                                   addr)):
                    self.car.log.info(
                        "HFP Client or A2DP SRC failed to disconnect.")
                    failure = failure + 1
                continue

        self.log.info("Failure {} total tests {}".format(failure,
                                                         NUM_TEST_RUNS))
        if failure > 0:
            return False
        return True