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
e9eebf0c40 for bt_conn_create_auto_le.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
Joakim Andersson 2020-01-31 14:04:31 +01:00 committed by Johan Hedberg
commit 8629f0a450

View file

@ -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);