Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-obex-server.c
index a8797fa..6b0a7ff 100644 (file)
@@ -20,7 +20,7 @@
 #include <dlog.h>
 #include <string.h>
 #include <dirent.h>
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
 #include "bt-service-dpm.h"
 #endif
 
 #define BT_OBEX_MANAGER "org.bluez.obex.AgentManager1"
 #define BT_OBEX_PATH "/org/bluez/obex"
 
+#define BT_OBEX_PATH_PREFIX "/opt/usr/media"
+#define BT_OBEX_DEFAULT_PATH "/opt/usr/home/owner/media"
+#define BT_OBEX_PATH_MAX_LENGTH 255
+
 
 typedef struct {
        char *filename;
@@ -243,8 +247,9 @@ void _bt_obex_check_pending_transfer(const char *address)
 static char *__bt_get_remote_device_name(const char *bdaddress)
 {
        char *device_path = NULL;
-       char *name = NULL;
-       GVariant *value;
+       const gchar *name = NULL;
+       gchar *dev_name = NULL;
+       gsize name_len = 0;
        GVariant *result = NULL;
        GError *err = NULL;
        GDBusProxy *device_proxy;
@@ -255,7 +260,7 @@ static char *__bt_get_remote_device_name(const char *bdaddress)
        device_path = _bt_get_device_object_path((char *)bdaddress);
        retv_if(device_path == NULL, NULL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, NULL);
        BT_INFO("Device_path %s", device_path);
        device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
@@ -267,42 +272,44 @@ static char *__bt_get_remote_device_name(const char *bdaddress)
        g_free(device_path);
        retv_if(device_proxy == NULL, NULL);
 
-       result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
-                       g_variant_new("(s)", BT_DEVICE_INTERFACE),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       DBUS_TIMEOUT, NULL,
-                       &err);
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
+                       G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, &err);
        if (err) {
                BT_ERR("DBus Error : %s", err->message);
                g_clear_error(&err);
-               return NULL;
-       }
-       if (result == NULL) {
-               BT_ERR("g_dbus_proxy_call_sync function return NULL");
-               return NULL;
+       } else {
+               GVariant *value;
+               g_variant_get(result, "(v)", &value);
+               name = g_variant_get_string(value, &name_len);
+               INFO_SECURE("Alias Name [%s]", name);
+               if (name_len)
+                       dev_name = g_strdup(name);
+               g_variant_unref(value);
+               g_variant_unref(result);
        }
