CHERRY-PICK FAFT: pulling wake* common code into firmware_test

Pulling delayed, wake_by_power_button,
wake_by_lid_switch, suspend_as_reboot into
firmware_test as it's common to both
firmware_DevModeStress and firmware_ECWakeSource.
Also increased WAKE_DELAY to 10, which is needed
for gnawty.

BUG=chromium:259640
BRANCH=none
TEST=tested on gnawty

Original Change-Id: Ib699f325b6d88980a1e028b770bcd0ea7febd166
Original Signed-off-by: Shelley Chen <shchen@chromium.org>
Original Reviewed-on: https://chromium-review.googlesource.com/307837
Original Reviewed-by: Wai-Hong Tam <waihong@chromium.org>

Change-Id: If4ca23587adbc789bed596e042b447668c125000
Signed-off-by: Luke_Yin@asus.com <Luke_Yin@asus.com>
Reviewed-on: https://chromium-review.googlesource.com/316091
Reviewed-by: Bernie Thompson <bhthompson@chromium.org>
Commit-Queue: Agnes Cheng <agnescheng@google.com>
diff --git a/server/cros/faft/firmware_test.py b/server/cros/faft/firmware_test.py
index bafc1e1..4556770 100644
--- a/server/cros/faft/firmware_test.py
+++ b/server/cros/faft/firmware_test.py
@@ -10,6 +10,7 @@
 import time
 import uuid
 
+from threading import Timer
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 from autotest_lib.server import test
@@ -94,6 +95,15 @@
     CHROMEOS_MAGIC = "CHROMEOS"
     CORRUPTED_MAGIC = "CORRUPTD"
 
+    # Delay for waiting client to return before EC suspend
+    EC_SUSPEND_DELAY = 5
+
+    # Delay between EC suspend and wake
+    WAKE_DELAY = 10
+
+    # Delay between closing and opening lid
+    LID_DELAY = 1
+
     _SERVOD_LOG = '/var/log/servod.log'
 
     _ROOTFS_PARTITION_NUMBER = 3
@@ -726,6 +736,69 @@
             self.check_ec_capability(['usbpd_uart'], suppress_warning=True)):
             self.servo.set('usbpd_uart_capture', 'off')
 
+    def _get_power_state(self, power_state):
+        """
+        Return the current power state of the AP
+        """
+        return self.ec.send_command_get_output("powerinfo", [power_state])
+
+    def wait_power_state(self, power_state, retries):
+        """
+        Wait for certain power state.
+
+        @param power_state: power state you are expecting
+        @param retries: retries.  This is necessary if AP is powering down
+        and transitioning through different states.
+        """
+        logging.info('Checking power state "%s" maximum %d times.',
+                     power_state, retries)
+        while retries > 0:
+            logging.info("try count: %d" % retries)
+            try:
+                retries = retries - 1
+                ret = self._get_power_state(power_state)
+                return True
+            except error.TestFail:
+                pass
+        return False
+
+    def delayed(seconds):
+        logging.info("delaying %d seconds" % seconds)
+        def decorator(f):
+            def wrapper(*args, **kargs):
+                t = Timer(seconds, f, args, kargs)
+                t.start()
+            return wrapper
+        return decorator
+
+    @delayed(WAKE_DELAY)
+    def wake_by_power_button(self):
+        """Delay by WAKE_DELAY seconds and then wake DUT with power button."""
+        self.servo.power_normal_press()
+
+    @delayed(WAKE_DELAY)
+    def wake_by_lid_switch(self):
+        """Delay by WAKE_DELAY seconds and then wake DUT with lid switch."""
+        self.servo.set('lid_open', 'no')
+        time.sleep(self.LID_DELAY)
+        self.servo.set('lid_open', 'yes')
+
+    def suspend_as_reboot(self, wake_func):
+        """
+        Suspend DUT and also kill FAFT client so that this acts like a reboot.
+
+        Args:
+          wake_func: A function that is called to wake DUT. Note that this
+            function must delay itself so that we don't wake DUT before
+            suspend_as_reboot returns.
+        """
+        cmd = '(sleep %d; powerd_dbus_suspend) &' % self.EC_SUSPEND_DELAY
+        self.faft_client.system.run_shell_command(cmd)
+        self.faft_client.disconnect()
+        time.sleep(self.EC_SUSPEND_DELAY)
+        logging.info("wake function disabled")
+        wake_func()
+
     def _fetch_servo_log(self):
         """Fetch the servo log."""
         cmd = '[ -e %s ] && cat %s || echo NOTFOUND' % ((self._SERVOD_LOG,) * 2)
