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