xref: /aosp_15_r20/external/autotest/server/cros/bluetooth/bluetooth_rvr_tests.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Server side Bluetooth range vs rate tests."""
6
7from __future__ import absolute_import
8from __future__ import division
9from __future__ import print_function
10
11import logging
12
13import common
14from autotest_lib.client.common_lib import error
15from autotest_lib.server.cros.bluetooth.bluetooth_adapter_tests import (
16        BluetoothAdapterTests)
17from six.moves import range
18
19
20class BluetoothAdapterRvRTests(BluetoothAdapterTests):
21    """Server side Bluetooth adapter audio test class."""
22
23    def check_rssi_vs_attenuation(self, device, bt_attenuator):
24        """
25        @param device: Object representing the peer device
26        @param bt_attenuator: Object representing the controllable variable attenuator
27
28        @returns: Dict containing attenuation:rssi values. Empty on failure
29
30        This function keeps measuring the rssi while increasing the attenuation.
31        At some point the device discovery will fail, which is expected. So this
32        failure is ignored and self.fails cleared.
33
34        This should not be run in a batch
35        """
36        try:
37            fixed_attenuation = bt_attenuator.get_minimal_total_attenuation()
38            logging.debug('Fixed attentuation is %s', fixed_attenuation)
39            final_attenuation = 100  # Maximum attenuation
40            freq = 2427  # Frequency used to calculate total attenuation
41            rssi_dict = {}
42            for attn in range(fixed_attenuation, final_attenuation):
43                logging.debug('Setting attenuation to %s', attn)
44                bt_attenuator.set_total_attenuation(attn, freq)
45                device.SetDiscoverable(True)
46                try:
47                    rssi = self.get_device_sample_rssi(device,
48                                                       use_cached_value=False)
49                except error.TestFail as e:
50                    # test_discover_device might fail if RSSI is too low
51                    logging.debug(
52                            'get_device_sample rssi failed with %s.'
53                            'This is expected if RSSI is too low', str(e))
54                    self.fails = []
55                    break
56                logging.info('Total attenuation is %s RSSI is %s', attn, rssi)
57                rssi_dict[attn] = rssi
58            return rssi_dict
59        except Exception as e:
60            logging.error('Exception in check_rssi_vs_attenuation %s', str(e))
61            return {}
62        finally:
63            bt_attenuator.set_variable_attenuation(0)
64