1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdbool.h>
8 #include <string.h>
9 
10 #include <zephyr/bluetooth/audio/bap.h>
11 #include <zephyr/bluetooth/iso.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/logging/log_core.h>
14 #include <zephyr/net_buf.h>
15 
16 #include "common.h"
17 
18 LOG_MODULE_REGISTER(bap_stream_rx, LOG_LEVEL_INF);
19 
log_stream_rx(struct bt_bap_stream * stream,const struct bt_iso_recv_info * info,struct net_buf * buf)20 static void log_stream_rx(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
21 			  struct net_buf *buf)
22 {
23 	struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
24 
25 	LOG_INF("[%zu|%zu]: Incoming audio on stream %p len %u, flags 0x%02X, seq_num %u and ts %u",
26 		test_stream->valid_rx_cnt, test_stream->rx_cnt, stream, buf->len, info->flags,
27 		info->seq_num, info->ts);
28 }
29 
bap_stream_rx_recv_cb(struct bt_bap_stream * stream,const struct bt_iso_recv_info * info,struct net_buf * buf)30 void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
31 			   struct net_buf *buf)
32 {
33 	struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
34 
35 	test_stream->rx_cnt++;
36 	if ((info->flags & BT_ISO_FLAGS_VALID) != 0) {
37 		if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
38 			test_stream->valid_rx_cnt++;
39 
40 			if (test_stream->valid_rx_cnt >= MIN_SEND_COUNT) {
41 				/* We set the flag is just one stream has received the expected */
42 				SET_FLAG(flag_audio_received);
43 			}
44 		} else {
45 			log_stream_rx(stream, info, buf);
46 			FAIL("Unexpected data received\n");
47 		}
48 	}
49 
50 	if ((test_stream->rx_cnt % 50U) == 0U) {
51 		log_stream_rx(stream, info, buf);
52 	}
53 
54 	if (info->ts == test_stream->last_info.ts) {
55 		log_stream_rx(stream, info, buf);
56 		if (test_stream->valid_rx_cnt > 1U) {
57 			FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
58 		}
59 	}
60 
61 	if (info->seq_num == test_stream->last_info.seq_num) {
62 		log_stream_rx(stream, info, buf);
63 		if (test_stream->valid_rx_cnt > 1U) {
64 			FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
65 		}
66 	}
67 
68 	if (info->flags & BT_ISO_FLAGS_ERROR) {
69 		/* Fail the test if we have not received what we expected */
70 		log_stream_rx(stream, info, buf);
71 		if (test_stream->valid_rx_cnt > 1U && !TEST_FLAG(flag_audio_received)) {
72 			FAIL("ISO receive error\n");
73 		}
74 	}
75 
76 	if (info->flags & BT_ISO_FLAGS_LOST) {
77 		log_stream_rx(stream, info, buf);
78 		if (test_stream->valid_rx_cnt > 1U) {
79 			FAIL("ISO receive lost\n");
80 		}
81 	}
82 }
83 
bap_stream_rx_can_recv(const struct bt_bap_stream * stream)84 bool bap_stream_rx_can_recv(const struct bt_bap_stream *stream)
85 {
86 	struct bt_bap_ep_info info;
87 	int err;
88 
89 	if (stream == NULL || stream->ep == NULL) {
90 		return false;
91 	}
92 
93 	err = bt_bap_ep_get_info(stream->ep, &info);
94 	if (err != 0) {
95 		return false;
96 	}
97 
98 	return info.can_recv;
99 }
100