Skip to content

Commit

Permalink
Bluetooth: Audio: Shell: Add preset copy functions
Browse files Browse the repository at this point in the history
Add common preset copy functions for unicast streams and
broadcast sources.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
  • Loading branch information
Thalley committed May 10, 2023
1 parent a5f6eda commit e4d1e4d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
64 changes: 64 additions & 0 deletions subsys/bluetooth/audio/shell/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/audio/cap.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/printk.h>

#include "shell/bt.h"

extern struct bt_csip_set_member_svc_inst *svc_inst;

ssize_t audio_ad_data_add(struct bt_data *data, const size_t data_size, const bool discoverable,
Expand Down Expand Up @@ -185,6 +188,67 @@ static inline void print_base(const struct shell *sh, const struct bt_bap_base *
shell_print(sh, "Possible indexes: %s", bis_indexes_str);
}
#endif /* BROADCAST_SNK_SUBGROUP_CNT > 0 */

static inline void copy_unicast_stream_preset(struct unicast_stream *stream,
const struct named_lc3_preset *named_preset)
{
memcpy(&stream->qos, &named_preset->preset.qos, sizeof(stream->qos));
memcpy(&stream->codec, &named_preset->preset.codec, sizeof(stream->codec));

#if CONFIG_BT_CODEC_MAX_DATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0
/* Need to update the `bt_data.data` pointer to the new value after copying the codec */
for (size_t i = 0U; i < ARRAY_SIZE(stream->codec.data); i++) {
const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i];
struct bt_codec_data *data = &stream->codec.data[i];

data->data.data = data->value;
data->data.data_len = preset_data->data.data_len;
memcpy(data->value, preset_data->data.data, preset_data->data.data_len);
}
#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */

#if CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0
for (size_t i = 0U; i < ARRAY_SIZE(stream->codec.meta); i++) {
const struct bt_codec_data *preset_data = &named_preset->preset.codec.meta[i];
struct bt_codec_data *data = &stream->codec.meta[i];

data->data.data = data->value;
data->data.data_len = preset_data->data.data_len;
memcpy(data->value, preset_data->data.data, preset_data->data.data_len);
}
#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */
}

static inline void copy_broadcast_source_preset(struct broadcast_source *source,
const struct named_lc3_preset *named_preset)
{
memcpy(&source->qos, &named_preset->preset.qos, sizeof(source->qos));
memcpy(&source->codec, &named_preset->preset.codec, sizeof(source->codec));

#if CONFIG_BT_CODEC_MAX_DATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0
/* Need to update the `bt_data.data` pointer to the new value after copying the codec */
for (size_t i = 0U; i < ARRAY_SIZE(source->codec.data); i++) {
const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i];
struct bt_codec_data *data = &source->codec.data[i];

data->data.data = data->value;
data->data.data_len = preset_data->data.data_len;
memcpy(data->value, preset_data->data.data, preset_data->data.data_len);
}
#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */

#if CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0
for (size_t i = 0U; i < ARRAY_SIZE(source->codec.meta); i++) {
const struct bt_codec_data *preset_data = &named_preset->preset.codec.meta[i];
struct bt_codec_data *data = &source->codec.meta[i];

data->data.data = data->value;
data->data.data_len = preset_data->data.data_len;
memcpy(data->value, preset_data->data.data, preset_data->data.data_len);
}
#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */
}

#endif /* CONFIG_BT_AUDIO */

#endif /* __AUDIO_H */
27 changes: 2 additions & 25 deletions subsys/bluetooth/audio/shell/bap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,29 +1040,7 @@ static int cmd_config(const struct shell *sh, size_t argc, char *argv[])
}

uni_stream = CONTAINER_OF(default_stream, struct unicast_stream, stream);
memcpy(&uni_stream->qos, &named_preset->preset.qos, sizeof(uni_stream->qos));
memcpy(&uni_stream->codec, &named_preset->preset.codec, sizeof(uni_stream->codec));
/* Need to update the `bt_data.data` pointer to the new value after copying the codec */
/* Need to copy from the data pointer, as the preset->value is empty, as they are defined as
* compound literals
*/
for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.data); i++) {
const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i];
struct bt_codec_data *data = &uni_stream->codec.data[i];

