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"""
17Bluetooth adapter libraries
18"""
19
20from acts.test_utils.bt.bt_constants import bt_scan_mode_types
21from acts.test_utils.bt.bt_test_utils import set_bt_scan_mode
22
23import pprint
24
25
26class BtaLib():
27    def __init__(self, log, dut, target_mac_address=None):
28        self.advertisement_list = []
29        self.dut = dut
30        self.log = log
31        self.target_mac_addr = target_mac_address
32
33    def set_target_mac_addr(self, mac_addr):
34        self.target_mac_addr = mac_addr
35
36    def set_scan_mode(self, scan_mode):
37        """Set the Scan mode of the Bluetooth Adapter"""
38        set_bt_scan_mode(self.dut, bt_scan_mode_types[scan_mode])
39
40    def set_device_name(self, line):
41        """Set Bluetooth Adapter Name"""
42        self.dut.droid.bluetoothSetLocalName(line)
43
44    def enable(self):
45        """Enable Bluetooth Adapter"""
46        self.dut.droid.bluetoothToggleState(True)
47
48    def disable(self):
49        """Disable Bluetooth Adapter"""
50        self.dut.droid.bluetoothToggleState(False)
51
52    def init_bond(self):
53        """Initiate bond to PTS device"""
54        self.dut.droid.bluetoothDiscoverAndBond(self.target_mac_addr)
55
56    def start_discovery(self):
57        """Start BR/EDR Discovery"""
58        self.dut.droid.bluetoothStartDiscovery()
59
60    def stop_discovery(self):
61        """Stop BR/EDR Discovery"""
62        self.dut.droid.bluetoothCancelDiscovery()
63
64    def get_discovered_devices(self):
65        """Get Discovered Br/EDR Devices"""
66        if self.dut.droid.bluetoothIsDiscovering():
67            self.dut.droid.bluetoothCancelDiscovery()
68        self.log.info(
69            pprint.pformat(self.dut.droid.bluetoothGetDiscoveredDevices()))
70
71    def bond(self):
72        """Bond to PTS device"""
73        self.dut.droid.bluetoothBond(self.target_mac_addr)
74
75    def disconnect(self):
76        """BTA disconnect"""
77        self.dut.droid.bluetoothDisconnectConnected(self.target_mac_addr)
78
79    def unbond(self):
80        """Unbond from PTS device"""
81        self.dut.droid.bluetoothUnbond(self.target_mac_addr)
82
83    def start_pairing_helper(self, line):
84        """Start or stop Bluetooth Pairing Helper"""
85        if line:
86            self.dut.droid.bluetoothStartPairingHelper(bool(line))
87        else:
88            self.dut.droid.bluetoothStartPairingHelper()
89
90    def push_pairing_pin(self, line):
91        """Push pairing pin to the Android Device"""
92        self.dut.droid.eventPost("BluetoothActionPairingRequestUserConfirm",
93                                 line)
94
95    def get_pairing_pin(self):
96        """Get pairing PIN"""
97        self.log.info(
98            self.dut.ed.pop_event("BluetoothActionPairingRequest", 1))
99
100    def fetch_uuids_with_sdp(self):
101        """BTA fetch UUIDS with SDP"""
102        self.log.info(
103            self.dut.droid.bluetoothFetchUuidsWithSdp(self.target_mac_addr))
104
105    def connect_profiles(self):
106        """Connect available profiles"""
107        self.dut.droid.bluetoothConnectBonded(self.target_mac_addr)
108
109    def tts_speak(self):
110        """Open audio channel by speaking characters"""
111        self.dut.droid.ttsSpeak(self.target_mac_addr)
112