Interpret scan-failed event from wpa_supplicant

Scans can fail if there are other scans going on in the
firmware. We can only find out about such failures from
supplicant (it prints out a failure event).

Bug: 20192693

Change-Id: I03fe60bb569430c041258c3a4ab8da2d6b5e9ff2
diff --git a/service/java/com/android/server/wifi/WifiMonitor.java b/service/java/com/android/server/wifi/WifiMonitor.java
index a8f83d9..870dfaa 100644
--- a/service/java/com/android/server/wifi/WifiMonitor.java
+++ b/service/java/com/android/server/wifi/WifiMonitor.java
@@ -28,6 +28,7 @@
 import android.net.wifi.p2p.nsd.WifiP2pServiceResponse;
 import android.os.Message;
 import android.text.TextUtils;
+import android.util.LocalLog;
 import android.util.Log;
 
 import com.android.server.wifi.hotspot2.Utils;
@@ -66,9 +67,10 @@
     private static final int ASSOC_REJECT = 9;
     private static final int SSID_TEMP_DISABLE = 10;
     private static final int SSID_REENABLE = 11;
-    private static final int BSS_ADDED = 12;
-    private static final int BSS_REMOVED = 13;
+    private static final int BSS_ADDED    = 12;
+    private static final int BSS_REMOVED  = 13;
     private static final int UNKNOWN      = 14;
+    private static final int SCAN_FAILED  = 15;
 
     /** All events coming from the supplicant start with this prefix */
     private static final String EVENT_PREFIX_STR = "CTRL-EVENT-";
@@ -158,6 +160,13 @@
 
     /**
      * <pre>
+     * CTRL-EVENT-SCAN-FAILED ret=code[ retry=1]
+     * </pre>
+     */
+    private static final String SCAN_FAILED_STR =  "SCAN-FAILED";
+
+    /**
+     * <pre>
      * CTRL-EVENT-LINK-SPEED x Mb/s
      * </pre>
      * {@code x} is the link speed in Mb/sec.
@@ -450,6 +459,8 @@
     /* Request SIM Auth */
     public static final int SUP_REQUEST_SIM_AUTH                 = BASE + 16;
 
+    public static final int SCAN_FAILED_EVENT                    = BASE + 17;
+
     /* P2P events */
     public static final int P2P_DEVICE_FOUND_EVENT               = BASE + 21;
     public static final int P2P_DEVICE_LOST_EVENT                = BASE + 22;
@@ -721,6 +732,7 @@
     private static class MonitorThread extends Thread {
         private final WifiNative mWifiNative;
         private final WifiMonitorSingleton mWifiMonitorSingleton;
+        private final LocalLog mLocalLog = WifiNative.getLocalLog();
 
         public MonitorThread(WifiNative wifiNative, WifiMonitorSingleton wifiMonitorSingleton) {
             super("WifiMonitor");
@@ -733,9 +745,11 @@
             for (;;) {
                 String eventStr = mWifiNative.waitForEvent();
 
-                // Skip logging the common but mostly uninteresting scan-results event
-                if (DBG && eventStr.indexOf(SCAN_RESULTS_STR) == -1) {
-                    Log.d(TAG, "Event [" + eventStr + "]");
+                // Skip logging the common but mostly uninteresting events
+                if (eventStr.indexOf(BSS_ADDED_STR) == -1
+                        && eventStr.indexOf(BSS_REMOVED_STR) == -1) {
+                    if (DBG) Log.d(TAG, "Event [" + eventStr + "]");
+                    mLocalLog.log("Event [" + eventStr + "]");
                 }
 
                 if (mWifiMonitorSingleton.dispatchEvent(eventStr)) {
@@ -829,6 +843,8 @@
             event = STATE_CHANGE;
         else if (eventName.equals(SCAN_RESULTS_STR))
             event = SCAN_RESULTS;
+        else if (eventName.equals(SCAN_FAILED_STR))
+            event = SCAN_FAILED;
         else if (eventName.equals(LINK_SPEED_STR))
             event = LINK_SPEED;
         else if (eventName.equals(TERMINATING_STR))
@@ -980,6 +996,10 @@
                 mStateMachine.sendMessage(SCAN_RESULTS_EVENT);
                 break;
 
+            case SCAN_FAILED:
+                mStateMachine.sendMessage(SCAN_FAILED_EVENT);
+                break;
+
             case UNKNOWN:
                 if (DBG) {
                     logDbg("handleEvent unknown: " + Integer.toString(event) + "  " + remainder);
diff --git a/service/java/com/android/server/wifi/WifiNative.java b/service/java/com/android/server/wifi/WifiNative.java
index c8d77f5..ac41ca5 100644
--- a/service/java/com/android/server/wifi/WifiNative.java
+++ b/service/java/com/android/server/wifi/WifiNative.java
@@ -136,7 +136,7 @@
     // hold mLock before accessing mCmdIdLock
     private static int sCmdId;
 
-    public LocalLog getLocalLog() {
+    public static LocalLog getLocalLog() {
         return mLocalLog;
     }
 
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index ee8b153..3c14f00 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -2671,6 +2671,8 @@
                     sb.append(" last=").append(key);
                 }
                 break;
+            case WifiMonitor.SCAN_FAILED_EVENT:
+                break;
             case WifiMonitor.NETWORK_CONNECTION_EVENT:
                 sb.append(" ");
                 sb.append(Integer.toString(msg.arg1));
@@ -5207,6 +5209,7 @@
                 case WifiMonitor.NETWORK_CONNECTION_EVENT:
                 case WifiMonitor.NETWORK_DISCONNECTION_EVENT:
                 case WifiMonitor.SCAN_RESULTS_EVENT:
+                case WifiMonitor.SCAN_FAILED_EVENT:
                 case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT:
                 case WifiMonitor.AUTHENTICATION_FAILURE_EVENT:
                 case WifiMonitor.ASSOCIATION_REJECTION_EVENT:
@@ -5572,6 +5575,7 @@
                     sendMessageDelayed(CMD_START_SUPPLICANT, SUPPLICANT_RESTART_INTERVAL_MSECS);
                     break;
                 case WifiMonitor.SCAN_RESULTS_EVENT:
+                case WifiMonitor.SCAN_FAILED_EVENT:
                     maybeRegisterNetworkFactory(); // Make sure our NetworkFactory is registered
                     closeRadioScanStats();
                     noteScanEnd();
@@ -5781,6 +5785,7 @@
                     deferMessage(message);
                     break;
                 case WifiMonitor.SCAN_RESULTS_EVENT:
+                case WifiMonitor.SCAN_FAILED_EVENT:
                     // Loose scan results obtained in Driver Starting state, they can only confuse
                     // the state machine
                     break;
@@ -6412,6 +6417,9 @@
             case WifiMonitor.SCAN_RESULTS_EVENT:
                 s = "SCAN_RESULTS_EVENT";
                 break;
+            case WifiMonitor.SCAN_FAILED_EVENT:
+                s = "SCAN_FAILED_EVENT";
+                break;
             case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT:
                 s = "SUPPLICANT_STATE_CHANGE_EVENT";
                 break;
@@ -8785,6 +8793,7 @@
                     }
                     break;
                 case WifiMonitor.SCAN_RESULTS_EVENT:
+                case WifiMonitor.SCAN_FAILED_EVENT:
                     /* Re-enable background scan when a pending scan result is received */
                     if (mEnableBackgroundScan && mIsScanOngoing) {
                         enableBackgroundScan(true);