Bluetooth: Audio: Shell: Add preset copy functions

Add common preset copy functions for unicast streams and
broadcast sources.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2023-05-05 11:40:21 +02:00 committed by Carles Cufí
parent 4d43101fa7
commit 926219156b
3 changed files with 76 additions and 52 deletions

View file

@ -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,
@ -185,6 +188,71 @@ 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];
const uint8_t data_len = preset_data->data.data_len;
data->data.data = data->value;
data->data.data_len = data_len;
memcpy(data->value, preset_data->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];
const uint8_t data_len = preset_data->data.data_len;
data->data.data = data->value;
data->data.data_len = data_len;
memcpy(data->value, preset_data->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];
const uint8_t data_len = preset_data->data.data_len;
data->data.data = data->value;
data->data.data_len = data_len;
memcpy(data->value, preset_data->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];
const uint8_t data_len = preset_data->data.data_len;
data->data.data = data->value;
data->data.data_len = data_len;
memcpy(data->value, preset_data->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 */

View file

@ -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) {
@ -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++) {

View file

@ -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[])
{
@ -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;
@ -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;
@ -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;
@ -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;