# Copyright (c) 2011 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.


# DESCRIPTION :
#
# This is a hardware test for EC. The test uses ectool to check if the EC can
# receive message from host and send expected reponse back to host. It also
# checks basic EC functionality, such as FAN and temperature sensor.


import logging
import time

from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
from autotest_lib.client.cros import ec as cros_ec
from autotest_lib.client.cros.power import power_utils


class hardware_EC(test.test):
    """Class for hardware_EC test."""
    version = 1

    def run_once(self,
                 num_temp_sensor=0,
                 temp_sensor_to_test=None,
                 test_fan=False,
                 fan_rpm_error_margin=200,
                 test_battery=None,
                 test_lightbar=False,
                 fan_delay_secs=3):

        ec = cros_ec.EC()

        if not cros_ec.has_ectool() or not ec.hello(ignore_status=True):
            raise error.TestNAError('No support for Google EC')

        if test_battery is None:
            test_battery = power_utils.has_battery()

        if test_fan:
            try:
                ec.set_fanspeed(10000)
                time.sleep(fan_delay_secs)
                max_reading = ec.get_fanspeed()
                if max_reading == 0:
                    raise error.TestError('Unable to start fan')

                target_fanspeed = max_reading / 2
                ec.set_fanspeed(target_fanspeed)
                time.sleep(fan_delay_secs)
                current_reading = ec.get_fanspeed()

                # Sometimes the actual fan speed is close but not equal to
                # the target speed, so we add some error margin here.
                lower_bound = target_fanspeed - fan_rpm_error_margin
                upper_bound = target_fanspeed + fan_rpm_error_margin
                if not (lower_bound <= current_reading <= upper_bound):
                    raise error.TestError('Unable to set fan speed')
            finally:
                ec.auto_fan_ctrl()

        if temp_sensor_to_test is None:
            temp_sensor_to_test = list(range(num_temp_sensor))

        for idx in temp_sensor_to_test:
            temperature = ec.get_temperature(idx) - 273
            if temperature < 0 or temperature > 100:
                raise error.TestError(
                        'Abnormal temperature reading on sensor %d' % idx)

        if test_battery:
            try:
                logging.info('Battery temperature %d K',
                             ec.get_temperature(name='Battery'))
            except cros_ec.ECError as e:
                logging.debug('ECError: %s', e)
                logging.warning('No battery temperature via EC.')

            try:
                if not ec.get_battery():
                    raise error.TestError('Battery communication failed')
            except cros_ec.ECError as e:
                logging.debug('ECError: %s', e)
                logging.warning('No battery info via EC.')

        if test_lightbar and not ec.get_lightbar():
            raise error.TestError('Lightbar communication failed')
