xref: /btstack/src/classic/avdtp_acceptor.c (revision 096e646974112af5a72ec55d41f9fe65faa21f51)
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 BLUEKITCHEN
24  * GMBH 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 <string.h>
41 
42 #include "classic/avdtp.h"
43 #include "classic/avdtp_util.h"
44 #include "classic/avdtp_acceptor.h"
45 
46 #include "btstack_debug.h"
47 #include "btstack_util.h"
48 #include "l2cap.h"
49 
50 
51 static int avdtp_acceptor_send_accept_response(uint16_t cid,  uint8_t transaction_label, avdtp_signal_identifier_t identifier){
52     uint8_t command[2];
53     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
54     command[1] = (uint8_t)identifier;
55     return l2cap_send(cid, command, sizeof(command));
56 }
57 
58 // returns true if command complete
59 static bool avdtp_acceptor_process_chunk(avdtp_signaling_packet_t * signaling_packet, uint8_t * packet, uint16_t size){
60     if ((signaling_packet->size + size) >= sizeof(signaling_packet->command)) {
61         log_info("Dropping incoming data, doesn't fit into command buffer");
62         signaling_packet->size = 0;
63         return false;
64     }
65 
66     (void)memcpy(signaling_packet->command + signaling_packet->size, packet, size);
67     signaling_packet->size += size;
68     return (signaling_packet->packet_type == AVDTP_SINGLE_PACKET) || (signaling_packet->packet_type == AVDTP_END_PACKET);
69 }
70 
71 static int avdtp_acceptor_validate_msg_length(avdtp_signal_identifier_t signal_identifier, uint16_t msg_size){
72     int minimal_msg_lenght = 2;
73     switch (signal_identifier){
74         case AVDTP_SI_GET_CAPABILITIES:
75         case AVDTP_SI_GET_ALL_CAPABILITIES:
76         case AVDTP_SI_SET_CONFIGURATION:
77         case AVDTP_SI_GET_CONFIGURATION:
78         case AVDTP_SI_START:
79         case AVDTP_SI_CLOSE:
80         case AVDTP_SI_ABORT:
81         case AVDTP_SI_RECONFIGURE:
82         case AVDTP_SI_OPEN:
83             minimal_msg_lenght = 3;
84             break;
85         default:
86             break;
87         }
88     return msg_size >= minimal_msg_lenght;
89 }
90 
91 static void
92 avdtp_acceptor_handle_configuration_command(avdtp_connection_t *connection, int offset, uint16_t packet_size, avdtp_stream_endpoint_t *stream_endpoint) {
93     log_info("W2_ANSWER_SET_CONFIGURATION cid 0x%02x", connection->avdtp_cid);
94     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE;
95     stream_endpoint->connection = connection;
96 
97     // process capabilities, first rejected service category is stored in connection
98     connection->reject_service_category = 0;
99     avdtp_sep_t sep;
100     sep.seid = connection->acceptor_signaling_packet.command[offset++] >> 2;
101     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, connection->acceptor_signaling_packet.signal_identifier, &sep.configuration, connection->acceptor_signaling_packet.command+offset, packet_size-offset);
102     sep.in_use = 1;
103 
104     // test if sep already in use
105     if (stream_endpoint->sep.in_use != 0){
106         log_info("stream endpoint already in use");
107         connection->error_code = AVDTP_ERROR_CODE_SEP_IN_USE;
108         connection->reject_service_category = 0;
109     }
110 
111     // let application validate media configuration as well
112     if (connection->error_code == 0){
113         if ((sep.configured_service_categories & (1 << AVDTP_MEDIA_CODEC)) != 0){
114             const adtvp_media_codec_capabilities_t * media = &sep.configuration.media_codec;
115             uint8_t error_code = avdtp_validate_media_configuration(stream_endpoint, connection->avdtp_cid, 0, media);
116             if (error_code != 0){
117                 log_info("media codec rejected by validator, error 0x%02x", error_code);
118                 connection->reject_service_category = AVDTP_MEDIA_CODEC;
119                 connection->error_code              = error_code;
120             }
121         }
122     }
123 
124     if (connection->error_code){
125         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
126         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
127         return;
128     }
129 
130     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_SET_CONFIGURATION;
131     // find or add sep
132 
133     log_info("local seid %d, remote seid %d", connection->acceptor_local_seid, sep.seid);
134 
135     if (is_avdtp_remote_seid_registered(stream_endpoint)){
136         if (stream_endpoint->remote_sep.in_use){
137             log_info("remote seid already in use");
138             connection->error_code = AVDTP_ERROR_CODE_SEP_IN_USE;
139             // find first registered category and fire the error
140             connection->reject_service_category = 0;
141             int i;
142             for (i = 1; i < 9; i++){
143                 if (get_bit16(sep.configured_service_categories, i)){
144                     connection->reject_service_category = i;
145                     break;
146                 }
147             }
148             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
149             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
150             return;
151         } else {
152             stream_endpoint->remote_sep = sep;
153             log_info("update remote seid %d", stream_endpoint->remote_sep.seid);
154         }
155     } else {
156         // add new
157         stream_endpoint->remote_sep = sep;
158         log_info("add remote seid %d", stream_endpoint->remote_sep.seid);
159     }
160 
161     // mark as in_use
162     stream_endpoint->sep.in_use = 1;
163 
164 	// if media codec configuration set, copy configuration and emit event
165 	if ((sep.configured_service_categories & (1 << AVDTP_MEDIA_CODEC)) != 0){
166 		if  (stream_endpoint->media_codec_configuration_len == sep.configuration.media_codec.media_codec_information_len){
167             (void) memcpy(stream_endpoint->media_codec_configuration_info, sep.configuration.media_codec.media_codec_information, stream_endpoint->media_codec_configuration_len);
168             // update media codec info to point to user configuration
169             stream_endpoint->remote_sep.configuration.media_codec.media_codec_information = stream_endpoint->media_codec_configuration_info;
170             // emit event
171             avdtp_signaling_emit_configuration(stream_endpoint, connection->avdtp_cid, 0, &sep.configuration, sep.configured_service_categories);
172 		}
173 	}
174 
175     avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
176                                 connection->acceptor_signaling_packet.signal_identifier, false);
177 }
178 
179 void avdtp_acceptor_stream_config_subsm(avdtp_connection_t *connection, uint8_t *packet, uint16_t size, int offset) {
180     avdtp_stream_endpoint_t * stream_endpoint = NULL;
181     connection->acceptor_transaction_label = connection->acceptor_signaling_packet.transaction_label;
182     if (!avdtp_acceptor_validate_msg_length(connection->acceptor_signaling_packet.signal_identifier, size)) {
183         connection->error_code = AVDTP_ERROR_CODE_BAD_LENGTH;
184         connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
185         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
186 		avdtp_request_can_send_now_acceptor(connection);
187         return;
188     }
189 
190     // handle error cases
191     switch (connection->acceptor_signaling_packet.signal_identifier){
192         case AVDTP_SI_DISCOVER:
193             if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
194             log_info("W2_ANSWER_DISCOVER_SEPS");
195             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS;
196 			avdtp_request_can_send_now_acceptor(connection);
197             return;
198         case AVDTP_SI_GET_CAPABILITIES:
199         case AVDTP_SI_GET_ALL_CAPABILITIES:
200         case AVDTP_SI_SET_CONFIGURATION:
201         case AVDTP_SI_GET_CONFIGURATION:
202         case AVDTP_SI_START:
203         case AVDTP_SI_CLOSE:
204         case AVDTP_SI_ABORT:
205         case AVDTP_SI_OPEN:
206         case AVDTP_SI_RECONFIGURE:
207         case AVDTP_SI_DELAYREPORT:
208             connection->acceptor_local_seid  = packet[offset++] >> 2;
209             stream_endpoint = avdtp_get_stream_endpoint_for_seid(connection->acceptor_local_seid);
210             if (!stream_endpoint){
211                 log_info("cmd %d - REJECT", connection->acceptor_signaling_packet.signal_identifier);
212                 connection->error_code = AVDTP_ERROR_CODE_BAD_ACP_SEID;
213                 if (connection->acceptor_signaling_packet.signal_identifier == AVDTP_SI_OPEN){
214                     connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
215                 }
216 
217                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
218                 if (connection->acceptor_signaling_packet.signal_identifier == AVDTP_SI_RECONFIGURE){
219                     connection->reject_service_category = connection->acceptor_local_seid;
220                     connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
221                 }
222                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
223 				avdtp_request_can_send_now_acceptor(connection);
224                 return;
225             }
226             break;
227 
228         case AVDTP_SI_SUSPEND:{
229             int i;
230             log_info("AVDTP_SI_SUSPEND");
231             connection->num_suspended_seids = 0;
232 
233             for (i = offset; i < size; i++){
234                 connection->suspended_seids[connection->num_suspended_seids] = packet[i] >> 2;
235                 offset++;
236                 log_info("%d, ", connection->suspended_seids[connection->num_suspended_seids]);
237                 connection->num_suspended_seids++;
238             }
239 
240             if (connection->num_suspended_seids == 0) {
241                 log_info("no susspended seids, BAD_ACP_SEID");
242                 connection->error_code = AVDTP_ERROR_CODE_BAD_ACP_SEID;
243                 connection->reject_service_category = connection->acceptor_local_seid;
244                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
245                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
246 				avdtp_request_can_send_now_acceptor(connection);
247                 return;
248             }
249             // deal with first susspended seid
250             connection->acceptor_local_seid = connection->suspended_seids[0];
251             stream_endpoint = avdtp_get_stream_endpoint_for_seid(connection->acceptor_local_seid);
252             if (!stream_endpoint){
253                 log_info("stream_endpoint not found, BAD_ACP_SEID");
254                 connection->error_code = AVDTP_ERROR_CODE_BAD_ACP_SEID;
255                 connection->reject_service_category = connection->acceptor_local_seid;
256                 connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
257                 connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
258                 connection->num_suspended_seids = 0;
259 				avdtp_request_can_send_now_acceptor(connection);
260                 return;
261             }
262             break;
263         }
264         default:
265             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE;
266             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
267             log_info("AVDTP_CMD_MSG signal %d not implemented, general reject", connection->acceptor_signaling_packet.signal_identifier);
268 			avdtp_request_can_send_now_acceptor(connection);
269             return;
270     }
271 
272     btstack_assert(stream_endpoint != NULL);
273 
274     bool command_complete = avdtp_acceptor_process_chunk(&connection->acceptor_signaling_packet, packet, size);
275     if (!command_complete) return;
276 
277     uint16_t packet_size = connection->acceptor_signaling_packet.size;
278     connection->acceptor_signaling_packet.size = 0;
279 
280     int request_to_send = 1;
281     switch (stream_endpoint->acceptor_config_state){
282         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
283             switch (connection->acceptor_signaling_packet.signal_identifier){
284                 case AVDTP_SI_DELAYREPORT:
285                     log_info("W2_ANSWER_DELAY_REPORT, local seid %d", connection->acceptor_local_seid);
286                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_DELAY_REPORT;
287                     avdtp_signaling_emit_delay(connection->avdtp_cid, connection->acceptor_local_seid,
288                                                big_endian_read_16(packet, offset));
289                     break;
290 
291                 case AVDTP_SI_GET_ALL_CAPABILITIES:
292                     log_info("AVDTP_SI_GET_ALL_CAPABILITIES, local seid %d", connection->acceptor_local_seid);
293                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_GET_ALL_CAPABILITIES;
294                     break;
295                 case AVDTP_SI_GET_CAPABILITIES:
296                     log_info("W2_ANSWER_GET_CAPABILITIES, local seid %d", connection->acceptor_local_seid);
297                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_GET_CAPABILITIES;
298                     break;
299                 case AVDTP_SI_SET_CONFIGURATION:{
300                     log_info("Received SET_CONFIGURATION cmd: config state %d", connection->configuration_state);
301                     switch (connection->configuration_state){
302                         case AVDTP_CONFIGURATION_STATE_IDLE:
303                             avdtp_acceptor_handle_configuration_command(connection, offset, packet_size,
304                                                                         stream_endpoint);
305                             connection->configuration_state = AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED;
306                             break;
307                         case AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED:
308                         case AVDTP_CONFIGURATION_STATE_REMOTE_INITIATED:
309                             log_info("Reject SET_CONFIGURATION BAD_STATE %d", connection->configuration_state);
310                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
311                             connection->reject_service_category = 0;
312                             connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
313                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
314                             break;
315                         case AVDTP_CONFIGURATION_STATE_LOCAL_CONFIGURED:
316                         case AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED:
317                             log_info("Reject SET_CONFIGURATION SEP_IN_USE");
318                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
319                             connection->reject_service_category = 0;
320                             connection->error_code = AVDTP_ERROR_CODE_SEP_IN_USE;
321                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
322                             break;
323                         default:
324                             break;
325                     }
326                     break;
327                 }
328                 case AVDTP_SI_RECONFIGURE:{
329                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_RECONFIGURE;
330                     connection->reject_service_category = 0;
331 
332                     avdtp_sep_t sep;
333                     log_info("W2_ANSWER_RECONFIGURE, local seid %d, remote seid %d", connection->acceptor_local_seid, stream_endpoint->remote_sep.seid);
334                     sep.configured_service_categories = avdtp_unpack_service_capabilities(connection, connection->acceptor_signaling_packet.signal_identifier, &sep.configuration, connection->acceptor_signaling_packet.command+offset, packet_size-offset);
335                     if (connection->error_code){
336                         // fire configuration parsing errors
337                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
338                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
339                         break;
340                     }
341 
342                     // find sep or raise error
343                     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
344                         log_info("REJECT AVDTP_SI_RECONFIGURE, BAD_ACP_SEID");
345                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
346                         connection->error_code = AVDTP_ERROR_CODE_BAD_ACP_SEID;
347                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
348                         break;
349                     }
350                     stream_endpoint->remote_sep.configured_service_categories = sep.configured_service_categories;
351                     stream_endpoint->remote_sep.configuration = sep.configuration;
352 
353                     log_info("update active remote seid %d", stream_endpoint->remote_sep.seid);
354 
355 					// if media codec configuration updated, copy configuration and emit event
356 					if ((sep.configured_service_categories & (1 << AVDTP_MEDIA_CODEC)) != 0){
357 						if (stream_endpoint->media_codec_configuration_len == sep.configuration.media_codec.media_codec_information_len){
358 							(void) memcpy(stream_endpoint->media_codec_configuration_info, sep.configuration.media_codec.media_codec_information, stream_endpoint->media_codec_configuration_len);
359                             stream_endpoint->sep.configuration.media_codec = stream_endpoint->remote_configuration.media_codec;
360                             avdtp_signaling_emit_configuration(stream_endpoint, connection->avdtp_cid, 1, &sep.configuration, sep.configured_service_categories);
361 						}
362 					}
363                     break;
364                 }
365 
366                 case AVDTP_SI_GET_CONFIGURATION:
367                     log_info("W2_ANSWER_GET_CONFIGURATION");
368                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_GET_CONFIGURATION;
369                     break;
370                 case AVDTP_SI_OPEN:
371                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURED){
372                         log_info("REJECT AVDTP_SI_OPEN, BAD_STATE");
373                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
374                         connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
375                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
376                         break;
377                     }
378                     log_info("AVDTP_STREAM_ENDPOINT_W2_ANSWER_OPEN_STREAM");
379                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_OPEN_STREAM;
380                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED;
381                     connection->acceptor_local_seid = stream_endpoint->sep.seid;
382                     break;
383                 case AVDTP_SI_START:
384                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_OPENED){
385                         log_info("REJECT AVDTP_SI_START, BAD_STATE, state %d", stream_endpoint->state);
386                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
387                         connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
388                         connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
389                         break;
390                     }
391 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
392                     log_info("W2_ACCEPT_START_STREAM");
393                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM;
394 #else
395                     log_info("W2_ANSWER_START_STREAM");
396                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_START_STREAM;
397 #endif
398                     break;
399                 case AVDTP_SI_CLOSE:
400                     switch (stream_endpoint->state){
401                         case AVDTP_STREAM_ENDPOINT_OPENED:
402                         case AVDTP_STREAM_ENDPOINT_STREAMING:
403                             log_info("W2_ANSWER_CLOSE_STREAM");
404                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CLOSING;
405                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_CLOSE_STREAM;
406                             break;
407                         default:
408                             log_info("AVDTP_SI_CLOSE, bad state %d ", stream_endpoint->state);
409                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
410                             connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
411                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
412                             break;
413                     }
414                     break;
415                 case AVDTP_SI_ABORT:
416                      switch (stream_endpoint->state){
417                         case AVDTP_STREAM_ENDPOINT_CONFIGURED:
418                         case AVDTP_STREAM_ENDPOINT_CLOSING:
419                         case AVDTP_STREAM_ENDPOINT_OPENED:
420                         case AVDTP_STREAM_ENDPOINT_STREAMING:
421                             log_info("W2_ANSWER_ABORT_STREAM");
422                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_ABORTING;
423                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_ABORT_STREAM;
424                             break;
425                         default:
426                             log_info("AVDTP_SI_ABORT, bad state %d ", stream_endpoint->state);
427                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE;
428                             connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
429                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
430                             break;
431                     }
432                     break;
433                 case AVDTP_SI_SUSPEND:
434                     switch (stream_endpoint->state){
435                         case AVDTP_STREAM_ENDPOINT_OPENED:
436                         case AVDTP_STREAM_ENDPOINT_STREAMING:
437                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
438                             connection->num_suspended_seids--;
439                             if (connection->num_suspended_seids <= 0){
440                                 log_info("W2_ANSWER_SUSPEND_STREAM");
441                                 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM;
442                             }
443                             break;
444                         default:
445                             log_info("AVDTP_SI_SUSPEND, bad state %d", stream_endpoint->state);
446                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
447                             connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
448                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
449                             break;
450                     }
451                     break;
452                 default:
453                     log_info("NOT IMPLEMENTED, Reject signal_identifier %02x", connection->acceptor_signaling_packet.signal_identifier);
454                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
455                     connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
456                     break;
457             }
458             break;
459         default:
460             return;
461     }
462 
463     if (!request_to_send){
464         log_info("NOT IMPLEMENTED");
465     }
466 	avdtp_request_can_send_now_acceptor(connection);
467 }
468 
469 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){
470     uint8_t command[2+2*AVDTP_MAX_NUM_SEPS];
471     int pos = 0;
472     command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
473     command[pos++] = (uint8_t)AVDTP_SI_DISCOVER;
474 
475     btstack_linked_list_iterator_t it;
476     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints);
477     while (btstack_linked_list_iterator_has_next(&it)){
478         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
479         command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1);
480         command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3);
481     }
482     return l2cap_send(cid, command, pos);
483 }
484 
485 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){
486     uint8_t command[4];
487     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
488     command[1] = (uint8_t)identifier;
489     command[2] = category;
490     command[3] = error_code;
491     return l2cap_send(cid, command, sizeof(command));
492 }
493 
494 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
495     uint8_t command[2];
496     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG);
497     command[1] = (uint8_t)identifier;
498     return l2cap_send(cid, command, sizeof(command));
499 }
500 
501 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
502     uint8_t command[2];
503     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
504     command[1] = (uint8_t)identifier;
505     return l2cap_send(cid, command, sizeof(command));
506 }
507 
508 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){
509     uint8_t command[3];
510     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
511     command[1] = (uint8_t)identifier;
512     command[2] = error_code;
513     return l2cap_send(cid, command, sizeof(command));
514 }
515 
516 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t *connection) {
517     int sent = 1;
518     btstack_linked_list_t * stream_endpoints = avdtp_get_stream_endpoints();
519 
520     switch (connection->acceptor_connection_state){
521         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS:
522             connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
523             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
524             avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints);
525             avdtp_signaling_emit_accept(connection->avdtp_cid, 0, connection->acceptor_signaling_packet.signal_identifier, false);
526             break;
527         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
528             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
529             avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label);
530             break;
531         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
532             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
533             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);
534             break;
535         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE:
536             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
537             avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label);
538             break;
539         default:
540             sent = 0;
541             break;
542     }
543     if (sent){
544         log_info("DONE");
545         return;
546     }
547 
548     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(connection->acceptor_local_seid);
549     if (!stream_endpoint) return;
550 
551     uint8_t reject_service_category = connection->reject_service_category;
552     avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier;
553     uint8_t error_code = connection->error_code;
554     uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid;
555     uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label;
556 
557     avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state;
558     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
559     uint8_t * out_buffer;
560     uint16_t pos;
561     avdtp_sep_t * remote_sep;
562 
563     bool emit_accept = false;
564     bool emit_reject = false;
565 
566     switch (acceptor_config_state){
567         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
568             break;
569         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_CAPABILITIES:
570             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES);
571             l2cap_reserve_packet_buffer();
572             out_buffer = l2cap_get_outgoing_buffer();
573             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
574             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
575                 stream_endpoint->acceptor_config_state = acceptor_config_state;
576                 log_info("fragmented");
577             } else {
578                 log_info("ACP:DONE");
579                 emit_accept = true;
580             }
581             l2cap_send_prepared(cid, pos);
582             break;
583         case AVDTP_ACCEPTOR_W2_ACCEPT_DELAY_REPORT:
584             log_info("DONE ");
585             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_DELAYREPORT);
586             emit_accept = true;
587             break;
588         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_ALL_CAPABILITIES:
589             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES);
590             l2cap_reserve_packet_buffer();
591             out_buffer = l2cap_get_outgoing_buffer();
592             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
593             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
594                 stream_endpoint->acceptor_config_state = acceptor_config_state;
595                 log_info("fragmented");
596             } else {
597                 log_info("ACP:DONE");
598                 emit_accept = true;
599             }
600             l2cap_send_prepared(cid, pos);
601             break;
602         case AVDTP_ACCEPTOR_W2_ACCEPT_SET_CONFIGURATION:
603             log_info("DONE");
604             log_info("    -> AVDTP_STREAM_ENDPOINT_CONFIGURED");
605             stream_endpoint->connection = connection;
606             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
607             connection->configuration_state = AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED;
608             // TODO: consider reconfiguration
609             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION);
610             emit_accept = true;
611             break;
612         case AVDTP_ACCEPTOR_W2_ACCEPT_RECONFIGURE:
613             log_info("DONE ");
614             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE);
615             emit_accept = true;
616             break;
617         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_CONFIGURATION:
618             remote_sep = &stream_endpoint->remote_sep;
619             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, remote_sep->configured_service_categories, remote_sep->configuration, AVDTP_SI_GET_CONFIGURATION);
620             l2cap_reserve_packet_buffer();
621             out_buffer = l2cap_get_outgoing_buffer();
622             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
623             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
624                 stream_endpoint->acceptor_config_state = acceptor_config_state;
625                 log_info("fragmented");
626             } else {
627                 log_info("ACP:DONE");
628                 emit_accept = true;
629             }
630             l2cap_send_prepared(cid, pos);
631             break;
632         case AVDTP_ACCEPTOR_W2_ACCEPT_OPEN_STREAM:
633             log_info("DONE");
634             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN);
635             emit_accept = true;
636             break;
637 
638 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
639         case AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM:
640             // keep state until user calls API to confirm or reject starting the stream
641             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM;
642             avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint), AVDTP_SI_ACCEPT_START, false);
643             break;
644         case AVDTP_ACCEPTOR_W2_REJECT_START_STREAM:
645             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
646             connection->acceptor_signaling_packet.signal_identifier = AVDTP_SI_START;
647             emit_reject = true;
648             avdtp_acceptor_send_response_reject(cid, AVDTP_SI_START, trid);
649             break;
650 #endif
651         case AVDTP_ACCEPTOR_W2_ACCEPT_START_STREAM:
652             log_info("DONE ");
653             log_info("    -> AVDTP_STREAM_ENDPOINT_STREAMING ");
654             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
655             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START);
656             emit_accept = true;
657             break;
658         case AVDTP_ACCEPTOR_W2_ACCEPT_CLOSE_STREAM:
659             log_info("DONE");
660             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE);
661             connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE;
662             emit_accept = true;
663             break;
664         case AVDTP_ACCEPTOR_W2_ACCEPT_ABORT_STREAM:
665             log_info("DONE");
666             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT);
667             emit_accept = true;
668             break;
669         case AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM:
670             log_info("DONE");
671             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
672             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND);
673             emit_accept = true;
674             break;
675         case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD:
676             log_info("DONE REJECT");
677             connection->reject_signal_identifier = AVDTP_SI_NONE;
678             avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid);
679             emit_reject = true;
680             break;
681         case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
682             log_info("DONE REJECT CATEGORY");
683             connection->reject_service_category = 0;
684             avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid);
685             emit_reject = true;
686             break;
687         case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
688             log_info("DONE REJECT");
689             connection->reject_signal_identifier = AVDTP_SI_NONE;
690             connection->error_code = 0;
691             avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid);
692             emit_reject = true;
693             break;
694         default:
695             log_info("NOT IMPLEMENTED");
696             sent = 0;
697             break;
698     }
699 
700     if (emit_accept == true){
701         avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
702                                     connection->acceptor_signaling_packet.signal_identifier, false);
703     } else if (emit_reject == true){
704         avdtp_signaling_emit_reject(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
705                                     connection->acceptor_signaling_packet.signal_identifier, false);
706     }
707     // check fragmentation
708     if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
709 		avdtp_request_can_send_now_acceptor(connection);
710     }
711 }
712