1 /* @file
2  * @brief Internal APIs for Audio Endpoint handling
3 
4  * Copyright (c) 2020 Intel Corporation
5  * Copyright (c) 2022-2023 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/bluetooth/audio/bap.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/types.h>
13 
14 #include "ascs_internal.h"
15 #include "bap_stream.h"
16 
17 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT)
18 #define UNICAST_GROUP_CNT	 CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_COUNT
19 #define UNICAST_GROUP_STREAM_CNT CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT
20 #else /* !CONFIG_BT_BAP_UNICAST_CLIENT */
21 #define UNICAST_GROUP_CNT 0
22 #define UNICAST_GROUP_STREAM_CNT 0
23 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT */
24 #if defined(CONFIG_BT_BAP_BROADCAST_SOURCE)
25 #define BROADCAST_STREAM_CNT CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT
26 #else /* !CONFIG_BT_BAP_BROADCAST_SOURCE */
27 #define BROADCAST_STREAM_CNT 0
28 #endif /* CONFIG_BT_BAP_BROADCAST_SOURCE */
29 
30 /* Temp struct declarations to handle circular dependencies */
31 struct bt_bap_unicast_group;
32 struct bt_bap_broadcast_source;
33 struct bt_bap_broadcast_sink;
34 
35 struct bt_bap_ep {
36 	uint8_t  dir;
37 	uint8_t  cig_id;
38 	uint8_t  cis_id;
39 	struct bt_ascs_ase_status status;
40 	struct bt_bap_stream *stream;
41 	struct bt_codec codec;
42 	struct bt_codec_qos qos;
43 	struct bt_codec_qos_pref qos_pref;
44 	struct bt_bap_iso *iso;
45 
46 	/* Used by the unicast server and client */
47 	bool receiver_ready;
48 
49 	/* TODO: Create a union to reduce memory usage */
50 	struct bt_bap_unicast_group *unicast_group;
51 	struct bt_bap_broadcast_source *broadcast_source;
52 	struct bt_bap_broadcast_sink *broadcast_sink;
53 };
54 
55 struct bt_bap_unicast_group {
56 	uint8_t index;
57 	bool allocated;
58 	/* QoS used to create the CIG */
59 	const struct bt_codec_qos *qos;
60 	struct bt_iso_cig *cig;
61 	/* The ISO API for CIG creation requires an array of pointers to ISO channels */
62 	struct bt_iso_chan *cis[UNICAST_GROUP_STREAM_CNT];
63 	sys_slist_t streams;
64 };
65 
66 struct bt_audio_broadcast_stream_data {
67 #if defined(CONFIG_BT_CODEC_MAX_DATA_COUNT)
68 	/** Codec Specific Data count */
69 	size_t   data_count;
70 	/** Codec Specific Data */
71 	struct bt_codec_data data[CONFIG_BT_CODEC_MAX_DATA_COUNT];
72 #endif /* CONFIG_BT_CODEC_MAX_DATA_COUNT */
73 };
74 
75 struct bt_bap_broadcast_source {
76 	uint8_t stream_count;
77 	uint8_t packing;
78 	bool encryption;
79 	uint32_t broadcast_id; /* 24 bit */
80 
81 	struct bt_iso_big *big;
82 	struct bt_codec_qos *qos;
83 
84 	/* The codec specific configured data for each stream in the subgroup */
85 	struct bt_audio_broadcast_stream_data stream_data[BROADCAST_STREAM_CNT];
86 
87 	uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE];
88 
89 	/* The subgroups containing the streams used to create the broadcast source */
90 	sys_slist_t subgroups;
91 };
92 
93 enum bt_bap_broadcast_sink_flag {
94 	/** Sink has been initialized */
95 	BT_BAP_BROADCAST_SINK_FLAG_INITIALIZED,
96 
97 	/** BIGInfo has been received */
98 	BT_BAP_BROADCAST_SINK_FLAG_BIGINFO_RECEIVED,
99 
100 	/** Periodic Advertising is syncing */
101 	BT_BAP_BROADCAST_SINK_FLAG_SYNCING,
102 
103 	/** Broadcast sink instance is scanning */
104 	BT_BAP_BROADCAST_SINK_FLAG_SCANNING,
105 
106 	/** The BIG is encrypted */
107 	BT_BAP_BROADCAST_SINK_FLAG_BIG_ENCRYPTED,
108 
109 	/** The Scan Delegator Source ID is valid */
110 	BT_BAP_BROADCAST_SINK_FLAG_SRC_ID_VALID,
111 
112 	/** Total number of flags */
113 	BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS,
114 };
115 
116 struct bt_bap_broadcast_sink {
117 	uint8_t index; /* index of broadcast_snks array */
118 	uint8_t stream_count;
119 	uint8_t bass_src_id;
120 	uint16_t iso_interval;
121 	uint16_t biginfo_num_bis;
122 	uint32_t broadcast_id; /* 24 bit */
123 	struct bt_bap_base base;
124 	struct bt_codec_qos codec_qos;
125 	struct bt_le_per_adv_sync *pa_sync;
126 	struct bt_iso_big *big;
127 	struct bt_iso_chan *bis[BROADCAST_SNK_STREAM_CNT];
128 	const struct bt_bap_scan_delegator_recv_state *recv_state;
129 	/* The streams used to create the broadcast sink */
130 	sys_slist_t streams;
131 
132 	/** Flags */
133 	ATOMIC_DEFINE(flags, BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS);
134 };
135 
bt_bap_ep_state_str(uint8_t state)136 static inline const char *bt_bap_ep_state_str(uint8_t state)
137 {
138 	switch (state) {
139 	case BT_BAP_EP_STATE_IDLE:
140 		return "idle";
141 	case BT_BAP_EP_STATE_CODEC_CONFIGURED:
142 		return "codec-configured";
143 	case BT_BAP_EP_STATE_QOS_CONFIGURED:
144 		return "qos-configured";
145 	case BT_BAP_EP_STATE_ENABLING:
146 		return "enabling";
147 	case BT_BAP_EP_STATE_STREAMING:
148 		return "streaming";
149 	case BT_BAP_EP_STATE_DISABLING:
150 		return "disabling";
151 	case BT_BAP_EP_STATE_RELEASING:
152 		return "releasing";
153 	default:
154 		return "unknown";
155 	}
156 }
157 
158 bool bt_bap_ep_is_broadcast_snk(const struct bt_bap_ep *ep);
159 bool bt_bap_ep_is_broadcast_src(const struct bt_bap_ep *ep);
160 bool bt_bap_ep_is_unicast_client(const struct bt_bap_ep *ep);
161