Home | History | Annotate | Download | only in firmware_ECPowerButton
      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 
      5 import logging
      6 import re
      7 from threading import Timer
      8 
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
     11 
     12 
     13 class firmware_ECPowerButton(FirmwareTest):
     14     """
     15     Servo based EC power button test.
     16     """
     17     version = 1
     18 
     19     # Delay between shutdown and wake by power button
     20     LONG_WAKE_DELAY = 13
     21     SHORT_WAKE_DELAY = 7
     22 
     23     # Short duration of holding down power button to power on
     24     POWER_BUTTON_SHORT_POWER_ON_DURATION = 0.05
     25 
     26     # Long duration of holding down power button to power on
     27     POWER_BUTTON_LONG_POWER_ON_DURATION = 1
     28 
     29     # Duration of holding down power button to shut down with powerd
     30     POWER_BUTTON_POWERD_DURATION = 6
     31 
     32     # Duration of holding down power button to shut down without powerd
     33     POWER_BUTTON_NO_POWERD_DURATION = 10
     34 
     35     def initialize(self, host, cmdline_args):
     36         super(firmware_ECPowerButton, self).initialize(host, cmdline_args)
     37         # Only run in normal mode
     38         self.switcher.setup_mode('normal')
     39 
     40     def kill_powerd(self):
     41         """Stop powerd on client."""
     42         self.faft_client.system.run_shell_command("stop powerd")
     43 
     44     def debounce_power_button(self):
     45         """Check if power button debouncing works.
     46 
     47         Press power button for a very short period and checks for power
     48         button keycode.
     49         """
     50         # Delay 3 seconds to ensure client machine is waiting for key press.
     51         # Press power button for only 10ms. Should be debounced.
     52         logging.info('ECPowerButton: debounce_power_button')
     53         Timer(3, self.servo.power_key, [0.001]).start()
     54         return self.faft_client.system.check_keys([116])
     55 
     56     def shutdown_and_wake(self,
     57                           shutdown_powerkey_duration,
     58                           wake_delay,
     59                           wake_powerkey_duration):
     60         """
     61         Shutdown the system by power button, delay, and then power on
     62         by power button again.
     63         """
     64         self.servo.power_key(shutdown_powerkey_duration)
     65         Timer(wake_delay,
     66               self.servo.power_key,
     67               [wake_powerkey_duration]).start()
     68 
     69     def run_once(self):
     70         if not self.check_ec_capability():
     71             raise error.TestNAError("Nothing needs to be tested on this device")
     72 
     73         logging.info("Shutdown when powerd is still running and wake from S5 "
     74                      "with short power button press.")
     75 
     76         if self.servo.is_localhost():
     77             self.check_state(self.debounce_power_button)
     78         self.switcher.mode_aware_reboot(
     79                 'custom',
     80                 lambda:self.shutdown_and_wake(
     81                         self.POWER_BUTTON_POWERD_DURATION,
     82                         self.SHORT_WAKE_DELAY,
     83                         self.POWER_BUTTON_SHORT_POWER_ON_DURATION))
     84 
     85         logging.info("Shutdown when powerd is stopped and wake from G3 "
     86                           "with short power button press.")
     87         self.kill_powerd()
     88         self.switcher.mode_aware_reboot(
     89                 'custom',
     90                 lambda:self.shutdown_and_wake(
     91                         self.POWER_BUTTON_NO_POWERD_DURATION,
     92                         self.LONG_WAKE_DELAY,
     93                         self.POWER_BUTTON_SHORT_POWER_ON_DURATION))
     94 
     95         logging.info("Shutdown when powerd is still running and wake from G3 "
     96                      "with long power button press.")
     97         self.switcher.mode_aware_reboot(
     98                 'custom',
     99                 lambda:self.shutdown_and_wake(
    100                         self.POWER_BUTTON_POWERD_DURATION,
    101                         self.LONG_WAKE_DELAY,
    102                         self.POWER_BUTTON_LONG_POWER_ON_DURATION))
    103 
    104         logging.info("Shutdown when powerd is stopped and wake from S5 "
    105                      "with long power button press.")
    106         self.kill_powerd()
    107         self.switcher.mode_aware_reboot(
    108                 'custom',
    109                 lambda:self.shutdown_and_wake(
    110                         self.POWER_BUTTON_NO_POWERD_DURATION,
    111                         self.SHORT_WAKE_DELAY,
    112                         self.POWER_BUTTON_LONG_POWER_ON_DURATION))
    113