Fix Coverity issues in Bluetooth-frwk
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-client.c
index ec6f955..6f1a605 100644 (file)
@@ -138,7 +138,6 @@ typedef struct {
 /* Linked list of connected GATT client connection */
 static GSList * hal_gattc_client_info_list = NULL;
 
-
 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
 static guint pending_le_conn_timer_id = 0;
 static int bt_conn_id = 0;
@@ -148,12 +147,12 @@ static int bt_inst_id = 0;
 
 typedef struct {
        int conn_id;
+       int result;
        btgatt_srvc_id_t srvc_id;
        btgatt_gatt_id_t char_id;
        btgatt_gatt_id_t desc_id;
 } hal_gatt_resp_data_t;
 
-
 typedef struct {
        int client_if;
        bt_uuid_t app_uuid;
@@ -175,6 +174,8 @@ static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int  conn_
 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
                                                const char* uuid_str, int inst_id);
 static void _bt_hal_send_search_service_complete_event(int conn_id, int status);
+static hal_gattc_server_info_t *__bt_find_gatt_conn_info(bt_bdaddr_t *serv_addr);
+static hal_gattc_client_info_t *__bt_find_gatt_client_info(bt_bdaddr_t *serv_addr);
 
 
 /* To send stack event to hal-av handler */
@@ -541,7 +542,8 @@ static hal_gattc_service_t* _gattc_find_service_from_uuid(hal_gattc_server_info_
 }
 
 
-static hal_gattc_char_t* _gattc_find_char_from_uuid(hal_gattc_service_t *gattc_svc, bt_uuid_t *char_uuid)
+static hal_gattc_char_t* _gattc_find_char_from_uuid(hal_gattc_service_t *gattc_svc, bt_uuid_t *char_uuid,
+                               bt_gatt_characteristic_property_t prop)
 {
        DBG("+");
 
@@ -553,7 +555,8 @@ static hal_gattc_char_t* _gattc_find_char_from_uuid(hal_gattc_service_t *gattc_s
                if (info == NULL)
                        continue;
 
-               if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t))) {
+               if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t)) &&
+                       (info->permission & prop)) {
                        INFO("Found GATT char uuid");
                        return info;
                }
