wpa_supplicant: HIDL implementation (2/2)

Migrated the AIDL interface to HIDL.

Summary of the changes:
1. Changed method signatures to always invoke a callback with the
status of the operation and possibly a return value.
2. Use hidl_vec, hidl_string, hidl_array in place of
std::vector, std::string, normal array,  for all HIDL method params.
3. Removed all parcelables and instead use the structs declared in the
HIDL interface.
4. Moved all constants to enums and use static_cast to retrieve their
underlying values.
5. Changed the input params for |ISupplicant.createInterface|. Only the
interface name is passed by the caller, the rest of the params are
hardcoded in the implementation of the HIDL interface.
6. Changed any remaining comments from "C" style to "C++" style.
7. Move the implementation to
android::hardware::wifi:supplicant::V1_0::implementation namespace.

TODO's:
1. Need to handle the |notifyNetworkRequest|. This needs some
additional string parsing logic which will be handled in a future CL.
2. Death notifications are not yet added in the HIDL interface. So, that
part of the code is under "#if 0".
3. The |hidl_vec| implementation does not support an |assign| method
yet, so marked places where we need this for cleanup later.

Bug: 31365276
Test: mmm -j32 external/wpa_supplicant_8
Test: Will test on device using go/aog/279621 once the HIDL changes are
in AOSP.

Change-Id: Iff20fb3c6fc23b0122c0c07529171e3a473b1d63
diff --git a/wpa_supplicant/hidl/supplicant.cpp b/wpa_supplicant/hidl/supplicant.cpp
index 3ff514e..b9f6ce2 100644
--- a/wpa_supplicant/hidl/supplicant.cpp
+++ b/wpa_supplicant/hidl/supplicant.cpp
@@ -7,252 +7,149 @@
  * See README for more details.
  */
 
-#include "supplicant.h"
 #include "hidl_manager.h"
-#include "../src/utils/wpa_debug.h"
+#include "hidl_return_macros.h"
+#include "supplicant.h"
 
