Modify build feature names as Tizen 3.0 build option naming rule
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index ff3d748..d33e0f3 100644 (file)
@@ -21,6 +21,7 @@
 #include<stdlib.h>
 #include<unistd.h>
 #include<stdint.h>
+#include<stdbool.h>
 
 #include "bt-common.h"
 #include "bt-internal-types.h"
@@ -34,6 +35,7 @@ static gboolean new_service = FALSE;
 static gboolean new_char = FALSE;
 static int serv_id = 1;
 static int register_pending_cnt = 0;
+static bool is_server_started = false;
 
 /* Introspection data for the service we are exporting */
 static const gchar service_introspection_xml[] =
@@ -187,7 +189,7 @@ static gchar *app_path = NULL;
 #define GATT_CHAR_INTERFACE            "org.bluez.GattCharacteristic1"
 #define GATT_DESC_INTERFACE            "org.bluez.GattDescriptor1"
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
 #define PROPERTIES_CHANGED "PropertiesChanged"
@@ -204,7 +206,35 @@ static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info(
 
 static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id);
 
-#ifdef HPS_FEATURE
+static void __bt_gatt_close_gdbus_connection(void)
+{
+       GError *err = NULL;
+
+       BT_DBG("+");
+
+       ret_if(g_conn == NULL);
+
+       if (!g_dbus_connection_flush_sync(g_conn, NULL, &err)) {
+               BT_ERR("Fail to flush the connection: %s", err->message);
+               g_error_free(err);
+               err = NULL;
+       }
+
+       if (!g_dbus_connection_close_sync(g_conn, NULL, &err)) {
+               if (err) {
+                       BT_ERR("Fail to close the dbus connection: %s", err->message);
+                       g_error_free(err);
+               }
+       }
+
+       g_object_unref(g_conn);
+
+       g_conn = NULL;
+
+       BT_DBG("-");
+}
+
+#ifdef TIZEN_FEATURE_BT_HPS
 static int __bt_send_event_to_hps(int event, GVariant *var)
 {
        GError *error = NULL;
@@ -247,7 +277,7 @@ static int __bt_send_event_to_hps(int event, GVariant *var)
 
        msg = g_dbus_message_new_signal(BT_HPS_OBJECT_PATH, BT_HPS_INTERFACE_NAME, PROPERTIES_CHANGED);
        g_dbus_message_set_body(msg, parameters);
-       if (!g_dbus_connection_send_message(g_conn, msg,G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
+       if (!g_dbus_connection_send_message(g_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
                if (error != NULL) {
                        BT_ERR("D-Bus API failure: errCode[%x], \
                                        message[%s]",
@@ -269,7 +299,7 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                        GDBusMethodInvocation *invocation,
                                        gpointer user_data)
 {
-       GSList *l1;
+       GSList *l1 = NULL;
        int len = 0;
        int i = 0;
 
@@ -292,13 +322,19 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                        GVariantBuilder *svc_builder = NULL;
                        GVariantBuilder *inner_builder = NULL;
 
-                       if (register_pending_cnt > 1) {
+                       if (register_pending_cnt > 1)
                                l1 = g_slist_nth(gatt_services, len - register_pending_cnt);
-                       } else {
+                       else
                                l1 = g_slist_last(gatt_services);
-                       }
+
                        register_pending_cnt--;
 
+                       if (l1 == NULL) {
+                               BT_ERR("gatt service list is NULL");
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                               return;
+                       }
+
                        struct gatt_service_info *serv_info = l1->data;
                        if (serv_info == NULL) {
                                BT_ERR("service info value is NULL");
@@ -386,7 +422,7 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                /*Value*/
                                builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-                               if(char_info->char_value != NULL) {
+                               if (char_info->char_value != NULL) {
                                        for (i = 0; i < char_info->value_length; i++) {
                                                g_variant_builder_add(builder1, "y",
                                                        char_info->char_value[i]);
@@ -481,7 +517,7 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                        /*Value*/
                                        builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-                                       if(desc_info->desc_value != NULL) {
+                                       if (desc_info->desc_value != NULL) {
                                                for (i = 0; i < desc_info->value_length; i++) {
                                                        g_variant_builder_add(builder1, "y",
                                                                desc_info->desc_value[i]);
@@ -622,7 +658,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                if (svc_info != NULL) {
                        read_req.service_handle = g_strdup(svc_info->serv_path);
                        user_info = _bt_get_user_data(BT_COMMON);
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                        GVariant *param = NULL;
 #endif
 
@@ -630,7 +666,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                        req_info = g_new0(struct gatt_req_info, 1);
                        req_info->attr_path = g_strdup(object_path);
                        req_info->svc_path = g_strdup(read_req.service_handle);
-                       req_info->request_id= req_id;
+                       req_info->request_id = req_id;
                        req_info->offset = offset;
                        req_info->context = invocation;
                        gatt_requests = g_slist_append(gatt_requests, req_info);
@@ -641,7 +677,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        BLUETOOTH_ERROR_NONE, &read_req,
                                        user_info->cb, user_info->user_data);
                        }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                        param = g_variant_new("(sssyq)",
                                        read_req.att_handle,
                                        read_req.service_handle,
@@ -669,7 +705,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                int len = 0;
                struct gatt_service_info *svc_info = NULL;
                struct gatt_req_info *req_info = NULL;
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                GVariant *param = NULL;
 #endif
 
@@ -714,7 +750,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                req_info = g_new0(struct gatt_req_info, 1);
                req_info->attr_path = g_strdup(object_path);
                req_info->svc_path = g_strdup(value_change.service_handle);
-               req_info->request_id= req_id;
+               req_info->request_id = req_id;
                req_info->offset = offset;
                req_info->context = invocation;
                gatt_requests = g_slist_append(gatt_requests, req_info);
@@ -726,7 +762,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                BLUETOOTH_ERROR_NONE, &value_change,
                                user_info->cb, user_info->user_data);
                }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                if (len > 0) {
                        gchar *svc_path;
                        svc_path = g_strdup(svc_info->serv_path);
@@ -855,7 +891,7 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
                        req_info = g_new0(struct gatt_req_info, 1);
                        req_info->attr_path = g_strdup(object_path);
                        req_info->svc_path = g_strdup(read_req.service_handle);
-                       req_info->request_id= req_id;
+                       req_info->request_id = req_id;
                        req_info->offset = offset;
                        req_info->context = invocation;
                        gatt_requests = g_slist_append(gatt_requests, req_info);
@@ -928,7 +964,7 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
                req_info = g_new0(struct gatt_req_info, 1);
                req_info->attr_path = g_strdup(object_path);
                req_info->svc_path = g_strdup(value_change.service_handle);
-               req_info->request_id= req_id;
+               req_info->request_id = req_id;
                req_info->offset = offset;
                req_info->context = invocation;
                gatt_requests = g_slist_append(gatt_requests, req_info);
@@ -952,13 +988,13 @@ gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
        GVariantBuilder *array_builder;
 
        array_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
-       g_variant_builder_init(array_builder, G_VARIANT_TYPE ("as"));
+       g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
        g_variant_builder_add(array_builder, "s", interface);
 
        ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
                                        "org.freedesktop.Dbus.Objectmanager",
                                        "InterfacesRemoved",
-                                       g_variant_new ("(oas)",
+                                       g_variant_new("(oas)",
                                        object_path, array_builder),
                                        &error);
 
@@ -1008,12 +1044,16 @@ static GDBusNodeInfo *__bt_gatt_create_method_node_info(
        if (introspection_data == NULL)
                return NULL;
 
+
+       BT_DBG("Create new node info");
        node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
 
        if (err) {
                BT_ERR("Unable to create node: %s", err->message);
                g_clear_error(&err);
+               return NULL;
        }
+
        return node_info;
 }
 
@@ -1094,9 +1134,8 @@ static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
        for (l = gatt_requests; l != NULL; l = l->next) {
                struct gatt_req_info *req_info = l->data;
 
-               if (req_info && req_info->request_id == request_id) {
+               if (req_info && req_info->request_id == request_id)
                        return req_info;
-               }
        }
        BT_ERR("Gatt Request not found");
        return NULL;
@@ -1309,7 +1348,7 @@ void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
                }
        } else {
                char *char_cmp = NULL;
-               g_variant_get (result, "(a{sv})", &iter);
+               g_variant_get(result, "(a{sv})", &iter);
                char_cmp = g_strdup_printf("Characteristic%d", n_char);
 
                while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
@@ -1357,6 +1396,8 @@ void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_d
                                                error->code, error->message);
                        g_clear_error(&error);
                }
+       } else {
+               g_variant_unref(result);
        }
 }
 
@@ -1380,6 +1421,8 @@ void unregister_application_cb(GObject *object, GAsyncResult *res,
                                        error->code, error->message);
                        g_clear_error(&error);
                }
+       } else {
+               g_variant_unref(result);
        }
 }
 
@@ -1397,43 +1440,78 @@ BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
 {
        GDBusProxy *proxy = NULL;
 
-       proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
-                                       "/org/bluez/hci0", GATT_MNGR_INTERFACE);
+       if (is_server_started) {
+               proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
+                               "/org/bluez/hci0", GATT_MNGR_INTERFACE);
 
-       if (proxy == NULL || app_path == NULL)
-               return BLUETOOTH_ERROR_INTERNAL;
+               if (proxy == NULL || app_path == NULL)
+                       return BLUETOOTH_ERROR_INTERNAL;
 
-       BT_INFO("UnregisterApplication");
+               BT_INFO("UnregisterApplication");
 
-       /* Async Call to Unregister Service */
-       g_dbus_proxy_call(proxy,
+               /* Async Call to Unregister Service */
+               g_dbus_proxy_call(proxy,
                                "UnregisterApplication",
                                g_variant_new("(o)",
-                               app_path),
+                                       app_path),
                                G_DBUS_CALL_FLAGS_NONE, -1,
                                NULL,
                                (GAsyncReadyCallback) unregister_application_cb,
                                NULL);
 
+               is_server_started = false;
+               return BLUETOOTH_ERROR_NONE;
+       }
+
+       BT_INFO("GATT server not started");
        return BLUETOOTH_ERROR_NONE;
 }
 
 static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
 {
        GDBusConnection *local_system_gconn = NULL;
+       char *address;
        GError *err = NULL;
 
        if (g_conn == NULL) {
-               g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               if (address == NULL) {
+                       if (err) {
+                               BT_ERR("Failed to get bus address: %s", err->message);
+                               g_clear_error(&err);
+                       }
+                       return NULL;
+               }
+
+               g_conn = g_dbus_connection_new_for_address_sync(address,
+                                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
+                                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+                                       NULL, /* GDBusAuthObserver */
+                                       NULL,
+                                       &err);
                if (!g_conn) {
                        if (err) {
                                BT_ERR("Unable to connect to dbus: %s", err->message);
                                g_clear_error(&err);
                        }
-                       g_conn = NULL;
+                       return NULL;
                }
        } else if (g_dbus_connection_is_closed(g_conn)) {
-               local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+               if (address == NULL) {
+                       if (err) {
+                               BT_ERR("Failed to get bus address: %s", err->message);
+                               g_clear_error(&err);
+                       }
+                       return NULL;
+               }
+
+               local_system_gconn = g_dbus_connection_new_for_address_sync(address,
+                                       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
+                                       G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+                                       NULL, /* GDBusAuthObserver */
+                                       NULL,
+                                       &err);
 
                if (!local_system_gconn) {
                        BT_ERR("Unable to connect to dbus: %s", err->message);
@@ -1449,13 +1527,20 @@ static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
 BT_EXPORT_API int bluetooth_gatt_init(void)
 {
        GDBusConnection *conn;
-       GDBusNodeInfo *obj_info;
        GError *error = NULL;
+       GDBusNodeInfo *node_info = NULL;
 
-       owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-                               BT_GATT_SERVICE_NAME,
-                               G_BUS_NAME_OWNER_FLAGS_NONE,
-                               NULL, NULL, NULL, NULL, NULL);
+       if (app_path != NULL) {
+               BT_ERR("app path already exists! initialized");
+               return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
+       }
+
+       if (owner_id == 0) {
+               owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+                                       BT_GATT_SERVICE_NAME,
+                                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                                       NULL, NULL, NULL, NULL, NULL);
+       }
 
        BT_DBG("owner_id is [%d]", owner_id);
        app_path = g_strdup_printf("/com/%d", getpid());
@@ -1465,30 +1550,47 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
        conn = __bt_gatt_get_gdbus_connection();
        if (!conn) {
                BT_ERR("Unable to get connection");
-               return BLUETOOTH_ERROR_INTERNAL;
+               goto failed;
        }
 
        /* Register ObjectManager interface */
-       obj_info = __bt_gatt_create_method_node_info(
+       node_info = __bt_gatt_create_method_node_info(
                                        manager_introspection_xml);
 
-       if (obj_info == NULL) {
+       if (node_info == NULL) {
                BT_ERR("failed to get node info");
-               return BLUETOOTH_ERROR_INTERNAL;
+               goto failed;
        }
 
-       manager_id = g_dbus_connection_register_object(g_conn, app_path,
-                                               obj_info->interfaces[0],
-                                               &manager_interface_vtable,
-                                               NULL, NULL, &error);
+       if (manager_id == 0) {
+               BT_INFO("manager_id does not exists");
+
+               manager_id = g_dbus_connection_register_object(g_conn, app_path,
+                                                       node_info->interfaces[0],
+                                                       &manager_interface_vtable,
+                                                       NULL, NULL, &error);
+       }
 
        if (manager_id == 0) {
                BT_ERR("failed to register: %s", error->message);
                g_error_free(error);
-               return BLUETOOTH_ERROR_INTERNAL;
+               goto failed;
        }
 
        return BLUETOOTH_ERROR_NONE;
+
+failed:
+       if (owner_id)
+               g_bus_unown_name(owner_id);
+
+       g_free(app_path);
+
+       app_path = NULL;
+       owner_id = 0;
+
+       __bt_gatt_close_gdbus_connection();
+
+       return BLUETOOTH_ERROR_INTERNAL;
 }
 
 BT_EXPORT_API int bluetooth_gatt_deinit()
@@ -1496,32 +1598,41 @@ BT_EXPORT_API int bluetooth_gatt_deinit()
        int ret = BLUETOOTH_ERROR_NONE;
        /* Unown gdbus bus */
        if (owner_id) {
-
                /* remove/unregister all services */
                BT_DBG("removing all registered gatt service\n");
                bluetooth_gatt_delete_services();
 
-               g_bus_unown_name(owner_id);
-
                /* unregister the exported interface for object manager */
                g_dbus_connection_unregister_object(g_conn,
                                        manager_id);
 
+               manager_id = 0;
+
+               ret = bluetooth_gatt_unregister_application();
+               if (ret != BLUETOOTH_ERROR_NONE)
+                       BT_ERR("Fail to unregister application\n");
+
+               g_bus_unown_name(owner_id);
+               owner_id = 0;
+
+               g_free(app_path);
+               app_path = NULL;
+
                BT_DBG("Gatt service deinitialized \n");
 
                g_slist_free(gatt_services);
                gatt_services = NULL;
 
-               ret = bluetooth_gatt_unregister_application();
+               g_object_unref(manager_gproxy);
+               manager_gproxy = NULL;
 
-               if (ret == BLUETOOTH_ERROR_NONE) {
-                       g_free(app_path);
-                       app_path = NULL;
-               }
+               __bt_gatt_close_gdbus_connection();
 
                return ret;
        }
 
+       __bt_gatt_close_gdbus_connection();
+
        return BLUETOOTH_ERROR_NOT_FOUND;
 }
 
@@ -1681,9 +1792,9 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
        char_info->char_id = object_id;
        char_info->char_uuid = g_strdup(char_uuid);
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                char_info->char_flags[i] = char_flags[i];
-               }
+
 
        char_info->flags_length = flag_count;
 
@@ -1699,9 +1810,8 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
 
        builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                g_variant_builder_add(builder2, "s", char_flags[i]);
-       }
 
        flags_val = g_variant_new("as", builder2);
        g_variant_builder_add(inner_builder, "{sv}", "Flags",
@@ -1780,9 +1890,8 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
 
        builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(builder1, "y", char_value[i]);
-       }
 
        char_val = g_variant_new("ay", builder1);
                g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
@@ -1841,6 +1950,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
        if (char_info == NULL) {
                g_strfreev(line_argv);
+               g_free(serv_path);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
@@ -1849,6 +1959,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
 
        if (node_info == NULL) {
                g_strfreev(line_argv);
+               g_free(serv_path);
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
@@ -1865,6 +1976,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
                g_error_free(error);
                g_free(path);
                g_strfreev(line_argv);
+               g_free(serv_path);
 
                return BLUETOOTH_ERROR_INTERNAL;
        }
@@ -1877,9 +1989,8 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        desc_info->desc_id = object_id;
        desc_info->desc_uuid = g_strdup(desc_uuid);
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                desc_info->desc_flags[i] = desc_flags[i];
-               }
 
        desc_info->flags_length = flag_count;
 
@@ -1895,9 +2006,8 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
 
        builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                g_variant_builder_add(builder2, "s", desc_flags[i]);
-       }
 
        flags_val = g_variant_new("as", builder2);
        g_variant_builder_add(inner_builder, "{sv}", "Flags",
@@ -1917,6 +2027,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        *desc_path = g_strdup(path);
 
        g_free(path);
+       g_free(serv_path);
        g_strfreev(line_argv);
        g_variant_builder_unref(inner_builder);
        g_variant_builder_unref(builder);
@@ -1968,9 +2079,9 @@ BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
 
        builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(builder1, "y", desc_value[i]);
-       }
+
        desc_val = g_variant_new("ay", builder1);
        g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
 
@@ -2043,27 +2154,36 @@ BT_EXPORT_API int bluetooth_gatt_register_application(void)
 {
        GDBusProxy *proxy = NULL;
 
-       if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
-                       == BLUETOOTH_ERROR_PERMISSION_DEINED) {
-               BT_ERR("Don't have aprivilege to use this API");
-               return BLUETOOTH_ERROR_PERMISSION_DEINED;
-       }
+       if (!is_server_started) {
 
-       proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
-                                       "/org/bluez/hci0", GATT_MNGR_INTERFACE);
-       if (proxy == NULL || app_path == NULL)
-               return BLUETOOTH_ERROR_INTERNAL;
+               if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_GATT_REGISTER_APPLICATION)
+                               == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+                       BT_ERR("Don't have aprivilege to use this API");
+                       return BLUETOOTH_ERROR_PERMISSION_DEINED;
+               }
 
