From 6158ff03f53dd2cf56edc1a46e8b94cf3124f80b Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 4 Dec 2023 13:30:31 +0100 Subject: [PATCH] Bluetooth: Shell: Only auto-connect to unconnected devices The bt connect and bt connect-name commands should only attempt to connect to connectable devices that we are not already connected to. This commit moves the check for BT_GAP_ADV_PROP_CONNECTABLE as well as checks that we do not already have a connection to that device with the current ID. Signed-off-by: Emil Gydesen --- subsys/bluetooth/shell/bt.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/subsys/bluetooth/shell/bt.c b/subsys/bluetooth/shell/bt.c index 7cba031f6d9..1d4c61a84f7 100644 --- a/subsys/bluetooth/shell/bt.c +++ b/subsys/bluetooth/shell/bt.c @@ -493,22 +493,31 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_si shell_info(ctx_shell, "%*s[SCAN DATA END]", strlen(scan_response_label), ""); } - /* Store address for later use */ #if defined(CONFIG_BT_CENTRAL) - auto_connect.addr_set = true; - bt_addr_le_copy(&auto_connect.addr, info->addr); + if ((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0U) { + struct bt_conn *conn = bt_conn_lookup_addr_le(selected_id, info->addr); - /* Use the above auto_connect.addr address to automatically connect */ - if ((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0U && auto_connect.connect_name) { - auto_connect.connect_name = false; + /* Only store auto-connect address for devices we are not already connected to */ + if (conn == NULL) { + /* Store address for later use */ + auto_connect.addr_set = true; + bt_addr_le_copy(&auto_connect.addr, info->addr); - cmd_scan_off(ctx_shell); + /* Use the above auto_connect.addr address to automatically connect */ + if (auto_connect.connect_name) { + auto_connect.connect_name = false; - /* "name" is what would be in argv[0] normally */ - cmd_scan_filter_clear_name(ctx_shell, 1, (char *[]){ "name" }); + cmd_scan_off(ctx_shell); - /* "connect" is what would be in argv[0] normally */ - cmd_connect_le(ctx_shell, 1, (char *[]){ "connect" }); + /* "name" is what would be in argv[0] normally */ + cmd_scan_filter_clear_name(ctx_shell, 1, (char *[]){"name"}); + + /* "connect" is what would be in argv[0] normally */ + cmd_connect_le(ctx_shell, 1, (char *[]){"connect"}); + } + } else { + bt_conn_unref(conn); + } } #endif /* CONFIG_BT_CENTRAL */ }