Skip to content

Commit 0b06117

Browse files
author
Andries Kruithof
committed
Bluetooth: Audio: add shell command for distribute broadcast code
Adds a shell command for the distribute broadcast code CAP procedure Signed-off-by: Andries Kruithof <[email protected]>
1 parent 5be4aed commit 0b06117

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

doc/connectivity/bluetooth/shell/audio/cap.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ the optionally included CSIS instance by calling (:code:`cap_commander discover`
271271
<type: public/random> <adv_sid> <broadcast_id>
272272
[<pa_interval>] [<sync_bis>] [<metadata>]
273273
broadcast_reception_stop : Stop broadcast reception <src_id [...]>
274+
distribute_broadcast_code : Distribute broadcast code <src_id [...]> <broadcast_code>
274275
275276
276277
Before being able to perform any stream operation, the device must also perform the
@@ -460,3 +461,21 @@ Starting and stopping broadcast reception
460461
uart:~$ cap_commander broadcast_reception_stop 0
461462
Stopping broadcast reception on 1 connection(s)
462463
Broadcast reception stop completed
464+
465+
Distributing the broadcast code
466+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
467+
468+
.. code-block:: console
469+
470+
uart:~$ bt connect <device A>
471+
Connected: <device A>
472+
uart:~$ bap_init
473+
uart:~$ cap_commander discover
474+
discovery completed with CSIS
475+
uart:~$ bap_broadcast_assistant discover
476+
BASS discover done with 1 recv states
477+
uart:~$ cap_commander broadcast_reception_start <device B> 0 4
478+
Starting broadcast reception on 1 connection(s)
479+
Broadcast reception start completed
480+
uart:~$ cap_commander distribute_broadcast_code 0 "BroadcastCode"
481+
Distribute broadcast code completed

subsys/bluetooth/audio/shell/cap_commander.c

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ static void cap_broadcast_reception_stop_cb(struct bt_conn *conn, int err)
114114

115115
shell_print(ctx_shell, "Broadcast reception stop completed");
116116
}
117+
118+
static void cap_distribute_broadcast_code_cb(struct bt_conn *conn, int err)
119+
{
120+
if (err != 0) {
121+
shell_error(ctx_shell, "Distribute broadcast code failed (%d) for conn %p", err,
122+
(void *)conn);
123+
return;
124+
}
125+
126+
shell_print(ctx_shell, "Distribute broadcast code completed");
127+
}
117128
#endif
118129

119130
static struct bt_cap_commander_cb cbs = {
@@ -134,6 +145,7 @@ static struct bt_cap_commander_cb cbs = {
134145
#if defined(CONFIG_BT_BAP_BROADCAST_ASSISTANT)
135146
.broadcast_reception_start = cap_broadcast_reception_start_cb,
136147
.broadcast_reception_stop = cap_broadcast_reception_stop_cb,
148+
.distribute_broadcast_code = cap_distribute_broadcast_code_cb,
137149
#endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */
138150
};
139151

@@ -751,6 +763,84 @@ static int cmd_cap_commander_broadcast_reception_stop(const struct shell *sh, si
751763
return 0;
752764
}
753765

766+
static int cmd_cap_commander_distribute_broadcast_code(const struct shell *sh, size_t argc,
767+
char *argv[])
768+
{
769+
struct bt_cap_commander_distribute_broadcast_code_member_param
770+
member_params[CONFIG_BT_MAX_CONN] = {0};
771+
const size_t cap_argc = argc - 1; /* First argument is the command itself */
772+
struct bt_cap_commander_distribute_broadcast_code_param param = {
773+
.type = BT_CAP_SET_TYPE_AD_HOC,
774+
.param = member_params,
775+
};
776+
777+
struct bt_conn *connected_conns[CONFIG_BT_MAX_CONN] = {0};
778+
size_t conn_cnt = 0U;
779+
int err = 0;
780+
781+
if (default_conn == NULL) {
782+
shell_error(sh, "Not connected");
783+
return -ENOEXEC;
784+
}
785+
786+
/* TODO Add support for coordinated sets */
787+
788+
/* Populate the array of connected connections */
789+
bt_conn_foreach(BT_CONN_TYPE_LE, populate_connected_conns, (void *)connected_conns);
790+
for (size_t i = 0; i < ARRAY_SIZE(connected_conns); i++) {
791+
struct bt_conn *conn = connected_conns[i];
792+
793+
if (conn == NULL) {
794+
break;
795+
}
796+
conn_cnt++;
797+
}
798+
799+
/* The number of cap_args needs to be the number of connections + 1 since the last argument
800+
* is the broadcast code
801+
*/
802+
if (cap_argc != conn_cnt + 1) {
803+
shell_error(sh, "Cannot use %zu arguments for %zu connections", argc, conn_cnt);
804+
return -ENOEXEC;
805+
}
806+
807+
/* the last argument is the broadcast code */
808+
if (strlen(argv[cap_argc]) > BT_ISO_BROADCAST_CODE_SIZE) {
809+
shell_error(sh, "Broadcast code can be maximum %d characters",
810+
BT_ISO_BROADCAST_CODE_SIZE);
811+
return -ENOEXEC;
812+
}
813+
814+
for (size_t i = 0; i < conn_cnt; i++) {
815+
const char *arg = argv[i + 1];
816+
unsigned long src_id;
817+
818+
src_id = shell_strtoul(arg, 0, &err);
819+
if (err != 0) {
820+
shell_error(sh, "Could not parce src_id: %d", err);
821+
return -ENOEXEC;
822+
}
823+
if (src_id > UINT8_MAX) {
824+
shell_error(sh, "Invalid src_id: %lu", src_id);
825+
return -ENOEXEC;
826+
}
827+
828+
member_params[i].src_id = src_id;
829+
member_params[i].member.member = connected_conns[i];
830+
param.count++;
831+
}
832+
833+
memcpy(param.broadcast_code, argv[cap_argc], strlen(argv[cap_argc]));
834+
shell_print(sh, "Distributing broadcast code on %zu connection(s)", param.count);
835+
err = bt_cap_commander_distribute_broadcast_code(&param);
836+
if (err != 0) {
837+
shell_print(sh, "Failed to initiate distribute broadcast code: %d", err);
838+
return -ENOEXEC;
839+
}
840+
841+
return 0;
842+
}
843+
754844
#endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */
755845

756846
static int cmd_cap_commander(const struct shell *sh, size_t argc, char **argv)
@@ -803,7 +893,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
803893
"Stop broadcast reception "
804894
"<src_id [...]>",
805895
cmd_cap_commander_broadcast_reception_stop, 2, 0),
806-
896+
SHELL_CMD_ARG(distribute_broadcast_code, NULL,
897+
"Distribute broadcast code <src_id [...]> <broadcast_code>",
898+
cmd_cap_commander_distribute_broadcast_code, 2, CONFIG_BT_MAX_CONN - 1),
807899
#endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */
808900
SHELL_SUBCMD_SET_END);
809901

0 commit comments

Comments
 (0)