[wifi] Modified logic so that callback is sent to their respective thread
[platform/core/api/wifi.git] / src / libnetwork.c
index 7ff72c5..b4b0c63 100755 (executable)
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "wifi_dbus_private.h"
 #include "net_wifi_private.h"
 
 static __thread bool is_init = false;
@@ -65,8 +66,12 @@ static __thread struct _wifi_cb_s wifi_callbacks = { 0, };
 static __thread struct _profile_list_s profile_iterator = { 0, NULL };
 static __thread struct _profile_list_s specific_profile_iterator = {0, NULL};
 static __thread char specific_profile_essid[NET_WLAN_ESSID_LEN + 1] = { 0, };
+static __thread bool is_feature_checked = false;
+static __thread bool feature_supported = false;
 static __thread GSList *managed_idler_list = NULL;
 
+wifi_dbus *g_dbus_h = NULL;
+
 bool _wifi_is_init(void)
 {
        return is_init;
@@ -200,12 +205,11 @@ static int __libnet_update_profile_iterator(void)
 
 static void __libnet_update_specific_profile_iterator(GSList *ap_list)
 {
-    int count=0;
-    GSList *list = ap_list;
+       int count = 0;
+       GSList *list = ap_list;
 
-    for (count = 0; list; list = list->next) {
+       for (count = 0; list; list = list->next)
                count++;
-    }
 
        if (count == 0) {
                WIFI_LOG(WIFI_INFO, "No hidden AP found\n");
@@ -621,7 +625,7 @@ static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
                        return;
                case NET_ERR_ACTIVE_CONNECTION_EXISTS:
                        return;
-               default :
+               default:
                        break;
                }
 
@@ -713,7 +717,7 @@ static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
        case NET_EVENT_WIFI_POWER_IND:
                __libnet_power_on_off_cb(event_cb, is_requested);
                break;
-       default :
+       default:
                break;
        }
 }
@@ -868,43 +872,42 @@ int _wifi_libnet_get_wifi_device_state(wifi_device_state_e *device_state)
        return WIFI_ERROR_NONE;
 }
 
