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