Commit ee335399 authored by Emil Gydesen's avatar Emil Gydesen Committed by Anas Nashif
Browse files

Bluetooth: Audio: Use generic count_bits to count bits

Instead of re-implementing or assuming that POPCOUNT is available
we now use the generic function from Zephyr.

Signed-off-by: default avatarEmil Gydesen <emil.gydesen@nordicsemi.no>
parent d05de070
Loading
Loading
Loading
Loading
+3 −2
Changes for samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
/** @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);

+3 −12
Changes for subsys/bluetooth/audio/audio.c: 3 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -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 <zephyr/bluetooth/hci_types.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/check.h>
#include <zephyr/sys/util.h>
#include <zephyr/toolchain.h>

#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)
+1 −17
Changes for subsys/bluetooth/audio/bap_broadcast_sink.c: 1 added line, 17 removed lines.
Original line number Diff line number Diff line
@@ -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);
+1 −1
Changes for tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -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)