From f9350704e1f0a46ec420fe7ef33e590d0a0421ef Mon Sep 17 00:00:00 2001 From: Wootak Jung Date: Mon, 10 Apr 2023 14:59:42 +0900 Subject: [PATCH] Fix infinite callback issue in EOF status Change-Id: Ic4cc4ab9c3edc20c233c825c7a4918c8ce2487e7 Signed-off-by: Wootak Jung --- bt-api/bt-common.c | 16 +++++++++++ bt-api/bt-l2cap-le-server.c | 49 ++++---------------------------- bt-api/bt-rfcomm-server.c | 68 ++++++++++++++++++--------------------------- bt-api/include/bt-common.h | 2 ++ 4 files changed, 51 insertions(+), 84 deletions(-) diff --git a/bt-api/bt-common.c b/bt-api/bt-common.c index 54133fd..9938795 100644 --- a/bt-api/bt-common.c +++ b/bt-api/bt-common.c @@ -2487,6 +2487,22 @@ int _bt_get_error_value_from_message(const char *error_message) return BLUETOOTH_ERROR_INTERNAL; } +char *_bt_print_gio_status(GIOStatus status) +{ + switch (status) { + case G_IO_STATUS_ERROR: + return "G_IO_STATUS_ERROR"; + case G_IO_STATUS_NORMAL: + return "G_IO_STATUS_NORMAL"; + case G_IO_STATUS_EOF: + return "G_IO_STATUS_EOF"; + case G_IO_STATUS_AGAIN: + return "G_IO_STATUS_AGAIN"; + } + + return "Unknown"; +} + BT_EXPORT_API int bluetooth_is_supported(void) { int is_supported = 0; diff --git a/bt-api/bt-l2cap-le-server.c b/bt-api/bt-l2cap-le-server.c index c86dfe3..1328c35 100644 --- a/bt-api/bt-l2cap-le-server.c +++ b/bt-api/bt-l2cap-le-server.c @@ -408,9 +408,6 @@ static int __sock_read(int server_fd, char *buf, unsigned int len, static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data) { -#ifdef TIZEN_BLUEDROID_PORTING - char len_buf[2] = {0, 0}; -#endif char *buffer = NULL; gsize len = 0; int result = BLUETOOTH_ERROR_NONE; @@ -430,55 +427,21 @@ static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond, goto fail; } -#ifdef TIZEN_BLUEDROID_PORTING - /* Read buffer length from socket */ - status = g_io_channel_read_chars(chan, len_buf, sizeof(len_buf), &len, &err); - if (status != G_IO_STATUS_NORMAL) { - BT_ERR("IO Channel read is failed with %d", status); - if (err) { - BT_ERR("IO Channel read error [%s]", err->message); - if (status == G_IO_STATUS_ERROR && - !g_strcmp0(err->message, - "Connection reset by peer")) - BT_ERR("cond : %d", cond); - - g_error_free(err); - } - goto fail; - } - - if (len == 0) { - BT_ERR("Length is zero, remote end hang up"); - goto fail; - } - - len = (unsigned char)len_buf[0] + ((unsigned char)len_buf[1] << 8); - BT_INFO("Recieved buffer len: %d", len); - - /* Read len bytes in buffer from socket */ - buffer = g_malloc0(len + 1); - status = g_io_channel_read_chars(chan, buffer, len, &len, &err); -#else buffer = g_malloc0(BT_L2CAP_LE_BUFFER_LEN + 1); status = g_io_channel_read_chars(chan, buffer, BT_L2CAP_LE_BUFFER_LEN, &len, &err); -#endif - if (status != G_IO_STATUS_NORMAL) { - BT_ERR("IO Channel read is failed with %d", status); + BT_ERR("IO Channel read is failed with %d(%s)", status, _bt_print_gio_status(status)); g_free(buffer); + + if (status == G_IO_STATUS_AGAIN) + return TRUE; + if (err) { BT_ERR("IO Channel read error [%s]", err->message); - if (status == G_IO_STATUS_ERROR && - !g_strcmp0(err->message, "Connection reset by peer")) { - BT_ERR("cond : %d", cond); - g_error_free(err); - goto fail; - } g_error_free(err); } - - return TRUE; + goto fail; } if (len == 0) { diff --git a/bt-api/bt-rfcomm-server.c b/bt-api/bt-rfcomm-server.c index 4288819..7a4e32a 100644 --- a/bt-api/bt-rfcomm-server.c +++ b/bt-api/bt-rfcomm-server.c @@ -264,13 +264,6 @@ static gboolean __rfcomm_server_disconnect(rfcomm_info_t *info) return FALSE; } -static gboolean __is_error_by_disconnect(GError *err) -{ - return !g_strcmp0(err->message, "Connection reset by peer") || - !g_strcmp0(err->message, "Connection timed out") || - !g_strcmp0(err->message, "Software caused connection abort"); -} - static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data) { @@ -312,43 +305,38 @@ static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond, } buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1); - status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN, &len, &err); if (status != G_IO_STATUS_NORMAL) { - BT_ERR("IO Channel read is failed with %d", status); - + BT_ERR("IO Channel read is failed with %d(%s)", status, _bt_print_gio_status(status)); g_free(buffer); - if (!err) + + if (status == G_IO_STATUS_AGAIN) return TRUE; - BT_ERR("IO Channel read error [%s]", err->message); - if (status == G_IO_STATUS_ERROR && - __is_error_by_disconnect(err)) { - BT_ERR("cond : %d", cond); + if (err) { + BT_ERR("IO Channel read error [%s]", err->message); g_error_free(err); + } - if (info->disconnect_idle_id > 0) { - BT_INFO("Disconnect idle still not process remove source"); - g_source_remove(info->disconnect_idle_id); - info->disconnect_idle_id = 0; - } - - conn = __find_rfcomm_conn_with_fd(info, fd); - if (conn == NULL) { - BT_ERR("No Connection info found with FD [%d]", fd); - return FALSE; - } + if (info->disconnect_idle_id > 0) { + BT_INFO("Disconnect idle still not process remove source"); + g_source_remove(info->disconnect_idle_id); + info->disconnect_idle_id = 0; + } - if (conn->disconnected == FALSE) { - close(conn->fd); - conn->disconnected = TRUE; - } - __rfcomm_server_disconnect(info); + conn = __find_rfcomm_conn_with_fd(info, fd); + if (conn == NULL) { + BT_ERR("No Connection info found with FD [%d]", fd); return FALSE; } - g_error_free(err); - return TRUE; + + if (conn->disconnected == FALSE) { + close(conn->fd); + conn->disconnected = TRUE; + } + __rfcomm_server_disconnect(info); + return FALSE; } if (len == 0) @@ -960,19 +948,17 @@ static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond, status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN, &len, &err); if (status != G_IO_STATUS_NORMAL) { - BT_ERR("IO Channel read is failed with %d", status); + BT_ERR("IO Channel read is failed with %d(%s)", status, _bt_print_gio_status(status)); g_free(buffer); + + if (status == G_IO_STATUS_AGAIN) + return TRUE; + if (err) { BT_ERR("IO Channel read error [%s]", err->message); - if (status == G_IO_STATUS_ERROR && - !g_strcmp0(err->message, "Connection reset by peer")) { - BT_ERR("cond : %d", cond); - g_error_free(err); - goto fail; - } g_error_free(err); } - return TRUE; + goto fail; } if (len == 0) { diff --git a/bt-api/include/bt-common.h b/bt-api/include/bt-common.h index 89fd05d..fc2c76d 100644 --- a/bt-api/include/bt-common.h +++ b/bt-api/include/bt-common.h @@ -434,6 +434,8 @@ void _bt_hid_free_hid_info(void); int _bt_get_error_value_from_message(const char *error_message); +char *_bt_print_gio_status(GIOStatus status); + void _bt_hdp_app_remove_obj_info(unsigned int channe_id); int _bt_hdp_app_acquire_fd(bt_hdp_connected_t *conn_info); -- 2.7.4