diff --git a/server/site_tests/firmware_DevModeStress/firmware_DevModeStress.py b/server/site_tests/firmware_DevModeStress/firmware_DevModeStress.py
index 0b70710..bc57ee4 100644
--- a/server/site_tests/firmware_DevModeStress/firmware_DevModeStress.py
+++ b/server/site_tests/firmware_DevModeStress/firmware_DevModeStress.py
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-from threading import Timer
+#from threading import Timer
 import logging
 import time
 
@@ -10,15 +10,6 @@
 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
 
 
-def delayed(seconds):
-    def decorator(f):
-        def wrapper(*args, **kargs):
-            t = Timer(seconds, f, args, kargs)
-            t.start()
-        return wrapper
-    return decorator
-
-
 class firmware_DevModeStress(FirmwareTest):
     """
     Servo based, iterative developer firmware boot test. One iteration
@@ -26,32 +17,6 @@
     """
     version = 1
 
-    # Delay for waiting client to return before EC suspend
-    EC_SUSPEND_DELAY = 5
-
-    # Delay between EC suspend and wake
-    WAKE_DELAY = 5
-
-    @delayed(WAKE_DELAY)
-    def wake_by_power_button(self):
-        """Delay by WAKE_DELAY seconds and then wake DUT with power button."""
-        self.servo.power_normal_press()
-
-    def suspend_as_reboot(self, wake_func):
-        """
-        Suspend DUT and also kill FAFT client so that this acts like a reboot.
-
-        Args:
-          wake_func: A function that is called to wake DUT. Note that this
-            function must delay itself so that we don't wake DUT before
-            suspend_as_reboot returns.
-        """
-        cmd = '(sleep %d; powerd_dbus_suspend) &' % self.EC_SUSPEND_DELAY
-        self.faft_client.system.run_shell_command(cmd)
-        self.faft_client.disconnect()
-        time.sleep(self.EC_SUSPEND_DELAY)
-        wake_func()
-
     def initialize(self, host, cmdline_args):
         # Parse arguments from command line
         dict_args = utils.args_to_dict(cmdline_args)
diff --git a/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py b/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
index aef50f0..ef094ed 100644
--- a/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
+++ b/server/site_tests/firmware_ECWakeSource/firmware_ECWakeSource.py
@@ -3,37 +3,18 @@
 # found in the LICENSE file.
 
 import logging
-from threading import Timer
 import time
 
 from autotest_lib.client.common_lib import error
 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
 
 
-def delayed(seconds):
-    def decorator(f):
-        def wrapper(*args, **kargs):
-            t = Timer(seconds, f, args, kargs)
-            t.start()
-        return wrapper
-    return decorator
-
-
 class firmware_ECWakeSource(FirmwareTest):
     """
     Servo based EC wake source test.
     """
     version = 1
 
-    # Delay for waiting client to return before EC suspend
-    EC_SUSPEND_DELAY = 5
-
-    # Delay between EC suspend and wake
-    WAKE_DELAY = 5
-
-    # Delay between closing and opening lid
-    LID_DELAY = 1
-
     # Delay for waiting client to shut down
     SHUTDOWN_DELAY = 10
 
@@ -42,33 +23,6 @@
         # Only run in normal mode
         self.switcher.setup_mode('normal')
 
-    @delayed(WAKE_DELAY)
-    def wake_by_power_button(self):
-        """Delay by WAKE_DELAY seconds and then wake DUT with power button."""
-        self.servo.power_normal_press()
-
-    @delayed(WAKE_DELAY)
-    def wake_by_lid_switch(self):
-        """Delay by WAKE_DELAY seconds and then wake DUT with lid switch."""
-        self.servo.set('lid_open', 'no')
-        time.sleep(self.LID_DELAY)
-        self.servo.set('lid_open', 'yes')
-
-    def suspend_as_reboot(self, wake_func):
-        """
-        Suspend DUT and also kill FAFT client so that this acts like a reboot.
-
-        Args:
-          wake_func: A function that is called to wake DUT. Note that this
-            function must delay itself so that we don't wake DUT before
-            suspend_as_reboot returns.
-        """
-        cmd = '(sleep %d; powerd_dbus_suspend) &' % self.EC_SUSPEND_DELAY
-        self.faft_client.system.run_shell_command(cmd)
-        self.faft_client.disconnect()
-        time.sleep(self.EC_SUSPEND_DELAY)
-        wake_func()
-
     def hibernate_and_wake_by_power_button(self):
         """Shutdown and hibernate EC. Then wake by power button."""
         self.faft_client.system.run_shell_command("shutdown -P now")