Wifi fixes for RSSI offload.

1. When disconnecting, stop RSSI monitoring.
2. If no thresholds are specified, stop offload.
3. If invalid threholds are specified, stop offload.
3. When logging CMD_START_RSSI_MONITORING_OFFLOAD, include the
   thresholds as well.
5. Delete stopRssiMonitoring(), which is unused.
6. Minor code and logging simplification.

Bug: 21405941
Bug: 23679346
Bug: 23815756
Change-Id: Iddbd2d3c6a3e2a8f1b283fbe5b41e3e4e3acbfa1
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 2862822..54a49e1 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -280,7 +280,7 @@
         sendMessage(CMD_RSSI_THRESHOLD_BREACH, curRssi);
     }
 
-    public void processRssiThreshold(byte curRssi) {
+    public void processRssiThreshold(byte curRssi, int reason) {
         if (curRssi == Byte.MAX_VALUE || curRssi == Byte.MIN_VALUE) {
             Log.wtf(TAG, "processRssiThreshold: Invalid rssi " + curRssi);
             return;
@@ -291,15 +291,14 @@
                 // bounded by high(127) and low(-128) at extremeties
                 byte maxRssi = mRssiRanges[i];
                 byte minRssi = mRssiRanges[i-1];
-                Log.d(TAG, "Re-program rssi thresholds" + "maxRssi=" + maxRssi
-                        + " minRssi=" + minRssi + " curRssi=" + curRssi);
                 // This value of hw has to be believed as this value is averaged and has breached
                 // the rssi thresholds and raised event to host. This would be eggregious if this
                 // value is invalid
                 mWifiInfo.setRssi((int) curRssi);
                 updateCapabilities(getCurrentWifiConfiguration());
                 int ret = startRssiMonitoringOffload(maxRssi, minRssi);
-                Log.d(TAG, "Post re-programming rssi threshold ret = " + ret);
+                Log.d(TAG, "Re-program RSSI thresholds for " + smToString(reason) +
+                        ": [" + minRssi + ", " + maxRssi + "], curRssi=" + curRssi + " ret=" + ret);
                 break;
             }
         }
@@ -2422,14 +2421,6 @@
         sendMessage(CMD_SET_HIGH_PERF_MODE, enable ? 1 : 0, 0);
     }
 
-    public int stopRssiMonitoring(AsyncChannel channel) {
-        Message resultMsg = channel.sendMessageSynchronously(CMD_STOP_RSSI_MONITORING_OFFLOAD,
-                                    mRssiRanges);
-        int ret = (int)resultMsg.obj;
-        resultMsg.recycle();
-        return ret;
-    }
-
     /**
      * Set the country code
      *
@@ -3227,6 +3218,14 @@
                 sb.append(Integer.toString(msg.arg2));
                 sb.append(" cur=").append(disconnectingWatchdogCount);
                 break;
+            case CMD_START_RSSI_MONITORING_OFFLOAD:
+            case CMD_STOP_RSSI_MONITORING_OFFLOAD:
+            case CMD_RSSI_THRESHOLD_BREACH:
+                sb.append(" rssi=");
+                sb.append(Integer.toString(msg.arg1));
+                sb.append(" thresholds=");
+                sb.append(Arrays.toString(mRssiRanges));
+                break;
             default:
                 sb.append(" ");
                 sb.append(Integer.toString(msg.arg1));
@@ -4961,6 +4960,7 @@
                 + " - " + Thread.currentThread().getStackTrace()[4].getMethodName()
                 + " - " + Thread.currentThread().getStackTrace()[5].getMethodName());
 
+        stopRssiMonitoringOffload();
 
         clearCurrentConfigBSSID("handleNetworkDisconnect");
 
@@ -8003,6 +8003,7 @@
 
         @Override
         protected void setSignalStrengthThresholds(int[] thresholds) {
+            // 0. If there are no thresholds, or if the thresholds are invalid, stop RSSI monitoring.
             // 1. Tell the hardware to start RSSI monitoring here, possibly adding MIN_VALUE and
             //    MAX_VALUE at the start/end of the thresholds array if necessary.
             // 2. Ensure that when the hardware event fires, we fetch the RSSI from the hardware
@@ -8013,6 +8014,11 @@
             //    received, or we might skip callbacks.
             // 3. Ensure that when we disconnect, RSSI monitoring is stopped.
             log("Received signal strength thresholds: " + Arrays.toString(thresholds));
+            if (thresholds.length == 0) {
+                WifiStateMachine.this.sendMessage(CMD_STOP_RSSI_MONITORING_OFFLOAD,
+                        mWifiInfo.getRssi());
+                return;
+            }
             int [] rssiVals = Arrays.copyOf(thresholds, thresholds.length + 2);
             rssiVals[rssiVals.length - 2] = Byte.MIN_VALUE;
             rssiVals[rssiVals.length - 1] = Byte.MAX_VALUE;
@@ -8023,14 +8029,15 @@
                 if (val <= Byte.MAX_VALUE && val >= Byte.MIN_VALUE) {
                     rssiRange[i] = (byte) val;
                 } else {
-                    Log.e(TAG, "Illegal values for rssi thresholds " + val);
+                    Log.e(TAG, "Illegal value " + val + " for RSSI thresholds: "
+                            + Arrays.toString(rssiVals));
+                    WifiStateMachine.this.sendMessage(CMD_STOP_RSSI_MONITORING_OFFLOAD,
+                            mWifiInfo.getRssi());
+                    return;
                 }
             }
-            // ToDo: Do we quash rssi values in this sorted array which are very close?
+            // TODO: Do we quash rssi values in this sorted array which are very close?
             mRssiRanges = rssiRange;
-            //In the degenerate case when the input range has no values, the
-            //rssiRange will have only 2 values(127, -128), which when armed to
-            //any chipset can never trigger a rssi breach
             WifiStateMachine.this.sendMessage(CMD_START_RSSI_MONITORING_OFFLOAD,
                     mWifiInfo.getRssi());
         }
@@ -8533,16 +8540,13 @@
                     }
                     break;
                 case CMD_START_RSSI_MONITORING_OFFLOAD:
-                    byte currRssi = (byte)message.arg1;
-                    processRssiThreshold(currRssi);
+                case CMD_RSSI_THRESHOLD_BREACH:
+                    byte currRssi = (byte) message.arg1;
+                    processRssiThreshold(currRssi, message.what);
                     break;
                 case CMD_STOP_RSSI_MONITORING_OFFLOAD:
                     stopRssiMonitoringOffload();
                     break;
-                case CMD_RSSI_THRESHOLD_BREACH:
-                    byte curRssi = (byte)message.arg1;
-                    processRssiThreshold(curRssi);
-                    break;
                 default:
                     return NOT_HANDLED;
             }