wpa_supplicant(hidl): Use hidl_return_util in |Supplicant|

Use the new helper functions in all |ISupplicant| methods.

Bug: 32869248
Test: Compiles
Change-Id: I6b2d59518311824ac9884e39f07e3efc57980d67
diff --git a/wpa_supplicant/hidl/supplicant.cpp b/wpa_supplicant/hidl/supplicant.cpp
index f32ceb9..e903221 100644
--- a/wpa_supplicant/hidl/supplicant.cpp
+++ b/wpa_supplicant/hidl/supplicant.cpp
@@ -8,7 +8,7 @@
  */
 
 #include "hidl_manager.h"
-#include "hidl_return_macros.h"
+#include "hidl_return_util.h"
 #include "supplicant.h"
 
 namespace android {
@@ -17,6 +17,7 @@
 namespace supplicant {
 namespace V1_0 {
 namespace implementation {
+using hidl_return_util::validateAndCall;
 
 // These are hardcoded for android.
 const char Supplicant::kDriverName[] = "nl80211";
@@ -24,93 +25,134 @@
     "/data/misc/wifi/wpa_supplicant.conf";
 
 Supplicant::Supplicant(struct wpa_global* global) : wpa_global_(global) {}
+bool Supplicant::isValid()
+{
+	// This top level object cannot be invalidated.
+	return true;
+}
+
 Return<void> Supplicant::getInterface(
     const IfaceInfo& iface_info, getInterface_cb _hidl_cb)
 {
-	struct wpa_supplicant* wpa_s =
-	    wpa_supplicant_get_iface(wpa_global_, iface_info.name.c_str());
-	if (!wpa_s) {
-		HIDL_RETURN(
-		    SupplicantStatusCode::FAILURE_IFACE_UNKNOWN, nullptr);
-	}
-
-	HidlManager* hidl_manager = HidlManager::getInstance();
-	if (iface_info.type == IfaceType::P2P) {
-		android::sp<ISupplicantP2pIface> iface;
-		if (!hidl_manager ||
-		    hidl_manager->getP2pIfaceHidlObjectByIfname(
-			wpa_s->ifname, &iface)) {
-			HIDL_RETURN(
-			    SupplicantStatusCode::FAILURE_UNKNOWN, iface);
-		}
-
-		HIDL_RETURN(SupplicantStatusCode::SUCCESS, iface);
-	} else {
-		android::sp<ISupplicantStaIface> iface;
-		if (!hidl_manager ||
-		    hidl_manager->getStaIfaceHidlObjectByIfname(
-			wpa_s->ifname, &iface)) {
-			HIDL_RETURN(
-			    SupplicantStatusCode::FAILURE_UNKNOWN, iface);
-		}
-
-		HIDL_RETURN(SupplicantStatusCode::SUCCESS, iface);
-	}
+	return validateAndCall(
+	    this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+	    &Supplicant::getInterfaceInternal, _hidl_cb, iface_info);
 }
 
 Return<void> Supplicant::listInterfaces(listInterfaces_cb _hidl_cb)
 {
-	std::vector<ISupplicant::IfaceInfo> ifaces;
-	for (struct wpa_supplicant* wpa_s = wpa_global_->ifaces; wpa_s;
-	     wpa_s = wpa_s->next) {
-		if (wpa_s->global->p2p_init_wpa_s == wpa_s) {
-			ifaces.emplace_back({IfaceType::P2P, wpa_s->ifname});
-		} else {
-			ifaces.emplace_back({IfaceType::STA, wpa_s->ifname});
-		}
-	}
-
-	HIDL_RETURN(SupplicantStatusCode::SUCCESS, ifaces);
+	return validateAndCall(
+	    this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+	    &Supplicant::listInterfacesInternal, _hidl_cb);
 }
 
 Return<void> Supplicant::registerCallback(
     const sp<ISupplicantCallback>& callback, registerCallback_cb _hidl_cb)
 {
-	HidlManager* hidl_manager = HidlManager::getInstance();
-	if (!hidl_manager ||
-	    hidl_manager->addSupplicantCallbackHidlObject(callback)) {
-		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN);
-	}
-
-	HIDL_RETURN(SupplicantStatusCode::SUCCESS);
+	return validateAndCall(
+	    this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+	    &Supplicant::registerCallbackInternal, _hidl_cb, callback);
 }
 
 Return<void> Supplicant::setDebugParams(
     ISupplicant::DebugLevel level, bool show_timestamp, bool show_keys,
     setDebugParams_cb _hidl_cb)
 {
-	if (wpa_supplicant_set_debug_params(
-		wpa_global_, static_cast<uint32_t>(level), show_timestamp,
-		show_keys)) {
-		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN);
-	}
-
-	HIDL_RETURN(SupplicantStatusCode::SUCCESS);
+	return validateAndCall(
+	    this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
+	    &Supplicant::setDebugParamsInternal, _hidl_cb, level,
+	    show_timestamp, show_keys);
 }
 
 Return<ISupplicant::DebugLevel> Supplicant::getDebugLevel()
 {
+	// TODO: Add SupplicantStatus in this method return for uniformity with
+	// the other methods in supplicant HIDL interface.
 	return (ISupplicant::DebugLevel)wpa_debug_level;
 }
 
 Return<bool> Supplicant::isDebugShowTimestampEnabled()
 {
-	return (wpa_debug_timestamp ? true : false);
+	// TODO: Add SupplicantStatus in this method return for uniformity with
+	// the other methods in supplicant HIDL interface.
+	return ((wpa_debug_timestamp != 0) ? true : false);
 }
 
 Return<bool> Supplicant::isDebugShowKeysEnabled()
 {
-	return (wpa_debug_show_keys ? true : false);
+	// TODO: Add SupplicantStatus in this method return for uniformity with
+	// the other methods in supplicant HIDL interface.
+	return ((wpa_debug_show_keys != 0) ? true : false);
+}
+
+std::pair<SupplicantStatus, sp<ISupplicantIface>>
+Supplicant::getInterfaceInternal(const IfaceInfo& iface_info)
+{
+	struct wpa_supplicant* wpa_s =
+	    wpa_supplicant_get_iface(wpa_global_, iface_info.name.c_str());
+	if (!wpa_s) {
+		return {{SupplicantStatusCode::FAILURE_IFACE_UNKNOWN, ""},
+			nullptr};
+	}
+	HidlManager* hidl_manager = HidlManager::getInstance();
+	if (iface_info.type == IfaceType::P2P) {
+		android::sp<ISupplicantP2pIface> iface;
+		if (!hidl_manager ||
+		    hidl_manager->getP2pIfaceHidlObjectByIfname(
+			wpa_s->ifname, &iface)) {
+			return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""},
+				iface};
+		}
+		return {{SupplicantStatusCode::SUCCESS, ""}, iface};
+	} else {
+		android::sp<ISupplicantStaIface> iface;
+		if (!hidl_manager ||
+		    hidl_manager->getStaIfaceHidlObjectByIfname(
+			wpa_s->ifname, &iface)) {
+			return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""},
+				iface};
+		}
+		return {{SupplicantStatusCode::SUCCESS, ""}, iface};
+	}
+}
+
+std::pair<SupplicantStatus, std::vector<ISupplicant::IfaceInfo>>
+Supplicant::listInterfacesInternal()
+{
+	std::vector<ISupplicant::IfaceInfo> ifaces;
+	for (struct wpa_supplicant* wpa_s = wpa_global_->ifaces; wpa_s;
+	     wpa_s = wpa_s->next) {
+		if (wpa_s->global->p2p_init_wpa_s == wpa_s) {
+			ifaces.emplace_back(ISupplicant::IfaceInfo{
+			    IfaceType::P2P, wpa_s->ifname});
+		} else {
+			ifaces.emplace_back(ISupplicant::IfaceInfo{
+			    IfaceType::STA, wpa_s->ifname});
+		}
+	}
+	return {{SupplicantStatusCode::SUCCESS, ""}, std::move(ifaces)};
+}
+
+SupplicantStatus Supplicant::registerCallbackInternal(
+    const sp<ISupplicantCallback>& callback)
+{
+	HidlManager* hidl_manager = HidlManager::getInstance();
+	if (!hidl_manager ||
+	    hidl_manager->addSupplicantCallbackHidlObject(callback)) {
+		return {SupplicantStatusCode::FAILURE_UNKNOWN, ""};
+	}
+	return {SupplicantStatusCode::SUCCESS, ""};
+}
+
+SupplicantStatus Supplicant::setDebugParamsInternal(
+    ISupplicant::DebugLevel level, bool show_timestamp, bool show_keys)
+{
+	if (wpa_supplicant_set_debug_params(
+		wpa_global_, static_cast<uint32_t>(level), show_timestamp,
+		show_keys)) {
+		return {SupplicantStatusCode::FAILURE_UNKNOWN, ""};
+	}
+	return {SupplicantStatusCode::SUCCESS, ""};
 }
 }  // namespace implementation
 }  // namespace V1_0
