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