FAFT: Fix firmware_TPMNotCorruptedDevMode by explicitly stopping tcsd/trunksd

tcsd/trunksd is no longer stopped when running a FAFT test. Explicitly call
APIs to stop/restart tcsd/trunksd.

BUG=chrome-os-partner:62150
TEST=Ran firmware_TPMNotCorruptedDevMode and passed the modified code.

Change-Id: I7de502820f741ed52c5230ac53a7c28fa86e635f
Reviewed-on: https://chromium-review.googlesource.com/436245
Commit-Ready: Wai-Hong Tam <waihong@google.com>
Tested-by: Wai-Hong Tam <waihong@google.com>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
diff --git a/client/cros/faft/rpc_functions.py b/client/cros/faft/rpc_functions.py
index 90d8213..fa1f022 100755
--- a/client/cros/faft/rpc_functions.py
+++ b/client/cros/faft/rpc_functions.py
@@ -640,6 +640,14 @@
         """Retrieve tpm kernel data key version."""
         return self._tpm_handler.get_kernel_key_version()
 
+    def _tpm_stop_daemon(self):
+        """Stop tpm related daemon."""
+        return self._tpm_handler.stop_daemon()
+
+    def _tpm_restart_daemon(self):
+        """Restart tpm related daemon which was stopped by stop_daemon()."""
+        return self._tpm_handler.restart_daemon()
+
     def _cgpt_get_attributes(self):
         """Get kernel attributes."""
         rootdev = self._system_get_root_dev()
diff --git a/client/cros/faft/utils/tpm_handler.py b/client/cros/faft/utils/tpm_handler.py
index 2c5c20b..7ac0765 100644
--- a/client/cros/faft/utils/tpm_handler.py
+++ b/client/cros/faft/utils/tpm_handler.py
@@ -74,28 +74,15 @@
                     1, [0x4c, 0x57, 0x52, 0x47])),
             'bios': TpmNvRam(FW_NV_ADDRESS, 10, 2)
             }
+        self.trunksd_started = False
+        self.tcsd_started = False
 
     def init(self, os_if):
         self.os_if = os_if
-        cmd = 'initctl status tcsd || initctl status trunksd'
-        status = self.os_if.run_shell_command_get_output(cmd) or ['']
-        # Expected status is like ['trunksd start/running, process 2375']
-        trunksd_started = status[0].startswith('trunksd start/running')
-        if trunksd_started:
-            self.os_if.run_shell_command('stop trunksd')
-        else:
-            tcsd_started = status[0].startswith('tcsd start/running')
-            if tcsd_started:
-                self.os_if.run_shell_command('stop tcsd')
-
+        self.stop_daemon()
         for nvram in self.nvrams.itervalues():
             nvram.init(self.os_if)
-
-        # Restart the daemon
-        if trunksd_started:
-            self.os_if.run_shell_command('start trunksd')
-        elif tcsd_started:
-            self.os_if.run_shell_command('start tcsd')
+        self.restart_daemon()
 
     def get_fw_version(self):
         return self.nvrams['bios'].get_body_version()
@@ -108,3 +95,28 @@
 
     def get_kernel_key_version(self):
         return self.nvrams['kernel'].get_key_version()
+
+    def stop_daemon(self):
+        """Stop TPM related daemon."""
+        if self.trunksd_started or self.tcsd_started:
+            raise TpmError('Called stop_daemon() before')
+
+        cmd = 'initctl status tcsd || initctl status trunksd'
+        status = self.os_if.run_shell_command_get_output(cmd) or ['']
+        # Expected status is like ['trunksd start/running, process 2375']
+        self.trunksd_started = status[0].startswith('trunksd start/running')
+        if self.trunksd_started:
+            self.os_if.run_shell_command('stop trunksd')
+        else:
+            self.tcsd_started = status[0].startswith('tcsd start/running')
+            if self.tcsd_started:
+                self.os_if.run_shell_command('stop tcsd')
+
+    def restart_daemon(self):
+        """Restart TPM related daemon which was stopped by stop_daemon()."""
+        if self.trunksd_started:
+            self.os_if.run_shell_command('start trunksd')
+            self.trunksd_started = False
+        elif self.tcsd_started:
+            self.os_if.run_shell_command('start tcsd')
+            self.tcsd_started = False