Skip to content

Commit 1e5be3f

Browse files
Thalleycarlescufi
authored andcommitted
Bluetooth: Audio: Fix issue with unicast client src/snk ASE count
If either the sink or source ASE count was zero, calls to ARRAY_SIZE(srcs) or ARRAY_SIZE(sinks) would cause a build warning. The arrays should actually not be there at all if the respective ASE count was 0, as that is just a waste of memory. The arrays, and all uses of them, have been properly guarded. This also adds a build assert to ensure that at least one of them is non-zero, and that we also test building either of them with the value 0. Signed-off-by: Emil Gydesen <[email protected]>
1 parent b9a6e04 commit 1e5be3f

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

subsys/bluetooth/audio/unicast_client.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
#include <zephyr/logging/log.h>
3131

32+
BUILD_ASSERT(CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 ||
33+
CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0,
34+
"CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT or "
35+
"CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT shall be non-zero");
36+
3237
LOG_MODULE_REGISTER(bt_unicast_client, CONFIG_BT_AUDIO_UNICAST_CLIENT_LOG_LEVEL);
3338

3439
#define PAC_DIR_UNUSED(dir) ((dir) != BT_AUDIO_DIR_SINK && (dir) != BT_AUDIO_DIR_SOURCE)
@@ -58,10 +63,14 @@ static const struct bt_uuid *ase_src_uuid = BT_UUID_ASCS_ASE_SRC;
5863
static const struct bt_uuid *cp_uuid = BT_UUID_ASCS_ASE_CP;
5964

6065

66+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
6167
static struct bt_unicast_client_ep snks[CONFIG_BT_MAX_CONN]
6268
[CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT];
69+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
70+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
6371
static struct bt_unicast_client_ep srcs[CONFIG_BT_MAX_CONN]
6472
[CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT];
73+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
6574

