Skip to content

Commit 85bb262

Browse files
Thalleyaescolar
authored andcommitted
Bluetooth: Audio: Refactor codec_cfg_get_freq
Refactor the codec_cfg_get_freq function to return the assigned numbers value, instead of a converted value, but with support for converting the value. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 551b9af commit 85bb262

File tree

10 files changed

+449
-125
lines changed

10 files changed

+449
-125
lines changed

include/zephyr/bluetooth/audio/audio.h

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,50 @@ struct bt_audio_codec_qos_pref {
566566
* @{
567567
*/
568568

569+
/**
570+
* @brief Convert assigned numbers frequency to frequency value.
571+
*
572+
* @param freq The assigned numbers frequency to convert.
573+
*
574+
* @retval -EINVAL if arguments are invalid.
575+
* @retval The converted frequency value in Hz.
576+
*/
577+
int bt_audio_codec_cfg_freq_to_freq_hz(enum bt_audio_codec_config_freq freq);
578+
579+
/**
580+
* @brief Convert frequency value to assigned numbers frequency.
581+
*
582+
* @param freq_hz The frequency value to convert.
583+
*
584+
* @retval -EINVAL if arguments are invalid.
585+
* @retval The assigned numbers frequency (@ref bt_audio_codec_config_freq).
586+
*/
587+
int bt_audio_codec_cfg_freq_hz_to_freq(uint32_t freq_hz);
588+
569589
/**@brief Extract the frequency from a codec configuration.
570590
*
571591
* @param codec_cfg The codec configuration to extract data from.
572592
*
573-
* @retval The frequency in Hz
593+
* @retval A @ref bt_audio_codec_config_freq value
574594
* @retval -EINVAL if arguments are invalid
575595
* @retval -ENODATA if not found
576596
* @retval -EBADMSG if found value has invalid size or value
577597
*/
578598
int bt_audio_codec_cfg_get_freq(const struct bt_audio_codec_cfg *codec_cfg);
579599

600+
/**
601+
* @brief Set the frequency of a codec configuration.
602+
*
603+
* @param codec_cfg The codec configuration to set data for.
604+
* @param freq The assigned numbers frequency to set.
605+
*
606+
* @retval The data_len of @p codec_cfg on success
607+
* @retval -EINVAL if arguments are invalid
608+
* @retval -ENOMEM if the new value could not set or added due to memory
609+
*/
610+
int bt_audio_codec_cfg_set_freq(struct bt_audio_codec_cfg *codec_cfg,
611+
enum bt_audio_codec_config_freq freq);
612+
580613
/** @brief Extract frame duration from BT codec config
581614
*
582615
* @param codec_cfg The codec configuration to extract data from.
@@ -661,6 +694,21 @@ int bt_audio_codec_cfg_get_frame_blocks_per_sdu(const struct bt_audio_codec_cfg
661694
uint8_t bt_audio_codec_cfg_get_val(const struct bt_audio_codec_cfg *codec_cfg, uint8_t type,
662695
const uint8_t **data);
663696

697+
/**
698+
* @brief Set or add a specific codec configuration value
699+
*
700+
* @param codec_cfg The codec data to set the value in.
701+
* @param type The type id to set
702+
* @param data Pointer to the data-pointer to set
703+
* @param data_len Length of @p data
704+
*
705+
* @retval The data_len of @p codec_cfg on success
706+
* @retval -EINVAL if arguments are invalid
707+
* @retval -ENOMEM if the new value could not set or added due to memory
708+
*/
709+
int bt_audio_codec_cfg_set_val(struct bt_audio_codec_cfg *codec_cfg, uint8_t type,
710+
const uint8_t *data, size_t data_len);
711+
664712
/** @} */ /* End of bt_audio_codec_cfg */
665713

666714
/**

include/zephyr/bluetooth/audio/lc3.h

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -173,73 +173,75 @@ struct bt_audio_codec_octets_per_codec_frame {
173173
enum bt_audio_codec_config_type {
174174

175175
/** @brief LC3 Sample Frequency configuration type. */
176-
BT_AUDIO_CODEC_CONFIG_LC3_FREQ = 0x01,
176+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ = 0x01,
177177

178178
/** @brief LC3 Frame Duration configuration type. */
179-
BT_AUDIO_CODEC_CONFIG_LC3_DURATION = 0x02,
179+
BT_AUDIO_CODEC_CONFIG_LC3_DURATION = 0x02,
180180

181181
/** @brief LC3 channel Allocation configuration type. */
182-
BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC = 0x03,
182+
BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC = 0x03,
183183

184184
/** @brief LC3 Frame Length configuration type. */
185-
BT_AUDIO_CODEC_CONFIG_LC3_FRAME_LEN = 0x04,
185+
BT_AUDIO_CODEC_CONFIG_LC3_FRAME_LEN = 0x04,
186186

187187
/** @brief Codec frame blocks, per SDU configuration type. */
188-
BT_AUDIO_CODEC_CONFIG_LC3_FRAME_BLKS_PER_SDU = 0x05,
188+
BT_AUDIO_CODEC_CONFIG_LC3_FRAME_BLKS_PER_SDU = 0x05,
189189
};
190190

191-
/**
192-
* @brief 8 Khz codec Sample Frequency configuration
193-
*/
194-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_8KHZ 0x01
195-
/**
196-
* @brief 11.025 Khz codec Sample Frequency configuration
197-
*/
198-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_11KHZ 0x02
199-
/**
200-
* @brief 16 Khz codec Sample Frequency configuration
201-
*/
202-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_16KHZ 0x03
203-
/**
204-
* @brief 22.05 Khz codec Sample Frequency configuration
205-
*/
206-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_22KHZ 0x04
207-
/**
208-
* @brief 24 Khz codec Sample Frequency configuration
209-
*/
210-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_24KHZ 0x05
211-
/**
212-
* @brief 32 Khz codec Sample Frequency configuration
213-
*/
214-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_32KHZ 0x06
215-
/**
216-
* @brief 44.1 Khz codec Sample Frequency configuration
217-
*/
218-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_44KHZ 0x07
219-
/**
220-
* @brief 48 Khz codec Sample Frequency configuration
221-
*/
222-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_48KHZ 0x08
223-
/**
224-
* @brief 88.2 Khz codec Sample Frequency configuration
225-
*/
226-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_88KHZ 0x09
227-
/**
228-
* @brief 96 Khz codec Sample Frequency configuration
229-
*/
230-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_96KHZ 0x0a
231-
/**
232-
* @brief 176.4 Khz codec Sample Frequency configuration
233-
*/
234-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_176KHZ 0x0b
235-
/**
236-
* @brief 192 Khz codec Sample Frequency configuration
237-
*/
238-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_192KHZ 0x0c
239-
/**
240-
* @brief 384 Khz codec Sample Frequency configuration
241-
*/
242-
#define BT_AUDIO_CODEC_CONFIG_LC3_FREQ_384KHZ 0x0d
191+
enum bt_audio_codec_config_freq {
192+
/**
193+
* @brief 8 Khz codec Sample Frequency configuration
194+
*/
195+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_8KHZ = 0x01,
196+
/**
197+
* @brief 11.025 Khz codec Sample Frequency configuration
198+
*/
199+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_11KHZ = 0x02,
200+
/**
201+
* @brief 16 Khz codec Sample Frequency configuration
202+
*/
203+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_16KHZ = 0x03,
204+
/**
205+
* @brief 22.05 Khz codec Sample Frequency configuration
206+
*/
207+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_22KHZ = 0x04,
208+
/**
209+
* @brief 24 Khz codec Sample Frequency configuration
210+
*/
211+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_24KHZ = 0x05,
212+
/**
213+
* @brief 32 Khz codec Sample Frequency configuration
214+
*/
215+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_32KHZ = 0x06,
216+
/**
217+
* @brief 44.1 Khz codec Sample Frequency configuration
218+
*/
219+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_44KHZ = 0x07,
220+
/**
221+
* @brief 48 Khz codec Sample Frequency configuration
222+
*/
223+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_48KHZ = 0x08,
224+
/**
225+
* @brief 88.2 Khz codec Sample Frequency configuration
226+
*/
227+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_88KHZ = 0x09,
228+
/**
229+
* @brief 96 Khz codec Sample Frequency configuration
230+
*/
231+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_96KHZ = 0x0a,
232+
/**
233+
* @brief 176.4 Khz codec Sample Frequency configuration
234+
*/
235+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_176KHZ = 0x0b,
236+
/**
237+
* @brief 192 Khz codec Sample Frequency configuration
238+
*/
239+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_192KHZ = 0x0c,
240+
/**
241+
* @brief 384 Khz codec Sample Frequency configuration
242+
*/
243+
BT_AUDIO_CODEC_CONFIG_LC3_FREQ_384KHZ = 0x0d,
244+
};
243245

244246
/**
245247
* @brief LC3 7.5 msec Frame Duration configuration
@@ -250,7 +252,6 @@ enum bt_audio_codec_config_type {
250252
*/
251253
#define BT_AUDIO_CODEC_CONFIG_LC3_DURATION_10 0x01
252254

253-
254255
/**
255256
* @brief Helper to declare LC3 codec capability
256257
*

samples/bluetooth/hap_ha/src/bap_unicast_sr.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ static void print_codec_cfg(const struct bt_audio_codec_cfg *codec_cfg)
7575
codec_cfg->vid, codec_cfg->data_len);
7676

7777
if (codec_cfg->id == BT_HCI_CODING_FORMAT_LC3) {
78-
/* LC3 uses the generic LTV format - other codecs might do as well */
79-
8078
enum bt_audio_location chan_allocation;
79+
int ret;
80+
81+
/* LC3 uses the generic LTV format - other codecs might do as well */
8182

8283
bt_audio_data_parse(codec_cfg->data, codec_cfg->data_len, print_cb, "data");
8384

84-
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_get_freq(codec_cfg));
85+
ret = bt_audio_codec_cfg_get_freq(codec_cfg);
86+
if (ret > 0) {
87+
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_freq_to_freq_hz(ret));
88+
}
89+
8590
printk(" Frame Duration: %d us\n",
8691
bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
8792
if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {

samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ static void print_codec_cfg(const struct bt_audio_codec_cfg *codec_cfg)
6464
codec_cfg->vid, codec_cfg->data_len);
6565

6666
if (codec_cfg->id == BT_HCI_CODING_FORMAT_LC3) {
67-
/* LC3 uses the generic LTV format - other codecs might do as well */
68-
6967
enum bt_audio_location chan_allocation;
68+
int ret;
69+
70+
/* LC3 uses the generic LTV format - other codecs might do as well */
7071

7172
bt_audio_data_parse(codec_cfg->data, codec_cfg->data_len, print_cb, "data");
7273

73-
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_get_freq(codec_cfg));
74+
ret = bt_audio_codec_cfg_get_freq(codec_cfg);
75+
if (ret > 0) {
76+
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_freq_to_freq_hz(ret));
77+
}
78+
7479
printk(" Frame Duration: %d us\n",
7580
bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
7681
if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {

samples/bluetooth/unicast_audio_client/src/main.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,19 @@ static void lc3_audio_timer_timeout(struct k_work *work)
221221
}
222222
}
223223

