• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17import collections
18import json
19import logging
20import math
21import os
22import random
23import re
24import requests
25import socket
26import time
27
28from acts.controllers.fuchsia_lib.base_lib import BaseLib
29
30
31class FuchsiaBtsLib(BaseLib):
32    # Class representing the Bluetooth Access Library.
33
34    def __init__(self, addr, tc, client_id):
35        self.address = addr
36        self.test_counter = tc
37        self.client_id = client_id
38
39    def setDiscoverable(self, discoverable):
40        """Sets the device to be discoverable over BR/EDR.
41
42        Args:
43            discoverable: A bool object for setting Bluetooth
44              device discoverable or not.
45
46        Returns:
47            Dictionary, None if success, error if error.
48        """
49        test_cmd = "bt_sys_facade.BluetoothSetDiscoverable"
50        test_args = {"discoverable": discoverable}
51        test_id = self.build_id(self.test_counter)
52        self.test_counter += 1
53
54        return self.send_command(test_id, test_cmd, test_args)
55
56    def setName(self, name):
57        """Sets the local Bluetooth name of the device.
58
59        Args:
60            name: A string that represents the name to set.
61
62        Returns:
63            Dictionary, None if success, error if error.
64        """
65        test_cmd = "bt_sys_facade.BluetoothSetName"
66        test_args = {"name": name}
67        test_id = self.build_id(self.test_counter)
68        self.test_counter += 1
69
70        return self.send_command(test_id, test_cmd, test_args)
71
72    def inputPairingPin(self, pin):
73        """Inputs the pairing pin to the Fuchsia devices' pairing delegate.
74
75        Args:
76            pin: A string that represents the pin to input.
77
78        Returns:
79            Dictionary, None if success, error if error.
80        """
81        test_cmd = "bt_sys_facade.BluetoothInputPairingPin"
82        test_args = {"pin": pin}
83        test_id = self.build_id(self.test_counter)
84        self.test_counter += 1
85
86        return self.send_command(test_id, test_cmd, test_args)
87
88    def getPairingPin(self):
89        """Gets the pairing pin from the Fuchsia devices' pairing delegate.
90
91        Returns:
92            Dictionary, None if success, error if error.
93        """
94        test_cmd = "bt_sys_facade.BluetoothGetPairingPin"
95        test_args = {}
96        test_id = self.build_id(self.test_counter)
97        self.test_counter += 1
98
99        return self.send_command(test_id, test_cmd, test_args)
100
101    def initBluetoothSys(self):
102        """Initialises the Bluetooth sys Interface proxy in SL4F.
103
104        Returns:
105            Dictionary, None if success, error if error.
106        """
107        test_cmd = "bt_sys_facade.BluetoothInitSys"
108        test_args = {}
109        test_id = self.build_id(self.test_counter)
110        self.test_counter += 1
111
112        return self.send_command(test_id, test_cmd, test_args)
113
114    def requestDiscovery(self, discovery):
115        """Start or stop Bluetooth Control device discovery.
116
117        Args:
118            discovery: A bool object representing starting or stopping
119              device discovery.
120
121        Returns:
122            Dictionary, None if success, error if error.
123        """
124        test_cmd = "bt_sys_facade.BluetoothRequestDiscovery"
125        test_args = {"discovery": discovery}
126        test_id = self.build_id(self.test_counter)
127        self.test_counter += 1
128
129        return self.send_command(test_id, test_cmd, test_args)
130
131    def getKnownRemoteDevices(self):
132        """Get known remote BR/EDR and LE devices.
133
134        Returns:
135            Dictionary, None if success, error if error.
136        """
137        test_cmd = "bt_sys_facade.BluetoothGetKnownRemoteDevices"
138        test_args = {}
139        test_id = self.build_id(self.test_counter)
140        self.test_counter += 1
141
142        return self.send_command(test_id, test_cmd, test_args)
143
144    def forgetDevice(self, identifier):
145        """Forgets a devices pairing.
146
147        Args:
148            identifier: A string representing the device id.
149
150        Returns:
151            Dictionary, None if success, error if error.
152        """
153        test_cmd = "bt_sys_facade.BluetoothForgetDevice"
154        test_args = {"identifier": identifier}
155        test_id = self.build_id(self.test_counter)
156        self.test_counter += 1
157
158        return self.send_command(test_id, test_cmd, test_args)
159
160    def disconnectDevice(self, identifier):
161        """Disconnects a devices.
162
163        Args:
164            identifier: A string representing the device id.
165
166        Returns:
167            Dictionary, None if success, error if error.
168        """
169        test_cmd = "bt_sys_facade.BluetoothDisconnectDevice"
170        test_args = {"identifier": identifier}
171        test_id = self.build_id(self.test_counter)
172        self.test_counter += 1
173
174        return self.send_command(test_id, test_cmd, test_args)
175
176    def connectDevice(self, identifier):
177        """Connects to a devices.
178
179        Args:
180            identifier: A string representing the device id.
181
182        Returns:
183            Dictionary, None if success, error if error.
184        """
185        test_cmd = "bt_sys_facade.BluetoothConnectDevice"
186        test_args = {"identifier": identifier}
187        test_id = self.build_id(self.test_counter)
188        self.test_counter += 1
189
190        return self.send_command(test_id, test_cmd, test_args)
191
192    def getActiveAdapterAddress(self):
193        """Gets the current Active Adapter's address.
194
195        Returns:
196            Dictionary, String address if success, error if error.
197        """
198        test_cmd = "bt_sys_facade.BluetoothGetActiveAdapterAddress"
199        test_args = {}
200        test_id = self.build_id(self.test_counter)
201        self.test_counter += 1
202
203        return self.send_command(test_id, test_cmd, test_args)
204
205    def pair(self, identifier, pairing_security_level, non_bondable,
206             transport):
207        """Pairs to a device.
208
209        Args:
210            identifier: A string representing the device id.
211            pairing_security_level: The security level required for this pairing request
212                represented as a u64. (Only for LE pairing)
213                Available Values
214                1 - ENCRYPTED: Encrypted without MITM protection (unauthenticated)
215                2 - AUTHENTICATED: Encrypted with MITM protection (authenticated).
216                None: No pairing security level.
217            non_bondable: A bool representing whether the pairing mode is bondable or not. None is
218                also accepted. False if bondable, True if non-bondable.
219            transport: A u64 representing the transport type.
220                Available Values
221                1 - BREDR: Classic BR/EDR transport
222                2 - LE: Bluetooth Low Energy Transport
223
224        Returns:
225            Dictionary, None if success, error if error.
226        """
227        test_cmd = "bt_sys_facade.BluetoothPairDevice"
228        test_args = {
229            "identifier": identifier,
230            "pairing_security_level": pairing_security_level,
231            "non_bondable": non_bondable,
232            "transport": transport,
233        }
234        test_id = self.build_id(self.test_counter)
235        self.test_counter += 1
236        return self.send_command(test_id, test_cmd, test_args)
237
238    def acceptPairing(self,
239                      input_capabilities="NONE",
240                      output_capabilities="NONE"):
241        """Accepts incoming pairing requests.
242
243        Args:
244            input: String - The input I/O capabilities to use
245                Available Values:
246                NONE - Input capability type None
247                CONFIRMATION - Input capability type confirmation
248                KEYBOARD - Input capability type Keyboard
249            output: String - The output I/O Capabilities to use
250                Available Values:
251                NONE - Output capability type None
252                DISPLAY - output capability type Display
253
254        Returns:
255            Dictionary, None if success, error if error.
256        """
257        test_cmd = "bt_sys_facade.BluetoothAcceptPairing"
258        test_args = {
259            "input": input_capabilities,
260            "output": output_capabilities,
261        }
262        test_id = self.build_id(self.test_counter)
263        self.test_counter += 1
264        return self.send_command(test_id, test_cmd, test_args)
265