Fixed some svace issues
[platform/core/connectivity/net-config.git] / src / wifi-agent.c
old mode 100644 (file)
new mode 100755 (executable)
index c9ac420..b2dd9d7
@@ -17,7 +17,6 @@
  *
  */
 
-#include <app.h>
 #include <stdio.h>
 #include <vconf.h>
 #include <stdlib.h>
@@ -71,61 +70,28 @@ static void __netconfig_agent_clear_fields(void)
        agent.wps_pbc = FALSE;
 }
 
-int netconfig_agent_register(void)
+int connman_register_agent(void)
 {
        GVariant *reply = NULL;
        GVariant *params = NULL;
-       GError *error;
-       GDBusConnection *connection = NULL;
 
-       connection = netconfig_gdbus_get_connection();
-       if (connection == NULL) {
-               ERR("GDBusconnection is NULL");
-               return -1;
-       }
+       params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
+       reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
+                       CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
+                       "RegisterAgent", params);
 
-       do {
-               error = NULL;
-               params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
-
-               reply = g_dbus_connection_call_sync(
-                               connection,
-                               CONNMAN_SERVICE,
-                               CONNMAN_MANAGER_PATH,
-                               CONNMAN_MANAGER_INTERFACE,
-                               "RegisterAgent",
-                               params,
-                               NULL,
-                               G_DBUS_CALL_FLAGS_NONE,
-                               DBUS_REPLY_TIMEOUT,
-                               netconfig_gdbus_get_gdbus_cancellable(),
-                               &error);
-
-               if (reply == NULL) {
-                if (error != NULL) {
-                        if (g_strcmp0(error->message,
-                                        "GDBus.Error:net.connman.Error.AlreadyExists: Already exists") == 0) {
-                                       break;
-                        } else {
-                                ERR("Fail to register agent [%d: %s]",
-                                                error->code, error->message);
-                        }
-
-                        g_error_free(error);
-                } else
-                        ERR("Fail to register agent");
-               } else
-                       g_variant_unref(reply);
-
-               sleep(1);
-       } while (TRUE);
+       if (reply == NULL) {
+                       ERR("Fail to register agent");
+                       return FALSE;
+       } else
+               g_variant_unref(reply);
 
        INFO("Registered to connman agent successfully");
 
-       return 0;
+       return TRUE;
 }
 
