1 /*
2  * avdtp_internal.h - avdtp handling
3 
4  * Copyright (c) 2015-2016 Intel Corporation
5  * Copyright 2021,2024 NXP
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/bluetooth/classic/avdtp.h>
11 
12 /* @brief A2DP ROLE's */
13 #define A2DP_SRC_ROLE 0x00
14 #define A2DP_SNK_ROLE 0x01
15 
16 /* @brief AVDTP Role */
17 #define BT_AVDTP_INT 0x00
18 #define BT_AVDTP_ACP 0x01
19 
20 #define BT_L2CAP_PSM_AVDTP 0x0019
21 
22 /* AVDTP SIGNAL HEADER - Packet Type*/
23 #define BT_AVDTP_PACKET_TYPE_SINGLE   0x00
24 #define BT_AVDTP_PACKET_TYPE_START    0x01
25 #define BT_AVDTP_PACKET_TYPE_CONTINUE 0x02
26 #define BT_AVDTP_PACKET_TYPE_END      0x03
27 
28 /* AVDTP SIGNAL HEADER - MESSAGE TYPE */
29 #define BT_AVDTP_CMD        0x00
30 #define BT_AVDTP_GEN_REJECT 0x01
31 #define BT_AVDTP_ACCEPT     0x02
32 #define BT_AVDTP_REJECT     0x03
33 
34 /* @brief AVDTP SIGNAL HEADER - Signal Identifier */
35 #define BT_AVDTP_DISCOVER             0x01
36 #define BT_AVDTP_GET_CAPABILITIES     0x02
37 #define BT_AVDTP_SET_CONFIGURATION    0x03
38 #define BT_AVDTP_GET_CONFIGURATION    0x04
39 #define BT_AVDTP_RECONFIGURE          0x05
40 #define BT_AVDTP_OPEN                 0x06
41 #define BT_AVDTP_START                0x07
42 #define BT_AVDTP_CLOSE                0x08
43 #define BT_AVDTP_SUSPEND              0x09
44 #define BT_AVDTP_ABORT                0x0a
45 #define BT_AVDTP_SECURITY_CONTROL     0x0b
46 #define BT_AVDTP_GET_ALL_CAPABILITIES 0x0c
47 #define BT_AVDTP_DELAYREPORT          0x0d
48 
49 /* @brief AVDTP STREAM STATE */
50 #define BT_AVDTP_STREAM_STATE_IDLE        0x01
51 #define BT_AVDTP_STREAM_STATE_CONFIGURED  0x02
52 #define BT_AVDTP_STREAM_STATE_OPEN        0x03
53 #define BT_AVDTP_STREAM_STATE_STREAMING   0x04
54 #define BT_AVDTP_STREAM_STATE_CLOSING     0x05
55 
56 /* @brief AVDTP Media TYPE */
57 #define BT_AVDTP_SERVICE_CAT_MEDIA_TRANSPORT    0x01
58 #define BT_AVDTP_SERVICE_CAT_REPORTING          0x02
59 #define BT_AVDTP_SERVICE_CAT_RECOVERY           0x03
60 #define BT_AVDTP_SERVICE_CAT_CONTENT_PROTECTION 0x04
61 #define BT_AVDTP_SERVICE_CAT_HDR_COMPRESSION    0x05
62 #define BT_AVDTP_SERVICE_CAT_MULTIPLEXING       0x06
63 #define BT_AVDTP_SERVICE_CAT_MEDIA_CODEC        0x07
64 #define BT_AVDTP_SERVICE_CAT_DELAYREPORTING     0x08
65 
66 /* AVDTP Error Codes */
67 #define BT_AVDTP_SUCCESS                        0x00
68 #define BT_AVDTP_ERR_BAD_HDR_FORMAT             0x01
69 #define BT_AVDTP_ERR_BAD_LENGTH                 0x11
70 #define BT_AVDTP_ERR_BAD_ACP_SEID               0x12
71 #define BT_AVDTP_ERR_SEP_IN_USE                 0x13
72 #define BT_AVDTP_ERR_SEP_NOT_IN_USE             0x14
73 #define BT_AVDTP_ERR_BAD_SERV_CATEGORY          0x17
74 #define BT_AVDTP_ERR_BAD_PAYLOAD_FORMAT         0x18
75 #define BT_AVDTP_ERR_NOT_SUPPORTED_COMMAND      0x19
76 #define BT_AVDTP_ERR_INVALID_CAPABILITIES       0x1a
77 #define BT_AVDTP_ERR_BAD_RECOVERY_TYPE          0x22
78 #define BT_AVDTP_ERR_BAD_MEDIA_TRANSPORT_FORMAT 0x23
79 #define BT_AVDTP_ERR_BAD_RECOVERY_FORMAT        0x25
80 #define BT_AVDTP_ERR_BAD_ROHC_FORMAT            0x26
81 #define BT_AVDTP_ERR_BAD_CP_FORMAT              0x27
82 #define BT_AVDTP_ERR_BAD_MULTIPLEXING_FORMAT    0x28
83 #define BT_AVDTP_ERR_UNSUPPORTED_CONFIGURAION   0x29
84 #define BT_AVDTP_ERR_BAD_STATE                  0x31
85 
86 #define BT_AVDTP_MIN_SEID 0x01
87 #define BT_AVDTP_MAX_SEID 0x3E
88 
89 struct bt_avdtp;
90 struct bt_avdtp_req;
91 struct bt_avdtp_sep_info;
92 
93 /** @brief AVDTP SEID Information AVDTP_SPEC V13 Table 8.8 */
94 struct bt_avdtp_sep_data {
95 #ifdef CONFIG_LITTLE_ENDIAN
96 	uint8_t rfa0:1;
97 	uint8_t inuse:1;
98 	uint8_t id:6;
99 	uint8_t rfa1:3;
100 	uint8_t tsep:1;
101 	uint8_t media_type:4;
102 #else
103 	uint8_t id:6;
104 	uint8_t inuse:1;
105 	uint8_t rfa0:1;
106 	uint8_t media_type:4;
107 	uint8_t tsep:1;
108 	uint8_t rfa1:3;
109 #endif
110 } __packed;
111 
112 typedef int (*bt_avdtp_func_t)(struct bt_avdtp_req *req);
113 
114 struct bt_avdtp_req {
115 	uint8_t sig;
116 	uint8_t tid;
117 	bt_avdtp_func_t func;
118 };
119 
120 struct bt_avdtp_single_sig_hdr {
121 	uint8_t hdr;
122 	uint8_t signal_id;
123 } __packed;
124 
125 struct bt_avdtp_media_hdr {
126 #ifdef CONFIG_LITTLE_ENDIAN
127 	uint8_t CSRC_count:4;
128 	uint8_t header_extension:1;
129 	uint8_t padding:1;
130 	uint8_t RTP_version:2;
131 	uint8_t playload_type:7;
132 	uint8_t marker:1;
133 #else
134 	uint8_t RTP_version:2;
135 	uint8_t padding:1;
136 	uint8_t header_extension:1;
137 	uint8_t CSRC_count:4;
138 	uint8_t marker:1;
139 	uint8_t playload_type:7;
140 #endif
141 	uint16_t sequence_number;
142 	uint32_t time_stamp;
143 	uint32_t synchronization_source;
144 } __packed;
145 
146 struct bt_avdtp_discover_params {
147 	struct bt_avdtp_req req;
148 	uint8_t status;
149 	struct net_buf *buf;
150 };
151 
152 struct bt_avdtp_get_capabilities_params {
153 	struct bt_avdtp_req req;
154 	uint8_t status;
155 	uint8_t stream_endpoint_id;
156 	struct net_buf *buf;
157 };
158 
159 struct bt_avdtp_set_configuration_params {
160 	struct bt_avdtp_req req;
161 	struct bt_avdtp_sep *sep;
162 	uint8_t status;
163 	uint8_t acp_stream_ep_id;
164 	uint8_t int_stream_endpoint_id;
165 	uint8_t media_type;
166 	uint8_t media_codec_type;
167 	uint8_t codec_specific_ie_len;
168 	uint8_t *codec_specific_ie;
169 };
170 
171 struct bt_avdtp_open_params {
172 	struct bt_avdtp_req req;
173 	struct bt_avdtp_sep *sep;
174 	uint8_t status;
175 	uint8_t acp_stream_ep_id;
176 };
177 
178 struct bt_avdtp_start_params {
179 	struct bt_avdtp_req req;
180 	struct bt_avdtp_sep *sep;
181 	uint8_t status;
182 	uint8_t acp_stream_ep_id;
183 };
184 
185 struct bt_avdtp_ops_cb {
186 	void (*connected)(struct bt_avdtp *session);
187 
188 	void (*disconnected)(struct bt_avdtp *session);
189 
190 	struct net_buf *(*alloc_buf)(struct bt_avdtp *session);
191 
192 	int (*discovery_ind)(struct bt_avdtp *session, uint8_t *errcode);
193 
194 	int (*get_capabilities_ind)(struct bt_avdtp *session,
195 		struct bt_avdtp_sep *sep, struct net_buf *rsp_buf, uint8_t *errcode);
196 
197 	int (*set_configuration_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
198 		uint8_t int_seid, struct net_buf *buf, uint8_t *errcode);
199 
200 	int (*open_ind)(struct bt_avdtp *session,
201 		struct bt_avdtp_sep *sep, uint8_t *errcode);
202 
203 	int (*close_ind)(struct bt_avdtp *session,
204 		struct bt_avdtp_sep *sep, uint8_t *errcode);
205 
206 	int (*start_ind)(struct bt_avdtp *session,
207 		struct bt_avdtp_sep *sep, uint8_t *errcode);
208 
209 	int (*suspend_ind)(struct bt_avdtp *session,
210 		struct bt_avdtp_sep *sep, uint8_t *errcode);
211 
212 	int (*abort_ind)(struct bt_avdtp *session,
213 		struct bt_avdtp_sep *sep, uint8_t *errcode);
214 };
215 
216 /** @brief Global AVDTP session structure. */
217 struct bt_avdtp {
218 	struct bt_l2cap_br_chan br_chan;
219 	struct bt_avdtp_req *req;
220 	const struct bt_avdtp_ops_cb *ops;
221 	struct bt_avdtp_sep *current_sep;
222 	struct k_work_delayable timeout_work;
223 	uint8_t signalling_l2cap_connected;
224 };
225 
226 struct bt_avdtp_event_cb {
227 	int (*accept)(struct bt_conn *conn, struct bt_avdtp **session);
228 };
229 
230 /* Initialize AVDTP layer*/
231 int bt_avdtp_init(void);
232 
233 /* Application register with AVDTP layer */
234 int bt_avdtp_register(struct bt_avdtp_event_cb *cb);
235 
236 /* AVDTP connect */
237 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session);
238 
239 /* AVDTP disconnect */
240 int bt_avdtp_disconnect(struct bt_avdtp *session);
241 
242 /* AVDTP SEP register function */
243 int bt_avdtp_register_sep(uint8_t media_type, uint8_t role,
244 				struct bt_avdtp_sep *sep);
245 
246 /* AVDTP Discover Request */
247 int bt_avdtp_discover(struct bt_avdtp *session,
248 		      struct bt_avdtp_discover_params *param);
249 
250 /* Parse the sep of discovered result */
251 int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info);
252 
253 /* AVDTP Get Capabilities */
254 int bt_avdtp_get_capabilities(struct bt_avdtp *session,
255 		      struct bt_avdtp_get_capabilities_params *param);
256 
257 /* Parse the codec type of capabilities */
258 int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
259 				uint8_t **codec_info_element, uint16_t *codec_info_element_len);
260 
261 /* AVDTP Set Configuration */
262 int bt_avdtp_set_configuration(struct bt_avdtp *session,
263 		      struct bt_avdtp_set_configuration_params *param);
264 
265 /* AVDTP reconfigure */
266 int bt_avdtp_reconfigure(struct bt_avdtp *session,
267 		      struct bt_avdtp_set_configuration_params *param);
268 
269 /* AVDTP OPEN */
270 int bt_avdtp_open(struct bt_avdtp *session,
271 		      struct bt_avdtp_open_params *param);
272 
273 /* AVDTP START */
274 int bt_avdtp_start(struct bt_avdtp *session,
275 		      struct bt_avdtp_start_params *param);
276 
277 /* AVDTP send data */
278 int bt_avdtp_send_media_data(struct bt_avdtp_sep *sep, struct net_buf *buf);
279 
280 /* get media l2cap connection MTU */
281 uint32_t bt_avdtp_get_media_mtu(struct bt_avdtp_sep *sep);
282