1# Copyright (c) 2010 The Chromium OS 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.bin import test, utils 6from autotest_lib.client.common_lib import error 7 8import logging, os, re 9 10class network_WiFiTxRx(test.test): 11 version = 1 12 13 def run_once(self, interface=None, 14 min_tx=-90, min_rx=-90, 15 min_qual=0.14, 16 max_tx=20, max_rx=20 ): 17 '''Check 802.11 WiFi signal strength 18 19 @param interface: Network interface to test (None to pick the default) 20 @param min_tx: Minimum acceptable WiFi Tx value in dBm (integer) 21 @param min_rx: Minimum acceptable WiFi Rx value in dBm (integer) 22 @param min_qual: Minimum signal quality (float, range 0 - 1) 23 @param max_tx: Maximum allowable WiFi Tx value in dBm (integer) 24 @param max_rx: Maximum allowable WiFi Rx value in dBm (integer) 25 26 |min_tx|, |min_rx| and |min_qual| are the only values you are likely to 27 want to change, and they should be set at sensible defaults anyway. 28 ''' 29 flipflop = os.path.dirname(__file__) 30 flipflop = os.path.join(flipflop, 'network-flipflop.sh') 31 32 if interface: 33 flipflop = '%s %s' % (flipflop, interface) 34 35 data = utils.system_output(flipflop, retain_output=True) 36 results = data.splitlines() 37 38 pattern = (r'802\.11([a-z]+) ' 39 r'freq [0-9]+(?:\.[0-9]+)? [A-Z]Hz ' 40 r'quality (\d+)/(\d+) ' 41 r'rx (-?\d+) dBm ' 42 r'tx (-?\d+) dBm') 43 regex = re.compile(pattern) 44 45 success = 0 46 47 range_fail = '802.11%s %s outside range (%d dBm: %d to %d)' 48 49 for association in results: 50 readings = regex.match(association) 51 if readings: 52 modulation = readings.group(1) 53 quality = int(readings.group(2)) / int(readings.group(3)) 54 rx = int(readings.group(4)) 55 tx = int(readings.group(5)) 56 57 if min_qual > quality: 58 raise error.TestFail('802.11%s quality too low (%f < %f)' 59 % (modulation, quality, min_qual)) 60 61 if tx < min_tx or tx > max_tx: 62 raise error.TestFail(range_fail % (modulation, 'Tx', 63 tx, min_tx, max_tx)) 64 65 if rx < min_rx or rx > max_rx: 66 raise error.TestFail(range_fail % (modulation, 'Rx', 67 rx, min_rx, max_rx)) 68 69 success += 1 70 logging.info('SUCCESS: 802.11%s signal is acceptable ' 71 '(Rx:%d dBm, Tx:%d dBm)', modulation, rx, tx) 72 73 if not success: 74 raise error.TestFail('No AP associations established') 75 76 return 0 77