autotest: Pick WiFi interfaces for WiFiClient by asking shill

Test code wants to reason about the WiFi interface that shill
is controlling.  To do this, just ask shill which interface
it is controlling rather than duplicating shill's heuristics
for picking interfaces.

While here, remove some deprecated fields that are no longer used.

BUG=chromium:339243
TEST=Tests now pass when the interface managed by shill is an
IBSS interface rather than a managed interface.

Change-Id: If00db60b517bf124d6edd05b57f74a29065ecb3c
Originally-Reviewed-on: https://chromium-review.googlesource.com/182734
(cherry picked from commit ba5df3ecf6c5cda20439633bb19aac4f33d835db)
Reviewed-on: https://chromium-review.googlesource.com/185265
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/client/common_lib/cros/network/shill_xmlrpc_server.py b/client/common_lib/cros/network/shill_xmlrpc_server.py
index a6443e6..94a111c 100755
--- a/client/common_lib/cros/network/shill_xmlrpc_server.py
+++ b/client/common_lib/cros/network/shill_xmlrpc_server.py
@@ -11,7 +11,6 @@
 import common
 from autotest_lib.client.common_lib import utils
 from autotest_lib.client.common_lib.cros import xmlrpc_server
-from autotest_lib.client.common_lib.cros.network import iw_runner
 from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
 from autotest_lib.client.cros import constants
 from autotest_lib.client.cros import cros_ui
@@ -177,7 +176,7 @@
         wifi_if = params.bgscan_config.interface
         if wifi_if is None:
             logging.info('Using default interface for bgscan configuration')
-            interfaces = iw_runner.IwRunner().list_interfaces()
+            interfaces = self.list_controlled_wifi_interfaces()
             if not interfaces:
                 return xmlrpc_datatypes.AssociationResult(
                         failure_reason='No wifi interfaces found?')
@@ -250,6 +249,24 @@
         return worked
 
 
+    @xmlrpc_server.dbus_safe(None)
+    def list_controlled_wifi_interfaces(self):
+        """List WiFi interfaces controlled by shill.
+
+        @return list of string WiFi device names (e.g. ['mlan0'])
+
+        """
+        ret = []
+        devices = self._wifi_proxy.get_devices()
+        for device in devices:
+            properties = self._wifi_proxy.dbus2primitive(
+                    device.GetProperties(utf8_strings=True))
+            if properties[self._wifi_proxy.DEVICE_PROPERTY_TYPE] != 'wifi':
+                continue
+            ret.append(properties[self._wifi_proxy.DEVICE_PROPERTY_NAME])
+        return ret
+
+
     @xmlrpc_server.dbus_safe(False)
     def disconnect(self, ssid):
         """Attempt to disconnect from the given ssid.
diff --git a/server/cros/network/wifi_client.py b/server/cros/network/wifi_client.py
index db20041..f7dd769 100644
--- a/server/cros/network/wifi_client.py
+++ b/server/cros/network/wifi_client.py
@@ -117,12 +117,6 @@
 
 
     @property
-    def command_ifconfig(self):
-        """@return string path to ifconfig command."""
-        return self._command_ifconfig
-
-
-    @property
     def command_ip(self):
         """@return string path to ip command."""
         return self._command_ip
@@ -227,18 +221,17 @@
         self._ping_thread = None
         self._result_dir = result_dir
         self._iw_runner = iw_runner.IwRunner(remote_host=self._host)
-        # Look up the WiFi device (and its MAC) on the client.
-        devs = self._iw_runner.list_interfaces()
-        if not devs:
-            raise error.TestFail('No wlan devices found on %s.' %
-                                 self.host.hostname)
-
-        if len(devs) > 1:
-            logging.warning('Warning, found multiple WiFi devices on %s: %r',
-                            self.host.hostname, devs)
-        self._wifi_if = devs[0]
-        self._interface = interface.Interface(self._wifi_if, host=self.host)
         if isinstance(self.host, adb_host.ADBHost):
+            # Look up the WiFi device (and its MAC) on the client.
+            devs = self.iw_runner.list_interfaces(desired_if_type='managed')
+            if not devs:
+                raise error.TestFail('No wlan devices found on %s.' %
+                                     self.host.hostname)
+
+            if len(devs) > 1:
+                logging.warning('Warning, found multiple WiFi devices on '
+                                '%s: %r', self.host.hostname, devs)
+            self._wifi_if = devs[0].if_name
             self._shill_proxy = wpa_cli_proxy.WpaCliProxy(
                     self.host, self._wifi_if)
         else:
@@ -253,14 +246,19 @@
                     command_name=constants.SHILL_XMLRPC_SERVER_CLEANUP_PATTERN,
                     ready_test_name=constants.SHILL_XMLRPC_SERVER_READY_METHOD,
                     timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS)
-            # These commands aren't known to work with ADB hosts.
-            self._command_ifconfig = 'ifconfig'
+            interfaces = self._shill_proxy.list_controlled_wifi_interfaces()
+            if not interfaces:
+                # TODO(wiley) Handle a missing management interface by
+                #             recreating it (or rebooting).
+                raise error.TestFail('No interfaces managed by shill on %s',
+                                     self.host.hostname)
+            self._wifi_if = interfaces[0]
             self._raise_logging_level()
         # Used for packet captures.
         self._packet_capturer = packet_capturer.get_packet_capturer(
                 self.host, host_description='client', ignore_failures=True)
         self._result_dir = result_dir
-
+        self._interface = interface.Interface(self._wifi_if, host=self.host)
         self._firewall_rules = []
         # Turn off powersave mode by default.
         self.powersave_switch(False)