6675
static struct bt_gatt_subscribe_params cp_subscribe[CONFIG_BT_MAX_CONN];
6776
static struct bt_gatt_subscribe_params snk_loc_subscribe[CONFIG_BT_MAX_CONN];
@@ -237,17 +246,21 @@ static struct bt_iso_chan_ops unicast_client_iso_ops = {
237246

238247
bool bt_audio_ep_is_unicast_client(const struct bt_audio_ep *ep)
239248
{
249+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
240250
for (size_t i = 0U; i < ARRAY_SIZE(snks); i++) {
241251
if (PART_OF_ARRAY(snks[i], ep)) {
242252
return true;
243253
}
244254
}
255+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
245256

257+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
246258
for (size_t i = 0U; i < ARRAY_SIZE(srcs); i++) {
247259
if (PART_OF_ARRAY(srcs[i], ep)) {
248260
return true;
249261
}
250262
}
263+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
251264

252265
return false;
253266
}
@@ -270,28 +283,31 @@ static void unicast_client_ep_init(struct bt_audio_ep *ep, uint16_t handle,
270283
static struct bt_audio_ep *unicast_client_ep_find(struct bt_conn *conn,
271284
uint16_t handle)
272285
{
273-
size_t i;
274286
uint8_t index;
275287

276288
index = bt_conn_index(conn);
277289

278-
for (i = 0; i < ARRAY_SIZE(snks[index]); i++) {
290+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
291+
for (size_t i = 0U; i < ARRAY_SIZE(snks[index]); i++) {
279292
struct bt_unicast_client_ep *client_ep = &snks[index][i];
280293

281294
if ((handle && client_ep->handle == handle) ||
282295
(!handle && client_ep->handle)) {
283296
return &client_ep->ep;
284297
}
285298
}
299+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
286300

287-
for (i = 0; i < ARRAY_SIZE(srcs[index]); i++) {
301+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
302+
for (size_t i = 0U; i < ARRAY_SIZE(srcs[index]); i++) {
288303
struct bt_unicast_client_ep *client_ep = &srcs[index][i];
289304

290305
if ((handle && client_ep->handle == handle) ||
291306
(!handle && client_ep->handle)) {
292307
return &client_ep->ep;
293308
}
294309
}
310+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
295311

296312
return NULL;
297313
}
@@ -323,14 +339,18 @@ static struct bt_audio_ep *unicast_client_ep_new(struct bt_conn *conn,
323339
index = bt_conn_index(conn);
324340

325341
switch (dir) {
342+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
326343
case BT_AUDIO_DIR_SINK:
327344
cache = snks[index];
328345
size = ARRAY_SIZE(snks[index]);
329346
break;
347+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
348+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
330349
case BT_AUDIO_DIR_SOURCE:
331350
cache = srcs[index];
332351
size = ARRAY_SIZE(srcs[index]);
333352
break;
353+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
334354
default:
335355
return NULL;
336356
}
@@ -1153,7 +1173,6 @@ static int unicast_client_ep_subscribe(struct bt_conn *conn,
11531173

11541174
static void unicast_client_ep_set_cp(struct bt_conn *conn, uint16_t handle)
11551175
{
1156-
size_t i;
11571176
uint8_t index;
11581177

11591178
LOG_DBG("conn %p 0x%04x", conn, handle);
@@ -1173,21 +1192,25 @@ static void unicast_client_ep_set_cp(struct bt_conn *conn, uint16_t handle)
11731192
bt_gatt_subscribe(conn, &cp_subscribe[index]);
11741193
}
11751194

1176-
for (i = 0; i < ARRAY_SIZE(snks[index]); i++) {
1195+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
1196+
for (size_t i = 0U; i < ARRAY_SIZE(snks[index]); i++) {
11771197
struct bt_unicast_client_ep *client_ep = &snks[index][i];
11781198

11791199
if (client_ep->handle) {
11801200
client_ep->cp_handle = handle;
11811201
}
11821202
}
1203+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
11831204

1184-
for (i = 0; i < ARRAY_SIZE(srcs[index]); i++) {
1205+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
1206+
for (size_t i = 0U; i < ARRAY_SIZE(srcs[index]); i++) {
11851207
struct bt_unicast_client_ep *client_ep = &srcs[index][i];
11861208

11871209
if (client_ep->handle) {
11881210
client_ep->cp_handle = handle;
11891211
}
11901212
}
1213+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
11911214
}
11921215

11931216
NET_BUF_SIMPLE_DEFINE_STATIC(ep_buf, CONFIG_BT_L2CAP_TX_MTU);
@@ -1495,24 +1518,27 @@ static void unicast_client_reset(struct bt_audio_ep *ep)
14951518

14961519
static void unicast_client_ep_reset(struct bt_conn *conn)
14971520
{
1498-
size_t i;
14991521
uint8_t index;
15001522

15011523
LOG_DBG("conn %p", conn);
15021524

15031525
index = bt_conn_index(conn);
15041526

1505-
for (i = 0; i < ARRAY_SIZE(snks[index]); i++) {
1527+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0
1528+
for (size_t i = 0U; i < ARRAY_SIZE(snks[index]); i++) {
15061529
struct bt_audio_ep *ep = &snks[index][i].ep;
15071530

15081531
unicast_client_reset(ep);
15091532
}
1533+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
15101534

1511-
for (i = 0; i < ARRAY_SIZE(srcs[index]); i++) {
1535+
#if CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0
1536+
for (size_t i = 0U; i < ARRAY_SIZE(srcs[index]); i++) {
15121537
struct bt_audio_ep *ep = &srcs[index][i].ep;
15131538

15141539
unicast_client_reset(ep);
15151540
}
1541+
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
15161542
}
15171543

15181544
int bt_unicast_client_config(struct bt_audio_stream *stream,

tests/bluetooth/shell/testcase.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,30 @@ tests:
201201
extra_configs:
202202
- CONFIG_BT_AUDIO_UNICAST_SERVER=n
203203
- CONFIG_BT_HAS=n
204-
bluetooth.audio_shell.no_ase_snk:
204+
bluetooth.audio_shell.no_server_ase_snk:
205205
extra_args: CONF_FILE="audio.conf"
206206
build_only: true
207207
platform_allow: native_posix
208208
extra_configs:
209209
- CONFIG_BT_ASCS_ASE_SNK_COUNT=0
210-
bluetooth.audio_shell.no_ase_src:
210+
bluetooth.audio_shell.no_server_ase_src:
211211
extra_args: CONF_FILE="audio.conf"
212212
build_only: true
213213
platform_allow: native_posix
214214
extra_configs:
215215
- CONFIG_BT_ASCS_ASE_SRC_COUNT=0
216+
bluetooth.audio_shell.no_client_ase_snk:
217+
extra_args: CONF_FILE="audio.conf"
218+
build_only: true
219+
platform_allow: native_posix
220+
extra_configs:
221+
- CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT=0
222+
bluetooth.audio_shell.no_client_ase_src:
223+
extra_args: CONF_FILE="audio.conf"
224+
build_only: true
225+
platform_allow: native_posix
226+
extra_configs:
227+
- CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT=0
216228
bluetooth.audio_shell.no_broadcast_source:
217229
extra_args: CONF_FILE="audio.conf"
218230
build_only: true

0 commit comments

Comments
 (0)