Closed-loop audio feedback client implementation.

This is an implementation of a feedback client for interactive audio
testing that is based on a closed-loop connection between the DUT's
audio-out/audio-in jacks.  This includes logic for both playback and
recording queries.

BUG=b:26162596
TEST=None

Change-Id: I930630a9beedbdbc1d9c879aeb50c27285682116
Reviewed-on: https://chromium-review.googlesource.com/319198
Commit-Ready: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Ralph Nathan <ralphnathan@chromium.org>
diff --git a/server/brillo/host_utils.py b/server/brillo/host_utils.py
new file mode 100644
index 0000000..7ac30bf
--- /dev/null
+++ b/server/brillo/host_utils.py
@@ -0,0 +1,42 @@
+# Copyright 2016 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Utilities used with Brillo hosts."""
+
+_RUN_BACKGROUND_TEMPLATE = '( %(cmd)s ) </dev/null >/dev/null 2>&1 & echo -n $!'
+
+_WAIT_CMD_TEMPLATE = """\
+to=%(timeout)d; \
+while test ${to} -ne 0; do \
+  test $(ps %(pid)d | wc -l) -gt 1 || break; \
+  sleep 1; \
+  to=$((to - 1)); \
+done; \
+test ${to} -ne 0 -o $(ps %(pid)d | wc -l) -eq 1 \
+"""
+
+
+def run_in_background(host, cmd):
+    """Runs a command in the background on the DUT.
+
+    @param host: A host object representing the DUT.
+    @param cmd: The command to run.
+
+    @return The background process ID (integer).
+    """
+    background_cmd = _RUN_BACKGROUND_TEMPLATE % {'cmd': cmd}
+    return int(host.run_output(background_cmd).strip())
+
+
+def wait_for_process(host, pid, timeout=-1):
+    """Waits for a process on the DUT to terminate.
+
+    @param host: A host object representing the DUT.
+    @param pid: The process ID (integer).
+    @param timeout: Number of seconds to wait; default is wait forever.
+
+    @return True if process terminated within the alotted time, False otherwise.
+    """
+    wait_cmd = _WAIT_CMD_TEMPLATE % {'pid': pid, 'timeout': timeout}
+    return host.run(wait_cmd, ignore_status=True).exit_status == 0