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 | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 5 | import json |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 6 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 7 | from autotest_lib.client.cros import constants |
| 8 | from autotest_lib.server import autotest |
| 9 | |
| 10 | |
| 11 | class BluetoothClient(object): |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 12 | """BluetoothClient is a thin layer of logic over a remote DUT. |
| 13 | |
| 14 | The Autotest host object representing the remote DUT, passed to this |
| 15 | class on initialization, can be accessed from its host property. |
| 16 | |
| 17 | """ |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 18 | |
| 19 | XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60 |
| 20 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 21 | def __init__(self, client_host): |
| 22 | """Construct a BluetoothClient. |
| 23 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 24 | @param client_host: host object representing a remote host. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 25 | |
| 26 | """ |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 27 | self.host = client_host |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 28 | # Make sure the client library is on the device so that the proxy code |
| 29 | # is there when we try to call it. |
| 30 | client_at = autotest.Autotest(self.host) |
| 31 | client_at.install() |
| 32 | # Start up the XML-RPC proxy on the client. |
| 33 | self._proxy = self.host.xmlrpc_connect( |
| 34 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_COMMAND, |
| 35 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_PORT, |
| 36 | command_name= |
| 37 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_CLEANUP_PATTERN, |
| 38 | ready_test_name= |
| 39 | constants.BLUETOOTH_CLIENT_XMLRPC_SERVER_READY_METHOD, |
| 40 | timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS) |
| 41 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 42 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 43 | def reset_on(self): |
| 44 | """Reset the adapter and settings and power up the adapter. |
| 45 | |
| 46 | @return True on success, False otherwise. |
| 47 | |
| 48 | """ |
| 49 | return self._proxy.reset_on() |
| 50 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 51 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 52 | def reset_off(self): |
| 53 | """Reset the adapter and settings, leave the adapter powered off. |
| 54 | |
| 55 | @return True on success, False otherwise. |
| 56 | |
| 57 | """ |
| 58 | return self._proxy.reset_off() |
| 59 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 60 | |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 61 | def has_adapter(self): |
| 62 | """@return True if an adapter is present, False if not.""" |
| 63 | return self._proxy.has_adapter() |
| 64 | |
| 65 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 66 | def set_powered(self, powered): |
| 67 | """Set the adapter power state. |
| 68 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 69 | @param powered: adapter power state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 70 | |
| 71 | @return True on success, False otherwise. |
| 72 | |
| 73 | """ |
| 74 | return self._proxy.set_powered(powered) |
| 75 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 76 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 77 | def set_discoverable(self, discoverable): |
| 78 | """Set the adapter discoverable state. |
| 79 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 80 | @param discoverable: adapter discoverable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 81 | |
| 82 | @return True on success, False otherwise. |
| 83 | |
| 84 | """ |
| 85 | return self._proxy.set_discoverable(discoverable) |
| 86 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 87 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 88 | def set_pairable(self, pairable): |
| 89 | """Set the adapter pairable state. |
| 90 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 91 | @param pairable: adapter pairable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 92 | |
| 93 | @return True on success, False otherwise. |
| 94 | |
| 95 | """ |
| 96 | return self._proxy.set_pairable(pairable) |
| 97 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 98 | |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 99 | def get_adapter_properties(self): |
| 100 | """Read the adapter properties from the Bluetooth Daemon. |
| 101 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame^] | 102 | @return the properties as a dictionary on success, |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 103 | the value False otherwise. |
| 104 | |
| 105 | """ |
| 106 | return json.loads(self._proxy.get_adapter_properties()) |
| 107 | |
| 108 | |
| 109 | def read_info(self): |
| 110 | """Read the adapter information from the Kernel. |
| 111 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame^] | 112 | @return the information as a tuple of: |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 113 | ( address, bluetooth_version, manufacturer_id, |
| 114 | supported_settings, current_settings, class_of_device, |
| 115 | name, short_name ) |
| 116 | |
| 117 | """ |
| 118 | return json.loads(self._proxy.read_info()) |
| 119 | |
| 120 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame^] | 121 | def get_devices(self): |
| 122 | """Read information about remote devices known to the adapter. |
| 123 | |
| 124 | @return the properties of each device as a JSON-encoded array of |
| 125 | dictionaries on success, the value False otherwise. |
| 126 | |
| 127 | """ |
| 128 | return json.loads(self._proxy.get_devices()) |
| 129 | |
| 130 | |
| 131 | def start_discovery(self): |
| 132 | """Start discovery of remote devices. |
| 133 | |
| 134 | Obtain the discovered device information using get_devices(), called |
| 135 | stop_discovery() when done. |
| 136 | |
| 137 | @return True on success, False otherwise. |
| 138 | |
| 139 | """ |
| 140 | return self._proxy.start_discovery() |
| 141 | |
| 142 | |
| 143 | def stop_discovery(self): |
| 144 | """Stop discovery of remote devices. |
| 145 | |
| 146 | @return True on success, False otherwise. |
| 147 | |
| 148 | """ |
| 149 | return self._proxy.stop_discovery() |
| 150 | |
| 151 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 152 | def close(self): |
| 153 | """Tear down state associated with the client.""" |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 154 | # Turn off the discoverable flag since it may affect future tests. |
| 155 | self._proxy.set_discoverable(False) |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 156 | # Leave the adapter powered off, but don't do a full reset. |
| 157 | self._proxy.set_powered(False) |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 158 | # This kills the RPC server. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 159 | self.host.close() |