-       BT_INFO("RegisterApplication");
+               proxy = __bt_gatt_gdbus_get_manager_proxy("org.bluez",
+                               "/org/bluez/hci0", GATT_MNGR_INTERFACE);
+               if (proxy == NULL || app_path == NULL)
+                       return BLUETOOTH_ERROR_INTERNAL;
 
-       g_dbus_proxy_call(proxy,
-                       "RegisterApplication",
-                       g_variant_new("(oa{sv})",
-                       app_path, NULL),
-                       G_DBUS_CALL_FLAGS_NONE, -1,
-                       NULL,
-                       (GAsyncReadyCallback) register_application_cb,
-                       NULL);
+               BT_INFO("RegisterApplication");
+
+               g_dbus_proxy_call(proxy,
+                               "RegisterApplication",
+                               g_variant_new("(oa{sv})",
+                                       app_path, NULL),
+                               G_DBUS_CALL_FLAGS_NONE, -1,
+                               NULL,
+                               (GAsyncReadyCallback) register_application_cb,
+                               NULL);
+
+               is_server_started = true;
+
+               return BLUETOOTH_ERROR_NONE;
+       }
+
+       BT_INFO("Already RegisterApplication");
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -2086,8 +2206,7 @@ BT_EXPORT_API int bluetooth_gatt_delete_services(void)
                        }
                }
                BT_DBG(" All services removed successfully.\n ");
