Merge 2.3 code for sync #2
[platform/core/api/connection.git] / src / libnetwork.c
index b63a600..3f61754 100755 (executable)
@@ -18,6 +18,7 @@
 #include <string.h>
 #include <glib.h>
 #include <vconf/vconf.h>
+
 #include "net_connection_private.h"
 
 static GSList *prof_handle_list = NULL;
@@ -39,14 +40,37 @@ struct _libnet_s {
        connection_opened_cb opened_cb;
        connection_closed_cb closed_cb;
        connection_set_default_cb set_default_cb;
+       connection_reset_cb reset_profile_cb;
        void *opened_user_data;
        void *closed_user_data;
        void *set_default_user_data;
+       void *reset_profile_user_data;
        bool registered;
+       bool is_created;
+};
+
+struct managed_idle_data {
+       GSourceFunc func;
+       gpointer user_data;
+       guint id;
+};
+
+struct feature_type {
+       bool telephony;
+       bool wifi;
+       bool tethering_bluetooth;
 };
 
 static struct _profile_list_s profile_iterator = {0, 0, NULL};
 static struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL, false};
+static __thread GSList *managed_idler_list = NULL;
+static __thread bool is_check_enable_feature = false;
+static __thread struct feature_type enable_feature = {false, false, false};
+
+bool _connection_is_created(void)
+{
+       return libnet.is_created;
+}
 
 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
 {
@@ -71,6 +95,8 @@ static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
                return CONNECTION_ERROR_OPERATION_ABORTED;
        case NET_ERR_TIME_OUT:
                return CONNECTION_ERROR_NO_REPLY;
+       case NET_ERR_ACCESS_DENIED:
+               return CONNECTION_ERROR_PERMISSION_DENIED;
        default:
                return CONNECTION_ERROR_OPERATION_FAILED;
        }
@@ -132,6 +158,14 @@ static const char *__libnet_convert_cp_state_to_string(connection_profile_state_
        }
 }
 
+static void __libnet_set_reset_profile_cb(connection_opened_cb user_cb, void *user_data)
+{
+       if (user_cb != NULL) {
+               libnet.reset_profile_cb = user_cb;
+               libnet.reset_profile_user_data = user_data;
+       }
+}
+
 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
 {
        if (user_cb) {
@@ -140,6 +174,31 @@ static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data
        }
 }
 
