P2P: get cached scan results on iface creation to avoid scan

This allows the p2p client to fast associate if the GO is already in the
scan cache without doing a full scan. This effectively reverts CL
14782089 and moves that optimization into core supplicant with the
intent of upstreaming.

Bug: 233925161
Test: Manual file transfer using Photos app, logs show that scan results
are updated directly from driver after interface creation and we fast
associate

09-08 03:26:19.538 D/wpa_supplicant( 2489): p2p-p2p0-0: Added interface p2p-p2p0-0
…
09-08 03:26:19.568 D/wpa_supplicant( 2489): nl80211: Received scan results (18 BSSes)
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: BSS: Start scan result update 1
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: BSS: Add new id 0 BSSID be:9b:d7:f0:a7:0e SSID 'DIRECT-30-16C8C7' freq 5180
…
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: Scan results matching the currently selected network
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: 0: be:9b:d7:f0:a7:0e freq=5180 level=-28 snr=75 est_throughput=390001
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: Selecting BSS from priority group 0
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: 0: be:9b:d7:f0:a7:0e ssid='DIRECT-30-16C8C7' wpa_ie_len=0 rsn_ie_len=20 caps=0x1031 level=-28 freq=5180  wps p2p
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0:    selected based on RSN IE
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0:    selected BSS be:9b:d7:f0:a7:0e ssid='DIRECT-30-16C8C7'
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: Considering connect request: reassociate: 1  selected: be:9b:d7:f0:a7:0e  bssid: 00:00:00:00:00:00  pending: 00:00:00:00:00:00  wpa_state: DISCONNECTED  ssid=0xb400007517a446e0  current_ssid=0xb400007517a446e0
09-08 03:26:19.569 D/wpa_supplicant( 2489): p2p-p2p0-0: Request association with be:9b:d7:f0:a7:0e
Test: wifi picker - preference - wifi direct, negotation still works

Change-Id: I73b2ab21bf5dcb6f21c6573139494ff3aefdcdc4
diff --git a/wpa_supplicant/aidl/p2p_iface.cpp b/wpa_supplicant/aidl/p2p_iface.cpp
index 105fb8e..d2276eb 100644
--- a/wpa_supplicant/aidl/p2p_iface.cpp
+++ b/wpa_supplicant/aidl/p2p_iface.cpp
@@ -338,9 +338,7 @@
 
 void joinScanWrapper(void *eloop_ctx, void *timeout_ctx)
 {
-	struct wpa_supplicant *wpa_s = (struct wpa_supplicant *) eloop_ctx;
-
-	if (pending_join_scan_callback != NULL) {
+	if (pending_join_scan_callback) {
 		pending_join_scan_callback();
 	}
 }
@@ -1370,7 +1368,6 @@
 	if (!wpa_group_s) {
 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
 	}
-	wpa_group_s->global->p2p_go_found_external_scan = 0;
 	if (wpas_p2p_group_remove(wpa_group_s, group_ifname.c_str())) {
 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
 	}
@@ -1966,10 +1963,8 @@
 		struct wpa_bss *bss = findBssBySsid(
 			wpa_s, peer_address.data(), ssid.data(), ssid.size());
 		if (bss) {
-			wpa_s->global->p2p_go_found_external_scan = 1;
 			if (0 != joinGroup(wpa_s, bss->bssid, ssid, passphrase)) {
 				wpa_printf(MSG_ERROR, "P2P: Failed to join a group.");
-				wpa_s->global->p2p_go_found_external_scan = 0;
 			}
 			// no need to notify group join failure here,
 			// it will be handled by wpas_p2p_group_add_persistent
diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c
index 2e7cc4c..a8cab8d 100644
--- a/wpa_supplicant/p2p_supplicant.c
+++ b/wpa_supplicant/p2p_supplicant.c
@@ -6950,6 +6950,8 @@
 				 int freq, int force_scan)
 {
 	struct wpa_ssid *ssid;
+	int other_iface_found = 0;
+	struct wpa_supplicant *ifs;
 
 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
 	if (wpa_s == NULL)
@@ -6994,6 +6996,26 @@
 	wpa_s->p2p_go_group_formation_completed = 0;
 	wpa_s->global->p2p_group_formation = wpa_s;
 
+	/*
+	 * Get latest scan results from driver in case cached scan results from
+	 * interfaces on the same wiphy allow us to skip the next scan by fast
+	 * associating. Also update the scan time to the most recent scan result
+	 * fetch time on the same radio so it reflects the actual time the last
+	 * scan result event occurred.
+	 */
+	wpa_supplicant_update_scan_results(wpa_s);
+	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
+			 radio_list) {
+		if (ifs == wpa_s)
+			continue;
+		if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
+							    &ifs->last_scan)) {
+			other_iface_found = 1;
+			wpa_s->last_scan.sec = ifs->last_scan.sec;
+			wpa_s->last_scan.usec = ifs->last_scan.usec;
+		}
+	}
+
 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
 			     NULL);
 	eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
diff --git a/wpa_supplicant/scan.c b/wpa_supplicant/scan.c
index 0e0937e..f9e927a 100644
--- a/wpa_supplicant/scan.c
+++ b/wpa_supplicant/scan.c
@@ -1036,19 +1036,6 @@
 	}
 
 #ifdef CONFIG_P2P
-#ifdef ANDROID
-	if (wpa_s->global->p2p_go_found_external_scan &&
-	    (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) &&
-	    (wpa_s->global->p2p_group_formation == wpa_s)) {
-		wpa_dbg(wpa_s, MSG_DEBUG,
-			"Try to fast associate since GO is found in external scan");
-		wpa_s->global->p2p_go_found_external_scan = 0;
-		if (wpa_supplicant_fast_associate(wpa_s) >= 0) {
-			return;
-		}
-	}
-#endif
-
 	if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
 	    wpa_s->go_params && !wpa_s->conf->passive_scan) {
 		wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
diff --git a/wpa_supplicant/wpa_supplicant_i.h b/wpa_supplicant/wpa_supplicant_i.h
index 9daf908..231c7ab 100644
--- a/wpa_supplicant/wpa_supplicant_i.h
+++ b/wpa_supplicant/wpa_supplicant_i.h
@@ -311,7 +311,6 @@
 	unsigned int p2p_24ghz_social_channels:1;
 	unsigned int pending_p2ps_group:1;
 	unsigned int pending_group_iface_for_p2ps:1;
-	unsigned int p2p_go_found_external_scan:1;
 	unsigned int pending_p2ps_group_freq;
 
 #ifdef CONFIG_WIFI_DISPLAY