[GATT Client] Delivery ATT error code to higher layer
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index 88d33f5..e483da5 100644 (file)
@@ -29,7 +29,6 @@
 #define NUMBER_OF_FLAGS        10
 
 GDBusConnection *g_conn;
-GDBusNodeInfo *obj_info;
 guint owner_id;
 guint manager_id;
 static gboolean new_service = FALSE;
@@ -61,14 +60,15 @@ static const gchar characteristics_introspection_xml[] =
 "  <interface name='org.bluez.GattCharacteristic1'>"
 "       <method name='ReadValue'>"
 "              <arg type='s' name='address' direction='in'/>"
-"              <arg type='y' name='id' direction='in'/>"
+"              <arg type='u' name='id' direction='in'/>"
 "              <arg type='q' name='offset' direction='in'/>"
 "              <arg type='ay' name='Value' direction='out'/>"
 "       </method>"
 "       <method name='WriteValue'>"
 "              <arg type='s' name='address' direction='in'/>"
-"              <arg type='y' name='id' direction='in'/>"
+"              <arg type='u' name='id' direction='in'/>"
 "              <arg type='q' name='offset' direction='in'/>"
+"              <arg type='b' name='response_needed' direction='in'/>"
 "              <arg type='ay' name='value' direction='in'/>"
 "       </method>"
 "       <method name='StartNotify'>"
@@ -104,14 +104,15 @@ static const gchar descriptor_introspection_xml[] =
 "  <interface name='org.bluez.GattDescriptor1'>"
 "       <method name='ReadValue'>"
 "              <arg type='s' name='address' direction='in'/>"
-"              <arg type='y' name='id' direction='in'/>"
+"              <arg type='u' name='id' direction='in'/>"
 "              <arg type='q' name='offset' direction='in'/>"
 "              <arg type='ay' name='Value' direction='out'/>"
 "       </method>"
 "       <method name='WriteValue'>"
 "              <arg type='s' name='address' direction='in'/>"
-"              <arg type='y' name='id' direction='in'/>"
+"              <arg type='u' name='id' direction='in'/>"
 "              <arg type='q' name='offset' direction='in'/>"
+"              <arg type='b' name='response_needed' direction='in'/>"
 "              <arg type='ay' name='value' direction='in'/>"
 "       </method>"
 "  </interface>"
@@ -190,13 +191,20 @@ 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"
 #define BT_HPS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+#define BT_OTP_OBJECT_PATH             "/org/projectx/otp"
