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