From 966d84d79da7dc6bab05db778746a46e9b0a2fbc Mon Sep 17 00:00:00 2001 From: Deokhyun Kim Date: Mon, 10 Aug 2020 16:57:01 +0900 Subject: [PATCH] Fix cleanup logic regarding notify fd Change-Id: I577cd0305e529d0dc259e4d38603ee3db5c2f08d Signed-off-by: Deokhyun Kim Signed-off-by: Wootak Jung --- bt-api/bt-gatt-service.c | 47 +++++++++++++++---------------------- include/bluetooth-gatt-server-api.h | 9 +++++++ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/bt-api/bt-gatt-service.c b/bt-api/bt-gatt-service.c index d848399..4b13102 100644 --- a/bt-api/bt-gatt-service.c +++ b/bt-api/bt-gatt-service.c @@ -343,17 +343,6 @@ static struct gatt_desc_info *__bt_gatt_find_gatt_desc_info( static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id); static int __bt_gatt_unregister_service(struct gatt_service_info *svc_info); - - -typedef struct { - int write_fd; - int relpy_fd; - int mtu; - int att_hand; - char *path ; -} bluetooth_gatt_acquire_notify_info_t; - - static int bluetooth_get_characteristic_fd(int att_handle , char *path) { GSList *l; @@ -3194,7 +3183,7 @@ void bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters) data.mtu = mtu; data.request_id = tran_id; - bluetooth_gatt_server_acquire_write_info_t *write_info = g_malloc0(sizeof(bluetooth_gatt_server_acquire_write_info_t)) ; + bluetooth_gatt_server_acquire_write_info_t *write_info = g_malloc0(sizeof(bluetooth_gatt_server_acquire_write_info_t)) ; write_info->attribute_handle = att_han; write_info->connection_id = tran_id; @@ -3204,7 +3193,6 @@ void bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters) BT_INFO("FD read %d remote address [%s ] \n", pipefd[0], addr); - channel = g_io_channel_unix_new(pipefd[0]); g_io_channel_set_encoding(channel, NULL, NULL); g_io_channel_set_buffered(channel, FALSE); @@ -3213,9 +3201,8 @@ void bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters) g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP), bluetooth_gatt_server_acquire_channel_write_cb, write_info); - - GUnixFDList *fd_list = g_unix_fd_list_new(); - GError *error = NULL; + GUnixFDList *fd_list = g_unix_fd_list_new(); + GError *error = NULL; g_unix_fd_list_append(fd_list, pipefd[1], &error); g_assert_no_error(error); @@ -3229,11 +3216,9 @@ void bluetooth_gatt_server_send_acquire_write_response(GVariant * parameters) in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL); BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); - + g_object_unref(fd_list); } - - void bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters, bt_event_info_t *event_info) { int con_id = -1; @@ -3282,7 +3267,7 @@ void bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters, data.mtu = mtu; data.request_id = tran_id; - BT_INFO("FD write %d characterstics path \n", pipefd[0]); + BT_INFO("FD write %d", pipefd[0]); chr_info = bluetooth_get_characteristic_info_from_path(att_han); if (!chr_info) { @@ -3295,15 +3280,18 @@ void bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters, chr_info->write_fd = fd; channel = g_io_channel_unix_new(fd); + chr_info->io_channel = channel; g_io_channel_set_encoding(channel, NULL, NULL); g_io_channel_set_buffered(channel, FALSE); g_io_channel_set_close_on_unref(channel, TRUE); g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL); - g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + chr_info->watch_id = + 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; + GUnixFDList *fd_list = g_unix_fd_list_new(); + GError *error = NULL; g_unix_fd_list_append(fd_list, pipefd[1], &error); g_assert_no_error(error); @@ -3317,7 +3305,7 @@ void bluetooth_gatt_server_send_acquire_notify_response(GVariant * parameters, in_param1, in_param2, in_param3, in_param4, fd_list, &out_param, NULL); BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); - + g_object_unref(fd_list); //send if (result == BLUETOOTH_ERROR_NONE) { @@ -3349,11 +3337,14 @@ void cleanup_gatt_acquire_fd(int handle) chr_info = bluetooth_get_characteristic_info_from_path(handle); if (chr_info != NULL) { - BT_INFO("GATT Server: acquire notification char info found"); + BT_INFO("GATT Server: acquire notification char info found [%s]", chr_info->path); + + if (chr_info->watch_id > 0) + g_source_remove(chr_info->watch_id); - if (chr_info->write_fd >= 0) { - BT_INFO("closing fd"); - close(chr_info->write_fd); + if (chr_info->io_channel) { + g_io_channel_shutdown(chr_info->io_channel, TRUE, NULL); + g_io_channel_unref(chr_info->io_channel); } BT_INFO("Removing char_info from the list"); diff --git a/include/bluetooth-gatt-server-api.h b/include/bluetooth-gatt-server-api.h index 6265219..08068b5 100644 --- a/include/bluetooth-gatt-server-api.h +++ b/include/bluetooth-gatt-server-api.h @@ -176,6 +176,15 @@ typedef struct { char address[BLUETOOTH_ADDRESS_STRING_LENGTH]; } bluetooth_gatt_server_acquire_write_info_t;; +typedef struct { + int write_fd; + int relpy_fd; + int mtu; + int att_hand; + void *io_channel; + guint watch_id; + char *path ; +} bluetooth_gatt_acquire_notify_info_t; /** * GATT Server Update Characteristic Value -- 2.7.4