+#define BT_OTP_INTERFACE_NAME          "org.projectx.otp_service"
+#define PROPERTIES_CHANGED             "PropertiesChanged"
+#define BT_OTP_PROPERTIES_INTERFACE    "org.freedesktop.DBus.Properties"
+#endif
+
 static GDBusProxy *manager_gproxy = NULL;
 
 static struct gatt_char_info *__bt_gatt_find_gatt_char_info(
@@ -235,7 +243,7 @@ static void __bt_gatt_close_gdbus_connection(void)
        BT_DBG("-");
 }
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
 static int __bt_send_event_to_hps(int event, GVariant *var)
 {
        GError *error = NULL;
@@ -291,6 +299,76 @@ static int __bt_send_event_to_hps(int event, GVariant *var)
 }
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+static int __bt_send_event_to_otp(int event, GVariant *var)
+{
+       GError *error = NULL;
+       GVariant *parameters = NULL;
+       GDBusMessage *msg = NULL;
+
+       BT_DBG(" ");
+
+       retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       if (event == BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "WriteValue", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       } else if (event == BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "ReadValue", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       } else if (event == BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED) {
+               GVariantBuilder *inner_builder;
+               GVariantBuilder *invalidated_builder;
+
+               BT_DBG("BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED");
+               inner_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+               g_variant_builder_add(inner_builder, "{sv}", "NotificationStateChanged", var);
+
+               invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+
+               parameters = g_variant_new("(a{sv}as)", inner_builder, invalidated_builder);
+               g_variant_builder_unref(invalidated_builder);
+               g_variant_builder_unref(inner_builder);
+       }
+
+       msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_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 (error != NULL) {
+                       BT_ERR("D-Bus API failure: errCode[%x], \
+                                       message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+       return BLUETOOTH_ERROR_NONE;
+}
+#endif
+
 static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                        const gchar *sender,
                                        const gchar *object_path,
@@ -300,7 +378,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;
 
@@ -323,13 +401,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");
@@ -627,80 +711,82 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
 
        if (g_strcmp0(method_name, "ReadValue") == 0) {
                gchar *addr = NULL;
-               guint8 req_id = 1;
+               guint req_id = 0;
                guint16 offset = 0;
                bt_gatt_read_req_t read_req = {0, };
                bt_user_info_t *user_info = NULL;
                struct gatt_req_info *req_info = NULL;
                struct gatt_service_info *svc_info = NULL;
-
-               BT_DBG("ReadValue");
-
-               g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
+               GVariant *param = NULL;
+#endif
 
                BT_DBG("Application path = %s", object_path);
+               BT_DBG("Sender = %s", sender);
 
-               BT_DBG("Remote Device address number = %s", addr);
-               BT_DBG("Request id = %d, Offset = %d", req_id, offset);
+               user_info = _bt_get_user_data(BT_COMMON);
+               if (user_info == NULL) {
+                       BT_INFO("No callback is set for %s", object_path);
+                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       return;
+               }
 
-               BT_DBG("Sender = %s", sender);
+               svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
+               if (svc_info == NULL) {
+                       BT_ERR("Coudn't find service for %s", object_path);
+                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       return;
+               }
+
+               g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
+               BT_DBG("Request id = %u, Offset = %u", req_id, offset);
 
-               read_req.att_handle = g_strdup(object_path);
-               read_req.address = g_strdup(addr);
+               read_req.att_handle = (char *)object_path;
+               read_req.address = addr;
                read_req.req_id = req_id;
                read_req.offset = offset;
-               svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
-               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
-                       GVariant *param = NULL;
-#endif
+               read_req.service_handle = svc_info->serv_path;
 
-                       /* Store requets information */
-                       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->offset = offset;
-                       req_info->context = invocation;
-                       gatt_requests = g_slist_append(gatt_requests, req_info);
+               /* Store requets information */
+               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->offset = offset;
+               req_info->context = invocation;
+               gatt_requests = g_slist_append(gatt_requests, req_info);
 
-                       if (user_info != NULL) {
-                               _bt_common_event_cb(
-                                       BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
+               _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
                                        BLUETOOTH_ERROR_NONE, &read_req,
                                        user_info->cb, user_info->user_data);
-                       }
-#ifdef HPS_FEATURE
-                       param = g_variant_new("(sssyq)",
-                                       read_req.att_handle,
-                                       read_req.service_handle,
-                                       read_req.address,
-                                       read_req.req_id,
-                                       read_req.offset);
-                       __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
-#endif
-               }
 
-               if (read_req.att_handle)
-                       g_free(read_req.att_handle);
-               if (read_req.address)
-                       g_free(read_req.address);
-               if (read_req.service_handle)
-                       g_free(read_req.service_handle);
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
+               param = g_variant_new("(sssyq)",
+                               read_req.att_handle,
+                               read_req.service_handle,
+                               read_req.address,
+                               read_req.req_id,
+                               read_req.offset);
+#ifdef TIZEN_FEATURE_BT_HPS
+               __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
+#endif
+#ifdef TIZEN_FEATURE_BT_OTP
+               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED, param);
+#endif
+#endif
                return;
        } else if (g_strcmp0(method_name, "WriteValue") == 0) {
                GVariant *var = NULL;
                gchar *addr = NULL;
-               guint8 req_id = 0;
+               guint req_id = 0;
                guint16 offset = 0;
+               gboolean response_needed = FALSE;
                bt_gatt_value_change_t value_change = {0, };
                bt_user_info_t *user_info = NULL;
                int len = 0;
                struct gatt_service_info *svc_info = NULL;
                struct gatt_req_info *req_info = NULL;
-#ifdef HPS_FEATURE
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                GVariant *param = NULL;
 #endif
 
@@ -708,56 +794,69 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                BT_DBG("Application path = %s", object_path);
                BT_DBG("Sender = %s", sender);
 
-               g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
+               g_variant_get(parameters, "(&suqb@ay)",
+                               &addr, &req_id, &offset, &response_needed, &var);
+               BT_DBG("Request id = %u, Offset = %u", req_id, offset);
+
+               user_info = _bt_get_user_data(BT_COMMON);
+               if (!user_info) {
+                       BT_INFO("No callback is set for %s", object_path);
+                       g_variant_unref(var);
+                       if (response_needed)
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                       else
+                               g_object_unref(invocation);
+                       return;
+               }
 
-               value_change.att_handle = g_strdup(object_path);
-               value_change.address = g_strdup(addr);
                svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
                if (svc_info == NULL) {
+                       BT_ERR("Coudn't find service for %s", object_path);
                        g_variant_unref(var);
-                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       if (response_needed)
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                       else
+                               g_object_unref(invocation);
                        return;
                }
 
-               value_change.service_handle = g_strdup(svc_info->serv_path);
+               value_change.att_handle = (char *)object_path;
+               value_change.address = addr;
+               value_change.service_handle = svc_info->serv_path;
                value_change.offset = offset;
                value_change.req_id = req_id;
+               value_change.response_needed = response_needed;
 
                len = g_variant_get_size(var);
                if (len > 0) {
                        char *data;
 
-                       value_change.att_value = (guint8 *)malloc(len);
-                       if (!value_change.att_value) {
-                               BT_ERR("att_value is NULL");
-                               g_variant_unref(var);
-                               g_dbus_method_invocation_return_value(invocation, NULL);
-                               return;
-                       }
+                       value_change.att_value = (guint8 *)g_malloc(len);
 
                        data = (char *)g_variant_get_data(var);
                        memcpy(value_change.att_value, data, len);
                }
-
                value_change.val_len = len;
 
-               /* Store requets information */
-               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->offset = offset;
-               req_info->context = invocation;
-               gatt_requests = g_slist_append(gatt_requests, req_info);
-
-               user_info = _bt_get_user_data(BT_COMMON);
-               if (user_info != NULL) {
-                       _bt_common_event_cb(
-                               BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
-                               BLUETOOTH_ERROR_NONE, &value_change,
-                               user_info->cb, user_info->user_data);
+               if (response_needed) {
+                       /* Store requets information */
+                       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->offset = offset;
+                       req_info->context = invocation;
+                       gatt_requests = g_slist_append(gatt_requests, req_info);
+               } else {
+                       g_object_unref(invocation);
                }
-#ifdef HPS_FEATURE
+
+               _bt_common_event_cb(
+                       BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
+                       BLUETOOTH_ERROR_NONE, &value_change,
+                       user_info->cb, user_info->user_data);
+
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                if (len > 0) {
                        gchar *svc_path;
                        svc_path = g_strdup(svc_info->serv_path);
@@ -768,54 +867,81 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        req_id,
                                        offset,
                                        var);
+#ifdef TIZEN_FEATURE_BT_HPS
                        __bt_send_event_to_hps(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
+#endif
+#ifdef TIZEN_FEATURE_BT_OTP
+                       __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED, param);
+#endif
                        if (svc_path)
                                g_free(svc_path);
                }
 #endif
+
+               g_free(value_change.att_value);
                g_variant_unref(var);
                return;
        } else if (g_strcmp0(method_name, "StartNotify") == 0) {
                bt_user_info_t *user_info = NULL;
                bt_gatt_char_notify_change_t notify_change = {0, };
+#if TIZEN_FEATURE_BT_OTP
+               GVariant *param = NULL;
+#endif
                BT_DBG("StartNotify");
                user_info = _bt_get_user_data(BT_COMMON);
                if (user_info != NULL) {
                        struct gatt_service_info *svc_info = NULL;
                        svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
                        if (svc_info) {
-                               notify_change.service_handle = g_strdup(svc_info->serv_path);
-                               notify_change.att_handle = g_strdup(object_path);
+                               notify_change.service_handle = svc_info->serv_path;
+                               notify_change.att_handle = (char *)object_path;
                                notify_change.att_notify = TRUE;
                                _bt_common_event_cb(
                                        BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
                                        BLUETOOTH_ERROR_NONE, &notify_change,
                                        user_info->cb, user_info->user_data);
+#if TIZEN_FEATURE_BT_OTP
+                               param = g_variant_new("(ssb)",
+                               notify_change.att_handle,
+                               notify_change.service_handle,
+                               notify_change.att_notify);
+                               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
+#endif
                        }
                }
        } else if (g_strcmp0(method_name, "StopNotify") == 0) {
                bt_user_info_t *user_info = NULL;
                bt_gatt_char_notify_change_t notify_change = {0, };
+#if TIZEN_FEATURE_BT_OTP
+               GVariant *param = NULL;
+#endif
                BT_DBG("StopNotify");
                user_info = _bt_get_user_data(BT_COMMON);
                if (user_info != NULL) {
                        struct gatt_service_info *svc_info = NULL;
                        svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
                        if (svc_info) {
-                               notify_change.service_handle = g_strdup(svc_info->serv_path);
-                               notify_change.att_handle = g_strdup(object_path);
+                               notify_change.service_handle = svc_info->serv_path;
+                               notify_change.att_handle = (char *)object_path;
                                notify_change.att_notify = FALSE;
                                _bt_common_event_cb(
                                        BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
                                        BLUETOOTH_ERROR_NONE, &notify_change,
                                        user_info->cb, user_info->user_data);
+#if TIZEN_FEATURE_BT_OTP
+                               param = g_variant_new("(ssb)",
+                               notify_change.att_handle,
+                               notify_change.service_handle,
+                               notify_change.att_notify);
+                               __bt_send_event_to_otp(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED, param);
+#endif
                        }
                }
        } else if (g_strcmp0(method_name, "IndicateConfirm") == 0) {
                gchar *addr = NULL;
                bt_gatt_indicate_confirm_t confirm = {0, };
                bt_user_info_t *user_info = NULL;
-               gboolean complete = 0;
+               gboolean complete = FALSE;
                struct gatt_service_info *svc_info = NULL;
 
                BT_DBG("IndicateConfirm");
@@ -823,17 +949,17 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                BT_DBG("Sender = %s", sender);
 
                g_variant_get(parameters, "(&sb)", &addr, &complete);
-
                BT_DBG("Remote Device address number = %s", addr);
-               confirm.att_handle = g_strdup(object_path);
-               confirm.address = g_strdup(addr);
+
+               confirm.att_handle = (char *)object_path;
+               confirm.address = addr;
                confirm.complete = complete;
 
                svc_info = __bt_gatt_find_gatt_service_from_char(object_path);
                if (svc_info != NULL) {
-                       confirm.service_handle = g_strdup(svc_info->serv_path);
-                       user_info = _bt_get_user_data(BT_COMMON);
+                       confirm.service_handle = svc_info->serv_path;
 
+                       user_info = _bt_get_user_data(BT_COMMON);
                        if (user_info != NULL) {
                                _bt_common_event_cb(
                                        BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
@@ -842,6 +968,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                        }
                }
        }
+
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
@@ -856,62 +983,61 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
 {
        if (g_strcmp0(method_name, "ReadValue") == 0) {
                gchar *addr = NULL;
-               guint8 req_id = 1;
+               guint req_id = 0;
                guint16 offset = 0;
                bt_gatt_read_req_t read_req = {0, };
                bt_user_info_t *user_info = NULL;
                struct gatt_req_info *req_info = NULL;
                struct gatt_service_info *svc_info = NULL;
-               BT_DBG("ReadValue");
-
-               g_variant_get(parameters, "(&syq)", &addr, &req_id, &offset);
 
+               BT_DBG("ReadValue");
                BT_DBG("Application path = %s", object_path);
+               BT_DBG("Sender = %s", sender);
 
-               BT_DBG("Remote Device address number = %s", addr);
-               BT_DBG("Request id = %d, Offset = %d", req_id, offset);
+               user_info = _bt_get_user_data(BT_COMMON);
+               if (user_info == NULL) {
+                       BT_INFO("No callback is set for %s", object_path);
+                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       return;
+               }
 
-               BT_DBG("Sender = %s", sender);
+               svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
+               if (svc_info == NULL) {
+                       BT_ERR("Coudn't find service for %s", object_path);
+                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       return;
+               }
 
-               read_req.att_handle = g_strdup(object_path);
-               read_req.address = g_strdup(addr);
+               g_variant_get(parameters, "(&suq)", &addr, &req_id, &offset);
+               BT_DBG("Request id = %u, Offset = %u", req_id, offset);
+
+               read_req.att_handle = (char *)object_path;
+               read_req.address = addr;
                read_req.req_id = req_id;
                read_req.offset = offset;
-               svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
-               if (svc_info != NULL) {
-                       read_req.service_handle = g_strdup(svc_info->serv_path);
-                       user_info = _bt_get_user_data(BT_COMMON);
-
-                       /* Store requets information */
-                       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->offset = offset;
-                       req_info->context = invocation;
-                       gatt_requests = g_slist_append(gatt_requests, req_info);
+               read_req.service_handle = svc_info->serv_path;
 
-                       if (user_info != NULL) {
-                               _bt_common_event_cb(
-                                       BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
-                                       BLUETOOTH_ERROR_NONE, &read_req,
-                                       user_info->cb, user_info->user_data);
-                       }
-               }
+               /* Store requets information */
+               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->offset = offset;
+               req_info->context = invocation;
+               gatt_requests = g_slist_append(gatt_requests, req_info);
 
-               if (read_req.att_handle)
-                       g_free(read_req.att_handle);
-               if (read_req.address)
-                       g_free(read_req.address);
-               if (read_req.service_handle)
-                       g_free(read_req.service_handle);
+               _bt_common_event_cb(
+                               BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
+                               BLUETOOTH_ERROR_NONE, &read_req,
+                               user_info->cb, user_info->user_data);
 
                return;
        } else if (g_strcmp0(method_name, "WriteValue") == 0) {
                GVariant *var = NULL;
                gchar *addr = NULL;
-               guint8 req_id = 0;
+               guint req_id = 0;
                guint16 offset = 0;
+               gboolean response_needed = FALSE;
                bt_gatt_value_change_t value_change = {0, };
                bt_user_info_t *user_info = NULL;
                int len = 0;
@@ -922,58 +1048,72 @@ static void __bt_gatt_desc_method_call(GDBusConnection *connection,
                BT_DBG("Application path = %s", object_path);
                BT_DBG("Sender = %s", sender);
 
-               g_variant_get(parameters, "(&syq@ay)", &addr, &req_id, &offset, &var);
+               g_variant_get(parameters, "(&suqb@ay)",
+                               &addr, &req_id, &offset, &response_needed, &var);
+               BT_DBG("Request id = %u, Offset = %u", req_id, offset);
+
+               user_info = _bt_get_user_data(BT_COMMON);
+               if (user_info == NULL) {
+                       BT_INFO("No callback is set for %s", object_path);
+                       g_variant_unref(var);
+                       if (response_needed)
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                       else
+                               g_object_unref(invocation);
+                       return;
+               }
 
-               value_change.att_handle = g_strdup(object_path);
-               value_change.address = g_strdup(addr);
                svc_info = __bt_gatt_find_gatt_service_from_desc(object_path);
                if (svc_info == NULL) {
+                       BT_ERR("Coudn't find service for %s", object_path);
                        g_variant_unref(var);
-                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       if (response_needed)
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                       else
+                               g_object_unref(invocation);
                        return;
                }
 
-               value_change.service_handle = g_strdup(svc_info->serv_path);
+               value_change.att_handle = (char *)object_path;
+               value_change.address = addr;
+               value_change.service_handle = svc_info->serv_path;
                value_change.offset = offset;
                value_change.req_id = req_id;
+               value_change.response_needed = response_needed;
 
                len = g_variant_get_size(var);
                if (len > 0) {
                        char *data;
 
-                       value_change.att_value = (guint8 *)malloc(len);
-                       if (!value_change.att_value) {
-                               BT_ERR("att_value is NULL");
-                               g_variant_unref(var);
-                               g_dbus_method_invocation_return_value(invocation, NULL);
-                               return;
-                       }
+                       value_change.att_value = (guint8 *)g_malloc(len);
+
                        data = (char *)g_variant_get_data(var);
                        memcpy(value_change.att_value, data, len);
                }
-               g_variant_unref(var);
-
                value_change.val_len = len;
 
-               /* Store requets information */
-               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->offset = offset;
-               req_info->context = invocation;
-               gatt_requests = g_slist_append(gatt_requests, req_info);
-
-               user_info = _bt_get_user_data(BT_COMMON);
-               if (user_info != NULL) {
-                       _bt_common_event_cb(
-                               BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
-                               BLUETOOTH_ERROR_NONE, &value_change,
-                               user_info->cb, user_info->user_data);
+               if (response_needed) {
+                       /* Store requets information */
+                       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->offset = offset;
+                       req_info->context = invocation;
+                       gatt_requests = g_slist_append(gatt_requests, req_info);
+               } else {
+                       g_object_unref(invocation);
                }
+
+               _bt_common_event_cb(
+                       BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
+                       BLUETOOTH_ERROR_NONE, &value_change,
+                       user_info->cb, user_info->user_data);
+
+               g_free(value_change.att_value);
+               g_variant_unref(var);
                return;
        }
-       g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
 gboolean __bt_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
@@ -1034,22 +1174,22 @@ static GDBusNodeInfo *__bt_gatt_create_method_node_info(
                                const gchar *introspection_data)
 {
        GError *err = NULL;
+       GDBusNodeInfo *node_info = NULL;
 
        if (introspection_data == NULL)
                return NULL;
 
-       if (obj_info == NULL) {
-               BT_DBG("Create new node info");
-               obj_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;
-               }
+       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 obj_info;
+       return node_info;
 }
 
 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
@@ -1129,9 +1269,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;
@@ -1392,6 +1531,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);
        }
 }
 
@@ -1415,6 +1556,8 @@ void unregister_application_cb(GObject *object, GAsyncResult *res,
                                        error->code, error->message);
                        g_clear_error(&error);
                }
