faft: Chrome EC class supports a reboot method

So far, we do a EC reboot by either sending a EC command or using client
side ectool. This CL make Chrome EC class provide a single interface
to require EC reboot. Several reboot types are supported:
     default: EC soft reboot;
     'hard': EC hard/cold reboot;
     'ap-off': Leave AP off after EC reboot (by default, EC turns
               AP on after reboot if lid is open).

BUG=chromium-os:35254
TEST=run_remote_tests.sh --board link --remote dut FAFTSetup

Change-Id: Iabbd87ee7f06b8ef361bd56016fadc9696517cec
Reviewed-on: https://gerrit.chromium.org/gerrit/36862
Reviewed-by: Vic Yang <victoryang@chromium.org>
Commit-Ready: Tom Wai-Hong Tam <waihong@chromium.org>
Tested-by: Tom Wai-Hong Tam <waihong@chromium.org>
diff --git a/server/cros/chrome_ec.py b/server/cros/chrome_ec.py
index 93ac248..cc7445f 100644
--- a/server/cros/chrome_ec.py
+++ b/server/cros/chrome_ec.py
@@ -145,3 +145,24 @@
                 self.send_key_string_raw(raw)
             else:
                 self.key_press(sp)
+
+
+    def reboot(self, flags=''):
+        """Reboot EC with given flags.
+
+        Args:
+          flags: Optional, a space-separated string of flags passed to the
+                 reboot command, including:
+                   default: EC soft reboot;
+                   'hard': EC hard/cold reboot;
+                   'ap-off': Leave AP off after EC reboot (by default, EC turns
+                             AP on after reboot if lid is open).
+
+        Raises:
+          error.TestError: If the string of flags is invalid.
+        """
+        for flag in flags.split():
+            if flag not in ('hard', 'ap-off'):
+                raise error.TestError(
+                        'The flag %s of EC reboot command is invalid.' % flag)
+        self.send_command("reboot %s" % flags)
diff --git a/server/cros/faftsequence.py b/server/cros/faftsequence.py
index 7e38658..9e4b504 100644
--- a/server/cros/faftsequence.py
+++ b/server/cros/faftsequence.py
@@ -819,7 +819,7 @@
             time.sleep(self.delay.hold_cold_reset)
             self.servo.set('cold_reset', 'off')
             time.sleep(self.delay.ec_boot_to_console)
-            self.ec.send_command("reboot ap-off")
+            self.ec.reboot("ap-off")
             time.sleep(self.delay.ec_boot_to_console)
             self.ec.send_command("hostevent set 0x4000")
             self.servo.power_short_press()
@@ -1032,21 +1032,18 @@
         self.cold_reboot()
 
 
-    def sync_and_ec_reboot(self, args=''):
+    def sync_and_ec_reboot(self, flags=''):
         """Request the client sync and do a EC triggered reboot.
 
         Args:
-          args: Arguments passed to "ectool reboot_ec". Including:
-                  RO: jump to EC RO firmware.
-                  RW: jump to EC RW firmware.
-                  cold: Cold/hard reboot.
+          flags: Optional, a space-separated string of flags passed to EC
+                 reboot command, including:
+                   default: EC soft reboot;
+                   'hard': EC cold/hard reboot.
         """
         self.faft_client.run_shell_command('sync')
         time.sleep(self.delay.sync)
-        # Since EC reboot happens immediately, delay before actual reboot to
-        # allow FAFT client returning.
-        self.faft_client.run_shell_command('(sleep %d; ectool reboot_ec %s)&' %
-                                           (self.delay.ec_reboot_cmd, args))
+        self.ec.reboot(flags)
         time.sleep(self.delay.ec_reboot_cmd)
         self.check_lid_and_power_on()
 
diff --git a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
index e1df4ce..35ca308 100644
--- a/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
+++ b/server/site_tests/firmware_ECWriteProtect/firmware_ECWriteProtect.py
@@ -75,7 +75,7 @@
                 'state_checker': (lambda: self.checkers.ro_normal_checker('A',
                                               twostop=True) and
                                           self.write_protect_checker()),
-                'reboot_action': (self.sync_and_ec_reboot, 'cold'),
+                'reboot_action': (self.sync_and_ec_reboot, 'hard'),
             },
             {   # Step 4, expected EC RW boot, write protected. Restore RO
                 #         normal flag and deactivate write protect.