Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index d33e0f3..7cb8ee8 100644 (file)
@@ -60,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'>"
@@ -103,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>"
@@ -196,6 +198,13 @@ static gchar *app_path = NULL;
 #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(
@@ -290,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,
@@ -632,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;
+               }
 
-               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_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 TIZEN_FEATURE_BT_HPS
-                       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);
-                       }
+
+#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
-                       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);
+               __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
-               }
-
-               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);
                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 TIZEN_FEATURE_BT_HPS
+#if defined(TIZEN_FEATURE_BT_HPS) || defined(TIZEN_FEATURE_BT_OTP)
                GVariant *param = NULL;
 #endif
 
@@ -713,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 TIZEN_FEATURE_BT_HPS
+
+               _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);
@@ -773,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");
@@ -828,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,
@@ -847,6 +968,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                        }
                }
        }
+
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
@@ -861,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);
+               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);
-
-                       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;
@@ -927,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)
@@ -1334,7 +1469,7 @@ void get_service_cb(GObject *object, GAsyncResult *res, gpointer user_data)
        int n_char = 1;
 
        BT_DBG(" ");
-       result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
+       result = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &error);
 
        if (result == NULL) {
                /* dBUS-RPC is failed */
@@ -1384,7 +1519,7 @@ void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_d
 
        register_pending_cnt = 0;
 
-       result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
+       result = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &error);
 
        if (result == NULL) {
                /* dBUS-RPC is failed */
@@ -1409,7 +1544,7 @@ void unregister_application_cb(GObject *object, GAsyncResult *res,
        GError *error = NULL;
        GVariant *result;
 
-       result = g_dbus_proxy_call_finish(manager_gproxy, res, &error);
+       result = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &error);
 
        if (result == NULL) {
                /* dBUS-RPC is failed */
@@ -1489,6 +1624,7 @@ static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
                                        NULL, /* GDBusAuthObserver */
                                        NULL,
                                        &err);
+               g_free(address);
                if (!g_conn) {
                        if (err) {
                                BT_ERR("Unable to connect to dbus: %s", err->message);
@@ -1512,7 +1648,7 @@ static GDBusConnection *__bt_gatt_get_gdbus_connection(void)
                                        NULL, /* GDBusAuthObserver */
                                        NULL,
                                        &err);
-
+               g_free(address);
                if (!local_system_gconn) {
                        BT_ERR("Unable to connect to dbus: %s", err->message);
                        g_clear_error(&err);
@@ -1556,7 +1692,6 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
        /* Register ObjectManager interface */
        node_info = __bt_gatt_create_method_node_info(
                                        manager_introspection_xml);
-
        if (node_info == NULL) {
                BT_ERR("failed to get node info");
                goto failed;
@@ -1570,7 +1705,7 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
                                                        &manager_interface_vtable,
                                                        NULL, NULL, &error);
        }
-
+       g_dbus_node_info_unref(node_info);
        if (manager_id == 0) {
                BT_ERR("failed to register: %s", error->message);
                g_error_free(error);
@@ -1651,7 +1786,6 @@ BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
 
        node_info = __bt_gatt_create_method_node_info(
                                        service_introspection_xml);
-
        if (node_info == NULL)
                return BLUETOOTH_ERROR_INTERNAL;
 
@@ -1662,6 +1796,7 @@ BT_EXPORT_API int bluetooth_gatt_add_service(const char *svc_uuid,
                                        node_info->interfaces[0],
                                        &serv_interface_vtable,
                                        NULL, NULL, &error);
+       g_dbus_node_info_unref(node_info);
 
        if (object_id == 0) {
                BT_ERR("failed to register: %s", error->message);
@@ -1755,7 +1890,6 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
 
        node_info = __bt_gatt_create_method_node_info(
                                        characteristics_introspection_xml);
-
        if (node_info == NULL)
                return BLUETOOTH_ERROR_INTERNAL;
 
@@ -1766,6 +1900,7 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
                                        node_info->interfaces[0],
                                        &char_interface_vtable,
                                        NULL, NULL, &error);
+       g_dbus_node_info_unref(node_info);
 
        if (object_id == 0) {
                BT_ERR("failed to register: %s", error->message);
@@ -1956,7 +2091,6 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
 
        node_info = __bt_gatt_create_method_node_info(
                                        descriptor_introspection_xml);
-
        if (node_info == NULL) {
                g_strfreev(line_argv);
                g_free(serv_path);
@@ -1970,6 +2104,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
                                node_info->interfaces[0],
                                &desc_interface_vtable,
                                NULL, NULL, &error);
+       g_dbus_node_info_unref(node_info);
 
        if (object_id == 0) {
                BT_ERR("failed to register: %s", error->message);
@@ -2031,6 +2166,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        g_strfreev(line_argv);
        g_variant_builder_unref(inner_builder);
        g_variant_builder_unref(builder);
+       g_variant_builder_unref(builder2);
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -2201,7 +2337,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);
                        }
                }
@@ -2234,6 +2370,7 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
        int i = 0;
        gchar **line_argv = NULL;
        gchar *serv_path = NULL;
+       const char *value = NULL;
 
        line_argv = g_strsplit_set(char_path, "/", 0);
        serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
@@ -2292,7 +2429,18 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
                char_info->value_length = value_length;
 
-               char_info->char_value = (char *)realloc(char_info->char_value, value_length);
+               value = (char *)realloc(char_info->char_value, value_length);
+               if (value == 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_MEMORY_ALLOCATION;
+               }
+
+               char_info->char_value = (char*)value;
                if (char_info->char_value) {
                        for (i = 0; i < value_length; i++)
                                char_info->char_value[i] = char_value[i];
@@ -2369,7 +2517,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;
        }
 
@@ -2458,37 +2606,18 @@ 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) {
-                       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);
-
-                       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);
+               char err_msg[20] = { 0, };
+               g_snprintf(err_msg, sizeof(err_msg), "ATT error: 0x%02x", resp_state);
+               g_dbus_method_invocation_return_dbus_error(req_info->context,
+                                               "org.bluez.Error.Failed", err_msg);
 
-                       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);
-               }
                gatt_requests = g_slist_remove(gatt_requests, req_info);
 
                req_info->context = NULL;
@@ -2497,9 +2626,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;
 }