Skip to content

Commit 146dce9

Browse files
Thalleyfabiobaltieri
authored andcommitted
Bluetooth: BAP: Shell: Reverse in/out terminology
In USB "in" refers to data going from device to host, and "out" refers to data going from host to device. From a BT perspective "usb_out" then refers to the data we receive from the host, to be sent over BT, so the "out" terminology still works for that, and vice versa for incoming BT data. Signed-off-by: Emil Gydesen <[email protected]>
1 parent a629d28 commit 146dce9

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

subsys/bluetooth/audio/shell/bap_usb.c

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <zephyr/net_buf.h>
2626
#include <zephyr/shell/shell.h>
2727
#include <zephyr/sys/__assert.h>
28+
#include <zephyr/sys/clock.h>
2829
#include <zephyr/sys/ring_buffer.h>
2930
#include <zephyr/sys/util.h>
3031
#include <zephyr/sys/util_macro.h>
@@ -51,8 +52,6 @@ LOG_MODULE_REGISTER(bap_usb, CONFIG_BT_BAP_STREAM_LOG_LEVEL);
5152
#define USB_MONO_FRAME_SIZE (USB_SAMPLE_CNT * USB_BYTES_PER_SAMPLE)
5253
#define USB_CHANNELS 2U
5354
#define USB_STEREO_FRAME_SIZE (USB_MONO_FRAME_SIZE * USB_CHANNELS)
54-
#define USB_OUT_RING_BUF_SIZE (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)
55-
#define USB_IN_RING_BUF_SIZE (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)
5655

5756
#define IN_TERMINAL_ID UAC2_ENTITY_ID(DT_NODELABEL(in_terminal))
5857
#define OUT_TERMINAL_ID UAC2_ENTITY_ID(DT_NODELABEL(out_terminal))
@@ -85,6 +84,8 @@ static void usb_sof_cb(const struct device *dev, void *user_data)
8584
}
8685

8786
#if defined CONFIG_BT_AUDIO_RX
87+
#define USB_IN_RING_BUF_SIZE (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)
88+
8889
struct decoded_sdu {
8990
int16_t right_frames[MAX_CODEC_FRAMES_PER_SDU][LC3_MAX_NUM_SAMPLES_MONO];
9091
int16_t left_frames[MAX_CODEC_FRAMES_PER_SDU][LC3_MAX_NUM_SAMPLES_MONO];
@@ -94,8 +95,8 @@ struct decoded_sdu {
9495
uint32_t ts;
9596
} decoded_sdu;
9697

97-
RING_BUF_DECLARE(usb_out_ring_buf, USB_OUT_RING_BUF_SIZE);
98-
K_MEM_SLAB_DEFINE_STATIC(usb_out_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY),
98+
RING_BUF_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE);
99+
K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY),
99100
USB_ENQUEUE_COUNT, UDC_BUF_ALIGN);
100101

101102
/* USB consumer callback, called every 1ms, consumes data from ring-buffer */
@@ -110,14 +111,14 @@ static void usb_data_request(const struct device *dev)
110111
return;
111112
}
112113

113-
err = k_mem_slab_alloc(&usb_out_buf_pool, &pcm_buf, K_NO_WAIT);
114+
err = k_mem_slab_alloc(&usb_in_buf_pool, &pcm_buf, K_NO_WAIT);
114115
if (err != 0) {
115116
LOG_WRN("Could not allocate pcm_buf: %d", err);
116117
return;
117118
}
118119

119120
/* This may fail without causing issues since usb_audio_data is 0-initialized */
120-
size = ring_buf_get(&usb_out_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
121+
size = ring_buf_get(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
121122
if (size != USB_STEREO_FRAME_SIZE) {
122123
/* If we could not fill the buffer, zero-fill the rest (possibly all) */
123124
memset(((uint8_t *)pcm_buf) + size, 0, USB_STEREO_FRAME_SIZE - size);
@@ -146,14 +147,14 @@ static void usb_data_request(const struct device *dev)
146147
LOG_ERR("Failed to send USB audio: %d (%zu)", err, cnt);
147148
}
148149

149-
k_mem_slab_free(&usb_out_buf_pool, pcm_buf);
150+
k_mem_slab_free(&usb_in_buf_pool, pcm_buf);
150151
}
151152
}
152153

153154
static void usb_buf_release_cb(const struct device *dev, uint8_t terminal, void *buf,
154155
void *user_data)
155156
{
156-
k_mem_slab_free(&usb_out_buf_pool, buf);
157+
k_mem_slab_free(&usb_in_buf_pool, buf);
157158
}
158159

159160
static void bap_usb_send_frames_to_usb(void)
@@ -180,7 +181,7 @@ static void bap_usb_send_frames_to_usb(void)
180181
uint32_t rb_size;
181182

182183
/* Not enough space to store data */
183-
if (ring_buf_space_get(&usb_out_ring_buf) < sizeof(stereo_frame)) {
184+
if (ring_buf_space_get(&usb_in_ring_buf) < sizeof(stereo_frame)) {
184185
if ((fail_cnt % bap_get_stats_interval()) == 0U) {
185186
LOG_WRN("[%zu] Could not send more than %zu frames to USB",
186187
fail_cnt, i);
@@ -218,7 +219,7 @@ static void bap_usb_send_frames_to_usb(void)
218219
}
219220
}
220221

221-
rb_size = ring_buf_put(&usb_out_ring_buf, (uint8_t *)stereo_frame,
222+
rb_size = ring_buf_put(&usb_in_ring_buf, (uint8_t *)stereo_frame,
222223
sizeof(stereo_frame));
223224
if (rb_size != sizeof(stereo_frame)) {
224225
LOG_WRN("Failed to put frame on USB ring buf");
@@ -356,14 +357,15 @@ void bap_usb_clear_frames_to_usb(void)
356357
#endif /* CONFIG_BT_AUDIO_RX */
357358

358359
#if defined(CONFIG_BT_AUDIO_TX)
360+
#define USB_OUT_RING_BUF_SIZE (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)
361+
359362
/* Allocate 3: 1 for USB to receive data to and 2 additional buffers to prevent out of memory
360363
* errors when USB host decides to perform rapid terminal enable/disable cycles.
361364
*/
362-
K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, USB_STEREO_FRAME_SIZE, 3, UDC_BUF_ALIGN);
365+
K_MEM_SLAB_DEFINE_STATIC(usb_out_buf_pool, USB_STEREO_FRAME_SIZE, 3, UDC_BUF_ALIGN);
363366

364-
BUILD_ASSERT((USB_IN_RING_BUF_SIZE % USB_MONO_FRAME_SIZE) == 0);
365-
static int16_t usb_in_left_ring_buffer[USB_IN_RING_BUF_SIZE];
366-
static int16_t usb_in_right_ring_buffer[USB_IN_RING_BUF_SIZE];
367+
static int16_t usb_out_left_ring_buffer[USB_OUT_RING_BUF_SIZE];
368+
static int16_t usb_out_right_ring_buffer[USB_OUT_RING_BUF_SIZE];
367369
static size_t write_index; /* Points to the oldest/uninitialized data */
368370

369371
size_t bap_usb_get_read_cnt(const struct shell_stream *sh_stream)
@@ -425,7 +427,7 @@ static void *usb_get_recv_buf_cb(const struct device *dev, uint8_t terminal, uin
425427

426428
__ASSERT(size <= USB_STEREO_FRAME_SIZE, "%u was not <= %d", size, USB_STEREO_FRAME_SIZE);
427429

428-
ret = k_mem_slab_alloc(&usb_in_buf_pool, &buf, K_NO_WAIT);
430+
ret = k_mem_slab_alloc(&usb_out_buf_pool, &buf, K_NO_WAIT);
429431
if (ret != 0) {
430432
LOG_WRN("Failed to allocate buffer: %d", ret);
431433
}
@@ -441,7 +443,7 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
441443
int16_t *pcm;
442444

443445
if (!out_terminal_enabled || buf == NULL || size == 0U) {
444-
k_mem_slab_free(&usb_in_buf_pool, buf);
446+
k_mem_slab_free(&usb_out_buf_pool, buf);
445447
return;
446448
}
447449

@@ -454,13 +456,13 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
454456
* can be done once afterwards
455457
*/
456458
for (size_t i = 0U, j = 0U; i < USB_SAMPLE_CNT; i++, j += USB_CHANNELS) {
457-
usb_in_left_ring_buffer[write_index + i] = pcm[j];
458-
usb_in_right_ring_buffer[write_index + i] = pcm[j + 1];
459+
usb_out_left_ring_buffer[write_index + i] = pcm[j];
460+
usb_out_right_ring_buffer[write_index + i] = pcm[j + 1];
459461
}
460462

461463
write_index += USB_SAMPLE_CNT;
462464

463-
if (write_index == USB_IN_RING_BUF_SIZE) {
465+
if (write_index == USB_OUT_RING_BUF_SIZE) {
464466
/* Overflow so that we start overwriting oldest */
465467
write_index = 0U;
466468
}
@@ -474,7 +476,7 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
474476
LOG_DBG("USB Data received (count = %d)", cnt);
475477
}
476478

477-
k_mem_slab_free(&usb_in_buf_pool, buf);
479+
k_mem_slab_free(&usb_out_buf_pool, buf);
478480
}
479481

480482
bool bap_usb_can_get_full_sdu(struct shell_stream *sh_stream)
@@ -505,14 +507,14 @@ bool bap_usb_can_get_full_sdu(struct shell_stream *sh_stream)
505507
buffer_cnt = write_index - read_idx;
506508
} else {
507509
/* Handle the case where the read spans across the end of the buffer */
508-
buffer_cnt = write_index + (USB_IN_RING_BUF_SIZE - read_idx);
510+
buffer_cnt = write_index + (USB_OUT_RING_BUF_SIZE - read_idx);
509511
}
510512

511513
if (buffer_cnt < retrieve_cnt) {
512514
/* Not enough for a frame yet */
513515
if (!failed_last_time) {
514516
LOG_WRN("Ring buffer (%u/%u) does not contain enough for an entire SDU %u",
515-
buffer_cnt, USB_IN_RING_BUF_SIZE, retrieve_cnt);
517+
buffer_cnt, USB_OUT_RING_BUF_SIZE, retrieve_cnt);
516518
}
517519

518520
failed_last_time = true;
@@ -536,19 +538,19 @@ static size_t usb_ring_buf_get(int16_t dest[], int16_t src[], size_t idx, size_t
536538
{
537539
size_t new_idx;
538540

539-
if (idx >= USB_IN_RING_BUF_SIZE) {
541+
if (idx >= USB_OUT_RING_BUF_SIZE) {
540542
LOG_ERR("Invalid idx %zu", idx);
541543

542544
return 0;
543545
}
544546

545-
if ((idx + cnt) < USB_IN_RING_BUF_SIZE) {
547+
if ((idx + cnt) < USB_OUT_RING_BUF_SIZE) {
546548
/* Simply copy of the data and increment the index*/
547549
memcpy(dest, &src[idx], cnt * USB_BYTES_PER_SAMPLE);
548550
new_idx = idx + cnt;
549551
} else {
550552
/* Handle wrapping */
551-
const size_t first_read_cnt = USB_IN_RING_BUF_SIZE - idx;
553+
const size_t first_read_cnt = USB_OUT_RING_BUF_SIZE - idx;
552554
const size_t second_read_cnt = cnt - first_read_cnt;
553555

554556
memcpy(dest, &src[idx], first_read_cnt * USB_BYTES_PER_SAMPLE);
@@ -570,10 +572,10 @@ void bap_usb_get_frame(struct shell_stream *sh_stream, enum bt_audio_location ch
570572

571573
if (is_left || is_mono) {
572574
sh_stream->tx.left_read_idx = usb_ring_buf_get(
573-
buffer, usb_in_left_ring_buffer, sh_stream->tx.left_read_idx, read_cnt);
575+
buffer, usb_out_left_ring_buffer, sh_stream->tx.left_read_idx, read_cnt);
574576
} else if (is_right) {
575577
sh_stream->tx.right_read_idx = usb_ring_buf_get(
576-
buffer, usb_in_right_ring_buffer, sh_stream->tx.right_read_idx, read_cnt);
578+
buffer, usb_out_right_ring_buffer, sh_stream->tx.right_read_idx, read_cnt);
577579
}
578580
}
579581
#endif /* CONFIG_BT_AUDIO_TX */

0 commit comments

Comments
 (0)