-       }
-       else {
+       } else {
                BT_DBG(" There are no registered services.\n ");
        }
 
@@ -2121,6 +2240,7 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
        if (!__bt_gatt_get_service_state(serv_path)) {
                BT_DBG("service not registered for this characteristic \n");
+               g_free(serv_path);
                g_strfreev(line_argv);
                return BLUETOOTH_ERROR_INTERNAL;
        }
@@ -2129,9 +2249,8 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
        invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
        inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(inner_builder, "y", char_value[i]);
-       }
 
        update_value = g_variant_new("ay", inner_builder);
 
@@ -2162,6 +2281,12 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
                char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
                if (char_info == NULL) {
+                       g_free(serv_path);
+                       g_strfreev(line_argv);
+                       g_variant_builder_unref(inner_builder);
+                       g_variant_builder_unref(outer_builder);
+                       g_variant_builder_unref(invalidated_builder);
+
                        return BLUETOOTH_ERROR_INVALID_DATA;
                }
 
@@ -2169,12 +2294,12 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
                char_info->char_value = (char *)realloc(char_info->char_value, value_length);
                if (char_info->char_value) {
-                       for (i = 0; i < value_length; i++) {
+                       for (i = 0; i < value_length; i++)
                                char_info->char_value[i] = char_value[i];
-                       }
                }
        }
 
