1# Copyright (c) 2012 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
5import logging
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
9
10
11class firmware_ECPowerG3(FirmwareTest):
12    """
13    Servo based EC X86 power G3 drop test.
14    """
15    version = 1
16
17    # Time out range for waiting system drop into G3.
18    G3_RETRIES = 13
19
20    # Record failure event
21    _failed = False
22
23    def initialize(self, host, cmdline_args):
24        super(firmware_ECPowerG3, self).initialize(host, cmdline_args)
25        # Only run in normal mode
26        self.switcher.setup_mode('normal')
27        self.ec.send_command("chan 0")
28
29    def cleanup(self):
30        self.ec.send_command("chan 0xffffffff")
31        super(firmware_ECPowerG3, self).cleanup()
32
33    def check_G3(self):
34        """Shutdown the system and check if X86 drop into G3 correctly."""
35        self.faft_client.system.run_shell_command("shutdown -P now")
36        if not self.wait_power_state("G3", self.G3_RETRIES):
37            logging.error("EC fails to drop into G3")
38            self._failed = True
39        self.servo.power_short_press()
40
41    def check_failure(self):
42        """Check whether any failure has occurred."""
43        return not self._failed
44
45    def run_once(self):
46        if not self.check_ec_capability(['x86']):
47            raise error.TestNAError("Nothing needs to be tested on this device")
48
49        logging.info("Power off and check if system drop into G3 correctly.")
50        self.switcher.mode_aware_reboot('custom', self.check_G3)
51
52        logging.info("Check if failure occurred.")
53        self.check_state(self.check_failure)
54