Fix gatt write response not occur issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index 0f0f906..aa514d9 100644 (file)
@@ -155,15 +155,15 @@ int bluetooth_gatt_convert_perm2string(
 
 #define NUMBER_OF_FLAGS        10
 
-GDBusConnection *g_conn;
-guint owner_id;
-guint manager_id;
+static GDBusConnection *g_conn;
+static guint owner_id;
+static guint manager_id;
 static gboolean new_service = FALSE;
 static gboolean new_char = FALSE;
 static int serv_id = 1;
 static bool is_server_started = false;
 
-GCancellable *register_cancel;
+static GCancellable *register_cancel;
 
 /* Introspection data for the service we are exporting */
 static const gchar service_introspection_xml[] =
@@ -358,10 +358,10 @@ static int bluetooth_get_characteristic_fd(int att_handle , char *path)
 {
        GSList *l;
 
-       BT_INFO("request found  path [%s] att_handle [ %d]", path, att_handle);
+       BT_DBG("request found  path [%s] att_handle [ %d]", path, att_handle);
        for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
                bluetooth_gatt_acquire_notify_info_t *info = l->data;
-               BT_INFO(" sid [ %d]" , info->att_hand);
+
                if (info->att_hand == att_handle)
                        return info->write_fd;
        }
@@ -372,10 +372,10 @@ static bluetooth_gatt_acquire_notify_info_t * bluetooth_get_characteristic_info_
 {
        GSList *l;
 
-       BT_INFO("request found  att_handle [ %d]", att_handle);
+       BT_DBG("request found  att_handle [ %d]", att_handle);
        for (l = gatt_characteristic_server_notify_list; l != NULL; l = l->next) {
                bluetooth_gatt_acquire_notify_info_t *info = l->data;
-               BT_INFO(" sid [ %d]" , info->att_hand);
+               BT_DBG(" sid [ %d]" , info->att_hand);
                if (info->att_hand == att_handle)
                        return info;
        }
@@ -405,12 +405,20 @@ static gboolean bluetooth_gatt_write_channel_watch_cb(GIOChannel *gio,
                g_io_channel_shutdown(gio, TRUE, NULL);
                g_io_channel_unref(gio);
 
-               gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
-               bluetooth_characteristic_info_free(chr_info);
+               if (g_slist_find(gatt_characteristic_server_notify_list, chr_info)) {
+                       BT_INFO("found char_info in the list");
+                       gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
+                       bluetooth_characteristic_info_free(chr_info);
+               }
 
                return FALSE;
        }
 
+       if (g_slist_find(gatt_characteristic_server_notify_list, chr_info) == NULL) {
+               BT_INFO("chr_info is not in the list");
+               return FALSE;
+       }
+
        return TRUE;
 }
 
@@ -427,9 +435,8 @@ static int bluetooth_gatt_write_characteristics_value_to_fd_(
                written = write(fd, value, length);
                if (written != length) {
                        att_result = BLUETOOTH_ERROR_INTERNAL;
-                       BT_INFO("write data failed  %d is ", written);
-               } else
-                  BT_INFO("write data %s is sucess ", value);
+                       BT_ERR("write data failed  %d is ", written);
+               }
 
                return att_result;
 }
@@ -1558,12 +1565,18 @@ BT_EXPORT_API int bluetooth_gatt_unregister_application(void)
                        if (err != NULL) {
                                BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
                                                err->code, err->message);
+                               if (err->code == G_DBUS_ERROR_SERVICE_UNKNOWN ||
+                                       g_strrstr(err->message, BT_ERROR_DOES_NOT_EXIST)) {
+                                       g_clear_error(&err);
+                                       goto done;
+                               }
                                g_clear_error(&err);
                        }
                        return BLUETOOTH_ERROR_INTERNAL;
                }
                g_variant_unref(ret);
 
+done:
                is_server_started = false;
 
                BT_INFO("UnregisterApplication is completed");