data->data.data = data->value;
data->data.data_len = preset_data->data.data_len;
memcpy(data->value, preset_data->data.data, preset_data->data.data_len);
}

for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.meta); i++) {
const struct bt_codec_data *preset_meta = &named_preset->preset.codec.meta[i];
struct bt_codec_data *meta = &uni_stream->codec.meta[i];

meta->data.data = meta->value;
meta->data.data_len = preset_meta->data.data_len;
memcpy(meta->value, preset_meta->data.data, preset_meta->data.data_len);
}
copy_unicast_stream_preset(uni_stream, named_preset);

/* If location has been modifed, we update the location in the codec configuration */
if (location != BT_AUDIO_LOCATION_PROHIBITED) {
Expand Down Expand Up @@ -1930,8 +1908,7 @@ static int cmd_create_broadcast(const struct shell *sh, size_t argc,
}
}

memcpy(&default_source.codec, &named_preset->preset.codec, sizeof(default_source.codec));
memcpy(&default_source.qos, &named_preset->preset.qos, sizeof(default_source.qos));
copy_broadcast_source_preset(&default_source, named_preset);

(void)memset(stream_params, 0, sizeof(stream_params));
for (size_t i = 0; i < ARRAY_SIZE(stream_params); i++) {
Expand Down
33 changes: 6 additions & 27 deletions subsys/bluetooth/audio/shell/cap_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,6 @@ static void populate_connected_conns(struct bt_conn *conn, void *data)
}
}

static void cap_copy_preset(struct unicast_stream *uni_stream,
const struct named_lc3_preset *name_preset)
{
memcpy(&uni_stream->qos, &name_preset->preset.qos, sizeof(uni_stream->qos));
memcpy(&uni_stream->codec, &name_preset->preset.codec,
sizeof(uni_stream->codec));

/* Need to update the `bt_data.data` pointer to the new value after copying the codec */
for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.data); i++) {
struct bt_codec_data *data = &uni_stream->codec.data[i];

data->data.data = data->value;
}

for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.meta); i++) {
struct bt_codec_data *data = &uni_stream->codec.meta[i];

data->data.data = data->value;
}
}

static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc,
char *argv[])
{
Expand Down Expand Up @@ -264,7 +243,7 @@ static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc,
stream_param[start_param.count].member.member = conn;
stream_param[start_param.count].stream = stream;
stream_param[start_param.count].ep = snk_ep;
cap_copy_preset(uni_stream, default_sink_preset);
copy_unicast_stream_preset(uni_stream, default_sink_preset);
stream_param[start_param.count].codec = &uni_stream->codec;
stream_param[start_param.count].qos = &uni_stream->qos;

Expand Down Expand Up @@ -298,7 +277,7 @@ static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc,
stream_param[start_param.count].member.member = conn;
stream_param[start_param.count].stream = stream;
stream_param[start_param.count].ep = src_ep;
cap_copy_preset(uni_stream, default_source_preset);
copy_unicast_stream_preset(uni_stream, default_source_preset);
stream_param[start_param.count].codec = &uni_stream->codec;
stream_param[start_param.count].qos = &uni_stream->qos;

Expand Down Expand Up @@ -399,9 +378,9 @@ static int cmd_cap_initiator_unicast_update(const struct shell *sh, size_t argc,


if (ep_info.dir == BT_AUDIO_DIR_SINK) {
cap_copy_preset(uni_stream, default_sink_preset);
copy_unicast_stream_preset(uni_stream, default_sink_preset);
} else {
cap_copy_preset(uni_stream, default_source_preset);
copy_unicast_stream_preset(uni_stream, default_source_preset);
}

params[count].meta = uni_stream->codec.meta;
Expand Down Expand Up @@ -441,9 +420,9 @@ static int cmd_cap_initiator_unicast_update(const struct shell *sh, size_t argc,
params[count].stream = stream;

if (ep_info.dir == BT_AUDIO_DIR_SINK) {
cap_copy_preset(uni_stream, default_sink_preset);
copy_unicast_stream_preset(uni_stream, default_sink_preset);
} else {
cap_copy_preset(uni_stream, default_source_preset);
copy_unicast_stream_preset(uni_stream, default_source_preset);
}

params[count].meta = uni_stream->codec.meta;
Expand Down

0 comments on commit e4d1e4d

Please sign in to comment.