Support for Wi-Fi Direct R2

This CL includes the stub functions for,
1. API support for USD based P2P service discovery
2. API support for Wi-Fi Direct R2 pairing protocol

Bug: 341971059
Test: Manual - Basic P2P connect/disconnect tests
Test: TH Presubmit tests
Test: atest VtsHalWifiSupplicantStaIfaceTargetTest \
      VtsHalWifiSupplicantStaNetworkTargetTest \
      VtsHalWifiSupplicantP2pIfaceTargetTest

Change-Id: If708e06af2327bdbad154908fc6432a37576fbbc
diff --git a/hostapd/aidl/hostapd.cpp b/hostapd/aidl/hostapd.cpp
index 9c485e7..ce32564 100644
--- a/hostapd/aidl/hostapd.cpp
+++ b/hostapd/aidl/hostapd.cpp
@@ -79,6 +79,12 @@
 	return expected_version <= aidl_client_version;
 }
 
+inline int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
+{
+	return isAidlServiceVersionAtLeast(expected_version)
+		&& isAidlClientVersionAtLeast(expected_version);
+}
+
 #define MAX_PORTS 1024
 bool GetInterfacesInBridge(std::string br_name,
                            std::vector<std::string>* interfaces) {
@@ -593,7 +599,7 @@
 #ifdef CONFIG_IEEE80211BE
 	if (iface_params.hwModeParams.enable80211BE && !is_60Ghz_used) {
 		eht_params_as_string = "ieee80211be=1\n";
-		if (isAidlServiceVersionAtLeast(2) && isAidlClientVersionAtLeast(2)) {
+		if (areAidlServiceAndClientAtLeastVersion(2)) {
 			std::string interface_mac_addr = getInterfaceMacAddress(
 					iface_params.usesMlo ? br_name : iface_params.name);
 			if (interface_mac_addr.empty()) {
diff --git a/wpa_supplicant/aidl/aidl_manager.cpp b/wpa_supplicant/aidl/aidl_manager.cpp
index db9ece4..177f478 100644
--- a/wpa_supplicant/aidl/aidl_manager.cpp
+++ b/wpa_supplicant/aidl/aidl_manager.cpp
@@ -425,6 +425,12 @@
 	return expected_version <= aidl_client_version;
 }
 
+int32_t AidlManager::areAidlServiceAndClientAtLeastVersion(int32_t expected_version)
+{
+	return isAidlServiceVersionAtLeast(expected_version)
+		&& isAidlClientVersionAtLeast(expected_version);
+}
+
 int AidlManager::registerAidlService(struct wpa_global *global)
 {
 	// Create the main aidl service object and register it.
@@ -1356,7 +1362,7 @@
 			std::back_inserter(aidl_vendor_elems));
 	}
 
-	if (isAidlServiceVersionAtLeast(3) && isAidlClientVersionAtLeast(3)) {
+	if (areAidlServiceAndClientAtLeastVersion(3)) {
 		P2pDeviceFoundEventParams params;
 		params.srcAddress = macAddrToArray(addr);
 		params.p2pDeviceAddress = macAddrToArray(info->p2p_device_addr);
@@ -1368,6 +1374,10 @@
 		params.wfdDeviceInfo = aidl_peer_wfd_device_info;
 		params.wfdR2DeviceInfo = aidl_peer_wfd_r2_device_info;
 		params.vendorElemBytes = aidl_vendor_elems;
+		if (areAidlServiceAndClientAtLeastVersion(4)) {
+			// TODO Fill the field when supplicant implementation is ready
+			params.pairingBootstrappingMethods = 0;
+		}
 		callWithEachP2pIfaceCallback(
 			misc_utils::charBufToString(wpa_s->ifname),
 			std::bind(
@@ -1529,7 +1539,7 @@
 			   params.p2pClientIpInfo.ipAddressMask,
 			   params.p2pClientIpInfo.ipAddressGo);
         }
-	if (isAidlServiceVersionAtLeast(4)) {
+	if (areAidlServiceAndClientAtLeastVersion(4)) {
 		// TODO Fill the field when supplicant implementation is ready
 		params.keyMgmtMask = 0;
 	}
@@ -1623,7 +1633,7 @@
 	}
 	bool aidl_is_request = (request == 1);
 
-	if (isAidlServiceVersionAtLeast(3) && isAidlClientVersionAtLeast(3)) {
+	if (areAidlServiceAndClientAtLeastVersion(3)) {
 		P2pProvisionDiscoveryCompletedEventParams params;
 		params.p2pDeviceAddress =  macAddrToArray(dev_addr);
 		params.isRequest = aidl_is_request;
@@ -1669,6 +1679,75 @@
 		byteArrToVec(tlvs, tlvs_len)));
 }
 
+void AidlManager::notifyUsdBasedServiceDiscoveryResult(
+	struct wpa_supplicant *wpa_s, const u8 *peer_addr, int subscribe_id,
+	int peer_publish_id, int srv_proto_type, const u8 *ssi, size_t ssi_len)
+{
+	// TODO define the reason and map to AIDL defenition.
+	if (!wpa_s)
+		return;
+
+	if (p2p_iface_object_map_.find(wpa_s->ifname) ==
+		p2p_iface_object_map_.end())
+		return;
+
+	if (!areAidlServiceAndClientAtLeastVersion(4)) {
+	      return;
+	}
+	// TODO Fill the fields when supplicant implementation is ready
+	P2pUsdBasedServiceDiscoveryResultParams params;
+
+	callWithEachP2pIfaceCallback(
+		misc_utils::charBufToString(wpa_s->ifname),
+		std::bind(
+		&ISupplicantP2pIfaceCallback::onUsdBasedServiceDiscoveryResult,
+		std::placeholders::_1, params));
+}
+
+void AidlManager::notifyUsdBasedServiceDiscoveryTerminated(
+	struct wpa_supplicant *wpa_s, int subscribe_id, int reason)
+{
+	// TODO define the reason and map to AIDL defenition.
+	if (!wpa_s)
+		return;
+
+	if (p2p_iface_object_map_.find(wpa_s->ifname) ==
+		p2p_iface_object_map_.end())
+		return;
+
+	if (!areAidlServiceAndClientAtLeastVersion(4)) {
+	      return;
+	}
+
+	callWithEachP2pIfaceCallback(
+		misc_utils::charBufToString(wpa_s->ifname),
+		std::bind(
+		&ISupplicantP2pIfaceCallback::onUsdBasedServiceDiscoveryTerminated,
+		std::placeholders::_1, subscribe_id, UsdTerminateReasonCode::UNKNOWN));
+}
+
+void AidlManager::notifyUsdBasedServiceAdvertisementTerminated(
+	struct wpa_supplicant *wpa_s, int publish_id, int reason)
+{
+	// TODO define the reason and map to AIDL defenition.
+	if (!wpa_s)
+		return;
+
+	if (p2p_iface_object_map_.find(wpa_s->ifname) ==
+		p2p_iface_object_map_.end())
+		return;
+
+	if (!areAidlServiceAndClientAtLeastVersion(4)) {
+	      return;
+	}
+
+	callWithEachP2pIfaceCallback(
+		misc_utils::charBufToString(wpa_s->ifname),
+		std::bind(
+		&ISupplicantP2pIfaceCallback::onUsdBasedServiceAdvertisementTerminated,
+		std::placeholders::_1, publish_id, UsdTerminateReasonCode::UNKNOWN));
+}
+
 void AidlManager::notifyApStaAuthorized(
 	struct wpa_supplicant *wpa_group_s, const u8 *sta, const u8 *p2p_dev_addr,
 	const u8 *ip)
@@ -1679,7 +1758,7 @@
 	if (!wpa_s)
 		return;
 
-	if (isAidlServiceVersionAtLeast(3) && isAidlClientVersionAtLeast(3)) {
+	if (areAidlServiceAndClientAtLeastVersion(3)) {
 		P2pPeerClientJoinedEventParams params;
 		params.groupInterfaceName = misc_utils::charBufToString(wpa_group_s->ifname);
 		params.clientInterfaceAddress = macAddrToArray(sta);
@@ -1690,7 +1769,7 @@
 			os_memcpy(&aidl_ip, &ip[0], 4);
 		}
 		params.clientIpAddress = aidl_ip;
-		if (isAidlServiceVersionAtLeast(4)) {
+		if (areAidlServiceAndClientAtLeastVersion(4)) {
 			// TODO Fill the field when supplicant implementation is ready
 			params.keyMgmtMask = 0;
 		}
@@ -1719,7 +1798,7 @@
 	if (!wpa_s)
 		return;
 
-	if (isAidlServiceVersionAtLeast(3) && isAidlClientVersionAtLeast(3)) {
+	if (areAidlServiceAndClientAtLeastVersion(3)) {
 		P2pPeerClientDisconnectedEventParams params;
 		params.groupInterfaceName = misc_utils::charBufToString(wpa_group_s->ifname);
 		params.clientInterfaceAddress = macAddrToArray(sta);
diff --git a/wpa_supplicant/aidl/aidl_manager.h b/wpa_supplicant/aidl/aidl_manager.h
index b3dfa82..46a40aa 100644
--- a/wpa_supplicant/aidl/aidl_manager.h
+++ b/wpa_supplicant/aidl/aidl_manager.h
@@ -120,6 +120,15 @@
 	void notifyP2pSdResponse(
 		struct wpa_supplicant *wpa_s, const u8 *sa, u16 update_indic,
 		const u8 *tlvs, size_t tlvs_len);
+	void notifyUsdBasedServiceDiscoveryResult(
+		struct wpa_supplicant *wpa_s, const u8 *peer_addr, int subscribe_id,
+		int peer_publish_id, int srv_proto_type, const u8 *ssi, size_t ssi_len);
+	void notifyUsdBasedServiceDiscoveryTerminated(
+		struct wpa_supplicant *wpa_s, int subscribe_id,
+		int reason);
+	void notifyUsdBasedServiceAdvertisementTerminated(
+		struct wpa_supplicant *wpa_s, int publish_id,
+		int reason);
 	void notifyApStaAuthorized(
 		struct wpa_supplicant *wpa_s, const u8 *sta,
 		const u8 *p2p_dev_addr, const u8 *ip);
@@ -173,6 +182,7 @@
 	// Methods called from aidl objects.
 	int32_t isAidlServiceVersionAtLeast(int32_t expected_version);
 	int32_t isAidlClientVersionAtLeast(int32_t expected_version);
+	int32_t areAidlServiceAndClientAtLeastVersion(int32_t expected_version);
 	void notifyExtRadioWorkStart(struct wpa_supplicant *wpa_s, uint32_t id);
 	void notifyExtRadioWorkTimeout(
 		struct wpa_supplicant *wpa_s, uint32_t id);
diff --git a/wpa_supplicant/aidl/p2p_iface.cpp b/wpa_supplicant/aidl/p2p_iface.cpp
index e6610de..81bf3f1 100644
--- a/wpa_supplicant/aidl/p2p_iface.cpp
+++ b/wpa_supplicant/aidl/p2p_iface.cpp
@@ -29,6 +29,7 @@
 const char kConfigMethodStrPbc[] = "pbc";
 const char kConfigMethodStrDisplay[] = "display";
 const char kConfigMethodStrKeypad[] = "keypad";
+const char kConfigMethodStrNone[] = "none";
 constexpr char kSetMiracastMode[] = "MIRACAST ";
 constexpr uint8_t kWfdDeviceInfoSubelemId = 0;
 constexpr uint8_t kWfdR2DeviceInfoSubelemId = 11;
@@ -861,6 +862,48 @@
 		&P2pIface::getFeatureSetInternal, _aidl_return);
 }
 
+::ndk::ScopedAStatus P2pIface::startUsdBasedServiceDiscovery(
+		const P2pUsdBasedServiceDiscoveryConfig& in_serviceDiscoveryConfig,
+		int32_t* _aidl_return)
+{
+	return validateAndCall(
+		this, SupplicantStatusCode::FAILURE_UNKNOWN,
+		&P2pIface::startUsdBasedServiceDiscoveryInternal, _aidl_return,
+		in_serviceDiscoveryConfig);
+}
+
+::ndk::ScopedAStatus P2pIface::stopUsdBasedServiceDiscovery(int32_t in_sessionId)
+{
+	return validateAndCall(
+		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+		&P2pIface::stopUsdBasedServiceDiscoveryInternal, in_sessionId);
+}
+
+::ndk::ScopedAStatus P2pIface::startUsdBasedServiceAdvertisement(
+		const P2pUsdBasedServiceAdvertisementConfig& in_serviceAdvertisementConfig,
+		int32_t* _aidl_return)
+{
+	return validateAndCall(
+		this, SupplicantStatusCode::FAILURE_UNKNOWN,
+		&P2pIface::startUsdBasedServiceAdvertisementInternal, _aidl_return,
+		in_serviceAdvertisementConfig);
+}
+
+::ndk::ScopedAStatus P2pIface::stopUsdBasedServiceAdvertisement(int32_t in_sessionId)
+{
+	return validateAndCall(
+		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+		&P2pIface::stopUsdBasedServiceAdvertisementInternal, in_sessionId);
+}
+
+::ndk::ScopedAStatus P2pIface::provisionDiscoveryWithParams(
+		const P2pProvisionDiscoveryParams& in_params)
+{
+	return validateAndCall(
+		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+		&P2pIface::provisionDiscoveryWithParamsInternal, in_params);
+}
+
 std::pair<std::string, ndk::ScopedAStatus> P2pIface::getNameInternal()
 {
 	return {ifname_, ndk::ScopedAStatus::ok()};
@@ -1059,6 +1102,9 @@
 	case WpsProvisionMethod::KEYPAD:
 		wps_method = WPS_PIN_KEYPAD;
 		break;
+	case WpsProvisionMethod::NONE:
+		wps_method = WPS_NOT_READY;
+		break;
 	}
 	int he = wpa_s->conf->p2p_go_he;
 	int vht = wpa_s->conf->p2p_go_vht;
@@ -1116,6 +1162,10 @@
 	case WpsProvisionMethod::KEYPAD:
 		config_method_str = kConfigMethodStrKeypad;
 		break;
+	// TODO Handle pairing bootstrapping method when supplicant implementation is ready
+	case WpsProvisionMethod::NONE:
+		config_method_str = kConfigMethodStrNone;
+		break;
 	}
 	if (wpas_p2p_prov_disc(
 		wpa_s, peer_address.data(), config_method_str,
@@ -1958,6 +2008,43 @@
 	return {0, ndk::ScopedAStatus::ok()};
 }
 
+std::pair<uint32_t, ndk::ScopedAStatus>
+P2pIface::startUsdBasedServiceDiscoveryInternal(
+	const P2pUsdBasedServiceDiscoveryConfig& serviceDiscoveryConfig)
+{
+	// TODO Fill the field when supplicant implementation is ready
+	return {0, ndk::ScopedAStatus::ok()};
+}
+
+ndk::ScopedAStatus P2pIface::stopUsdBasedServiceDiscoveryInternal(
+	uint32_t sessionId)
+{
+	// TODO Fill the field when supplicant implementation is ready
+	return ndk::ScopedAStatus::ok();
+}
+
+std::pair<uint32_t, ndk::ScopedAStatus>
+P2pIface::startUsdBasedServiceAdvertisementInternal(
+	const P2pUsdBasedServiceAdvertisementConfig& serviceAdvertisementConfig)
+{
+	// TODO Fill the field when supplicant implementation is ready
+	return {0, ndk::ScopedAStatus::ok()};
+}
+
+ndk::ScopedAStatus P2pIface::stopUsdBasedServiceAdvertisementInternal(
+	uint32_t sessionId)
+{
+	// TODO Fill the field when supplicant implementation is ready
+	return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus P2pIface::provisionDiscoveryWithParamsInternal(
+	const P2pProvisionDiscoveryParams& params)
+{
+	// TODO Fill the field when supplicant implementation is ready
+	return ndk::ScopedAStatus::ok();
+}
+
 /**
  * Retrieve the underlying |wpa_supplicant| struct
  * pointer for this iface.
diff --git a/wpa_supplicant/aidl/p2p_iface.h b/wpa_supplicant/aidl/p2p_iface.h
index 4f7ae0a..840878e 100644
--- a/wpa_supplicant/aidl/p2p_iface.h
+++ b/wpa_supplicant/aidl/p2p_iface.h
@@ -185,6 +185,16 @@
 	::ndk::ScopedAStatus createGroupOwner(
 		const P2pCreateGroupOwnerInfo& in_groupOwnerInfo) override;
 	::ndk::ScopedAStatus getFeatureSet(int64_t* _aidl_return) override;
+	::ndk::ScopedAStatus startUsdBasedServiceDiscovery(
+		const P2pUsdBasedServiceDiscoveryConfig& in_serviceDiscoveryConfig,
+		int32_t* _aidl_return) override;
+	::ndk::ScopedAStatus stopUsdBasedServiceDiscovery(int32_t in_sessionId) override;
+	::ndk::ScopedAStatus startUsdBasedServiceAdvertisement(
+		const P2pUsdBasedServiceAdvertisementConfig& in_serviceAdvertisementConfig,
+		int32_t* _aidl_return) override;
+	::ndk::ScopedAStatus stopUsdBasedServiceAdvertisement(int32_t in_sessionId) override;
+	::ndk::ScopedAStatus provisionDiscoveryWithParams(
+		const P2pProvisionDiscoveryParams& in_params) override;
 
 
 private:
@@ -318,6 +328,14 @@
 	ndk::ScopedAStatus createGroupOwnerInternal(
 		const P2pCreateGroupOwnerInfo& groupOwnerInfo);
 	std::pair<int64_t, ndk::ScopedAStatus> getFeatureSetInternal();
+	std::pair<uint32_t, ndk::ScopedAStatus> startUsdBasedServiceDiscoveryInternal(
+		const P2pUsdBasedServiceDiscoveryConfig& serviceDiscoveryConfig);
+	::ndk::ScopedAStatus stopUsdBasedServiceDiscoveryInternal(uint32_t sessionId);
+	std::pair<uint32_t, ndk::ScopedAStatus> startUsdBasedServiceAdvertisementInternal(
+		const P2pUsdBasedServiceAdvertisementConfig& serviceAdvertisementConfig);
+	::ndk::ScopedAStatus stopUsdBasedServiceAdvertisementInternal(uint32_t sessionId);
+	::ndk::ScopedAStatus provisionDiscoveryWithParamsInternal(
+		const P2pProvisionDiscoveryParams& params);
 
 	struct wpa_supplicant* retrieveIfacePtr();
 	struct wpa_supplicant* retrieveGroupIfacePtr(