Merge "Change to add music files from path"
diff --git a/acts/framework/acts/controllers/iperf_server.py b/acts/framework/acts/controllers/iperf_server.py
index 07eb459..7576d86 100755
--- a/acts/framework/acts/controllers/iperf_server.py
+++ b/acts/framework/acts/controllers/iperf_server.py
@@ -55,8 +55,9 @@
         elif type(c) is dict and 'ssh_config' in c and 'port' in c:
             results.append(IPerfServerOverSsh(c['ssh_config'], c['port']))
         else:
-            raise ValueError('Config entry %s in %s is not a valid IPerfServer '
-                             'config.' % (repr(c), configs))
+            raise ValueError(
+                'Config entry %s in %s is not a valid IPerfServer '
+                'config.' % (repr(c), configs))
     return results
 
 
@@ -193,8 +194,7 @@
         instantaneous_rates = self.instantaneous_rates[iperf_ignored_interval:
                                                        -1]
         avg_rate = math.fsum(instantaneous_rates) / len(instantaneous_rates)
-        sqd_deviations = [(rate - avg_rate) ** 2 for rate in
-                          instantaneous_rates]
+        sqd_deviations = [(rate - avg_rate)**2 for rate in instantaneous_rates]
         std_dev = math.sqrt(
             math.fsum(sqd_deviations) / (len(sqd_deviations) - 1))
         return std_dev
@@ -252,8 +252,8 @@
 
         with IPerfServerBase.__log_file_lock:
             tags = [tag, IPerfServerBase.__log_file_counter]
-            out_file_name = 'IPerfServer,%s.log' % (','.join(
-                [str(x) for x in tags if x != '' and x is not None]))
+            out_file_name = 'IPerfServer,%s.log' % (
+                ','.join([str(x) for x in tags if x != '' and x is not None]))
             IPerfServerBase.__log_file_counter += 1
 
         file_path = os.path.join(out_dir, out_file_name)
@@ -379,14 +379,15 @@
             return
 
         self._ssh_session.run_async('kill -9 {}'.format(str(self._iperf_pid)))
-        iperf_result = self._ssh_session.run(
-            'cat {}'.format(self._get_remote_log_path()))
+        iperf_result = self._ssh_session.run('cat {}'.format(
+            self._get_remote_log_path()))
 
         log_file = self._get_full_file_path(self._current_tag)
         with open(log_file, 'w') as f:
             f.write(iperf_result.stdout)
 
-        self._ssh_session.run_async('rm {}'.format(self._get_remote_log_path()))
+        self._ssh_session.run_async('rm {}'.format(
+            self._get_remote_log_path()))
         self._iperf_pid = None
         return log_file
 
@@ -412,7 +413,8 @@
         _AndroidDeviceBridge.android_devices = {}
 
 
-event_bus.register_subscription(_AndroidDeviceBridge.on_test_begin.subscription)
+event_bus.register_subscription(
+    _AndroidDeviceBridge.on_test_begin.subscription)
 event_bus.register_subscription(_AndroidDeviceBridge.on_test_end.subscription)
 
 
@@ -472,6 +474,8 @@
                 cmd=self._iperf_command,
                 extra_flags=extra_args,
                 log_file=self._get_device_log_path()))
+        self._iperf_process_adb_pid = self._android_device.adb.shell(
+            'pgrep iperf3 -n')
 
         self._current_tag = tag
 
@@ -486,15 +490,24 @@
 
         job.run('kill -9 {}'.format(self._iperf_process.pid))
 
-        iperf_result = self._android_device.adb.shell(
-            'cat {}'.format(self._get_device_log_path()))
+        #TODO(markdr): update with definitive kill method
+        while True:
+            iperf_process_list = self._android_device.adb.shell('pgrep iperf3')
+            if iperf_process_list.find(self._iperf_process_adb_pid) == -1:
+                break
+            else:
+                self._android_device.adb.shell("kill -9 {}".format(
+                    self._iperf_process_adb_pid))
+
+        iperf_result = self._android_device.adb.shell('cat {}'.format(
+            self._get_device_log_path()))
 
         log_file = self._get_full_file_path(self._current_tag)
         with open(log_file, 'w') as f:
             f.write(iperf_result)
 
-        self._android_device.adb.shell(
-            'rm {}'.format(self._get_device_log_path()))
+        self._android_device.adb.shell('rm {}'.format(
+            self._get_device_log_path()))
 
         self._iperf_process = None
         return log_file
diff --git a/acts/framework/acts/controllers/utils_lib/ssh/connection.py b/acts/framework/acts/controllers/utils_lib/ssh/connection.py
index 6405b52..891a07e 100644
--- a/acts/framework/acts/controllers/utils_lib/ssh/connection.py
+++ b/acts/framework/acts/controllers/utils_lib/ssh/connection.py
@@ -214,7 +214,7 @@
                 line_index = output.find('\n') + 1
                 if line_index == 0:
                     line_index = len(output)
-                real_output = output[line_index + 1:].encode(io_encoding)
+                real_output = output[line_index:].encode(io_encoding)
 
                 result = job.Result(
                     command=result.command,
diff --git a/acts/framework/acts/test_utils/wifi/wifi_test_utils.py b/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
index 37523e3..665388d 100755
--- a/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
+++ b/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
@@ -41,7 +41,7 @@
 # change.
 SHORT_TIMEOUT = 30
 ROAMING_TIMEOUT = 30
-
+WIFI_CONNECTION_TIMEOUT_DEFAULT = 30
 # Speed of light in m/s.
 SPEED_OF_LIGHT = 299792458
 
@@ -1555,6 +1555,28 @@
             raise signals.TestFailure(msg)
 
 
+def check_autoconnect_to_open_network(ad, conn_timeout=WIFI_CONNECTION_TIMEOUT_DEFAULT):
+    """Connects to any open WiFI AP
+     Args:
+         timeout value in sec to wait for UE to connect to a WiFi AP
+     Returns:
+         True if UE connects to WiFi AP (supplicant_state = completed)
+         False if UE fails to complete connection within WIFI_CONNECTION_TIMEOUT time.
+    """
+    if ad.droid.wifiCheckState():
+        return True
+    ad.droid.wifiToggleState()
+    wifi_connection_state = None
+    timeout = time.time() + conn_timeout
+    while wifi_connection_state != "completed":
+        wifi_connection_state = ad.droid.wifiGetConnectionInfo()[
+            'supplicant_state']
+        if time.time() > timeout:
+            ad.log.warning("Failed to connect to WiFi AP")
+            return False
+    return True
+
+
 def expand_enterprise_config_by_phase2(config):
     """Take an enterprise config and generate a list of configs, each with
     a different phase2 auth type.