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