-namespace wpa_supplicant_hidl {
+namespace android {
+namespace hardware {
+namespace wifi {
+namespace supplicant {
+namespace V1_0 {
+namespace implementation {
 
-Supplicant::Supplicant(struct wpa_global *global) : wpa_global_(global) {}
-android::hidl::Status Supplicant::CreateInterface(
-    const fi::w1::wpa_supplicant::ParcelableIfaceParams &params,
-    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
+// These are hardcoded for android.
+const char Supplicant::kDriverName[] = "nl80211";
+const char Supplicant::kConfigFilePath[] =
+    "/data/misc/wifi/wpa_supplicant.conf";
+
+Supplicant::Supplicant(struct wpa_global* global) : wpa_global_(global) {}
+Return<void> Supplicant::createInterface(
+    const hidl_string& ifname, createInterface_cb _hidl_cb)
 {
-	/* Check if required Ifname argument is missing */
-	if (params.ifname_.isEmpty()) {
-		return android::hidl::Status::fromExceptionCode(
-		    android::hidl::Status::EX_ILLEGAL_ARGUMENT,
-		    "Ifname missing in params.");
+	android::sp<ISupplicantIface> iface;
+
+	// Check if required |ifname| argument is empty.
+	if (ifname.size() == 0) {
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_ARGS_INVALID, iface);
 	}
-	/*
-	 * Try to get the wpa_supplicant record for this iface, return
-	 * an error if we already control it.
-	 */
-	if (wpa_supplicant_get_iface(wpa_global_, params.ifname_.string()) !=
-	    NULL) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_IFACE_EXISTS,
-		    "wpa_supplicant already controls this interface.");
+	// Try to get the wpa_supplicant record for this iface, return
+	// an error if we already control it.
+	if (wpa_supplicant_get_iface(wpa_global_, ifname.c_str()) != NULL) {
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_IFACE_EXISTS, iface);
 	}
 
-	android::hidl::Status status;
-	struct wpa_supplicant *wpa_s = NULL;
-	struct wpa_interface iface;
-
-	os_memset(&iface, 0, sizeof(iface));
-	iface.driver = os_strdup(params.driver_.string());
-	iface.ifname = os_strdup(params.ifname_.string());
-	iface.confname = os_strdup(params.config_file_.string());
-	iface.bridge_ifname = os_strdup(params.bridge_ifname_.string());
-	/* Otherwise, have wpa_supplicant attach to it. */
-	wpa_s = wpa_supplicant_add_iface(wpa_global_, &iface, NULL);
-	/* The supplicant core creates a corresponding hidl object via
-	 * HidlManager when |wpa_supplicant_add_iface| is called. */
+	// Otherwise, have wpa_supplicant attach to it.
+	struct wpa_supplicant* wpa_s = NULL;
+	struct wpa_interface iface_params;
+	os_memset(&iface_params, 0, sizeof(iface));
+	iface_params.ifname = ifname.c_str();
+	iface_params.confname = kConfigFilePath;
+	iface_params.driver = kDriverName;
+	wpa_s = wpa_supplicant_add_iface(wpa_global_, &iface_params, NULL);
 	if (!wpa_s) {
-		status = android::hidl::Status::fromServiceSpecificError(
-		    ERROR_GENERIC,
-		    "wpa_supplicant couldn't grab this interface.");
-	} else {
-		HidlManager *hidl_manager = HidlManager::getInstance();
-
-		if (!hidl_manager ||
-		    hidl_manager->getIfaceHidlObjectByIfname(
-			wpa_s->ifname, iface_object_out)) {
-			status =
-			    android::hidl::Status::fromServiceSpecificError(
-				ERROR_GENERIC,
-				"wpa_supplicant encountered a hidl error.");
-		} else {
-			status = android::hidl::Status::ok();
-		}
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN, iface);
 	}
-	os_free((void *)iface.driver);
-	os_free((void *)iface.ifname);
-	os_free((void *)iface.confname);
-	os_free((void *)iface.bridge_ifname);
-	return status;
+	// The supplicant core creates a corresponding hidl object via
+	// HidlManager when |wpa_supplicant_add_iface| is called.
+	HidlManager* hidl_manager = HidlManager::getInstance();
+	if (!hidl_manager ||
+	    hidl_manager->getIfaceHidlObjectByIfname(wpa_s->ifname, &iface)) {
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN, iface);
+	}
+
+	HIDL_RETURN(SupplicantStatusCode::SUCCESS, iface);
 }
 
-android::hidl::Status Supplicant::RemoveInterface(const std::string &ifname)
+Return<void> Supplicant::removeInterface(
+    const hidl_string& ifname, removeInterface_cb _hidl_cb)
 {
-	struct wpa_supplicant *wpa_s;
+	struct wpa_supplicant* wpa_s;
 
 	wpa_s = wpa_supplicant_get_iface(wpa_global_, ifname.c_str());
 	if (!wpa_s) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_IFACE_UNKNOWN,
-		    "wpa_supplicant does not control this interface.");
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
 	}
 	if (wpa_supplicant_remove_iface(wpa_global_, wpa_s, 0)) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_GENERIC,
-		    "wpa_supplicant couldn't remove this interface.");
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN);
 	}
-	return android::hidl::Status::ok();
+
+	HIDL_RETURN(SupplicantStatusCode::SUCCESS);
 }
 
-android::hidl::Status Supplicant::GetInterface(
-    const std::string &ifname,
-    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
+Return<void> Supplicant::getInterface(
+    const hidl_string& ifname, getInterface_cb _hidl_cb)
 {
-	struct wpa_supplicant *wpa_s;
+	android::sp<ISupplicantIface> iface;
 
-	wpa_s = wpa_supplicant_get_iface(wpa_global_, ifname.c_str());
+	struct wpa_supplicant* wpa_s =
+	    wpa_supplicant_get_iface(wpa_global_, ifname.c_str());
 	if (!wpa_s) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_IFACE_UNKNOWN,
-		    "wpa_supplicant does not control this interface.");
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN, iface);
 	}
 
