xref: /btstack/src/classic/avdtp_acceptor.c (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 #define __BTSTACK_FILE__ "avdtp_acceptor.c"
38 
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "btstack.h"
45 #include "classic/avdtp.h"
46 #include "classic/avdtp_util.h"
47 #include "classic/avdtp_acceptor.h"
48 
49 
50 static int avdtp_acceptor_send_accept_response(uint16_t cid,  uint8_t transaction_label, avdtp_signal_identifier_t identifier){
51     uint8_t command[2];
52     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
53     command[1] = (uint8_t)identifier;
54     return l2cap_send(cid, command, sizeof(command));
55 }
56 
57 static int avdtp_acceptor_process_chunk(avdtp_signaling_packet_t * signaling_packet, uint8_t * packet, uint16_t size){
58     memcpy(signaling_packet->command + signaling_packet->size, packet, size);
59     signaling_packet->size += size;
60     return signaling_packet->packet_type == AVDTP_SINGLE_PACKET || signaling_packet->packet_type == AVDTP_END_PACKET;
61 }
62 
63 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){
64     int minimal_msg_lenght = 2;
65     switch (signal_identifier){
66         case AVDTP_SI_GET_CAPABILITIES:
67         case AVDTP_SI_GET_ALL_CAPABILITIES:
68         case AVDTP_SI_SET_CONFIGURATION:
69         case AVDTP_SI_GET_CONFIGURATION:
70         case AVDTP_SI_START:
71         case AVDTP_SI_CLOSE:
72         case AVDTP_SI_ABORT:
73         case AVDTP_SI_RECONFIGURE:
74         case AVDTP_SI_OPEN:
75             minimal_msg_lenght = 3;
76             break;
77         default:
78             break;
79         }
80     return msg_size >= minimal_msg_lenght;
81 }
82 
83 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t * connection, uint8_t * packet, uint16_t size, int offset, avdtp_context_t * context){
84     avdtp_stream_endpoint_t * stream_endpoint;
85     connection->acceptor_transaction_label = connection->signaling_packet.transaction_label;
86     if (!avdtp_acceptor_validate_msg_length(connection->signaling_packet.signal_identifier, size)) {
87         connection->error_code = BAD_LENGTH;
88         connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
89         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
90         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
91         return;
92     }
93 
94     switch (connection->signaling_packet.signal_identifier){
95         case AVDTP_SI_DISCOVER:
96             if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
97             log_info("ACP: AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS");
98             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS;
99             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
100             return;
101         case AVDTP_SI_GET_CAPABILITIES:
102         case AVDTP_SI_GET_ALL_CAPABILITIES:
103         case AVDTP_SI_SET_CONFIGURATION:
104         case AVDTP_SI_GET_CONFIGURATION:
105         case AVDTP_SI_START:
106         case AVDTP_SI_CLOSE:
107         case AVDTP_SI_ABORT:
108         case AVDTP_SI_OPEN:
109         case AVDTP_SI_RECONFIGURE:
110             connection->local_seid  = packet[offset++] >> 2;
111             stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
112             if (!stream_endpoint){
113                 log_info("ACP: cmd %d - RESPONSE REJECT", connection->signaling_packet.signal_identifier);
114                 connection->error_code = BAD_ACP_SEID;
115                 if (connection->signaling_packet.signal_identifier == AVDTP_SI_OPEN){
116                     connection->error_code = BAD_STATE;
117                 }
118 
119                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
120                 if (connection->signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){
121                     connection->reject_service_category = connection->local_seid;
122                     connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
123                 }
124                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
125                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
126                 return;
127             }
128             break;
129 
130         case AVDTP_SI_SUSPEND:{
131             int i;
132             log_info("ACP: AVDTP_SI_SUSPEND seids: ");
133             connection->num_suspended_seids = 0;
134 
135             for (i = offset; i < size; i++){
136                 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2;
137                 offset++;
138                 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]);
139                 connection->num_suspended_seids++;
140             }
141 
142             if (connection->num_suspended_seids == 0) {
143                 log_info("ACP: CATEGORY RESPONSE REJECT BAD_ACP_SEID");
144                 connection->error_code = BAD_ACP_SEID;
145                 connection->reject_service_category = connection->local_seid;
146                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
147                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
148                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
149                 return;
150             }
151             // deal with first susspended seid
152             connection->local_seid = connection->suspended_seids[0];
153             stream_endpoint = avdtp_stream_endpoint_with_seid(connection->local_seid, context);
154             if (!stream_endpoint){
155                 log_info("ACP: stream_endpoint not found, CATEGORY RESPONSE REJECT BAD_ACP_SEID");
156                 connection->error_code = BAD_ACP_SEID;
157                 connection->reject_service_category = connection->local_seid;
158                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
159                 connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
160                 connection->num_suspended_seids = 0;
161                 avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
162                 return;
163             }
164             break;
165         }
166         default:
167             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE;
168             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
169             log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->signaling_packet.signal_identifier);
170             avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
171             return;
172     }
173 
174     if (!stream_endpoint) {
175         return;
176     }
177 
178     if (!avdtp_acceptor_process_chunk(&connection->signaling_packet, packet, size)) return;
179 
180     uint16_t packet_size = connection->signaling_packet.size;
181     connection->signaling_packet.size = 0;
182 
183     int request_to_send = 1;
184     switch (stream_endpoint->acceptor_config_state){
185         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
186             switch (connection->signaling_packet.signal_identifier){
187                 case AVDTP_SI_GET_ALL_CAPABILITIES:
188                     log_info("ACP: AVDTP_SI_GET_ALL_CAPABILITIES");
189                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES;
190                     break;
191                 case AVDTP_SI_GET_CAPABILITIES:
192                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES");
193                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES;
194                     break;
195                 case AVDTP_SI_SET_CONFIGURATION:{
196                     // log_info("acceptor SM received SET_CONFIGURATION cmd: role is_initiator %d", connection->is_initiator);
197                     if (connection->is_initiator){
198                         if (connection->is_configuration_initiated_locally){
199                             log_info("ACP: Set configuration already initiated locally, reject cmd ");
200                             // fire configuration parsing errors
201                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
202                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
203                             connection->is_initiator = 1;
204                             break;
205                         }
206                         connection->is_initiator = 0;
207                         log_info("acceptor SM received SET_CONFIGURATION cmd: change role to acceptor, is_initiator %d", connection->is_initiator);
208                     }
209 
210                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION connection %p", connection);
211                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE;
212                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION;
213                     connection->reject_service_category = 0;
214                     stream_endpoint->connection = connection;
215                     avdtp_sep_t sep;
216                     sep.seid = connection->signaling_packet.command[offset++] >> 2;
217                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset);
218                     sep.in_use = 1;
219 
220                     if (connection->error_code){
221                         log_info("fire configuration parsing errors ");
222                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
223                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
224                         break;
225                     }
226                     // find or add sep
227 
228                     log_info("ACP .. seid %d, remote sep seid %d", sep.seid, stream_endpoint->remote_sep.seid);
229 
230                     if (is_avdtp_remote_seid_registered(stream_endpoint)){
231                         if (stream_endpoint->remote_sep.in_use){
232                             log_info("reject as it is already in use");
233                             connection->error_code = SEP_IN_USE;
234                             // find first registered category and fire the error
235                             connection->reject_service_category = 0;
236                             int i;
237                             for (i = 1; i < 9; i++){
238                                 if (get_bit16(sep.configured_service_categories, i)){
239                                     connection->reject_service_category = i;
240                                     break;
241                                 }
242                             }
243                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
244                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
245                         } else {
246                             stream_endpoint->remote_sep = sep;
247                             log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
248                         }
249                     } else {
250                         // add new
251                         log_info("ACP: seid %d not found in %p", sep.seid, stream_endpoint);
252                         stream_endpoint->remote_sep = sep;
253                         log_info("ACP: add seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
254                     }
255 
256                     avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories);
257                     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
258                     break;
259                 }
260                 case AVDTP_SI_RECONFIGURE:{
261                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE;
262                     connection->reject_service_category = 0;
263 
264                     avdtp_sep_t sep;
265                     sep.seid = connection->local_seid;
266                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE seid %d", sep.seid);
267                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, &sep.configuration, connection->signaling_packet.command+offset, packet_size-offset);
268                     if (connection->error_code){
269                         // fire configuration parsing errors
270                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
271                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
272                         break;
273                     }
274 
275                     // find sep or raise error
276                     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
277                         log_info("ACP: REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID");
278                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
279                         connection->error_code = BAD_ACP_SEID;
280                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
281                         break;
282                     }
283                     stream_endpoint->remote_sep = sep;
284                     log_info("ACP: update seid %d, to %p", stream_endpoint->remote_sep.seid, stream_endpoint);
285 
286                     avdtp_emit_configuration(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), &sep.configuration, sep.configured_service_categories);
287                     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
288                     break;
289                 }
290 
291                 case AVDTP_SI_GET_CONFIGURATION:
292                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION");
293                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION;
294                     break;
295                 case AVDTP_SI_OPEN:
296                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){
297                         log_info("ACP: REJECT AVDTP_SI_OPEN, BAD_STATE");
298                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
299                         connection->error_code = BAD_STATE;
300                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
301                         break;
302                     }
303                     log_info("ACP: AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM");
304                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM;
305                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED;
306                     connection->local_seid = stream_endpoint->sep.seid;
307                     break;
308                 case AVDTP_SI_START:
309                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){
310                         log_info("ACP: REJECT AVDTP_SI_START, BAD_STATE, state %d", stream_endpoint->state);
311                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
312                         connection->error_code = BAD_STATE;
313                         connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
314                         break;
315                     }
316                     log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM");
317                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM;
318                     break;
319                 case AVDTP_SI_CLOSE:
320                     switch (stream_endpoint->state){
321                         case AVDTP_STREAM_ENDPOINT_OPENED:
322                         case AVDTP_STREAM_ENDPOINT_STREAMING:
323                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM");
324                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING;
325                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM;
326                             break;
327                         default:
328                             log_info("ACP: AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state);
329                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
330                             connection->error_code = BAD_STATE;
331                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
332                             break;
333                     }
334                     break;
335                 case AVDTP_SI_ABORT:
336                      switch (stream_endpoint->state){
337                         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
338                         case AVDTP_STREAM_ENDPOINT_CLOSING:
339                         case AVDTP_STREAM_ENDPOINT_OPENED:
340                         case AVDTP_STREAM_ENDPOINT_STREAMING:
341                             log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM");
342                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
343                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM;
344                             break;
345                         default:
346                             log_info("ACP: AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state);
347                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
348                             connection->error_code = BAD_STATE;
349                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
350                             break;
351                     }
352                     break;
353                 case AVDTP_SI_SUSPEND:
354                     log_info(" entering AVDTP_SI_SUSPEND");
355                     switch (stream_endpoint->state){
356                         case AVDTP_STREAM_ENDPOINT_OPENED:
357                         case AVDTP_STREAM_ENDPOINT_STREAMING:
358                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
359                             connection->num_suspended_seids--;
360                             if (connection->num_suspended_seids <= 0){
361                                 log_info("ACP: AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM");
362                                 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM;
363                             }
364                             break;
365                         default:
366                             log_info("ACP: AVDTP_SI_SUSPEND, bad state ");
367                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
368                             connection->error_code = BAD_STATE;
369                             connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
370                             break;
371                     }
372 
373                     //stream_endpoint->state = AVDTP_STREAM_ENDPOINT_SUSPENDING;
374                     //stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_SUSPEND_STREAM;
375                     break;
376                 default:
377                     log_info("ACP: NOT IMPLEMENTED, Reject signal_identifier %02x", connection->signaling_packet.signal_identifier);
378                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
379                     connection->reject_signal_identifier = connection->signaling_packet.signal_identifier;
380                     break;
381             }
382             break;
383         default:
384             return;
385     }
386 
387     if (!request_to_send){
388         log_info("ACP: NOT IMPLEMENTED");
389     }
390     avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
391 }
392 
393 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){
394     uint8_t command[2+2*AVDTP_MAX_NUM_SEPS];
395     int pos = 0;
396     command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
397     command[pos++] = (uint8_t)AVDTP_SI_DISCOVER;
398 
399     btstack_linked_list_iterator_t it;
400     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints);
401     while (btstack_linked_list_iterator_has_next(&it)){
402         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
403         command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1);
404         command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3);
405     }
406     return l2cap_send(cid, command, pos);
407 }
408 
409 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){
410     uint8_t command[4];
411     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
412     command[1] = (uint8_t)identifier;
413     command[2] = category;
414     command[3] = error_code;
415     return l2cap_send(cid, command, sizeof(command));
416 }
417 
418 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
419     uint8_t command[2];
420     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG);
421     command[1] = (uint8_t)identifier;
422     return l2cap_send(cid, command, sizeof(command));
423 }
424 
425 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
426     uint8_t command[2];
427     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
428     command[1] = (uint8_t)identifier;
429     return l2cap_send(cid, command, sizeof(command));
430 }
431 
432 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){
433     uint8_t command[3];
434     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
435     command[1] = (uint8_t)identifier;
436     command[2] = error_code;
437     return l2cap_send(cid, command, sizeof(command));
438 }
439 
440 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t * connection, avdtp_context_t * context){
441     int sent = 1;
442     btstack_linked_list_t * stream_endpoints = &context->stream_endpoints;
443 
444     switch (connection->acceptor_connection_state){
445         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS:
446             connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
447             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
448             avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints);
449             break;
450         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
451             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
452             avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label);
453             break;
454         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
455             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
456             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);
457             break;
458         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE:
459             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
460             avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label);
461             break;
462         default:
463             sent = 0;
464             break;
465     }
466     if (sent){
467         log_info("ACP: DONE");
468         return;
469     }
470 
471     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
472     if (!stream_endpoint) return;
473 
474     uint8_t reject_service_category = connection->reject_service_category;
475     avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier;
476     uint8_t error_code = connection->error_code;
477     uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid;
478     uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label;
479 
480     avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state;
481     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
482     uint8_t * out_buffer;
483     uint16_t pos;
484 
485     switch (acceptor_config_state){
486         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
487             break;
488         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CAPABILITIES:
489             avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES);
490             l2cap_reserve_packet_buffer();
491             out_buffer = l2cap_get_outgoing_buffer();
492             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
493             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
494                 stream_endpoint->acceptor_config_state = acceptor_config_state;
495                 log_info("ACP: fragmented");
496             } else {
497                 log_info("ACP:DONE");
498             }
499             l2cap_send_prepared(cid, pos);
500             break;
501         case AVDTP_ACCEPTOR_W2_ANSWER_GET_ALL_CAPABILITIES:
502             avdtp_prepare_capabilities(&connection->signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES);
503             l2cap_reserve_packet_buffer();
504             out_buffer = l2cap_get_outgoing_buffer();
505             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
506             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
507                 stream_endpoint->acceptor_config_state = acceptor_config_state;
508                 log_info("ACP: fragmented");
509             } else {
510                 log_info("ACP:DONE");
511             }
512             l2cap_send_prepared(cid, pos);
513             break;
514         case AVDTP_ACCEPTOR_W2_ANSWER_SET_CONFIGURATION:
515             log_info("ACP: DONE");
516             log_info("    -> AVDTP_STREAM_ENDPOINT_CONFIGURED");
517             stream_endpoint->connection = connection;
518             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
519             // TODO: consider reconfiguration
520             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION);
521             break;
522         case AVDTP_ACCEPTOR_W2_ANSWER_RECONFIGURE:
523             log_info("ACP: DONE ");
524             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE);
525             break;
526 
527         case AVDTP_ACCEPTOR_W2_ANSWER_GET_CONFIGURATION:{
528             avdtp_sep_t sep = stream_endpoint->remote_sep;
529             avdtp_prepare_capabilities(&connection->signaling_packet, trid, sep.configured_service_categories, sep.configuration, AVDTP_SI_GET_CONFIGURATION);
530             l2cap_reserve_packet_buffer();
531             out_buffer = l2cap_get_outgoing_buffer();
532             pos = avdtp_signaling_create_fragment(cid, &connection->signaling_packet, out_buffer);
533             if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
534                 stream_endpoint->acceptor_config_state = acceptor_config_state;
535                 log_info("ACP: fragmented");
536             } else {
537                 log_info("ACP:DONE");
538             }
539             l2cap_send_prepared(cid, pos);
540             break;
541         }
542         case AVDTP_ACCEPTOR_W2_ANSWER_OPEN_STREAM:
543             log_info("ACP: DONE");
544             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN);
545             break;
546         case AVDTP_ACCEPTOR_W2_ANSWER_START_STREAM:
547             log_info("ACP: DONE ");
548             log_info("    -> AVDTP_STREAM_ENDPOINT_STREAMING ");
549             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
550             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START);
551             break;
552         case AVDTP_ACCEPTOR_W2_ANSWER_CLOSE_STREAM:
553             log_info("ACP: DONE");
554             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE);
555             break;
556         case AVDTP_ACCEPTOR_W2_ANSWER_ABORT_STREAM:
557             log_info("ACP: DONE");
558             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT);
559             break;
560         case AVDTP_ACCEPTOR_W2_ANSWER_SUSPEND_STREAM:
561             log_info("ACP: DONE");
562             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
563             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND);
564             break;
565         case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD:
566             log_info("ACP: DONE REJECT");
567             connection->reject_signal_identifier = AVDTP_SI_NONE;
568             avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid);
569             break;
570         case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
571             log_info("ACP: DONE REJECT CATEGORY");
572             connection->reject_service_category = 0;
573             avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid);
574             break;
575         case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
576             log_info("ACP: DONE REJECT");
577             connection->reject_signal_identifier = AVDTP_SI_NONE;
578             connection->error_code = 0;
579             avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid);
580             break;
581         default:
582             log_info("ACP: NOT IMPLEMENTED");
583             sent = 0;
584             break;
585     }
586     avdtp_signaling_emit_accept(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint), connection->signaling_packet.signal_identifier);
587     // check fragmentation
588     if (connection->signaling_packet.packet_type != AVDTP_SINGLE_PACKET && connection->signaling_packet.packet_type != AVDTP_END_PACKET){
589         avdtp_request_can_send_now_acceptor(connection, connection->l2cap_signaling_cid);
590     }
591 }
592