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