xref: /btstack/src/classic/avdtp_acceptor.c (revision dbe92de6a93924b91532f8d818af55adf6b370d8)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 #define BTSTACK_FILE__ "avdtp_acceptor.c"
38 
39 #include <stdint.h>
40 #include <string.h>
41 
42 #include "btstack.h"
43 #include "classic/avdtp.h"
44 #include "classic/avdtp_util.h"
45 #include "classic/avdtp_acceptor.h"
46 
47 
48 static int avdtp_acceptor_send_accept_response(uint16_t cid,  uint8_t transaction_label, avdtp_signal_identifier_t identifier){
49     uint8_t command[2];
50     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
51     command[1] = (uint8_t)identifier;
52     return l2cap_send(cid, command, sizeof(command));
53 }
54 
55 static int avdtp_acceptor_process_chunk(avdtp_signaling_packet_t * signaling_packet, uint8_t * packet, uint16_t size){
56     (void)memcpy(signaling_packet->command + signaling_packet->size, packet,
57                  size);
58     signaling_packet->size += size;
59     return (signaling_packet->packet_type == AVDTP_SINGLE_PACKET) || (signaling_packet->packet_type == AVDTP_END_PACKET);
60 }
61 
62 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){
63     int minimal_msg_lenght = 2;
64     switch (signal_identifier){
65         case AVDTP_SI_GET_CAPABILITIES:
66         case AVDTP_SI_GET_ALL_CAPABILITIES:
67         case AVDTP_SI_SET_CONFIGURATION:
68         case AVDTP_SI_GET_CONFIGURATION:
69         case AVDTP_SI_START:
70         case AVDTP_SI_CLOSE:
71         case AVDTP_SI_ABORT:
72         case AVDTP_SI_RECONFIGURE:
73         case AVDTP_SI_OPEN:
74             minimal_msg_lenght = 3;
75             break;
76         default:
77             break;
78         }
79     return msg_size >= minimal_msg_lenght;
80 }
81 
82 static void
83 avdtp_acceptor_handle_configuration_command(avdtp_connection_t *connection, int offset, uint16_t packet_size, avdtp_stream_endpoint_t *stream_endpoint) {
84     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION connection %p", connection);
85     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE;
86     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION;
87     connection->reject_service_category = 0;
88     stream_endpoint->connection = connection;
89     avdtp_sep_t sep;
90     sep.seid = connection->acceptor_signaling_packet.command[offset++] >> 2;
91     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, connection->acceptor_signaling_packet.signal_identifier, &sep.configuration, connection->acceptor_signaling_packet.command+offset, packet_size-offset);
92     sep.in_use = 1;
93 
94     if (connection->error_code){
95         log_info("fire configuration parsing errors ");
96         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
97         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
98         return;
99     }
100     // find or add sep
101 
102     log_info("ACP .. seid %d, remote sep seid %d", sep.seid, stream_endpoint->remote_sep.seid);
103 
104     if (is_avdtp_remote_seid_registered(stream_endpoint)){
105         if (stream_endpoint->remote_sep.in_use){
106             log_info("reject as it is already in use");
107             connection->error_code = SEP_IN_USE;
108             // find first registered category and fire the error
109             connection->reject_service_category = 0;
110             int i;
111             for (i = 1; i < 9; i++){
112                 if (get_bit16(sep.configured_service_categories, i)){
113                     connection->reject_service_category = i;
114                     break;
115                 }
116             }
117             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
118             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
119         } else {
120             stream_endpoint->remote_sep = sep;
121             log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
122         }
123     } else {
124         // add new
125         log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint);
126         stream_endpoint->remote_sep = sep;
127         log_info("ACP: add seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
128     }
129 
130     avdtp_emit_configuration(stream_endpoint, connection->avdtp_cid, &sep.configuration, sep.configured_service_categories);
131     avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
132                                 connection->acceptor_signaling_packet.signal_identifier, false);
133 }
134 
135 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t *connection, uint8_t *packet, uint16_t size, int offset) {
136     avdtp_stream_endpoint_t * stream_endpoint = NULL;
137     connection->acceptor_transaction_label = connection->acceptor_signaling_packet.transaction_label;
138     if (!avdtp_acceptor_validate_msg_length(connection->acceptor_signaling_packet.signal_identifier, size)) {
139         connection->error_code = BAD_LENGTH;
140         connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
141         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
142         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
143         return;
144     }
145 
146     // handle error cases
147     switch (connection->acceptor_signaling_packet.signal_identifier){
148         case AVDTP_SI_DISCOVER:
149             if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
150             log_info("ACP: AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS");
151             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS;
152             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
153             return;
154         case AVDTP_SI_GET_CAPABILITIES:
155         case AVDTP_SI_GET_ALL_CAPABILITIES:
156         case AVDTP_SI_SET_CONFIGURATION:
157         case AVDTP_SI_GET_CONFIGURATION:
158         case AVDTP_SI_START:
159         case AVDTP_SI_CLOSE:
160         case AVDTP_SI_ABORT:
161         case AVDTP_SI_OPEN:
162         case AVDTP_SI_RECONFIGURE:
163         case AVDTP_SI_DELAYREPORT:
164             connection->acceptor_local_seid  = packet[offset++] >> 2;
165             stream_endpoint = avdtp_get_stream_endpoint_with_seid(connection->acceptor_local_seid);
166             if (!stream_endpoint){
167                 log_info("ACP: cmd %d - RESPONSE REJECT", connection->acceptor_signaling_packet.signal_identifier);
168                 connection->error_code = BAD_ACP_SEID;
169                 if (connection->acceptor_signaling_packet.signal_identifier == AVDTP_SI_OPEN){
170                     connection->error_code = BAD_STATE;
171                 }
172 
173                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
174                 if (connection->acceptor_signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){
175                     connection->reject_service_category = connection->acceptor_local_seid;
176                     connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
177                 }
178                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
179                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
180                 return;
181             }
182             break;
183 
184         case AVDTP_SI_SUSPEND:{
185             int i;
186             log_info("ACP: AVDTP_SI_SUSPEND seids: ");
187             connection->num_suspended_seids = 0;
188 
189             for (i = offset; i < size; i++){
190                 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2;
191                 offset++;
192                 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]);
193                 connection->num_suspended_seids++;
194             }
195 
196             if (connection->num_suspended_seids == 0) {
197                 log_info("ACP: CATEGORY RESPONSE REJECT BAD_ACP_SEID");
198                 connection->error_code = BAD_ACP_SEID;
199                 connection->reject_service_category = connection->acceptor_local_seid;
200                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
201                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
202                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
203                 return;
204             }
205             // deal with first susspended seid
206             connection->acceptor_local_seid = connection->suspended_seids[0];
207             stream_endpoint = avdtp_get_stream_endpoint_with_seid(connection->acceptor_local_seid);
208             if (!stream_endpoint){
209                 log_info("ACP: stream_endpoint not found, CATEGORY RESPONSE REJECT BAD_ACP_SEID");
210                 connection->error_code = BAD_ACP_SEID;
211                 connection->reject_service_category = connection->acceptor_local_seid;
212                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
213                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
214                 connection->num_suspended_seids = 0;
215                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
216                 return;
217             }
218             break;
219         }
220         default:
221             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE;
222             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
223             log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->acceptor_signaling_packet.signal_identifier);
224             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
225             return;
226     }
227 
228     btstack_assert(stream_endpoint != NULL);
229 
230     if (!avdtp_acceptor_process_chunk(&connection->acceptor_signaling_packet, packet, size)) return;
231 
232     uint16_t packet_size = connection->acceptor_signaling_packet.size;
233     connection->acceptor_signaling_packet.size = 0;
234 
235     int request_to_send = 1;
236     switch (stream_endpoint->acceptor_config_state){
237         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
238             switch (connection->acceptor_signaling_packet.signal_identifier){
239                 case AVDTP_SI_DELAYREPORT:
240                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT, local seid %d", connection->acceptor_local_seid);
241                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT;
242                     avdtp_signaling_emit_delay(connection->avdtp_cid, connection->acceptor_local_seid,
243                                                big_endian_read_16(packet, offset));
244                     break;
245 
246                 case AVDTP_SI_GET_ALL_CAPABILITIES:
247                     log_info("ACP: AVDTP_SI_GET_ALL_CAPABILITIES, local seid %d", connection->acceptor_local_seid);
248                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES;
249                     break;
250                 case AVDTP_SI_GET_CAPABILITIES:
251                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES, local seid %d", connection->acceptor_local_seid);
252                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES;
253                     break;
254                 case AVDTP_SI_SET_CONFIGURATION:{
255                     log_info("Received SET_CONFIGURATION cmd: config state %d", connection->configuration_state);
256                     switch (connection->configuration_state){
257                         case AVDTP_CONFIGURATION_STATE_IDLE:
258                             avdtp_acceptor_handle_configuration_command(connection, offset, packet_size,
259                                                                         stream_endpoint);
260                             connection->configuration_state = AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED;
261                             break;
262                         case AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED:
263                         case AVDTP_CONFIGURATION_STATE_LOCAL_CONFIGURED:
264                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
265                             connection->reject_service_category = 0;
266                             connection->error_code = BAD_STATE;
267                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
268                             break;
269                         default:
270                             break;
271                     }
272                     break;
273                 }
274                 case AVDTP_SI_RECONFIGURE:{
275                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE;
276                     connection->reject_service_category = 0;
277 
278                     avdtp_sep_t sep;
279                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE, local seid %d, remote seid %d", connection->acceptor_local_seid, stream_endpoint->remote_sep.seid);
280                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, connection->acceptor_signaling_packet.signal_identifier, &sep.configuration, connection->acceptor_signaling_packet.command+offset, packet_size-offset);
281                     if (connection->error_code){
282                         // fire configuration parsing errors
283                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
284                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
285                         break;
286                     }
287 
288                     // find sep or raise error
289                     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
290                         log_info("ACP: REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID");
291                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
292                         connection->error_code = BAD_ACP_SEID;
293                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
294                         break;
295                     }
296                     stream_endpoint->remote_sep.configured_service_categories = sep.configured_service_categories;
297                     stream_endpoint->remote_sep.configuration = sep.configuration;
298 
299                     log_info("ACP: update active remote seid %d", stream_endpoint->remote_sep.seid);
300 
301                     avdtp_emit_configuration(stream_endpoint, connection->avdtp_cid, &sep.configuration, sep.configured_service_categories);
302                     break;
303                 }
304 
305                 case AVDTP_SI_GET_CONFIGURATION:
306                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION");
307                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION;
308                     break;
309                 case AVDTP_SI_OPEN:
310                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){
311                         log_info("ACP: REJECT AVDTP_SI_OPEN, BAD_STATE");
312                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
313                         connection->error_code = BAD_STATE;
314                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
315                         break;
316                     }
317                     log_info("ACP: AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM");
318                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM;
319                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED;
320                     connection->acceptor_local_seid = stream_endpoint->sep.seid;
321                     break;
322                 case AVDTP_SI_START:
323                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){
324                         log_info("ACP: REJECT AVDTP_SI_START, BAD_STATE, state %d", stream_endpoint->state);
325                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
326                         connection->error_code = BAD_STATE;
327                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
328                         break;
329                     }
330                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM");
331                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM;
332                     break;
333                 case AVDTP_SI_CLOSE:
334                     switch (stream_endpoint->state){
335                         case AVDTP_STREAM_ENDPOINT_OPENED:
336                         case AVDTP_STREAM_ENDPOINT_STREAMING:
337                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM");
338                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING;
339                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM;
340                             break;
341                         default:
342                             log_info("ACP: AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state);
343                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
344                             connection->error_code = BAD_STATE;
345                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
346                             break;
347                     }
348                     break;
349                 case AVDTP_SI_ABORT:
350                      switch (stream_endpoint->state){
351                         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
352                         case AVDTP_STREAM_ENDPOINT_CLOSING:
353                         case AVDTP_STREAM_ENDPOINT_OPENED:
354                         case AVDTP_STREAM_ENDPOINT_STREAMING:
355                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM");
356                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
357                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM;
358                             break;
359                         default:
360                             log_info("ACP: AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state);
361                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
362                             connection->error_code = BAD_STATE;
363                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
364                             break;
365                     }
366                     break;
367                 case AVDTP_SI_SUSPEND:
368                     log_info(" entering AVDTP_SI_SUSPEND");
369                     switch (stream_endpoint->state){
370                         case AVDTP_STREAM_ENDPOINT_OPENED:
371                         case AVDTP_STREAM_ENDPOINT_STREAMING:
372                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
373                             connection->num_suspended_seids--;
374                             if (connection->num_suspended_seids <= 0){
375                                 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM");
376                                 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM;
377                             }
378                             break;
379                         default:
380                             log_info("ACP: AVDTP_SI_SUSPEND, bad state ");
381                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
382                             connection->error_code = BAD_STATE;
383                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
384                             break;
385                     }
386                     break;
387                 default:
388                     log_info("ACP: NOT IMPLEMENTED, Reject signal_identifier %02x", connection->acceptor_signaling_packet.signal_identifier);
389                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
390                     connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
391                     break;
392             }
393             break;
394         default:
395             return;
396     }
397 
398     if (!request_to_send){
399         log_info("ACP: NOT IMPLEMENTED");
400     }
401     avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
402 }
403 
404 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){
405     uint8_t command[2+2*AVDTP_MAX_NUM_SEPS];
406     int pos = 0;
407     command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
408     command[pos++] = (uint8_t)AVDTP_SI_DISCOVER;
409 
410     btstack_linked_list_iterator_t it;
411     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints);
412     while (btstack_linked_list_iterator_has_next(&it)){
413         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
414         command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1);
415         command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3);
416     }
417     return l2cap_send(cid, command, pos);
418 }
419 
420 static int avdtp_acceptor_send_response_reject_service_category(uint16_t cid,  avdtp_signal_identifier_t identifier, uint8_t category, uint8_t error_code, uint8_t transaction_label){
421     uint8_t command[4];
422     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
423     command[1] = (uint8_t)identifier;
424     command[2] = category;
425     command[3] = error_code;
426     return l2cap_send(cid, command, sizeof(command));
427 }
428 
429 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
430     uint8_t command[2];
431     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG);
432     command[1] = (uint8_t)identifier;
433     return l2cap_send(cid, command, sizeof(command));
434 }
435 
436 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
437     uint8_t command[2];
438     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
439     command[1] = (uint8_t)identifier;
440     return l2cap_send(cid, command, sizeof(command));
441 }
442 
443 static int avdtp_acceptor_send_response_reject_with_error_code(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t error_code, uint8_t transaction_label){
444     uint8_t command[3];
445     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
446     command[1] = (uint8_t)identifier;
447     command[2] = error_code;
448     return l2cap_send(cid, command, sizeof(command));
449 }
450 
451 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t *connection) {
452     int sent = 1;
453     btstack_linked_list_t * stream_endpoints = avdtp_get_stream_endpoints();
454 
455     switch (connection->acceptor_connection_state){
456         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS:
457             connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
458             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
459             avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints);
460             break;
461         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
462             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
463             avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label);
464             break;
465         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
466             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
467             avdtp_acceptor_send_response_reject_service_category(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->reject_service_category, connection->error_code, connection->acceptor_transaction_label);
468             break;
469         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE:
470             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
471             avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label);
472             break;
473         default:
474             sent = 0;
475             break;
476     }
477     if (sent){
478         log_info("ACP: DONE");
479         return;
480     }
481 
482     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(connection->acceptor_local_seid);
483     if (!stream_endpoint) return;
484 
485     uint8_t reject_service_category = connection->reject_service_category;
486     avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier;
487     uint8_t error_code = connection->error_code;
488     uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid;
489     uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label;
490 
491     avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state;
492     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
493     uint8_t * out_buffer;
494     uint16_t pos;
495 
496     bool emit_accept = false;
497     bool emit_reject = false;
498 
499     switch (acceptor_config_state){
500         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
501             break;
502         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES:
503             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES);
504             l2cap_reserve_packet_buffer();
505             out_buffer = l2cap_get_outgoing_buffer();
506             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
507             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
508                 stream_endpoint->acceptor_config_state = acceptor_config_state;
509                 log_info("ACP: fragmented");
510             } else {
511                 log_info("ACP:DONE");
512                 emit_accept = true;
513             }
514             l2cap_send_prepared(cid, pos);
515             break;
516         case AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT:
517             log_info("ACP: DONE ");
518             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_DELAYREPORT);
519             emit_accept = true;
520             break;
521         case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES:
522             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES);
523             l2cap_reserve_packet_buffer();
524             out_buffer = l2cap_get_outgoing_buffer();
525             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
526             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
527                 stream_endpoint->acceptor_config_state = acceptor_config_state;
528                 log_info("ACP: fragmented");
529             } else {
530                 log_info("ACP:DONE");
531                 emit_accept = true;
532             }
533             l2cap_send_prepared(cid, pos);
534             break;
535         case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION:
536             log_info("ACP: DONE");
537             log_info("    -> AVDTP_STREAM_ENDPOINT_CONFIGURED");
538             stream_endpoint->connection = connection;
539             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
540             connection->configuration_state = AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED;
541             // TODO: consider reconfiguration
542             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION);
543             emit_accept = true;
544             break;
545         case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE:
546             log_info("ACP: DONE ");
547             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE);
548             emit_accept = true;
549             break;
550 
551         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{
552             avdtp_sep_t sep = stream_endpoint->remote_sep;
553             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION);
554             l2cap_reserve_packet_buffer();
555             out_buffer = l2cap_get_outgoing_buffer();
556             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
557             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
558                 stream_endpoint->acceptor_config_state = acceptor_config_state;
559                 log_info("ACP: fragmented");
560             } else {
561                 log_info("ACP:DONE");
562                 emit_accept = true;
563             }
564             l2cap_send_prepared(cid, pos);
565             break;
566         }
567         case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM:
568             log_info("ACP: DONE");
569             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN);
570             emit_accept = true;
571             break;
572         case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM:
573             log_info("ACP: DONE ");
574             log_info("    -> AVDTP_STREAM_ENDPOINT_STREAMING ");
575             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
576             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START);
577             emit_accept = true;
578             break;
579         case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM:
580             log_info("ACP: DONE");
581             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE);
582             connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE;
583             emit_accept = true;
584             break;
585         case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM:
586             log_info("ACP: DONE");
587             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT);
588             emit_accept = true;
589             break;
590         case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM:
591             log_info("ACP: DONE");
592             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
593             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND);
594             emit_accept = true;
595             break;
596         case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD:
597             log_info("ACP: DONE REJECT");
598             connection->reject_signal_identifier = AVDTP_SI_NONE;
599             avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid);
600             emit_reject = true;
601             break;
602         case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
603             log_info("ACP: DONE REJECT CATEGORY");
604             connection->reject_service_category = 0;
605             avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid);
606             emit_reject = true;
607             break;
608         case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
609             log_info("ACP: DONE REJECT");
610             connection->reject_signal_identifier = AVDTP_SI_NONE;
611             connection->error_code = 0;
612             avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid);
613             emit_reject = true;
614             break;
615         default:
616             log_info("ACP: NOT IMPLEMENTED");
617             sent = 0;
618             break;
619     }
620 
621     if (emit_accept == true){
622         avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
623                                     connection->acceptor_signaling_packet.signal_identifier, false);
624     } else if (emit_reject == true){
625         avdtp_signaling_emit_reject(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
626                                     connection->acceptor_signaling_packet.signal_identifier, false);
627     }
628     // check fragmentation
629     if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
630         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
631     }
632 }
633