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_audio_codec_cfg codec_cfg;
42 	struct bt_audio_codec_qos qos;
43 	struct bt_audio_codec_qos_pref qos_pref;
44 	struct bt_bap_iso *iso;
45 
46 	/* unicast stopped reason */
47 	uint8_t reason;
48 
49 	/* Used by the unicast server and client */
50 	bool receiver_ready;
51 
52 	/* TODO: Create a union to reduce memory usage */
53 	struct bt_bap_unicast_group *unicast_group;
54 	struct bt_bap_broadcast_source *broadcast_source;
55 	struct bt_bap_broadcast_sink *broadcast_sink;
56 };
57 
58 struct bt_bap_unicast_group {
59 	uint8_t index;
60 	bool allocated;
61 	/* QoS used to create the CIG */
62 	const struct bt_audio_codec_qos *qos;
63 	struct bt_iso_cig *cig;
64 	/* The ISO API for CIG creation requires an array of pointers to ISO channels */
65 	struct bt_iso_chan *cis[UNICAST_GROUP_STREAM_CNT];
66 	sys_slist_t streams;
67 };
68 
69 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
70 struct bt_audio_broadcast_stream_data {
71 	/** Codec Specific Data len */
72 	size_t data_len;
73 	/** Codec Specific Data */
74 	uint8_t data[CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE];
75 };
76 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
77 
78 struct bt_bap_broadcast_source {
79 	uint8_t stream_count;
80 	uint8_t packing;
81 	bool encryption;
82 	uint32_t broadcast_id; /* 24 bit */
83 
84 	struct bt_iso_big *big;
85 	struct bt_audio_codec_qos *qos;
86 
87 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
88 	/* The codec specific configured data for each stream in the subgroup */
89 	struct bt_audio_broadcast_stream_data stream_data[BROADCAST_STREAM_CNT];
90 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
91 
92 	uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE];
93 
94 	/* The subgroups containing the streams used to create the broadcast source */
95 	sys_slist_t subgroups;
96 };
97 
98 enum bt_bap_broadcast_sink_flag {
99 	/** Sink has been initialized */
100 	BT_BAP_BROADCAST_SINK_FLAG_INITIALIZED,
101 
102 	/** BIGInfo has been received */
103 	BT_BAP_BROADCAST_SINK_FLAG_BIGINFO_RECEIVED,
104 
105 	/** Periodic Advertising is syncing */
106 	BT_BAP_BROADCAST_SINK_FLAG_SYNCING,
107 
108 	/** Broadcast sink instance is scanning */
109 	BT_BAP_BROADCAST_SINK_FLAG_SCANNING,
110 
111 	/** The BIG is encrypted */
112 	BT_BAP_BROADCAST_SINK_FLAG_BIG_ENCRYPTED,
113 
114 	/** The Scan Delegator Source ID is valid */
115 	BT_BAP_BROADCAST_SINK_FLAG_SRC_ID_VALID,
116 
117 	/** Total number of flags */
118 	BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS,
119 };
120 
121 struct bt_bap_broadcast_sink {
122 	uint8_t index; /* index of broadcast_snks array */
123 	uint8_t stream_count;
124 	uint8_t bass_src_id;
125 	uint16_t iso_interval;
126 	uint16_t biginfo_num_bis;
127 	uint32_t broadcast_id; /* 24 bit */
128 	uint32_t indexes_bitfield;
129 	struct bt_bap_base base;
130 	struct bt_audio_codec_qos codec_qos;
131 	struct bt_le_per_adv_sync *pa_sync;
132 	struct bt_iso_big *big;
133 	struct bt_iso_chan *bis[BROADCAST_SNK_STREAM_CNT];
134 	const struct bt_bap_scan_delegator_recv_state *recv_state;
135 	/* The streams used to create the broadcast sink */
136 	sys_slist_t streams;
137 
138 	/** Flags */
139 	ATOMIC_DEFINE(flags, BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS);
140 };
141 
bt_bap_ep_state_str(uint8_t state)142 static inline const char *bt_bap_ep_state_str(uint8_t state)
143 {
144 	switch (state) {
145 	case BT_BAP_EP_STATE_IDLE:
146 		return "idle";
147 	case BT_BAP_EP_STATE_CODEC_CONFIGURED:
148 		return "codec-configured";
149 	case BT_BAP_EP_STATE_QOS_CONFIGURED:
150 		return "qos-configured";
151 	case BT_BAP_EP_STATE_ENABLING:
152 		return "enabling";
153 	case BT_BAP_EP_STATE_STREAMING:
154 		return "streaming";
155 	case BT_BAP_EP_STATE_DISABLING:
156 		return "disabling";
157 	case BT_BAP_EP_STATE_RELEASING:
158 		return "releasing";
159 	default:
160 		return "unknown";
161 	}
162 }
163 
164 bool bt_bap_ep_is_broadcast_snk(const struct bt_bap_ep *ep);
165 bool bt_bap_ep_is_broadcast_src(const struct bt_bap_ep *ep);
166 bool bt_bap_ep_is_unicast_client(const struct bt_bap_ep *ep);
167