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 EC. The test uses ectool to check if the EC can
9# receive message from host and send expected reponse back to host. It also
10# checks basic EC functionality, such as FAN and temperature sensor.
11
12
13import logging
14import time
15
16from autotest_lib.client.bin import test
17from autotest_lib.client.common_lib import error
18from autotest_lib.client.cros import ec as cros_ec
19from autotest_lib.client.cros.power import power_utils
20
21
22class hardware_EC(test.test):
23    """Class for hardware_EC test."""
24    version = 1
25
26    def run_once(self,
27                 num_temp_sensor=0,
28                 temp_sensor_to_test=None,
29                 test_fan=False,
30                 fan_rpm_error_margin=200,
31                 test_battery=None,
32                 test_lightbar=False,
33                 fan_delay_secs=3):
34
35        ec = cros_ec.EC()
36
37        if not cros_ec.has_ectool() or not ec.hello(ignore_status=True):
38            raise error.TestNAError('No support for Google EC')
39
40        if test_battery is None:
41            test_battery = power_utils.has_battery()
42
43        if test_fan:
44            try:
45                ec.set_fanspeed(10000)
46                time.sleep(fan_delay_secs)
47                max_reading = ec.get_fanspeed()
48                if max_reading == 0:
49                    raise error.TestError('Unable to start fan')
50
51                target_fanspeed = max_reading / 2
52                ec.set_fanspeed(target_fanspeed)
53                time.sleep(fan_delay_secs)
54                current_reading = ec.get_fanspeed()
55
56                # Sometimes the actual fan speed is close but not equal to
57                # the target speed, so we add some error margin here.
58                lower_bound = target_fanspeed - fan_rpm_error_margin
59                upper_bound = target_fanspeed + fan_rpm_error_margin
60                if not (lower_bound <= current_reading <= upper_bound):
61                    raise error.TestError('Unable to set fan speed')
62            finally:
63                ec.auto_fan_ctrl()
64
65        if temp_sensor_to_test is None:
66            temp_sensor_to_test = list(range(num_temp_sensor))
67
68        for idx in temp_sensor_to_test:
69            temperature = ec.get_temperature(idx) - 273
70            if temperature < 0 or temperature > 100:
71                raise error.TestError(
72                        'Abnormal temperature reading on sensor %d' % idx)
73
74        if test_battery:
75            try:
76                logging.info('Battery temperature %d K',
77                             ec.get_temperature(name='Battery'))
78            except cros_ec.ECError as e:
79                logging.debug('ECError: %s', e)
80                logging.warning('No battery temperature via EC.')
81
82            try:
83                if not ec.get_battery():
84                    raise error.TestError('Battery communication failed')
85            except cros_ec.ECError as e:
86                logging.debug('ECError: %s', e)
87                logging.warning('No battery info via EC.')
88
89        if test_lightbar and not ec.get_lightbar():
90            raise error.TestError('Lightbar communication failed')
91