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