@@ -2815,12 +2828,8 @@ BT_EXPORT_API int bluetooth_gatt_server_add_new_characteristic(const char *char_
 
        int result;
        char uuid[BT_GATT_ATT_UUID_LEN_MAX + 1];
-       int flag_count = 0;
-       char *char_flags[NUMBER_OF_FLAGS];
 
        g_strlcpy(uuid, char_uuid, sizeof(uuid));
-       flag_count = bluetooth_gatt_convert_prop2string(param->properties, char_flags);
-       BT_INFO("Flag count [%d]", flag_count);
 
        BT_INIT_PARAMS();
        BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
@@ -3045,7 +3054,7 @@ static gboolean bluetooth_gatt_server_acquire_channel_write_cb(GIOChannel *gio,
 
        bluetooth_gatt_server_acquire_write_info_t *write_data =  (bluetooth_gatt_server_acquire_write_info_t*)data;
 
-       BT_INFO("FD io write data  received  remote adress  [%s]\n", write_data->address);
+       BT_DBG("FD io write data  received [%s]", write_data->address);
 
        if (cond & G_IO_IN) {
                GIOStatus status = G_IO_STATUS_NORMAL;
@@ -3078,10 +3087,6 @@ static gboolean bluetooth_gatt_server_acquire_channel_write_cb(GIOChannel *gio,
                }
 
                if (len > 0) {
-
-                       BT_INFO(" FD io sending  value changed %s %zd \n", buffer, len);
-
-
                        bluetooth_gatt_server_write_requested_info_t write_info;
                        if (len < BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX)
                        memcpy(write_info.data.data, buffer, len);
@@ -3095,8 +3100,6 @@ static gboolean bluetooth_gatt_server_acquire_channel_write_cb(GIOChannel *gio,
                        write_info.offset = write_data->offset;
                        write_info.request_id = -2;
 
-                       BT_INFO("ACQUIRING EVENT \n");
-
                        bt_event_info_t *event_info;
                        event_info = _bt_event_get_cb_data(BT_GATT_SERVER_EVENT);
 
@@ -3271,7 +3274,6 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
                                } else
                                        chr_info->write_fd = fd;
 
-
                                channel = g_io_channel_unix_new(fd);
                                g_io_channel_set_encoding(channel, NULL, NULL);
                                g_io_channel_set_buffered(channel, FALSE);
@@ -3280,9 +3282,6 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
                                g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
                                                bluetooth_gatt_write_channel_watch_cb, chr_info);
 
-
-
-
                                 GUnixFDList *fd_list = g_unix_fd_list_new();
                                 GError *error = NULL;
 
@@ -3292,7 +3291,7 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
 
                                g_array_append_vals(in_param1, &data, sizeof(bluetooth_gatt_server_acquire_response_params_t));
 
-                               BT_INFO("Sending event BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE file descriptor value [%d] ", data.fd);
+                               BT_DBG("Sending event BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE file descriptor value [%d] ", data.fd);
 
                                result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE,
                                                in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL);
@@ -3302,8 +3301,7 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
 
                                //send
                                if (result == BLUETOOTH_ERROR_NONE) {
-
-                                       BT_INFO("sending gatt server notification state changed event");
+                                       BT_DBG("sending gatt server notification state changed event");
                                        bluetooth_gatt_server_notification_changed_t info;
                                        bluetooth_device_address_t dev_address = { {0} };
                                        memset(&info, 0x00, sizeof(bluetooth_gatt_server_notification_changed_t));
@@ -3313,6 +3311,7 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
                                                        dev_address.addr,
                                                        BLUETOOTH_ADDRESS_LENGTH);
                                        info.handle = att_han;
+                                       info.notification = TRUE;
 
                                        _bt_gatt_server_event_cb(BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
                                                        result, &info,
@@ -3320,3 +3319,25 @@ void  bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters,
 
                                }
 }
+
+void cleanup_gatt_acquire_fd(int handle)
+{
+       bluetooth_gatt_acquire_notify_info_t *chr_info = NULL;
+
+       BT_INFO("+");
+
+       chr_info = bluetooth_get_characteristic_info_from_path(handle);
+
+       if (chr_info != NULL) {
+               BT_INFO("GATT Server: acquire notification char info found");
+
+               if (chr_info->write_fd >= 0) {
+                       BT_INFO("closing fd");
+                       close(chr_info->write_fd);
+               }
+
+               BT_INFO("Removing char_info from the list");
+               gatt_characteristic_server_notify_list = g_slist_remove(gatt_characteristic_server_notify_list, chr_info);
+               bluetooth_characteristic_info_free(chr_info);
+       }
+}