Bluetooth: Audio: Add parsers for getting metadata values

Add parsers to retrieve metadata values from codec
capabilities and codec configuration structs.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2023-09-15 15:21:49 +02:00 committed by Fabio Baltieri
parent 8eecca2419
commit 9693ae3021
2 changed files with 499 additions and 11 deletions

View file

@ -659,11 +659,8 @@ int bt_audio_codec_cfg_get_octets_per_frame(const struct bt_audio_codec_cfg *cod
int bt_audio_codec_cfg_get_frame_blocks_per_sdu(const struct bt_audio_codec_cfg *codec_cfg,
bool fallback_to_default);
/** @brief Lookup a specific value based on type
*
* Depending on context bt_audio_codec_cfg will be either codec capabilities, codec configuration
* or meta data.
*
/** @brief Lookup a specific codec configuration value based on type
* *
* Typically types used are:
* @ref bt_audio_codec_capability_type
* @ref bt_audio_codec_config_type
@ -679,6 +676,191 @@ uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg, u
/** @} */ /* End of bt_audio_codec_cfg */
/**
* @brief Audio codec Config APIs
* @defgroup bt_audio_codec_meta Metadata parsing APIs
*
* Functions to parse LTV formatted metadata.
*
* @{
*/
/** @brief Lookup a specific metadata value based on type
*
* This works for metadata from both @ref bt_audio_codec_cfg meta and @ref bt_audio_codec_cap meta.
* *
* @param[in] meta The metadata to search in.
* @param[in] meta_len The length of @p meta.
* @param[in] type The type id to look for
* @param[out] data Pointer to the data-pointer to update when item is found
*
* @retval Length of found @p data (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_val(const uint8_t meta[], size_t meta_len, uint8_t type,
const uint8_t **data);
/** @brief Extract preferred contexts
*
* See @ref BT_AUDIO_METADATA_TYPE_PREF_CONTEXT for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval The preferred context type if positive or 0
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
* @retval -EBADMSG if found value has invalid size
*/
int bt_audio_codec_meta_get_pref_context(const uint8_t meta[], size_t meta_len);
/** @brief Extract stream contexts
*
* See @ref BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval The stream context type if positive or 0
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
* @retval -EBADMSG if found value has invalid size
*/
int bt_audio_codec_meta_get_stream_context(const uint8_t meta[], size_t meta_len);
/** @brief Extract program info
*
* See @ref BT_AUDIO_METADATA_TYPE_PROGRAM_INFO for more information about this value.
*
* @param[in] meta The metadata to get the data from in.
* @param[in] meta_len The length of @p meta.
* @param[out] program_info Pointer to the UTF-8 formatted program info.
*
* @retval The length of the @p program_info (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_program_info(const uint8_t meta[], size_t meta_len,
const uint8_t **program_info);
/** @brief Extract stream language
*
* See @ref BT_AUDIO_METADATA_TYPE_STREAM_LANG for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval The stream language if positive or 0
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
* @retval -EBADMSG if found value has invalid size
*/
int bt_audio_codec_meta_get_stream_lang(const uint8_t meta[], size_t meta_len);
/** @brief Extract CCID list
*
* See @ref BT_AUDIO_METADATA_TYPE_CCID_LIST for more information about this value.
*
* @param[in] meta The metadata to get the data from in.
* @param[in] meta_len The length of @p meta.
* @param[out] ccid_list Pointer to the array containing 8-bit CCIDs.
*
* @retval The length of the @p ccid_list (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_ccid_list(const uint8_t meta[], size_t meta_len,
const uint8_t **ccid_list);
/** @brief Extract parental rating
*
* See @ref BT_AUDIO_METADATA_TYPE_PARENTAL_RATING for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval The parental rating if positive or 0
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
* @retval -EBADMSG if found value has invalid size
*/
int bt_audio_codec_meta_get_parental_rating(const uint8_t meta[], size_t meta_len);
/** @brief Extract program info URI
*
* See @ref BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI for more information about this value.
*
* @param[in] meta The metadata to get the data from in.
* @param[in] meta_len The length of @p meta.
* @param[out] program_info_uri Pointer to the UTF-8 formatted program info URI.
*
* @retval The length of the @p ccid_list (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_program_info_uri(const uint8_t meta[], size_t meta_len,
const uint8_t **program_info_uri);
/** @brief Extract audio active state
*
* See @ref BT_AUDIO_METADATA_TYPE_AUDIO_STATE for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval The preferred context type if positive or 0
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
* @retval -EBADMSG if found value has invalid size
*/
int bt_audio_codec_meta_get_audio_active_state(const uint8_t meta[], size_t meta_len);
/** @brief Extract broadcast audio immediate rendering flag
*
* See @ref BT_AUDIO_METADATA_TYPE_BROADCAST_IMMEDIATE for more information about this value.
*
* @param meta The metadata to get the data from in.
* @param meta_len The length of @p meta.
*
* @retval 0 if the flag was found
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not the flag was not found
*/
int bt_audio_codec_meta_get_broadcast_audio_immediate_rendering_flag(const uint8_t meta[],
size_t meta_len);
/** @brief Extract extended metadata
*
* See @ref BT_AUDIO_METADATA_TYPE_EXTENDED for more information about this value.
*
* @param[in] meta The metadata to get the data from in.
* @param[in] meta_len The length of @p meta.
* @param[out] extended_meta Pointer to the extended metadata.
*
* @retval The length of the @p ccid_list (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_extended(const uint8_t meta[], size_t meta_len,
const uint8_t **extended_meta);
/** @brief Extract vendor specific metadata
*
* See @ref BT_AUDIO_METADATA_TYPE_VENDOR for more information about this value.
*
* @param[in] meta The metadata to get the data from in.
* @param[in] meta_len The length of @p meta.
* @param[out] vendor_meta Pointer to the vendor specific metadata.
*
* @retval The length of the @p ccid_list (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
int bt_audio_codec_meta_get_vendor(const uint8_t meta[], size_t meta_len,
const uint8_t **vendor_meta);
/** @} */ /* End of bt_audio_codec_meta */
#ifdef __cplusplus
}
#endif

