Handling le connection interval
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-obex-agent.c
index 174373e..f8f3af7 100644 (file)
@@ -1,11 +1,5 @@
 /*
- * Bluetooth-frwk
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
- *              Girishashok Joshi <girish.joshi@samsung.com>
- *              Chanyeol Park <chanyeol.park@samsung.com>
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "bt-service-event.h"
 #include "bt-service-util.h"
 #include "bt-service-obex-agent.h"
+#include "bt-service-obex-server.h"
 #include "marshal.h"
 
 static GDBusConnection *conn = NULL;
+static GDBusConnection *auth_reply_conn = NULL;
 static GSList *obex_agent_list = NULL;
 
 typedef struct {
@@ -42,6 +38,7 @@ typedef struct {
 
        int openobex_id;
        int obex_agent_id;
+       int obex_reply_id;
 
        /* callback data */
        gpointer authorize_data;
@@ -90,7 +87,7 @@ static const gchar obex_service_agent_xml1[] =
 "    <method name='Complete'>"
 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
 "      <arg type='o' name='transfer'/>"
- "   </method>"
+"   </method>"
 "    <method name='Release'>"
 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
 "    </method>"
@@ -123,6 +120,15 @@ static const gchar obex_service_agent_xml2[] =
 "  </interface>"
 "</node>";
 
