bluetooth: Check RSSI before running tests

Verify that the RSSI is greater than some minimum RSSI and raise TestNA
if it is not.

BUG=b:158333537
TEST=With MIN_RSSI set to -20, verified RSSI check is done and fails
     test. Restored back to -70 and verified it is passing again.

Change-Id: Ibd0d7abb70c4cf5d29781493cb3284aa570bd81a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2244292
Reviewed-by: Daniel Winkler <danielwinkler@google.com>
Tested-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Commit-Queue: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
diff --git a/server/cros/bluetooth/bluetooth_adapter_quick_tests.py b/server/cros/bluetooth/bluetooth_adapter_quick_tests.py
index 38d61f1..a25d0a7 100644
--- a/server/cros/bluetooth/bluetooth_adapter_quick_tests.py
+++ b/server/cros/bluetooth/bluetooth_adapter_quick_tests.py
@@ -76,6 +76,11 @@
             logging.info('Starting peer devices...')
             self.get_device_rasp(devices)
 
+            # Make sure device RSSI is sufficient
+            for _ , device_list in self.devices.items():
+                for device in device_list:
+                    self.verify_device_rssi(device.address)
+
     def _print_delimiter(self):
         logging.info('=======================================================')
 
diff --git a/server/cros/bluetooth/bluetooth_adapter_tests.py b/server/cros/bluetooth/bluetooth_adapter_tests.py
index dfb4f83..204f9e6 100644
--- a/server/cros/bluetooth/bluetooth_adapter_tests.py
+++ b/server/cros/bluetooth/bluetooth_adapter_tests.py
@@ -575,6 +575,9 @@
     SUSPEND_ENTER_SECS=10
     RESUME_TIME_SECS=30
 
+    # Minimum RSSI required for peer devices during testing
+    MIN_RSSI = -70
+
     # hci0 is the default hci device if there is no external bluetooth dongle.
     EXPECTED_HCI = 'hci0'
 
@@ -3689,6 +3692,32 @@
         if self.host.chameleon is None and self.host.btpeer_list == []:
             raise error.TestError('Have to specify a working Bluetooth peer')
 
+
+    def verify_device_rssi(self, device_address):
+        """ Test device rssi is over required threshold.
+
+        @param device_address: Peer address we're measuring rssi for
+
+        @raises error.TestNA if device isn't found or RSSI is too low
+        """
+        # The RSSI property is only maintained while discovery is enabled.
+        # Stopping discovery removes the property. Thus, look up the RSSI while
+        # still in discovery.
+        found = self.test_discover_device(device_address, stop_discovery=False)
+        rssi = self.bluetooth_facade.get_device_property(device_address, 'RSSI')
+        self.test_stop_discovery()
+
+        if not found:
+            raise error.TestNAError(
+                    'Peer {} not discovered'.format(device_address))
+
+        if not rssi or rssi < self.MIN_RSSI:
+            raise error.TestNAError('Peer {} RSSI is too low: {}'.format(
+                    device_address, rssi))
+
+        logging.info('Peer {} RSSI {}'.format(device_address, rssi))
+
+
     def set_fail_fast(self, args_dict, default=False):
         """Set whether the test should fail fast if running into any problem