+static gboolean __libnet_reset_profile_cb_idle(gpointer data)
+{
+       connection_error_e result = (connection_error_e)data;
+
+       if (libnet.reset_profile_cb != NULL)
+               libnet.reset_profile_cb(result, libnet.reset_profile_user_data);
+
+       libnet.reset_profile_cb = NULL;
+       libnet.reset_profile_user_data = NULL;
+
+       return FALSE;
+}
+
+static void __libnet_reset_profile_cb(connection_error_e result)
+{
+       if (_connection_is_created() != true) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
+                               "If multi-threaded, thread integrity be broken.");
+               return;
+       }
+
+       if (libnet.reset_profile_cb != NULL)
+               _connection_callback_add(__libnet_reset_profile_cb_idle, (gpointer)result);
+}
+
 static void __libnet_opened_cb(connection_error_e result)
 {
        if (libnet.opened_cb)
@@ -303,6 +362,10 @@ static void __libnet_evt_cb(net_event_info_t*  event_cb, void* user_data)
        case NET_EVENT_WIFI_WPS_RSP:
                CONNECTION_LOG(CONNECTION_INFO, "Got wifi WPS RSP\n");
                /* fall through */
+       case NET_EVENT_CELLULAR_RESET_DEFAULT_RSP:
+               result = __libnet_convert_to_cp_error_type(event_cb->Error);
+               CONNECTION_LOG(CONNECTION_INFO, "Got reset default profile RSP %d", result);
+               __libnet_reset_profile_cb(result);
        default :
                CONNECTION_LOG(CONNECTION_ERROR, "Error! Unknown Event\n\n");
                break;
@@ -415,8 +478,11 @@ int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
        net_profile_name_t profile_name;
 
        rv = net_get_wifi_state(&wlan_state, &profile_name);
-       if (rv != NET_ERR_NONE) {
-               CONNECTION_LOG(CONNECTION_ERROR, "Error!! net_get_wifi_state() failed.\n");
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi state[%d]", rv);
                return CONNECTION_ERROR_OPERATION_FAILED;
        }
 
@@ -442,12 +508,12 @@ int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
 
 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e* state)
 {
+       int rv;
        struct _profile_list_s ethernet_profiles = {0, 0, NULL};
-       net_get_profile_list(NET_DEVICE_ETHERNET, &ethernet_profiles.profiles, &ethernet_profiles.count);
-
-       if (ethernet_profiles.count == 0) {
-               *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
-               return CONNECTION_ERROR_NONE;
+       rv = net_get_profile_list(NET_DEVICE_ETHERNET, &ethernet_profiles.profiles, &ethernet_profiles.count);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
        }
 
        switch (ethernet_profiles.profiles->ProfileState) {
@@ -474,8 +540,13 @@ int _connection_libnet_get_ethernet_state(connection_ethernet_state_e* state)
 int _connection_libnet_get_bluetooth_state(connection_bt_state_e* state)
 {
        int i = 0;
+       int rv = 0;
        struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
-       net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
+       rv = net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       }
 
        if (bluetooth_profiles.count == 0) {
                *state = CONNECTION_BT_STATE_DEACTIVATED;
@@ -612,7 +683,10 @@ int _connection_libnet_get_current_profile(connection_profile_h *profile)
        rv = net_get_active_net_info(&active_profile);
        if (rv == NET_ERR_NO_SERVICE)
                return CONNECTION_ERROR_NO_CONNECTION;
-       else if (rv != NET_ERR_NONE)
+       else if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        *profile = g_try_malloc0(sizeof(net_profile_info_t));
@@ -625,16 +699,41 @@ int _connection_libnet_get_current_profile(connection_profile_h *profile)
        return CONNECTION_ERROR_NONE;
 }
 
+int _connection_libnet_reset_profile(connection_reset_option_e type,
+               connection_cellular_subscriber_id_e id, connection_reset_cb callback, void *user_data)
+{
+       int rv;
+
+       rv = net_reset_profile(type, id);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
+               return CONNECTION_ERROR_OPERATION_FAILED;
+       }
+
+       __libnet_set_reset_profile_cb(callback, user_data);
+
+       return CONNECTION_ERROR_NONE;
+}
+
 int _connection_libnet_open_profile(connection_profile_h profile, connection_opened_cb callback, void* user_data)
 {
+       int rv;
+
        if (!(_connection_libnet_check_profile_validity(profile))) {
-               CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
                return CONNECTION_ERROR_INVALID_PARAMETER;
        }
 
        net_profile_info_t *profile_info = profile;
 
-       if (net_open_connection_with_profile(profile_info->ProfileName) != NET_ERR_NONE)
+       rv = net_open_connection_with_profile(profile_info->ProfileName);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        __libnet_set_opened_cb(callback, user_data);
@@ -652,8 +751,13 @@ int _connection_libnet_get_cellular_service_profile(connection_cellular_service_
        struct _profile_list_s cellular_profiles = {0, 0, NULL};
 
        rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
-       if (rv != NET_ERR_NONE)
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Failed to get profile list (%d)", rv);
                return CONNECTION_ERROR_OPERATION_FAILED;
+       }
 
        for (;i < cellular_profiles.count;i++)
                if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
@@ -696,6 +800,8 @@ done:
 
 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
 {
+       int rv;
+
        if (!(_connection_libnet_check_profile_validity(profile))) {
                CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
                return CONNECTION_ERROR_INVALID_PARAMETER;
@@ -709,7 +815,11 @@ int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_ser
        if (service_type != type)
                return CONNECTION_ERROR_INVALID_PARAMETER;
 
-       if (net_set_default_cellular_service_profile(profile_info->ProfileName) != NET_ERR_NONE)
+       rv = net_set_default_cellular_service_profile(profile_info->ProfileName);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        return CONNECTION_ERROR_NONE;
@@ -718,6 +828,8 @@ int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_ser
 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
                        connection_profile_h profile, connection_set_default_cb callback, void* user_data)
 {
+       int rv;
+
        if (!(_connection_libnet_check_profile_validity(profile))) {
                CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
                return CONNECTION_ERROR_INVALID_PARAMETER;
@@ -731,7 +843,11 @@ int _connection_libnet_set_cellular_service_profile_async(connection_cellular_se
        if (service_type != type)
                return CONNECTION_ERROR_INVALID_PARAMETER;
 
-       if (net_set_default_cellular_service_profile_async(profile_info->ProfileName) != NET_ERR_NONE)
+       rv = net_set_default_cellular_service_profile_async(profile_info->ProfileName);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        __libnet_set_default_cb(callback, user_data);
@@ -741,6 +857,8 @@ int _connection_libnet_set_cellular_service_profile_async(connection_cellular_se
 
 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
 {
+       int rv;
+
        if (!(_connection_libnet_check_profile_validity(profile))) {
                CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
                return CONNECTION_ERROR_INVALID_PARAMETER;
@@ -748,6 +866,13 @@ int _connection_libnet_close_profile(connection_profile_h profile, connection_cl
 
        net_profile_info_t *profile_info = profile;
 
+       rv = net_close_connection(profile_info->ProfileName);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED;
+
        if (net_close_connection(profile_info->ProfileName) != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
@@ -758,6 +883,7 @@ int _connection_libnet_close_profile(connection_profile_h profile, connection_cl
 
 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
 {
+       int rv;
        char *endstr = strrchr(host_address, '.');
 
        if (endstr == NULL ||
@@ -769,7 +895,35 @@ int _connection_libnet_add_route(const char *interface_name, const char *host_ad
                return CONNECTION_ERROR_INVALID_PARAMETER;
        }
 
-       if (net_add_route(host_address, interface_name) != NET_ERR_NONE)
+       rv = net_add_route(host_address, interface_name);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED;
+
+       return CONNECTION_ERROR_NONE;
+}
+
+int _connection_libnet_remove_route(const char *interface_name, const char *host_address)
+{
+       int rv;
+       char *endstr = strrchr(host_address, '.');
+
+       if (endstr == NULL ||
+           strcmp(endstr, ".0") == 0 ||
+           strncmp(host_address, "0.", 2) == 0 ||
+           strstr(host_address, ".0.") != NULL ||
+           strstr(host_address, "255") != NULL) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed");
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
+
+       rv = net_remove_route(host_address, interface_name);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        return CONNECTION_ERROR_NONE;
@@ -817,7 +971,12 @@ bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile
 
 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
 {
-       if (net_set_statistics(device_type, statistics_type) != NET_ERR_NONE)
+       int rv;
+       rv = net_set_statistics(device_type, statistics_type);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
        return CONNECTION_ERROR_NONE;
@@ -825,9 +984,158 @@ int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_t
 
 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
 {
-       if (net_get_statistics(NET_DEVICE_WIFI, statistics_type, size) != NET_ERR_NONE)
-                       return CONNECTION_ERROR_OPERATION_FAILED;
+       int rv;
+       rv = net_get_statistics(NET_DEVICE_WIFI, statistics_type, size);
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       }else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED;
 
-               return CONNECTION_ERROR_NONE;
+       return CONNECTION_ERROR_NONE;
+}
+
+int _connection_libnet_set_cellular_subscriber_id(connection_profile_h profile,
+               connection_cellular_subscriber_id_e sim_id)
+{
+       char *modem_path = NULL;
+       net_profile_info_t *profile_info = (net_profile_info_t *)profile;
+
+       if (net_get_cellular_modem_object_path(&modem_path, sim_id) != NET_ERR_NONE) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Failed to get subscriber[%d]", sim_id);
+               return CONNECTION_ERROR_OPERATION_FAILED;
+       }
+
+       if (!modem_path) {
+               CONNECTION_LOG(CONNECTION_ERROR, "NULL modem object path");
+               return CONNECTION_ERROR_OPERATION_FAILED;
+       }
+
+       g_strlcpy(profile_info->ProfileInfo.Pdp.PSModemPath, modem_path,
+                               NET_PROFILE_NAME_LEN_MAX);
+       g_free(modem_path);
+
+       return CONNECTION_ERROR_NONE;
+}
+
+static void __connection_idle_destroy_cb(gpointer data)
+{
+       if (!data)
+               return;
+
+       managed_idler_list = g_slist_remove(managed_idler_list, data);
+       g_free(data);
+}
+
+static gboolean __connection_idle_cb(gpointer user_data)
+{
+       struct managed_idle_data *data = (struct managed_idle_data *)user_data;
+
+       if (!data)
+               return FALSE;
+
+       return data->func(data->user_data);
+}
+
+guint _connection_callback_add(GSourceFunc func, gpointer user_data)
+{
+       guint id;
+       struct managed_idle_data *data;
+
+       if (!func)
+               return 0;
+
+       data = g_try_new0(struct managed_idle_data, 1);
+       if (!data)
+               return 0;
+
+       data->func = func;
+       data->user_data = user_data;
+
+       id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, __connection_idle_cb, data,
+                       __connection_idle_destroy_cb);
+       if (!id) {
+               g_free(data);
+               return id;
+       }
+
+       data->id = id;
+
+       managed_idler_list = g_slist_append(managed_idler_list, data);
+
+       return id;
+}
+
+void _connection_callback_cleanup(void)
+{
+       GSList *cur = managed_idler_list;
+       GSource *src;
+       struct managed_idle_data *data;
+
+       while (cur) {
+               GSList *next = cur->next;
+               data = (struct managed_idle_data *)cur->data;
+
+               src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
+               if (src) {
+                       g_source_destroy(src);
+                       cur = managed_idler_list;
+               } else
+                       cur = next;
+       }
+
+       g_slist_free(managed_idler_list);
+       managed_idler_list = NULL;
 }
 
+int _connection_libnet_check_get_privilege()
+{
+       int rv;
+
+       rv = net_check_get_privilege();
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED;
+
+       return CONNECTION_ERROR_NONE;
+}
+
+int _connection_libnet_check_profile_privilege()
+{
+       int rv;
+
+       rv = net_check_profile_privilege();
+       if (rv == NET_ERR_ACCESS_DENIED) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
+               return CONNECTION_ERROR_PERMISSION_DENIED;
+       } else if (rv != NET_ERR_NONE)
+               return CONNECTION_ERROR_OPERATION_FAILED;
+
+       return CONNECTION_ERROR_NONE;
+}
+
+bool _connection_libnet_get_is_check_enable_feature()
+{
+       return is_check_enable_feature;
+}
+
+bool _connection_libnet_get_enable_feature_state(enable_feature_type_e feature_type)
+{
+       if(is_check_enable_feature){
+               switch(feature_type) {
+               case FEATURE_TYPE_TELEPHONY:
+                       return enable_feature.telephony;
+               case FEATURE_TYPE_WIFI:
+                       return enable_feature.wifi;
+               case FEATURE_TYPE_TETHERING_BLUETOOTH:
+                       return enable_feature.tethering_bluetooth;
+               default:
+                       CONNECTION_LOG(CONNECTION_ERROR, "Invalid feature type");
+                       return false;
+               }
+       }
+       CONNECTION_LOG(CONNECTION_ERROR, "Not checked enable feature yet");
+       return false;
+}