wifi_client: Don't randomly scan with iw

Scanning with iw will conflict with shill/wpa_supplicant
initiated scans. Use the wifi_client helper functions
to make shill let go of the wifi interface before you scan,
and then restore the wifi interface with shill after you're done.

BUG=chromium:628791
TEST=Ran network_WiFi_Prefer5GHz locally.

Change-Id: I4517f2c2227bcf303c9412c6867692d062dc3524
Signed-off-by: Kirtika Ruchandani <kirtika@google.com>
Reviewed-on: https://chromium-review.googlesource.com/1292791
Commit-Ready: Kirtika Ruchandani <kirtika@chromium.org>
Tested-by: Kirtika Ruchandani <kirtika@chromium.org>
Reviewed-by: Grant Grundler <grundler@chromium.org>
diff --git a/server/cros/network/wifi_client.py b/server/cros/network/wifi_client.py
index 086af66..b403ba1 100644
--- a/server/cros/network/wifi_client.py
+++ b/server/cros/network/wifi_client.py
@@ -609,14 +609,18 @@
           @return boolean representing whether the expected bss count matches
           how many in the scan match the given ssid
           """
-          scan_results = self.iw_runner.scan(
-                  self.wifi_if,
-                  frequencies=[],
-                  ssids=[ssid])
-          if scan_results is None:
-              return False
-          num_bss_actual = sum(ssid == bss.ssid for bss in scan_results)
-          return num_bss_expected == num_bss_actual
+          self.claim_wifi_if() # Stop shill/supplicant scans
+          try:
+            scan_results = self.iw_runner.scan(
+                    self.wifi_if,
+                    frequencies=[],
+                    ssids=[ssid])
+            if scan_results is None:
+                return False
+            num_bss_actual = sum(ssid == bss.ssid for bss in scan_results)
+            return num_bss_expected == num_bss_actual
+          finally:
+            self.release_wifi_if()
 
       utils.poll_for_condition(
               condition=are_all_bsses_discovered,