Skip to content

Commit e6daca5

Browse files
committed
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
1 parent 3c5b65c commit e6daca5

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

services/java/com/android/server/WifiService.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -921,18 +921,14 @@ public void onReceive(Context context, Intent intent) {
921921
Slog.d(TAG, "ACTION_SCREEN_ON");
922922
}
923923
mAlarmManager.cancel(mIdleIntent);
924-
mDeviceIdle = false;
925924
mScreenOff = false;
926-
// Once the screen is on, we are not keeping WIFI running
927-
// because of any locks so clear that tracking immediately.
928-
reportStartWorkSource();
929925
evaluateTrafficStatsPolling();
930926
mWifiStateMachine.enableRssiPolling(true);
931927
if (mBackgroundScanSupported) {
932928
mWifiStateMachine.enableBackgroundScanCommand(false);
933929
}
934930
mWifiStateMachine.enableAllNetworks();
935-
updateWifiState();
931+
setDeviceIdleAndUpdateWifi(false);
936932
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
937933
if (DBG) {
938934
Slog.d(TAG, "ACTION_SCREEN_OFF");
@@ -950,36 +946,17 @@ public void onReceive(Context context, Intent intent) {
950946
* or plugged in to AC).
951947
*/
952948
if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
953-
WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
954-
if (info.getSupplicantState() != SupplicantState.COMPLETED) {
955-
// we used to go to sleep immediately, but this caused some race conditions
956-
// we don't have time to track down for this release. Delay instead,
957-
// but not as long as we would if connected (below)
958-
// TODO - fix the race conditions and switch back to the immediate turn-off
959-
long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
960-
if (DBG) {
961-
Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
962-
}
963-
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
964-
// // do not keep Wifi awake when screen is off if Wifi is not associated
965-
// mDeviceIdle = true;
966-
// updateWifiState();
949+
//Delayed shutdown if wifi is connected
950+
if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
951+
if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
952+
mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
953+
+ idleMillis, mIdleIntent);
967954
} else {
968-
long triggerTime = System.currentTimeMillis() + idleMillis;
969-
if (DBG) {
970-
Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
971-
+ "ms");
972-
}
973-
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
955+
setDeviceIdleAndUpdateWifi(true);
974956
}
975957
}
976958
} else if (action.equals(ACTION_DEVICE_IDLE)) {
977-
if (DBG) {
978-
Slog.d(TAG, "got ACTION_DEVICE_IDLE");
979-
}
980-
mDeviceIdle = true;
981-
reportStartWorkSource();
982-
updateWifiState();
959+
setDeviceIdleAndUpdateWifi(true);
983960
} else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
984961
/*
985962
* Set a timer to put Wi-Fi to sleep, but only if the screen is off
@@ -1056,6 +1033,12 @@ private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType)
10561033
}
10571034
};
10581035

1036+
private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
1037+
mDeviceIdle = deviceIdle;
1038+
reportStartWorkSource();
1039+
updateWifiState();
1040+
}
1041+
10591042
private synchronized void reportStartWorkSource() {
10601043
mTmpWorkSource.clear();
10611044
if (mDeviceIdle) {

wifi/java/android/net/wifi/WifiStateMachine.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,17 @@ public class WifiStateMachine extends StateMachine {
211211
static final int CMD_STOP_SUPPLICANT = BASE + 12;
212212
/* Start the driver */
213213
static final int CMD_START_DRIVER = BASE + 13;
214-
/* Start the driver */
214+
/* Stop the driver */
215215
static final int CMD_STOP_DRIVER = BASE + 14;
216216
/* Indicates Static IP succeded */
217217
static final int CMD_STATIC_IP_SUCCESS = BASE + 15;
218218
/* Indicates Static IP failed */
219219
static final int CMD_STATIC_IP_FAILURE = BASE + 16;
220220
/* Indicates supplicant stop failed */
221221
static final int CMD_STOP_SUPPLICANT_FAILED = BASE + 17;
222+
/* Delayed stop to avoid shutting down driver too quick*/
223+
static final int CMD_DELAYED_STOP_DRIVER = BASE + 18;
224+
222225

