xref: /aosp_15_r20/external/autotest/server/cros/bluetooth/bluetooth_attenuator.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2019 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"""
6This class provides functions to initialize variable attentuator used for
7Bluetooth range vs rate tests
8"""
9
10import logging
11
12from autotest_lib.client.bin import utils
13from autotest_lib.client.common_lib import error
14from autotest_lib.client.common_lib import global_config
15from autotest_lib.client.common_lib.cros.network import ping_runner
16from autotest_lib.server.cros import dnsname_mangler
17from autotest_lib.server.cros.network import attenuator_controller
18
19
20def init_btattenuator(host, args_dict):
21    """
22    Function to initialize bluetooth attenuator and zero the attenuator
23
24    Attenuator address can be passed as argument to test_that or have to
25    be derived from the host name (hostname-btattenuator). For devices in lab,
26    attenuator is assumed to be absent unless added to attenuator_hosts.py file
27    If attenuator is present but not accessible, an exception is raised.
28
29    @param host: cros host object representing the DUT
30           args_dict : arguments passed to test_that
31    @return: AttenuatorController object if attenutator is present else None
32    @raises: TestError if attenautor init fails or if attenutator cannot be
33             accessed
34    """
35    try:
36        if not utils.is_in_container():
37            is_moblab = utils.is_moblab()
38        else:
39            is_moblab = global_config.global_config.get_config_value(
40                    'SSP', 'is_moblab', type=bool, default=False)
41        if is_moblab:
42            # TODO(b:183231262) Implement for moblab
43            logging.debug('bt attenuator not implemented for moblab')
44            return None
45
46        # If attenuator address is provided in args, then it is used
47        # else try to derive attenuator hostname from DUT hostname
48        btattenuator_args = host.get_btattenuator_arguments(
49                args_dict) if args_dict is not None else {}
50        btatten_addr = btattenuator_args.get('btatten_addr')
51        btatten_addr = dnsname_mangler.get_btattenuator_addr(
52                host.hostname, btatten_addr, True)
53        logging.debug('Bluetooth attentuator address is %s', btatten_addr)
54
55        if not btatten_addr:
56            logging.debug('Bluetooth attenuator not present')
57            return None
58        # Attenuator retains previous attenuation set even if it powered down
59        # Do not proceed if attenutator is not accessible
60        if not ping_runner.PingRunner().simple_ping(btatten_addr):
61            logging.debug('Bluetooth attenuator not accessible')
62            return None
63
64        # Init also sets attenutation to zero
65        logging.debug('Initializing bluetooth attenuator')
66        return attenuator_controller.AttenuatorController(btatten_addr)
67    except error.TestError:
68        raise
69    except Exception as e:
70        logging.error('Exception %s while initializing bt attenuator', str(e))
71        return None
72