• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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, time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib import utils
9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
10
11# platform_S0ixCycle test timing constants
12BEFORE_SUSPEND_WAIT_TIME_SECONDS = 10
13BEFORE_RESUME_WAIT_TIME_SECONDS = 2
14SUSPEND_WAIT_TIME_SECONDS = 5
15LIDOPEN_WAIT_TIME_SECONDS = 2
16POWER_STATE_RETRY_COUNT = 10
17
18
19class platform_S0ixCycle(FirmwareTest):
20    '''
21    Servo based S0ix cycle test and wake source.
22    '''
23    version = 1
24
25    def initialize(self, host, cmdline_args):
26        dict_args = utils.args_to_dict(cmdline_args)
27        self.faft_iterations = int(dict_args.get('faft_iterations', 1))
28        super(platform_S0ixCycle, self).initialize(host, cmdline_args)
29        self.switcher.setup_mode('normal')
30
31    def perform_s0ix_cycle(self):
32        """
33        Perform S0ix suspend/resume cycle and check state transition.
34        """
35        resume_sources = ['powerbtn', 'lid', 'kbpress']
36        for resume_source in resume_sources:
37            time.sleep(BEFORE_SUSPEND_WAIT_TIME_SECONDS)
38            self.perform_suspend()
39            self.perform_resume(resume_source)
40
41    def perform_suspend(self):
42        """
43        Perform suspend to idle and check state transition.
44        """
45        logging.info('== S0ix suspend and check the state transition ==')
46        # check S0ix state transition
47        if not self.wait_power_state('S0', POWER_STATE_RETRY_COUNT):
48            raise error.TestFail('Platform failed to reach S0 state.')
49        self.faft_client.system.run_shell_command(
50                'echo freeze > /sys/power/state &')
51        time.sleep(SUSPEND_WAIT_TIME_SECONDS)
52        # check S0ix state transition
53        if not self.wait_power_state('S0ix', POWER_STATE_RETRY_COUNT):
54            raise error.TestFail('Platform failed to reach S0ix state.')
55
56    def perform_resume(self, resume_source):
57        """
58        Perform resume with selected resume source and check state transition.
59        @param resume_source(string):resume source option.
60        """
61        logging.info('== S0ix resume and check the state transition ==')
62        time.sleep(BEFORE_RESUME_WAIT_TIME_SECONDS)
63        if resume_source == 'powerbtn':
64            self.ec.send_command('powerbtn')
65        elif resume_source == 'lid':
66            self.ec.send_command('lidclose')
67            time.sleep(LIDOPEN_WAIT_TIME_SECONDS)
68            self.ec.send_command('lidopen')
69        elif resume_source == 'kbpress':
70            self.ec.key_press('<enter>')
71        else:
72            raise error.TestFail('Invalid resume source.')
73        # check S0 state transition
74        if not self.wait_power_state('S0', POWER_STATE_RETRY_COUNT):
75            raise error.TestFail('Platform failed to reach S0 state.')
76
77    def is_skl_board(self):
78        """
79        Check this device is a SKL based ChromeBook.
80        """
81        skl_boards = ('Kunimitsu', 'Lars', 'Glados', 'Chell', 'Sentry')
82        output = self.faft_client.system.get_platform_name()
83        return output in skl_boards
84
85    def is_s0ix_supported(self):
86        """
87        Check this device supports suspend to idle.
88        """
89        cmd = 'cat /var/lib/power_manager/suspend_to_idle'
90        output = self.faft_client.system.run_shell_command_get_output(cmd)
91        if not output:
92            return False
93        else:
94            return int(output[0]) == 1
95
96    def run_once(self):
97        """
98        Main test logic
99        """
100        if not self.faft_config.chrome_ec or not self.check_ec_capability():
101            raise error.TestNAError(
102                    'Chrome EC is not supported on this device.')
103
104        if not (self.is_skl_board() and self.is_s0ix_supported()):
105            raise error.TestNAError(
106                    'Suspend to idle is not supported on this device.')
107
108        for i in xrange(self.faft_iterations):
109            logging.info('== Running FAFT ITERATION %d/%s ==', i + 1,
110                         self.faft_iterations)
111            logging.info(
112                    'S0ix suspend/resume back and check state transition.')
113            # wake the display by key press.
114            self.ec.key_press('<enter>')
115            self.switcher.mode_aware_reboot('custom', self.perform_s0ix_cycle)
116
117    def cleanup(self):
118        """
119        Cleanup after test completes
120        """
121        self.ec.set_uart_regexp('None')
122        # Test may failed before resume, wake the system.
123        self.ec.send_command('powerbtn')
124        # Perform a warm reboot as part of the cleanup.
125        self._client.reboot()
126        super(platform_S0ixCycle, self).cleanup()
127