From 8629f0a4507b84480af1c6b5b1d298d392cc4e77 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Fri, 31 Jan 2020 14:04:31 +0100 Subject: [PATCH] Bluetooth: host: Fix app notified connected but no connection exists Fix problem where application was notified about a new connection being established, but no connection has actually been made. This occurred because the LE Create Connection command failed directly from the API, in which case the state transition thinks the err is valid and always notifies the application. Introduced by: 6c1f52dff7 for bt_conn_create_le e9eebf0c40a4 for bt_conn_create_auto_le. Signed-off-by: Joakim Andersson --- subsys/bluetooth/host/conn.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 07afae9e8b3..d9722855c73 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -1692,8 +1692,13 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) k_poll_signal_raise(&conn_change, 0); /* The last ref will be dropped during cleanup */ } else if (old_state == BT_CONN_CONNECT) { - /* conn->err will be set in this case */ - notify_connected(conn); + /* LE Create Connection command failed. This might be + * directly from the API, don't notify application in + * this case. + */ + if (conn->err) { + notify_connected(conn); + } bt_conn_unref(conn); } else if (old_state == BT_CONN_CONNECT_SCAN) { /* this indicate LE Create Connection with peer address @@ -2142,7 +2147,7 @@ int bt_conn_create_auto_le(const struct bt_le_conn_param *param) err = bt_le_create_conn(conn); if (err) { BT_ERR("Failed to start whitelist scan"); - + conn->err = 0; bt_conn_set_state(conn, BT_CONN_DISCONNECTED); bt_conn_unref(conn); return err; @@ -2257,6 +2262,7 @@ struct bt_conn *bt_conn_create_le(const bt_addr_le_t *peer, bt_conn_set_state(conn, BT_CONN_CONNECT); if (bt_le_create_conn(conn)) { + conn->err = 0; bt_conn_set_state(conn, BT_CONN_DISCONNECTED); bt_conn_unref(conn);