Fix infinite callback issue in EOF status 07/291107/3
authorWootak Jung <wootak.jung@samsung.com>
Mon, 10 Apr 2023 05:59:42 +0000 (14:59 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Mon, 10 Apr 2023 06:37:39 +0000 (15:37 +0900)
Change-Id: Ic4cc4ab9c3edc20c233c825c7a4918c8ce2487e7
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
bt-api/bt-common.c
bt-api/bt-l2cap-le-server.c
bt-api/bt-rfcomm-server.c
bt-api/include/bt-common.h

index 54133fdad91cd3d092c6459c4c11f17ddbea87f8..9938795c157f20572b8ec52f92e4fcaaa9a27068 100644 (file)
@@ -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;
index c86dfe37bdbb2b4ef90de8e1a52b4f90bdbec5d8..1328c35dbc6d47d8c54fd340b89f67ccb5245ed1 100644 (file)
@@ -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) {
index 428881987dd3f9e3cd21b160cbb8f84845c96381..7a4e32a8a9fffa9784ba7ffc69e9f6f9d2b525d7 100644 (file)
@@ -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) {
index 89fd05df31da25e93c716b48ff88c6e9d8223a1d..fc2c76d74fcf7d8f7d8e5b5e1b4be3fd2666df04 100644 (file)
@@ -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);