@@ -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