1#!/usr/bin/env python3
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of 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,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from acts.controllers.fuchsia_lib.base_lib import BaseLib
18
19
20class FuchsiaBtsLib(BaseLib):
21    # Class representing the Bluetooth Access Library.
22
23    def __init__(self, addr: str) -> None:
24        super().__init__(addr, "bt_sys")
25
26    def setDiscoverable(self, discoverable):
27        """Sets the device to be discoverable over BR/EDR.
28
29        Args:
30            discoverable: A bool object for setting Bluetooth
31              device discoverable or not.
32
33        Returns:
34            Dictionary, None if success, error if error.
35        """
36        test_cmd = "bt_sys_facade.BluetoothSetDiscoverable"
37        test_args = {"discoverable": discoverable}
38
39        return self.send_command(test_cmd, test_args)
40
41    def setName(self, name):
42        """Sets the local Bluetooth name of the device.
43
44        Args:
45            name: A string that represents the name to set.
46
47        Returns:
48            Dictionary, None if success, error if error.
49        """
50        test_cmd = "bt_sys_facade.BluetoothSetName"
51        test_args = {"name": name}
52
53        return self.send_command(test_cmd, test_args)
54
55    def inputPairingPin(self, pin):
56        """Inputs the pairing pin to the Fuchsia devices' pairing delegate.
57
58        Args:
59            pin: A string that represents the pin to input.
60
61        Returns:
62            Dictionary, None if success, error if error.
63        """
64        test_cmd = "bt_sys_facade.BluetoothInputPairingPin"
65        test_args = {"pin": pin}
66
67        return self.send_command(test_cmd, test_args)
68
69    def getPairingPin(self):
70        """Gets the pairing pin from the Fuchsia devices' pairing delegate.
71
72        Returns:
73            Dictionary, None if success, error if error.
74        """
75        test_cmd = "bt_sys_facade.BluetoothGetPairingPin"
76        test_args = {}
77
78        return self.send_command(test_cmd, test_args)
79
80    def initBluetoothSys(self):
81        """Initialises the Bluetooth sys Interface proxy in SL4F.
82
83        Returns:
84            Dictionary, None if success, error if error.
85        """
86        test_cmd = "bt_sys_facade.BluetoothInitSys"
87        test_args = {}
88
89        return self.send_command(test_cmd, test_args)
90
91    def requestDiscovery(self, discovery):
92        """Start or stop Bluetooth Control device discovery.
93
94        Args:
95            discovery: A bool object representing starting or stopping
96              device discovery.
97
98        Returns:
99            Dictionary, None if success, error if error.
100        """
101        test_cmd = "bt_sys_facade.BluetoothRequestDiscovery"
102        test_args = {"discovery": discovery}
103
104        return self.send_command(test_cmd, test_args)
105
106    def getKnownRemoteDevices(self):
107        """Get known remote BR/EDR and LE devices.
108
109        Returns:
110            Dictionary, None if success, error if error.
111        """
112        test_cmd = "bt_sys_facade.BluetoothGetKnownRemoteDevices"
113        test_args = {}
114
115        return self.send_command(test_cmd, test_args)
116
117    def forgetDevice(self, identifier):
118        """Forgets a devices pairing.
119
120        Args:
121            identifier: A string representing the device id.
122
123        Returns:
124            Dictionary, None if success, error if error.
125        """
126        test_cmd = "bt_sys_facade.BluetoothForgetDevice"
127        test_args = {"identifier": identifier}
128
129        return self.send_command(test_cmd, test_args)
130
131    def disconnectDevice(self, identifier):
132        """Disconnects a devices.
133
134        Args:
135            identifier: A string representing the device id.
136
137        Returns:
138            Dictionary, None if success, error if error.
139        """
140        test_cmd = "bt_sys_facade.BluetoothDisconnectDevice"
141        test_args = {"identifier": identifier}
142
143        return self.send_command(test_cmd, test_args)
144
145    def connectDevice(self, identifier):
146        """Connects to a devices.
147
148        Args:
149            identifier: A string representing the device id.
150
151        Returns:
152            Dictionary, None if success, error if error.
153        """
154        test_cmd = "bt_sys_facade.BluetoothConnectDevice"
155        test_args = {"identifier": identifier}
156
157        return self.send_command(test_cmd, test_args)
158
159    def getActiveAdapterAddress(self):
160        """Gets the current Active Adapter's address.
161
162        Returns:
163            Dictionary, String address if success, error if error.
164        """
165        test_cmd = "bt_sys_facade.BluetoothGetActiveAdapterAddress"
166        test_args = {}
167
168        return self.send_command(test_cmd, test_args)
169
170    def pair(self, identifier, pairing_security_level, non_bondable,
171             transport):
172        """Pairs to a device.
173
174        Args:
175            identifier: A string representing the device id.
176            pairing_security_level: The security level required for this pairing request
177                represented as a u64. (Only for LE pairing)
178                Available Values
179                1 - ENCRYPTED: Encrypted without MITM protection (unauthenticated)
180                2 - AUTHENTICATED: Encrypted with MITM protection (authenticated).
181                None: No pairing security level.
182            non_bondable: A bool representing whether the pairing mode is bondable or not. None is
183                also accepted. False if bondable, True if non-bondable.
184            transport: A u64 representing the transport type.
185                Available Values
186                1 - BREDR: Classic BR/EDR transport
187                2 - LE: Bluetooth Low Energy Transport
188
189        Returns:
190            Dictionary, None if success, error if error.
191        """
192        test_cmd = "bt_sys_facade.BluetoothPairDevice"
193        test_args = {
194            "identifier": identifier,
195            "pairing_security_level": pairing_security_level,
196            "non_bondable": non_bondable,
197            "transport": transport,
198        }
199
200        return self.send_command(test_cmd, test_args)
201
202    def acceptPairing(self,
203                      input_capabilities="NONE",
204                      output_capabilities="NONE"):
205        """Accepts incoming pairing requests.
206
207        Args:
208            input: String - The input I/O capabilities to use
209                Available Values:
210                NONE - Input capability type None
211                CONFIRMATION - Input capability type confirmation
212                KEYBOARD - Input capability type Keyboard
213            output: String - The output I/O Capabilities to use
214                Available Values:
215                NONE - Output capability type None
216                DISPLAY - output capability type Display
217
218        Returns:
219            Dictionary, None if success, error if error.
220        """
221        test_cmd = "bt_sys_facade.BluetoothAcceptPairing"
222        test_args = {
223            "input": input_capabilities,
224            "output": output_capabilities,
225        }
226
227        return self.send_command(test_cmd, test_args)
228