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