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