@@ -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
119130static 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
756846static 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