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