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 #define BT_AVDTP_RTP_VERSION 2
90 
91 struct bt_avdtp;
92 struct bt_avdtp_req;
93 struct bt_avdtp_sep_info;
94 
95 /** @brief AVDTP SEID Information AVDTP_SPEC V13 Table 8.8 */
96 struct bt_avdtp_sep_data {
97 #ifdef CONFIG_LITTLE_ENDIAN
98 	uint8_t rfa0: 1;
99 	uint8_t inuse: 1;
100 	uint8_t id: 6;
101 	uint8_t rfa1: 3;
102 	uint8_t tsep: 1;
103 	uint8_t media_type: 4;
104 #else
105 	uint8_t id: 6;
106 	uint8_t inuse: 1;
107 	uint8_t rfa0: 1;
108 	uint8_t media_type: 4;
109 	uint8_t tsep: 1;
110 	uint8_t rfa1: 3;
111 #endif
112 } __packed;
113 
114 typedef int (*bt_avdtp_func_t)(struct bt_avdtp_req *req);
115 
116 struct bt_avdtp_req {
117 	uint8_t sig;
118 	uint8_t tid;
119 	uint8_t status;
120 	bt_avdtp_func_t func;
121 };
122 
123 struct bt_avdtp_single_sig_hdr {
124 	uint8_t hdr;
125 	uint8_t signal_id;
126 } __packed;
127 
128 struct bt_avdtp_media_hdr {
129 #ifdef CONFIG_LITTLE_ENDIAN
130 	uint8_t CSRC_count: 4;
131 	uint8_t header_extension: 1;
132 	uint8_t padding: 1;
133 	uint8_t RTP_version: 2;
134 	uint8_t playload_type: 7;
135 	uint8_t marker: 1;
136 #else
137 	uint8_t RTP_version: 2;
138 	uint8_t padding: 1;
139 	uint8_t header_extension: 1;
140 	uint8_t CSRC_count: 4;
141 	uint8_t marker: 1;
142 	uint8_t playload_type: 7;
143 #endif
144 	uint16_t sequence_number;
145 	uint32_t time_stamp;
146 	uint32_t synchronization_source;
147 } __packed;
148 
149 struct bt_avdtp_discover_params {
150 	struct bt_avdtp_req req;
151 	struct net_buf *buf;
152 };
153 
154 struct bt_avdtp_get_capabilities_params {
155 	struct bt_avdtp_req req;
156 	uint8_t stream_endpoint_id;
157 	struct net_buf *buf;
158 };
159 
160 struct bt_avdtp_set_configuration_params {
161 	struct bt_avdtp_req req;
162 	struct bt_avdtp_sep *sep;
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 /* avdtp_open, avdtp_close, avdtp_start, avdtp_suspend */
172 struct bt_avdtp_ctrl_params {
173 	struct bt_avdtp_req req;
174 	struct bt_avdtp_sep *sep;
175 	uint8_t acp_stream_ep_id;
176 };
177 
178 struct bt_avdtp_ops_cb {
179 	void (*connected)(struct bt_avdtp *session);
180 
181 	void (*disconnected)(struct bt_avdtp *session);
182 
183 	struct net_buf *(*alloc_buf)(struct bt_avdtp *session);
184 
185 	int (*discovery_ind)(struct bt_avdtp *session, uint8_t *errcode);
186 
187 	int (*get_capabilities_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
188 				    struct net_buf *rsp_buf, uint8_t *errcode);
189 
190 	int (*set_configuration_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
191 				     uint8_t int_seid, struct net_buf *buf, uint8_t *errcode);
192 
193 	int (*re_configuration_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
194 				    uint8_t int_seid, struct net_buf *buf, uint8_t *errcode);
195 
196 	int (*open_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
197 
198 	int (*close_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
199 
200 	int (*start_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
201 
202 	int (*suspend_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
203 
204 	int (*abort_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
205 
206 	/* stream l2cap is closed */
207 	int (*stream_l2cap_disconnected)(struct bt_avdtp *session, struct bt_avdtp_sep *sep);
208 };
209 
210 /** @brief Global AVDTP session structure. */
211 struct bt_avdtp {
212 	struct bt_l2cap_br_chan br_chan;
213 	struct bt_avdtp_req *req;
214 	const struct bt_avdtp_ops_cb *ops;
215 	struct bt_avdtp_sep *current_sep;
216 	struct k_work_delayable timeout_work;
217 	/* semaphore for lock/unlock */
218 	struct k_sem sem_lock;
219 };
220 
221 struct bt_avdtp_event_cb {
222 	int (*accept)(struct bt_conn *conn, struct bt_avdtp **session);
223 };
224 
225 /* Initialize AVDTP layer*/
226 int bt_avdtp_init(void);
227 
228 /* Application register with AVDTP layer */
229 int bt_avdtp_register(struct bt_avdtp_event_cb *cb);
230 
231 /* AVDTP connect */
232 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session);
233 
234 /* AVDTP disconnect */
235 int bt_avdtp_disconnect(struct bt_avdtp *session);
236 
237 /* AVDTP SEP register function */
238 int bt_avdtp_register_sep(uint8_t media_type, uint8_t sep_type, struct bt_avdtp_sep *sep);
239 
240 /* AVDTP Discover Request */
241 int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params *param);
242 
243 /* Parse the sep of discovered result */
244 int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info);
245 
246 /* AVDTP Get Capabilities */
247 int bt_avdtp_get_capabilities(struct bt_avdtp *session,
248 			      struct bt_avdtp_get_capabilities_params *param);
249 
250 /* Parse the codec type of capabilities */
251 int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
252 				    uint8_t **codec_info_element, uint16_t *codec_info_element_len);
253 
254 /* AVDTP Set Configuration */
255 int bt_avdtp_set_configuration(struct bt_avdtp *session,
256 			       struct bt_avdtp_set_configuration_params *param);
257 
258 /* AVDTP reconfigure */
259 int bt_avdtp_reconfigure(struct bt_avdtp *session, struct bt_avdtp_set_configuration_params *param);
260 
261 /* AVDTP OPEN */
262 int bt_avdtp_open(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
263 
264 /* AVDTP CLOSE */
265 int bt_avdtp_close(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
266 
267 /* AVDTP START */
268 int bt_avdtp_start(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
269 
270 /* AVDTP SUSPEND */
271 int bt_avdtp_suspend(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
272 
273 /* AVDTP ABORT */
274 int bt_avdtp_abort(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
275 
276 /* AVDTP send data */
277 int bt_avdtp_send_media_data(struct bt_avdtp_sep *sep, struct net_buf *buf);
278 
279 /* get media l2cap connection MTU */
280 uint32_t bt_avdtp_get_media_mtu(struct bt_avdtp_sep *sep);
281