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