xref: /btstack/src/classic/avdtp_acceptor.c (revision 3c4cc6427fe05577c00b7d2593f58c7abcf9eab7)
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 <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "btstack.h"
45 #include "classic/avdtp.h"
46 #include "classic/avdtp_util.h"
47 #include "classic/avdtp_acceptor.h"
48 
49 
50 static int avdtp_acceptor_send_accept_response(uint16_t cid,  uint8_t transaction_label, avdtp_signal_identifier_t identifier){
51     uint8_t command[2];
52     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
53     command[1] = (uint8_t)identifier;
54     return l2cap_send(cid, command, sizeof(command));
55 }
56 
57 static int avdtp_acceptor_process_chunk(avdtp_signaling_packet_t * signaling_packet, uint8_t * packet, uint16_t size){
58     memcpy(signaling_packet->command + signaling_packet->size, packet, size);
59     signaling_packet->size += size;
60     return signaling_packet->packet_type == AVDTP_SINGLE_PACKET || signaling_packet->packet_type == AVDTP_END_PACKET;
61 }
62 
63 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){
64     int minimal_msg_lenght = 2;
65     switch (signal_identifier){
66         case AVDTP_SI_GET_CAPABILITIES:
67         case AVDTP_SI_GET_ALL_CAPABILITIES:
68         case AVDTP_SI_SET_CONFIGURATION:
69         case AVDTP_SI_GET_CONFIGURATION:
70         case AVDTP_SI_START:
71         case AVDTP_SI_CLOSE:
72         case AVDTP_SI_ABORT:
73         case AVDTP_SI_RECONFIGURE:
74         case AVDTP_SI_OPEN:
75             minimal_msg_lenght = 3;
76             break;
77         default:
78             break;
79         }
80     return msg_size >= minimal_msg_lenght;
81 }
82 
83 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t * packet, uint16_t size, int offset, avdtp_context_t * context){
84     avdtp_stream_endpoint_t * stream_endpoint;
85     connection->acceptor_transaction_label = connection->signaling_packet.transaction_label;
86     if (!avdtp_acceptor_validate_msg_length(connection->signaling_packet.signal_identifier, size)) {
87         connection->error_code = BAD_LENGTH;
88         connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
89         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
90         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
91         return;
92     }
93 
94     // handle error cases
95     switch (connection->signaling_packet.signal_identifier){
96         case AVDTP_SI_DISCOVER:
97             if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
98             log_info("ACP: AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS");
99             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS;
100             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
101             return;
102         case AVDTP_SI_GET_CAPABILITIES:
103         case AVDTP_SI_GET_ALL_CAPABILITIES:
104         case AVDTP_SI_SET_CONFIGURATION:
105         case AVDTP_SI_GET_CONFIGURATION:
106         case AVDTP_SI_START:
107         case AVDTP_SI_CLOSE:
108         case AVDTP_SI_ABORT:
109         case AVDTP_SI_OPEN:
110         case AVDTP_SI_RECONFIGURE:
111         case AVDTP_SI_DELAYREPORT:
112             connection->local_seid  = packet[offset++] >> 2;
113             stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
114             if (!stream_endpoint){
115                 log_info("ACP: cmd %d - RESPONSE REJECT", connection->signaling_packet.signal_identifier);
116                 connection->error_code = BAD_ACP_SEID;
117                 if (connection->signaling_packet.signal_identifier == AVDTP_SI_OPEN){
118                     connection->error_code = BAD_STATE;
119                 }
120 
121                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
122                 if (connection->signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){
123                     connection->reject_service_category = connection->local_seid;
124                     connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
125                 }
126                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
127                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
128                 return;
129             }
130             break;
131 
132         case AVDTP_SI_SUSPEND:{
133             int i;
134             log_info("ACP: AVDTP_SI_SUSPEND seids: ");
135             connection->num_suspended_seids = 0;
136 
137             for (i = offset; i < size; i++){
138                 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2;
139                 offset++;
140                 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]);
141                 connection->num_suspended_seids++;
142             }
143 
144             if (connection->num_suspended_seids == 0) {
145                 log_info("ACP: CATEGORY RESPONSE REJECT BAD_ACP_SEID");
146                 connection->error_code = BAD_ACP_SEID;
147                 connection->reject_service_category = connection->local_seid;
148                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
149                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
150                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
151                 return;
152             }
153             // deal with first susspended seid
154             connection->local_seid = connection->suspended_seids[0];
155             stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
156             if (!stream_endpoint){
157                 log_info("ACP: stream_endpoint not found, CATEGORY RESPONSE REJECT BAD_ACP_SEID");
158                 connection->error_code = BAD_ACP_SEID;
159                 connection->reject_service_category = connection->local_seid;
160                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
161                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
162                 connection->num_suspended_seids = 0;
163                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
164                 return;
165             }
166             break;
167         }
168         default:
169             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE;
170             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
171             log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->signaling_packet.signal_identifier);
172             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
173             return;
174     }
175 
176     if (!stream_endpoint) {
177         return;
178     }
179 
180     if (!avdtp_acceptor_process_chunk(&connection->signaling_packet, packet, size)) return;
181 
182     uint16_t packet_size = connection->signaling_packet.size;
183     connection->signaling_packet.size = 0;
184 
185     int request_to_send = 1;
186     switch (stream_endpoint->acceptor_config_state){
187         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
188             switch (connection->signaling_packet.signal_identifier){
189                 case AVDTP_SI_DELAYREPORT:
190                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT");
191                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT;
192                     avdtp_signaling_emit_delay(context->avdtp_callback, connection->avdtp_cid, connection->local_seid, big_endian_read_16(packet, offset));
193                     break;
194 
195                 case AVDTP_SI_GET_ALL_CAPABILITIES:
196                     log_info("ACP: AVDTP_SI_GET_ALL_CAPABILITIES");
197                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES;
198                     break;
199                 case AVDTP_SI_GET_CAPABILITIES:
200                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES");
201                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES;
202                     break;
203                 case AVDTP_SI_SET_CONFIGURATION:{
204                     // log_info("acceptor SM received SET_CONFIGURATION cmd: role is_initiator %d", connection->is_initiator);
205                     if (connection->is_initiator){
206                         if (connection->is_configuration_initiated_locally){
207                             log_info("ACP: Set configuration already initiated locally, reject cmd ");
208                             // fire configuration parsing errors
209                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
210                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
211                             connection->is_initiator = 1;
212                             break;
213                         }
214                         connection->is_initiator = 0;
215                         log_info("acceptor SM received SET_CONFIGURATION cmd: change role to acceptor, is_initiator %d", connection->is_initiator);
216                     }
217 
218                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION connection %p", connection);
219                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE;
220                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION;
221                     connection->reject_service_category = 0;
222                     stream_endpoint->connection = connection;
223                     avdtp_sep_t sep;
224                     sep.seid = connection->signaling_packet.command[offset++] >> 2;
225                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset);
226                     sep.in_use = 1;
227 
228                     if (connection->error_code){
229                         log_info("fire configuration parsing errors ");
230                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
231                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
232                         break;
233                     }
234                     // find or add sep
235 
236                     log_info("ACP .. seid %d, remote sep seid %d", sep.seid, stream_endpoint->remote_sep.seid);
237 
238                     if (is_avdtp_remote_seid_registered(stream_endpoint)){
239                         if (stream_endpoint->remote_sep.in_use){
240                             log_info("reject as it is already in use");
241                             connection->error_code = SEP_IN_USE;
242                             // find first registered category and fire the error
243                             connection->reject_service_category = 0;
244                             int i;
245                             for (i = 1; i < 9; i++){
246                                 if (get_bit16(sep.configured_service_categories, i)){
247                                     connection->reject_service_category = i;
248                                     break;
249                                 }
250                             }
251                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
252                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
253                         } else {
254                             stream_endpoint->remote_sep = sep;
255                             log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
256                         }
257                     } else {
258                         // add new
259                         log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint);
260                         stream_endpoint->remote_sep = sep;
261                         log_info("ACP: add seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
262                     }
263 
264                     avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories);
265                     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
266                     break;
267                 }
268                 case AVDTP_SI_RECONFIGURE:{
269                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE;
270                     connection->reject_service_category = 0;
271 
272                     avdtp_sep_t sep;
273                     sep.seid = connection->local_seid;
274                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE seid %d", sep.seid);
275                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset);
276                     if (connection->error_code){
277                         // fire configuration parsing errors
278                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
279                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
280                         break;
281                     }
282 
283                     // find sep or raise error
284                     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
285                         log_info("ACP: REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID");
286                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
287                         connection->error_code = BAD_ACP_SEID;
288                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
289                         break;
290                     }
291                     stream_endpoint->remote_sep = sep;
292                     log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
293 
294                     avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories);
295                     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
296                     break;
297                 }
298 
299                 case AVDTP_SI_GET_CONFIGURATION:
300                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION");
301                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION;
302                     break;
303                 case AVDTP_SI_OPEN:
304                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){
305                         log_info("ACP: REJECT AVDTP_SI_OPEN, BAD_STATE");
306                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
307                         connection->error_code = BAD_STATE;
308                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
309                         break;
310                     }
311                     log_info("ACP: AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM");
312                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM;
313                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED;
314                     connection->local_seid = stream_endpoint->sep.seid;
315                     break;
316                 case AVDTP_SI_START:
317                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){
318                         log_info("ACP: REJECT AVDTP_SI_START, BAD_STATE, state %d", stream_endpoint->state);
319                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
320                         connection->error_code = BAD_STATE;
321                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
322                         break;
323                     }
324                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM");
325                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM;
326                     break;
327                 case AVDTP_SI_CLOSE:
328                     switch (stream_endpoint->state){
329                         case AVDTP_STREAM_ENDPOINT_OPENED:
330                         case AVDTP_STREAM_ENDPOINT_STREAMING:
331                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM");
332                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING;
333                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM;
334                             break;
335                         default:
336                             log_info("ACP: AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state);
337                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
338                             connection->error_code = BAD_STATE;
339                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
340                             break;
341                     }
342                     break;
343                 case AVDTP_SI_ABORT:
344                      switch (stream_endpoint->state){
345                         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
346                         case AVDTP_STREAM_ENDPOINT_CLOSING:
347                         case AVDTP_STREAM_ENDPOINT_OPENED:
348                         case AVDTP_STREAM_ENDPOINT_STREAMING:
349                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM");
350                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
351                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM;
352                             break;
353                         default:
354                             log_info("ACP: AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state);
355                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
356                             connection->error_code = BAD_STATE;
357                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
358                             break;
359                     }
360                     break;
361                 case AVDTP_SI_SUSPEND:
362                     log_info(" entering AVDTP_SI_SUSPEND");
363                     switch (stream_endpoint->state){
364                         case AVDTP_STREAM_ENDPOINT_OPENED:
365                         case AVDTP_STREAM_ENDPOINT_STREAMING:
366                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
367                             connection->num_suspended_seids--;
368                             if (connection->num_suspended_seids <= 0){
369                                 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM");
370                                 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM;
371                             }
372                             break;
373                         default:
374                             log_info("ACP: AVDTP_SI_SUSPEND, bad state ");
375                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
376                             connection->error_code = BAD_STATE;
377                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
378                             break;
379                     }
380 
381                     //stream_endpoint->state = AVDTP_STREAM_ENDPOINT_SUSPENDING;
382                     //stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_SUSPEND_STREAM;
383                     break;
384                 default:
385                     log_info("ACP: NOT IMPLEMENTED, Reject signal_identifier %02x", connection->signaling_packet.signal_identifier);
386                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
387                     connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
388                     break;
389             }
390             break;
391         default:
392             return;
393     }
394 
395     if (!request_to_send){
396         log_info("ACP: NOT IMPLEMENTED");
397     }
398     avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
399 }
400 
401 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){
402     uint8_t command[2+2*AVDTP_MAX_NUM_SEPS];
403     int pos = 0;
404     command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
405     command[pos++] = (uint8_t)AVDTP_SI_DISCOVER;
406 
407     btstack_linked_list_iterator_t it;
408     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints);
409     while (btstack_linked_list_iterator_has_next(&it)){
410         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
411         command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1);
412         command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3);
413     }
414     return l2cap_send(cid, command, pos);
415 }
416 
417 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){
418     uint8_t command[4];
419     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
420     command[1] = (uint8_t)identifier;
421     command[2] = category;
422     command[3] = error_code;
423     return l2cap_send(cid, command, sizeof(command));
424 }
425 
426 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
427     uint8_t command[2];
428     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG);
429     command[1] = (uint8_t)identifier;
430     return l2cap_send(cid, command, sizeof(command));
431 }
432 
433 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
434     uint8_t command[2];
435     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
436     command[1] = (uint8_t)identifier;
437     return l2cap_send(cid, command, sizeof(command));
438 }
439 
440 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){
441     uint8_t command[3];
442     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
443     command[1] = (uint8_t)identifier;
444     command[2] = error_code;
445     return l2cap_send(cid, command, sizeof(command));
446 }
447 
448 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_context_t * context){
449     int sent = 1;
450     btstack_linked_list_t * stream_endpoints = &context->stream_endpoints;
451 
452     switch (connection->acceptor_connection_state){
453         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS:
454             connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
455             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
456             avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints);
457             break;
458         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
459             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
460             avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label);
461             break;
462         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
463             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
464             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);
465             break;
466         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE:
467             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
468             avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label);
469             break;
470         default:
471             sent = 0;
472             break;
473     }
474     if (sent){
475         log_info("ACP: DONE");
476         return;
477     }
478 
479     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
480     if (!stream_endpoint) return;
481 
482     uint8_t reject_service_category = connection->reject_service_category;
483     avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier;
484     uint8_t error_code = connection->error_code;
485     uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid;
486     uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label;
487 
488     avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state;
489     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
490     uint8_t * out_buffer;
491     uint16_t pos;
492 
493     switch (acceptor_config_state){
494         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
495             break;
496         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES:
497             avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES);
498             l2cap_reserve_packet_buffer();
499             out_buffer = l2cap_get_outgoing_buffer();
500             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
501             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
502                 stream_endpoint->acceptor_config_state = acceptor_config_state;
503                 log_info("ACP: fragmented");
504             } else {
505                 log_info("ACP:DONE");
506             }
507             l2cap_send_prepared(cid, pos);
508             break;
509         case AVDTP_ACCEPTOR_W2_ANSWER_DELAY_REPORT:
510             log_info("ACP: DONE ");
511             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_DELAYREPORT);
512             break;
513         case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES:
514             avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES);
515             l2cap_reserve_packet_buffer();
516             out_buffer = l2cap_get_outgoing_buffer();
517             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
518             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
519                 stream_endpoint->acceptor_config_state = acceptor_config_state;
520                 log_info("ACP: fragmented");
521             } else {
522                 log_info("ACP:DONE");
523             }
524             l2cap_send_prepared(cid, pos);
525             break;
526         case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION:
527             log_info("ACP: DONE");
528             log_info("    -> AVDTP_STREAM_ENDPOINT_CONFIGURED");
529             stream_endpoint->connection = connection;
530             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
531             // TODO: consider reconfiguration
532             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION);
533             break;
534         case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE:
535             log_info("ACP: DONE ");
536             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE);
537             break;
538 
539         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{
540             avdtp_sep_t sep = stream_endpoint->remote_sep;
541             avdtp_prepare_capabilities(&connection->signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION);
542             l2cap_reserve_packet_buffer();
543             out_buffer = l2cap_get_outgoing_buffer();
544             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
545             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
546                 stream_endpoint->acceptor_config_state = acceptor_config_state;
547                 log_info("ACP: fragmented");
548             } else {
549                 log_info("ACP:DONE");
550             }
551             l2cap_send_prepared(cid, pos);
552             break;
553         }
554         case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM:
555             log_info("ACP: DONE");
556             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN);
557             break;
558         case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM:
559             log_info("ACP: DONE ");
560             log_info("    -> AVDTP_STREAM_ENDPOINT_STREAMING ");
561             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
562             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START);
563             break;
564         case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM:
565             log_info("ACP: DONE");
566             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE);
567             break;
568         case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM:
569             log_info("ACP: DONE");
570             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT);
571             break;
572         case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM:
573             log_info("ACP: DONE");
574             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
575             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND);
576             break;
577         case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD:
578             log_info("ACP: DONE REJECT");
579             connection->reject_signal_identifier = AVDTP_SI_NONE;
580             avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid);
581             break;
582         case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
583             log_info("ACP: DONE REJECT CATEGORY");
584             connection->reject_service_category = 0;
585             avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid);
586             break;
587         case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
588             log_info("ACP: DONE REJECT");
589             connection->reject_signal_identifier = AVDTP_SI_NONE;
590             connection->error_code = 0;
591             avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid);
592             break;
593         default:
594             log_info("ACP: NOT IMPLEMENTED");
595             sent = 0;
596             break;
597     }
598     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
599     // check fragmentation
600     if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
601         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
602     }
603 }
604