Skip to content

Commit 6e60451

Browse files
Thalleycarlescufi
authored andcommitted
Bluetooth: Audio: Change array of unicast streams to array of pointers
In bt_audio_unicast_group_create, bt_audio_unicast_group_add_streams and bt_audio_unicast_group_remove_streams to use an array of pointers, instead of an array of streams. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 302e643 commit 6e60451

File tree

5 files changed

+69
-46
lines changed

5 files changed

+69
-46
lines changed

include/zephyr/bluetooth/audio/audio.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,14 +1551,14 @@ int bt_audio_stream_send(struct bt_audio_stream *stream, struct net_buf *buf);
15511551
* unicast client. Streams in a unicast group shall share the same interval,
15521552
* framing and latency (see @ref bt_codec_qos).
15531553
*
1554-
* @param[in] streams Array of stream objects being used for the
1555-
* group.
1554+
* @param[in] streams Array of stream object pointers being used for
1555+
* the group.
15561556
* @param[in] num_stream Number of streams in @p streams.
15571557
* @param[out] unicast_group Pointer to the unicast group created
15581558
*
15591559
* @return Zero on success or (negative) error code otherwise.
15601560
*/
1561-
int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
1561+
int bt_audio_unicast_group_create(struct bt_audio_stream *streams[],
15621562
size_t num_stream,
15631563
struct bt_audio_unicast_group **unicast_group);
15641564

@@ -1573,13 +1573,14 @@ int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
15731573
* (see bt_audio_stream_ops.stopped()).
15741574
*
15751575
* @param unicast_group Pointer to the unicast group
1576-
* @param streams Array of stream objects being added to the group.
1576+
* @param streams Array of stream object pointers being added to the
1577+
* group.
15771578
* @param num_stream Number of streams in @p streams.
15781579
*
15791580
* @return 0 in case of success or negative value in case of error.
15801581
*/
15811582
int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_group,
1582-
struct bt_audio_stream *streams,
1583+
struct bt_audio_stream *streams[],
15831584
size_t num_stream);
15841585

15851586
/** @brief Remove streams from a unicast group as a unicast client
@@ -1592,13 +1593,14 @@ int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_gr
15921593
* (see bt_audio_stream_ops.stopped()).
15931594
*
15941595
* @param unicast_group Pointer to the unicast group
1595-
* @param streams Array of stream objects removed from the group.
1596+
* @param streams Array of stream object pointers removed from the
1597+
* group.
15961598
* @param num_stream Number of streams in @p streams.
15971599
*
15981600
* @return 0 in case of success or negative value in case of error.
15991601
*/
16001602
int bt_audio_unicast_group_remove_streams(struct bt_audio_unicast_group *unicast_group,
1601-
struct bt_audio_stream *streams,
1603+
struct bt_audio_stream *streams[],
16021604
size_t num_stream);
16031605

16041606
/** @brief Delete audio unicast group.

samples/bluetooth/unicast_audio_client/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ static int create_group(struct bt_audio_stream *stream)
642642
{
643643
int err;
644644

645-
err = bt_audio_unicast_group_create(stream, 1, &unicast_group);
645+
err = bt_audio_unicast_group_create(&stream, 1, &unicast_group);
646646
if (err != 0) {
647647
printk("Could not create unicast group (err %d)\n", err);
648648
return err;

subsys/bluetooth/audio/stream.c

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ int bt_audio_stream_connect(struct bt_audio_stream *stream)
10031003
}
10041004
}
10051005

1006-
int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
1006+
int bt_audio_unicast_group_create(struct bt_audio_stream *streams[],
10071007
size_t num_stream,
10081008
struct bt_audio_unicast_group **out_unicast_group)
10091009
{
@@ -1029,6 +1029,18 @@ int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
10291029
return -EINVAL;
10301030
}
10311031

1032+
for (size_t i = 0; i < num_stream; i++) {
1033+
CHECKIF(streams[i] == NULL) {
1034+
return -EINVAL;
1035+
}
1036+
1037+
if (streams[i]->group != NULL) {
1038+
BT_DBG("Channel[%u] (%p) already part of group %p",
1039+
i, streams[i], streams[i]->group);
1040+
return -EALREADY;
1041+
}
1042+
}
1043+
10321044
unicast_group = NULL;
10331045
for (index = 0; index < ARRAY_SIZE(unicast_groups); index++) {
10341046
/* Find free entry */
@@ -1047,23 +1059,7 @@ int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
10471059
sys_slist_t *group_streams = &unicast_group->streams;
10481060
struct bt_audio_stream *stream;
10491061