-int _wifi_libnet_get_wifi_state(wifi_connection_state_econnection_state)
+int _wifi_libnet_get_wifi_state(wifi_connection_state_e *connection_state)
 {
-       int rv;
-       net_wifi_state_t wlan_state = 0;
-       net_profile_name_t profile_name;
+       wifi_dbus *dbus_h = NULL;
+       GError *error = NULL;
+       GVariant *result = NULL;
+       gint state = 0;
+
+       dbus_h = _wifi_get_dbus_handle();
+       if (dbus_h == NULL) {
+               WIFI_LOG(WIFI_ERROR, "Not initialized for wifi dbus connection");
+               return WIFI_ERROR_INVALID_OPERATION;
+       }
 
-       rv = net_get_wifi_state(&wlan_state, &profile_name);
-       if (rv == NET_ERR_ACCESS_DENIED) {
-               WIFI_LOG(WIFI_ERROR, "Access denied");
-               return WIFI_ERROR_PERMISSION_DENIED;
-       } else if (rv != NET_ERR_NONE) {
-               WIFI_LOG(WIFI_ERROR, "Failed to get Wi-Fi state");
+       result = g_dbus_connection_call_sync(dbus_h->dbus_conn,
+                                            NETCONFIG_SERVICE,
+                                            NETCONFIG_WIFI_PATH,
+                                            NETCONFIG_IWIFI,
+                                            "GetWifiState",
+                                            g_variant_new("()"),
+                                            NULL, G_DBUS_CALL_FLAGS_NONE,
+                                            DBUS_REPLY_TIMEOUT, dbus_h->ca,
+                                            &error);
+
+       if (error) {
+               WIFI_LOG(WIFI_ERROR, "Fail to GetWifiState [%d: %s]", error->code, error->message);
+               g_error_free(error);
                return WIFI_ERROR_OPERATION_FAILED;
        }
 
-       switch (wlan_state) {
-       case WIFI_OFF:
-       case WIFI_ON:
-               *connection_state = WIFI_CONNECTION_STATE_DISCONNECTED;
-               break;
-       case WIFI_ASSOCIATION:
-               *connection_state = WIFI_CONNECTION_STATE_ASSOCIATION;
-               break;
-       case WIFI_CONFIGURATION:
-               *connection_state = WIFI_CONNECTION_STATE_CONFIGURATION;
-               break;
-       case WIFI_CONNECTED:
-               *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
-               break;
-       case WIFI_DISCONNECTING:
-               *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
-               break;
-       default :
-               WIFI_LOG(WIFI_ERROR, "Unknown state");
-               return WIFI_ERROR_OPERATION_FAILED;
+       if (result != NULL) {
+               g_variant_get(result, "(i)", &state);
+               g_variant_unref(result);
        }
 
+       *connection_state = state;
+
        return WIFI_ERROR_NONE;
 }
 
@@ -951,12 +954,12 @@ int _wifi_libnet_scan_request(wifi_scan_finished_cb callback, void *user_data)
 }
 
 int _wifi_libnet_scan_specific_ap(const char *essid,
-                                       wifi_scan_finished_cb callback, void *user_data)
+                                                       wifi_scan_finished_cb callback, void *user_data)
 {
        int rv;
-    rv = net_specific_scan_wifi(essid);
+       rv = net_specific_scan_wifi(essid);
 
-    if (rv == NET_ERR_NONE) {
+       if (rv == NET_ERR_NONE) {
                g_strlcpy(specific_profile_essid, essid, NET_WLAN_ESSID_LEN+1);
                __libnet_set_specific_scan_cb(callback, user_data);
                return WIFI_ERROR_NONE;
@@ -1284,6 +1287,8 @@ guint _wifi_callback_add(GSourceFunc func, gpointer user_data)
 {
        guint id;
        struct managed_idle_data *data;
+       GMainContext *context;
+       GSource *src;
 
        if (!func)
                return 0;
@@ -1295,8 +1300,12 @@ guint _wifi_callback_add(GSourceFunc func, gpointer user_data)
        data->func = func;
        data->user_data = user_data;
 
-       id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __wifi_idle_cb, data,
-                       __wifi_idle_destroy_cb);
+       context = g_main_context_get_thread_default();
+       src = g_idle_source_new();
+       g_source_set_callback(src, __wifi_idle_cb, data, __wifi_idle_destroy_cb);
+       id = g_source_attach(src, context);
+       g_source_unref(src);
+
        if (!id) {
                g_free(data);
                return id;
@@ -1330,3 +1339,55 @@ void _wifi_callback_cleanup(void)
        g_slist_free(managed_idler_list);
        managed_idler_list = NULL;
 }
+
+int _wifi_check_feature_supported(const char *feature_name)
+{
+       if (is_feature_checked) {
+               if (!feature_supported) {
+                       LOGE("%s feature is disabled", feature_name);
+                       return WIFI_ERROR_NOT_SUPPORTED;
+               }
+       } else {
+               if (!system_info_get_platform_bool(feature_name, &feature_supported)) {
+                       is_feature_checked = true;
+                       if (!feature_supported) {
+                               LOGE("%s feature is disabled", feature_name);
+                               return WIFI_ERROR_NOT_SUPPORTED;
+                       }
+               } else {
+                       LOGE("Error - Feature getting from System Info");
+                       return WIFI_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       return WIFI_ERROR_NONE;
+}
+
+int _wifi_dbus_init(void)
+{
+       int rv;
+
+       rv = wifi_dbus_init(&g_dbus_h);
+       if (rv != NET_ERR_NONE)
+               return rv;
+
+       return NET_ERR_NONE;
+}
+
+int _wifi_dbus_deinit(void)
+{
+       wifi_dbus_deinit(g_dbus_h);
+       g_dbus_h = NULL;
+
+       return NET_ERR_NONE;
+}
+
+wifi_dbus *_wifi_get_dbus_handle(void)
+{
+       if (g_dbus_h == NULL) {
+               WIFI_LOG(WIFI_ERROR, "g_dbus_h is NULL");
+               return NULL;
+       }
+
+       return g_dbus_h;
+}