@@ -968,7 +971,7 @@ static bt_status_t _hal_gattc_get_characteristic_info(hal_gattc_char_t *gattc_ch
        GVariantIter *property_iter;
        GVariantIter *char_desc_iter;
        char* char_handle = NULL;
-       const gchar *char_uuid_str = NULL;
+       gchar *char_uuid_str = NULL;
        GPtrArray *gp_desc_array  = NULL;
        GVariantIter *char_perm_iter;
        gchar* permission;
@@ -1047,6 +1050,7 @@ static bt_status_t _hal_gattc_get_characteristic_info(hal_gattc_char_t *gattc_ch
 
        _hal_gattc_update_char_property(gattc_char, char_uuid_str, gp_desc_array, char_permission);
 
+       g_free(char_uuid_str);
        g_variant_iter_free(property_iter);
        g_variant_unref(result);
        g_object_unref(properties_proxy);
@@ -1192,13 +1196,13 @@ static bt_status_t _hal_gattc_get_descriptor_info(hal_gattc_desc_t *gattc_desc)
 
        while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
                if (!g_strcmp0(key, "UUID")) {
-                       desc_uuid_str = g_variant_dup_string(value, &len);
+                       desc_uuid_str = g_variant_get_string(value, &len);
                        DBG("desc UUID [%s]", desc_uuid_str);
+                       _hal_gattc_update_desc_property(gattc_desc, desc_uuid_str);
+                       break;
                }
        }
 
-       _hal_gattc_update_desc_property(gattc_desc, desc_uuid_str);
-
        g_variant_iter_free(property_iter);
        g_variant_unref(result);
        g_object_unref(properties_proxy);
@@ -1243,6 +1247,7 @@ static bt_status_t _hal_gattc_get_all_descriptor(int conn_id,
        hal_gattc_server_info_t * conn_info = NULL;
        hal_gattc_service_t *gattc_service = NULL;
        GSList *l;
+       GSList *m;
        hal_gattc_char_t *gattc_char = NULL;
        hal_gattc_desc_t *gattc_desc = NULL;
        char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
@@ -1269,26 +1274,33 @@ static bt_status_t _hal_gattc_get_all_descriptor(int conn_id,
        DBG("service uuid [%s]", svc_uuid_str);
 
        /* find characteristics */
-       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
-       if (NULL == gattc_char) {
-               DBG("Failed to get the gatt char");
-               return BT_STATUS_FAIL;
-       }
+       /* a service can have two char with same uuid */
+       for (l = gattc_service->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
+               gattc_char = (hal_gattc_char_t*)l->data;
+               if (gattc_char == NULL)
+                       continue;
 
-       DBG("char path [%s]", gattc_char->chr_path);
-       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
-       DBG("char uuid [%s]", char_uuid_str);
+               if (!memcmp(&gattc_char->chr_uuid, &char_id->uuid, sizeof(bt_uuid_t))) {
+                       INFO("Found GATT char uuid");
+                       DBG("char path [%s]", gattc_char->chr_path);
+                       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+                       DBG("char uuid [%s]", char_uuid_str);
+
+                       /* get descriptor uuid */
+                       for (m = gattc_char->gatt_list_descs; m != NULL; m = g_slist_next(m)) {
+                               gattc_desc = (hal_gattc_desc_t *)m->data;
+                               if (gattc_desc == NULL)
+                                       continue;
 
-       /* get descriptor uuid */
-       for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
-               gattc_desc = (hal_gattc_desc_t *)l->data;
-               status = _hal_gattc_get_descriptor_info(gattc_desc);
+                               status = _hal_gattc_get_descriptor_info(gattc_desc);
 
-               /* send event */
-               if (BT_STATUS_SUCCESS == status) {
-                       DBG("Sending the success descriptor event");
-                       _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id,
-                                       char_id, &gattc_desc->desc_uuid);
+                               /* send event */
+                               if (BT_STATUS_SUCCESS == status) {
+                                       DBG("Sending the success descriptor event");
+                                       _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id,
+                                                       char_id, &gattc_desc->desc_uuid);
+                               }
+                       }
                }
        }
 
@@ -1373,7 +1385,7 @@ static void __hal_internal_read_char_cb(GObject *source_object,
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
        if (error) {
-               ERR("Read Characteristic dbus failed Error:", error->message);
+               ERR("Read Characteristic dbus failed Error:[%s]", error->message);
 
                //send failed event
                result  = BT_STATUS_FAIL;
@@ -1443,7 +1455,8 @@ static bt_status_t _hal_read_characteristic_value(int conn_id, btgatt_srvc_id_t
 
 
        /* find characteristic */
-       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
+       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid,
+                       HAL_GATT_CHARACTERISTIC_PROPERTY_READ);
        if (NULL == gattc_char) {
                DBG("Failed to get the gatt char");
                return BT_STATUS_FAIL;
@@ -1540,7 +1553,7 @@ static void __hal_bluetooth_internal_write_cb(GObject *source_object,
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
        if (error) {
-               ERR("write Characteristic dbus failed Error:", error->message);
+               ERR("write Characteristic dbus failed Error:[%s]", error->message);
 
                result  = BT_STATUS_FAIL;
                //send failed event
@@ -1559,14 +1572,14 @@ static void __hal_bluetooth_internal_write_cb(GObject *source_object,
        DBG("-");
 }
 
-static bt_status_t __hal_get_write_prop(hal_gatt_write_type_t type, hal_gatt_property_e *prop)
+static bt_status_t __hal_get_write_prop(hal_gatt_write_type_t type, bt_gatt_characteristic_property_t *prop)
 {
        switch (type) {
        case HAL_GATT_WRITE_TYPE_WRITE:
-               *prop = HAL_GATT_PROPERTY_WRITE;
+               *prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
                break;
        case HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE:
-               *prop = HAL_GATT_PROPERTY_WRITE_WITHOUT_RESPONSE;
+               *prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
                break;
        default:
                ERR("Unknow write type : %d", type);
@@ -1592,7 +1605,7 @@ static bt_status_t _hal_write_characteristic_value(int conn_id, btgatt_srvc_id_t
        char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
        char char_uuid_str[BT_HAL_UUID_STRING_LEN];
        char* char_handle = NULL;
-       hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
+       bt_gatt_characteristic_property_t write_prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
        int ret = BT_STATUS_SUCCESS;
 
        DBG("+");
@@ -1622,7 +1635,7 @@ static bt_status_t _hal_write_characteristic_value(int conn_id, btgatt_srvc_id_t
        DBG("service uuid [%s]", svc_uuid_str);
 
        /* find characteristic */
-       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
+       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid, write_prop);
        if (NULL == gattc_char) {
                DBG("Failed to get the gatt char");
                return BT_STATUS_FAIL;
@@ -1744,7 +1757,7 @@ static void __hal_internal_read_desc_cb(GObject *source_object,
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
        if (error) {
-               ERR("Read descriptor dbus failed Error:", error->message);
+               ERR("Read descriptor dbus failed Error:[%s]", error->message);
 
                //send failed event
                result  = BT_STATUS_FAIL;
@@ -1791,6 +1804,7 @@ static bt_status_t _hal_read_descriptor_value(int conn_id, btgatt_srvc_id_t *srv
        char char_uuid_str[BT_HAL_UUID_STRING_LEN];
        char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
        char* desc_handle = NULL;
+       GSList *l;
 
        hal_gattc_desc_t *gattc_desc = NULL;
 
@@ -1815,27 +1829,34 @@ static bt_status_t _hal_read_descriptor_value(int conn_id, btgatt_srvc_id_t *srv
        DBG("service uuid [%s]", svc_uuid_str);
 
        /* find characteristic */
-       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
-       if (NULL == gattc_char) {
-               DBG("Failed to get the gatt char");
-               return BT_STATUS_FAIL;
-       }
+       /* service can have two char with same uuid */
+       for (l = gattc_service->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
+               gattc_char = (hal_gattc_char_t*)l->data;
+               if (gattc_char == NULL)
+                       continue;
 
-       DBG("char path [%s]", gattc_char->chr_path);
-       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
-       DBG("char uuid [%s]", char_uuid_str);
+               if (!memcmp(&gattc_char->chr_uuid, &char_id->uuid, sizeof(bt_uuid_t))) {
+                       INFO("Found GATT char uuid");
+                       DBG("char path [%s]", gattc_char->chr_path);
+                       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+                       DBG("char uuid [%s]", char_uuid_str);
+
+                       /* find descriptor */
+                       gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &desc_id->uuid);
+                       if (gattc_desc) {
+                               DBG("desc path [%s]", gattc_desc->desc_path);
+                               _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
+                               DBG("desc uuid [%s]", desc_uuid_str);
+                               break;
+                       }
+               }
+       }
 
-       /* find descriptor */
-       gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &desc_id->uuid);
        if (NULL == gattc_desc) {
                DBG("Failed to get the gatt desc");
                return BT_STATUS_FAIL;
        }
 
-       DBG("desc path [%s]", gattc_desc->desc_path);
-       _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
-       DBG("desc uuid [%s]", desc_uuid_str);
-
        g_conn = _bt_hal_get_system_gconn();
        if (NULL == g_conn) {
                 ERR("_bt_gdbus_get_system_gconn returned NULL");
@@ -1924,7 +1945,7 @@ static void __hal_bluetooth_internal_desc_write_cb(GObject *source_object,
        value = g_dbus_connection_call_finish(system_gconn, res, &error);
 
        if (error) {
-               ERR("write descriptor dbus failed Error:", error->message);
+               ERR("write descriptor dbus failed Error: [%s]", error->message);
 
                //send failed event
                result  = BT_STATUS_FAIL;
@@ -1962,8 +1983,9 @@ static bt_status_t _hal_write_descriptor_value(int conn_id, btgatt_srvc_id_t *sr
        char char_uuid_str[BT_HAL_UUID_STRING_LEN];
        char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
        char* desc_handle = NULL;
-       hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
+       bt_gatt_characteristic_property_t write_prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
        int ret = BT_STATUS_SUCCESS;
+       GSList *l;
 
        DBG("+");
 
@@ -1992,27 +2014,34 @@ static bt_status_t _hal_write_descriptor_value(int conn_id, btgatt_srvc_id_t *sr
        DBG("service uuid [%s]", svc_uuid_str);
 
        /* find characteristic */
-       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
-       if (NULL == gattc_char) {
-               DBG("Failed to get the gatt char");
-               return BT_STATUS_FAIL;
-       }
+       /* service can have two char with same uuid */
+       for (l = gattc_service->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
+               gattc_char = (hal_gattc_char_t*)l->data;
+               if (gattc_char == NULL)
+                       continue;
 
-       DBG("char path [%s]", gattc_char->chr_path);
-       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
-       DBG("char uuid [%s]", char_uuid_str);
+               if (!memcmp(&gattc_char->chr_uuid, &char_id->uuid, sizeof(bt_uuid_t))) {
+                       INFO("Found GATT char uuid");
+                       DBG("char path [%s]", gattc_char->chr_path);
+                       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+                       DBG("char uuid [%s]", char_uuid_str);
+
+                       /* find descriptor */
+                       gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &descr_id->uuid);
+                       if (gattc_desc) {
+                               DBG("desc path [%s]", gattc_desc->desc_path);
+                               _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
+                               DBG("desc uuid [%s]", desc_uuid_str);
+                               break;
+                       }
+               }
+       }
 
-       /* find descriptor */
-       gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &descr_id->uuid);
        if (NULL == gattc_desc) {
-               DBG("Failed to get the gatt char");
+               DBG("Failed to get the gatt desc");
                return BT_STATUS_FAIL;
        }
 
-       DBG("desc path [%s]", gattc_desc->desc_path);
-       _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
-       DBG("char uuid [%s]", desc_uuid_str);
-
        g_conn = _bt_hal_get_system_gconn();
        if (NULL == g_conn) {
                 ERR("_bt_gdbus_get_system_gconn returned NULL");
@@ -2079,25 +2108,314 @@ bt_status_t execute_write(int conn_id, int execute)
        return BT_STATUS_UNSUPPORTED;
 }
 
+static gboolean _hal_watch_register_notifi_cb(gpointer user_data)
+{
+       struct hal_ev_gatt_client_watch_notification ev;
+       hal_gatt_resp_data_t *resp_data = user_data;
+
+       DBG("sending the watch register notification event");
+       /* send the event */
+       memset(&ev, 0, sizeof(ev));
+       ev.client_if = resp_data->conn_id; /* conn_id is saved with client_if */
+       ev.registered = 1;
+       ev.status = resp_data->result;
+
+       ev.is_primary = resp_data->srvc_id.is_primary;
+       ev.inst_id = resp_data->srvc_id.id.inst_id;
+
+       memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
+       memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
+
+       if (!event_cb)
+               ERR("GATT Callback not registered");
+       else
+               event_cb(HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION, (void *)&ev, sizeof(ev));
+
+       g_free(user_data);
+
+       return FALSE;
+}
+
+static bt_status_t _hal_register_for_notification(int client_if,
+               bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
+               btgatt_gatt_id_t *char_id)
+{
+       int result = BT_STATUS_SUCCESS;
+       GError *error = NULL;
+       GDBusConnection *g_conn;
+       hal_gattc_client_info_t *gattc_client = NULL;
+       hal_gattc_server_info_t * conn_info = NULL;
+       hal_gattc_service_t *gattc_service = NULL;
+       hal_gattc_char_t *gattc_char = NULL;
+       char* char_handle = NULL;
+       char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
+       char char_uuid_str[BT_HAL_UUID_STRING_LEN];
+       hal_gatt_resp_data_t *resp_data;
+
+       DBG("+");
+
+       gattc_client = __bt_find_gatt_client_info(bd_addr);
+       if (gattc_client == NULL) {
+               ERR("failed to get the gatt client info");
+               return BT_STATUS_FAIL;
+       }
+
+       if (gattc_client->client_if != client_if) {
+               ERR("could not find the gatt client for client id[%d]", client_if);
+               return BT_STATUS_FAIL;
+       }
+
+        /* get the connection info */
+       conn_info = __bt_find_gatt_conn_info(bd_addr);
+       if (NULL == conn_info) {
+               ERR("Failed to get the conn_info");
+               return BT_STATUS_FAIL;
+       }
+
+       if (conn_info->inst_id != gattc_client->inst_id) {
+               ERR("could not fild the conn_info");
+               return BT_STATUS_FAIL;
+       }
+
+       /* find service */
+       gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
+       if (NULL == gattc_service) {
+               DBG("Failed to get the gatt service");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("service path [%s]", gattc_service->svc_path);
+       _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
+       DBG("service uuid [%s]", svc_uuid_str);
+
+
+       /* find characteristic */
+       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid,
+                       HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY);
+       if (NULL == gattc_char) {
+               DBG("Failed to get the gatt char");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("char path [%s]", gattc_char->chr_path);
+       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+       DBG("char uuid [%s]", char_uuid_str);
+
+       char_handle = gattc_char->chr_path;
+
+       g_conn = _bt_hal_get_system_gconn();
+       if (g_conn == NULL) {
+               ERR("conn NULL");
+               return BT_STATUS_FAIL;
+       }
+
+       resp_data = malloc(sizeof(hal_gatt_resp_data_t));
+       if (NULL == resp_data) {
+               ERR("failed to get the memory");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("#StartNotify");
+       g_dbus_connection_call_sync(g_conn,
+                       BT_HAL_BLUEZ_NAME,
+                       char_handle,
+                       BT_HAL_GATT_CHAR_INTERFACE,
+                       "StartNotify",
+                       NULL,
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       BT_HAL_MAX_DBUS_TIMEOUT, NULL, &error);
+
+       if (error) {
+               g_dbus_error_strip_remote_error(error);
+               ERR("### Watch Failed: %s", error->message);
+               if (g_strrstr(error->message, "Already notifying"))
+                       result = BT_STATUS_SUCCESS;
+               else if (g_strrstr(error->message, "In Progress"))
+                       result = BT_STATUS_BUSY;
+               else if (g_strrstr(error->message, "Operation is not supported"))
+                       result = BT_STATUS_UNSUPPORTED;
+               /*failed because of either Insufficient Authorization or Write Not Permitted */
+               else if (g_strrstr(error->message, "Write not permitted") ||
+                               g_strrstr(error->message, "Operation Not Authorized"))
+                       result = BT_STATUS_AUTH_FAILURE;
+               /* failed because of either Insufficient Authentication,
+                  Insufficient Encryption Key Size, or Insufficient Encryption. */
+               else if (g_strrstr(error->message, "Not paired"))
+                       result = BT_STATUS_NOT_READY;
+               else
+                       result = BT_STATUS_FAIL;
+
+               g_clear_error(&error);
+       }
+
+       resp_data->conn_id = gattc_client->client_if; /* saving client_if instead of conn_id */
+       resp_data->result = result;
+       memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
+       memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
+
+       g_idle_add(_hal_watch_register_notifi_cb, (gpointer)resp_data);
+
+       DBG("-");
+
+       return BT_STATUS_SUCCESS;
+}
+
 /**
  * Register to receive notifications or indications for a given
  * characteristic
  */
-bt_status_t register_for_notification(int client_if,
+bt_status_t btif_register_for_notification(int client_if,
                const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
                btgatt_gatt_id_t *char_id)
 {
        CHECK_BTGATT_INIT();
-       return BT_STATUS_UNSUPPORTED;
+
+       return _hal_register_for_notification(client_if, (bt_bdaddr_t *)bd_addr, srvc_id, char_id);
+}
+
+static gboolean _hal_watch_deregister_notifi_cb(gpointer user_data)
+{
+       struct hal_ev_gatt_client_watch_notification ev;
+       hal_gatt_resp_data_t *resp_data = user_data;
+
+       DBG("sending the watch deregister notification event");
+       /* send the event */
+       memset(&ev, 0, sizeof(ev));
+       ev.client_if = resp_data->conn_id; /* conn_id is saved with client_if */
+       ev.registered = 0;
+       ev.status = resp_data->result;
+
+       ev.is_primary = resp_data->srvc_id.is_primary;
+       ev.inst_id = resp_data->srvc_id.id.inst_id;
+
+       memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
+       memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
+
+       if (!event_cb)
+               ERR("GATT Callback not registered");
+       else
+               event_cb(HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION, (void *)&ev, sizeof(ev));
+
+       g_free(user_data);
+
+       return FALSE;
 }
 
+static bt_status_t _hal_deregister_for_notification(int client_if,
+               bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
+               btgatt_gatt_id_t *char_id)
+{
+       int result = BT_STATUS_SUCCESS;
+       GError *error = NULL;
+       GDBusConnection *g_conn;
+       hal_gattc_client_info_t *gattc_client = NULL;
+       hal_gattc_server_info_t * conn_info = NULL;
+       hal_gattc_service_t *gattc_service = NULL;
+       hal_gattc_char_t *gattc_char = NULL;
+       char* char_handle = NULL;
+       char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
+       char char_uuid_str[BT_HAL_UUID_STRING_LEN];
+       hal_gatt_resp_data_t *resp_data;
+
+       DBG("+");
+
+       gattc_client = __bt_find_gatt_client_info(bd_addr);
+       if (gattc_client == NULL) {
+               ERR("failed to get the gatt client info");
+               return BT_STATUS_FAIL;
+       }
+
+       if (gattc_client->client_if != client_if) {
+               ERR("could not find the gatt client for client id[%d]", client_if);
+               return BT_STATUS_FAIL;
+       }
+
+        /* get the connection info */
+       conn_info = __bt_find_gatt_conn_info(bd_addr);
+       if (NULL == conn_info) {
+               ERR("Failed to get the conn_info");
+               return BT_STATUS_FAIL;
+       }
+
+       if (conn_info->inst_id != gattc_client->inst_id) {
+               ERR("could not fild the conn_info");
+               return BT_STATUS_FAIL;
+       }
+
+       /* find service */
+       gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
+       if (NULL == gattc_service) {
+               DBG("Failed to get the gatt service");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("service path [%s]", gattc_service->svc_path);
+       _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
+       DBG("service uuid [%s]", svc_uuid_str);
+
+
+       /* find characteristic */
+       gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid,
+                       HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY);
+       if (NULL == gattc_char) {
+               DBG("Failed to get the gatt char");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("char path [%s]", gattc_char->chr_path);
+       _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
+       DBG("char uuid [%s]", char_uuid_str);
+
+       char_handle = gattc_char->chr_path;
+
+       g_conn = _bt_hal_get_system_gconn();
+       if (g_conn == NULL) {
+               ERR("conn NULL");
+               return BT_STATUS_FAIL;
+       }
+
+       resp_data = malloc(sizeof(hal_gatt_resp_data_t));
+       if (NULL == resp_data) {
+               ERR("failed to get the memory");
+               return BT_STATUS_FAIL;
+       }
+
+       DBG("#StartNotify");
+       g_dbus_connection_call_sync(g_conn,
+                       BT_HAL_BLUEZ_NAME,
+                       char_handle,
+                       BT_HAL_GATT_CHAR_INTERFACE,
+                       "StopNotify",
+                       NULL,
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       BT_HAL_MAX_DBUS_TIMEOUT, NULL, &error);
+
+       if (error) {
+               ERR("### Watch Failed: %s", error->message);
+               g_clear_error(&error);
+               result = BT_STATUS_FAIL;
+       }
+
+       resp_data->conn_id = gattc_client->client_if; /* saving client_if instead of conn_id */
+       resp_data->result = result;
+       memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
+       memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
+
+       g_idle_add(_hal_watch_deregister_notifi_cb, (gpointer)resp_data);
+
+       DBG("-");
+
+       return BT_STATUS_SUCCESS;
+}
 /** Deregister a previous request for notifications/indications */
-bt_status_t deregister_for_notification(int client_if,
+bt_status_t btif_deregister_for_notification(int client_if,
                const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
                btgatt_gatt_id_t *char_id)
 {
        CHECK_BTGATT_INIT();
-       return BT_STATUS_UNSUPPORTED;
+       return _hal_deregister_for_notification(client_if, (bt_bdaddr_t *)bd_addr, srvc_id, char_id);
 }
 
 /** Request RSSI for a given remote device */
@@ -2313,8 +2631,8 @@ const btgatt_client_interface_t btgatt_client_interface = {
        btif_read_descriptor,
        btif_write_descriptor,
        execute_write,
-       register_for_notification,
-       deregister_for_notification,
+       btif_register_for_notification,
+       btif_deregister_for_notification,
        read_remote_rssi,
        ota_fw_update,
        get_device_type,