+       } else {
+               g_variant_unref(result);
        }
 }
 
@@ -1572,15 +1715,11 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
        return BLUETOOTH_ERROR_NONE;
 
 failed:
-       if (obj_info)
-               g_dbus_node_info_unref(obj_info);
-
        if (owner_id)
                g_bus_unown_name(owner_id);
 
        g_free(app_path);
 
-       obj_info = NULL;
        app_path = NULL;
        owner_id = 0;
 
@@ -1622,12 +1761,6 @@ BT_EXPORT_API int bluetooth_gatt_deinit()
                g_object_unref(manager_gproxy);
                manager_gproxy = NULL;
 
-               /* Temperary block under codes to avoid TC blocking issue.
-                   But we should unref node info in later. */
-#if 0
-               g_dbus_node_info_unref(obj_info);
-               obj_info = NULL;
-#endif
                __bt_gatt_close_gdbus_connection();
 
                return ret;
@@ -1794,9 +1927,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;
 
@@ -1812,9 +1945,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",
@@ -1893,9 +2025,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);
@@ -1954,6 +2085,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;
        }
 
@@ -1962,6 +2094,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;
        }
 
@@ -1978,6 +2111,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;
        }
@@ -1990,9 +2124,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;
 
@@ -2008,9 +2141,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",
@@ -2030,6 +2162,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);
@@ -2081,9 +2214,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);
 
