Skip to content

Commit 45a04db

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Avoid quick shutdown after a driver start" into ics-mr1
2 parents eaa17b7 + e6daca5 commit 45a04db

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;
@@ -1780,6 +1790,7 @@ public boolean processMessage(Message message) {
17801790
case CMD_STOP_SUPPLICANT_FAILED:
17811791
case CMD_START_DRIVER:
17821792
case CMD_STOP_DRIVER:
1793+
case CMD_DELAYED_STOP_DRIVER:
17831794
case CMD_START_AP:
17841795
case CMD_START_AP_SUCCESS:
17851796
case CMD_START_AP_FAILURE:
@@ -2442,6 +2453,7 @@ public void enter() {
24422453
EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
24432454

24442455
mIsRunning = true;
2456+
mInDelayedStop = false;
24452457
updateBatteryWorkSource(null);
24462458

24472459
/**
@@ -2521,6 +2533,30 @@ public boolean processMessage(Message message) {
25212533
WifiNative.setBluetoothCoexistenceScanModeCommand(mBluetoothConnectionActive);
25222534
break;
25232535
case CMD_STOP_DRIVER:
2536+
/* Already doing a delayed stop */
2537+
if (mInDelayedStop) {
2538+
if (DBG) log("Already in delayed stop");
2539+
break;
2540+
}
2541+
mInDelayedStop = true;
2542+
mDelayedStopCounter++;
2543+
if (DBG) log("Delayed stop message " + mDelayedStopCounter);
2544+
sendMessageDelayed(obtainMessage(CMD_DELAYED_STOP_DRIVER, mDelayedStopCounter,
2545+
0), DELAYED_DRIVER_STOP_MS);
2546+
break;
2547+
case CMD_START_DRIVER:
2548+
if (mInDelayedStop) {
2549+
mInDelayedStop = false;
2550+
mDelayedStopCounter++;
2551+
if (DBG) log("Delayed stop ignored due to start");
2552+
}
2553+
break;
2554+
case CMD_DELAYED_STOP_DRIVER:
2555+
if (message.arg1 != mDelayedStopCounter) break;
2556+
if (getCurrentState() != mDisconnectedState) {
2557+
WifiNative.disconnectCommand();
2558+
handleNetworkDisconnect();
2559+
}
25242560
mWakeLock.acquire();
25252561
WifiNative.stopDriverCommand();
25262562
transitionTo(mDriverStoppingState);
@@ -2879,10 +2915,6 @@ public boolean processMessage(Message message) {
28792915
/* Ignore */
28802916
case WifiMonitor.NETWORK_CONNECTION_EVENT:
28812917
break;
2882-
case CMD_STOP_DRIVER:
2883-
sendMessage(CMD_DISCONNECT);
2884-
deferMessage(message);
2885-
break;
28862918
case CMD_SET_SCAN_MODE:
28872919
if (message.arg1 == SCAN_ONLY_MODE) {
28882920
sendMessage(CMD_DISCONNECT);
@@ -2937,10 +2969,6 @@ public boolean processMessage(Message message) {
29372969
WifiNative.disconnectCommand();
29382970
transitionTo(mDisconnectingState);
29392971
break;
2940-
case CMD_STOP_DRIVER:
2941-
sendMessage(CMD_DISCONNECT);
2942-
deferMessage(message);
2943-
break;
29442972
case CMD_REQUEST_CM_WAKELOCK:
29452973
checkAndSetConnectivityInstance();
29462974
mCm.requestNetworkTransitionWakelock(TAG);
@@ -3036,9 +3064,6 @@ public void enter() {
30363064
public boolean processMessage(Message message) {
30373065
if (DBG) log(getName() + message.toString() + "\n");
30383066
switch (message.what) {
3039-
case CMD_STOP_DRIVER: /* Stop driver only after disconnect handled */
3040-
deferMessage(message);
3041-
break;
30423067
case CMD_SET_SCAN_MODE:
30433068
if (message.arg1 == SCAN_ONLY_MODE) {
30443069
deferMessage(message);

0 commit comments

Comments
 (0)