|
26 | 26 | #include <zephyr/toolchain.h> |
27 | 27 |
|
28 | 28 | #include "bap_common.h" |
| 29 | +#include "bap_stream_tx.h" |
29 | 30 | #include "bstests.h" |
30 | 31 | #include "common.h" |
31 | 32 |
|
|
37 | 38 | #if defined(CONFIG_BT_BAP_BROADCAST_SOURCE) |
38 | 39 | CREATE_FLAG(flag_source_started); |
39 | 40 |
|
40 | | -/* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that |
41 | | - * the controller is never idle |
42 | | - */ |
43 | | -#define BROADCAST_ENQUEUE_COUNT 2U |
44 | | -#define TOTAL_BUF_NEEDED (BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT) |
45 | | - |
46 | | -BUILD_ASSERT(CONFIG_BT_ISO_TX_BUF_COUNT >= TOTAL_BUF_NEEDED, |
47 | | - "CONFIG_BT_ISO_TX_BUF_COUNT should be at least " |
48 | | - "BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT"); |
49 | | - |
50 | | -NET_BUF_POOL_FIXED_DEFINE(tx_pool, |
51 | | - TOTAL_BUF_NEEDED, |
52 | | - BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), |
53 | | - CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL); |
54 | | - |
55 | | -extern enum bst_result_t bst_result; |
56 | 41 | static struct audio_test_stream broadcast_source_streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT]; |
57 | 42 | static struct bt_bap_lc3_preset preset_16_2_1 = BT_BAP_LC3_BROADCAST_PRESET_16_2_1( |
58 | 43 | BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED); |
@@ -216,60 +201,36 @@ static void stream_started_cb(struct bt_bap_stream *stream) |
216 | 201 | return; |
217 | 202 | } |
218 | 203 |
|
| 204 | + err = bap_stream_tx_register(stream); |
| 205 | + if (err != 0) { |
| 206 | + FAIL("Failed to register stream %p for TX: %d\n", stream, err); |
| 207 | + return; |
| 208 | + } |
| 209 | + |
219 | 210 | printk("Stream %p started\n", stream); |
220 | 211 | validate_stream_codec_cfg(stream); |
221 | 212 | k_sem_give(&sem_stream_started); |
222 | 213 | } |
223 | 214 |
|
224 | 215 | static void steam_stopped_cb(struct bt_bap_stream *stream, uint8_t reason) |
225 | 216 | { |
226 | | - printk("Stream %p stopped with reason 0x%02X\n", stream, reason); |
227 | | - k_sem_give(&sem_stream_stopped); |
228 | | -} |
229 | | - |
230 | | -static void stream_sent_cb(struct bt_bap_stream *stream) |
231 | | -{ |
232 | | - struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); |
233 | | - struct net_buf *buf; |
234 | | - int ret; |
235 | | - |
236 | | - if (!test_stream->tx_active) { |
237 | | - return; |
238 | | - } |
239 | | - |
240 | | - if ((test_stream->tx_cnt % 100U) == 0U) { |
241 | | - printk("Sent with seq_num %u\n", test_stream->seq_num); |
242 | | - } |
243 | | - |
244 | | - buf = net_buf_alloc(&tx_pool, K_FOREVER); |
245 | | - if (buf == NULL) { |
246 | | - printk("Could not allocate buffer when sending on %p\n", |
247 | | - stream); |
248 | | - return; |
249 | | - } |
250 | | - |
251 | | - net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE); |
252 | | - net_buf_add_mem(buf, mock_iso_data, test_stream->tx_sdu_size); |
253 | | - ret = bt_bap_stream_send(stream, buf, test_stream->seq_num++); |
254 | | - if (ret < 0) { |
255 | | - /* This will end broadcasting on this stream. */ |
256 | | - net_buf_unref(buf); |
| 217 | + int err; |
257 | 218 |
|
258 | | - /* Only fail if tx is active (may fail if we are disabling the stream) */ |
259 | | - if (test_stream->tx_active) { |
260 | | - FAIL("Unable to broadcast data on %p: %d\n", stream, ret); |
261 | | - } |
| 219 | + printk("Stream %p stopped with reason 0x%02X\n", stream, reason); |
262 | 220 |
|
| 221 | + err = bap_stream_tx_unregister(stream); |
| 222 | + if (err != 0) { |
| 223 | + FAIL("Failed to unregister stream %p for TX: %d\n", stream, err); |
263 | 224 | return; |
264 | 225 | } |
265 | 226 |
|
266 | | - test_stream->tx_cnt++; |
| 227 | + k_sem_give(&sem_stream_stopped); |
267 | 228 | } |
268 | 229 |
|
269 | 230 | static struct bt_bap_stream_ops stream_ops = { |
270 | 231 | .started = stream_started_cb, |
271 | 232 | .stopped = steam_stopped_cb, |
272 | | - .sent = stream_sent_cb, |
| 233 | + .sent = bap_stream_tx_sent_cb, |
273 | 234 | }; |
274 | 235 |
|
275 | 236 | static void source_started_cb(struct bt_bap_broadcast_source *source) |
@@ -525,10 +486,6 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source) |
525 | 486 |
|
526 | 487 | printk("Stopping broadcast source\n"); |
527 | 488 |
|
528 | | - for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) { |
529 | | - broadcast_source_streams[i].tx_active = false; |
530 | | - } |
531 | | - |
532 | 489 | err = bt_bap_broadcast_source_stop(source); |
533 | 490 | if (err != 0) { |
534 | 491 | FAIL("Unable to stop broadcast source: %d\n", err); |
@@ -597,6 +554,7 @@ static void init(void) |
597 | 554 | } |
598 | 555 |
|
599 | 556 | printk("Bluetooth initialized\n"); |
| 557 | + bap_stream_tx_init(); |
600 | 558 |
|
601 | 559 | err = bt_bap_broadcast_source_register_cb(&broadcast_source_cb); |
602 | 560 | if (err != 0) { |
@@ -629,17 +587,6 @@ static void test_main(void) |
629 | 587 |
|
630 | 588 | test_broadcast_source_start(source, adv); |
631 | 589 |
|
632 | | - /* Initialize sending */ |
633 | | - printk("Sending data\n"); |
634 | | - for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) { |
635 | | - for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) { |
636 | | - struct audio_test_stream *test_stream = &broadcast_source_streams[i]; |
637 | | - |
638 | | - test_stream->tx_active = true; |
639 | | - stream_sent_cb(&test_stream->stream.bap_stream); |
640 | | - } |
641 | | - } |
642 | | - |
643 | 590 | /* Wait for other devices to have received what they wanted */ |
644 | 591 | backchannel_sync_wait_any(); |
645 | 592 |
|
@@ -701,17 +648,6 @@ static void test_main_encrypted(void) |
701 | 648 |
|
702 | 649 | test_broadcast_source_start(source, adv); |
703 | 650 |
|
704 | | - /* Initialize sending */ |
705 | | - printk("Sending data\n"); |
706 | | - for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) { |
707 | | - for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) { |
708 | | - struct audio_test_stream *test_stream = &broadcast_source_streams[i]; |
709 | | - |
710 | | - test_stream->tx_active = true; |
711 | | - stream_sent_cb(&test_stream->stream.bap_stream); |
712 | | - } |
713 | | - } |
714 | | - |
715 | 651 | /* Wait for other devices to have received data */ |
716 | 652 | backchannel_sync_wait_any(); |
717 | 653 |
|
|
0 commit comments