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, struct net_buf *buf);
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 };
152 
153 struct bt_avdtp_get_capabilities_params {
154 	struct bt_avdtp_req req;
155 	uint8_t stream_endpoint_id;
156 };
157 
158 struct bt_avdtp_set_configuration_params {
159 	struct bt_avdtp_req req;
160 	struct bt_avdtp_sep *sep;
161 	uint8_t acp_stream_ep_id;
162 	uint8_t int_stream_endpoint_id;
163 	uint8_t media_type;
164 	uint8_t media_codec_type;
165 	uint8_t codec_specific_ie_len;
166 	uint8_t *codec_specific_ie;
167 };
168 
169 /* avdtp_open, avdtp_close, avdtp_start, avdtp_suspend */
170 struct bt_avdtp_ctrl_params {
171 	struct bt_avdtp_req req;
172 	struct bt_avdtp_sep *sep;
173 	uint8_t acp_stream_ep_id;
174 };
175 
176 struct bt_avdtp_ops_cb {
177 	void (*connected)(struct bt_avdtp *session);
178 
179 	void (*disconnected)(struct bt_avdtp *session);
180 
181 	struct net_buf *(*alloc_buf)(struct bt_avdtp *session);
182 
183 	int (*discovery_ind)(struct bt_avdtp *session, uint8_t *errcode);
184 
185 	int (*get_capabilities_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
186 				    struct net_buf *rsp_buf, uint8_t *errcode);
187 
188 	int (*set_configuration_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
189 				     uint8_t int_seid, struct net_buf *buf, uint8_t *errcode);
190 
191 	int (*re_configuration_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep,
192 				    uint8_t int_seid, struct net_buf *buf, uint8_t *errcode);
193 
194 	int (*open_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
195 
196 	int (*close_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
197 
198 	int (*start_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
199 
200 	int (*suspend_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
201 
202 	int (*abort_ind)(struct bt_avdtp *session, struct bt_avdtp_sep *sep, uint8_t *errcode);
203 
204 	/* stream l2cap is closed */
205 	int (*stream_l2cap_disconnected)(struct bt_avdtp *session, struct bt_avdtp_sep *sep);
206 };
207 
208 /** @brief Global AVDTP session structure. */
209 struct bt_avdtp {
210 	struct bt_l2cap_br_chan br_chan;
211 	struct bt_avdtp_req *req;
212 	const struct bt_avdtp_ops_cb *ops;
213 	struct bt_avdtp_sep *current_sep;
214 	struct k_work_delayable timeout_work;
215 	/* semaphore for lock/unlock */
216 	struct k_sem sem_lock;
217 };
218 
219 struct bt_avdtp_event_cb {
220 	int (*accept)(struct bt_conn *conn, struct bt_avdtp **session);
221 };
222 
223 /* Initialize AVDTP layer*/
224 int bt_avdtp_init(void);
225 
226 /* Application register with AVDTP layer */
227 int bt_avdtp_register(struct bt_avdtp_event_cb *cb);
228 
229 /* AVDTP connect */
230 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session);
231 
232 /* AVDTP disconnect */
233 int bt_avdtp_disconnect(struct bt_avdtp *session);
234 
235 /* AVDTP SEP register function */
236 int bt_avdtp_register_sep(uint8_t media_type, uint8_t sep_type, struct bt_avdtp_sep *sep);
237 
238 /* AVDTP Discover Request */
239 int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params *param);
240 
241 /* Parse the sep of discovered result */
242 int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info);
243 
244 /* AVDTP Get Capabilities */
245 int bt_avdtp_get_capabilities(struct bt_avdtp *session,
246 			      struct bt_avdtp_get_capabilities_params *param);
247 
248 /* Parse the codec type of capabilities */
249 int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
250 				    uint8_t **codec_info_element, uint16_t *codec_info_element_len);
251 
252 /* AVDTP Set Configuration */
253 int bt_avdtp_set_configuration(struct bt_avdtp *session,
254 			       struct bt_avdtp_set_configuration_params *param);
255 
256 /* AVDTP reconfigure */
257 int bt_avdtp_reconfigure(struct bt_avdtp *session, struct bt_avdtp_set_configuration_params *param);
258 
259 /* AVDTP OPEN */
260 int bt_avdtp_open(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
261 
262 /* AVDTP CLOSE */
263 int bt_avdtp_close(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
264 
265 /* AVDTP START */
266 int bt_avdtp_start(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
267 
268 /* AVDTP SUSPEND */
269 int bt_avdtp_suspend(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
270 
271 /* AVDTP ABORT */
272 int bt_avdtp_abort(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param);
273 
274 /* AVDTP send data */
275 int bt_avdtp_send_media_data(struct bt_avdtp_sep *sep, struct net_buf *buf);
276 
277 /* get media l2cap connection MTU */
278 uint32_t bt_avdtp_get_media_mtu(struct bt_avdtp_sep *sep);
279