@@ -359,8 +359,12 @@ public class WifiStateMachine extends StateMachine {
359359 static final int CMD_RESET_SUPPLICANT_STATE = BASE + 111 ;
360360
361361 /* P2p commands */
362+ /* We are ok with no response here since we wont do much with it anyway */
362363 public static final int CMD_ENABLE_P2P = BASE + 131 ;
363- public static final int CMD_DISABLE_P2P = BASE + 132 ;
364+ /* In order to shut down supplicant cleanly, we wait till p2p has
365+ * been disabled */
366+ public static final int CMD_DISABLE_P2P_REQ = BASE + 132 ;
367+ public static final int CMD_DISABLE_P2P_RSP = BASE + 133 ;
364368
365369 private static final int CONNECT_MODE = 1 ;
366370 private static final int SCAN_ONLY_MODE = 2 ;
@@ -458,6 +462,11 @@ public class WifiStateMachine extends StateMachine {
458462 private State mDriverStartingState = new DriverStartingState ();
459463 /* Driver started */
460464 private State mDriverStartedState = new DriverStartedState ();
465+ /* Wait until p2p is disabled
466+ * This is a special state which is entered right after we exit out of DriverStartedState
467+ * before transitioning to another state.
468+ */
469+ private State mWaitForP2pDisableState = new WaitForP2pDisableState ();
461470 /* Driver stopping */
462471 private State mDriverStoppingState = new DriverStoppingState ();
463472 /* Driver stopped */
@@ -699,6 +708,7 @@ public void onChange(boolean selfChange) {
699708 addState (mDisconnectingState , mConnectModeState );
700709 addState (mDisconnectedState , mConnectModeState );
701710 addState (mWpsRunningState , mConnectModeState );
711+ addState (mWaitForP2pDisableState , mSupplicantStartedState );
702712 addState (mDriverStoppingState , mSupplicantStartedState );
703713 addState (mDriverStoppedState , mSupplicantStartedState );
704714 addState (mSupplicantStoppingState , mDefaultState );
@@ -2433,7 +2443,11 @@ public boolean processMessage(Message message) {
24332443 WifiConfiguration config ;
24342444 switch (message .what ) {
24352445 case CMD_STOP_SUPPLICANT : /* Supplicant stopped by user */
2436- transitionTo (mSupplicantStoppingState );
2446+ if (mP2pSupported ) {
2447+ transitionTo (mWaitForP2pDisableState );
2448+ } else {
2449+ transitionTo (mSupplicantStoppingState );
2450+ }
24372451 break ;
24382452 case WifiMonitor .SUP_DISCONNECTION_EVENT : /* Supplicant connection lost */
24392453 loge ("Connection lost, restart supplicant" );
@@ -2443,7 +2457,11 @@ public boolean processMessage(Message message) {
24432457 handleNetworkDisconnect ();
24442458 sendSupplicantConnectionChangedBroadcast (false );
24452459 mSupplicantStateTracker .sendMessage (CMD_RESET_SUPPLICANT_STATE );
2446- transitionTo (mDriverLoadedState );
2460+ if (mP2pSupported ) {
2461+ transitionTo (mWaitForP2pDisableState );
2462+ } else {
2463+ transitionTo (mDriverLoadedState );
2464+ }
24472465 sendMessageDelayed (CMD_START_SUPPLICANT , SUPPLICANT_RESTART_INTERVAL_MSECS );
24482466 break ;
24492467 case WifiMonitor .SCAN_RESULTS_EVENT :
@@ -2838,8 +2856,12 @@ public boolean processMessage(Message message) {
28382856 }
28392857 mWakeLock .acquire ();
28402858 mWifiNative .stopDriver ();
2841- transitionTo (mDriverStoppingState );
28422859 mWakeLock .release ();
2860+ if (mP2pSupported ) {
2861+ transitionTo (mWaitForP2pDisableState );
2862+ } else {
2863+ transitionTo (mDriverStoppingState );
2864+ }
28432865 break ;
28442866 case CMD_START_PACKET_FILTERING :
28452867 if (message .arg1 == MULTICAST_V6 ) {
@@ -2885,8 +2907,63 @@ public void exit() {
28852907 mIsRunning = false ;
28862908 updateBatteryWorkSource (null );
28872909 mScanResults = new ArrayList <ScanResult >();
2910+ }
2911+ }
28882912
2889- if (mP2pSupported ) mWifiP2pChannel .sendMessage (WifiStateMachine .CMD_DISABLE_P2P );
2913+ class WaitForP2pDisableState extends State {
2914+ private State mTransitionToState ;
2915+ @ Override
2916+ public void enter () {
2917+ if (DBG ) log (getName () + "\n " );
2918+ EventLog .writeEvent (EVENTLOG_WIFI_STATE_CHANGED , getName ());
2919+ switch (getCurrentMessage ().what ) {
2920+ case WifiMonitor .SUP_DISCONNECTION_EVENT :
2921+ mTransitionToState = mDriverLoadedState ;
2922+ break ;
2923+ case CMD_DELAYED_STOP_DRIVER :
2924+ mTransitionToState = mDriverStoppingState ;
2925+ break ;
2926+ case CMD_STOP_SUPPLICANT :
2927+ mTransitionToState = mSupplicantStoppingState ;
2928+ break ;
2929+ default :
2930+ mTransitionToState = mDriverStoppingState ;
2931+ break ;
2932+ }
2933+ mWifiP2pChannel .sendMessage (WifiStateMachine .CMD_DISABLE_P2P_REQ );
2934+ }
2935+ @ Override
2936+ public boolean processMessage (Message message ) {
2937+ if (DBG ) log (getName () + message .toString () + "\n " );
2938+ switch (message .what ) {
2939+ case WifiStateMachine .CMD_DISABLE_P2P_RSP :
2940+ transitionTo (mTransitionToState );
2941+ break ;
2942+ /* Defer wifi start/shut and driver commands */
2943+ case CMD_LOAD_DRIVER :
2944+ case CMD_UNLOAD_DRIVER :
2945+ case CMD_START_SUPPLICANT :
2946+ case CMD_STOP_SUPPLICANT :
2947+ case CMD_START_AP :
2948+ case CMD_STOP_AP :
2949+ case CMD_START_DRIVER :
2950+ case CMD_STOP_DRIVER :
2951+ case CMD_SET_SCAN_MODE :
2952+ case CMD_SET_SCAN_TYPE :
2953+ case CMD_SET_COUNTRY_CODE :
2954+ case CMD_SET_FREQUENCY_BAND :
2955+ case CMD_START_PACKET_FILTERING :
2956+ case CMD_STOP_PACKET_FILTERING :
2957+ case CMD_START_SCAN :
2958+ case CMD_DISCONNECT :
2959+ case CMD_REASSOCIATE :
2960+ case CMD_RECONNECT :
2961+ deferMessage (message );
2962+ break ;
2963+ default :
2964+ return NOT_HANDLED ;
2965+ }
2966+ return HANDLED ;
28902967 }
28912968 }
28922969
0 commit comments