@@ -2203,7 +2336,7 @@ BT_EXPORT_API int bluetooth_gatt_delete_services(void)
                        if (bluetooth_gatt_unregister_service(info->serv_path)
                                        != BLUETOOTH_ERROR_NONE) {
                                error = BLUETOOTH_ERROR_INTERNAL;
-                               BT_DBG(" Error in removing service %s \n",
+                               BT_ERR("Error in removing service %s \n",
                                                 info->serv_path);
                        }
                }
@@ -2242,6 +2375,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;
        }
@@ -2250,9 +2384,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);
 
@@ -2283,6 +2416,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;
                }
 
@@ -2290,12 +2429,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);
@@ -2304,14 +2443,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)
 {
-       int i = 0;
        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);
@@ -2323,7 +2504,7 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
 
        err = __bt_gatt_unregister_service(svc_path);
        if (err != BLUETOOTH_ERROR_NONE) {
-               BT_DBG("Could not unregister application");
+               BT_ERR("Could not unregister application");
                return err;
        }
 
@@ -2349,16 +2530,9 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                                err = BLUETOOTH_ERROR_INTERNAL;
                        }
 
+                       /* list remove & free */
                        char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
-
-                       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);
+                       __bt_gatt_free_descriptor_info(desc_info);
                }
 
                g_slist_free(char_info->desc_data);
