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