Merge "Fixed memory leak in SetField method" into tizen
[platform/core/connectivity/net-config.git] / src / wifi-indicator.c
index 35753cc..86fd4d8 100755 (executable)
@@ -19,6 +19,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <netinet/in.h>
@@ -65,76 +66,53 @@ int netconfig_wifi_get_rssi(void)
 
 static int __netconfig_wifi_update_and_get_rssi(void)
 {
-       FILE *fp;
-       char buf[512];
-       char *p_ifname = NULL, *p_entry = NULL;
+       char *if_path = NULL;
+       GVariant *message = NULL;
+       GVariant *value = NULL;
+       gchar *key;
+       GVariantIter *iter;
+       GVariant *variant;
+       gint32 key_value;
        int rssi_dbm = VCONFKEY_WIFI_SNR_MIN;
 
-       fp = fopen(NETCONFIG_PROCWIRELESS, "r");
-       if (fp == NULL) {
-               ERR("Failed to open %s", NETCONFIG_PROCWIRELESS);
-               return rssi_dbm;
+       if_path = netconfig_wifi_get_supplicant_interface();
+       if (if_path == NULL) {
+               ERR("Fail to get wpa_supplicant DBus path");
+               return 0;
        }
 
-       /* skip the first and second line */
-       if (fgets(buf, sizeof(buf), fp) == NULL ||
-                       fgets(buf, sizeof(buf), fp) == NULL)
-               goto endline;
-
-       while (fgets(buf, sizeof(buf), fp)) {
-               unsigned int status;
-               int link, noise;
-               /* No need to read */
-               /*
-               unsigned long nwid, crypt, frag, retry, misc, missed;
-               */
-
-               p_ifname = buf;
-               while (*p_ifname == ' ') p_ifname++;
-               p_entry = strchr(p_ifname, ':');
-               if (p_entry == NULL)
-                       goto endline;
-               *p_entry++ = '\0';
-
-               if (g_strcmp0(p_ifname, WIFI_IFNAME) != 0)
-                       continue;
-
-               /* read wireless status */
-               char *saveptr;
-               p_entry = strtok_r(p_entry, " .", &saveptr);    /* status                       "%x" */
-               if (p_entry != NULL)
-                       sscanf(p_entry, "%x", &status);
-               p_entry = strtok_r(NULL, " .", &saveptr);               /* Quality link         "%d" */
-               if (p_entry != NULL)
-                       sscanf(p_entry, "%d", &link);
-               p_entry = strtok_r(NULL, " .", &saveptr);               /* Quality level        "%d" */
-               if (p_entry != NULL)
-                       sscanf(p_entry, "%d", &rssi_dbm);
-               p_entry = strtok_r(NULL, " .", &saveptr);               /* Quality noise        "%d" */
-               if (p_entry != NULL)
-                       sscanf(p_entry, "%d", &noise);
-
-               /* No need to read */
-               /*
-               p_entry = strtok(NULL, " .");           // Discarded nwid       "%lu"
-               sscanf(p_entry, "%lu", &nwid);
-               p_entry = strtok(NULL, " .");           // Discarded crypt      "%lu"
-               sscanf(p_entry, "%lu", &crypt);
-               p_entry = strtok(NULL, " .");           // Discarded frag       "%lu"
-               sscanf(p_entry, "%lu", &frag);
-               p_entry = strtok(NULL, " .");           // Discarded retry      "%lu"
-               sscanf(p_entry, "%lu", &retry);
-               p_entry = strtok(NULL, " .");           // Discarded misc       "%lu"
-               sscanf(p_entry, "%lu", &misc);
-               p_entry = strtok(NULL, " .");           // Discarded missed     "%lu"
-               sscanf(p_entry, "%lu", &missed);
-               */
+       message = netconfig_invoke_dbus_method(SUPPLICANT_SERVICE, if_path,
+                       SUPPLICANT_INTERFACE ".Interface", "SignalPoll", NULL);
+       if (message == NULL) {
+               ERR("Fail to get SignalPoll from wpa_supplicant");
+               g_free(if_path);
+               return 0;
+       }
 
-               break;
+       g_variant_get(message, "(v)", &value);
+
+       g_variant_get(value, "a{sv}", &iter);
+       while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
+               key_value = g_variant_get_int32(variant);
+
+               if (g_strcmp0(key, "rssi") == 0) {
+                       rssi_dbm = (int)key_value;
+                       DBG("Currently signal dbm value [%d]", rssi_dbm);
+                       g_variant_unref(variant);
+                       g_free(key);
+                       break;
+               }
        }
 
-endline:
-       fclose(fp);
+       g_variant_iter_free(iter);
+
+       if (value)
+               g_variant_unref(value);
+
+       g_variant_unref(message);
+
+       g_free(if_path);
+
        netconfig_wifi_rssi = rssi_dbm;
 
        return rssi_dbm;
@@ -324,7 +302,7 @@ static gboolean __wifi_indicator_monitor(gpointer data)
                return FALSE;
 
        /* In case of LCD off, we don't need to update Wi-Fi indicator */
-       vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
+       netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
        if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
                return TRUE;