diff --git a/wpa_supplicant/hidl/supplicant.h b/wpa_supplicant/hidl/supplicant.h
index f4a77d9..673b9f5 100644
--- a/wpa_supplicant/hidl/supplicant.h
+++ b/wpa_supplicant/hidl/supplicant.h
@@ -39,6 +39,7 @@
 public:
 	Supplicant(struct wpa_global* global);
 	~Supplicant() override = default;
+	bool isValid();
 
 	// Hidl methods exposed.
 	Return<void> getInterface(
@@ -55,6 +56,16 @@
 	Return<bool> isDebugShowKeysEnabled() override;
 
 private:
+	// Corresponding worker functions for the HIDL methods.
+	std::pair<SupplicantStatus, sp<ISupplicantIface>> getInterfaceInternal(
+	    const IfaceInfo& iface_info);
+	std::pair<SupplicantStatus, std::vector<ISupplicant::IfaceInfo>>
+	listInterfacesInternal();
+	SupplicantStatus registerCallbackInternal(
+	    const sp<ISupplicantCallback>& callback);
+	SupplicantStatus setDebugParamsInternal(
+	    ISupplicant::DebugLevel level, bool show_timestamp, bool show_keys);
+
 	// Raw pointer to the global structure maintained by the core.
 	struct wpa_global* wpa_global_;
 	// Driver name to be used for creating interfaces.