diff --git a/samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c b/samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c index 190bef26eab..35d8cd0d393 100644 --- a/samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c +++ b/samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c @@ -1,7 +1,7 @@ /** @file * @brief Bluetooth Common Audio Profile (CAP) Acceptor broadcast. * - * Copyright (c) 2024 Nordic Semiconductor ASA + * Copyright (c) 2024-2025 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ @@ -459,7 +459,8 @@ static int bis_sync_req_cb(struct bt_conn *conn, LOG_INF("BIS sync request received for %p: 0x%08x", recv_state, bis_sync_req[0]); - if (new_bis_sync_req != BT_BAP_BIS_SYNC_NO_PREF && POPCOUNT(new_bis_sync_req) > 1U) { + if (new_bis_sync_req != BT_BAP_BIS_SYNC_NO_PREF && + count_bits(&new_bis_sync_req, sizeof(new_bis_sync_req)) > 1U) { LOG_WRN("Rejecting BIS sync request for 0x%08X as we do not support that", new_bis_sync_req); diff --git a/subsys/bluetooth/audio/audio.c b/subsys/bluetooth/audio/audio.c index 75109d4fd96..9720d7dbc63 100644 --- a/subsys/bluetooth/audio/audio.c +++ b/subsys/bluetooth/audio/audio.c @@ -2,6 +2,7 @@ /* * Copyright (c) 2022 Codecoup + * Copyright (c) 2025 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ @@ -23,6 +24,7 @@ #include #include #include +#include #include #include "audio_internal.h" @@ -146,18 +148,7 @@ uint8_t bt_audio_get_chan_count(enum bt_audio_location chan_allocation) return 1; } -#ifdef POPCOUNT - return POPCOUNT(chan_allocation); -#else - uint8_t cnt = 0U; - - while (chan_allocation != 0U) { - cnt += chan_allocation & 1U; - chan_allocation >>= 1U; - } - - return cnt; -#endif + return count_bits(&chan_allocation, sizeof(chan_allocation)); } static bool valid_ltv_cb(struct bt_data *data, void *user_data) diff --git a/subsys/bluetooth/audio/bap_broadcast_sink.c b/subsys/bluetooth/audio/bap_broadcast_sink.c index fa1f0b8ee56..62142379bea 100644 --- a/subsys/bluetooth/audio/bap_broadcast_sink.c +++ b/subsys/bluetooth/audio/bap_broadcast_sink.c @@ -1071,22 +1071,6 @@ int bt_bap_broadcast_sink_create(struct bt_le_per_adv_sync *pa_sync, uint32_t br return 0; } -static uint8_t bit_count(uint32_t bitfield) -{ -#ifdef POPCOUNT - return POPCOUNT(bitfield); -#else - uint8_t cnt = 0U; - - while (bitfield != 0U) { - cnt += bitfield & 1U; - bitfield >>= 1U; - } - - return cnt; -#endif -} - struct sync_base_info_data { struct bt_audio_codec_cfg codec_cfgs[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT]; struct bt_audio_codec_cfg *subgroup_codec_cfg; @@ -1271,7 +1255,7 @@ int bt_bap_broadcast_sink_sync(struct bt_bap_broadcast_sink *sink, uint32_t inde } /* Validate that number of bits set is within supported range */ - bis_count = bit_count(indexes_bitfield); + bis_count = count_bits(&indexes_bitfield, sizeof(indexes_bitfield)); if (bis_count > CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT) { LOG_DBG("Cannot sync to more than %d streams (%u was requested)", CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT, bis_count); diff --git a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c index be580e02ea6..1336712cb0e 100644 --- a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c @@ -801,7 +801,7 @@ static void test_broadcast_sync(const uint8_t broadcast_code[BT_ISO_BROADCAST_CO return; } - stream_sync_cnt = POPCOUNT(bis_index_bitfield); + stream_sync_cnt = count_bits(&bis_index_bitfield, sizeof(bis_index_bitfield)); } static void test_broadcast_sync_inval(void)