binder: Set/Get debug params

Implement the |wpa_supplicant| debug params setter/getter methods.

BUG: 30065582
TEST: Ran the integration tests under |wificond|.

Change-Id: I632541da8c86bcd352d17ae8a71bcadbe3a68034
Signed-off-by: Roshan Pius <rpius@google.com>
diff --git a/wpa_supplicant/binder/fi/w1/wpa_supplicant/ISupplicant.aidl b/wpa_supplicant/binder/fi/w1/wpa_supplicant/ISupplicant.aidl
index 7e353a0..4ce92fb 100644
--- a/wpa_supplicant/binder/fi/w1/wpa_supplicant/ISupplicant.aidl
+++ b/wpa_supplicant/binder/fi/w1/wpa_supplicant/ISupplicant.aidl
@@ -26,6 +26,17 @@
 	const int ERROR_IFACE_UNKNOWN = 3;
 
 	/**
+	 * Debug levels for wpa_supplicant.
+	 * These correspond to levels defined in |wpa_debug.h|.
+	 */
+	const int DEBUG_LEVEL_EXCESSIVE = 1;
+	const int DEBUG_LEVEL_MSGDUMP = 2;
+	const int DEBUG_LEVEL_DEBUG = 3;
+	const int DEBUG_LEVEL_INFO = 4;
+	const int DEBUG_LEVEL_WARNING = 5;
+	const int DEBUG_LEVEL_ERROR = 6;
+
+	/**
 	 * Registers a wireless interface in wpa_supplicant.
 	 *
 	 * @param args A dictionary with arguments used to add the interface to
@@ -58,4 +69,39 @@
 	 * @return Binder object representing the interface.
 	 */
 	IIface GetInterface(in String ifname);
+
+	/**
+	 * Set debug parameters for wpa_supplicant.
+	 *
+	 * @param level Debug logging level for wpa_supplicant.
+	 *        (one of DEBUG_LEVEL_* values).
+	 * @param timestamp Determines whether to show timestamps in logs or
+	 *        not.
+	 * @param show_keys Determines whether to show keys in debug logs or
+	 *        not.
+	 *        CAUTION: Do not set this param in production code!
+	 */
+	void SetDebugParams(
+	    int level, boolean show_timestamp, boolean show_keys);
+
+	/**
+	 * Get the debug level set.
+	 *
+	 * @return one of DEBUG_LEVEL_* values.
+	 */
+	int GetDebugLevel();
+
+	/**
+	 * Get whether the |show_timestamp| parameter has been set ot not.
+	 *
+	 * @return true if set, false otherwise.
+	 */
+	boolean GetDebugShowTimestamp();
+
+	/**
+	 * Get whether the |show_keys| parameter has been set ot not.
+	 *
+	 * @return true if set, false otherwise.
+	 */
+	boolean GetDebugShowKeys();
 }
diff --git a/wpa_supplicant/binder/supplicant.cpp b/wpa_supplicant/binder/supplicant.cpp
index 5a0db75..fccf9ca 100644
--- a/wpa_supplicant/binder/supplicant.cpp
+++ b/wpa_supplicant/binder/supplicant.cpp
@@ -16,7 +16,7 @@
 
 android::binder::Status Supplicant::CreateInterface(
     const android::os::PersistableBundle &params,
-    android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return)
+    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
 {
 	android::String16 driver, ifname, confname, bridge_ifname;
 
@@ -65,7 +65,7 @@
 
 		if (!binder_manager ||
 		    binder_manager->getIfaceBinderObjectByIfname(
-			wpa_s->ifname, aidl_return)) {
+			wpa_s->ifname, iface_object_out)) {
 			status =
 			    android::binder::Status::fromServiceSpecificError(
 				ERROR_GENERIC,
@@ -101,7 +101,7 @@
 
 android::binder::Status Supplicant::GetInterface(
     const std::string &ifname,
-    android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return)
+    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
 {
 	struct wpa_supplicant *wpa_s;
 
@@ -115,7 +115,7 @@
 	BinderManager *binder_manager = BinderManager::getInstance();
 	if (!binder_manager ||
 	    binder_manager->getIfaceBinderObjectByIfname(
-		wpa_s->ifname, aidl_return)) {
+		wpa_s->ifname, iface_object_out)) {
 		return android::binder::Status::fromServiceSpecificError(
 		    ERROR_GENERIC,
 		    "wpa_supplicant encountered a binder error.");
@@ -124,4 +124,116 @@
 	return android::binder::Status::ok();
 }
 
+android::binder::Status
+Supplicant::SetDebugParams(int level, bool show_timestamp, bool show_keys)
+{
+	int internal_level;
+	if (convertDebugLevelToInternalLevel(level, &internal_level)) {
+		const std::string error_msg =
+		    "invalid debug level: " + std::to_string(level);
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	if (wpa_supplicant_set_debug_params(
+		wpa_global_, internal_level, show_timestamp, show_keys)) {
+		return android::binder::Status::fromServiceSpecificError(
+		    ERROR_GENERIC,
+		    "wpa_supplicant could not set debug params.");
+	}
+	return android::binder::Status::ok();
+}
+
+android::binder::Status Supplicant::GetDebugLevel(int *level_out)
+{
+	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();
+}
+
+android::binder::Status
+Supplicant::GetDebugShowTimestamp(bool *show_timestamp_out)
+{
+	*show_timestamp_out = wpa_debug_timestamp ? true : false;
+	return android::binder::Status::ok();
+}
+
+android::binder::Status Supplicant::GetDebugShowKeys(bool *show_keys_out)
+{
+	*show_keys_out = wpa_debug_show_keys ? true : false;
+	return android::binder::Status::ok();
+}
+
+/**
+ * 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;
+	}
+}
+
+/**
+ * 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)
+{
+	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;
+	}
+}
 } /* namespace wpa_supplicant_binder */
diff --git a/wpa_supplicant/binder/supplicant.h b/wpa_supplicant/binder/supplicant.h
index 5b5fafa..10db25c 100644
--- a/wpa_supplicant/binder/supplicant.h
+++ b/wpa_supplicant/binder/supplicant.h
@@ -35,16 +35,30 @@
 	Supplicant(struct wpa_global *global);
 	~Supplicant() override = default;
 
+	// Binder methods exposed in aidl.
 	android::binder::Status CreateInterface(
 	    const android::os::PersistableBundle &params,
-	    android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return) override;
+	    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
+	    override;
 	android::binder::Status
 	RemoveInterface(const std::string &ifname) override;
 	android::binder::Status GetInterface(
 	    const std::string &ifname,
-	    android::sp<fi::w1::wpa_supplicant::IIface> *aidl_return) override;
+	    android::sp<fi::w1::wpa_supplicant::IIface> *iface_object_out)
+	    override;
+	android::binder::Status
+	SetDebugParams(int level, bool show_timestamp, bool show_keys) override;
+	android::binder::Status GetDebugLevel(int *level_out) override;
+	android::binder::Status
+	GetDebugShowTimestamp(bool *show_timestamp_out) override;
+	android::binder::Status GetDebugShowKeys(bool *show_keys_out) override;
 
 private:
+	int convertDebugLevelToInternalLevel(
+	    int external_level, int *internal_level);
+	int convertDebugLevelToExternalLevel(
+	    int internal_level, int *external_level);
+
 	/* Raw pointer to the global structure maintained by the core. */
 	struct wpa_global *wpa_global_;
 	/* All the callback objects registered by the clients. */