-       g_variant_get(result, "(@a{sv})", &value);
-
-       if (value) {
-               GVariant *temp_value = g_variant_lookup_value(value, "Alias",
-                       G_VARIANT_TYPE_STRING);
-               g_variant_get(temp_value, "s", &name);
-               if (temp_value)
-                       g_variant_unref(temp_value);
 
-               if (name != NULL)
-                       DBG_SECURE("Alias Name [%s]", name);
-               else {
-                       temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
-                       g_variant_get(temp_value, "s", &name);
-                       if (temp_value)
-                               g_variant_unref(temp_value);
-                       DBG_SECURE("Name = %s", name);
+       if (name_len == 0) {
+               result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+               if (err) {
+                       ERR("DBus Error : %s", err->message);
+                       g_clear_error(&err);
+               } else {
+                       GVariant *value;
+                       g_variant_get(result, "(v)", &value);
+                       name = g_variant_get_string(value, &name_len);
+                       INFO_SECURE("Name = %s", name);
+                       if (name_len)
+                               dev_name = g_strdup(name);
+                       g_variant_unref(value);
+                       g_variant_unref(result);
                }
        }
-       g_variant_unref(result);
+
        g_object_unref(device_proxy);
-       return name;
+       return dev_name;
 }
 
 static void __bt_get_remote_device_name_authinfo(const char *bdaddress,
@@ -322,7 +329,7 @@ static void __bt_get_remote_device_name_authinfo(const char *bdaddress,
        device_path = _bt_get_device_object_path((char *)bdaddress);
        ret_if(device_path == NULL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        ret_if(conn == NULL);
        BT_INFO("Device_path %s", device_path);
        device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
@@ -366,7 +373,7 @@ static void __bt_get_remote_device_name_authinfo(const char *bdaddress,
                                g_variant_unref(temp_value);
                        DBG_SECURE("Name = %s", name);
                }
-               temp_value = g_variant_lookup_value (value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
+               temp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
                if (temp_value) {
                        is_alias_set = g_variant_get_boolean(temp_value);
                        g_variant_unref(temp_value);
@@ -403,7 +410,7 @@ static GDBusProxy *__bt_get_transfer_proxy(const char *transfer_path)
        GDBusProxy *proxy;
        GError *err = NULL;
 
-       conn = _bt_get_session_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, NULL);
 
        proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
@@ -412,7 +419,7 @@ static GDBusProxy *__bt_get_transfer_proxy(const char *transfer_path)
                                        BT_OBEX_TRANSFER_INTERFACE,
                                        NULL, &err);
 
-       if (err) {
+       if (proxy == NULL && err) {
                BT_ERR("Error : %s", err->message);
                g_clear_error(&err);
                return NULL;
@@ -426,7 +433,7 @@ static GDBusProxy *__bt_get_transfer_properties_proxy(const char *transfer_path)
        GDBusConnection *conn;
        GDBusProxy *proxy;
        GError *err = NULL;
-       conn = _bt_get_session_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, NULL);
 
        proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
@@ -434,7 +441,7 @@ static GDBusProxy *__bt_get_transfer_properties_proxy(const char *transfer_path)
                                        transfer_path,
                                        BT_PROPERTIES_INTERFACE,
                                        NULL, &err);
-       if (err) {
+       if (proxy == NULL && err) {
                BT_ERR("Error : %s", err->message);
                g_clear_error(&err);
                return NULL;
@@ -531,6 +538,9 @@ static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
        GVariant *param = NULL;
        GError *err = NULL;
        bt_session_info_t *session_info = NULL;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int value = DPM_BT_ERROR;
+#endif
 
        BT_DBG(" path [%s] \n", path);
 
@@ -569,8 +579,9 @@ static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
 
        agent_info.auth_info->transfer_path = g_strdup(path);
 
-#ifdef TIZEN_DPM_ENABLE
-       if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_HANDSFREE_ONLY) {
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_allow_bluetooth_mode(&value);
+       if (value == DPM_BT_HANDSFREE_ONLY) {
                /* Free auth info in next function */
                _bt_obex_server_reject_authorize();
                return FALSE;
@@ -581,13 +592,12 @@ static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
                GVariant *val;
                gsize len = 0;
                while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
-                       if (g_strcmp0(key, "Name") == 0) {
+                       if (g_strcmp0(key, "Name") == 0)
                                agent_info.auth_info->filename = g_variant_dup_string(val, &len);
-                       } else if (g_strcmp0(key, "Address") == 0) {
+                       else if (g_strcmp0(key, "Address") == 0)
                                bdaddress = g_variant_dup_string(val, &len);
-                       } else if (g_strcmp0(key, "Size") == 0) {
+                       else if (g_strcmp0(key, "Size") == 0)
                                agent_info.auth_info->file_size = g_variant_get_uint64(val);
-                       }
                }
                g_variant_iter_free(iter);
        }
@@ -602,7 +612,7 @@ static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
        memcpy(agent_info.auth_info->contact_auth_info, auth_info, 5);
 
        session_info = __bt_find_session_by_path((char *)path);
-       if(NULL == session_info) {
+       if (NULL == session_info) {
                session_info = g_malloc0(sizeof(bt_session_info_t));
                session_info->path = g_strdup(path);
                session_info->address = g_strdup(bdaddress);
@@ -619,17 +629,18 @@ static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
                return TRUE;
        }
 
-       if(session_info->authorized == FALSE) {
-               _bt_launch_system_popup(BT_AGENT_EVENT_EXCHANGE_REQUEST, device_name,
+       if (session_info->authorized == FALSE) {
+               if (headed_plugin_info->plugin_headed_enabled)
+                       headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_EXCHANGE_REQUEST, device_name,
                                        auth_info, NULL, NULL, BT_OBEX_SERVER_AGENT_PATH);
        } else {
                param = g_variant_new("(istss)", result,
-                       agent_info.auth_info->filename,
-                       agent_info.auth_info->file_size,
-                       agent_info.auth_info->address,
-                       agent_info.auth_info->device_name);
+                               agent_info.auth_info->filename,
+                               agent_info.auth_info->file_size,
+                               agent_info.auth_info->address,
+                               agent_info.auth_info->device_name);
                _bt_send_event(BT_OPP_SERVER_EVENT,
-                       BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
+                               BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
        }
 
        return TRUE;
@@ -704,8 +715,10 @@ done:
        BT_DBG("Transfer id %d", transfer_info->transfer_id);
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
-       for (i = 0; i < 5; i++)
-               g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
+       for (i = 0; i < 5; i++) {
+               if (agent_info.auth_info)
+                       g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
+       }
 
        param = g_variant_new("(isssstii(ay))", result,
                                transfer_info->device_name,
@@ -720,7 +733,7 @@ done:
        __bt_free_auth_info(agent_info.auth_info);
        agent_info.auth_info = NULL;
 
-       g_variant_builder_unref (builder);
+       g_variant_builder_unref(builder);
 
        _bt_send_event(BT_OPP_SERVER_EVENT,
                BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
@@ -806,9 +819,9 @@ void _bt_obex_transfer_completed(const char *transfer_path, gboolean success)
                        g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
 
                param = g_variant_new("(isssstii(ay))", result,
+                                       transfer_info->device_name,
                                        transfer_info->filename,
                                        transfer_info->type,
-                                       transfer_info->device_name,
                                        transfer_info->address,
                                        transfer_info->file_size,
                                        transfer_info->transfer_id,
@@ -817,7 +830,7 @@ void _bt_obex_transfer_completed(const char *transfer_path, gboolean success)
                _bt_send_event(BT_OPP_SERVER_EVENT,
                        BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
                        param);
-               g_variant_builder_unref (builder);
+               g_variant_builder_unref(builder);
        }
        param = g_variant_new("(issssstii)", result,
                                transfer_info->filename,
@@ -894,7 +907,7 @@ int _bt_register_obex_server(void)
        GError *g_error = NULL;
 
        /* Get the session bus. */
-       g_conn = _bt_get_session_gconn();
+       g_conn = _bt_gdbus_get_system_gconn();
        retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_obex_agent_new(BT_OBEX_SERVER_AGENT_PATH);
@@ -910,9 +923,8 @@ int _bt_register_obex_server(void)
                                                BT_OBEX_MANAGER,
                                                NULL, &g_error);
 
-       if (manager_proxy == NULL) {
+       if (manager_proxy == NULL)
                return BLUETOOTH_ERROR_INTERNAL;
-       }
 
        result = g_dbus_proxy_call_sync(manager_proxy, "RegisterAgent",
                                g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
@@ -981,35 +993,61 @@ gboolean __bt_check_folder_path(const char *dest_path)
        return TRUE;
 }
 
+char *__bt_transfer_folder_path(char *dest_path)
+{
+       char *dst_path = (char *)g_malloc0(BT_OBEX_PATH_MAX_LENGTH);
+       if (g_str_has_prefix(dest_path, BT_OBEX_PATH_PREFIX))
+               snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, BT_OBEX_DEFAULT_PATH"%s", dest_path + strlen(BT_OBEX_PATH_PREFIX));
+       else
+               snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, "%s", dest_path);
+
+       BT_INFO("obex transfed path : %s", dst_path);
+       return dst_path;
+}
+
 int _bt_obex_server_allocate(char *sender, const char *dest_path, int app_pid, gboolean is_native)
 {
        BT_DBG("+");
-       if (__bt_check_folder_path(dest_path) == FALSE)
+
+       char *dst_path;
+       dst_path = __bt_transfer_folder_path((char *)dest_path);
+
+       if (__bt_check_folder_path(dst_path) == FALSE) {
+               g_free(dst_path);
                return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
 
        if (is_native == TRUE) {
-               retv_if(agent_info.native_server,
-                               BLUETOOTH_ERROR_DEVICE_BUSY);
+               if (agent_info.native_server) {
+                       BT_ERR("obex native server busy");
+                       g_free(dst_path);
+                       return BLUETOOTH_ERROR_DEVICE_BUSY;
+               }
 
                /* Force to change the control to native */
                agent_info.native_server = g_malloc0(sizeof(bt_server_info_t));
-               agent_info.native_server->dest_path = g_strdup(dest_path);
+               agent_info.native_server->dest_path = g_strdup(dst_path);
                agent_info.native_server->sender = g_strdup(sender);
                agent_info.native_server->app_pid = app_pid;
                agent_info.server_type = BT_NATIVE_SERVER;
                _bt_unregister_osp_server_in_agent(BT_OBEX_SERVER, NULL);
        } else {
-               retv_if(agent_info.custom_server,
-                               BLUETOOTH_ERROR_DEVICE_BUSY);
+               if (agent_info.custom_server) {
+                       BT_ERR("obex custom server busy");
+                       g_free(dst_path);
+                       return BLUETOOTH_ERROR_DEVICE_BUSY;
+               }
 
                /* Force to change the control to custom */
                agent_info.custom_server = g_malloc0(sizeof(bt_server_info_t));
-               agent_info.custom_server->dest_path = g_strdup(dest_path);
+               agent_info.custom_server->dest_path = g_strdup(dst_path);
                agent_info.custom_server->sender = g_strdup(sender);
                agent_info.custom_server->app_pid = app_pid;
                agent_info.server_type = BT_CUSTOM_SERVER;
                _bt_register_osp_server_in_agent(BT_OBEX_SERVER, NULL, NULL, -1);
        }
+
+       g_free(dst_path);
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
@@ -1101,11 +1139,11 @@ void _bt_obex_server_reply_accept(void)
        _bt_send_event(BT_OPP_SERVER_EVENT, BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
 
        session_info = __bt_find_session_by_path(agent_info.auth_info->transfer_path);
-       if(NULL == session_info) {
+
+       if (NULL == session_info)
                BT_ERR("Couldn't get the session info from the list");
-       } else {
+       else
                session_info->authorized = TRUE;
-       }
 }
 
 int _bt_obex_server_reject_authorize(void)
@@ -1133,15 +1171,18 @@ int _bt_obex_server_set_destination_path(const char *dest_path,
                                                gboolean is_native)
 {
        bt_server_info_t *server_info;
-
        BT_CHECK_PARAMETER(dest_path, return);
 
+       char *dst_path;
+       dst_path = __bt_transfer_folder_path((char *)dest_path);
+
        DIR *dp = NULL;
 
-       dp = opendir(dest_path);
+       dp = opendir(dst_path);
 
        if (dp == NULL) {
                BT_ERR("The directory does not exist");
+               g_free(dst_path);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
@@ -1152,12 +1193,16 @@ int _bt_obex_server_set_destination_path(const char *dest_path,
        else
                server_info = agent_info.custom_server;
 
-       retv_if(server_info == NULL,
-                       BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
+       if (!server_info) {
+               BT_ERR("obex server info is NULL");
+               g_free(dst_path);
+               return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
+       }
 
        g_free(server_info->dest_path);
-       server_info->dest_path = g_strdup(dest_path);
+       server_info->dest_path = g_strdup(dst_path);
 
+       g_free(dst_path);
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -1171,19 +1216,26 @@ int _bt_obex_server_set_root(const char *root)
 
        BT_CHECK_PARAMETER(root, return);
 
-       retv_if(agent_info.proxy == NULL,
-                               BLUETOOTH_ERROR_INTERNAL);
+       char *dst_root;
+       dst_root = __bt_transfer_folder_path((char *)root);
+
+       if (!agent_info.proxy) {
+               BT_ERR("obex agent_info proxy error");
+               g_free(dst_root);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
 
-       dp = opendir(root);
+       dp = opendir(dst_root);
 
        if (dp == NULL) {
                BT_ERR("The directory does not exist");
+               g_free(dst_root);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
        closedir(dp);
 
-       folder = g_variant_new_string(root);
+       folder = g_variant_new_string(dst_root);
        result = g_dbus_proxy_call_sync(agent_info.proxy, "SetProperty",
                        g_variant_new("(sv)", string, folder),
                        G_DBUS_CALL_FLAGS_NONE,
@@ -1193,12 +1245,14 @@ int _bt_obex_server_set_root(const char *root)
        if (g_error) {
                BT_ERR("SetProperty Fail: %s", g_error->message);
                g_error_free(g_error);
+               g_free(dst_root);
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
        if (result)
                g_variant_unref(result);
 
+       g_free(dst_root);
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -1253,11 +1307,10 @@ int _bt_obex_server_is_activated(gboolean *activated)
 {
        BT_CHECK_PARAMETER(activated, return);
 
-       if (agent_info.custom_server) {
+       if (agent_info.custom_server)
                *activated = TRUE;
-       } else {
+       else
                *activated = FALSE;
-       }
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -1266,11 +1319,10 @@ int _bt_obex_server_check_allocation(gboolean *allocation)
 {
        BT_CHECK_PARAMETER(allocation, return);
 
-       if (agent_info.native_server || agent_info.custom_server) {
+       if (agent_info.native_server || agent_info.custom_server)
                *allocation = TRUE;
-       } else {
+       else
                *allocation = FALSE;
-       }
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -1300,11 +1352,10 @@ int _bt_obex_server_is_receiving(gboolean *receiving)
 {
        BT_CHECK_PARAMETER(receiving, return);
 
-       if (transfers == NULL || g_slist_length(transfers) == 0) {
+       if (transfers == NULL || g_slist_length(transfers) == 0)
                *receiving = FALSE;
-       } else {
+       else
                *receiving = TRUE;
-       }
 
        return BLUETOOTH_ERROR_NONE;
 }