Remove vconftool command to use buxton key
[platform/core/connectivity/net-config.git] / src / wifi-ssid-scan.c
old mode 100644 (file)
new mode 100755 (executable)
index 166feb3..390fe1b
 #include "wifi-ssid-scan.h"
 #include "wifi-background-scan.h"
 
-enum netconfig_wifi_security {
+typedef enum {
        WIFI_SECURITY_UNKNOWN = 0x00,
        WIFI_SECURITY_NONE = 0x01,
        WIFI_SECURITY_WEP = 0x02,
        WIFI_SECURITY_PSK = 0x03,
        WIFI_SECURITY_IEEE8021X = 0x04,
-};
+} wifi_security_e;
 
-struct bss_info_t {
+typedef struct {
        unsigned char ssid[33];
-       enum netconfig_wifi_security security;
+       wifi_security_e security;
        gboolean privacy;
        gboolean wps;
-};
+} bss_info_t;
 
-static gboolean wifi_ssid_scan_state = FALSE;
-static GSList *wifi_bss_info_list = NULL;
-static guint netconfig_wifi_ssid_scan_timer = 0;
+static gboolean g_ssid_scan_state = FALSE;
+static GSList *bss_info_list = NULL;
+static guint ssid_scan_timer = 0;
 static char *g_ssid = NULL;
 
-static gboolean __netconfig_wifi_ssid_scan_timeout(gpointer data)
+static void __check_security(const char *str_keymgmt, bss_info_t *bss_info)
 {
-       netconfig_wifi_notify_ssid_scan_done();
+       INFO("keymgmt : %s", str_keymgmt);
+
+       if (g_strcmp0(str_keymgmt, "ieee8021x") == 0) {
+               bss_info->security = WIFI_SECURITY_IEEE8021X;
+       } else if (g_strcmp0(str_keymgmt, "wpa-psk") == 0) {
+               bss_info->security = WIFI_SECURITY_PSK;
+       } else if (g_strcmp0(str_keymgmt, "wpa-psk-sha256") == 0) {
+               bss_info->security = WIFI_SECURITY_PSK;
+       } else if (g_strcmp0(str_keymgmt, "wpa-ft-psk") == 0) {
+               bss_info->security = WIFI_SECURITY_PSK;
+       } else if (g_strcmp0(str_keymgmt, "wpa-ft-eap") == 0) {
+               bss_info->security = WIFI_SECURITY_IEEE8021X;
+       } else if (g_strcmp0(str_keymgmt, "wpa-eap") == 0) {
+               bss_info->security = WIFI_SECURITY_IEEE8021X;
+       } else if (g_strcmp0(str_keymgmt, "wpa-eap-sha256") == 0) {
+               bss_info->security = WIFI_SECURITY_IEEE8021X;
+       } else if (g_strcmp0(str_keymgmt, "wps") == 0) {
+               bss_info->wps = TRUE;
+       }
+}
+
+static gboolean __ssid_scan_timeout(gpointer data)
+{
+       wifi_ssid_scan_emit_scan_completed();
 
        return FALSE;
 }
 
-static void __netconfig_wifi_ssid_scan_started(void)
+static void _start_ssid_scan_timer(void)
 {
        INFO("Wi-Fi SSID scan started");
-       wifi_ssid_scan_state = TRUE;
+       g_ssid_scan_state = TRUE;
 
-       netconfig_start_timer_seconds(5,
-                       __netconfig_wifi_ssid_scan_timeout,
-                       NULL,
-                       &netconfig_wifi_ssid_scan_timer);
+       netconfig_start_timer_seconds(5, __ssid_scan_timeout, NULL, &ssid_scan_timer);
 }
 
-static void __netconfig_wifi_ssid_scan_finished(void)
+static void _stop_ssid_scan_timer(void)
 {
        INFO("Wi-Fi SSID scan finished");
-       wifi_ssid_scan_state = FALSE;
+       g_ssid_scan_state = FALSE;
 
-       netconfig_stop_timer(&netconfig_wifi_ssid_scan_timer);
+       netconfig_stop_timer(&ssid_scan_timer);
 }
 
