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
6from threading import Timer
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
10
11
12class firmware_FAFTSetup(FirmwareTest):
13    """This test checks the following FAFT hardware requirement:
14      - Warm reset
15      - Cold reset
16      - Recovery boot with USB stick
17      - USB stick is plugged into Servo board, not DUT
18      - Keyboard simulation
19      - No terminal opened on EC console
20    """
21    version = 1
22
23    # Delay to ensure client is ready to read the key press.
24    KEY_PRESS_DELAY = 2
25
26
27    def console_checker(self):
28        """Verify EC console is available if using Chrome EC."""
29        if not self.check_ec_capability(suppress_warning=True):
30            # Not Chrome EC. Nothing to check.
31            return True
32        try:
33            if self.ec.get_version():
34                return True
35        except: # pylint: disable=W0702
36            pass
37
38        logging.error("Cannot talk to EC console.")
39        logging.error("Please check there is no terminal opened on EC console.")
40        raise error.TestFail("Failed EC console check.")
41
42    def base_keyboard_checker(self, press_action):
43        """Press key and check from DUT.
44
45        Args:
46            press_action: A callable that would press the keys when called.
47        """
48        result = True
49        # Stop UI so that key presses don't go to Chrome.
50        self.faft_client.system.run_shell_command("stop ui")
51
52        # Press the keys
53        Timer(self.KEY_PRESS_DELAY, press_action).start()
54
55        # Invoke client side script to monitor keystrokes
56        if not self.faft_client.system.check_keys([28, 29, 32]):
57            result = False
58
59        # Turn UI back on
60        self.faft_client.system.run_shell_command("start ui")
61        return result
62
63    def keyboard_checker(self):
64        """Press 'd', Ctrl, ENTER by servo and check from DUT."""
65
66        def keypress():
67            """Press 'd', Ctrl, ENTER"""
68            self.servo.ctrl_d()
69            self.servo.enter_key()
70
71        return self.base_keyboard_checker(keypress)
72
73    def run_once(self):
74        """Main test logic"""
75        logging.info("Check EC console is available and test warm reboot")
76        self.console_checker()
77        self.switcher.mode_aware_reboot()
78
79        logging.info("Check test image is on USB stick and run recovery boot")
80        self.setup_usbkey(usbkey=True, host=False)
81        self.switcher.reboot_to_mode(to_mode='rec')
82
83        self.check_state((self.checkers.crossystem_checker,
84                          {'mainfw_type': 'recovery'}))
85
86        logging.info("Check cold boot")
87        self.switcher.mode_aware_reboot(reboot_type='cold')
88
89        if self.faft_config.fw_bypasser_type == 'ctrl_d_bypasser':
90            logging.info("Check keyboard simulation")
91            self.check_state(self.keyboard_checker)
92        else:
93            logging.info("Skip keyboard simulation on an embedded device")
94