xref: /btstack/src/classic/avdtp.c (revision 3c4cc6427fe05577c00b7d2593f58c7abcf9eab7)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "avdtp.c"
39 
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 #include "classic/avdtp.h"
48 #include "classic/avdtp_util.h"
49 #include "classic/avdtp_acceptor.h"
50 #include "classic/avdtp_initiator.h"
51 
52 #define CONFIGURATION_TIMEOUT_MS 300
53 
54 static int record_id = -1;
55 static uint8_t   attribute_value[1000];
56 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
57 
58 // typedef struct {
59 //     btstack_linked_list_t * avdtp_connections;
60 //     avdtp_connection_t * connection;
61 //     btstack_packet_handler_t avdtp_callback;
62 //     avdtp_sep_type_t query_role;
63 //     btstack_packet_handler_t packet_handler;
64 //     uint16_t avdtp_l2cap_psm;
65 //     uint16_t avdtp_version;
66 //     uint8_t  role_supported;
67 // } avdtp_sdp_query_context_t;
68 
69 static avdtp_context_t * sdp_query_context;
70 static uint16_t avdtp_cid_counter = 0;
71 
72 static void (*handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
73 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
74 
75 void avdtp_configuration_timeout_handler(btstack_timer_source_t * timer){
76     avdtp_connection_t * connection = (avdtp_connection_t *) btstack_run_loop_get_timer_context(timer);
77     if (!connection){
78         log_error("Context of avdtp_configuration_timeout_handler is NULL");
79         return;
80     }
81     avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t*) connection->active_stream_endpoint;
82     if (!stream_endpoint) {
83         log_error("avdtp_configuration_timeout_handler: no initiator stream endpoint for seid %d", connection->local_seid);
84         return;
85     }
86     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE) return;
87     connection->is_configuration_initiated_locally = 1;
88     connection->is_initiator = 1;
89     connection->initiator_transaction_label++;
90     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
91     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
92 }
93 
94 void avdtp_configuration_timer_start(avdtp_connection_t * connection){
95     avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t*) connection->active_stream_endpoint;
96     if (!stream_endpoint) {
97         log_error("avdtp_configuration_timeout_handler: no initiator stream endpoint for seid %d", connection->local_seid);
98         return;
99     }
100     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE) return;
101 
102     btstack_run_loop_remove_timer(&connection->configuration_timer);
103     btstack_run_loop_set_timer_handler(&connection->configuration_timer, avdtp_configuration_timeout_handler);
104     btstack_run_loop_set_timer_context(&connection->configuration_timer, connection);
105     btstack_run_loop_set_timer(&connection->configuration_timer, CONFIGURATION_TIMEOUT_MS);
106     btstack_run_loop_add_timer(&connection->configuration_timer);
107 }
108 
109 void avdtp_configuration_timer_stop(avdtp_connection_t * connection){
110     btstack_run_loop_remove_timer(&connection->configuration_timer);
111 }
112 
113 static uint16_t avdtp_get_next_initiator_transaction_label(avdtp_context_t * context){
114     context->initiator_transaction_id_counter++;
115     if (context->initiator_transaction_id_counter == 0){
116         context->initiator_transaction_id_counter = 1;
117     }
118     return context->initiator_transaction_id_counter;
119 }
120 
121 static uint16_t avdtp_get_next_avdtp_cid(avdtp_context_t * context){
122     do {
123         if (avdtp_cid_counter == 0xffff) {
124             avdtp_cid_counter = 1;
125         } else {
126             avdtp_cid_counter++;
127         }
128     } while (avdtp_connection_for_avdtp_cid(avdtp_cid_counter, context) !=  NULL) ;
129     return avdtp_cid_counter;
130 }
131 
132 static avdtp_stream_endpoint_t * avdtp_stream_endpoint_for_id(avdtp_context_t * context, uint16_t stream_endpoint_id) {
133     btstack_linked_item_t *it;
134     for (it = (btstack_linked_item_t *) context->stream_endpoints; it ; it = it->next){
135         avdtp_stream_endpoint_t * stream_endpoint = ((avdtp_stream_endpoint_t *) it);
136 
137         if (stream_endpoint->sep.seid == stream_endpoint_id) {
138             return stream_endpoint;
139         };
140     }
141     return NULL;
142 }
143 
144 static uint16_t avdtp_get_next_local_seid(avdtp_context_t * context){
145     uint16_t stream_endpoint_id = context->stream_endpoints_id_counter;
146     do {
147         if (stream_endpoint_id == 0xffff) {
148             stream_endpoint_id = 1;
149         } else {
150             stream_endpoint_id++;
151         }
152     } while (avdtp_stream_endpoint_for_id(context, stream_endpoint_id) !=  NULL) ;
153     return stream_endpoint_id;
154 }
155 
156 
157 uint8_t avdtp_connect(bd_addr_t remote, avdtp_sep_type_t query_role, avdtp_context_t * avdtp_context, uint16_t * avdtp_cid){
158     sdp_query_context = avdtp_context;
159     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(remote, avdtp_context);
160     if (!connection){
161         connection = avdtp_create_connection(remote, avdtp_context);
162         if (!connection){
163             log_error("Not enough memory to create connection.");
164             return BTSTACK_MEMORY_ALLOC_FAILED;
165         }
166     }
167 
168     *avdtp_cid = connection->avdtp_cid;
169     if (!avdtp_cid) {
170         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
171     }
172     avdtp_context->avdtp_cid = connection->avdtp_cid;
173 
174     uint8_t err;
175     switch (connection->state){
176         case AVDTP_SIGNALING_CONNECTION_IDLE:
177             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE;
178             connection->is_initiator = 1;
179             sdp_query_context = avdtp_context;
180             avdtp_context->avdtp_l2cap_psm = 0;
181             avdtp_context->avdtp_version = 0;
182             avdtp_context->query_role = query_role;
183             err = sdp_client_query_uuid16(&avdtp_handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_AVDTP);
184             if (err != ERROR_CODE_SUCCESS){
185                 connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
186                 btstack_linked_list_remove(&avdtp_context->connections, (btstack_linked_item_t*) connection);
187                 btstack_memory_avdtp_connection_free(connection);
188             }
189             return err;
190         case AVDTP_SIGNALING_CONNECTION_OPENED:{
191             avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid, avdtp_context);
192             if (stream_endpoint){
193                 avdtp_streaming_emit_connection_established(avdtp_context->avdtp_callback, connection->avdtp_cid, remote, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
194                 break;
195             }
196             avdtp_signaling_emit_connection_established(avdtp_context->avdtp_callback, connection->avdtp_cid, connection->remote_addr, ERROR_CODE_SUCCESS);
197             break;
198         }
199         default:
200             log_error("avdtp_connect: sink in wrong state");
201             return AVDTP_CONNECTION_IN_WRONG_STATE;
202 
203     }
204     return ERROR_CODE_SUCCESS;
205 }
206 
207 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){
208     if (!stream_endpoint){
209         log_error("Stream endpoint with given seid is not registered.");
210         return;
211     }
212     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
213     stream_endpoint->sep.registered_service_categories = bitmap;
214 }
215 
216 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
217     if (!stream_endpoint){
218         log_error("Stream endpoint with given seid is not registered.");
219         return;
220     }
221     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1);
222     stream_endpoint->sep.registered_service_categories = bitmap;
223 }
224 
225 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
226     if (!stream_endpoint){
227         log_error("Stream endpoint with given seid is not registered.");
228         return;
229     }
230     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
231     stream_endpoint->sep.registered_service_categories = bitmap;
232 }
233 
234 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
235     if (!stream_endpoint){
236         log_error("Stream endpoint with given seid is not registered.");
237         return;
238     }
239     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
240     stream_endpoint->sep.registered_service_categories = bitmap;
241     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
242     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
243     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
244 }
245 
246 void avdtp_register_content_protection_category(avdtp_stream_endpoint_t * stream_endpoint, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){
247     if (!stream_endpoint){
248         log_error("Stream endpoint with given seid is not registered.");
249         return;
250     }
251     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
252     stream_endpoint->sep.registered_service_categories = bitmap;
253     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
254     memcpy(stream_endpoint->sep.capabilities.content_protection.cp_type_value, cp_type_value, btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN));
255     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN);
256 }
257 
258 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
259     if (!stream_endpoint){
260         log_error("Stream endpoint with given seid is not registered.");
261         return;
262     }
263     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
264     stream_endpoint->sep.registered_service_categories = bitmap;
265     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
266     stream_endpoint->sep.capabilities.header_compression.media = media;
267     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
268 }
269 
270 void avdtp_register_media_codec_category(avdtp_stream_endpoint_t * stream_endpoint, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, uint8_t * media_codec_info, uint16_t media_codec_info_len){
271     if (!stream_endpoint){
272         log_error("Stream endpoint with given seid is not registered.");
273         return;
274     }
275     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
276     stream_endpoint->sep.registered_service_categories = bitmap;
277     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
278     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
279     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
280     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
281 }
282 
283 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
284     if (!stream_endpoint){
285         log_error("Stream endpoint with given seid is not registered.");
286         return;
287     }
288     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
289     stream_endpoint->sep.registered_service_categories = bitmap;
290     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
291 }
292 
293 
294 /* START: tracking can send now requests pro l2cap cid */
295 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
296     if (connection->wait_to_send_acceptor){
297         connection->wait_to_send_acceptor = 0;
298         avdtp_acceptor_stream_config_subsm_run(connection, context);
299     } else if (connection->wait_to_send_initiator){
300         connection->wait_to_send_initiator = 0;
301         avdtp_initiator_stream_config_subsm_run(connection, context);
302     } else if (connection->wait_to_send_self){
303         connection->wait_to_send_self = 0;
304         if (connection->disconnect){
305             btstack_linked_list_iterator_t it;
306             btstack_linked_list_iterator_init(&it, &context->stream_endpoints);
307             while (btstack_linked_list_iterator_has_next(&it)){
308                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
309                 if (stream_endpoint->connection == connection){
310                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
311                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
312                         avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
313                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
314                         return;
315                     }
316                 }
317             }
318             connection->disconnect = 0;
319             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
320             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
321             return;
322         }
323     }
324 
325     // re-register
326     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
327     if (more_to_send){
328         l2cap_request_can_send_now_event(l2cap_cid);
329     }
330 }
331 /* END: tracking can send now requests pro l2cap cid */
332 
333 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){
334     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
335     if (!connection){
336         log_error("Not enough memory to create connection");
337         return NULL;
338     }
339     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
340     connection->initiator_transaction_label = avdtp_get_next_initiator_transaction_label(context);
341     connection->avdtp_cid = avdtp_get_next_avdtp_cid(context);
342     context->avdtp_cid = connection->avdtp_cid;
343     memcpy(connection->remote_addr, remote_addr, 6);
344     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
345     return connection;
346 }
347 
348 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){
349     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
350     if (!stream_endpoint){
351         log_error("Not enough memory to create stream endpoint");
352         return NULL;
353     }
354     stream_endpoint->sep.seid = avdtp_get_next_local_seid(context);
355     stream_endpoint->sep.media_type = media_type;
356     stream_endpoint->sep.type = sep_type;
357     btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
358     return stream_endpoint;
359 }
360 
361 
362 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){
363     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
364     switch (connection->signaling_packet.message_type){
365         case AVDTP_CMD_MSG:
366             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
367             break;
368         default:
369             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
370             break;
371     }
372 }
373 
374 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
375     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(sdp_query_context->avdtp_cid, sdp_query_context);
376     if (!connection) {
377         log_error("SDP query, connection with 0x%02x cid not found", sdp_query_context->avdtp_cid);
378         return;
379     }
380     if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return;
381 
382     UNUSED(packet_type);
383     UNUSED(channel);
384     UNUSED(size);
385 
386     des_iterator_t des_list_it;
387     des_iterator_t prot_it;
388     uint8_t status;
389 
390     switch (hci_event_packet_get_type(packet)){
391         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
392             // Handle new SDP record
393             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
394                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
395                 // log_info("SDP Record: Nr: %d", record_id);
396             }
397 
398             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
399                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
400 
401                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
402 
403                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
404                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
405                             if (de_get_element_type(attribute_value) != DE_DES) break;
406                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
407                                 uint8_t * element = des_iterator_get_element(&des_list_it);
408                                 if (de_get_element_type(element) != DE_UUID) continue;
409                                 uint32_t uuid = de_get_uuid32(element);
410                                 switch (uuid){
411                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
412                                         if (sdp_query_context->query_role == AVDTP_SOURCE) {
413                                             sdp_query_context->role_supported = 1;
414                                             break;
415                                         }
416                                         // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
417                                         // avdtp_remote_uuid = uuid;
418                                         break;
419                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
420                                         if (sdp_query_context->query_role == AVDTP_SINK) {
421                                             sdp_query_context->role_supported = 1;
422                                             break;
423                                         }
424                                         // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
425                                         // avdtp_remote_uuid = uuid;
426                                         break;
427                                     default:
428                                         break;
429                                 }
430                             }
431                             break;
432 
433                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
434                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
435                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
436                                     uint8_t       *des_element;
437                                     uint8_t       *element;
438                                     uint32_t       uuid;
439 
440                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
441 
442                                     des_element = des_iterator_get_element(&des_list_it);
443                                     des_iterator_init(&prot_it, des_element);
444                                     element = des_iterator_get_element(&prot_it);
445 
446                                     if (de_get_element_type(element) != DE_UUID) continue;
447 
448                                     uuid = de_get_uuid32(element);
449                                     des_iterator_next(&prot_it);
450                                     switch (uuid){
451                                         case BLUETOOTH_PROTOCOL_L2CAP:
452                                             if (!des_iterator_has_more(&prot_it)) continue;
453                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_l2cap_psm);
454                                             break;
455                                         case BLUETOOTH_PROTOCOL_AVDTP:
456                                             if (!des_iterator_has_more(&prot_it)) continue;
457                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_version);
458                                             break;
459                                         default:
460                                             break;
461                                     }
462                                 }
463                             }
464                             break;
465                         default:
466                             break;
467                     }
468                 }
469             } else {
470                 log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
471             }
472             break;
473 
474         case SDP_EVENT_QUERY_COMPLETE:
475             if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE){
476                 // bail out, we must have had an incoming connection in the meantime;
477                 break;
478             }
479             status = sdp_event_query_complete_get_status(packet);
480             if (status != ERROR_CODE_SUCCESS){
481                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, status);
482                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
483                 btstack_memory_avdtp_connection_free(connection);
484                 log_info("SDP query failed with status 0x%02x.", status);
485                 break;
486             }
487             if (!sdp_query_context->role_supported){
488                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
489                 btstack_memory_avdtp_connection_free(connection);
490                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
491                 log_info("SDP query, remote device does not support required role.");
492                 break;
493             }
494             if (!sdp_query_context->avdtp_l2cap_psm) {
495                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*)connection);
496                 btstack_memory_avdtp_connection_free(connection);
497                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST);
498                 log_info("SDP query, no l2cap psm found.");
499                 break;
500             }
501             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
502             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
503             break;
504     }
505 }
506 
507 
508 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
509     bd_addr_t event_addr;
510     uint16_t psm;
511     uint16_t local_cid;
512     uint8_t  status;
513     int accept_signaling_connection;
514     avdtp_stream_endpoint_t * stream_endpoint = NULL;
515     avdtp_connection_t * connection = NULL;
516     btstack_linked_list_t * avdtp_connections = &context->connections;
517     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
518     handle_media_data = context->handle_media_data;
519     // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet));
520     switch (packet_type) {
521         case L2CAP_DATA_PACKET:
522             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
523             if (connection){
524                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
525                 break;
526             }
527 
528             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
529             if (!stream_endpoint){
530                 if (!connection) break;
531                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
532                 break;
533             }
534 
535             if (stream_endpoint->connection){
536                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
537                     int offset = avdtp_read_signaling_header(&stream_endpoint->connection->signaling_packet, packet, size);
538                     if (stream_endpoint->connection->signaling_packet.message_type == AVDTP_CMD_MSG){
539                         avdtp_acceptor_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
540                     } else {
541                         avdtp_initiator_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
542                     }
543                     break;
544                 }
545             }
546 
547             if (channel == stream_endpoint->l2cap_media_cid){
548                 if (handle_media_data){
549                     (*handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size);
550                 }
551                 break;
552             }
553 
554             if (channel == stream_endpoint->l2cap_reporting_cid){
555                 // TODO
556                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
557             } else if (channel == stream_endpoint->l2cap_recovery_cid){
558                 // TODO
559                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
560             } else {
561                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
562             }
563             break;
564 
565         case HCI_EVENT_PACKET:
566             switch (hci_event_packet_get_type(packet)) {
567                 case L2CAP_EVENT_INCOMING_CONNECTION:
568                     l2cap_event_incoming_connection_get_address(packet, event_addr);
569                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
570                     connection = avdtp_connection_for_bd_addr(event_addr, context);
571                     log_info("L2CAP_EVENT_INCOMING_CONNECTION, local cid 0x%02x ", local_cid);
572                     if (!connection){
573                         // create connection struct for regular inconming connection request
574                         connection = avdtp_create_connection(event_addr, context);
575                         if (!connection){
576                             log_error("Could not create connection, reject");
577                             l2cap_decline_connection(local_cid);
578                             break;
579                         }
580                         connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
581                     }
582 
583                     // connection exists, check states
584                     log_info("Connection state %d", connection->state);
585                     accept_signaling_connection = 0;
586                     switch (connection->state){
587                         case AVDTP_SIGNALING_CONNECTION_IDLE:
588                             // regular incoming connection
589                             accept_signaling_connection = 1;
590                             break;
591                         case AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE:
592                             // incoming connection during sdp query, just accept it
593                             accept_signaling_connection = 1;
594                             break;
595                         case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED:
596                             log_info("Reject incoming connection after creating outgoing");
597                             l2cap_decline_connection(local_cid);
598                             return;
599                         case AVDTP_SIGNALING_CONNECTION_OPENED:
600                             // handled below
601                             break;
602                         case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED:
603                             log_info("Reject incoming connection during disconnect");
604                             l2cap_decline_connection(local_cid);
605                             return;
606                     }
607 
608                     if (accept_signaling_connection){
609                         connection->is_initiator = 0;
610                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
611                         log_info("L2CAP_EVENT_INCOMING_CONNECTION: role is_initiator %d", connection->is_initiator);
612                         log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d, avdtp cid 0x%02x", connection, connection->state, connection->avdtp_cid);
613                         l2cap_accept_connection(local_cid);
614                         break;
615                     }
616 
617                     // now, we're only dealing with media connections
618                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
619                     if (!stream_endpoint) {
620                         log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->local_seid);
621                         l2cap_decline_connection(local_cid);
622                         break;
623                     }
624 
625                     log_info("Checking l2cap_media_cid %d, for local seid %d, state of stream endpoint %d, role is_initiator %d", stream_endpoint->l2cap_media_cid, connection->local_seid, stream_endpoint->state, connection->is_initiator);
626                     if (stream_endpoint->l2cap_media_cid == 0){
627                         if (connection->is_initiator){
628                             l2cap_decline_connection(local_cid);
629                             break;
630                         }
631                         l2cap_accept_connection(local_cid);
632                         break;
633                     }
634                     l2cap_decline_connection(local_cid);
635                     break;
636 
637                 case L2CAP_EVENT_CHANNEL_OPENED:
638                     psm = l2cap_event_channel_opened_get_psm(packet);
639                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
640                         log_info("Unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED ");
641                         return;
642                     }
643 
644                     status = l2cap_event_channel_opened_get_status(packet);
645                     // inform about new l2cap connection
646                     l2cap_event_channel_opened_get_address(packet, event_addr);
647                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
648                     connection = avdtp_connection_for_bd_addr(event_addr, context);
649 
650                     log_info("L2CAP_EVENT_CHANNEL_OPENED: status %d, cid 0x%02x , signaling connection %p ", status, local_cid, connection);
651                     connection = avdtp_connection_for_bd_addr(event_addr, context);
652                     if (!connection){
653                         log_info("L2CAP_EVENT_CHANNEL_OPENED 2: status %d, signaling connection %p ", status, connection);
654                         break;
655                     }
656 
657                     if (connection->l2cap_signaling_cid == 0) {
658                         if (status){
659                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
660                             connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
661                             connection->l2cap_signaling_cid = 0;
662                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, l2cap_event_channel_opened_get_status(packet));
663                             break;
664                         }
665                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) {
666                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
667                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, AVDTP_CONNECTION_IN_WRONG_STATE);
668                             break;
669                         }
670                         connection->l2cap_signaling_cid = local_cid;
671                         connection->local_seid = 0;
672                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
673                         log_info("AVDTP_SIGNALING_CONNECTION_OPENED, connection %p, l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x", connection, local_cid, connection->avdtp_cid);
674                         avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, 0);
675                         break;
676                     }
677 
678                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
679                     if (!stream_endpoint){
680                         log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
681                         return;
682                     }
683 
684                     if (stream_endpoint->l2cap_media_cid == 0){
685                         status = l2cap_event_channel_opened_get_status(packet);
686                         if (status){
687                             log_info("AVDTP_STREAM_ENDPOINT_OPENED failed with status %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", status, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
688                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
689                             avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
690                             break;
691                         }
692                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED){
693                             log_info("AVDTP_STREAM_ENDPOINT_OPENED failed - stream endpoint in wrong state %d, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", stream_endpoint->state, connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
694                             avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
695                             break;
696                         }
697 
698                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
699                         stream_endpoint->connection = connection;
700                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
701                         stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
702 
703                         log_info("AVDTP_STREAM_ENDPOINT_OPENED, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", connection->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
704                         avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
705                         return;
706                     }
707                     break;
708 
709                 case L2CAP_EVENT_CHANNEL_CLOSED:
710                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
711                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
712                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
713                     log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
714 
715                     if (stream_endpoint){
716                         if (stream_endpoint->l2cap_media_cid == local_cid){
717                             connection = stream_endpoint->connection;
718                             if (connection) {
719                                 avdtp_streaming_emit_connection_released(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint));
720                             }
721                             avdtp_reset_stream_endpoint(stream_endpoint);
722                             if (connection && connection->disconnect){
723                                 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
724                             }
725                             break;
726                         }
727                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
728                             log_info("L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
729                             stream_endpoint->l2cap_recovery_cid = 0;
730                             break;
731                         }
732 
733                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
734                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
735                             stream_endpoint->l2cap_reporting_cid = 0;
736                             break;
737                         }
738                     }
739 
740                     if (connection){
741                         btstack_linked_list_iterator_t it;
742                         btstack_linked_list_iterator_init(&it, stream_endpoints);
743                         while (btstack_linked_list_iterator_has_next(&it)){
744                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
745                             if (_stream_endpoint->connection == connection){
746                                 avdtp_reset_stream_endpoint(_stream_endpoint);
747                             }
748                         }
749                         btstack_run_loop_remove_timer(&connection->configuration_timer);
750                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
751                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
752                         btstack_memory_avdtp_connection_free(connection);
753                         break;
754                     }
755 
756                     break;
757 
758                 case HCI_EVENT_DISCONNECTION_COMPLETE:
759                     break;
760 
761                 case L2CAP_EVENT_CAN_SEND_NOW:
762                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
763                     if (!connection) {
764                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
765                         if (!stream_endpoint->connection) break;
766                         connection = stream_endpoint->connection;
767                     }
768                     avdtp_handle_can_send_now(connection, channel, context);
769                     break;
770                 default:
771                     log_info("Unknown HCI event type %02x", hci_event_packet_get_type(packet));
772                     break;
773             }
774             break;
775 
776         default:
777             // other packet type
778             break;
779     }
780 }
781 
782 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
783     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
784     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
785     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE){
786         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
787         return ERROR_CODE_SUCCESS;
788     }
789     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
790 
791     connection->disconnect = 1;
792     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
793     return ERROR_CODE_SUCCESS;
794 }
795 
796 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
797     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
798     if (!connection){
799         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
800         return AVDTP_CONNECTION_DOES_NOT_EXIST;
801     }
802 
803     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
804         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
805         return AVDTP_CONNECTION_IN_WRONG_STATE;
806     }
807 
808     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
809     if (!stream_endpoint) {
810         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
811         return AVDTP_SEID_DOES_NOT_EXIST;
812     }
813 
814     if (stream_endpoint->remote_sep.seid != remote_seid){
815         log_error("avdtp_media_connect: no remote sep with seid %d registered with the stream endpoint", remote_seid);
816         return AVDTP_SEID_DOES_NOT_EXIST;
817     }
818 
819     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
820 
821     connection->initiator_transaction_label++;
822     connection->remote_seid = remote_seid;
823     connection->local_seid = local_seid;
824     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
825     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
826     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
827     return ERROR_CODE_SUCCESS;
828 }
829 
830 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
831     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
832     if (!connection){
833         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
834         return AVDTP_CONNECTION_DOES_NOT_EXIST;
835     }
836 
837     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
838     if (!stream_endpoint) {
839         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
840         return AVDTP_SEID_DOES_NOT_EXIST;
841     }
842 
843     if (stream_endpoint->l2cap_media_cid == 0){
844         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
845         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
846     }
847 
848     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
849         log_error("avdtp_media_connect: no remote sep registered with the stream endpoint");
850         return AVDTP_SEID_DOES_NOT_EXIST;
851     }
852 
853     stream_endpoint->start_stream = 1;
854     connection->local_seid = local_seid;
855     connection->remote_seid = stream_endpoint->remote_sep.seid;
856     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
857     return ERROR_CODE_SUCCESS;
858 }
859 
860 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
861     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
862     if (!connection){
863         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
864         return AVDTP_CONNECTION_DOES_NOT_EXIST;
865     }
866 
867     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
868     if (!stream_endpoint) {
869         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
870         return AVDTP_SEID_DOES_NOT_EXIST;
871     }
872 
873     if (stream_endpoint->l2cap_media_cid == 0){
874         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
875         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
876     }
877 
878     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->stop_stream){
879         return ERROR_CODE_SUCCESS;
880     }
881 
882     stream_endpoint->stop_stream = 1;
883     connection->local_seid = local_seid;
884     connection->remote_seid = stream_endpoint->remote_sep.seid;
885     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
886     return ERROR_CODE_SUCCESS;
887 }
888 
889 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
890     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
891     if (!connection){
892         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
893         return AVDTP_CONNECTION_DOES_NOT_EXIST;
894     }
895 
896     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
897     if (!stream_endpoint) {
898         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
899         return AVDTP_SEID_DOES_NOT_EXIST;
900     }
901 
902     if (stream_endpoint->l2cap_media_cid == 0){
903         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
904         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
905     }
906 
907     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->abort_stream){
908         return ERROR_CODE_SUCCESS;
909     }
910 
911     stream_endpoint->abort_stream = 1;
912     connection->local_seid = local_seid;
913     connection->remote_seid = stream_endpoint->remote_sep.seid;
914     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
915     return ERROR_CODE_SUCCESS;
916 }
917 
918 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
919     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
920     if (!connection){
921         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
922         return AVDTP_CONNECTION_DOES_NOT_EXIST;
923     }
924     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
925     if (!stream_endpoint) {
926         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
927         return AVDTP_SEID_DOES_NOT_EXIST;
928     }
929 
930     if (stream_endpoint->l2cap_media_cid == 0){
931         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
932         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
933     }
934 
935     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->suspend_stream){
936         return ERROR_CODE_SUCCESS;
937     }
938 
939     stream_endpoint->suspend_stream = 1;
940     connection->local_seid = local_seid;
941     connection->remote_seid = stream_endpoint->remote_sep.seid;
942     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
943     return ERROR_CODE_SUCCESS;
944 }
945 
946 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
947     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
948     if (!connection){
949         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
950         return AVDTP_CONNECTION_DOES_NOT_EXIST;
951     }
952     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
953         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
954         return AVDTP_CONNECTION_IN_WRONG_STATE;
955     }
956 
957     connection->initiator_transaction_label++;
958     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
959     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
960 }
961 
962 
963 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
964     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
965     if (!connection){
966         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
967         return AVDTP_CONNECTION_DOES_NOT_EXIST;
968     }
969     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
970         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
971         return AVDTP_CONNECTION_IN_WRONG_STATE;
972     }
973 
974     connection->initiator_transaction_label++;
975     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
976     connection->remote_seid = remote_seid;
977     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
978 }
979 
980 
981 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
982     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
983     if (!connection){
984         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
985         return AVDTP_CONNECTION_DOES_NOT_EXIST;
986     }
987     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
988         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
989         return AVDTP_CONNECTION_IN_WRONG_STATE;
990     }
991 
992     connection->initiator_transaction_label++;
993     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
994     connection->remote_seid = remote_seid;
995     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
996 }
997 
998 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
999     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1000     if (!connection){
1001         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1002         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1003     }
1004     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
1005         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
1006         return AVDTP_CONNECTION_IN_WRONG_STATE;
1007     }
1008 
1009     connection->initiator_transaction_label++;
1010     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
1011     connection->remote_seid = remote_seid;
1012     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1013 }
1014 
1015 uint8_t avdtp_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
1016     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1017     if (!connection){
1018         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1019         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1020     }
1021     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
1022         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
1023         log_error("connection in wrong state, %d, initiator state %d", connection->state, connection->initiator_connection_state);
1024         return AVDTP_CONNECTION_IN_WRONG_STATE;
1025     }
1026 
1027     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
1028     if (!stream_endpoint) {
1029         log_error("No initiator stream endpoint for seid %d", local_seid);
1030         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
1031     }
1032     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_CONFIGURED){
1033         log_error("Stream endpoint seid %d in wrong state %d", local_seid, stream_endpoint->state);
1034         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1035     }
1036     connection->active_stream_endpoint = (void*) stream_endpoint;
1037     connection->is_configuration_initiated_locally = 1;
1038     connection->is_initiator = 1;
1039 
1040     connection->initiator_transaction_label++;
1041     connection->remote_seid = remote_seid;
1042     connection->local_seid = local_seid;
1043     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
1044     stream_endpoint->remote_configuration = configuration;
1045     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
1046 
1047     // cache media codec information for SBC
1048     stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type;
1049     if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){
1050         stream_endpoint->media_type = configuration.media_codec.media_type;
1051         memcpy(stream_endpoint->media_codec_sbc_info, configuration.media_codec.media_codec_information, 4);
1052     }
1053     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1054 }
1055 
1056 uint8_t avdtp_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
1057     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1058     if (!connection){
1059         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1060         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1061     }
1062     //TODO: if opened only app capabilities, enable reconfigure for not opened
1063     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
1064         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
1065         return AVDTP_CONNECTION_IN_WRONG_STATE;
1066     }
1067 
1068     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
1069     if (!stream_endpoint) {
1070         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
1071         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
1072     }
1073 
1074     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
1075         log_error("avdtp_reconfigure: no associated remote sep");
1076         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1077     }
1078 
1079     connection->initiator_transaction_label++;
1080     connection->remote_seid = remote_seid;
1081     connection->local_seid = local_seid;
1082     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
1083     stream_endpoint->remote_configuration = configuration;
1084     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
1085     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1086 }
1087 
1088 void    avdtp_set_preferred_sampling_frequeny(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency){
1089     stream_endpoint->preferred_sampling_frequency = sampling_frequency;
1090 }
1091 
1092 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
1093     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1094     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
1095 
1096     uint8_t channel_mode = AVDTP_SBC_STEREO;
1097     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
1098         channel_mode = AVDTP_SBC_JOINT_STEREO;
1099     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
1100         channel_mode = AVDTP_SBC_STEREO;
1101     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
1102         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1103     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1104         channel_mode = AVDTP_SBC_MONO;
1105     }
1106     return channel_mode;
1107 }
1108 
1109 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1110     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1111     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1112 
1113     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1114     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1115         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1116     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1117         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1118     }
1119     return allocation_method;
1120 }
1121 
1122 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1123     if (!stream_endpoint) return 0;
1124     return stream_endpoint->sep.seid;
1125 }
1126 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1127     if (!stream_endpoint) return 0;
1128     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1129     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1130 
1131     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1132     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1133         subbands = AVDTP_SBC_SUBBANDS_8;
1134     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1135         subbands = AVDTP_SBC_SUBBANDS_4;
1136     }
1137     return subbands;
1138 }
1139 
1140 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1141     if (!stream_endpoint) return 0;
1142     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1143     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1144 
1145     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1146     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1147         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1148     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1149         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1150     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1151         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1152     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1153         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1154     }
1155     return block_length;
1156 }
1157 
1158 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1159     if (!stream_endpoint) return 0;
1160     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1161     uint8_t supported_sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1162     uint8_t sampling_frequency = AVDTP_SBC_44100;   // some default
1163 
1164     // use preferred sampling frequency if possible
1165     if        ((stream_endpoint->preferred_sampling_frequency == 48000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_48000)){
1166         sampling_frequency = AVDTP_SBC_48000;
1167     } else if ((stream_endpoint->preferred_sampling_frequency == 44100) && (supported_sampling_frequency_bitmap & AVDTP_SBC_44100)){
1168         sampling_frequency = AVDTP_SBC_44100;
1169     } else if ((stream_endpoint->preferred_sampling_frequency == 32000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_32000)){
1170         sampling_frequency = AVDTP_SBC_32000;
1171     } else if ((stream_endpoint->preferred_sampling_frequency == 16000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_16000)){
1172         sampling_frequency = AVDTP_SBC_16000;
1173     }
1174     // otherwise, use highest available
1175     else if (supported_sampling_frequency_bitmap & AVDTP_SBC_48000){
1176         sampling_frequency = AVDTP_SBC_48000;
1177     } else if (supported_sampling_frequency_bitmap & AVDTP_SBC_44100){
1178         sampling_frequency = AVDTP_SBC_44100;
1179     } else if (supported_sampling_frequency_bitmap & AVDTP_SBC_32000){
1180         sampling_frequency = AVDTP_SBC_32000;
1181     } else if (supported_sampling_frequency_bitmap & AVDTP_SBC_16000){
1182         sampling_frequency = AVDTP_SBC_16000;
1183     }
1184     return sampling_frequency;
1185 }
1186 
1187 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1188     if (!stream_endpoint) return 0;
1189     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1190     return btstack_min(media_codec[3], remote_max_bitpool_value);
1191 }
1192 
1193 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1194     if (!stream_endpoint) return 0;
1195     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1196     return btstack_max(media_codec[2], remote_min_bitpool_value);
1197 }
1198 
1199 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint){
1200     if (!stream_endpoint) return 0;
1201     if (stream_endpoint->remote_sep.seid == 0) return 0;
1202     if (stream_endpoint->remote_sep.seid > 0x3E) return 0;
1203     return 1;
1204 }
1205