-int netconfig_agent_unregister(void)
+int connman_unregister_agent(void)
 {
        gboolean reply = FALSE;
        GVariant *param = NULL;
@@ -161,7 +127,7 @@ gboolean netconfig_wifi_set_agent_field_for_eap_network(
        name_len = strlen(name);
        agent.ssid = g_byte_array_sized_new(name_len);
        agent.ssid->len = name_len;
-       memcpy(agent.ssid->data, name, name_len);
+       memcpy(agent.ssid->data, name, name_len + 1);
 
        if (identity)
                agent.identity = g_strdup(identity);
@@ -203,17 +169,13 @@ gboolean handle_set_field(NetConnmanAgent *connman_agent,
        g_variant_get(fields, "a{sv}", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
                if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
-                       g_free(agent.passphrase);
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
                                agent.passphrase = g_strdup(g_variant_get_string(value, NULL));
                                updated = TRUE;
 
                                DBG("Field [%s] - []", field);
-                       } else {
-                               agent.passphrase = NULL;
                        }
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
-                       agent.wps_pbc = FALSE;
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
                                        g_strcmp0(g_variant_get_string(value, NULL), "enable") == 0) {
                                agent.wps_pbc = TRUE;
@@ -222,25 +184,18 @@ gboolean handle_set_field(NetConnmanAgent *connman_agent,
                                DBG("Field [%s] - [%d]", field, agent.wps_pbc);
                        }
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
-                       g_free(agent.wps_pin);
-                       agent.wps_pbc = FALSE;
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
                                agent.wps_pin = g_strdup(g_variant_get_string(value, NULL));
                                updated = TRUE;
 
                                DBG("Field [%s] - []", field);
-                       } else {
-                               agent.wps_pin = NULL;
                        }
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0) {
-                       g_free(agent.name);
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
                                agent.name = g_strdup(g_variant_get_string(value, NULL));
                                updated = TRUE;
 
                                DBG("Field [%s] - []", field);
-                       } else {
-                               agent.name = NULL;
                        }
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0) {
                        if (agent.ssid != NULL) {
@@ -254,9 +209,8 @@ gboolean handle_set_field(NetConnmanAgent *connman_agent,
                                GByteArray *array = g_byte_array_new();
 
                                g_variant_get(value, "ay", &iter1);
-                               while(g_variant_iter_loop(iter1, "y",  &char_value)) {
+                               while (g_variant_iter_loop(iter1, "y", &char_value))
                                        g_byte_array_append(array, &char_value, 1);
-                               }
                                g_variant_iter_free(iter1);
                                if (array != NULL && (array->len > 0)) {
                                        agent.ssid = g_byte_array_sized_new(array->len);
@@ -268,23 +222,21 @@ gboolean handle_set_field(NetConnmanAgent *connman_agent,
                                }
                        }
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0) {
-                       g_free(agent.identity);
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
                                agent.identity = g_strdup(g_variant_get_string(value, NULL));
                                updated = TRUE;
 
                                DBG("Field [%s] - []", field);
-                       } else {
-                               agent.identity = NULL;
                        }
                }
        }
 
        if (updated == TRUE) {
                reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
-                               service, CONNMAN_SERVICE_INTERFACE, "Connect", NULL, NULL);
+                               service, CONNMAN_SERVICE_INTERFACE, "Connect",
+                               NULL, __netconfig_wifi_connect_reply);
                if (reply == TRUE) {
-                       g_dbus_method_invocation_return_value (context, NULL);
+                       g_dbus_method_invocation_return_value(context, NULL);
                } else {
                        error = g_error_new(G_DBUS_ERROR,
                                        G_DBUS_ERROR_AUTH_FAILED,
@@ -330,7 +282,7 @@ gboolean handle_request_input(NetConnmanAgent *connman_agent,
 
        DBG("Agent fields requested for service: %s", service);
 
-       builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
 
        g_variant_get(fields, "a{sv}", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
@@ -345,7 +297,7 @@ gboolean handle_request_input(NetConnmanAgent *connman_agent,
                } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
                                (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
                        if (agent.wps_pbc == TRUE) {
-                               // Sending empty string for WPS push button method
+                               /* Sending empty string for WPS push button method */
                                g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
 
                                updated = TRUE;
@@ -366,11 +318,10 @@ gboolean handle_request_input(NetConnmanAgent *connman_agent,
                                agent.ssid != NULL) {
                        int i = 0;
                        GVariantBuilder *builder1 = NULL;
-                       builder1 = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
+                       builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
-                       for (i = 0; i < (agent.ssid->len); i++) {
-                               g_variant_builder_add (builder1, "y", agent.ssid->data[i]);
-                       }
+                       for (i = 0; i < (agent.ssid->len); i++)
+                               g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
 
                        g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
                        if (builder1 != NULL)
@@ -395,14 +346,14 @@ gboolean handle_request_input(NetConnmanAgent *connman_agent,
        g_variant_iter_free(iter);
 
 
-       if (NULL == out_table){
+       if (NULL == out_table) {
                net_connman_agent_complete_request_input(connman_agent, context, out_table);
 
                return FALSE;
        }
 
        if (updated == TRUE)
-               g_dbus_method_invocation_return_value (context, out_table);
+               g_dbus_method_invocation_return_value(context, out_table);
        else {
                GError *error = NULL;
                error = g_error_new(G_DBUS_ERROR,
@@ -430,7 +381,7 @@ gboolean handle_report_error(NetConnmanAgent *connman_agent,
        net_connman_agent_complete_report_error(connman_agent, context);
        DBG("Agent error for service[%s] - [%s]", service, error);
 
-       // Do something when it failed to make a connection
+       /* Do something when it failed to make a connection */
 
        return ret;
 }
@@ -448,7 +399,6 @@ static gboolean is_monitor_notifier_registered = FALSE;
 
 #if defined TIZEN_WEARABLE
 static gboolean is_portal_msg_shown = FALSE;
-static guint portal_msg_timer = 0;
 #endif
 
 struct poll_timer_data {
@@ -457,8 +407,8 @@ struct poll_timer_data {
        void* data;
 };
 
-static struct poll_timer_data timer_data =
-                       {QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
+static struct poll_timer_data timer_data = {
+               QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
 
 static gboolean __check_ignore_portal_list(const char * ssid)
 {
@@ -474,7 +424,7 @@ static gboolean __check_ignore_portal_list(const char * ssid)
        DBG("csc string [%s]", def_str);
        gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
        ignore_ap_count = g_strv_length(ignore_ap_list);
-       for(i = 0; i < ignore_ap_count; i++) {
+       for (i = 0; i < ignore_ap_count; i++) {
                DBG("[%d] - [%s]", i, ignore_ap_list[i]);
                if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
                        g_strfreev(ignore_ap_list);
@@ -486,15 +436,15 @@ static gboolean __check_ignore_portal_list(const char * ssid)
        return FALSE;
 }
 
-static void __wifi_state_monitor(enum netconfig_wifi_service_state state,
+static void __wifi_state_monitor(wifi_service_state_e state,
                void *user_data);
 
-static struct netconfig_wifi_state_notifier wifi_state_monitor_notifier = {
-               .netconfig_wifi_state_changed = __wifi_state_monitor,
+static wifi_state_notifier wifi_state_monitor_notifier = {
+               .wifi_state_changed = __wifi_state_monitor,
                .user_data = NULL,
 };
 
-static void __wifi_state_monitor(enum netconfig_wifi_service_state state,
+static void __wifi_state_monitor(wifi_service_state_e state,
                void *user_data)
 {
        DBG("Wi-Fi state: %x", state);
@@ -503,7 +453,7 @@ static void __wifi_state_monitor(enum netconfig_wifi_service_state state,
                return;
 
        if (is_monitor_notifier_registered == TRUE) {
-               netconfig_wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
+               wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
                is_monitor_notifier_registered = FALSE;
        }
 
@@ -534,8 +484,7 @@ static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
 
        if (TRUE == netconfig_get_internet_status()) {
                if (is_monitor_notifier_registered == TRUE) {
-                       netconfig_wifi_state_notifier_unregister(
-                                                       &wifi_state_monitor_notifier);
+                       wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
                        is_monitor_notifier_registered = FALSE;
                }
 
@@ -546,8 +495,7 @@ static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
                        DBG("Login failed, update ConnMan");
 
                        if (is_monitor_notifier_registered == TRUE) {
-                               netconfig_wifi_state_notifier_unregister(
-                                               &wifi_state_monitor_notifier);
+                               wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
                                is_monitor_notifier_registered = FALSE;
                        }
 
@@ -566,7 +514,7 @@ static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
                        }
                } else {
                        if (NETCONFIG_WIFI_CONNECTED ==
-                                       netconfig_wifi_state_get_service_state()) {
+                                       wifi_state_get_service_state()) {
                                /* check Internet availability by sending and receiving data*/
                                netconfig_check_internet_accessibility();
                                /* Returning TRUE itself is enough to restart the timer */
@@ -580,18 +528,6 @@ static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
        return FALSE;
 }
 
-#if defined TIZEN_WEARABLE
-static gboolean __netconfig_display_portal_msg(gpointer data)
-{
-       DBG("");
-       wc_launch_popup(WC_POPUP_TYPE_CAPTIVE_PORTAL);
-
-       netconfig_stop_timer(&portal_msg_timer);
-
-       return FALSE;
-}
-#endif
-
 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
                *data)
 {
@@ -633,27 +569,17 @@ gboolean handle_request_browser(NetConnmanAgent *connman_agent,
 
        ignore_portal = __check_ignore_portal_list(ssid);
 
-       if (ignore_portal == TRUE){
+       if (ignore_portal == TRUE) {
                net_connman_agent_complete_request_browser(connman_agent, context);
                return TRUE;
        }
        /* Register for Wifi state change notifier*/
        if (is_monitor_notifier_registered == FALSE) {
-               netconfig_wifi_state_notifier_register(&wifi_state_monitor_notifier);
+               wifi_state_notifier_register(&wifi_state_monitor_notifier);
                is_monitor_notifier_registered = TRUE;
        }
 
-#if defined TIZEN_WEARABLE
-       if (is_portal_msg_shown){
-               net_connman_agent_complete_request_browser(connman_agent, context);
-               return TRUE;
-       }
-
-       is_portal_msg_shown = TRUE;
-       netconfig_start_timer_seconds(4, __netconfig_display_portal_msg, NULL, &portal_msg_timer);
-#else
        ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
-#endif
 
        timer_data.time_elapsed = 0;
        __netconfig_wifi_portal_login_timer_start(&timer_data);