@@ -2373,12 +2547,9 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                        err = BLUETOOTH_ERROR_INTERNAL;
                }
 
-               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]);
+               /* 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);
@@ -2393,28 +2564,19 @@ 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_remove(gatt_services, tmp->data);
-                       g_free(info->serv_path);
-                       g_free(info->service_uuid);
-                       g_free(info);
-               }
-       }
+       /* 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 == NULL) {
+       if (gatt_services == NULL)
                serv_id = 1;
-       } else if (gatt_services->next == NULL) {
+       else if (gatt_services->next == NULL)
                serv_id--;
-       }
 
        return err;
 }
@@ -2431,40 +2593,41 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
        }
 
        req_info = __bt_gatt_find_request_info(request_id);
+       if (req_info == NULL) {
+               BT_ERR("Coundn't find request id [%d]", request_id);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
 
-       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);
-
-                       gatt_requests = g_slist_remove(gatt_requests, req_info);
-
-                       req_info->context = NULL;
-                       if (req_info->attr_path)
-                               g_free(req_info->attr_path);
-                       if (req_info->svc_path)
-                               g_free(req_info->svc_path);
-                       g_free(req_info);
+       if (resp_state != BLUETOOTH_ATT_ERROR_NONE) {
+               BT_ERR("resp_state is 0x%X", resp_state);
 
-                       return BLUETOOTH_ERROR_NONE;
-               }
-               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"));
-                       if (value_length > 0 && value != NULL) {
-                               for (i = 0; i < value_length; i++)
-                                       g_variant_builder_add(inner_builder, "y", value[i]);
-                       }
-                       g_dbus_method_invocation_return_value(req_info->context,
-                                               g_variant_new("(ay)", inner_builder));
-                       g_variant_builder_unref(inner_builder);
-               } else {
-                       g_dbus_method_invocation_return_value(req_info->context, NULL);
+               switch (resp_state) {
+               case BLUETOOTH_ATT_ERROR_WRITE_REQUEST_REJECTED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Write Request Rejected");
+                       break;
+               case BLUETOOTH_ATT_ERROR_OBJECT_NOT_SELECTED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Object Not Selected");
+                       break;
+               case BLUETOOTH_ATT_ERROR_CONCURRENCY_LIMIT_EXCEEDED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Concurrency Limit Exceeded");
+                       break;
+               case BLUETOOTH_ATT_ERROR_OBJECT_NAME_EXISTS:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Object Name Already Exists");
+                       break;
+               case BLUETOOTH_ATT_ERROR_CCCD_IMPROPERLY_CONFIGURED:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "CCC Improperly Configured");
+                       break;
+               default:
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                       "org.bluez.Error.Failed", "Application Error");
+                       break;
                }
+
                gatt_requests = g_slist_remove(gatt_requests, req_info);
 
                req_info->context = NULL;
@@ -2473,9 +2636,32 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
                if (req_info->svc_path)
                        g_free(req_info->svc_path);
                g_free(req_info);
+
+               return BLUETOOTH_ERROR_NONE;
+       }
+
+       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"));
+               if (value_length > 0 && value != NULL) {
+                       for (i = 0; i < value_length; i++)
+                               g_variant_builder_add(inner_builder, "y", value[i]);
+               }
+               g_dbus_method_invocation_return_value(req_info->context,
+                               g_variant_new("(ay)", inner_builder));
+               g_variant_builder_unref(inner_builder);
        } else {
-               return BLUETOOTH_ERROR_INTERNAL;
+               g_dbus_method_invocation_return_value(req_info->context, NULL);
        }
+       gatt_requests = g_slist_remove(gatt_requests, req_info);
+
+       req_info->context = NULL;
+       if (req_info->attr_path)
+               g_free(req_info->attr_path);
+       if (req_info->svc_path)
+               g_free(req_info->svc_path);
+       g_free(req_info);
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -2498,10 +2684,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"));