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