Add dbus methods for setting bg scan interval
[platform/core/connectivity/net-config.git] / src / clatd-handler.c
old mode 100644 (file)
new mode 100755 (executable)
index b953eed..570092f
 #include "log.h"
 #include "util.h"
 #include "network-state.h"
+#include "netdbus.h"
 #include "clatd-handler.h"
 
-#define CLAT_EXEC_PATH "/usr/sbin/clatd"
-#define ROUTE_EXEC_PATH "/sbin/route"
-#define KILLALL_EXEC_PATH "/usr/bin/killall"
-#define IFCONFIG_EXEC_PATH "/sbin/ifconfig"
+static gboolean g_is_running = FALSE;
 
-char g_ifname[32] = {0, };
+static void __netconfig_clatd_async_callback(GObject *source_object,
+               GAsyncResult *res, gpointer user_data)
+{
+       GDBusConnection *conn = NULL;
+       GVariant *reply = NULL;
+       GError *error = NULL;
+       DBG("__netconfig_clatd_async_callback");
+
+       conn = G_DBUS_CONNECTION(source_object);
+       reply = g_dbus_connection_call_finish(conn, res, &error);
+       if (error) {
+               ERR("Dbus error : %s", error->message);
+               g_error_free(error);
+       } else {
+               int is_clat_enabled = -1;
+               g_variant_get(reply, "(i)", &is_clat_enabled);
+
+               if (is_clat_enabled == 0) {
+                       DBG("OK");
+               } else if (is_clat_enabled == 1) {
+                       INFO("already on going");
+                       g_is_running = TRUE;
+               } else {
+                       ERR("Failed to clat enable, %d", is_clat_enabled);
+               }
+       }
+
+       if (reply)
+               g_variant_unref(reply);
+}
 
 int netconfig_clatd_enable(void)
 {
-       int rv = 0;
-
-       if (g_ifname[0] != '\0') {
-               rv = netconfig_clatd_disable();
+       DBG("");
+       GVariant *params = NULL;
+       GVariantBuilder *builder = NULL;
+       gboolean rv = FALSE;
+       struct clatd_ctrl_hint hint = {0 ,};
+       char ns_buff[256] = {0, };
+
+       if (g_is_running) {
+               INFO("clatd is already ENABLED");
+               return 0;
+       }
 
-               if (rv < 0) {
-                       DBG("Failed to disable existing clatd process");
-                       return -1;
-               }
+       if (!netconfig_get_connected_cellular_internet_ipv6only_profile(&hint)) {
+               ERR("Failed to get cellular profile");
+               return -1;
        }
 
        const char *if_name = netconfig_get_default_ifname();
-
        if (if_name == NULL) {
-               DBG("There is no interface name");
+               int idx;
+               for (idx = 0; idx < MAX_DNS; idx++)
+                       g_free(hint.nameserver[idx]);
+
+               ERR("There is no interface name");
                return -1;
+       } else {
+               DBG("if_name : %s", if_name);
        }
 
-       memset(g_ifname, 0, sizeof(g_ifname));
-       g_strlcat(g_ifname, if_name, 32);
+       builder = g_variant_builder_new(G_VARIANT_TYPE ("a{is}"));
 
-       const char *path = CLAT_EXEC_PATH;
-       char *const args[] = { "/usr/sbin/clatd", "-i", g_ifname, NULL };
+       g_variant_builder_add(builder, "{is}", 'i', if_name);
 
-       rv = netconfig_execute_clatd(path, args);
+       // nameserver addresses
+       if (hint.nameserver[0] != NULL || hint.nameserver[1] != NULL) {
+               g_snprintf(ns_buff, 256, "%s;%s",
+                                       ((hint.nameserver[0] != NULL) ? hint.nameserver[0] : "NULL"),
+                                       ((hint.nameserver[1] != NULL) ? hint.nameserver[1] : "NULL"));
+       }
+       g_variant_builder_add(builder, "{is}", 'd', ns_buff);
+
+       params = g_variant_builder_end(builder);
+       g_variant_builder_unref(builder);
+
+       rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
+                       CLATD_INTERFACE, "Start", g_variant_new("(@a{is})", params),
+                       __netconfig_clatd_async_callback, NULL);
 
-       if (rv < 0) {
-               DBG("Failed to enable clatd process %d", rv);
+       if (!rv) {
+               DBG("Failed to dbus call");
+               g_variant_unref(params);
+
+               int idx;
+               for (idx = 0; idx < MAX_DNS; idx++)
+                       g_free(hint.nameserver[idx]);
                return -1;
        }
 
-       DBG("Successfully enabled clatd process with %s interface", g_ifname);
+       g_is_running = TRUE;
+       DBG("clatd enabled[%d]", g_is_running);
+       int idx;
+       for (idx = 0; idx < MAX_DNS; idx++)
+               g_free(hint.nameserver[idx]);
        return 0;
 }
 
 int netconfig_clatd_disable(void)
 {
-       int rv = 0;
-
-       const char *path = KILLALL_EXEC_PATH;
-       char *const args[] = { "/usr/bin/kill -15", "clatd", NULL };
-       char *const envs[] = { NULL };
+       DBG("");
+       gboolean rv = FALSE;
 
-       if (g_ifname[0] == '\0') {
-               DBG("There is no clatd process");
-               return -1;
+       if (!g_is_running) {
+               ERR("clatd is already DISABLED");
+               return 0;
        }
 
-       memset(g_ifname, 0, sizeof(g_ifname));
-
-       rv = netconfig_execute_file(path, args, envs);
-
-       if (rv < 0) {
-               DBG("Failed to disable clatd process %d", rv);
+       rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
+                       CLATD_INTERFACE, "Stop", NULL, NULL, NULL);
+       g_is_running = FALSE;
+       DBG("clatd disabled[%d]", g_is_running);
+       if (!rv) {
+               DBG("Failed to dbus call");
                return -1;
        }
 
-       DBG("Successfully disable clatd process");;
        return 0;
 }
+
+void netconfig_clatd_reset()
+{
+       DBG("");
+       g_is_running = FALSE;
+       netconfig_clatd_enable();
+}