xref: /aosp_15_r20/external/autotest/server/cros/network/apmanager_service_provider.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2014 The Chromium 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
6from autotest_lib.client.common_lib.cros.network import apmanager_constants
7from autotest_lib.client.cros import constants
8from autotest_lib.server import autotest
9from autotest_lib.server.cros.network import hostap_config
10
11XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60
12
13def get_xmlrpc_proxy(host):
14    """Get an apmanager XMLRPC proxy for |host|.
15
16    @param host: host object representing a remote device.
17    @return proxy object for remote XMLRPC server.
18
19    """
20    # Make sure the client library on the device is up-to-date.
21    client_at = autotest.Autotest(host)
22    client_at.install()
23    # Start up the XMLRPC proxy on the device.
24    proxy = host.rcp_server_tracker.xmlrpc_connect(
25            constants.APMANAGER_XMLRPC_SERVER_COMMAND,
26            constants.APMANAGER_XMLRPC_SERVER_PORT,
27            command_name=constants.APMANAGER_XMLRPC_SERVER_CLEANUP_PATTERN,
28            ready_test_name=constants.APMANAGER_XMLRPC_SERVER_READY_METHOD,
29            timeout_seconds=XMLRPC_BRINGUP_TIMEOUT_SECONDS)
30    return proxy
31
32
33class ApmanagerServiceProvider(object):
34    """Provide AP service using apmanager."""
35
36    XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60
37    APMANAGER_DEFAULT_CHANNEL = 6
38
39    def __init__(self, linux_system, config_params):
40        """
41        @param linux_system SiteLinuxSystem machine to setup AP on.
42        @param config_params dictionary of configuration parameters.
43        """
44        self._linux_system = linux_system
45        self._config_params = config_params
46        self._xmlrpc_server = None
47        self._service = None
48
49
50    def __enter__(self):
51        # Create a managed mode interface to start the AP on. Autotest removes
52        # all wifi interfaces before and after each test in SiteLinuxSystem.
53        channel = apmanager_constants.DEFAULT_CHANNEL_NUMBER
54        if apmanager_constants.CONFIG_CHANNEL in self._config_params:
55            channel = int(
56                    self._config_params[apmanager_constants.CONFIG_CHANNEL])
57        self._linux_system.get_wlanif(
58                hostap_config.HostapConfig.get_frequency_for_channel(
59                        channel),
60                'managed')
61        self._xmlrpc_server = get_xmlrpc_proxy(self._linux_system.host)
62        self._service = self._xmlrpc_server.start_service(self._config_params)
63
64
65    def __exit__(self, exception, value, traceback):
66        if self._service is not None:
67            self._xmlrpc_server.terminate_service(self._service)
68