xref: /btstack/src/classic/avdtp.c (revision a15efc861737d1da360dc5169bcf7780f5e7e649)
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 = 0x55;
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     connection->is_initiator = 1;
82     log_info("avdtp_configuration_timeout_handler: role is_initiator %d", connection->is_initiator);
83     connection->is_configuration_initiated_locally = 1;
84     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
85 }
86 
87 void avdtp_configuration_timer_start(avdtp_connection_t * connection){
88     btstack_run_loop_remove_timer(&connection->configuration_timer);
89     btstack_run_loop_set_timer_handler(&connection->configuration_timer, avdtp_configuration_timeout_handler);
90     btstack_run_loop_set_timer_context(&connection->configuration_timer, connection);
91     btstack_run_loop_set_timer(&connection->configuration_timer, CONFIGURATION_TIMEOUT_MS);
92     btstack_run_loop_add_timer(&connection->configuration_timer);
93 }
94 
95 void avdtp_configuration_timer_stop(avdtp_connection_t * connection){
96     btstack_run_loop_remove_timer(&connection->configuration_timer);
97 }
98 
99 static uint16_t avdtp_get_next_initiator_transaction_label(avdtp_context_t * context){
100     context->initiator_transaction_id_counter++;
101     if (context->initiator_transaction_id_counter == 0){
102         context->initiator_transaction_id_counter = 1;
103     }
104     return context->initiator_transaction_id_counter;
105 }
106 
107 static uint16_t avdtp_get_next_avdtp_cid(void){
108     avdtp_cid_counter++;
109     if (avdtp_cid_counter == 0){
110         avdtp_cid_counter = 1;
111     }
112     return avdtp_cid_counter;
113 }
114 
115 static uint16_t avdtp_get_next_local_seid(avdtp_context_t * context){
116     context->stream_endpoints_id_counter++;
117     if (context->stream_endpoints_id_counter == 0){
118         context->stream_endpoints_id_counter = 1;
119     }
120     return context->stream_endpoints_id_counter;
121 }
122 
123 uint8_t avdtp_connect(bd_addr_t remote, avdtp_sep_type_t query_role, avdtp_context_t * avdtp_context, uint16_t * avdtp_cid){
124     sdp_query_context = avdtp_context;
125     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(remote, avdtp_context);
126     if (!connection){
127         connection = avdtp_create_connection(remote, avdtp_context);
128         if (!connection){
129             log_error("avdtp: not enough memory to create connection");
130             return BTSTACK_MEMORY_ALLOC_FAILED;
131         }
132     }
133 
134     *avdtp_cid = connection->avdtp_cid;
135     if (!avdtp_cid) {
136         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
137     }
138     avdtp_context->avdtp_cid = connection->avdtp_cid;
139 
140     uint8_t err;
141     switch (connection->state){
142         case AVDTP_SIGNALING_CONNECTION_IDLE:
143             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE;
144             connection->is_initiator = 1;
145             log_info("avdtp_connect: role is_initiator %d", connection->is_initiator);
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("avdtp_register_media_transport_category: 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("avdtp_register_media_transport_category: 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("avdtp_register_media_transport_category: 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("avdtp_register_media_transport_category: 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("avdtp_register_media_transport_category: 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     stream_endpoint->sep.capabilities.content_protection.cp_type_value = cp_type_value;
222     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = cp_type_value_len;
223 }
224 
225 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
226     if (!stream_endpoint){
227         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
228         return;
229     }
230     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
231     stream_endpoint->sep.registered_service_categories = bitmap;
232     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
233     stream_endpoint->sep.capabilities.header_compression.media = media;
234     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
235 }
236 
237 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){
238     if (!stream_endpoint){
239         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
240         return;
241     }
242     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
243     stream_endpoint->sep.registered_service_categories = bitmap;
244     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
245     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
246     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
247     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
248 }
249 
250 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
251     if (!stream_endpoint){
252         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
253         return;
254     }
255     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
256     stream_endpoint->sep.registered_service_categories = bitmap;
257     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
258 }
259 
260 
261 /* START: tracking can send now requests pro l2cap cid */
262 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
263     if (connection->wait_to_send_acceptor){
264         connection->wait_to_send_acceptor = 0;
265         avdtp_acceptor_stream_config_subsm_run(connection, context);
266     } else if (connection->wait_to_send_initiator){
267         connection->wait_to_send_initiator = 0;
268         avdtp_initiator_stream_config_subsm_run(connection, context);
269     } else if (connection->wait_to_send_self){
270         connection->wait_to_send_self = 0;
271         if (connection->disconnect){
272             btstack_linked_list_iterator_t it;
273             btstack_linked_list_iterator_init(&it, &context->stream_endpoints);
274             while (btstack_linked_list_iterator_has_next(&it)){
275                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
276                 if (stream_endpoint->connection == connection){
277                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
278                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
279                         avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
280                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
281                         return;
282                     }
283                 }
284             }
285             connection->disconnect = 0;
286             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
287             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
288             return;
289         }
290     }
291 
292     // re-register
293     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
294     if (more_to_send){
295         l2cap_request_can_send_now_event(l2cap_cid);
296     }
297 }
298 /* END: tracking can send now requests pro l2cap cid */
299 
300 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){
301     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
302     if (!connection){
303         log_error("avdtp: not enough memory to create connection");
304         return NULL;
305     }
306     memset(connection, 0, sizeof(avdtp_connection_t));
307     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
308     connection->initiator_transaction_label = avdtp_get_next_initiator_transaction_label(context);
309     connection->avdtp_cid = avdtp_get_next_avdtp_cid();
310     context->avdtp_cid = connection->avdtp_cid;
311     memcpy(connection->remote_addr, remote_addr, 6);
312     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
313     return connection;
314 }
315 
316 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){
317     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
318     if (!stream_endpoint){
319         log_error("avdtp: not enough memory to create stream endpoint");
320         return NULL;
321     }
322     memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t));
323     stream_endpoint->sep.seid = avdtp_get_next_local_seid(context);
324     stream_endpoint->sep.media_type = media_type;
325     stream_endpoint->sep.type = sep_type;
326     btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
327     return stream_endpoint;
328 }
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     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
333     switch (connection->signaling_packet.message_type){
334         case AVDTP_CMD_MSG:
335             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
336             break;
337         default:
338             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
339             break;
340     }
341 }
342 
343 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
344     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(sdp_query_context->avdtp_cid, sdp_query_context);
345     if (!connection) {
346         log_error("avdtp: sdp query, connection with 0x%02x cid not found", sdp_query_context->avdtp_cid);
347         return;
348     }
349     if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return;
350 
351     UNUSED(packet_type);
352     UNUSED(channel);
353     UNUSED(size);
354 
355     des_iterator_t des_list_it;
356     des_iterator_t prot_it;
357     uint8_t status;
358 
359     switch (hci_event_packet_get_type(packet)){
360         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
361             // Handle new SDP record
362             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
363                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
364                 // log_info("SDP Record: Nr: %d", record_id);
365             }
366 
367             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
368                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
369 
370                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
371 
372                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
373                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
374                             if (de_get_element_type(attribute_value) != DE_DES) break;
375                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
376                                 uint8_t * element = des_iterator_get_element(&des_list_it);
377                                 if (de_get_element_type(element) != DE_UUID) continue;
378                                 uint32_t uuid = de_get_uuid32(element);
379                                 switch (uuid){
380                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
381                                         if (sdp_query_context->query_role == AVDTP_SOURCE) {
382                                             sdp_query_context->role_supported = 1;
383                                             break;
384                                         }
385                                         // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
386                                         // avdtp_remote_uuid = uuid;
387                                         break;
388                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
389                                         if (sdp_query_context->query_role == AVDTP_SINK) {
390                                             sdp_query_context->role_supported = 1;
391                                             break;
392                                         }
393                                         // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
394                                         // avdtp_remote_uuid = uuid;
395                                         break;
396                                     default:
397                                         break;
398                                 }
399                             }
400                             break;
401 
402                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
403                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
404                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
405                                     uint8_t       *des_element;
406                                     uint8_t       *element;
407                                     uint32_t       uuid;
408 
409                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
410 
411                                     des_element = des_iterator_get_element(&des_list_it);
412                                     des_iterator_init(&prot_it, des_element);
413                                     element = des_iterator_get_element(&prot_it);
414 
415                                     if (de_get_element_type(element) != DE_UUID) continue;
416 
417                                     uuid = de_get_uuid32(element);
418                                     switch (uuid){
419                                         case BLUETOOTH_PROTOCOL_L2CAP:
420                                             if (!des_iterator_has_more(&prot_it)) continue;
421                                             des_iterator_next(&prot_it);
422                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_l2cap_psm);
423                                             break;
424                                         case BLUETOOTH_PROTOCOL_AVDTP:
425                                             if (!des_iterator_has_more(&prot_it)) continue;
426                                             des_iterator_next(&prot_it);
427                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_version);
428                                             break;
429                                         default:
430                                             break;
431                                     }
432                                 }
433                             }
434                             break;
435                         default:
436                             break;
437                     }
438                 }
439             } else {
440                 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));
441             }
442             break;
443 
444         case SDP_EVENT_QUERY_COMPLETE:
445             status = sdp_event_query_complete_get_status(packet);
446             if (status != ERROR_CODE_SUCCESS){
447                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, status);
448                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
449                 btstack_memory_avdtp_connection_free(connection);
450                 log_info("AVDTP: SDP query failed with status 0x%02x.", status);
451                 break;
452             }
453             if (!sdp_query_context->role_supported){
454                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
455                 btstack_memory_avdtp_connection_free(connection);
456                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
457                 log_info("AVDTP: SDP query, remote device does not support required role.");
458                 break;
459             }
460             if (!sdp_query_context->avdtp_l2cap_psm) {
461                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*)connection);
462                 btstack_memory_avdtp_connection_free(connection);
463                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST);
464                 log_info("AVDTP: SDP query, no l2cap psm found.");
465                 break;
466             }
467             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
468             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
469             break;
470     }
471 }
472 
473 
474 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
475     bd_addr_t event_addr;
476     uint16_t psm;
477     uint16_t local_cid;
478     uint8_t  status;
479     avdtp_stream_endpoint_t * stream_endpoint = NULL;
480     avdtp_connection_t * connection = NULL;
481     btstack_linked_list_t * avdtp_connections = &context->connections;
482     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
483     handle_media_data = context->handle_media_data;
484     // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet));
485     switch (packet_type) {
486         case L2CAP_DATA_PACKET:
487             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
488             if (connection){
489                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
490                 break;
491             }
492 
493             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
494             if (!stream_endpoint){
495                 if (!connection) break;
496                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
497                 break;
498             }
499 
500             if (stream_endpoint->connection){
501                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
502                     int offset = avdtp_read_signaling_header(&stream_endpoint->connection->signaling_packet, packet, size);
503                     if (stream_endpoint->connection->signaling_packet.message_type == AVDTP_CMD_MSG){
504                         avdtp_acceptor_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
505                     } else {
506                         avdtp_initiator_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
507                     }
508                     break;
509                 }
510             }
511 
512             if (channel == stream_endpoint->l2cap_media_cid){
513                 if (handle_media_data){
514                     (*handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size);
515                 }
516                 break;
517             }
518 
519             if (channel == stream_endpoint->l2cap_reporting_cid){
520                 // TODO
521                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
522             } else if (channel == stream_endpoint->l2cap_recovery_cid){
523                 // TODO
524                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
525             } else {
526                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
527             }
528             break;
529 
530         case HCI_EVENT_PACKET:
531             switch (hci_event_packet_get_type(packet)) {
532                 case L2CAP_EVENT_INCOMING_CONNECTION:
533                     l2cap_event_incoming_connection_get_address(packet, event_addr);
534                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
535                     connection = avdtp_connection_for_bd_addr(event_addr, context);
536 
537                     if (!connection || connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED){
538                         connection = avdtp_create_connection(event_addr, context);
539                         connection->is_initiator = 0;
540                         log_info("L2CAP_EVENT_INCOMING_CONNECTION: role is_initiator %d", connection->is_initiator);
541                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
542                         log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d, avdtp cid 0x%02x", connection, connection->state, connection->avdtp_cid);
543                         l2cap_accept_connection(local_cid);
544                         break;
545                     }
546 
547                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
548                     if (!stream_endpoint) {
549                         log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->local_seid);
550                         break;
551                     }
552 
553                     if (stream_endpoint->l2cap_media_cid == 0){
554                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
555                         l2cap_accept_connection(local_cid);
556                         break;
557                     }
558                     break;
559 
560                 case L2CAP_EVENT_CHANNEL_OPENED:
561                     psm = l2cap_event_channel_opened_get_psm(packet);
562                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
563                         log_info("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
564                         return;
565                     }
566 
567                     status = l2cap_event_channel_opened_get_status(packet);
568                     // inform about new l2cap connection
569                     l2cap_event_channel_opened_get_address(packet, event_addr);
570                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
571                     connection = avdtp_connection_for_bd_addr(event_addr, context);
572 
573                     log_info("L2CAP_EVENT_CHANNEL_OPENED: status %d, cid 0x%02x , signaling connection %p \n", status, local_cid, connection);
574                     connection = avdtp_connection_for_bd_addr(event_addr, context);
575                     if (!connection){
576                         log_info("L2CAP_EVENT_CHANNEL_OPENED 2: status %d, signaling connection %p \n", status, connection);
577                         break;
578                     }
579 
580                     if (connection->l2cap_signaling_cid == 0) {
581                         if (status){
582                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
583                             connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
584                             connection->l2cap_signaling_cid = 0;
585                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, l2cap_event_channel_opened_get_status(packet));
586                             break;
587                         }
588                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) {
589                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
590                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, AVDTP_CONNECTION_IN_WRONG_STATE);
591                             break;
592                         }
593                         connection->l2cap_signaling_cid = local_cid;
594                         connection->local_seid = 0;
595                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
596                         log_info(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p, l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x", connection, local_cid, connection->avdtp_cid);
597                         avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, 0);
598                         break;
599                     }
600 
601                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
602                     if (!stream_endpoint){
603                         log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
604                         return;
605                     }
606 
607                     if (stream_endpoint->l2cap_media_cid == 0){
608                         status = l2cap_event_channel_opened_get_status(packet);
609                         if (status){
610                             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));
611                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
612                             avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
613                             break;
614                         }
615                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED){
616                             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));
617                             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);
618                             break;
619                         }
620 
621                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
622                         stream_endpoint->connection = connection;
623                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
624                         stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
625 
626                         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));
627                         avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
628                         return;
629                     }
630                     break;
631 
632                 case L2CAP_EVENT_CHANNEL_CLOSED:
633                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
634                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
635                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
636                     log_info("received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
637 
638                     if (stream_endpoint){
639                         if (stream_endpoint->l2cap_media_cid == local_cid){
640                             connection = stream_endpoint->connection;
641                             if (connection) {
642                                 avdtp_streaming_emit_connection_released(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint));
643                             }
644                             stream_endpoint->l2cap_media_cid = 0;
645                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
646                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
647                             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
648                             stream_endpoint->remote_sep_index = 0;
649                             if (connection && connection->disconnect){
650                                 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
651                             }
652                             break;
653                         }
654                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
655                             log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
656                             stream_endpoint->l2cap_recovery_cid = 0;
657                             break;
658                         }
659 
660                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
661                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
662                             stream_endpoint->l2cap_reporting_cid = 0;
663                             break;
664                         }
665                     }
666 
667                     if (connection){
668                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
669                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
670                         btstack_linked_list_iterator_t it;
671                         btstack_linked_list_iterator_init(&it, stream_endpoints);
672                         while (btstack_linked_list_iterator_has_next(&it)){
673                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
674 
675                             if (_stream_endpoint->connection == connection){
676                                 avdtp_initialize_stream_endpoint(_stream_endpoint);
677                             }
678                         }
679                         btstack_memory_avdtp_connection_free(connection);
680                         break;
681                     }
682 
683                     break;
684 
685                 case HCI_EVENT_DISCONNECTION_COMPLETE:
686                     break;
687 
688                 case L2CAP_EVENT_CAN_SEND_NOW:
689                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
690                     if (!connection) {
691                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
692                         if (!stream_endpoint->connection) break;
693                         connection = stream_endpoint->connection;
694                     }
695                     avdtp_handle_can_send_now(connection, channel, context);
696                     break;
697                 default:
698                     log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet));
699                     break;
700             }
701             break;
702 
703         default:
704             // other packet type
705             break;
706     }
707 }
708 
709 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
710     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
711     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
712     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE){
713         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
714         return ERROR_CODE_SUCCESS;
715     }
716     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
717 
718     connection->disconnect = 1;
719     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
720     return ERROR_CODE_SUCCESS;
721 }
722 
723 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
724     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
725     if (!connection){
726         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
727         return AVDTP_CONNECTION_DOES_NOT_EXIST;
728     }
729     if (avdtp_find_remote_sep(connection, remote_seid) == 0xFF){
730         log_error("avdtp_media_connect: no remote sep for seid %d found", remote_seid);
731         return AVDTP_SEID_DOES_NOT_EXIST;
732     }
733 
734     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
735         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
736         return AVDTP_CONNECTION_IN_WRONG_STATE;
737     }
738 
739     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
740     if (!stream_endpoint) {
741         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
742         return AVDTP_SEID_DOES_NOT_EXIST;
743     }
744 
745     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
746     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return AVDTP_SEID_DOES_NOT_EXIST;
747 
748     connection->initiator_transaction_label++;
749     connection->remote_seid = remote_seid;
750     connection->local_seid = local_seid;
751     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
752     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
753     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
754     return ERROR_CODE_SUCCESS;
755 }
756 
757 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
758     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
759     if (!connection){
760         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
761         return AVDTP_CONNECTION_DOES_NOT_EXIST;
762     }
763 
764     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
765     if (!stream_endpoint) {
766         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
767         return AVDTP_SEID_DOES_NOT_EXIST;
768     }
769 
770     if (stream_endpoint->l2cap_media_cid == 0){
771         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
772         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
773     }
774 
775     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream){
776         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
777     }
778 
779     stream_endpoint->start_stream = 1;
780     connection->local_seid = local_seid;
781     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
782     return ERROR_CODE_SUCCESS;
783 }
784 
785 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
786     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
787     if (!connection){
788         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
789         return AVDTP_CONNECTION_DOES_NOT_EXIST;
790     }
791 
792     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
793     if (!stream_endpoint) {
794         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
795         return AVDTP_SEID_DOES_NOT_EXIST;
796     }
797 
798     if (stream_endpoint->l2cap_media_cid == 0){
799         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
800         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
801     }
802     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return ERROR_CODE_SUCCESS;
803 
804     stream_endpoint->stop_stream = 1;
805     connection->local_seid = local_seid;
806     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
807     return ERROR_CODE_SUCCESS;
808 }
809 
810 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
811     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
812     if (!connection){
813         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
814         return AVDTP_CONNECTION_DOES_NOT_EXIST;
815     }
816 
817     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
818     if (!stream_endpoint) {
819         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
820         return AVDTP_SEID_DOES_NOT_EXIST;
821     }
822 
823     if (stream_endpoint->l2cap_media_cid == 0){
824         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
825         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
826     }
827     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return ERROR_CODE_SUCCESS;
828 
829     stream_endpoint->abort_stream = 1;
830     connection->local_seid = local_seid;
831     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
832     return ERROR_CODE_SUCCESS;
833 }
834 
835 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
836     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
837     if (!connection){
838         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
839         return AVDTP_CONNECTION_DOES_NOT_EXIST;
840     }
841 
842     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
843     if (!stream_endpoint) {
844         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
845         return AVDTP_SEID_DOES_NOT_EXIST;
846     }
847 
848     if (stream_endpoint->l2cap_media_cid == 0){
849         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
850         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
851     }
852     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return ERROR_CODE_SUCCESS;
853 
854     stream_endpoint->suspend_stream = 1;
855     connection->local_seid = local_seid;
856     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
857     return ERROR_CODE_SUCCESS;
858 }
859 
860 void avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
861     log_info("call avdtp_discover_stream_endpoints");
862 
863     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
864     if (!connection){
865         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
866         return;
867     }
868 
869     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
870 
871     switch (connection->initiator_connection_state){
872         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
873             connection->initiator_transaction_label++;
874             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
875             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
876             break;
877         default:
878             log_error("avdtp_discover_stream_endpoints: wrong state");
879             break;
880     }
881 }
882 
883 
884 void avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_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_get_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
888         return;
889     }
890     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
891     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
892     connection->initiator_transaction_label++;
893     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
894     connection->remote_seid = remote_seid;
895     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
896 }
897 
898 
899 void avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
900     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
901     if (!connection){
902         log_error("avdtp_get_all_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
903         return;
904     }
905     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
906     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
907     connection->initiator_transaction_label++;
908     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
909     connection->remote_seid = remote_seid;
910     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
911 }
912 
913 void avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
914     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
915     if (!connection){
916         log_error("avdtp_get_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
917         return;
918     }
919     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
920     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
921     connection->initiator_transaction_label++;
922     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
923     connection->remote_seid = remote_seid;
924     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
925 }
926 
927 void 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){
928     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
929     if (!connection){
930         log_error("avdtp_set_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
931         return;
932     }
933     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
934     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
935 
936     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
937     if (!stream_endpoint) {
938         log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d", local_seid);
939         return;
940     }
941     connection->is_configuration_initiated_locally = 1;
942     connection->is_initiator = 1;
943     log_info("avdtp_set_configuration locally: role is_initiator %d", connection->is_initiator);
944 
945     connection->initiator_transaction_label++;
946     connection->remote_seid = remote_seid;
947     connection->local_seid = local_seid;
948     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
949     stream_endpoint->remote_configuration = configuration;
950     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
951 
952     // cache media codec information for SBC
953     stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type;
954     if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){
955         stream_endpoint->media_type = configuration.media_codec.media_type;
956         memcpy(stream_endpoint->media_codec_sbc_info, configuration.media_codec.media_codec_information, 4);
957     }
958     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
959 }
960 
961 void 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){
962     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
963     if (!connection){
964         log_error("avdtp_reconfigure: no connection for AVDTP cid 0x%02x found", avdtp_cid);
965         return;
966     }
967     //TODO: if opened only app capabilities, enable reconfigure for not opened
968     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
969     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
970 
971     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
972     if (!stream_endpoint) {
973         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
974         return;
975     }
976 
977     if (stream_endpoint->remote_sep_index == 0xFF){
978         log_error("avdtp_reconfigure: no associated remote sep");
979         return;
980     }
981 
982     connection->initiator_transaction_label++;
983     connection->remote_seid = remote_seid;
984     connection->local_seid = local_seid;
985     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
986     stream_endpoint->remote_configuration = configuration;
987     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
988     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
989 }
990 
991 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){
992     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
993     if (!connection){
994         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
995         return 0;
996     }
997     return connection->remote_seps_num;
998 }
999 
1000 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){
1001     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1002     if (!connection){
1003         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
1004         return NULL;
1005     }
1006     return &connection->remote_seps[index];
1007 }
1008 
1009 void avdtp_initialize_sbc_configuration_storage(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * config_storage, uint16_t storage_size, uint8_t * packet, uint16_t packet_size){
1010     UNUSED(packet_size);
1011     if (storage_size < 4) {
1012         log_error("storage must have 4 bytes");
1013         return;
1014     }
1015     uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
1016     uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
1017     uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
1018     uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
1019 
1020     uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
1021     uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
1022     uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
1023 
1024     config_storage[0] = (sampling_frequency << 4) | channel_mode;
1025     config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method;
1026     config_storage[2] = min_bitpool_value;
1027     config_storage[3] = max_bitpool_value;
1028 
1029     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
1030     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
1031     stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
1032     stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size;
1033     stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage;
1034 }
1035 
1036 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
1037     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1038     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
1039 
1040     uint8_t channel_mode = AVDTP_SBC_STEREO;
1041     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
1042         channel_mode = AVDTP_SBC_JOINT_STEREO;
1043     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
1044         channel_mode = AVDTP_SBC_STEREO;
1045     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
1046         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1047     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1048         channel_mode = AVDTP_SBC_MONO;
1049     }
1050     return channel_mode;
1051 }
1052 
1053 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1054     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1055     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1056 
1057     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1058     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1059         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1060     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1061         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1062     }
1063     return allocation_method;
1064 }
1065 
1066 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1067     if (!stream_endpoint) return 0;
1068     return stream_endpoint->sep.seid;
1069 }
1070 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1071     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1072     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1073 
1074     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1075     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1076         subbands = AVDTP_SBC_SUBBANDS_8;
1077     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1078         subbands = AVDTP_SBC_SUBBANDS_4;
1079     }
1080     return subbands;
1081 }
1082 
1083 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1084     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1085     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1086 
1087     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1088     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1089         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1090     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1091         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1092     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1093         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1094     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1095         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1096     }
1097     return block_length;
1098 }
1099 
1100 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1101     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1102     uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1103 
1104     uint8_t sampling_frequency = AVDTP_SBC_44100;
1105     if (sampling_frequency_bitmap & AVDTP_SBC_48000){
1106         sampling_frequency = AVDTP_SBC_48000;
1107     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){
1108         sampling_frequency = AVDTP_SBC_44100;
1109     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){
1110         sampling_frequency = AVDTP_SBC_32000;
1111     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){
1112         sampling_frequency = AVDTP_SBC_16000;
1113     }
1114     return sampling_frequency;
1115 }
1116 
1117 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1118     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1119     return btstack_min(media_codec[3], remote_max_bitpool_value);
1120 }
1121 
1122 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1123     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1124     return btstack_max(media_codec[2], remote_min_bitpool_value);
1125 }
1126