Skip to content

Bluetooth: ISO: Cleanup BIG before stopped callback #93324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions subsys/bluetooth/host/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ struct bt_conn *bt_conn_new(struct bt_conn *conns, size_t size)
k_work_init_delayable(&conn->deferred_work, deferred_work);
#endif /* CONFIG_BT_CONN */
#if defined(CONFIG_BT_CONN_TX)
__ASSERT(!k_work_is_pending(&conn->tx_complete_work),
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion uses k_work_is_pending() but the mock system only provides k_work_busy_get(). The k_work_is_pending() function may not be available in the test environment, potentially causing build failures in tests.

Suggested change
__ASSERT(!k_work_is_pending(&conn->tx_complete_work),
__ASSERT(!(k_work_busy_get(&conn->tx_complete_work) & K_WORK_BUSY_PENDING),

Copilot uses AI. Check for mistakes.

"tx_complete_work is pending, performing k_work_init will break the workqueue");
k_work_init(&conn->tx_complete_work, tx_complete_work);
#endif /* CONFIG_BT_CONN_TX */

Expand Down
22 changes: 14 additions & 8 deletions subsys/bluetooth/host/iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,15 @@ static void bt_iso_chan_disconnected(struct bt_iso_chan *chan, uint8_t reason)
}
#endif /* CONFIG_BT_ISO_CENTRAL */
}
} else if (IS_ENABLED(CONFIG_BT_ISO_BROADCASTER) &&
conn_type == BT_ISO_CHAN_TYPE_BROADCASTER) {
/* BIS do not get a HCI Disconnected event and will not handle cleanup of pending TX
* complete in the same way as ACL and CIS do. Call bt_conn_tx_notify directly here
* to flush the chan->iso->tx_complete for each disconnected BIS
*/
bt_conn_tx_notify(chan->iso, true);
} else {
/* No special handling for BT_ISO_CHAN_TYPE_SYNC_RECEIVER */
}
}

Expand Down Expand Up @@ -2722,6 +2731,11 @@ static void big_disconnect(struct bt_iso_big *big, uint8_t reason)
bt_iso_chan_disconnected(bis, reason);
}

/* Cleanup the BIG before calling the `stopped` so that the `big` pointer and the ISO
* channels in the `big` can be reused in the callback
*/
cleanup_big(big);

if (!sys_slist_is_empty(&iso_big_cbs)) {
struct bt_iso_big_cb *listener;

Expand Down Expand Up @@ -3138,7 +3152,6 @@ void hci_le_big_complete(struct net_buf *buf)
big = big_lookup_flag(BT_BIG_PENDING);
if (big) {
big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
cleanup_big(big);
}

return;
Expand All @@ -3156,7 +3169,6 @@ void hci_le_big_complete(struct net_buf *buf)
big->num_bis);
}
big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
cleanup_big(big);
return;
}

Expand Down Expand Up @@ -3196,7 +3208,6 @@ void hci_le_big_terminate(struct net_buf *buf)
LOG_DBG("BIG[%u] %p terminated", big->handle, big);

big_disconnect(big, evt->reason);
cleanup_big(big);
}
#endif /* CONFIG_BT_ISO_BROADCASTER */

Expand Down Expand Up @@ -3283,7 +3294,6 @@ int bt_iso_big_terminate(struct bt_iso_big *big)

if (!err) {
big_disconnect(big, BT_HCI_ERR_LOCALHOST_TERM_CONN);
cleanup_big(big);
}
} else {
err = -EINVAL;
Expand Down Expand Up @@ -3328,7 +3338,6 @@ void hci_le_big_sync_established(struct net_buf *buf)
big = big_lookup_flag(BT_BIG_SYNCING);
if (big) {
big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
cleanup_big(big);
}

return;
Expand All @@ -3346,7 +3355,6 @@ void hci_le_big_sync_established(struct net_buf *buf)
big->num_bis);
}
big_disconnect(big, evt->status ? evt->status : BT_HCI_ERR_UNSPECIFIED);
cleanup_big(big);
return;
}

Expand Down Expand Up @@ -3386,7 +3394,6 @@ void hci_le_big_sync_lost(struct net_buf *buf)
LOG_DBG("BIG[%u] %p sync lost", big->handle, big);

big_disconnect(big, evt->reason);
cleanup_big(big);
}

static int hci_le_big_create_sync(const struct bt_le_per_adv_sync *sync, struct bt_iso_big *big,
Expand Down Expand Up @@ -3578,7 +3585,6 @@ void bt_iso_reset(void)
struct bt_iso_big *big = &bigs[i];

big_disconnect(big, BT_HCI_ERR_UNSPECIFIED);
cleanup_big(big);
}
#endif /* CONFIG_BT_ISO_BROADCAST */
}
6 changes: 5 additions & 1 deletion tests/bluetooth/host/conn/mocks/kernel.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdbool.h>

#include <zephyr/fff.h>
#include <zephyr/kernel.h>

#include "kernel.h"
Expand All @@ -22,6 +25,7 @@ DEFINE_FAKE_VALUE_FUNC(int, k_work_submit, struct k_work *);
DEFINE_FAKE_VALUE_FUNC(int, k_work_submit_to_queue, struct k_work_q *, struct k_work *);
DEFINE_FAKE_VALUE_FUNC(int, k_work_reschedule, struct k_work_delayable *, k_timeout_t);
DEFINE_FAKE_VALUE_FUNC(int, k_work_schedule, struct k_work_delayable *, k_timeout_t);
DEFINE_FAKE_VALUE_FUNC(int, k_work_busy_get, const struct k_work *);
DEFINE_FAKE_VOID_FUNC(k_queue_init, struct k_queue *);
DEFINE_FAKE_VOID_FUNC(k_queue_append, struct k_queue *, void *);
DEFINE_FAKE_VALUE_FUNC(int, k_queue_is_empty, struct k_queue *);
Expand Down
4 changes: 3 additions & 1 deletion tests/bluetooth/host/conn/mocks/kernel.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>

#include <zephyr/kernel.h>
#include <zephyr/fff.h>
Expand Down Expand Up @@ -41,6 +42,7 @@ DECLARE_FAKE_VOID_FUNC(k_sem_give, struct k_sem *);
DECLARE_FAKE_VALUE_FUNC(k_tid_t, k_sched_current_thread_query);
DECLARE_FAKE_VOID_FUNC(k_work_init, struct k_work *, k_work_handler_t);
DECLARE_FAKE_VOID_FUNC(k_work_init_delayable, struct k_work_delayable *, k_work_handler_t);
DECLARE_FAKE_VALUE_FUNC(int, k_work_busy_get, const struct k_work *);
DECLARE_FAKE_VALUE_FUNC(int, k_work_cancel_delayable, struct k_work_delayable *);
DECLARE_FAKE_VALUE_FUNC(bool, k_work_flush, struct k_work *, struct k_work_sync *);
DECLARE_FAKE_VALUE_FUNC(int, k_work_submit, struct k_work *);
Expand Down