Add timeout logic for le coc connection request 58/308558/1
authorAnuj Jain <anuj01.jain@samsung.com>
Wed, 27 Mar 2024 10:27:52 +0000 (15:57 +0530)
committerAnuj Jain <anuj01.jain@samsung.com>
Wed, 27 Mar 2024 10:27:52 +0000 (15:57 +0530)
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 d203b206adaa404b6f59ea54a6ddfcd05a38fc37..391b4094bc2fbec09353a9068532ea1cbc34478b 100644 (file)
@@ -419,6 +419,7 @@ struct l2cap_le_conn_info {
        int psm;
        guint auth_id;
        char *auth_uuid;
+       guint conn_timer;
 };
 
 struct l2cap_le_profile_info {
@@ -3504,7 +3505,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. */
@@ -4537,6 +4538,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)
 {
@@ -4548,6 +4563,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,
@@ -4588,8 +4613,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);
@@ -4610,6 +4633,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);
 }
@@ -4804,6 +4832,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,
@@ -4818,12 +4853,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);
 
@@ -5537,6 +5571,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",
@@ -8573,7 +8608,6 @@ static void gatt_server_init(struct btd_device *device,
                error("Failed to initialize bt_gatt_server");
                return;
        }
        bt_att_set_enc_key_size(device->att, device->ltk_enc_size);
 
        bt_gatt_server_set_debug(device->server, gatt_debug, NULL, NULL);