blob: b0553650b993ec8634acc05464e761aa20003d34 [file] [log] [blame]
Scott James Remnant4dcd73f2013-07-22 15:00:24 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Scott James Remnantda9f43c2013-08-07 17:53:14 -07005import base64
6import json
7
Scott James Remnant4dcd73f2013-07-22 15:00:24 -07008from autotest_lib.client.cros import constants
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -07009from autotest_lib.server import autotest, hosts
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070010
11
12class BluetoothTester(object):
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070013 """BluetoothTester is a thin layer of logic over a remote tester.
14
15 The Autotest host object representing the remote tester, passed to this
16 class on initialization, can be accessed from its host property.
17
18 """
19
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070020
21 XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60
22
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070023 def __init__(self, tester_host):
24 """Construct a BluetoothTester.
25
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070026 @param tester_host: host object representing a remote host.
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070027
28 """
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070029 self.host = tester_host
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070030 # Make sure the client library is on the device so that the proxy code
31 # is there when we try to call it.
32 client_at = autotest.Autotest(self.host)
33 client_at.install()
34 # Start up the XML-RPC proxy on the tester.
35 self._proxy = self.host.xmlrpc_connect(
36 constants.BLUETOOTH_TESTER_XMLRPC_SERVER_COMMAND,
37 constants.BLUETOOTH_TESTER_XMLRPC_SERVER_PORT,
38 command_name=
39 constants.BLUETOOTH_TESTER_XMLRPC_SERVER_CLEANUP_PATTERN,
40 ready_test_name=
41 constants.BLUETOOTH_TESTER_XMLRPC_SERVER_READY_METHOD,
42 timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS)
43
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070044
Scott James Remnantda9f43c2013-08-07 17:53:14 -070045 def setup(self, profile):
46 """Set up the tester with the given profile.
47
48 @param profile: Profile to use for this test, valid values are:
49 computer - a standard computer profile
50
51 @return True on success, False otherwise.
52
53 """
54 return self._proxy.setup(profile)
55
56
57 def discover_devices(self, br_edr=True, le_public=True, le_random=True):
58 """Discover remote devices.
59
60 Activates device discovery and collects the set of devices found,
61 returning them as a list.
62
63 @param br_edr: Whether to detect BR/EDR devices.
64 @param le_public: Whether to detect LE Public Address devices.
65 @param le_random: Whether to detect LE Random Address devices.
66
67 @return List of devices found as tuples with the format
68 (address, address_type, rssi, flags, base64-encoded eirdata),
69 or False if discovery could not be started.
70
71 """
72 devices = self._proxy.discover_devices(br_edr, le_public, le_random)
73 if devices == False:
74 return False
75
76 return (
77 (address, address_type, rssi, flags,
78 base64.decodestring(eirdata))
79 for address, address_type, rssi, flags, eirdata
80 in json.loads(devices)
81 )
82
83
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070084 def close(self):
85 """Tear down state associated with the client."""
86 # This kills the RPC server.
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070087 self.host.close()
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -070088
89
90def create_host_from(client_host):
91 """Creates a host object for the Tester associated with a DUT.
92
93 Will raise an exception if there isn't a tester for the DUT.
94
95 @param client_host: Autotest host object for the DUT.
96
97 @return Autotest host object for the Tester.
98
99 """
100
101 client_hostname = client_host.hostname
102
103 parts = client_hostname.split('.')
104 parts[0] = parts[0] + '-bluetooth'
105 tester_hostname = '.'.join(parts)
106
107 return hosts.create_host(tester_hostname)