224-
static void init_lc3(void)
224+
static int init_lc3(void)
225225
{
226226
const struct bt_audio_codec_cfg *codec_cfg = &codec_configuration.codec_cfg;
227227
unsigned int num_samples;
228+
int ret;
229+
230+
ret = bt_audio_codec_cfg_get_freq(codec_cfg);
231+
if (ret > 0) {
232+
freq_hz = bt_audio_codec_cfg_freq_to_freq_hz(ret);
233+
} else {
234+
return ret;
235+
}
228236

229-
freq_hz = bt_audio_codec_cfg_get_freq(codec_cfg);
230237
frame_duration_us = bt_audio_codec_cfg_get_frame_duration_us(codec_cfg);
231238
octets_per_frame = bt_audio_codec_cfg_get_octets_per_frame(codec_cfg);
232239
frames_per_sdu = bt_audio_codec_cfg_get_frame_blocks_per_sdu(codec_cfg, true);
@@ -271,7 +278,7 @@ static void init_lc3(void)
271278

272279
#else
273280

274-
#define init_lc3(...)
281+
#define init_lc3(...) 0
275282

276283
/**
277284
* @brief Send audio data on timeout
@@ -979,7 +986,11 @@ static int set_stream_qos(void)
979986
static int enable_streams(void)
980987
{
981988
if (IS_ENABLED(CONFIG_LIBLC3)) {
982-
init_lc3();
989+
int err = init_lc3();
990+
991+
if (err != 0) {
992+
return err;
993+
}
983994
}
984995

985996
for (size_t i = 0U; i < configured_stream_count; i++) {

samples/bluetooth/unicast_audio_server/src/main.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,18 @@ static void print_codec_cfg(const struct bt_audio_codec_cfg *codec_cfg)
135135
codec_cfg->vid, codec_cfg->data_len);
136136

137137
if (codec_cfg->id == BT_HCI_CODING_FORMAT_LC3) {
138-
/* LC3 uses the generic LTV format - other codecs might do as well */
139-
140138
enum bt_audio_location chan_allocation;
139+
int ret;
140+
141+
/* LC3 uses the generic LTV format - other codecs might do as well */
141142

142143
bt_audio_data_parse(codec_cfg->data, codec_cfg->data_len, print_cb, "data");
143144

144-
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_get_freq(codec_cfg));
145+
ret = bt_audio_codec_cfg_get_freq(codec_cfg);
146+
if (ret > 0) {
147+
printk(" Frequency: %d Hz\n", bt_audio_codec_cfg_freq_to_freq_hz(ret));
148+
}
149+
145150
printk(" Frame Duration: %d us\n",
146151
bt_audio_codec_cfg_get_frame_duration_us(codec_cfg));
147152
if (bt_audio_codec_cfg_get_chan_allocation_val(codec_cfg, &chan_allocation) == 0) {
@@ -349,15 +354,19 @@ static int lc3_enable(struct bt_bap_stream *stream, const uint8_t meta[], size_t
349354

350355
#if defined(CONFIG_LIBLC3)
351356
{
352-
const int freq = bt_audio_codec_cfg_get_freq(stream->codec_cfg);
353357
const int frame_duration_us =
354358
bt_audio_codec_cfg_get_frame_duration_us(stream->codec_cfg);
359+
int freq;
360+
int ret;
355361

356-
if (freq < 0) {
362+
ret = bt_audio_codec_cfg_get_freq(stream->codec_cfg);
363+
if (ret > 0) {
364+
freq = bt_audio_codec_cfg_freq_to_freq_hz(ret);
365+
} else {
357366
printk("Error: Codec frequency not set, cannot start codec.");
358367
*rsp = BT_BAP_ASCS_RSP(BT_BAP_ASCS_RSP_CODE_CONF_INVALID,
359368
BT_BAP_ASCS_REASON_CODEC_DATA);
360-
return -1;
369+
return ret;
361370
}
362371

363372
if (frame_duration_us < 0) {

0 commit comments

Comments
 (0)