1# Copyright (c) 2011 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
5
6# DESCRIPTION :
7#
8# This is a hardware test for system fan.  The test first sets the fan to max
9# speed, turns off the fan, and then sets to 50% speed.  The test sleeps for a
10# few seconds after setting the fan speed, which allows the fan some time to
11# spin up/down and ensures the correctness of the test.  The test restores fan
12# setting when finished.  This test uses mosys to read and control fan settings.
13
14
15import re
16import time
17
18from autotest_lib.client.bin import test, utils
19from autotest_lib.client.common_lib import error
20
21
22class FanControl(object):
23    MOSYS_OUTPUT_RE = re.compile('(\w+)="(.*?)"')
24
25    def __init__(self, fan_name='system'):
26        self._fan_name = fan_name
27
28    def get_values(self):
29        values = {}
30        cmd = 'mosys -k sensor print fantach %s' % self._fan_name
31        for kv in self.MOSYS_OUTPUT_RE.finditer(utils.system_output(cmd)):
32            key, value = kv.groups()
33            if key == 'reading':
34                value = int(value)
35            values[key] = value
36        return values
37
38    def get_mode(self):
39        return self.get_values()['mode']
40
41    def get_reading(self):
42        return self.get_values()['reading']
43
44    def set_percent(self, percent):
45        cmd = 'mosys sensor set fantach %s %s' % (self._fan_name, percent)
46        utils.system_output(cmd)
47
48
49class hardware_Fan(test.test):
50    version = 1
51    DELAY = 3
52
53    def run_once(self):
54        fan = FanControl()
55        original_values = fan.get_values()
56        max_reading = 0
57
58        try:
59            fan.set_percent('100')
60            time.sleep(self.DELAY)
61            if fan.get_mode() != 'manual':
62                raise error.TestError('Unable to manually set fan speed')
63            if fan.get_reading() == 0:
64                raise error.TestError('Fan cannot be turned on')
65            max_reading = fan.get_reading()
66
67            fan.set_percent('off')
68            time.sleep(self.DELAY)
69            if fan.get_reading() != 0:
70                raise error.TestError('Unable to turn off fan')
71
72            fan.set_percent('50')
73            time.sleep(self.DELAY)
74            if not 0 < fan.get_reading() < max_reading:
75                raise error.TestError('Fan speed not in reasonable range')
76        finally:
77            if original_values['mode'] == 'manual' and max_reading > 0:
78                fan.set_percent(100 * original_values['reading'] / max_reading)
79            else:
80                fan.set_percent('auto')
81