-static gboolean __netconfig_wifi_invoke_ssid_scan(
-               const char *object_path, const char *ssid)
+static void _parse_keymgmt_message(GVariant *param, bss_info_t *bss_info)
+{
+       GVariantIter *iter1;
+       GVariant *var;
+       gchar *key;
+
+       g_variant_get(param, "a{sv}", &iter1);
+       while (g_variant_iter_loop(iter1, "{sv}", &key, &var)) {
+               if (g_strcmp0(key, "KeyMgmt") == 0) {//check this :iterate
+                       GVariantIter *iter2;
+                       g_variant_get(var, "as", &iter2);
+                       char *str;
+                       while (g_variant_iter_loop(iter2, "s", &str)) {
+                               if (str == NULL) {
+                                       break;
+                               }
+                               __check_security(str, bss_info);
+                       }
+                       g_variant_iter_free (iter2);
+               }
+       }
+
+       g_variant_iter_free (iter1);
+
+       return;
+}
+
+static gboolean _request_ssid_scan(const char *object_path, const char *ssid)
 {
        /* TODO: Revise following code */
 
@@ -89,7 +135,7 @@ static gboolean __netconfig_wifi_invoke_ssid_scan(
        const gchar *key2 = "SSIDs";
        int i = 0;
 
-       connection = netconfig_gdbus_get_connection();
+       connection = netdbus_get_connection();
        if (connection == NULL) {
                DBG("Failed to get GDBusconnection");
                return FALSE;
@@ -124,7 +170,7 @@ static gboolean __netconfig_wifi_invoke_ssid_scan(
                        NULL,
                        G_DBUS_CALL_FLAGS_NONE,
                        NETCONFIG_DBUS_REPLY_TIMEOUT,
-                       netconfig_gdbus_get_gdbus_cancellable(),
+                       netdbus_get_cancellable(),
                        &error);
 
        if (reply == NULL) {
@@ -149,7 +195,7 @@ static gboolean __netconfig_wifi_invoke_ssid_scan(
        return TRUE;
 }
 
-static void __netconfig_wifi_notify_ssid_scan_done(void)
+static void _emit_ssid_scan_completed(void)
 {
        GVariantBuilder *builder = NULL;
        GSList* list = NULL;
@@ -158,32 +204,28 @@ static void __netconfig_wifi_notify_ssid_scan_done(void)
        const char *prop_wps = "wps";
 
        builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
-       for (list = wifi_bss_info_list; list != NULL; list = list->next) {
-               struct bss_info_t *bss_info = (struct bss_info_t *)list->data;
+       for (list = bss_info_list; list != NULL; list = list->next) {
+               bss_info_t *bss_info = (bss_info_t *)list->data;
                if (bss_info && g_strcmp0((char *)bss_info->ssid, g_ssid) == 0) {
                        const gchar *ssid = (char *)bss_info->ssid;
-                       enum netconfig_wifi_security security = bss_info->security;
+                       wifi_security_e security = bss_info->security;
                        gboolean wps = bss_info->wps;
                        DBG("BSS found; SSID:%s security:%d WPS:%d", ssid, security, wps);
-
-                       /* SSID */
-                       g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new("(s)", ssid));
-                       /* Security */
+                       g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new_string(ssid));
                        g_variant_builder_add(builder, "{sv}", prop_security, g_variant_new_int32(security));
                        /* WPS */
                        g_variant_builder_add(builder, "{sv}", prop_wps, g_variant_new_boolean(wps));
                }
        }
 
-       wifi_emit_specific_scan_completed((Wifi *)get_netconfig_wifi_object(),
-                       g_variant_builder_end(builder));
+       wifi_emit_specific_scan_completed((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
 
        if (builder)
                g_variant_builder_unref(builder);
 
-       if (wifi_bss_info_list != NULL) {
-               g_slist_free_full(wifi_bss_info_list, g_free);
-               wifi_bss_info_list = NULL;
+       if (bss_info_list != NULL) {
+               g_slist_free_full(bss_info_list, g_free);
+               bss_info_list = NULL;
        }
 
        if (g_ssid != NULL) {
@@ -196,80 +238,82 @@ static void __netconfig_wifi_notify_ssid_scan_done(void)
        return;
 }
 
-static void __netconfig_wifi_check_security(const char *str_keymgmt, struct bss_info_t *bss_data)
+gboolean wifi_ssid_scan(const char *ssid)
 {
-       INFO("keymgmt : %s", str_keymgmt);
+       const char *if_path;
+       static char *scan_ssid = NULL;
+
+       netconfig_wifi_bgscan_stop();
 
-       if (strcmp(str_keymgmt, "ieee8021x") == 0) {
-               bss_data->security = WIFI_SECURITY_IEEE8021X;
-       } else if (strcmp(str_keymgmt, "wpa-psk") == 0) {
-               bss_data->security = WIFI_SECURITY_PSK;
-       } else if (strcmp(str_keymgmt, "wpa-psk-sha256") == 0) {
-               bss_data->security = WIFI_SECURITY_PSK;
-       } else if (strcmp(str_keymgmt, "wpa-ft-psk") == 0) {
-               bss_data->security = WIFI_SECURITY_PSK;
-       } else if (strcmp(str_keymgmt, "wpa-ft-eap") == 0) {
-               bss_data->security = WIFI_SECURITY_IEEE8021X;
-       } else if (strcmp(str_keymgmt, "wpa-eap") == 0) {
-               bss_data->security = WIFI_SECURITY_IEEE8021X;
-       } else if (strcmp(str_keymgmt, "wpa-eap-sha256") == 0) {
-               bss_data->security = WIFI_SECURITY_IEEE8021X;
-       } else if (strcmp(str_keymgmt, "wps") == 0) {
-               bss_data->wps = TRUE;
+       if (ssid != NULL) {
+               if (scan_ssid != NULL) {
+                       g_free(scan_ssid);
+               }
+               scan_ssid = g_strdup(ssid);
        }
-}
 
-static void __netconfig_wifi_parse_keymgmt_message(GVariant *param, struct bss_info_t *bss_data)
-{
-       GVariantIter *iter1;
-       GVariant *var;
-       gchar *key;
+       if (scan_ssid == NULL)
+               goto error;
 
-       g_variant_get(param, "a{sv}", &iter1);
-       while (g_variant_iter_loop(iter1, "{sv}", &key, &var)) {
-               if (g_strcmp0(key, "KeyMgmt") == 0) {//check this :iterate
-                       GVariantIter *iter2;
-                       g_variant_get(var, "as", &iter2);
-                       char *str;
-                       while (g_variant_iter_loop(iter2, "s", &str)) {
-                               if (str == NULL) {
-                                       break;
-                               }
-                               __netconfig_wifi_check_security(str, bss_data);
-                       }
-                       g_variant_iter_free (iter2);
-               }
+       if_path = netconfig_wifi_get_supplicant_interface();
+       if (if_path == NULL) {
+               DBG("Fail to get wpa_supplicant DBus path");
+               goto error;
        }
 
-       g_variant_iter_free (iter1);
+       if (netconfig_wifi_get_scanning() == TRUE) {
+               DBG("Wi-Fi scan in progress, %s scan will be delayed", scan_ssid);
+               g_free(scan_ssid);
+               return TRUE;
+       }
 
-       return;
+       if (bss_info_list) {
+               g_slist_free_full(bss_info_list, g_free);
+               bss_info_list = NULL;
+       }
+
+       INFO("Start Wi-Fi scan with %s(%d)", scan_ssid, strlen(scan_ssid));
+       if (_request_ssid_scan(if_path, (const char *)scan_ssid) == TRUE) {
+               _start_ssid_scan_timer();
+               g_free(scan_ssid);
+               scan_ssid = NULL;
+               return TRUE;
+       }
+
+error:
+       if (scan_ssid != NULL) {
+               g_free(scan_ssid);
+               scan_ssid = NULL;
+       }
+
+       netconfig_wifi_bgscan_start(FALSE);
+
+       return FALSE;
 }
 
-gboolean netconfig_wifi_get_ssid_scan_state(void)
+gboolean wifi_ssid_scan_get_state(void)
 {
-       return wifi_ssid_scan_state;
+       return g_ssid_scan_state;
 }
 
-void netconfig_wifi_notify_ssid_scan_done(void)
+void wifi_ssid_scan_emit_scan_completed(void)
 {
-       if (netconfig_wifi_get_ssid_scan_state() != TRUE)
+       if (g_ssid_scan_state != TRUE)
                return;
 
-       __netconfig_wifi_ssid_scan_finished();
-
-       __netconfig_wifi_notify_ssid_scan_done();
+       _stop_ssid_scan_timer();
+       _emit_ssid_scan_completed();
 }
 
-void netconfig_wifi_bss_added(GVariant *message)
+void wifi_ssid_scan_add_bss(GVariant *message)
 {
        GVariantIter *iter;
        GVariant *value;
-       const gchar *path = NULL;
-       const gchar *key;
-       struct bss_info_t *bss_info;
+       gchar *path = NULL;
+       gchar *key;
+       bss_info_t *bss_info;
 
-       if (netconfig_wifi_get_ssid_scan_state() != TRUE)
+       if (g_ssid_scan_state != TRUE)
                return;
 
        INFO("NEW BSS added");
@@ -279,11 +323,10 @@ void netconfig_wifi_bss_added(GVariant *message)
                return;
        }
 
-
        if (path != NULL)
                INFO("Object path of BSS added is %s",path);
 
-       bss_info = g_try_new0(struct bss_info_t, 1);
+       bss_info = g_try_new0(bss_info_t, 1);
        if (bss_info == NULL)
                return;
 
@@ -293,7 +336,7 @@ void netconfig_wifi_bss_added(GVariant *message)
                        const guchar *ssid;
                        gsize ssid_len;
                        ssid = g_variant_get_fixed_array(value, &ssid_len, sizeof(guchar));
-                       if (ssid_len > 0 && ssid_len < 33)
+                       if (ssid != NULL && ssid_len > 0 && ssid_len < 33)
                                memcpy(bss_info->ssid, ssid, ssid_len);
                        else
                                memset(bss_info->ssid, 0, sizeof(bss_info->ssid));
@@ -302,7 +345,7 @@ void netconfig_wifi_bss_added(GVariant *message)
                        privacy = g_variant_get_boolean(value);
                        bss_info->privacy = privacy;
                } else if ((g_strcmp0(key, "RSN") == 0) || (g_strcmp0(key, "WPA") == 0)) {
-                       __netconfig_wifi_parse_keymgmt_message(value, bss_info);
+                       _parse_keymgmt_message(value, bss_info);
                } else if (g_strcmp0(key, "IEs") == 0) {
                        const guchar *ie;
                        gsize ie_len;
@@ -312,6 +355,8 @@ void netconfig_wifi_bss_added(GVariant *message)
        }
 
        g_variant_iter_free(iter);
+       if (path)
+               g_free(path);
 
        if (bss_info->ssid[0] == '\0') {
                g_free(bss_info);
@@ -325,61 +370,7 @@ void netconfig_wifi_bss_added(GVariant *message)
                        bss_info->security = WIFI_SECURITY_NONE;
        }
 
-       wifi_bss_info_list = g_slist_append(wifi_bss_info_list, bss_info);
-}
-
-gboolean netconfig_wifi_ssid_scan(const char *ssid)
-{
-       const char *if_path;
-       static char *scan_ssid = NULL;
-
-       netconfig_wifi_bgscan_stop();
-
-       if (ssid != NULL) {
-               g_free(scan_ssid);
-               scan_ssid = g_strdup(ssid);
-       }
-
-       if (scan_ssid == NULL)
-               goto error;
-
-       if_path = netconfig_wifi_get_supplicant_interface();
-       if (if_path == NULL) {
-               DBG("Fail to get wpa_supplicant DBus path");
-               goto error;
-       }
-
-       if (netconfig_wifi_get_scanning() == TRUE) {
-               DBG("Wi-Fi scan in progress, %s scan will be delayed", scan_ssid);
-               g_free(scan_ssid);
-               return TRUE;
-       }
-
-       if (wifi_bss_info_list) {
-               g_slist_free_full(wifi_bss_info_list, g_free);
-               wifi_bss_info_list = NULL;
-       }
-
-       INFO("Start Wi-Fi scan with %s(%d)", scan_ssid, strlen(scan_ssid));
-       if (__netconfig_wifi_invoke_ssid_scan(if_path,
-                                                       (const char *)scan_ssid) == TRUE) {
-               __netconfig_wifi_ssid_scan_started();
-
-               g_free(scan_ssid);
-               scan_ssid = NULL;
-
-               return TRUE;
-       }
-
-error:
-       if (scan_ssid != NULL) {
-               g_free(scan_ssid);
-               scan_ssid = NULL;
-       }
-
-       netconfig_wifi_bgscan_start(FALSE);
-
-       return FALSE;
+       bss_info_list = g_slist_append(bss_info_list, bss_info);
 }
 
 gboolean handle_request_specific_scan(Wifi *wifi,
@@ -390,7 +381,7 @@ gboolean handle_request_specific_scan(Wifi *wifi,
        g_return_val_if_fail(wifi != NULL, FALSE);
        g_return_val_if_fail(ssid != NULL, FALSE);
 
-       result = netconfig_wifi_ssid_scan((const char *)ssid);
+       result = wifi_ssid_scan((const char *)ssid);
 
        if (result != TRUE) {
                netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSpecificScan");