Commit 146dce9f authored by Emil Gydesen's avatar Emil Gydesen Committed by Fabio Baltieri
Browse files

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: default avatarEmil Gydesen <emil.gydesen@nordicsemi.no>
parent a629d282
Loading
Loading
Loading
Loading
+29 −27
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <zephyr/net_buf.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/clock.h>
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_macro.h>
@@ -51,8 +52,6 @@ LOG_MODULE_REGISTER(bap_usb, CONFIG_BT_BAP_STREAM_LOG_LEVEL);
#define USB_MONO_FRAME_SIZE    (USB_SAMPLE_CNT * USB_BYTES_PER_SAMPLE)
#define USB_CHANNELS           2U
#define USB_STEREO_FRAME_SIZE  (USB_MONO_FRAME_SIZE * USB_CHANNELS)
#define USB_OUT_RING_BUF_SIZE  (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)
#define USB_IN_RING_BUF_SIZE   (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)

#define IN_TERMINAL_ID  UAC2_ENTITY_ID(DT_NODELABEL(in_terminal))
#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)
}

#if defined CONFIG_BT_AUDIO_RX
#define USB_IN_RING_BUF_SIZE (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)

struct decoded_sdu {
	int16_t right_frames[MAX_CODEC_FRAMES_PER_SDU][LC3_MAX_NUM_SAMPLES_MONO];
	int16_t left_frames[MAX_CODEC_FRAMES_PER_SDU][LC3_MAX_NUM_SAMPLES_MONO];
@@ -94,8 +95,8 @@ struct decoded_sdu {
	uint32_t ts;
} decoded_sdu;

RING_BUF_DECLARE(usb_out_ring_buf, USB_OUT_RING_BUF_SIZE);
K_MEM_SLAB_DEFINE_STATIC(usb_out_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY),
RING_BUF_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE);
K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY),
			 USB_ENQUEUE_COUNT, UDC_BUF_ALIGN);

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

	err = k_mem_slab_alloc(&usb_out_buf_pool, &pcm_buf, K_NO_WAIT);
	err = k_mem_slab_alloc(&usb_in_buf_pool, &pcm_buf, K_NO_WAIT);
	if (err != 0) {
		LOG_WRN("Could not allocate pcm_buf: %d", err);
		return;
	}

	/* This may fail without causing issues since usb_audio_data is 0-initialized */
	size = ring_buf_get(&usb_out_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
	size = ring_buf_get(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
	if (size != USB_STEREO_FRAME_SIZE) {
		/* If we could not fill the buffer, zero-fill the rest (possibly all) */
		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)
			LOG_ERR("Failed to send USB audio: %d (%zu)", err, cnt);
		}

		k_mem_slab_free(&usb_out_buf_pool, pcm_buf);
		k_mem_slab_free(&usb_in_buf_pool, pcm_buf);
	}
}

static void usb_buf_release_cb(const struct device *dev, uint8_t terminal, void *buf,
			       void *user_data)
{
	k_mem_slab_free(&usb_out_buf_pool, buf);
	k_mem_slab_free(&usb_in_buf_pool, buf);
}

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

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

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

#if defined(CONFIG_BT_AUDIO_TX)
#define USB_OUT_RING_BUF_SIZE (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)

/* Allocate 3: 1 for USB to receive data to and 2 additional buffers to prevent out of memory
 * errors when USB host decides to perform rapid terminal enable/disable cycles.
 */
K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, USB_STEREO_FRAME_SIZE, 3, UDC_BUF_ALIGN);
K_MEM_SLAB_DEFINE_STATIC(usb_out_buf_pool, USB_STEREO_FRAME_SIZE, 3, UDC_BUF_ALIGN);

BUILD_ASSERT((USB_IN_RING_BUF_SIZE % USB_MONO_FRAME_SIZE) == 0);
static int16_t usb_in_left_ring_buffer[USB_IN_RING_BUF_SIZE];
static int16_t usb_in_right_ring_buffer[USB_IN_RING_BUF_SIZE];
static int16_t usb_out_left_ring_buffer[USB_OUT_RING_BUF_SIZE];
static int16_t usb_out_right_ring_buffer[USB_OUT_RING_BUF_SIZE];
static size_t write_index; /* Points to the oldest/uninitialized data */

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

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

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

	if (!out_terminal_enabled || buf == NULL || size == 0U) {
		k_mem_slab_free(&usb_in_buf_pool, buf);
		k_mem_slab_free(&usb_out_buf_pool, buf);
		return;
	}

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

	write_index += USB_SAMPLE_CNT;

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

	k_mem_slab_free(&usb_in_buf_pool, buf);
	k_mem_slab_free(&usb_out_buf_pool, buf);
}

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)
		buffer_cnt = write_index - read_idx;
	} else {
		/* Handle the case where the read spans across the end of the buffer */
		buffer_cnt = write_index + (USB_IN_RING_BUF_SIZE - read_idx);
		buffer_cnt = write_index + (USB_OUT_RING_BUF_SIZE - read_idx);
	}

	if (buffer_cnt < retrieve_cnt) {
		/* Not enough for a frame yet */
		if (!failed_last_time) {
			LOG_WRN("Ring buffer (%u/%u) does not contain enough for an entire SDU %u",
				buffer_cnt, USB_IN_RING_BUF_SIZE, retrieve_cnt);
				buffer_cnt, USB_OUT_RING_BUF_SIZE, retrieve_cnt);
		}

		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
{
	size_t new_idx;

	if (idx >= USB_IN_RING_BUF_SIZE) {
	if (idx >= USB_OUT_RING_BUF_SIZE) {
		LOG_ERR("Invalid idx %zu", idx);

		return 0;
	}

	if ((idx + cnt) < USB_IN_RING_BUF_SIZE) {
	if ((idx + cnt) < USB_OUT_RING_BUF_SIZE) {
		/* Simply copy of the data and increment the index*/
		memcpy(dest, &src[idx], cnt * USB_BYTES_PER_SAMPLE);
		new_idx = idx + cnt;
	} else {
		/* Handle wrapping */
		const size_t first_read_cnt = USB_IN_RING_BUF_SIZE - idx;
		const size_t first_read_cnt = USB_OUT_RING_BUF_SIZE - idx;
		const size_t second_read_cnt = cnt - first_read_cnt;

		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

	if (is_left || is_mono) {
		sh_stream->tx.left_read_idx = usb_ring_buf_get(
			buffer, usb_in_left_ring_buffer, sh_stream->tx.left_read_idx, read_cnt);
			buffer, usb_out_left_ring_buffer, sh_stream->tx.left_read_idx, read_cnt);
	} else if (is_right) {
		sh_stream->tx.right_read_idx = usb_ring_buf_get(
			buffer, usb_in_right_ring_buffer, sh_stream->tx.right_read_idx, read_cnt);
			buffer, usb_out_right_ring_buffer, sh_stream->tx.right_read_idx, read_cnt);
	}
}
#endif /* CONFIG_BT_AUDIO_TX */