-	HidlManager *hidl_manager = HidlManager::getInstance();
+	HidlManager* hidl_manager = HidlManager::getInstance();
 	if (!hidl_manager ||
-	    hidl_manager->getIfaceHidlObjectByIfname(
-		wpa_s->ifname, iface_object_out)) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_GENERIC,
-		    "wpa_supplicant encountered a hidl error.");
+	    hidl_manager->getIfaceHidlObjectByIfname(wpa_s->ifname, &iface)) {
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN, iface);
 	}
 
-	return android::hidl::Status::ok();
+	HIDL_RETURN(SupplicantStatusCode::SUCCESS, iface);
 }
 
-android::hidl::Status Supplicant::SetDebugParams(
-    int level, bool show_timestamp, bool show_keys)
+Return<void> Supplicant::listInterfaces(listInterfaces_cb _hidl_cb)
 {
-	int internal_level;
-	if (convertDebugLevelToInternalLevel(level, &internal_level)) {
-		const std::string error_msg =
-		    "invalid debug level: " + std::to_string(level);
-		return android::hidl::Status::fromExceptionCode(
-		    android::hidl::Status::EX_ILLEGAL_ARGUMENT,
-		    error_msg.c_str());
+	std::vector<hidl_string> ifnames;
+	for (struct wpa_supplicant* wpa_s = wpa_global_->ifaces; wpa_s;
+	     wpa_s = wpa_s->next) {
+		ifnames.emplace_back(wpa_s->ifname);
 	}
-	if (wpa_supplicant_set_debug_params(
-<<<<<<< 390ba2881ef621db480848b7e50b93d61542206a:wpa_supplicant/binder/supplicant.cpp
-		wpa_global_, internal_level, show_timestamp, show_keys)) {
-		return android::binder::Status::fromServiceSpecificError(
-=======
-		wpa_global_, level, show_timestamp, show_keys)) {
-		return android::hidl::Status::fromServiceSpecificError(
->>>>>>> wpa_supplicant: HIDL implementation (1/2):wpa_supplicant/hidl/supplicant.cpp
-		    ERROR_GENERIC,
-		    "wpa_supplicant could not set debug params.");
-	}
-	return android::hidl::Status::ok();
+
+	HIDL_RETURN(SupplicantStatusCode::SUCCESS, ifnames);
 }
 
-android::hidl::Status Supplicant::GetDebugLevel(int *level_out)
+Return<void> Supplicant::registerCallback(
+    const sp<ISupplicantCallback>& callback, registerCallback_cb _hidl_cb)
 {
-<<<<<<< 390ba2881ef621db480848b7e50b93d61542206a:wpa_supplicant/binder/supplicant.cpp
-	if (convertDebugLevelToExternalLevel(wpa_debug_level, level_out)) {
-		const std::string error_msg =
-		    "invalid debug level: " + std::to_string(wpa_debug_level);
-		return android::binder::Status::fromExceptionCode(
-		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
-		    error_msg.c_str());
-	}
-	return android::binder::Status::ok();
-=======
-	*level_out = wpa_debug_level;
-	return android::hidl::Status::ok();
->>>>>>> wpa_supplicant: HIDL implementation (1/2):wpa_supplicant/hidl/supplicant.cpp
-}
-
-android::hidl::Status Supplicant::GetDebugShowTimestamp(
-    bool *show_timestamp_out)
-{
-	*show_timestamp_out = wpa_debug_timestamp ? true : false;
-	return android::hidl::Status::ok();
-}
-
-android::hidl::Status Supplicant::GetDebugShowKeys(bool *show_keys_out)
-{
-	*show_keys_out = wpa_debug_show_keys ? true : false;
-	return android::hidl::Status::ok();
-}
-
-android::hidl::Status Supplicant::RegisterCallback(
-    const android::sp<fi::w1::wpa_supplicant::ISupplicantCallback> &callback)
-{
-	HidlManager *hidl_manager = HidlManager::getInstance();
+	HidlManager* hidl_manager = HidlManager::getInstance();
 	if (!hidl_manager ||
 	    hidl_manager->addSupplicantCallbackHidlObject(callback)) {
-		return android::hidl::Status::fromServiceSpecificError(
-		    ERROR_GENERIC,
-		    "wpa_supplicant encountered a hidl error.");
+		HIDL_RETURN(SupplicantStatusCode::FAILURE_UNKNOWN);
 	}
-	return android::hidl::Status::ok();
-}
-<<<<<<< 390ba2881ef621db480848b7e50b93d61542206a:wpa_supplicant/binder/supplicant.cpp
 
-/**
- * Helper function to convert the debug level parameter from the binder
- * interface values to internal values.
- */
-int Supplicant::convertDebugLevelToInternalLevel(
-    int external_level, int *internal_level)
-{
-	switch (external_level) {
-	case DEBUG_LEVEL_EXCESSIVE:
-		*internal_level = MSG_EXCESSIVE;
-		return 0;
-	case DEBUG_LEVEL_MSGDUMP:
-		*internal_level = MSG_MSGDUMP;
-		return 0;
-	case DEBUG_LEVEL_DEBUG:
-		*internal_level = MSG_DEBUG;
-		return 0;
-	case DEBUG_LEVEL_INFO:
-		*internal_level = MSG_INFO;
-		return 0;
-	case DEBUG_LEVEL_WARNING:
-		*internal_level = MSG_WARNING;
-		return 0;
-	case DEBUG_LEVEL_ERROR:
-		*internal_level = MSG_ERROR;
-		return 0;
-	default:
-		wpa_printf(
-		    MSG_ERROR, "Invalid external log level: %d",
-		    external_level);
-		return 1;
-	}
+	HIDL_RETURN(SupplicantStatusCode::SUCCESS);
 }
 
-/**
- * Helper function to convert the debug level parameter from the internal values
- * to binder interface values.
- */
-int Supplicant::convertDebugLevelToExternalLevel(
-    int internal_level, int *external_level)
+Return<void> Supplicant::setDebugParams(
+    ISupplicant::DebugLevel level, bool show_timestamp, bool show_keys,
+    setDebugParams_cb _hidl_cb)
 {
-	switch (internal_level) {
-	case MSG_EXCESSIVE:
-		*external_level = DEBUG_LEVEL_EXCESSIVE;
-		return 0;
-	case MSG_MSGDUMP:
-		*external_level = DEBUG_LEVEL_MSGDUMP;
-		return 0;
-	case MSG_DEBUG:
-		*external_level = DEBUG_LEVEL_DEBUG;
-		return 0;
-	case MSG_INFO:
-		*external_level = DEBUG_LEVEL_INFO;
-		return 0;
-	case MSG_WARNING:
-		*external_level = DEBUG_LEVEL_WARNING;
-		return 0;
-	case MSG_ERROR:
-		*external_level = DEBUG_LEVEL_ERROR;
-		return 0;
-	default:
-		wpa_printf(
-		    MSG_ERROR, "Invalid internal log level: %d",
-		    internal_level);
-		return 1;
+	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);
 }
-} /* namespace wpa_supplicant_binder */
-=======
-} /* namespace wpa_supplicant_hidl */
->>>>>>> wpa_supplicant: HIDL implementation (1/2):wpa_supplicant/hidl/supplicant.cpp
+
+Return<ISupplicant::DebugLevel> Supplicant::getDebugLevel()
+{
+	return (ISupplicant::DebugLevel)wpa_debug_level;
+}
+
+Return<bool> Supplicant::isDebugShowTimestampEnabled()
+{
+	return (wpa_debug_timestamp ? true : false);
+}
+
+Return<bool> Supplicant::isDebugShowKeysEnabled()
+{
+	return (wpa_debug_show_keys ? true : false);
+}
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace wifi
+}  // namespace supplicant
+}  // namespace hardware
+}  // namespace android