Add timeout logic for le coc connection request 17/309817/1
authorAnuj Jain <anuj01.jain@samsung.com>
Wed, 27 Mar 2024 10:27:52 +0000 (15:57 +0530)
committerWootak Jung <wootak.jung@samsung.com>
Wed, 17 Apr 2024 05:01:11 +0000 (14:01 +0900)
This patch adds 10 sec timeout upon receiving l2cap_le
connection request.

Change-Id: Id4cf266603e64eab8685325f218b1180c9c5b125
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
src/device.c

index 9860500..edcde01 100644 (file)
@@ -441,6 +441,7 @@ struct l2cap_le_conn_info {
        int psm;
        guint auth_id;
        char *auth_uuid;
+       guint conn_timer;
 };
 
 struct l2cap_le_profile_info {
@@ -3567,7 +3568,7 @@ static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type,
        }
 
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
-       if (!req) 
+       if (!req)
                return;
 
        /* If bdaddr_type is LE but req is for SDP, don't complete browse req. */
@@ -4603,6 +4604,20 @@ static gboolean l2cap_le_disconnected_cb(GIOChannel *chan,
        return FALSE;
 }
 
+static gboolean handle_conn_fail(gpointer data)
+{
+       DBG("+");
+       struct l2cap_le_conn_info *conn = data;
+
+       conn->conn_timer = 0;
+       g_dbus_emit_signal(dbus_conn, conn->dev_path,
+                       DEVICE_INTERFACE, "ConnectionFail",
+                       DBUS_TYPE_STRING, &(conn->profile_info->path),
+                       DBUS_TYPE_INVALID);
+       DBG("-");
+       return FALSE;
+}
+
 static void l2cap_le_connect_cb(GIOChannel *chan, GError *gerr,
                                                gpointer user_data)
 {
@@ -4614,6 +4629,16 @@ static void l2cap_le_connect_cb(GIOChannel *chan, GError *gerr,
        int fd;
        GError *io_err = NULL;
        char addr[18];
+       int is_request_by_app = 0;
+
+       dev_path = conn->dev_path;
+
+       if (conn->conn_timer) {
+               DBG("Removing timer %d", conn->conn_timer);
+               is_request_by_app = 1;
+               g_source_remove(conn->conn_timer);
+               conn->conn_timer = 0;
+       }
 
        if (!bt_io_get(chan, &io_err,
                                BT_IO_OPT_DEST, addr,
@@ -4654,8 +4679,6 @@ static void l2cap_le_connect_cb(GIOChannel *chan, GError *gerr,
                goto drop;
        }
 
-       dev_path = conn->dev_path;
-
        dbus_message_iter_init_append(msg, &iter);
 
        dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &dev_path);
@@ -4676,6 +4699,11 @@ drop:
        if (io_err)
                g_error_free(io_err);
 
+       if(is_request_by_app) {
+               error("Call Connection Failed Event");
+               handle_conn_fail((gpointer)conn);
+       }
+
        info->conn = g_slist_remove(info->conn, conn);
        l2cap_le_io_destroy(conn);
 }
@@ -4870,6 +4898,13 @@ static int _connect_l2cap_le(struct btd_device *device,
        conn = g_malloc0(sizeof(struct l2cap_le_conn_info));
        info->conn = g_slist_append(info->conn, conn);
 
+       info->role = BT_L2CAP_LE_CLIENT_ROLE;
+       conn->dev_path = dev_path;
+       conn->profile_info = info;
+
+       DBG("Add timer for 10 sec");
+       conn->conn_timer = g_timeout_add(10000, handle_conn_fail, conn);
+
        conn->io = bt_io_connect(l2cap_le_connect_cb,
                                        conn, NULL, &gerr,
                                        BT_IO_OPT_SOURCE_BDADDR, src,
@@ -4884,12 +4919,11 @@ static int _connect_l2cap_le(struct btd_device *device,
                error("L2CAP_LE Connect failed : %s", gerr->message);
                g_error_free(gerr);
                _remove_l2cap_le_socket(info);
+               g_source_remove(conn->conn_timer);
+               conn->conn_timer = 0;
                return -EIO;
        }
 
-       info->role = BT_L2CAP_LE_CLIENT_ROLE;
-       conn->dev_path = dev_path;
-       conn->profile_info = info;
        conn->connected = false;
        g_io_channel_set_close_on_unref(conn->io, FALSE);
 
@@ -5606,6 +5640,7 @@ static const GDBusSignalTable device_signals[] = {
                        GDBUS_ARGS({ "bdaddr_type", "y" }, { "reason", "y" },
                                { "name", "s" })) },
        { GDBUS_SIGNAL("DeviceConnected", GDBUS_ARGS({ "bdaddr_type", "y"})) },
+       { GDBUS_SIGNAL("ConnectionFail", GDBUS_ARGS({ "path", "s" }))},
        { GDBUS_SIGNAL("ProfileStateChanged",
                        GDBUS_ARGS({ "profile", "s"}, {"state", "i"})) },
        { GDBUS_SIGNAL("AdvReport",