xref: /btstack/src/classic/avdtp_acceptor.c (revision 77bf845768a9c39a5561c69033c0112e62f08947)
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_STREAMING:
436                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
437                             connection->num_suspended_seids--;
438                             if (connection->num_suspended_seids <= 0){
439                                 log_info("W2_ANSWER_SUSPEND_STREAM");
440                                 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM;
441                             }
442                             break;
443                         default:
444                             log_info("AVDTP_SI_SUSPEND, bad state %d", stream_endpoint->state);
445                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE;
446                             connection->error_code = AVDTP_ERROR_CODE_BAD_STATE;
447                             connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
448                             break;
449                     }
450                     break;
451                 default:
452                     log_info("NOT IMPLEMENTED, Reject signal_identifier %02x", connection->acceptor_signaling_packet.signal_identifier);
453                     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD;
454                     connection->reject_signal_identifier = connection->acceptor_signaling_packet.signal_identifier;
455                     break;
456             }
457             break;
458         default:
459             return;
460     }
461 
462     if (!request_to_send){
463         log_info("NOT IMPLEMENTED");
464     }
465 	avdtp_request_can_send_now_acceptor(connection);
466 }
467 
468 static int avdtp_acceptor_send_seps_response(uint16_t cid, uint8_t transaction_label, avdtp_stream_endpoint_t * endpoints){
469     uint8_t command[2+2*AVDTP_MAX_NUM_SEPS];
470     int pos = 0;
471     command[pos++] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_ACCEPT_MSG);
472     command[pos++] = (uint8_t)AVDTP_SI_DISCOVER;
473 
474     btstack_linked_list_iterator_t it;
475     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) endpoints);
476     while (btstack_linked_list_iterator_has_next(&it)){
477         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
478         command[pos++] = (stream_endpoint->sep.seid << 2) | (stream_endpoint->sep.in_use<<1);
479         command[pos++] = (stream_endpoint->sep.media_type << 4) | (stream_endpoint->sep.type << 3);
480     }
481     return l2cap_send(cid, command, pos);
482 }
483 
484 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){
485     uint8_t command[4];
486     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
487     command[1] = (uint8_t)identifier;
488     command[2] = category;
489     command[3] = error_code;
490     return l2cap_send(cid, command, sizeof(command));
491 }
492 
493 static int avdtp_acceptor_send_response_general_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
494     uint8_t command[2];
495     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_GENERAL_REJECT_MSG);
496     command[1] = (uint8_t)identifier;
497     return l2cap_send(cid, command, sizeof(command));
498 }
499 
500 static int avdtp_acceptor_send_response_reject(uint16_t cid, avdtp_signal_identifier_t identifier, uint8_t transaction_label){
501     uint8_t command[2];
502     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
503     command[1] = (uint8_t)identifier;
504     return l2cap_send(cid, command, sizeof(command));
505 }
506 
507 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){
508     uint8_t command[3];
509     command[0] = avdtp_header(transaction_label, AVDTP_SINGLE_PACKET, AVDTP_RESPONSE_REJECT_MSG);
510     command[1] = (uint8_t)identifier;
511     command[2] = error_code;
512     return l2cap_send(cid, command, sizeof(command));
513 }
514 
515 void avdtp_acceptor_stream_config_subsm_run(avdtp_connection_t *connection) {
516     int sent = 1;
517     btstack_linked_list_t * stream_endpoints = avdtp_get_stream_endpoints();
518 
519     switch (connection->acceptor_connection_state){
520         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_ANSWER_DISCOVER_SEPS:
521             connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
522             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
523             avdtp_acceptor_send_seps_response(connection->l2cap_signaling_cid, connection->acceptor_transaction_label, (avdtp_stream_endpoint_t *) stream_endpoints);
524             avdtp_signaling_emit_accept(connection->avdtp_cid, 0, connection->acceptor_signaling_packet.signal_identifier, false);
525             break;
526         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
527             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
528             avdtp_acceptor_send_response_reject_with_error_code(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->error_code, connection->acceptor_transaction_label);
529             break;
530         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
531             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
532             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);
533             break;
534         case AVDTP_SIGNALING_CONNECTION_ACCEPTOR_W2_GENERAL_REJECT_WITH_ERROR_CODE:
535             connection->acceptor_connection_state = AVDTP_SIGNALING_CONNECTION_ACCEPTOR_IDLE;
536             avdtp_acceptor_send_response_general_reject(connection->l2cap_signaling_cid, connection->reject_signal_identifier, connection->acceptor_transaction_label);
537             break;
538         default:
539             sent = 0;
540             break;
541     }
542     if (sent){
543         log_info("DONE");
544         return;
545     }
546 
547     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(connection->acceptor_local_seid);
548     if (!stream_endpoint) return;
549 
550     uint8_t reject_service_category = connection->reject_service_category;
551     avdtp_signal_identifier_t reject_signal_identifier = connection->reject_signal_identifier;
552     uint8_t error_code = connection->error_code;
553     uint16_t cid = stream_endpoint->connection ? stream_endpoint->connection->l2cap_signaling_cid : connection->l2cap_signaling_cid;
554     uint8_t trid = stream_endpoint->connection ? stream_endpoint->connection->acceptor_transaction_label : connection->acceptor_transaction_label;
555 
556     avdtp_acceptor_stream_endpoint_state_t acceptor_config_state = stream_endpoint->acceptor_config_state;
557     stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
558     uint8_t * out_buffer;
559     uint16_t pos;
560     avdtp_sep_t * remote_sep;
561 
562     bool emit_accept = false;
563     bool emit_reject = false;
564 
565     switch (acceptor_config_state){
566         case AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE:
567             break;
568         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_CAPABILITIES:
569             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_CAPABILITIES);
570             l2cap_reserve_packet_buffer();
571             out_buffer = l2cap_get_outgoing_buffer();
572             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
573             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
574                 stream_endpoint->acceptor_config_state = acceptor_config_state;
575                 log_info("fragmented");
576             } else {
577                 log_info("ACP:DONE");
578                 emit_accept = true;
579             }
580             l2cap_send_prepared(cid, pos);
581             break;
582         case AVDTP_ACCEPTOR_W2_ACCEPT_DELAY_REPORT:
583             log_info("DONE ");
584             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_DELAYREPORT);
585             emit_accept = true;
586             break;
587         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_ALL_CAPABILITIES:
588             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, stream_endpoint->sep.registered_service_categories, stream_endpoint->sep.capabilities, AVDTP_SI_GET_ALL_CAPABILITIES);
589             l2cap_reserve_packet_buffer();
590             out_buffer = l2cap_get_outgoing_buffer();
591             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
592             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
593                 stream_endpoint->acceptor_config_state = acceptor_config_state;
594                 log_info("fragmented");
595             } else {
596                 log_info("ACP:DONE");
597                 emit_accept = true;
598             }
599             l2cap_send_prepared(cid, pos);
600             break;
601         case AVDTP_ACCEPTOR_W2_ACCEPT_SET_CONFIGURATION:
602             log_info("DONE");
603             log_info("    -> AVDTP_STREAM_ENDPOINT_CONFIGURED");
604             stream_endpoint->connection = connection;
605             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_CONFIGURED;
606             connection->configuration_state = AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED;
607             // TODO: consider reconfiguration
608             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SET_CONFIGURATION);
609             emit_accept = true;
610             break;
611         case AVDTP_ACCEPTOR_W2_ACCEPT_RECONFIGURE:
612             log_info("DONE ");
613             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_RECONFIGURE);
614             emit_accept = true;
615             break;
616         case AVDTP_ACCEPTOR_W2_ACCEPT_GET_CONFIGURATION:
617             remote_sep = &stream_endpoint->remote_sep;
618             avdtp_prepare_capabilities(&connection->acceptor_signaling_packet, trid, remote_sep->configured_service_categories, remote_sep->configuration, AVDTP_SI_GET_CONFIGURATION);
619             l2cap_reserve_packet_buffer();
620             out_buffer = l2cap_get_outgoing_buffer();
621             pos = avdtp_signaling_create_fragment(cid, &connection->acceptor_signaling_packet, out_buffer);
622             if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
623                 stream_endpoint->acceptor_config_state = acceptor_config_state;
624                 log_info("fragmented");
625             } else {
626                 log_info("ACP:DONE");
627                 emit_accept = true;
628             }
629             l2cap_send_prepared(cid, pos);
630             break;
631         case AVDTP_ACCEPTOR_W2_ACCEPT_OPEN_STREAM:
632             log_info("DONE");
633             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_OPEN);
634             emit_accept = true;
635             break;
636 
637 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
638         case AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM:
639             // keep state until user calls API to confirm or reject starting the stream
640             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_W4_USER_CONFIRM_START_STREAM;
641             avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint), AVDTP_SI_ACCEPT_START, false);
642             break;
643         case AVDTP_ACCEPTOR_W2_REJECT_START_STREAM:
644             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
645             connection->acceptor_signaling_packet.signal_identifier = AVDTP_SI_START;
646             emit_reject = true;
647             avdtp_acceptor_send_response_reject(cid, AVDTP_SI_START, trid);
648             break;
649 #endif
650         case AVDTP_ACCEPTOR_W2_ACCEPT_START_STREAM:
651             log_info("DONE ");
652             log_info("    -> AVDTP_STREAM_ENDPOINT_STREAMING ");
653             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
654             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_START);
655             emit_accept = true;
656             break;
657         case AVDTP_ACCEPTOR_W2_ACCEPT_CLOSE_STREAM:
658             log_info("DONE");
659             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_CLOSE);
660             connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE;
661             emit_accept = true;
662             break;
663         case AVDTP_ACCEPTOR_W2_ACCEPT_ABORT_STREAM:
664             log_info("DONE");
665             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_ABORT);
666             emit_accept = true;
667             break;
668         case AVDTP_ACCEPTOR_W2_ACCEPT_SUSPEND_STREAM:
669             log_info("DONE");
670             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
671             avdtp_acceptor_send_accept_response(cid, trid, AVDTP_SI_SUSPEND);
672             emit_accept = true;
673             break;
674         case AVDTP_ACCEPTOR_W2_REJECT_UNKNOWN_CMD:
675             log_info("DONE REJECT");
676             connection->reject_signal_identifier = AVDTP_SI_NONE;
677             avdtp_acceptor_send_response_reject(cid, reject_signal_identifier, trid);
678             emit_reject = true;
679             break;
680         case AVDTP_ACCEPTOR_W2_REJECT_CATEGORY_WITH_ERROR_CODE:
681             log_info("DONE REJECT CATEGORY");
682             connection->reject_service_category = 0;
683             avdtp_acceptor_send_response_reject_service_category(cid, reject_signal_identifier, reject_service_category, error_code, trid);
684             emit_reject = true;
685             break;
686         case AVDTP_ACCEPTOR_W2_REJECT_WITH_ERROR_CODE:
687             log_info("DONE REJECT");
688             connection->reject_signal_identifier = AVDTP_SI_NONE;
689             connection->error_code = 0;
690             avdtp_acceptor_send_response_reject_with_error_code(cid, reject_signal_identifier, error_code, trid);
691             emit_reject = true;
692             break;
693         default:
694             log_info("NOT IMPLEMENTED");
695             sent = 0;
696             break;
697     }
698 
699     if (emit_accept == true){
700         avdtp_signaling_emit_accept(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
701                                     connection->acceptor_signaling_packet.signal_identifier, false);
702     } else if (emit_reject == true){
703         avdtp_signaling_emit_reject(connection->avdtp_cid, avdtp_local_seid(stream_endpoint),
704                                     connection->acceptor_signaling_packet.signal_identifier, false);
705     }
706     // check fragmentation
707     if ((connection->acceptor_signaling_packet.packet_type != AVDTP_SINGLE_PACKET) && (connection->acceptor_signaling_packet.packet_type != AVDTP_END_PACKET)){
708 		avdtp_request_can_send_now_acceptor(connection);
709     }
710 }
711