1050-
stream = &streams[i];
1051-
1052-
if (stream->group != NULL) {
1053-
BT_DBG("Channel[%u] (%p) already part of group %p",
1054-
i, stream, stream->group);
1055-
1056-
/* Cleanup */
1057-
for (size_t j = 0; j < i; j++) {
1058-
stream = &streams[j];
1059-
1060-
(void)sys_slist_find_and_remove(group_streams,
1061-
&stream->node);
1062-
stream->unicast_group = NULL;
1063-
}
1064-
return -EALREADY;
1065-
}
1066-
1062+
stream = streams[i];
10671063
stream->unicast_group = unicast_group;
10681064
sys_slist_append(group_streams, &stream->node);
10691065
}
@@ -1074,7 +1070,7 @@ int bt_audio_unicast_group_create(struct bt_audio_stream *streams,
10741070
}
10751071

10761072
int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_group,
1077-
struct bt_audio_stream *streams,
1073+
struct bt_audio_stream *streams[],
10781074
size_t num_stream)
10791075
{
10801076
struct bt_audio_stream *tmp_stream;
@@ -1096,6 +1092,12 @@ int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_gr
10961092
return -EINVAL;
10971093
}
10981094

1095+
for (size_t i = 0; i < num_stream; i++) {
1096+
CHECKIF(streams[i] == NULL) {
1097+
return -EINVAL;
1098+
}
1099+
}
1100+
10991101
total_stream_cnt = num_stream;
11001102
SYS_SLIST_FOR_EACH_CONTAINER(&unicast_group->streams, tmp_stream, node) {
11011103
total_stream_cnt++;
@@ -1110,9 +1112,9 @@ int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_gr
11101112

11111113
/* Validate input */
11121114
for (size_t i = 0; i < num_stream; i++) {
1113-
if (streams[i].group != NULL) {
1115+
if (streams[i]->group != NULL) {
11141116
BT_DBG("stream[%zu] is already part of group %p",
1115-
i, streams[i].group);
1117+
i, streams[i]->group);
11161118
return -EINVAL;
11171119
}
11181120
}
@@ -1128,7 +1130,7 @@ int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_gr
11281130

11291131
for (size_t i = 0; i < num_stream; i++) {
11301132
sys_slist_t *group_streams = &unicast_group->streams;
1131-
struct bt_audio_stream *stream = &streams[i];
1133+
struct bt_audio_stream *stream = streams[i];
11321134

11331135
stream->unicast_group = unicast_group;
11341136
sys_slist_append(group_streams, &stream->node);
@@ -1138,7 +1140,7 @@ int bt_audio_unicast_group_add_streams(struct bt_audio_unicast_group *unicast_gr
11381140
}
11391141

11401142
int bt_audio_unicast_group_remove_streams(struct bt_audio_unicast_group *unicast_group,
1141-
struct bt_audio_stream *streams,
1143+
struct bt_audio_stream *streams[],
11421144
size_t num_stream)
11431145
{
11441146
struct bt_iso_cig *cig;
@@ -1160,9 +1162,13 @@ int bt_audio_unicast_group_remove_streams(struct bt_audio_unicast_group *unicast
11601162

11611163
/* Validate input */
11621164
for (size_t i = 0; i < num_stream; i++) {
1163-
if (streams[i].group != unicast_group) {
1165+
CHECKIF(streams[i] == NULL) {
1166+
return -EINVAL;
1167+
}
1168+
1169+
if (streams[i]->group != unicast_group) {
11641170
BT_DBG("stream[%zu] group %p is not group %p",
1165-
i, streams[i].group, unicast_group);
1171+
i, streams[i]->group, unicast_group);
11661172
return -EINVAL;
11671173
}
11681174
}
@@ -1178,11 +1184,11 @@ int bt_audio_unicast_group_remove_streams(struct bt_audio_unicast_group *unicast
11781184

11791185
for (size_t i = 0; i < num_stream; i++) {
11801186
sys_slist_t *group_streams = &unicast_group->streams;
1181-
struct bt_audio_stream *stream = &streams[i];
1187+
struct bt_audio_stream *stream = streams[i];
11821188

11831189
stream->unicast_group = NULL;
11841190
(void)sys_slist_find_and_remove(group_streams,
1185-
&streams->node);
1191+
&stream->node);
11861192
}
11871193

11881194
return 0;

subsys/bluetooth/shell/audio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static int lc3_reconfig(struct bt_audio_stream *stream,
313313
int err;
314314

315315
if (default_unicast_group == NULL) {
316-
err = bt_audio_unicast_group_create(default_stream, 1,
316+
err = bt_audio_unicast_group_create(&default_stream, 1,
317317
&default_unicast_group);
318318
if (err != 0) {
319319
shell_error(ctx_shell,
@@ -733,7 +733,7 @@ static int cmd_qos(const struct shell *sh, size_t argc, char *argv[])
733733
}
734734

735735
if (default_unicast_group == NULL) {
736-
err = bt_audio_unicast_group_create(default_stream, 1, &default_unicast_group);
736+
err = bt_audio_unicast_group_create(&default_stream, 1, &default_unicast_group);
737737
if (err != 0) {
738738
shell_error(sh, "Unable to create default unicast group: %d", err);
739739
return -ENOEXEC;

tests/bluetooth/bsim_bt/bsim_test_audio/src/unicast_client_test.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ static size_t configure_streams(void)
266266
size_t stream_cnt;
267267

268268
for (stream_cnt = 0; stream_cnt < ARRAY_SIZE(g_sinks); stream_cnt++) {
269+
struct bt_audio_stream *stream = &g_streams[stream_cnt];
269270
int err;
270271

271272
if (g_sinks[stream_cnt] == NULL) {
272273
break;
273274
}
274275

275-
err = configure_stream(&g_streams[stream_cnt],
276-
g_sinks[stream_cnt]);
276+
err = configure_stream(stream, g_sinks[stream_cnt]);
277277
if (err != 0) {
278278
FAIL("Unable to configure stream[%zu]: %d",
279279
stream_cnt, err);
@@ -310,28 +310,36 @@ static size_t release_streams(size_t stream_cnt)
310310
static void create_unicast_group(struct bt_audio_unicast_group **unicast_group,
311311
size_t stream_cnt)
312312
{
313+
struct bt_audio_stream *streams[ARRAY_SIZE(g_streams)];
313314
int err;
314315

315-
err = bt_audio_unicast_group_create(g_streams, 1, unicast_group);
316+
for (size_t i = 0U; i < stream_cnt; i++) {
317+
streams[i] = &g_streams[i];
318+
}
319+
320+
printk("Creating unicast group\n");
321+
err = bt_audio_unicast_group_create(streams, 1, unicast_group);
316322
if (err != 0) {
317323
FAIL("Unable to create unicast group: %d", err);
318324
return;
319325
}
320326

321327
/* Test removing streams from group before adding them */
322328
if (stream_cnt > 1) {
329+
const size_t remaining_streams = stream_cnt - 1;
330+
323331
err = bt_audio_unicast_group_remove_streams(*unicast_group,
324-
g_streams + 1,
325-
stream_cnt - 1);
332+
&streams[1],
333+
remaining_streams);
326334
if (err == 0) {
327335
FAIL("Able to remove stream not in group");
328336
return;
329337
}
330338

331339
/* Test adding streams to group after creation */
332340
err = bt_audio_unicast_group_add_streams(*unicast_group,
333-
g_streams + 1,
334-
stream_cnt - 1);
341+
&streams[1],
342+
remaining_streams);
335343
if (err != 0) {
336344
FAIL("Unable to add streams to unicast group: %d", err);
337345
return;
@@ -342,12 +350,19 @@ static void create_unicast_group(struct bt_audio_unicast_group **unicast_group,
342350
static void delete_unicast_group(struct bt_audio_unicast_group *unicast_group,
343351
size_t stream_cnt)
344352
{
353+
struct bt_audio_stream *streams[ARRAY_SIZE(g_streams)];
345354
int err;
346355

356+
for (size_t i = 0U; i < stream_cnt; i++) {
357+
streams[i] = &g_streams[i];
358+
}
359+
347360
if (stream_cnt > 1) {
361+
const size_t remove_streams_cnt = stream_cnt - 1;
362+
348363
err = bt_audio_unicast_group_remove_streams(unicast_group,
349-
g_streams + 1,
350-
stream_cnt - 1);
364+
&streams[1],
365+
remove_streams_cnt);
351366
if (err != 0) {
352367
FAIL("Unable to remove streams from unicast group: %d",
353368
err);

0 commit comments

Comments
 (0)