Avoid quick shutdown after a driver start
Causes problems on our wext driver and potentially on cfg
based driver as well.
NLP can trigger very quick acquire and release within seconds
leading to driver switching between start and stop state.
We now keep driver up for atleast couple of minutes after a start
Bug: 5478196
Change-Id: I1b261578252c5fb9a65446241b51e5686d4d9cc3
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index 5bfe6f8..01eade1 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -921,18 +921,14 @@
Slog.d(TAG, "ACTION_SCREEN_ON");
}
mAlarmManager.cancel(mIdleIntent);
- mDeviceIdle = false;
mScreenOff = false;
- // Once the screen is on, we are not keeping WIFI running
- // because of any locks so clear that tracking immediately.
- reportStartWorkSource();
evaluateTrafficStatsPolling();
mWifiStateMachine.enableRssiPolling(true);
if (mBackgroundScanSupported) {
mWifiStateMachine.enableBackgroundScanCommand(false);
}
mWifiStateMachine.enableAllNetworks();
- updateWifiState();
+ setDeviceIdleAndUpdateWifi(false);
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
if (DBG) {
Slog.d(TAG, "ACTION_SCREEN_OFF");
@@ -950,36 +946,17 @@
* or plugged in to AC).
*/
if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
- WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
- if (info.getSupplicantState() != SupplicantState.COMPLETED) {
- // we used to go to sleep immediately, but this caused some race conditions
- // we don't have time to track down for this release. Delay instead,
- // but not as long as we would if connected (below)
- // TODO - fix the race conditions and switch back to the immediate turn-off
- long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
- if (DBG) {
- Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
- }
- mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
- // // do not keep Wifi awake when screen is off if Wifi is not associated
- // mDeviceIdle = true;
- // updateWifiState();
+ //Delayed shutdown if wifi is connected
+ if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
+ if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
+ mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ + idleMillis, mIdleIntent);
} else {
- long triggerTime = System.currentTimeMillis() + idleMillis;
- if (DBG) {
- Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
- + "ms");
- }
- mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
+ setDeviceIdleAndUpdateWifi(true);
}
}
} else if (action.equals(ACTION_DEVICE_IDLE)) {
- if (DBG) {
- Slog.d(TAG, "got ACTION_DEVICE_IDLE");
- }
- mDeviceIdle = true;
- reportStartWorkSource();
- updateWifiState();
+ setDeviceIdleAndUpdateWifi(true);
} else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
/*
* Set a timer to put Wi-Fi to sleep, but only if the screen is off
@@ -1056,6 +1033,12 @@
}
};
+ private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
+ mDeviceIdle = deviceIdle;
+ reportStartWorkSource();
+ updateWifiState();
+ }
+
private synchronized void reportStartWorkSource() {
mTmpWorkSource.clear();
if (mDeviceIdle) {
diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java
index e981da7..caf07c3 100644
--- a/wifi/java/android/net/wifi/WifiStateMachine.java
+++ b/wifi/java/android/net/wifi/WifiStateMachine.java
@@ -211,7 +211,7 @@
static final int CMD_STOP_SUPPLICANT = BASE + 12;
/* Start the driver */
static final int CMD_START_DRIVER = BASE + 13;
- /* Start the driver */
+ /* Stop the driver */
static final int CMD_STOP_DRIVER = BASE + 14;
/* Indicates Static IP succeded */
static final int CMD_STATIC_IP_SUCCESS = BASE + 15;
@@ -219,6 +219,9 @@
static final int CMD_STATIC_IP_FAILURE = BASE + 16;
/* Indicates supplicant stop failed */
static final int CMD_STOP_SUPPLICANT_FAILED = BASE + 17;
+ /* Delayed stop to avoid shutting down driver too quick*/
+ static final int CMD_DELAYED_STOP_DRIVER = BASE + 18;
+
/* Start the soft access point */
static final int CMD_START_AP = BASE + 21;
@@ -389,6 +392,13 @@
private static final int MIN_INTERVAL_ENABLE_ALL_NETWORKS_MS = 10 * 60 * 1000; /* 10 minutes */
private long mLastEnableAllNetworksTime;
+ /**
+ * Starting and shutting down driver too quick causes problems leading to driver
+ * being in a bad state. Delay driver stop.
+ */
+ private static final int DELAYED_DRIVER_STOP_MS = 2 * 60 * 1000; /* 2 minutes */
+ private int mDelayedStopCounter;
+ private boolean mInDelayedStop = false;
private static final int MIN_RSSI = -200;
private static final int MAX_RSSI = 256;
@@ -1779,6 +1789,7 @@
case CMD_STOP_SUPPLICANT_FAILED:
case CMD_START_DRIVER:
case CMD_STOP_DRIVER:
+ case CMD_DELAYED_STOP_DRIVER:
case CMD_START_AP:
case CMD_START_AP_SUCCESS:
case CMD_START_AP_FAILURE:
@@ -2441,6 +2452,7 @@
EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
mIsRunning = true;
+ mInDelayedStop = false;
updateBatteryWorkSource(null);
/**
@@ -2520,6 +2532,30 @@
WifiNative.setBluetoothCoexistenceScanModeCommand(mBluetoothConnectionActive);
break;
case CMD_STOP_DRIVER:
+ /* Already doing a delayed stop */
+ if (mInDelayedStop) {
+ if (DBG) log("Already in delayed stop");
+ break;
+ }
+ mInDelayedStop = true;
+ mDelayedStopCounter++;
+ if (DBG) log("Delayed stop message " + mDelayedStopCounter);
+ sendMessageDelayed(obtainMessage(CMD_DELAYED_STOP_DRIVER, mDelayedStopCounter,
+ 0), DELAYED_DRIVER_STOP_MS);
+ break;
+ case CMD_START_DRIVER:
+ if (mInDelayedStop) {
+ mInDelayedStop = false;
+ mDelayedStopCounter++;
+ if (DBG) log("Delayed stop ignored due to start");
+ }
+ break;
+ case CMD_DELAYED_STOP_DRIVER:
+ if (message.arg1 != mDelayedStopCounter) break;
+ if (getCurrentState() != mDisconnectedState) {
+ WifiNative.disconnectCommand();
+ handleNetworkDisconnect();
+ }
mWakeLock.acquire();
WifiNative.stopDriverCommand();
transitionTo(mDriverStoppingState);
@@ -2878,10 +2914,6 @@
/* Ignore */
case WifiMonitor.NETWORK_CONNECTION_EVENT:
break;
- case CMD_STOP_DRIVER:
- sendMessage(CMD_DISCONNECT);
- deferMessage(message);
- break;
case CMD_SET_SCAN_MODE:
if (message.arg1 == SCAN_ONLY_MODE) {
sendMessage(CMD_DISCONNECT);
@@ -2936,10 +2968,6 @@
WifiNative.disconnectCommand();
transitionTo(mDisconnectingState);
break;
- case CMD_STOP_DRIVER:
- sendMessage(CMD_DISCONNECT);
- deferMessage(message);
- break;
case CMD_REQUEST_CM_WAKELOCK:
checkAndSetConnectivityInstance();
mCm.requestNetworkTransitionWakelock(TAG);
@@ -3035,9 +3063,6 @@
public boolean processMessage(Message message) {
if (DBG) log(getName() + message.toString() + "\n");
switch (message.what) {
- case CMD_STOP_DRIVER: /* Stop driver only after disconnect handled */
- deferMessage(message);
- break;
case CMD_SET_SCAN_MODE:
if (message.arg1 == SCAN_ONLY_MODE) {
deferMessage(message);