+       g_free(serv_path);
        g_strfreev(line_argv);
        g_variant_builder_unref(inner_builder);
        g_variant_builder_unref(outer_builder);
@@ -2183,13 +2308,56 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
        return err;
 }
 
+static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
+{
+       int i;
+
+       if (!desc_info)
+               return;
+
+       g_free(desc_info->desc_path);
+       g_free(desc_info->desc_uuid);
+       g_free(desc_info->desc_value);
+
+       for (i = 0; i < desc_info->flags_length; i++)
+               g_free(desc_info->desc_flags[i]);
+
+       g_free(desc_info);
+}
+
+static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
+{
+       int i;
+
+       if (!char_info)
+               return;
+
+       g_free(char_info->char_path);
+       g_free(char_info->char_uuid);
+       g_free(char_info->char_value);
+
+       for (i = 0; i < char_info->flags_length; i++)
+               g_free(char_info->char_flags[i]);
+
+       g_free(char_info);
+}
+
+static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
+{
+       if (!svc_info)
+               return;
+
+       g_free(svc_info->serv_path);
+       g_free(svc_info->service_uuid);
+       g_free(svc_info);
+}
+
 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
 {
        GSList *l, *l1;
        struct gatt_service_info *svc_info;
        gboolean ret;
        int err = BLUETOOTH_ERROR_NONE;
-       GSList *tmp;
 
        BT_DBG("svc_path %s", svc_path);
        svc_info = __bt_gatt_find_gatt_service_info(svc_path);
@@ -2208,9 +2376,15 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
        for (l = svc_info->char_data; l != NULL; l = l->next) {
                struct gatt_char_info *char_info = l->data;
 
+               if (char_info == NULL)
+                       break;
+
                for (l1 = char_info->desc_data; l1 != NULL; l1 = l1->next) {
                        struct gatt_desc_info *desc_info = l1->data;
 
+                       if (desc_info == NULL)
+                               break;
+
                        ret = g_dbus_connection_unregister_object(g_conn,
                                                desc_info->desc_id);
                        if (ret) {
@@ -2220,7 +2394,15 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                        } else {
                                err = BLUETOOTH_ERROR_INTERNAL;
                        }
+
+                       /* list remove & free */
+                       char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
+                       __bt_gatt_free_descriptor_info(desc_info);
                }
+
+               g_slist_free(char_info->desc_data);
+               char_info->desc_data = NULL;
+
                ret = g_dbus_connection_unregister_object(g_conn,
                                        char_info->char_id);
                if (ret) {
@@ -2229,7 +2411,15 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                } else {
                        err = BLUETOOTH_ERROR_INTERNAL;
                }
+
+               /* list remove & free */
+               svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
+               __bt_gatt_free_characteristic_info(char_info);
        }
+
+       g_slist_free(svc_info->char_data);
+       svc_info->char_data = NULL;
+
        ret = g_dbus_connection_unregister_object(g_conn, svc_info->serv_id);
        if (ret) {
                __bt_gatt_emit_interface_removed(svc_info->serv_path,
@@ -2239,21 +2429,18 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
        }
 
        ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
-       if (ret) {
+       if (ret)
                BT_DBG("Unregistered the service on properties interface");
-       }
-
-       for (tmp = gatt_services; tmp != NULL; tmp = tmp->next) {
-               struct gatt_service_info *info = tmp->data;
 
-               if (g_strcmp0(info->serv_path, svc_path) == 0) {
-                       gatt_services = g_slist_delete_link(gatt_services, tmp->data);
-               }
-       }
+       /* list remove & free */
+       gatt_services = g_slist_remove(gatt_services, svc_info);
+       __bt_gatt_free_service_info(svc_info);
 
        new_service = FALSE;
 
-       if (gatt_services->next == NULL)
+       if (gatt_services == NULL)
+               serv_id = 1;
+       else if (gatt_services->next == NULL)
                serv_id--;
 
        return err;
@@ -2274,11 +2461,8 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
 
        if (req_info) {
                if (resp_state != BLUETOOTH_ERROR_NONE) {
-
-                       GQuark quark = g_quark_from_string("gatt-server");
-                       GError *err = g_error_new(quark, 0, "Application Error");
-                       g_dbus_method_invocation_return_gerror(req_info->context, err);
-                       g_error_free(err);
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                                               "org.bluez.Error.Failed", "Application Error");
 
                        gatt_requests = g_slist_remove(gatt_requests, req_info);
 
@@ -2294,7 +2478,7 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
                if (req_type == BLUETOOTH_GATT_ATT_REQUEST_TYPE_READ) {
                        int i;
                        GVariantBuilder *inner_builder = NULL;
-                       inner_builder = g_variant_builder_new(G_VARIANT_TYPE ("ay"));
+                       inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
                        if (value_length > 0 && value != NULL) {
                                for (i = 0; i < value_length; i++)
                                        g_variant_builder_add(inner_builder, "y", value[i]);
@@ -2338,10 +2522,13 @@ BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
 
        if (!__bt_gatt_get_service_state(serv_path)) {
                BT_DBG("service not registered for this characteristic \n");
+               g_free(serv_path);
                g_strfreev(line_argv);
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+       g_free(serv_path);
+
        outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));