@@ -109,6 +109,9 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
109109 /* Set a two minute discover timeout to avoid STA scans from being blocked */
110110 private static final int DISCOVER_TIMEOUT_S = 120 ;
111111
112+ /* Idle time after a peer is gone when the group is torn down */
113+ private static final int GROUP_IDLE_TIME_S = 2 ;
114+
112115 /**
113116 * Delay between restarts upon failure to setup connection with supplicant
114117 */
@@ -343,10 +346,21 @@ public boolean processMessage(Message message) {
343346 case WifiMonitor .NETWORK_CONNECTION_EVENT :
344347 case WifiMonitor .NETWORK_DISCONNECTION_EVENT :
345348 case WifiMonitor .SUPPLICANT_STATE_CHANGE_EVENT :
349+ case WifiMonitor .P2P_GROUP_REMOVED_EVENT :
346350 case PEER_CONNECTION_USER_ACCEPT :
347351 case PEER_CONNECTION_USER_REJECT :
348352 case GROUP_CREATING_TIMED_OUT :
349353 break ;
354+ /* unexpected group created, remove */
355+ case WifiMonitor .P2P_GROUP_STARTED_EVENT :
356+ mGroup = (WifiP2pGroup ) message .obj ;
357+ loge ("Unexpected group creation, remove " + mGroup );
358+ mWifiNative .p2pGroupRemove (mGroup .getInterface ());
359+ break ;
360+ case WifiMonitor .P2P_GROUP_FORMATION_FAILURE_EVENT :
361+ loge ("Unexpected group failure, flush peers" );
362+ mWifiNative .p2pFlush ();
363+ break ;
350364 default :
351365 loge ("Unhandled message " + message );
352366 return NOT_HANDLED ;
@@ -476,6 +490,7 @@ public void enter() {
476490 if (DBG ) logd (getName ());
477491 sendP2pStateChangedBroadcast (true );
478492 mNetworkInfo .setIsAvailable (true );
493+ sendP2pConnectionChangedBroadcast ();
479494 initializeP2pSettings ();
480495 }
481496
@@ -561,9 +576,11 @@ public boolean processMessage(Message message) {
561576 //If peer is a GO, we do not need to send provisional discovery,
562577 //the supplicant takes care of it.
563578 if (mWifiNative .isGroupOwner (mSavedPeerConfig .deviceAddress )) {
579+ if (DBG ) logd ("Sending join to GO" );
564580 p2pConnectWithPinDisplay (mSavedPeerConfig , JOIN_GROUP );
565581 transitionTo (mGroupNegotiationState );
566582 } else {
583+ if (DBG ) logd ("Sending prov disc" );
567584 transitionTo (mProvisionDiscoveryState );
568585 }
569586 }
@@ -651,9 +668,7 @@ public boolean processMessage(Message message) {
651668 case GROUP_CREATING_TIMED_OUT :
652669 if (mGroupCreatingTimeoutIndex == message .arg1 ) {
653670 if (DBG ) logd ("Group negotiation timed out" );
654- updateDeviceStatus (mSavedPeerConfig .deviceAddress , WifiP2pDevice .FAILED );
655- mSavedPeerConfig = null ;
656- sendP2pPeersChangedBroadcast ();
671+ handleGroupCreationFailure ();
657672 transitionTo (mInactiveState );
658673 }
659674 break ;
@@ -663,12 +678,15 @@ public boolean processMessage(Message message) {
663678 WifiP2pManager .BUSY );
664679 break ;
665680 case WifiP2pManager .CANCEL_CONNECT :
666- if (mWifiNative .p2pCancelConnect ()) {
667- replyToMessage (message , WifiP2pManager .CANCEL_CONNECT_SUCCEEDED );
668- } else {
669- replyToMessage (message , WifiP2pManager .CANCEL_CONNECT_FAILED ,
670- WifiP2pManager .ERROR );
671- }
681+ //Do a supplicant p2p_cancel which only cancels an ongoing
682+ //group negotiation. This will fail for a pending provision
683+ //discovery or for a pending user action, but at the framework
684+ //level, we always treat cancel as succeded and enter
685+ //an inactive state
686+ mWifiNative .p2pCancelConnect ();
687+ handleGroupCreationFailure ();
688+ transitionTo (mInactiveState );
689+ replyToMessage (message , WifiP2pManager .CANCEL_CONNECT_SUCCEEDED );
672690 break ;
673691 default :
674692 return NOT_HANDLED ;
@@ -811,9 +829,7 @@ public boolean processMessage(Message message) {
811829 case WifiMonitor .P2P_GO_NEGOTIATION_FAILURE_EVENT :
812830 case WifiMonitor .P2P_GROUP_FORMATION_FAILURE_EVENT :
813831 if (DBG ) logd (getName () + " go failure" );
814- updateDeviceStatus (mSavedPeerConfig .deviceAddress , WifiP2pDevice .FAILED );
815- mSavedPeerConfig = null ;
816- sendP2pPeersChangedBroadcast ();
832+ handleGroupCreationFailure ();
817833 transitionTo (mInactiveState );
818834 break ;
819835 default :
@@ -838,6 +854,10 @@ public void enter() {
838854 setWifiP2pInfoOnGroupFormation (SERVER_ADDRESS );
839855 sendP2pConnectionChangedBroadcast ();
840856 }
857+
858+ if (!mPersistGroup ) {
859+ mWifiNative .setP2pGroupIdle (mGroup .getInterface (), GROUP_IDLE_TIME_S );
860+ }
841861 }
842862
843863 @ Override
@@ -886,6 +906,8 @@ public boolean processMessage(Message message) {
886906 if (DBG ) logd ("DhcpInfo: " + dhcpInfo );
887907 setWifiP2pInfoOnGroupFormation (dhcpInfo .serverAddress );
888908 sendP2pConnectionChangedBroadcast ();
909+ //Turn on power save on client
910+ mWifiNative .setP2pPowerSave (mGroup .getInterface (), true );
889911 } else {
890912 loge ("DHCP failed" );
891913 mWifiNative .p2pGroupRemove (mGroup .getInterface ());
@@ -1258,6 +1280,8 @@ private void initializeP2pSettings() {
12581280 //The supplicant default is to support everything, but a bug necessitates
12591281 //the framework to specify this explicitly
12601282 mWifiNative .setConfigMethods ("keypad display push_button" );
1283+ //STA has higher priority over P2P
1284+ mWifiNative .setConcurrencyPriority ("sta" );
12611285
12621286 mThisDevice .deviceAddress = mWifiNative .p2pGetDeviceAddress ();
12631287 updateThisDevice (WifiP2pDevice .AVAILABLE );
@@ -1269,6 +1293,14 @@ private void updateThisDevice(int status) {
12691293 sendThisDeviceChangedBroadcast ();
12701294 }
12711295
1296+ private void handleGroupCreationFailure () {
1297+ mSavedPeerConfig = null ;
1298+ /* After cancelling group formation, new connections on existing peers can fail
1299+ * at supplicant. Flush and restart discovery */
1300+ mWifiNative .p2pFlush ();
1301+ sendMessage (WifiP2pManager .DISCOVER_PEERS );
1302+ }
1303+
12721304 //State machine initiated requests can have replyTo set to null indicating
12731305 //there are no recepients, we ignore those reply actions
12741306 private void replyToMessage (Message msg , int what ) {
0 commit comments