Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 1 | # 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 Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 5 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 6 | from autotest_lib.client.cros import constants |
| 7 | from autotest_lib.server import autotest |
| 8 | |
| 9 | |
| 10 | class BluetoothClient(object): |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 11 | """BluetoothClient is a thin layer of logic over a remote DUT. |
| 12 | |
| 13 | The Autotest host object representing the remote DUT, passed to this |
| 14 | class on initialization, can be accessed from its host property. |
| 15 | |
| 16 | """ |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 17 | |
| 18 | XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60 |
| 19 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 20 | def __init__(self, client_host): |
| 21 | """Construct a BluetoothClient. |
| 22 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 23 | @param client_host: host object representing a remote host. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 24 | |
| 25 | """ |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 26 | self.host = client_host |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 27 | # Make sure the client library is on the device so that the proxy code |
| 28 | # is there when we try to call it. |
| 29 | client_at = autotest.Autotest(self.host) |
| 30 | client_at.install() |
| 31 | # Start up the XML-RPC proxy on the client. |
| 32 | self._proxy = self.host.xmlrpc_connect( |
| 33 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_COMMAND, |
| 34 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_PORT, |
| 35 | command_name= |
| 36 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_CLEANUP_PATTERN, |
| 37 | ready_test_name= |
| 38 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_READY_METHOD, |
| 39 | timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS) |
| 40 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 41 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 42 | def reset_on(self): |
| 43 | """Reset the adapter and settings and power up the adapter. |
| 44 | |
| 45 | @return True on success, False otherwise. |
| 46 | |
| 47 | """ |
| 48 | return self._proxy.reset_on() |
| 49 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 50 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 51 | def reset_off(self): |
| 52 | """Reset the adapter and settings, leave the adapter powered off. |
| 53 | |
| 54 | @return True on success, False otherwise. |
| 55 | |
| 56 | """ |
| 57 | return self._proxy.reset_off() |
| 58 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 59 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 60 | def set_powered(self, powered): |
| 61 | """Set the adapter power state. |
| 62 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 63 | @param powered: adapter power state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 64 | |
| 65 | @return True on success, False otherwise. |
| 66 | |
| 67 | """ |
| 68 | return self._proxy.set_powered(powered) |
| 69 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 70 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 71 | def set_discoverable(self, discoverable): |
| 72 | """Set the adapter discoverable state. |
| 73 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 74 | @param discoverable: adapter discoverable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 75 | |
| 76 | @return True on success, False otherwise. |
| 77 | |
| 78 | """ |
| 79 | return self._proxy.set_discoverable(discoverable) |
| 80 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 81 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 82 | def set_pairable(self, pairable): |
| 83 | """Set the adapter pairable state. |
| 84 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 85 | @param pairable: adapter pairable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 86 | |
| 87 | @return True on success, False otherwise. |
| 88 | |
| 89 | """ |
| 90 | return self._proxy.set_pairable(pairable) |
| 91 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 92 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 93 | def close(self): |
| 94 | """Tear down state associated with the client.""" |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 95 | # Leave the adapter powered off, but don't do a full reset. |
| 96 | self._proxy.set_powered(False) |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 97 | # This kills the RPC server. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 98 | self.host.close() |