Skip to content

Commit 0347d92

Browse files
Thalleycfriedt
authored andcommitted
Bluetooth: BAP: SD: Add missing error checks for mutex locks
Add missing error checks for mutex locks after the mutexes were changed to not use K_FOREVER which should not be done in the BT RX thread. A larger overhaul of how mutexes are using within the scan delegator should be considered, as there are a lot of locks and unlocks when handling the callbacks. Signed-off-by: Emil Gydesen <[email protected]>
1 parent bd774ff commit 0347d92

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

subsys/bluetooth/audio/bap_scan_delegator.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,12 @@ static int scan_delegator_add_src(struct bt_conn *conn,
778778

779779
err = pa_sync_request(conn, state, pa_sync, pa_interval);
780780
if (err != 0) {
781-
k_mutex_lock(&internal_state->mutex, K_FOREVER);
781+
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
782+
if (err != 0) {
783+
LOG_DBG("Failed to lock mutex: %d", err);
784+
785+
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
786+
}
782787

783788
(void)memset(state, 0, sizeof(*state));
784789
internal_state->active = false;
@@ -792,7 +797,12 @@ static int scan_delegator_add_src(struct bt_conn *conn,
792797
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
793798
}
794799

795-
k_mutex_lock(&internal_state->mutex, K_FOREVER);
800+
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
801+
if (err != 0) {
802+
LOG_DBG("Failed to lock mutex: %d", err);
803+
804+
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
805+
}
796806
}
797807

798808
LOG_DBG("Index %u: New source added: ID 0x%02x",
@@ -1013,7 +1023,12 @@ static int scan_delegator_mod_src(struct bt_conn *conn,
10131023

10141024
err = pa_sync_request(conn, state, pa_sync, pa_interval);
10151025
if (err != 0) {
1016-
k_mutex_lock(&internal_state->mutex, K_FOREVER);
1026+
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
1027+
if (err != 0) {
1028+
LOG_DBG("Failed to lock mutex: %d", err);
1029+
1030+
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
1031+
}
10171032

10181033
/* Restore backup */
10191034
(void)memcpy(state, &backup_state, sizeof(backup_state));
@@ -1032,7 +1047,13 @@ static int scan_delegator_mod_src(struct bt_conn *conn,
10321047
*/
10331048
state_changed = true;
10341049
}
1035-
k_mutex_lock(&internal_state->mutex, K_FOREVER);
1050+
1051+
err = k_mutex_lock(&internal_state->mutex, SCAN_DELEGATOR_BUF_SEM_TIMEOUT);
1052+
if (err != 0) {
1053+
LOG_DBG("Failed to lock mutex: %d", err);
1054+
1055+
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
1056+
}
10361057
} else if (pa_sync == BT_BAP_BASS_PA_REQ_NO_SYNC &&
10371058
(state->pa_sync_state == BT_BAP_PA_STATE_INFO_REQ ||
10381059
state->pa_sync_state == BT_BAP_PA_STATE_SYNCED)) {

0 commit comments

Comments
 (0)