wpa_supplicant: Improve scan during p2p_find

Change-Id: I733b23c2b7a24cfa96fe6593adbcbeb7d0798002
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/wpa_supplicant/events.c b/wpa_supplicant/events.c
index bb870c0..6be345d 100644
--- a/wpa_supplicant/events.c
+++ b/wpa_supplicant/events.c
@@ -1074,11 +1074,7 @@
 	wpa_supplicant_notify_scanning(wpa_s, 0);
 
 #ifdef CONFIG_P2P
-#ifdef ANDROID_P2P
-	if (p2p_search_pending(wpa_s->global->p2p) && !wpa_s->global->p2p_disabled &&
-#else
 	if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
-#endif
 	    wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending) {
 		wpa_s->p2p_cb_on_scan_complete = 0;
 		if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c
index 7afdfa5..469e383 100644
--- a/wpa_supplicant/p2p_supplicant.c
+++ b/wpa_supplicant/p2p_supplicant.c
@@ -62,11 +62,7 @@
 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
 
 #ifndef P2P_CONCURRENT_SEARCH_DELAY
-#ifdef ANDROID_P2P
-#define P2P_CONCURRENT_SEARCH_DELAY 0
-#else
 #define P2P_CONCURRENT_SEARCH_DELAY 500
-#endif
 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
 
 enum p2p_group_removal_reason {
@@ -1200,7 +1196,6 @@
 #define WFD_DEV_INFO_SIZE 9
 	char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
 	os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
-
 #ifdef CONFIG_WIFI_DISPLAY
 	if (info->wfd_subelems) {
 		wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
@@ -5340,7 +5335,12 @@
 		rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
 		if (!rn2 || os_strcmp(rn, rn2) != 0)
 			continue;
+#ifdef ANDROID_P2P
+		/* We need not delay the p2p_scan if STA is already connected */
+		if (ifs->wpa_state > WPA_SCANNING && ifs->wpa_state < WPA_COMPLETED) {
+#else
 		if (ifs->wpa_state > WPA_SCANNING) {
+#endif
 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
 				"delay due to concurrent operation on "
 				"interface %s",
@@ -5377,7 +5377,7 @@
 			 * P2P Client, remove the interface depending up on the connection
 			 * priority */
 			if(!wpas_is_p2p_prioritized(wpa_s)) {
-				/* STA connection has priority over existing 
+				/* STA connection has priority over existing
 				 * P2P connection. So remove the interface */
 				wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
 						"concurrent mode frequency conflict");
diff --git a/wpa_supplicant/scan.c b/wpa_supplicant/scan.c
index d8db81f..d8b3139 100644
--- a/wpa_supplicant/scan.c
+++ b/wpa_supplicant/scan.c
@@ -690,6 +690,12 @@
 
 	if (ret) {
 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
+#ifdef ANDROID_P2P
+		/* Restore back the wpa_s->scan_req if we failed the scan becoz of any reason */
+		wpa_msg(wpa_s, MSG_DEBUG, "Restoring back the wpa_s->scan_req "
+			"to the original value %d", scan_req);
+		wpa_s->scan_req = scan_req;
+#endif
 		if (prev_state != wpa_s->wpa_state)
 			wpa_supplicant_set_state(wpa_s, prev_state);
 		wpa_supplicant_req_scan(wpa_s, 1, 0);