+static const gchar obex_service_reply_xml3[] =
+"<node name='/'>"
+"      <interface name='org.bluez.obex.reply'>"
+"              <method name='ReplyAuthorize'>"
+"                      <arg type='u' name='accept' direction='in'/>"
+"              </method>"
+"      </interface>"
+"</node>";
+
 static bt_obex_agent_info *__find_obex_agent_info(char *path)
 {
        GSList *l;
@@ -188,9 +194,8 @@ static void __new_connection_method(GDBusConnection *connection,
                if (info->name == NULL) {
                        info->name = sender;
                } else {
-                       if (g_strcmp0(sender, info->name) != 0) {
+                       if (g_strcmp0(sender, info->name) != 0)
                                goto fail;
-                       }
                }
 
                if (info->request_cb == NULL)
@@ -234,9 +239,8 @@ static void __new_connection_method(GDBusConnection *connection,
 
                sender = (char *)g_dbus_method_invocation_get_sender(invocation);
 
-               if (g_strcmp0(sender, info->name) != 0) {
+               if (g_strcmp0(sender, info->name) != 0)
                        goto fail;
-               }
 
                if (info->progress_cb == NULL)
                        goto fail;
@@ -279,9 +283,8 @@ static void __new_connection_method(GDBusConnection *connection,
 
                sender = (char *)g_dbus_method_invocation_get_sender(invocation);
 
-               if (g_strcmp0(sender, info->name) != 0) {
+               if (g_strcmp0(sender, info->name) != 0)
                        goto fail;
-               }
 
                if (info->error_cb == NULL)
                        goto fail;
@@ -319,9 +322,8 @@ static void __new_connection_method(GDBusConnection *connection,
 
                sender = (char *)g_dbus_method_invocation_get_sender(invocation);
 
-               if (g_strcmp0(sender, info->name) != 0) {
+               if (g_strcmp0(sender, info->name) != 0)
                        goto fail;
-               }
 
                if (info->complete_cb == NULL)
                        goto fail;
@@ -358,9 +360,8 @@ static void __new_connection_method(GDBusConnection *connection,
                if (info->name) {
                        /*In H2 if user denies auth,release will come without request and hence
                        info->name will be NULL */
-                       if (g_strcmp0(sender, info->name) != 0) {
+                       if (g_strcmp0(sender, info->name) != 0)
                                goto fail;
-                       }
                }
 
                if (info->release_cb == NULL)
@@ -369,6 +370,19 @@ static void __new_connection_method(GDBusConnection *connection,
                info->release_cb(invocation, info->release_data);
 
                return;
+       } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
+               const guint accept;
+
+               g_variant_get(parameters, "(u)", &accept);
+               BT_DBG("Accept: %d", accept);
+
+               if (accept == 0) {
+                       BT_DBG("Transfer accepted");
+                       _bt_obex_server_reply_accept();
+               } else {
+                       BT_ERR("Transfer denied");
+                       _bt_obex_server_reject_authorize();
+               }
        }
 fail:
                BT_ERR("Fail case");
@@ -387,15 +401,19 @@ void _bt_obex_agent_new(char *path)
                        g_error_free(error);
                        return;
                }
+               auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+               if (error != NULL) {
+                       BT_ERR("Fail to get dbus: %s", error->message);
+                       g_error_free(error);
+                       return;
+               }
        }
-       info = (bt_obex_agent_info *)malloc (sizeof(bt_obex_agent_info));
-       if (info == NULL) {
-               BT_ERR("Failed to allocate memory");
-               return;
+       info = (bt_obex_agent_info *)malloc(sizeof(bt_obex_agent_info));
+       if (info) {
+               memset(info, 0, sizeof(bt_obex_agent_info));
+               info->path = g_strdup(path);
+               obex_agent_list = g_slist_append(obex_agent_list, info);
        }
-       memset(info, 0, sizeof(bt_obex_agent_info));
-       info->path = g_strdup(path);
-       obex_agent_list = g_slist_append(obex_agent_list, info);
 }
 
 void _bt_obex_agent_destroy(char *path)
@@ -417,6 +435,9 @@ void _bt_obex_agent_destroy(char *path)
        if (info->obex_agent_id)
                g_dbus_connection_unregister_object(conn,
                        info->obex_agent_id);
+       if (info->obex_reply_id)
+               g_dbus_connection_unregister_object(auth_reply_conn,
+                                       info->obex_reply_id);
 
        g_free(info);
 }
@@ -424,7 +445,8 @@ gboolean _bt_obex_setup(const char *path)
 {
        bt_obex_agent_info *info;
        GDBusProxy *proxy;
-       GDBusNodeInfo *new_conn_node;
+       GDBusNodeInfo *new_conn_node = NULL;
+       GDBusNodeInfo *auth_reply_node = NULL;
        GError *err = NULL;
 
        info = __find_obex_agent_info((char *)path);
@@ -450,6 +472,8 @@ gboolean _bt_obex_setup(const char *path)
        }
 
        new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
+       if (new_conn_node == NULL)
+               return FALSE;
 
        info->openobex_id = g_dbus_connection_register_object(conn, info->path,
                                                new_conn_node->interfaces[0],
@@ -457,13 +481,16 @@ gboolean _bt_obex_setup(const char *path)
                                                NULL, NULL, &err);
        g_dbus_node_info_unref(new_conn_node);
        if (err) {
-               BT_INFO("Dbus Err: %s", err->message);
+               BT_ERR("Dbus Err: %s", err->message);
                g_clear_error(&err);
                return FALSE;
        }
        if (info->openobex_id == 0)
                BT_ERR("Error while registering object");
+
        new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
+       if (new_conn_node == NULL)
+               return FALSE;
 
        info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
                                                new_conn_node->interfaces[0],
@@ -473,7 +500,25 @@ gboolean _bt_obex_setup(const char *path)
        if (info->obex_agent_id == 0)
                BT_ERR("Error while registering object");
        if (err) {
-               BT_INFO("Dbus Err: %s", err->message);
+               BT_ERR("Dbus Err: %s", err->message);
+               g_clear_error(&err);
+               return FALSE;
+       }
+
+       auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
+
+       if (auth_reply_node == NULL) {
+               BT_ERR("Fail to create the node info for xml");
+               return FALSE;
+       }
+
+       info->obex_reply_id = g_dbus_connection_register_object(auth_reply_conn, info->path,
+               auth_reply_node->interfaces[0], &method_table, NULL, NULL, &err);
+       g_dbus_node_info_unref(auth_reply_node);
+       if (info->obex_reply_id == 0)
+               BT_ERR("Error while registering object");
+       if (err) {
+               BT_ERR("Dbus Err: %s", err->message);
                g_clear_error(&err);
                return FALSE;
        }
@@ -484,6 +529,8 @@ void _bt_obex_set_authorize_cb(char *object_path,
                         bt_obex_authorize_cb func, gpointer data)
 {
        bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
+       if (info == NULL)
+               return;
 
        info->authorize_cb = func;
        info->authorize_data = data;
@@ -493,8 +540,6 @@ void _bt_obex_set_release_cb(char *object_path,
                       bt_obex_release_cb func, gpointer data)
 {
        bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
-
-       /* Fix : NULL_RETURNS */
        if (info == NULL)
                return;
 
@@ -506,8 +551,6 @@ void _bt_obex_set_request_cb(char *object_path,
                       bt_obex_request_cb func, gpointer data)
 {
        bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
-
-       /* Fix : NULL_RETURNS */
        if (info == NULL)
                return;
 
@@ -519,8 +562,6 @@ void _bt_obex_set_progress_cb(char *object_path,
                        bt_obex_progress_cb func, gpointer data)
 {
        bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
-
-       /* Fix : NULL_RETURNS */
        if (info == NULL)
                return;
 
@@ -531,9 +572,7 @@ void _bt_obex_set_progress_cb(char *object_path,
 void _bt_obex_set_complete_cb(char *object_path,
                        bt_obex_complete_cb func, gpointer data)
 {
-       bt_obex_agent_info *info =__find_obex_agent_info(object_path);;
-
-       /* Fix : NULL_RETURNS */
+       bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
        if (info == NULL)
                return;
 
@@ -545,8 +584,6 @@ void _bt_obex_set_error_cb(char *object_path,
                        bt_obex_error_cb func, gpointer data)
 {
        bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
-
-       /* Fix : NULL_RETURNS */
        if (info == NULL)
                return;