[Autotest]Set default attenuation to zero

The new simplified Android rig consists of attenuators that
are used forthe Android side tests. The autotests that will
run on these rigs will not require any attenuators. Hence,
they need to be normalized before running any of the SimpleConnect tests.

BUG=b:29877322
TEST=Tested locally

Change-Id: Iff4007c62c9295706ed9b466ca4a926f2c989578
Reviewed-on: https://chromium-review.googlesource.com/357251
Commit-Ready: Bindu Mahadev <bmahadev@chromium.org>
Tested-by: Bindu Mahadev <bmahadev@chromium.org>
Reviewed-by: Ang Li <angli@google.com>
Reviewed-by: Kris Rambish <krisr@chromium.org>
diff --git a/client/cros/networking/android_xmlrpc_server.py b/client/cros/networking/android_xmlrpc_server.py
index 7a18c91..f765216 100755
--- a/client/cros/networking/android_xmlrpc_server.py
+++ b/client/cros/networking/android_xmlrpc_server.py
@@ -12,6 +12,7 @@
 import select
 import shutil
 import signal
+import subprocess
 import threading
 import time
 
@@ -20,6 +21,7 @@
 from acts import logger
 from acts import utils
 from acts.controllers import android_device
+from acts.controllers import attenuator
 from acts.test_utils.wifi import wifi_test_utils as wutils
 
 
@@ -154,11 +156,13 @@
     SHILL_CONNECTED_STATES =  ['portal', 'online', 'ready']
     DISCONNECTED_SSID = '0x'
     DISCOVERY_POLLING_INTERVAL = 1
+    NUM_ATTEN = 4
 
 
-    def __init__(self, serial_number, log_dir):
+    def __init__(self, serial_number, log_dir, test_station):
         """Initializes the ACTS library components.
 
+        @test_station string represting teststation's hostname.
         @param serial_number Serial number of the android device to be tested,
                None if there is only one device connected to the host.
         @param log_dir Path to store output logs of this run.
@@ -181,6 +185,46 @@
                    ) % serial_number
             logging.error(msg)
             raise XmlRpcServerError(msg)
+        # Even if we find one attenuator assume the rig has attenuators for now.
+        # With the single IP attenuator, this will be a easy check.
+        rig_has_attenuator = False
+        count = 0
+        for i in range(1, self.NUM_ATTEN + 1):
+            atten_addr = test_station+'-attenuator-'+'%d' %i
+            if subprocess.Popen(['ping', '-c', '2', atten_addr],
+                                 stdout=subprocess.PIPE).communicate()[0]:
+                rig_has_attenuator = True
+                count = count + 1
+        if rig_has_attenuator and count == self.NUM_ATTEN:
+            atten = attenuator.create([{"Address":test_station+'-attenuator-1',
+                                        "Port":23,
+                                        "Model":"minicircuits",
+                                        "InstrumentCount": 1,
+                                        "Paths":["Attenuator-1"]},
+                                        {"Address":test_station+'-attenuator-2',
+                                        "Port":23,
+                                        "Model":"minicircuits",
+                                        "InstrumentCount": 1,
+                                        "Paths":["Attenuator-2"]},
+                                        {"Address":test_station+'-attenuator-3',
+                                        "Port":23,
+                                        "Model":"minicircuits",
+                                        "InstrumentCount": 1,
+                                        "Paths":["Attenuator-3"]},
+                                        {"Address":test_station+'-attenuator-4',
+                                        "Port":23,
+                                        "Model":"minicircuits",
+                                        "InstrumentCount": 1,
+                                        "Paths":["Attenuator-4"]}])
+            device = 0
+            # Set attenuation on all attenuators to 0.
+            for device in range(len(atten)):
+               atten[device].set_atten(0)
+            attenuator.destroy(atten)
+        elif rig_has_attenuator and count < self.NUM_ATTEN:
+            msg = 'One or more attenuators are down.'
+            logging.error(msg)
+            raise XmlRpcServerError(msg)
 
 
     def __enter__(self):
@@ -471,10 +515,13 @@
                          help='Serial Number of the device to test.')
     parser.add_argument('-l', '--log-dir', action='store', default=None,
                          help='Path to store output logs.')
+    parser.add_argument('-t', '--test-station', action='store', default=None,
+                         help='The accompaning teststion hostname.')
     args = parser.parse_args()
     logging.basicConfig(level=logging.DEBUG)
     logging.debug("android_xmlrpc_server main...")
     server = XmlRpcServer('localhost', 9989)
     server.register_delegate(
-            AndroidXmlRpcDelegate(args.serial_number, args.log_dir))
+            AndroidXmlRpcDelegate(args.serial_number, args.log_dir,
+                                  args.test_station))
     server.run()
diff --git a/server/cros/network/wifi_client.py b/server/cros/network/wifi_client.py
index be4f96f..1b9075c 100644
--- a/server/cros/network/wifi_client.py
+++ b/server/cros/network/wifi_client.py
@@ -92,6 +92,7 @@
         xmlrpc_server_command = constants.SHILL_BRILLO_XMLRPC_SERVER_COMMAND
         log_path = SHILL_BRILLO_XMLRPC_LOG_PATH
         command_name = constants.SHILL_BRILLO_XMLRPC_SERVER_CLEANUP_PATTERN
+        rpc_server_host = host
     elif host.get_os_type() == adb_host.OS_TYPE_ANDROID:
         xmlrpc_server_command = constants.ANDROID_XMLRPC_SERVER_COMMAND
         command_name = constants.ANDROID_XMLRPC_SERVER_CLEANUP_PATTERN
@@ -99,18 +100,22 @@
             raise error.TestFail('No serial number detected')
         debug_dir = ANDROID_XMLRPC_DEBUG_DIR_FMT % host.adb_serial
         log_path = ANDROID_XMLRPC_LOG_FILE_FMT % host.adb_serial
+        teststation = host.teststation
+        hostname = teststation.hostname.split('.')[0]
         xmlrpc_server_command = (
-                '%s -s %s -l %s' % (
-                xmlrpc_server_command, host.adb_serial, debug_dir))
+                '%s -s %s -l %s -t %s' % (
+                xmlrpc_server_command, host.adb_serial, debug_dir,
+                hostname))
+        install_android_xmlrpc_server(teststation)
         # For android, start the XML RPC server on the accompanying host.
-        host = host.teststation
-        install_android_xmlrpc_server(host)
+        rpc_server_host = teststation
     else:
         xmlrpc_server_command = constants.SHILL_XMLRPC_SERVER_COMMAND
         log_path = SHILL_XMLRPC_LOG_PATH
         command_name = constants.SHILL_XMLRPC_SERVER_CLEANUP_PATTERN
+        rpc_server_host = host
     # Start up the XMLRPC proxy on the client
-    proxy = host.rpc_server_tracker.xmlrpc_connect(
+    proxy = rpc_server_host.rpc_server_tracker.xmlrpc_connect(
             xmlrpc_server_command,
             constants.SHILL_XMLRPC_SERVER_PORT,
             command_name=command_name,