223226
/* Start the soft access point */
224227
static final int CMD_START_AP = BASE + 21;
@@ -389,6 +392,13 @@ public class WifiStateMachine extends StateMachine {
389392
private static final int MIN_INTERVAL_ENABLE_ALL_NETWORKS_MS = 10 * 60 * 1000; /* 10 minutes */
390393
private long mLastEnableAllNetworksTime;
391394

395+
/**
396+
* Starting and shutting down driver too quick causes problems leading to driver
397+
* being in a bad state. Delay driver stop.
398+
*/
399+
private static final int DELAYED_DRIVER_STOP_MS = 2 * 60 * 1000; /* 2 minutes */
400+
private int mDelayedStopCounter;
401+
private boolean mInDelayedStop = false;
392402

393403
private static final int MIN_RSSI = -200;
394404
private static final int MAX_RSSI = 256;
@@ -1779,6 +1789,7 @@ public boolean processMessage(Message message) {
17791789
case CMD_STOP_SUPPLICANT_FAILED:
17801790
case CMD_START_DRIVER:
17811791
case CMD_STOP_DRIVER:
1792+
case CMD_DELAYED_STOP_DRIVER:
17821793
case CMD_START_AP:
17831794
case CMD_START_AP_SUCCESS:
17841795
case CMD_START_AP_FAILURE:
@@ -2441,6 +2452,7 @@ public void enter() {
24412452
EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
24422453

24432454
mIsRunning = true;
2455+
mInDelayedStop = false;
24442456
updateBatteryWorkSource(null);
24452457

24462458
/**
@@ -2520,6 +2532,30 @@ public boolean processMessage(Message message) {
25202532
WifiNative.setBluetoothCoexistenceScanModeCommand(mBluetoothConnectionActive);
25212533
break;
25222534
case CMD_STOP_DRIVER:
2535+
/* Already doing a delayed stop */
2536+
if (mInDelayedStop) {
2537+
if (DBG) log("Already in delayed stop");
2538+
break;
2539+
}
2540+
mInDelayedStop = true;
2541+
mDelayedStopCounter++;
2542+
if (DBG) log("Delayed stop message " + mDelayedStopCounter);
2543+
sendMessageDelayed(obtainMessage(CMD_DELAYED_STOP_DRIVER, mDelayedStopCounter,
2544+
0), DELAYED_DRIVER_STOP_MS);
2545+
break;
2546+
case CMD_START_DRIVER:
2547+
if (mInDelayedStop) {
2548+
mInDelayedStop = false;
2549+
mDelayedStopCounter++;
2550+
if (DBG) log("Delayed stop ignored due to start");
2551+
}
2552+
break;
2553+
case CMD_DELAYED_STOP_DRIVER:
2554+
if (message.arg1 != mDelayedStopCounter) break;
2555+
if (getCurrentState() != mDisconnectedState) {
2556+
WifiNative.disconnectCommand();
2557+
handleNetworkDisconnect();
2558+
}
25232559
mWakeLock.acquire();
25242560
WifiNative.stopDriverCommand();
25252561
transitionTo(mDriverStoppingState);
@@ -2878,10 +2914,6 @@ public boolean processMessage(Message message) {
28782914
/* Ignore */
28792915
case WifiMonitor.NETWORK_CONNECTION_EVENT:
28802916
break;
2881-
case CMD_STOP_DRIVER:
2882-
sendMessage(CMD_DISCONNECT);
2883-
deferMessage(message);
2884-
break;
28852917
case CMD_SET_SCAN_MODE:
28862918
if (message.arg1 == SCAN_ONLY_MODE) {
28872919
sendMessage(CMD_DISCONNECT);
@@ -2936,10 +2968,6 @@ public boolean processMessage(Message message) {
29362968
WifiNative.disconnectCommand();
29372969
transitionTo(mDisconnectingState);
29382970
break;
2939-
case CMD_STOP_DRIVER:
2940-
sendMessage(CMD_DISCONNECT);
2941-
deferMessage(message);
2942-
break;
29432971
case CMD_REQUEST_CM_WAKELOCK:
29442972
checkAndSetConnectivityInstance();
29452973
mCm.requestNetworkTransitionWakelock(TAG);
@@ -3035,9 +3063,6 @@ public void enter() {
30353063
public boolean processMessage(Message message) {
30363064
if (DBG) log(getName() + message.toString() + "\n");
30373065
switch (message.what) {
3038-
case CMD_STOP_DRIVER: /* Stop driver only after disconnect handled */
3039-
deferMessage(message);
3040-
break;
30413066
case CMD_SET_SCAN_MODE:
30423067
if (message.arg1 == SCAN_ONLY_MODE) {
30433068
deferMessage(message);

0 commit comments

Comments
 (0)