From f26f9427231a16652da2ba9064ea9ba89ff6701a Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Wed, 12 Jul 2017 12:27:28 +0100
Subject: [PATCH 1/2] tagert: factor out the wait_boot_complete code

The connect() method embeds some code to wait for a target to be
completely booted.

Let's move this code into a dedicated function.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/target.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/devlib/target.py b/devlib/target.py
index 35473b4..8a38a8c 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -877,8 +877,16 @@ class AndroidTarget(Target):
             pass
         self._connected_as_root = None
 
-    def connect(self, timeout=10, check_boot_completed=True):  # pylint: disable=arguments-differ
+    def wait_boot_complete(self, timeout=10):
         start = time.time()
+        boot_completed = boolean(self.getprop('sys.boot_completed'))
+        while not boot_completed and timeout >= time.time() - start:
+            time.sleep(5)
+            boot_completed = boolean(self.getprop('sys.boot_completed'))
+        if not boot_completed:
+            raise TargetError('Connected but Android did not fully boot.')
+
+    def connect(self, timeout=10, check_boot_completed=True):  # pylint: disable=arguments-differ
         device = self.connection_settings.get('device')
         if device and ':' in device:
             # ADB does not automatically remove a network device from it's
@@ -890,12 +898,7 @@ class AndroidTarget(Target):
         super(AndroidTarget, self).connect(timeout=timeout)
 
         if check_boot_completed:
-            boot_completed = boolean(self.getprop('sys.boot_completed'))
-            while not boot_completed and timeout >= time.time() - start:
-                time.sleep(5)
-                boot_completed = boolean(self.getprop('sys.boot_completed'))
-            if not boot_completed:
-                raise TargetError('Connected but Android did not fully boot.')
+            self.wait_boot_complete(timeout)
 
     def setup(self, executables=None):
         super(AndroidTarget, self).setup(executables)

From 0c7eb9e91e46fd551d9986afebe10670b1f160aa Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Wed, 12 Jul 2017 12:30:30 +0100
Subject: [PATCH 2/2] target: add support to kill/restart and ADB connection

Sometimes it could be useful to disconnect from the target, for example
when a "pass thought" energy monitor is used to control the USB
connection to the device.

This patch adds a couple of utility methods which allows to kill the ADB
server and restart it while waiting for the device to be ready to accept
new connections.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/target.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/devlib/target.py b/devlib/target.py
index 8a38a8c..e9b24df 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -1077,6 +1077,12 @@ class AndroidTarget(Target):
     def clear_logcat(self):
         adb_command(self.adb_name, 'logcat -c', timeout=30)
 
+    def adb_kill_server(self, timeout=30):
+        adb_command(self.adb_name, 'kill-server', timeout)
+
+    def adb_wait_for_device(self, timeout=30):
+        adb_command(self.adb_name, 'wait-for-device', timeout)
+
     def adb_reboot_bootloader(self, timeout=30):
         adb_command(self.adb_name, 'reboot-bootloader', timeout)