blob: da3da7d5c86b26e5470f63182c1c48dff9c74869 [file] [log] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
import logging
import re
import time
from autotest_lib.client.common_lib import error
from import xmlrpc_datatypes
# Used to represent stations we parse out of scan results.
Station = collections.namedtuple('Station',
['bssid', 'frequency', 'signal', 'ssid'])
class WpaCliProxy(object):
"""Interacts with a DUT through wpa_cli rather than shill."""
# From wpa_supplicant.c:wpa_supplicant_state_txt()
def __init__(self, host, wifi_if):
self._host = host
self._created_networks = {}
# TODO(wiley) Hardcoding this IFNAME prefix makes some big assumptions.
# we'll need to discover this parameter as it becomes more
# generally useful.
self._wpa_cli_cmd = 'wpa_cli IFNAME=%s' % wifi_if
def _add_network(self, ssid):
Add a wpa_supplicant network for ssid.
@param ssid string: name of network to add.
@return int network id of added network.
add_result = self._run_wpa_cli_cmd('add_network', check_result=False)
network_id = int(add_result.stdout.splitlines()[-1])
self._run_wpa_cli_cmd('set_network %d ssid \'\\"%s\\"\'' %
(network_id, ssid))
self._created_networks[ssid] = network_id
logging.debug('Added network %s=%d', ssid, network_id)
return network_id
def _run_wpa_cli_cmd(self, command, check_result=True):
Run a wpa_cli command and optionally check the result.
@param command string: suffix of a command to be prefixed with
an appropriate wpa_cli for this host.
@param check_result bool: True iff we want to check that the
command comes back with an 'OK' response.
@return result object returned by
result ='%s %s' % (self._wpa_cli_cmd, command))
if check_result and not result.stdout.strip().endswith('OK'):
raise error.TestFail('wpa_cli command failed: %s' % command)
return result
def _get_status_dict(self):
Gets the status output for a WiFi interface.
Get the output of wpa_cli status. This summarizes what wpa_supplicant
is doing with respect to the WiFi interface.
Example output:
Using interface 'wlan0'
@return dict of key/value pairs parsed from output using = as divider.
status_result = self._run_wpa_cli_cmd('status', check_result=False)
return dict([line.strip().split('=', 1)
for line in status_result.stdout.splitlines()
if line.find('=') > 0])
def _is_associating_or_associated(self):
"""@return True if the DUT is assocating or associated with a BSS."""
state = self._get_status_dict().get('wpa_state', None)
def _is_associated(self, ssid):
Check if the DUT is associated to a given SSID.
@param ssid string: SSID of the network we're concerned about.
@return True if we're associated with the specified SSID.
status_dict = self._get_status_dict()
return (status_dict.get('ssid', None) == ssid and
status_dict.get('wpa_state', None) in
def _is_connected(self, ssid):
Check that we're connected to |ssid| and have an IP address.
@param ssid string: SSID of the network we're concerned about.
@return True if we have an IP and we're associated with |ssid|.
status_dict = self._get_status_dict()
return (status_dict.get('ssid', None) == ssid and
status_dict.get('ip_address', None))
def _wait_until(self, value_check, timeout_seconds):
Call a function repeatedly until we time out.
Call value_check() every POLLING_INTERVAL_SECONDS seconds
until |timeout_seconds| have passed. Return whether
value_check() returned a True value and the time we spent in this
@param timeout_seconds numeric: number of seconds to wait.
@return a tuple (success, duration_seconds) where success is a boolean
and duration is a float.
start_time = time.time()
while time.time() - start_time < timeout_seconds:
duration = time.time() - start_time
if value_check():
return (True, duration)
duration = time.time() - start_time
return (False, duration)
def clean_profiles(self):
"""Remove state associated with past networks we've connected to."""
# list_networks output looks like:
# Using interface 'wlan0'^M
# network id / ssid / bssid / flags^M
# 0 SimpleConnect_jstja_ch1 any [DISABLED]^M
# 1 SimpleConnect_gjji2_ch6 any [DISABLED]^M
# 2 SimpleConnect_xe9d1_ch11 any [DISABLED]^M
list_networks_result = self._run_wpa_cli_cmd(
'list_networks', check_result=False)
start_parsing = False
for line in list_networks_result.stdout.splitlines():
if not start_parsing:
if line.startswith('network id'):
start_parsing = True
network_id = int(line.split()[0])
self._run_wpa_cli_cmd('remove_network %d' % network_id)
self._created_networks = {}
def create_profile(self, _):
This is a no op, since we don't have profiles.
@param _ ignored.
"""'Skipping create_profile on %s', self.__class__.__name__)
def pop_profile(self, _):
This is a no op, since we don't have profiles.
@param _ ignored.
"""'Skipping pop_profile on %s', self.__class__.__name__)
def push_profile(self, _):
This is a no op, since we don't have profiles.
@param _ ignored.
"""'Skipping push_profile on %s', self.__class__.__name__)
def remove_profile(self, _):
This is a no op, since we don't have profiles.
@param _ ignored.
"""'Skipping remove_profile on %s', self.__class__.__name__)
def init_test_network_state(self):
"""Create a clean slate for tests with respect to remembered networks.
For wpa_cli hosts, this means removing all remembered networks.
@return True iff operation succeeded, False otherwise.
return True
def connect_wifi(self, assoc_params):
Connect to the WiFi network described by AssociationParameters.
@param assoc_params AssociationParameters object.
@return serialized AssociationResult object.
# Ouptut should look like:
# Using interface 'wlan0'
# 0
assoc_result = xmlrpc_datatypes.AssociationResult()
network_id = self._add_network(assoc_params.ssid)
if assoc_params.is_hidden:
self._run_wpa_cli_cmd('set_network %d %s %s' %
(network_id, 'scan_ssid', '1'))
sec_config = assoc_params.security_config
for field, value in sec_config.get_wpa_cli_properties().iteritems():
self._run_wpa_cli_cmd('set_network %d %s %s' %
(network_id, field, value))
self._run_wpa_cli_cmd('select_network %d' % network_id)
# Wait for an appropriate BSS to appear in scan results.
scan_results_pattern = '\t'.join(['([0-9a-f:]{17})', # BSSID
'([0-9]+)', # Frequency
'(-[0-9]+)', # Signal level
'(.*)', # Encryption types
'(.*)']) # SSID
last_scan_time = -1.0
start_time = time.time()
while time.time() - start_time < assoc_params.discovery_timeout:
assoc_result.discovery_time = time.time() - start_time
if self._is_associating_or_associated():
# Internally, wpa_supplicant writes its scan_results response
# to a 4kb buffer. When there are many BSS's, the buffer fills
# up, and we'll never see the BSS we care about in some cases.
scan_result = self._run_wpa_cli_cmd('scan_results',
found_stations = []
for line in scan_result.stdout.strip().splitlines():
match = re.match(scan_results_pattern, line)
if match is None:
logging.debug('Found stations: %r',
[station.ssid for station in found_stations])
if [station for station in found_stations
if station.ssid == assoc_params.ssid]:
if time.time() - last_scan_time > self.SCANNING_INTERVAL_SECONDS:
# Sometimes this might fail with a FAIL-BUSY if the previous
# scan hasn't finished.
scan_result = self._run_wpa_cli_cmd('scan', check_result=False)
if scan_result.stdout.strip().endswith('OK'):
last_scan_time = time.time()
assoc_result.failure_reason = 'Discovery timed out'
return assoc_result.serialize()
# Wait on association to finish.
success, assoc_result.association_time = self._wait_until(
lambda: self._is_associated(assoc_params.ssid),
if not success:
assoc_result.failure_reason = 'Association timed out'
return assoc_result.serialize()
# Then wait for ip configuration to finish.
success, assoc_result.configuration_time = self._wait_until(
lambda: self._is_connected(assoc_params.ssid),
if not success:
assoc_result.failure_reason = 'DHCP negotiation timed out'
return assoc_result.serialize()
assoc_result.success = True'Connected to %s', assoc_params.ssid)
return assoc_result.serialize()
def disconnect(self, ssid):
Disconnect from a WiFi network named |ssid|.
@param ssid string: name of network to disable in wpa_supplicant.
if ssid not in self._created_networks:
return False
self._run_wpa_cli_cmd('disable_network %d' %
return True
def sync_time_to(self, epoch_seconds):
Sync time on the DUT to |epoch_seconds| from the epoch.
@param epoch_seconds float: number of seconds since the epoch.
# This will claim to fail, but will work anyway.'date -u %f' % epoch_seconds, ignore_status=True)