View file

@ -18,9 +18,8 @@
LOG_MODULE_REGISTER(bt_audio_codec, CONFIG_BT_AUDIO_CODEC_LOG_LEVEL);
#if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
struct search_type_param {
bool found;
uint8_t type;
uint8_t data_len;
const uint8_t **data;
@ -31,6 +30,7 @@ static bool parse_cb(struct bt_data *data, void *user_data)
struct search_type_param *param = (struct search_type_param *)user_data;
if (param->type == data->type) {
param->found = true;
param->data_len = data->data_len;
*param->data = data->data;
@ -40,10 +40,13 @@ static bool parse_cb(struct bt_data *data, void *user_data)
return true;
}
#if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg, uint8_t type,
const uint8_t **data)
{
struct search_type_param param = {
.found = false,
.type = type,
.data_len = 0,
.data = data,
@ -168,8 +171,8 @@ int bt_audio_codec_cfg_get_chan_allocation_val(const struct bt_audio_codec_cfg *
return BT_AUDIO_CODEC_PARSE_ERR_INVALID_PARAM;
}
data_len = bt_audio_codec_cfg_get_val(codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC,
&data);
data_len =
bt_audio_codec_cfg_get_val(codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC, &data);
if (data == NULL) {
return BT_AUDIO_CODEC_PARSE_ERR_TYPE_NOT_FOUND;
}
@ -193,8 +196,8 @@ int bt_audio_codec_cfg_get_octets_per_frame(const struct bt_audio_codec_cfg *cod
return BT_AUDIO_CODEC_PARSE_ERR_INVALID_PARAM;
}
data_len = bt_audio_codec_cfg_get_val(codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_FRAME_LEN,
&data);
data_len =
bt_audio_codec_cfg_get_val(codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_FRAME_LEN, &data);
if (data == NULL) {
return BT_AUDIO_CODEC_PARSE_ERR_TYPE_NOT_FOUND;
}
@ -234,3 +237,306 @@ int bt_audio_codec_cfg_get_frame_blocks_per_sdu(const struct bt_audio_codec_cfg
return data[0];
}
#endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
#if CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE > 0 || \
CONFIG_BT_AUDIO_CODEC_CAP_MAX_METADATA_SIZE > 0
int bt_audio_codec_meta_get_val(const uint8_t meta[], size_t meta_len, uint8_t type,
const uint8_t **data)
{
struct search_type_param param = {
.type = type,
.data_len = 0,
.data = data,
};
int err;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(data == NULL) {
LOG_DBG("data is NULL");
return -EINVAL;
}
*data = NULL;
err = bt_audio_data_parse(meta, meta_len, parse_cb, &param);
if (err != 0 && err != -ECANCELED) {
LOG_DBG("Could not parse the meta data: %d", err);
return err;
}
if (!param.found) {
LOG_DBG("Could not find the type %u", type);
return -ENODATA;
}
return param.data_len;
}
int bt_audio_codec_meta_get_pref_context(const uint8_t meta[], size_t meta_len)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
&data);
if (data == NULL) {
return -ENODATA;
}
if (ret != sizeof(uint16_t)) {
return -EBADMSG;
}
return sys_get_le16(data);
}
int bt_audio_codec_meta_get_stream_context(const uint8_t meta[], size_t meta_len)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
&data);
if (data == NULL) {
return -ENODATA;
}
if (ret != sizeof(uint16_t)) {
return -EBADMSG;
}
return sys_get_le16(data);
}
int bt_audio_codec_meta_get_program_info(const uint8_t meta[], size_t meta_len,
const uint8_t **program_info)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(program_info == NULL) {
LOG_DBG("program_info is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_PROGRAM_INFO,
&data);
if (data == NULL) {
return -ENODATA;
}
*program_info = data;
return ret;
}
int bt_audio_codec_meta_get_stream_lang(const uint8_t meta[], size_t meta_len)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_STREAM_LANG,
&data);
if (data == NULL) {
return -ENODATA;
}
if (ret != 3) { /* Stream language is 3 octets */
return -EBADMSG;
}
return sys_get_le24(data);
}
int bt_audio_codec_meta_get_ccid_list(const uint8_t meta[], size_t meta_len,
const uint8_t **ccid_list)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(ccid_list == NULL) {
LOG_DBG("ccid_list is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_CCID_LIST, &data);
if (data == NULL) {
return -ENODATA;
}
*ccid_list = data;
return ret;
}
int bt_audio_codec_meta_get_parental_rating(const uint8_t meta[], size_t meta_len)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
&data);
if (data == NULL) {
return -ENODATA;
}
if (ret != sizeof(uint8_t)) {
return -EBADMSG;
}
return data[0];
}
int bt_audio_codec_meta_get_program_info_uri(const uint8_t meta[], size_t meta_len,
const uint8_t **program_info_uri)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(program_info_uri == NULL) {
LOG_DBG("program_info_uri is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI,
&data);
if (data == NULL) {
return -ENODATA;
}
*program_info_uri = data;
return ret;
}
int bt_audio_codec_meta_get_audio_active_state(const uint8_t meta[], size_t meta_len)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_AUDIO_STATE,
&data);
if (data == NULL) {
return -ENODATA;
}
if (ret != sizeof(uint8_t)) {
return -EBADMSG;
}
return data[0];
}
int bt_audio_codec_meta_get_broadcast_audio_immediate_rendering_flag(const uint8_t meta[],
size_t meta_len)
{
const uint8_t *data;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
return bt_audio_codec_meta_get_val(meta, meta_len,
BT_AUDIO_METADATA_TYPE_BROADCAST_IMMEDIATE, &data);
}
int bt_audio_codec_meta_get_extended(const uint8_t meta[], size_t meta_len,
const uint8_t **extended_meta)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(extended_meta == NULL) {
LOG_DBG("extended_meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_EXTENDED, &data);
if (data == NULL) {
return -ENODATA;
}
*extended_meta = data;
return ret;
}
int bt_audio_codec_meta_get_vendor(const uint8_t meta[], size_t meta_len,
const uint8_t **vendor_meta)
{
const uint8_t *data;
int ret;
CHECKIF(meta == NULL) {
LOG_DBG("meta is NULL");
return -EINVAL;
}
CHECKIF(vendor_meta == NULL) {
LOG_DBG("vendor_meta is NULL");
return -EINVAL;
}
ret = bt_audio_codec_meta_get_val(meta, meta_len, BT_AUDIO_METADATA_TYPE_VENDOR, &data);
if (data == NULL) {
return -ENODATA;
}
*vendor_meta = data;
return ret;
}
#endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE > 0 || \
* CONFIG_BT_AUDIO_CODEC_CAP_MAX_METADATA_SIZE > 0 \
*/