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