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