Skip to content

Commit

Permalink
Bluetooth: Audio: Update return value of {cfg,cap}_get_val
Browse files Browse the repository at this point in the history
The get_val functions will now return -ENODATA in case that
a value isn't found, instead of 0.

This makes them more similar to the meta_get_val functions.

Signed-off-by: Emil Gydesen <[email protected]>
  • Loading branch information
Thalley committed Dec 7, 2023
1 parent 0963b6c commit 216328d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
17 changes: 11 additions & 6 deletions include/zephyr/bluetooth/audio/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,13 @@ int bt_audio_codec_cfg_set_frame_blocks_per_sdu(struct bt_audio_codec_cfg *codec
* @param[in] codec_cfg The codec data to search in.
* @param[in] type The type id to look for
* @param[out] data Pointer to the data-pointer to update when item is found
* @return Length of found @p data or 0 if not found
*
* @retval Length of found @p data (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg,
enum bt_audio_codec_config_type type, const uint8_t **data);
int bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg,
enum bt_audio_codec_config_type type, const uint8_t **data);

/**
* @brief Set or add a specific codec configuration value
Expand Down Expand Up @@ -1178,10 +1181,12 @@ int bt_audio_codec_cfg_meta_set_vendor(struct bt_audio_codec_cfg *codec_cfg,
* @param[in] type The type id to look for
* @param[out] data Pointer to the data-pointer to update when item is found
*
* @return Length of found @p data or 0 if not found
* @retval Length of found @p data (may be 0)
* @retval -EINVAL if arguments are invalid
* @retval -ENODATA if not found
*/
uint8_t bt_audio_codec_cap_get_val(const struct bt_audio_codec_cap *codec_cap,
enum bt_audio_codec_capability_type type, const uint8_t **data);
int bt_audio_codec_cap_get_val(const struct bt_audio_codec_cap *codec_cap,
enum bt_audio_codec_capability_type type, const uint8_t **data);

/**
* @brief Set or add a specific codec capability value
Expand Down
34 changes: 17 additions & 17 deletions subsys/bluetooth/audio/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ static void init_net_buf_simple_from_codec_cfg(struct net_buf_simple *buf,
buf->len = codec_cfg->data_len;
}

uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg,
enum bt_audio_codec_config_type type, const uint8_t **data)
int bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg,
enum bt_audio_codec_config_type type, const uint8_t **data)
{
struct search_type_param param = {
.found = false,
Expand All @@ -283,25 +283,25 @@ uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg,

CHECKIF(codec_cfg == NULL) {
LOG_DBG("codec is NULL");
return 0;
return -EINVAL;
}

CHECKIF(data == NULL) {
LOG_DBG("data is NULL");
return 0;
return -EINVAL;
}

*data = NULL;

err = bt_audio_data_parse(codec_cfg->data, codec_cfg->data_len, parse_cb, &param);
if (err != 0 && err != -ECANCELED) {
LOG_DBG("Could not parse the data: %d", err);
return 0;
return err;
}

if (param.data == NULL) {
if (!param.found) {
LOG_DBG("Could not find the type %u", type);
return 0;
return -ENODATA;
}

return param.data_len;
Expand Down Expand Up @@ -582,12 +582,12 @@ static void init_net_buf_simple_from_meta(struct net_buf_simple *buf, uint8_t me
buf->len = meta_len;
}

static int codec_meta_get_val(const uint8_t meta[], size_t meta_len, uint8_t type,
const uint8_t **data)
static int codec_meta_get_val(const uint8_t meta[], size_t meta_len,
enum bt_audio_metadata_type type, const uint8_t **data)
{
struct search_type_param param = {
.found = false,
.type = type,
.type = (uint8_t)type,
.data_len = 0,
.data = data,
};
Expand Down Expand Up @@ -1784,8 +1784,8 @@ static void init_net_buf_simple_from_codec_cap(struct net_buf_simple *buf,
buf->len = codec_cap->data_len;
}

uint8_t bt_audio_codec_cap_get_val(const struct bt_audio_codec_cap *codec_cap,
enum bt_audio_codec_capability_type type, const uint8_t **data)
int bt_audio_codec_cap_get_val(const struct bt_audio_codec_cap *codec_cap,
enum bt_audio_codec_capability_type type, const uint8_t **data)
{
struct search_type_param param = {
.found = false,
Expand All @@ -1797,25 +1797,25 @@ uint8_t bt_audio_codec_cap_get_val(const struct bt_audio_codec_cap *codec_cap,

CHECKIF(codec_cap == NULL) {
LOG_DBG("codec_cap is NULL");
return 0;
return -EINVAL;
}

CHECKIF(data == NULL) {
LOG_DBG("data is NULL");
return 0;
return -EINVAL;
}

*data = NULL;

err = bt_audio_data_parse(codec_cap->data, codec_cap->data_len, parse_cb, &param);
if (err != 0 && err != -ECANCELED) {
LOG_DBG("Could not parse the data: %d", err);
return 0;
return err;
}

if (param.data == NULL) {
if (!param.found) {
LOG_DBG("Could not find the type %u", type);
return 0;
return -ENODATA;
}

return param.data_len;
Expand Down
8 changes: 4 additions & 4 deletions tests/bluetooth/audio/codec/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_val_new)
int ret;

ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_FREQ, &data);
zassert_equal(ret, 0, "Unexpected return value %d", ret);
zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);

ret = bt_audio_codec_cfg_set_val(&codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_FREQ,
&new_expected_data, sizeof(new_expected_data));
Expand Down Expand Up @@ -95,7 +95,7 @@ ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_unset_val)
zassert_true(ret >= 0, "Unexpected return value %d", ret);

ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CONFIG_LC3_FREQ, &data);
zassert_equal(ret, 0, "Unexpected return value %d", ret);
zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
}

ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_freq_to_freq_hz)
Expand Down Expand Up @@ -816,7 +816,7 @@ ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_val_new)
int ret;

ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CONFIG_LC3_FREQ, &data);
zassert_equal(ret, 0, "Unexpected return value %d", ret);
zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);

ret = bt_audio_codec_cap_set_val(&codec_cap, BT_AUDIO_CODEC_CONFIG_LC3_FREQ,
&new_expected_data, sizeof(new_expected_data));
Expand Down Expand Up @@ -846,7 +846,7 @@ ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_unset_val)
zassert_true(ret >= 0, "Unexpected return value %d", ret);

ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CONFIG_LC3_FREQ, &data);
zassert_equal(ret, 0, "Unexpected return value %d", ret);
zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
}

ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_freq)
Expand Down

0 comments on commit 216328d

Please sign in to comment.