xref: /btstack/src/classic/avdtp.c (revision 0fbfe0705ce515f74b808566b16b94c29ea198ee)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "avdtp.c"
39 
40 
41 #include <stdint.h>
42 #include <string.h>
43 
44 #include "bluetooth_psm.h"
45 #include "bluetooth_sdp.h"
46 #include "btstack_debug.h"
47 #include "btstack_event.h"
48 #include "btstack_memory.h"
49 #include "classic/avdtp.h"
50 #include "classic/avdtp_acceptor.h"
51 #include "classic/avdtp_initiator.h"
52 #include "classic/avdtp_util.h"
53 #include "classic/sdp_client.h"
54 #include "classic/sdp_util.h"
55 
56 avdtp_context_t * avdtp_source_context = NULL;
57 avdtp_context_t * avdtp_sink_context = NULL;
58 
59 btstack_linked_list_t stream_endpoints;
60 
61 static btstack_packet_handler_t avdtp_source_callback;
62 static btstack_packet_handler_t avdtp_sink_callback;
63 
64 static uint16_t sdp_query_context_avdtp_cid = 0;
65 
66 static uint16_t stream_endpoints_id_counter = 0;
67 
68 static btstack_linked_list_t connections;
69 static uint16_t initiator_transaction_id_counter = 0;
70 
71 static int record_id = -1;
72 static uint8_t   attribute_value[45];
73 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
74 
75 static void (*avdtp_sink_handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
76 
77 static uint16_t avdtp_cid_counter = 0;
78 
79 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
80 
81 btstack_packet_handler_t
82 avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint) {
83     return (stream_endpoint->sep.type == AVDTP_SOURCE) ? avdtp_source_callback : avdtp_sink_callback;
84 }
85 
86 static void avdtp_streaming_emit_connection_established(avdtp_stream_endpoint_t *stream_endpoint, uint8_t status) {
87     uint8_t event[14];
88     int pos = 0;
89     event[pos++] = HCI_EVENT_AVDTP_META;
90     event[pos++] = sizeof(event) - 2;
91     event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED;
92     little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid);
93     pos += 2;
94     reverse_bd_addr(stream_endpoint->connection->remote_addr, &event[pos]);
95     pos += 6;
96     event[pos++] = avdtp_local_seid(stream_endpoint);
97     event[pos++] = avdtp_remote_seid(stream_endpoint);
98     event[pos++] = status;
99 
100     btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
101     (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
102 }
103 
104 static void avdtp_streaming_emit_connection_released(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid, uint8_t local_seid) {
105     uint8_t event[6];
106     int pos = 0;
107     event[pos++] = HCI_EVENT_AVDTP_META;
108     event[pos++] = sizeof(event) - 2;
109     event[pos++] = AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED;
110     little_endian_store_16(event, pos, avdtp_cid);
111     pos += 2;
112     event[pos++] = local_seid;
113 
114     btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
115     (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
116 }
117 
118 void avdtp_streaming_emit_can_send_media_packet_now(avdtp_stream_endpoint_t *stream_endpoint, uint16_t sequence_number) {
119     uint8_t event[8];
120     int pos = 0;
121     event[pos++] = HCI_EVENT_AVDTP_META;
122     event[pos++] = sizeof(event) - 2;
123     event[pos++] = AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW;
124     little_endian_store_16(event, pos, stream_endpoint->connection->avdtp_cid);
125     pos += 2;
126     event[pos++] = avdtp_local_seid(stream_endpoint);
127     little_endian_store_16(event, pos, sequence_number);
128     pos += 2;
129 
130     btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
131     (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
132 }
133 
134 
135 void avdtp_signaling_emit_delay(uint16_t avdtp_cid, uint8_t local_seid, uint16_t delay) {
136     uint8_t event[8];
137     int pos = 0;
138     event[pos++] = HCI_EVENT_AVDTP_META;
139     event[pos++] = sizeof(event) - 2;
140     event[pos++] = AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT;
141     little_endian_store_16(event, pos, avdtp_cid);
142     pos += 2;
143     event[pos++] = local_seid;
144     little_endian_store_16(event, pos, delay);
145     pos += 2;
146     (*avdtp_source_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
147 }
148 
149 void avdtp_emit_configuration(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
150                               avdtp_capabilities_t *configuration, uint16_t configured_service_categories) {
151 
152     uint8_t local_seid = avdtp_local_seid(stream_endpoint);
153     uint8_t remote_seid = avdtp_remote_seid(stream_endpoint);
154     btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
155 
156     if (get_bit16(configured_service_categories, AVDTP_MEDIA_CODEC)){
157         switch (configuration->media_codec.media_codec_type){
158             case AVDTP_CODEC_SBC:
159                 avdtp_signaling_emit_media_codec_sbc_configuration(
160                         stream_endpoint, avdtp_cid,
161                         configuration->media_codec.media_type,
162                         configuration->media_codec.media_codec_information);
163                 break;
164             default:
165                 avdtp_signaling_emit_media_codec_other_configuration(packet_handler, avdtp_cid, local_seid, remote_seid, configuration->media_codec);
166                 break;
167         }
168     }
169 }
170 
171 static inline void
172 avdtp_signaling_emit_media_codec_sbc(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
173                                      avdtp_media_type_t media_type, const uint8_t *media_codec_information,
174                                      uint8_t reconfigure) {
175 
176     btstack_packet_handler_t packet_handler = avdtp_packet_handler_for_stream_endpoint(stream_endpoint);
177     uint8_t local_seid = avdtp_local_seid(stream_endpoint);
178     uint8_t remote_seid = avdtp_remote_seid(stream_endpoint);
179 
180     uint8_t event[16 + 2];
181     int pos = 0;
182     event[pos++] = HCI_EVENT_AVDTP_META;
183     event[pos++] = sizeof(event) - 2;
184 
185     event[pos++] = AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION;
186     little_endian_store_16(event, pos, avdtp_cid);
187     pos += 2;
188     event[pos++] = local_seid;
189     event[pos++] = remote_seid;
190     event[pos++] = reconfigure;
191 
192 
193     uint8_t sampling_frequency_bitmap = media_codec_information[0] >> 4;
194     uint8_t channel_mode_bitmap = media_codec_information[0] & 0x0F;
195     uint8_t block_length_bitmap = media_codec_information[1] >> 4;
196     uint8_t subbands_bitmap = (media_codec_information[1] & 0x0F) >> 2;
197 
198     uint8_t num_channels = 0;
199     if ((channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO) ||
200         (channel_mode_bitmap & AVDTP_SBC_STEREO) ||
201         (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL)) {
202         num_channels = 2;
203     } else if (channel_mode_bitmap & AVDTP_SBC_MONO) {
204         num_channels = 1;
205     }
206 
207     uint16_t sampling_frequency = 0;
208     if (sampling_frequency_bitmap & AVDTP_SBC_48000) {
209         sampling_frequency = 48000;
210     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100) {
211         sampling_frequency = 44100;
212     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000) {
213         sampling_frequency = 32000;
214     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000) {
215         sampling_frequency = 16000;
216     }
217 
218     uint8_t subbands = 0;
219     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
220         subbands = 8;
221     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
222         subbands = 4;
223     }
224 
225     uint8_t block_length = 0;
226     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
227         block_length = 16;
228     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
229         block_length = 12;
230     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
231         block_length = 8;
232     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
233         block_length = 4;
234     }
235 
236     event[pos++] = media_type;
237     little_endian_store_16(event, pos, sampling_frequency);
238     pos += 2;
239 
240     event[pos++] = channel_mode_bitmap;
241     event[pos++] = num_channels;
242     event[pos++] = block_length;
243     event[pos++] = subbands;
244     event[pos++] = media_codec_information[1] & 0x03;
245     event[pos++] = media_codec_information[2];
246     event[pos++] = media_codec_information[3];
247     (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
248 }
249 
250 void avdtp_signaling_emit_media_codec_sbc_configuration(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
251                                                         avdtp_media_type_t media_type,
252                                                         const uint8_t *media_codec_information) {
253     avdtp_signaling_emit_media_codec_sbc(stream_endpoint, avdtp_cid, media_type,
254                                          media_codec_information, 0);
255 }
256 
257 void avdtp_signaling_emit_media_codec_sbc_reconfiguration(avdtp_stream_endpoint_t *stream_endpoint, uint16_t avdtp_cid,
258                                                           avdtp_media_type_t media_type,
259                                                           const uint8_t *media_codec_information) {
260     avdtp_signaling_emit_media_codec_sbc(stream_endpoint, avdtp_cid, media_type,
261                                          media_codec_information, 1);
262 }
263 
264 btstack_linked_list_t * avdtp_get_stream_endpoints(void){
265     return &stream_endpoints;
266 }
267 
268 static avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr){
269     btstack_linked_list_iterator_t it;
270     btstack_linked_list_iterator_init(&it, &connections);
271     while (btstack_linked_list_iterator_has_next(&it)){
272         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
273         if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
274         return connection;
275     }
276     return NULL;
277 }
278 
279  avdtp_connection_t * avdtp_get_connection_for_avdtp_cid(uint16_t avdtp_cid){
280     btstack_linked_list_iterator_t it;
281     btstack_linked_list_iterator_init(&it, &connections);
282     while (btstack_linked_list_iterator_has_next(&it)){
283         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
284         if (connection->avdtp_cid != avdtp_cid) continue;
285         return connection;
286     }
287     return NULL;
288 }
289 
290 
291 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_seid(uint16_t seid){
292     btstack_linked_list_iterator_t it;
293     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
294     while (btstack_linked_list_iterator_has_next(&it)){
295         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
296         if (stream_endpoint->sep.seid == seid){
297             return stream_endpoint;
298         }
299     }
300     return NULL;
301 }
302 
303 avdtp_connection_t * avdtp_get_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid){
304     btstack_linked_list_iterator_t it;
305     btstack_linked_list_iterator_init(&it, &connections);
306     while (btstack_linked_list_iterator_has_next(&it)){
307         avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it);
308         if (connection->l2cap_signaling_cid != l2cap_cid) continue;
309         return connection;
310     }
311     return NULL;
312 }
313 
314 static avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_l2cap_cid(uint16_t l2cap_cid){
315     btstack_linked_list_iterator_t it;
316     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
317     while (btstack_linked_list_iterator_has_next(&it)){
318         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
319         if (stream_endpoint->l2cap_media_cid == l2cap_cid){
320             return stream_endpoint;
321         }
322         if (stream_endpoint->l2cap_reporting_cid == l2cap_cid){
323             return stream_endpoint;
324         }
325         if (stream_endpoint->l2cap_recovery_cid == l2cap_cid){
326             return stream_endpoint;
327         }
328     }
329     return NULL;
330 }
331 
332 static avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_signaling_cid(uint16_t l2cap_cid){
333     btstack_linked_list_iterator_t it;
334     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
335     while (btstack_linked_list_iterator_has_next(&it)){
336         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
337         if (stream_endpoint->connection){
338             if (stream_endpoint->connection->l2cap_signaling_cid == l2cap_cid){
339                 return stream_endpoint;
340             }
341         }
342     }
343     return NULL;
344 }
345 
346 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_with_seid(uint8_t seid){
347     btstack_linked_list_iterator_t it;
348     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
349     while (btstack_linked_list_iterator_has_next(&it)){
350         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
351         if (stream_endpoint->sep.seid == seid){
352             return stream_endpoint;
353         }
354     }
355     return NULL;
356 }
357 
358 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_associated_with_acp_seid(uint16_t acp_seid){
359     btstack_linked_list_iterator_t it;
360     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
361     while (btstack_linked_list_iterator_has_next(&it)){
362         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
363         if (stream_endpoint->remote_sep.seid == acp_seid){
364             return stream_endpoint;
365         }
366     }
367     return NULL;
368 }
369 
370 static uint16_t avdtp_get_next_initiator_transaction_label(void){
371     initiator_transaction_id_counter++;
372     if (initiator_transaction_id_counter == 0){
373         initiator_transaction_id_counter = 1;
374     }
375     return initiator_transaction_id_counter;
376 }
377 
378 static avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, uint16_t cid){
379     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
380     if (!connection){
381         log_error("Not enough memory to create connection");
382         return NULL;
383     }
384     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
385     connection->initiator_transaction_label = avdtp_get_next_initiator_transaction_label();
386     connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE;
387     connection->avdtp_cid = cid;
388     (void)memcpy(connection->remote_addr, remote_addr, 6);
389 
390     btstack_linked_list_add(&connections, (btstack_linked_item_t *) connection);
391     return connection;
392 }
393 
394 static uint16_t avdtp_get_next_cid(void){
395     if (avdtp_cid_counter == 0xffff) {
396         avdtp_cid_counter = 1;
397     } else {
398         avdtp_cid_counter++;
399     }
400     return avdtp_cid_counter;
401 }
402 
403 static uint16_t avdtp_get_next_local_seid(void){
404     if (stream_endpoints_id_counter == 0xffff) {
405         stream_endpoints_id_counter = 1;
406     } else {
407         stream_endpoints_id_counter++;
408     }
409     return stream_endpoints_id_counter;
410 }
411 
412 static uint8_t avdtp_start_sdp_query(btstack_packet_handler_t packet_handler, avdtp_connection_t * connection) {
413     connection->avdtp_l2cap_psm = 0;
414     connection->avdtp_version  = 0;
415     connection->sink_supported = false;
416     connection->source_supported = false;
417     sdp_query_context_avdtp_cid = connection->avdtp_cid;
418 
419     return sdp_client_query_uuid16(packet_handler, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP);
420 }
421 
422 uint8_t avdtp_connect(bd_addr_t remote, avdtp_role_t role, uint16_t * avdtp_cid){
423     // TODO: implement delayed SDP query
424     if (sdp_client_ready() == 0){
425         return ERROR_CODE_COMMAND_DISALLOWED;
426     }
427 
428     avdtp_connection_t * connection = avdtp_get_connection_for_bd_addr(remote);
429     if (connection){
430         return ERROR_CODE_COMMAND_DISALLOWED;
431     }
432 
433     uint16_t cid = avdtp_get_next_cid();
434     if (avdtp_cid != NULL) {
435         *avdtp_cid = cid;
436     }
437 
438     connection = avdtp_create_connection(remote, cid);
439     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
440 
441     connection->avdtp_cid = cid;
442 
443     switch (role){
444         case AVDTP_ROLE_SOURCE:
445             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE;
446             break;
447         case AVDTP_ROLE_SINK:
448             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE;
449             break;
450         default:
451             return ERROR_CODE_COMMAND_DISALLOWED;
452     }
453     return avdtp_start_sdp_query(&avdtp_handle_sdp_client_query_result, connection);
454 }
455 
456 
457 void avdtp_register_sink_packet_handler(btstack_packet_handler_t callback){
458     btstack_assert(callback != NULL);
459     avdtp_sink_callback = callback;
460 }
461 
462 void avdtp_register_source_packet_handler(btstack_packet_handler_t callback){
463     btstack_assert(callback != NULL);
464     avdtp_source_callback = callback;
465 }
466 
467 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){
468     if (!stream_endpoint){
469         log_error("Stream endpoint with given seid is not registered.");
470         return;
471     }
472     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
473     stream_endpoint->sep.registered_service_categories = bitmap;
474 }
475 
476 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
477     if (!stream_endpoint){
478         log_error("Stream endpoint with given seid is not registered.");
479         return;
480     }
481     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1);
482     stream_endpoint->sep.registered_service_categories = bitmap;
483 }
484 
485 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
486     if (!stream_endpoint){
487         log_error("Stream endpoint with given seid is not registered.");
488         return;
489     }
490     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
491     stream_endpoint->sep.registered_service_categories = bitmap;
492 }
493 
494 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
495     if (!stream_endpoint){
496         log_error("Stream endpoint with given seid is not registered.");
497         return;
498     }
499     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
500     stream_endpoint->sep.registered_service_categories = bitmap;
501     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
502     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
503     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
504 }
505 
506 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){
507     if (!stream_endpoint){
508         log_error("Stream endpoint with given seid is not registered.");
509         return;
510     }
511     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
512     stream_endpoint->sep.registered_service_categories = bitmap;
513     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
514     (void)memcpy(stream_endpoint->sep.capabilities.content_protection.cp_type_value,
515                  cp_type_value,
516                  btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN));
517     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN);
518 }
519 
520 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
521     if (!stream_endpoint){
522         log_error("Stream endpoint with given seid is not registered.");
523         return;
524     }
525     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
526     stream_endpoint->sep.registered_service_categories = bitmap;
527     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
528     stream_endpoint->sep.capabilities.header_compression.media = media;
529     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
530 }
531 
532 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){
533     if (!stream_endpoint){
534         log_error("Stream endpoint with given seid is not registered.");
535         return;
536     }
537     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
538     stream_endpoint->sep.registered_service_categories = bitmap;
539     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
540     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
541     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
542     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
543 }
544 
545 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
546     if (!stream_endpoint){
547         log_error("Stream endpoint with given seid is not registered.");
548         return;
549     }
550     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
551     stream_endpoint->sep.registered_service_categories = bitmap;
552     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
553 }
554 
555 void avdtp_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){
556     avdtp_sink_handle_media_data = callback;
557 }
558 
559 /* START: tracking can send now requests pro l2cap cid */
560 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
561     if (connection->wait_to_send_acceptor){
562         log_debug("call avdtp_acceptor_stream_config_subsm_run");
563         connection->wait_to_send_acceptor = 0;
564         avdtp_acceptor_stream_config_subsm_run(connection, context);
565     } else if (connection->wait_to_send_initiator){
566         log_debug("call avdtp_initiator_stream_config_subsm_run");
567         connection->wait_to_send_initiator = 0;
568         avdtp_initiator_stream_config_subsm_run(connection, context);
569     }
570 
571     // re-register
572     bool more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator;
573     log_debug("ask for more to send %d: acc-%d, ini-%d",  more_to_send, connection->wait_to_send_acceptor, connection->wait_to_send_initiator);
574 
575     if (more_to_send){
576         l2cap_request_can_send_now_event(l2cap_cid);
577     }
578 }
579 /* END: tracking can send now requests pro l2cap cid */
580 
581 
582 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
583     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
584     if (!stream_endpoint){
585         log_error("Not enough memory to create stream endpoint");
586         return NULL;
587     }
588     stream_endpoint->sep.seid = avdtp_get_next_local_seid();
589     stream_endpoint->sep.media_type = media_type;
590     stream_endpoint->sep.type = sep_type;
591     btstack_linked_list_add(avdtp_get_stream_endpoints(), (btstack_linked_item_t *) stream_endpoint);
592     return stream_endpoint;
593 }
594 
595 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){
596     if (size < 2) return;
597 
598     uint16_t offset;
599     avdtp_message_type_t message_type = avdtp_get_signaling_packet_type(packet);
600     switch (message_type){
601         case AVDTP_CMD_MSG:
602             offset = avdtp_read_signaling_header(&connection->acceptor_signaling_packet, packet, size);
603             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
604             break;
605         default:
606             offset = avdtp_read_signaling_header(&connection->initiator_signaling_packet, packet, size);
607             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
608             break;
609     }
610 }
611 
612 static void avdtp_handle_sdp_client_query_attribute_value(avdtp_connection_t * connection, uint8_t *packet){
613     des_iterator_t des_list_it;
614     des_iterator_t prot_it;
615 
616     // Handle new SDP record
617     if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
618         record_id = sdp_event_query_attribute_byte_get_record_id(packet);
619         // log_info("SDP Record: Nr: %d", record_id);
620     }
621 
622     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
623         attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
624 
625         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
626 
627             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
628 
629                 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
630                     if (de_get_element_type(attribute_value) != DE_DES) break;
631                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
632                         uint8_t * element = des_iterator_get_element(&des_list_it);
633                         if (de_get_element_type(element) != DE_UUID) continue;
634                         uint32_t uuid = de_get_uuid32(element);
635                         switch (uuid){
636                             case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
637                                 connection->source_supported = true;
638                                 log_info("source_supported");
639                                 break;
640                             case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
641                                 connection->sink_supported = true;
642                                 log_info("sink_supported");
643                                 break;
644                             default:
645                                 break;
646                         }
647                     }
648                     break;
649 
650                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
651                     // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
652                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
653                         uint8_t       *des_element;
654                         uint8_t       *element;
655                         uint32_t       uuid;
656 
657                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
658 
659                         des_element = des_iterator_get_element(&des_list_it);
660                         des_iterator_init(&prot_it, des_element);
661                         element = des_iterator_get_element(&prot_it);
662 
663                         if (de_get_element_type(element) != DE_UUID) continue;
664 
665                         uuid = de_get_uuid32(element);
666                         des_iterator_next(&prot_it);
667                         // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both
668                         switch (uuid){
669                             case BLUETOOTH_PROTOCOL_L2CAP:
670                                 if (!des_iterator_has_more(&prot_it)) continue;
671                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->avdtp_l2cap_psm);
672                                 break;
673                             case BLUETOOTH_PROTOCOL_AVDTP:
674                                 if (!des_iterator_has_more(&prot_it)) continue;
675                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->avdtp_version);
676                                 break;
677                             default:
678                                 break;
679                         }
680                     }
681                     break;
682 
683                 default:
684                     break;
685             }
686         }
687     } else {
688         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));
689     }
690 
691 }
692 
693 static void avdtp_finalize_connection(avdtp_connection_t * connection){
694     btstack_run_loop_remove_timer(&connection->retry_timer);
695     btstack_linked_list_remove(&connections, (btstack_linked_item_t*) connection);
696     btstack_memory_avdtp_connection_free(connection);
697 }
698 
699 static void avdtp_handle_sdp_query_failed(avdtp_connection_t * connection, uint8_t status){
700     printf("avdtp_handle_sdp_query_failed \n");
701     switch (connection->state){
702         case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE:
703             avdtp_signaling_emit_connection_established(avdtp_source_callback, connection->avdtp_cid, connection->remote_addr, status);
704             break;
705         case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE:
706             avdtp_signaling_emit_connection_established(avdtp_sink_callback, connection->avdtp_cid, connection->remote_addr, status);
707             break;
708         default:
709             return;
710     }
711     avdtp_finalize_connection(connection);
712     sdp_query_context_avdtp_cid = 0;
713     log_info("SDP query failed with status 0x%02x.", status);
714 }
715 
716 static void avdtp_handle_sdp_query_succeeded(avdtp_connection_t * connection){
717     printf("avdtp_handle_sdp_query_succeeded \n");
718     connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
719 }
720 
721 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
722     UNUSED(packet_type);
723     UNUSED(channel);
724     UNUSED(size);
725 
726     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(sdp_query_context_avdtp_cid);
727     if (!connection) {
728         log_error("SDP query, connection with 0x%02x cid not found", sdp_query_context_avdtp_cid);
729         return;
730     }
731 
732     uint8_t status;
733     bool query_succeded = false;
734 
735     switch (connection->state){
736         case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE:
737             switch (hci_event_packet_get_type(packet)){
738                 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
739                     avdtp_handle_sdp_client_query_attribute_value(connection, packet);
740                     return;
741                 case SDP_EVENT_QUERY_COMPLETE:
742                     status = sdp_event_query_complete_get_status(packet);
743                     if (status != ERROR_CODE_SUCCESS) break;
744                     if (!connection->sink_supported) break;
745                     if (connection->avdtp_l2cap_psm == 0) break;
746                     query_succeded = true;
747                     break;
748                 default:
749                     btstack_assert(false);
750                     break;
751             }
752             break;
753         case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE:
754             switch (hci_event_packet_get_type(packet)){
755                 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
756                     avdtp_handle_sdp_client_query_attribute_value(connection, packet);
757                     return;
758                 case SDP_EVENT_QUERY_COMPLETE:
759                     status = sdp_event_query_complete_get_status(packet);
760                     if (status != ERROR_CODE_SUCCESS) break;
761                     if (!connection->source_supported) break;
762                     if (connection->avdtp_l2cap_psm == 0) break;
763                     query_succeded = true;
764                     break;
765                 default:
766                     btstack_assert(false);
767                     break;
768             }
769             break;
770         default:
771             // bail out, we must have had an incoming connection in the meantime;
772             return;
773     }
774 
775     if (query_succeded){
776         avdtp_handle_sdp_query_succeeded(connection);
777         l2cap_create_channel(avdtp_packet_handler, connection->remote_addr, connection->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
778     } else {
779         avdtp_handle_sdp_query_failed(connection, status);
780     }
781 }
782 
783 static avdtp_connection_t * avdtp_handle_incoming_connection(avdtp_connection_t * connection, bd_addr_t event_addr, uint16_t local_cid){
784     if (connection == NULL){
785         uint16_t cid = avdtp_get_next_cid();
786         connection = avdtp_create_connection(event_addr, cid);
787     }
788 
789     if (connection) {
790         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
791         connection->l2cap_signaling_cid = local_cid;
792         btstack_run_loop_remove_timer(&connection->retry_timer);
793     }
794     return connection;
795 }
796 
797 static avdtp_context_t * avdtp_get_active_contex(void){
798     // assume only one context is active
799     avdtp_context_t * context = NULL;
800     if (avdtp_sink_context != NULL){
801         context = avdtp_sink_context;
802     } else {
803         context = avdtp_source_context;
804     }
805     btstack_assert(context != NULL);
806     return context;
807 }
808 
809 static void avdtp_retry_timer_timeout_handler(btstack_timer_source_t * timer){
810     uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
811 
812     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
813     if (connection == NULL) return;
814 
815     if (connection->state == AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY){
816         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
817         l2cap_create_channel(&avdtp_packet_handler, connection->remote_addr, connection->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
818     }
819 }
820 
821 static void avdtp_retry_timer_start(avdtp_connection_t * connection){
822     btstack_run_loop_set_timer_handler(&connection->retry_timer, avdtp_retry_timer_timeout_handler);
823     btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avdtp_cid);
824 
825     // add some jitter/randomness to reconnect delay
826     uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F);
827     btstack_run_loop_set_timer(&connection->retry_timer, timeout);
828     btstack_run_loop_add_timer(&connection->retry_timer);
829 }
830 
831 
832 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
833     bd_addr_t event_addr;
834     uint16_t psm;
835     uint16_t local_cid;
836     uint8_t  status;
837     uint16_t l2cap_mtu;
838 
839     bool accept_streaming_connection;
840     bool outoing_signaling_active;
841     bool decline_connection;
842 
843     avdtp_stream_endpoint_t * stream_endpoint = NULL;
844     avdtp_connection_t * connection = NULL;
845 
846     avdtp_context_t * context = avdtp_get_active_contex();
847 
848     switch (packet_type) {
849         case L2CAP_DATA_PACKET:
850             connection = avdtp_get_connection_for_l2cap_signaling_cid(channel);
851             if (connection){
852                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
853                 break;
854             }
855 
856             stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel);
857             if (!stream_endpoint){
858                 if (!connection) break;
859                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
860                 break;
861             }
862 
863             if (stream_endpoint->connection){
864                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
865                     handle_l2cap_data_packet_for_signaling_connection(stream_endpoint->connection, packet, size, context);
866                     break;
867                 }
868             }
869 
870             if (channel == stream_endpoint->l2cap_media_cid){
871                 btstack_assert(avdtp_sink_handle_media_data);
872                 (*avdtp_sink_handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size);
873                 break;
874             }
875 
876             if (channel == stream_endpoint->l2cap_reporting_cid){
877                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
878             } else if (channel == stream_endpoint->l2cap_recovery_cid){
879                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
880             } else {
881                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
882             }
883             break;
884 
885         case HCI_EVENT_PACKET:
886             switch (hci_event_packet_get_type(packet)) {
887 
888                 case L2CAP_EVENT_INCOMING_CONNECTION:
889                     l2cap_event_incoming_connection_get_address(packet, event_addr);
890                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
891 
892                     outoing_signaling_active = false;
893                     accept_streaming_connection = false;
894 
895                     connection = avdtp_get_connection_for_bd_addr(event_addr);
896                     if (connection != NULL){
897                         switch (connection->state){
898                             case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED:
899                             case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED:
900                                 outoing_signaling_active = true;
901                                 connection->incoming_declined = true;
902                                 break;
903                             case AVDTP_SIGNALING_CONNECTION_OPENED:
904                                 outoing_signaling_active = true;
905                                 accept_streaming_connection = true;
906                                 break;
907                             default:
908                                 break;
909                         }
910                     }
911                     log_info("incoming: %s, outoing_signaling_active %d, accept_streaming_connection %d",
912                         bd_addr_to_str(event_addr), outoing_signaling_active, accept_streaming_connection);
913 
914                     decline_connection = outoing_signaling_active && !accept_streaming_connection;
915                     if (outoing_signaling_active == false){
916                         connection = avdtp_handle_incoming_connection(connection, event_addr, local_cid);
917                         if (connection == NULL){
918                             decline_connection = true;
919                         }
920                     } else if (accept_streaming_connection){
921                         if ((connection == NULL) || (connection->configuration_state != AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED)) {
922                             decline_connection = true;
923                         } else {
924                             // now, we're only dealing with media connections that are created by remote side - we're acceptor here
925                             stream_endpoint = avdtp_get_stream_endpoint_with_seid(connection->acceptor_local_seid);
926                             if ((stream_endpoint == NULL) || (stream_endpoint->l2cap_media_cid != 0) ) {
927                                 decline_connection = true;
928                             }
929                         }
930                     }
931 
932                     if (decline_connection){
933                         l2cap_decline_connection(local_cid);
934                     } else {
935                         l2cap_accept_connection(local_cid);
936                     }
937                     break;
938 
939                 case L2CAP_EVENT_CHANNEL_OPENED:
940                     btstack_assert(context != NULL);
941 
942                     psm = l2cap_event_channel_opened_get_psm(packet);
943                     if (psm != BLUETOOTH_PSM_AVDTP){
944                         log_info("Unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED ");
945                         return;
946                     }
947 
948                     status = l2cap_event_channel_opened_get_status(packet);
949                     // inform about new l2cap connection
950                     l2cap_event_channel_opened_get_address(packet, event_addr);
951                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
952                     l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
953                     connection = avdtp_get_connection_for_bd_addr(event_addr);
954                     if (connection == NULL){
955                         log_info("L2CAP_EVENT_CHANNEL_OPENED: no connection found for %s", bd_addr_to_str(event_addr));
956                         break;
957                     }
958 
959                     switch (connection->state){
960                         case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED:
961                             switch (status){
962                                 case ERROR_CODE_SUCCESS:
963                                     connection->l2cap_signaling_cid = local_cid;
964                                     connection->incoming_declined = false;
965                                     connection->l2cap_mtu = l2cap_mtu;
966                                     connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
967                                     log_info("Connection opened l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x", connection->l2cap_signaling_cid, connection->avdtp_cid);
968                                     avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, status);
969                                     return;
970                                 case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
971                                     if (connection->incoming_declined == true) {
972                                         log_info("Connection was declined, and the outgoing failed");
973                                         connection->state = AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY;
974                                         connection->incoming_declined = false;
975                                         avdtp_retry_timer_start(connection);
976                                         return;
977                                     }
978                                     break;
979                                 default:
980                                     log_info("Connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
981                                     break;
982                             }
983                             avdtp_finalize_connection(connection);
984                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, status);
985                             break;
986 
987                         case AVDTP_SIGNALING_CONNECTION_OPENED:
988                             stream_endpoint = avdtp_get_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid);
989                             if (!stream_endpoint){
990                                 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found for signaling cid 0x%02x", connection->l2cap_signaling_cid);
991                                 return;
992                             }
993                             if (status != ERROR_CODE_SUCCESS){
994                                 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));
995                                 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
996                                 avdtp_streaming_emit_connection_established(stream_endpoint, status);
997                                 break;
998                             }
999                             switch (stream_endpoint->state){
1000                                 case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED:
1001                                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
1002                                     stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
1003                                     stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
1004 
1005                                     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));
1006                                     avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_SUCCESS);
1007                                     break;
1008                                 default:
1009                                     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));
1010                                     avdtp_streaming_emit_connection_established(stream_endpoint, AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
1011                                     break;
1012                             }
1013                             break;
1014 
1015                         default:
1016                             log_info("L2CAP connection to %s ignored: status code 0x%02x, connection state %d", bd_addr_to_str(event_addr), status, connection->state);
1017                             break;
1018                     }
1019                     break;
1020 
1021                 case L2CAP_EVENT_CHANNEL_CLOSED:
1022                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
1023                     stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(local_cid);
1024                     connection = avdtp_get_connection_for_l2cap_signaling_cid(local_cid);
1025 
1026                     log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
1027 
1028                     if (stream_endpoint){
1029                         if (stream_endpoint->l2cap_media_cid == local_cid){
1030                             connection = stream_endpoint->connection;
1031                             if (connection) {
1032                                 avdtp_streaming_emit_connection_released(stream_endpoint,
1033                                                                          connection->avdtp_cid,
1034                                                                          avdtp_local_seid(stream_endpoint));
1035                             }
1036                             avdtp_reset_stream_endpoint(stream_endpoint);
1037                             break;
1038                         }
1039                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
1040                             log_info("L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
1041                             stream_endpoint->l2cap_recovery_cid = 0;
1042                             break;
1043                         }
1044 
1045                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
1046                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
1047                             stream_endpoint->l2cap_reporting_cid = 0;
1048                             break;
1049                         }
1050                     }
1051 
1052                     if (connection){
1053                         btstack_linked_list_iterator_t it;
1054                         btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
1055                         while (btstack_linked_list_iterator_has_next(&it)){
1056                             stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
1057                             if (stream_endpoint->connection == connection){
1058                                 avdtp_reset_stream_endpoint(stream_endpoint);
1059                             }
1060                         }
1061                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
1062                         avdtp_finalize_connection(connection);
1063                         break;
1064                     }
1065 
1066                     break;
1067 
1068                 case HCI_EVENT_DISCONNECTION_COMPLETE:
1069                     break;
1070 
1071                 case L2CAP_EVENT_CAN_SEND_NOW:
1072                     log_debug("avdtp_packet_handler, L2CAP_EVENT_CAN_SEND_NOW l2cap_cid 0x%02x", channel);
1073                     connection = avdtp_get_connection_for_l2cap_signaling_cid(channel);
1074                     if (!connection) {
1075                         stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel);
1076                         if (!stream_endpoint->connection) break;
1077                         connection = stream_endpoint->connection;
1078                     }
1079                     avdtp_handle_can_send_now(connection, channel, context);
1080                     break;
1081                 default:
1082                     log_info("Unknown HCI event type %02x", hci_event_packet_get_type(packet));
1083                     break;
1084             }
1085             break;
1086 
1087         default:
1088             // other packet type
1089             break;
1090     }
1091 }
1092 
1093 uint8_t avdtp_disconnect(uint16_t avdtp_cid){
1094     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1095     if (!connection) return AVDTP_CONNECTION_DOES_NOT_EXIST;
1096 
1097     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
1098 
1099     btstack_linked_list_iterator_t it;
1100     btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints());
1101 
1102     while (btstack_linked_list_iterator_has_next(&it)){
1103         avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
1104         if (stream_endpoint->connection != connection) continue;
1105 
1106         switch (stream_endpoint->state){
1107             case AVDTP_STREAM_ENDPOINT_OPENED:
1108             case AVDTP_STREAM_ENDPOINT_STREAMING:
1109                 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
1110                 l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
1111                 break;
1112             default:
1113                 break;
1114         }
1115     }
1116 
1117     connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
1118     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
1119     return ERROR_CODE_SUCCESS;
1120 }
1121 
1122 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){
1123     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1124     if (!connection){
1125         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
1126         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1127     }
1128 
1129     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
1130         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
1131         return AVDTP_CONNECTION_IN_WRONG_STATE;
1132     }
1133 
1134     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
1135     if (!stream_endpoint) {
1136         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
1137         return AVDTP_SEID_DOES_NOT_EXIST;
1138     }
1139 
1140     if (stream_endpoint->remote_sep.seid != remote_seid){
1141         log_error("avdtp_media_connect: no remote sep with seid %d registered with the stream endpoint", remote_seid);
1142         return AVDTP_SEID_DOES_NOT_EXIST;
1143     }
1144 
1145     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1146 
1147     connection->initiator_transaction_label++;
1148     connection->initiator_remote_seid = remote_seid;
1149     connection->initiator_local_seid = local_seid;
1150     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
1151     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
1152     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1153     return ERROR_CODE_SUCCESS;
1154 }
1155 
1156 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid){
1157     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1158     if (!connection){
1159         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
1160         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1161     }
1162 
1163     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
1164     if (!stream_endpoint) {
1165         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
1166         return AVDTP_SEID_DOES_NOT_EXIST;
1167     }
1168 
1169     if (stream_endpoint->l2cap_media_cid == 0){
1170         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
1171         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
1172     }
1173 
1174     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
1175         log_error("avdtp_media_connect: no remote sep registered with the stream endpoint");
1176         return AVDTP_SEID_DOES_NOT_EXIST;
1177     }
1178 
1179     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->start_stream == 1){
1180         return ERROR_CODE_COMMAND_DISALLOWED;
1181     }
1182 
1183     stream_endpoint->start_stream = 1;
1184     connection->initiator_local_seid = local_seid;
1185     connection->initiator_remote_seid = stream_endpoint->remote_sep.seid;
1186     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1187     return ERROR_CODE_SUCCESS;
1188 }
1189 
1190 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){
1191     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1192     if (!connection){
1193         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
1194         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1195     }
1196 
1197     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
1198     if (!stream_endpoint) {
1199         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
1200         return AVDTP_SEID_DOES_NOT_EXIST;
1201     }
1202 
1203     if (stream_endpoint->l2cap_media_cid == 0){
1204         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
1205         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
1206     }
1207 
1208     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->stop_stream){
1209         return ERROR_CODE_COMMAND_DISALLOWED;
1210     }
1211 
1212     stream_endpoint->stop_stream = 1;
1213     connection->initiator_local_seid = local_seid;
1214     connection->initiator_remote_seid = stream_endpoint->remote_sep.seid;
1215     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1216     return ERROR_CODE_SUCCESS;
1217 }
1218 
1219 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){
1220     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1221     if (!connection){
1222         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
1223         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1224     }
1225 
1226     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
1227     if (!stream_endpoint) {
1228         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
1229         return AVDTP_SEID_DOES_NOT_EXIST;
1230     }
1231 
1232     if (stream_endpoint->l2cap_media_cid == 0){
1233         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
1234         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
1235     }
1236 
1237     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->abort_stream){
1238         return ERROR_CODE_COMMAND_DISALLOWED;
1239     }
1240 
1241     stream_endpoint->abort_stream = 1;
1242     connection->initiator_local_seid = local_seid;
1243     connection->initiator_remote_seid = stream_endpoint->remote_sep.seid;
1244     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1245     return ERROR_CODE_SUCCESS;
1246 }
1247 
1248 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid){
1249     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1250     if (!connection){
1251         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
1252         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1253     }
1254     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid);
1255     if (!stream_endpoint) {
1256         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
1257         return AVDTP_SEID_DOES_NOT_EXIST;
1258     }
1259 
1260     if (stream_endpoint->l2cap_media_cid == 0){
1261         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
1262         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
1263     }
1264 
1265     if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->suspend_stream){
1266         return ERROR_CODE_COMMAND_DISALLOWED;
1267     }
1268 
1269     stream_endpoint->suspend_stream = 1;
1270     connection->initiator_local_seid = local_seid;
1271     connection->initiator_remote_seid = stream_endpoint->remote_sep.seid;
1272     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1273     return ERROR_CODE_SUCCESS;
1274 }
1275 
1276 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid){
1277     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1278     if (!connection){
1279         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
1280         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1281     }
1282     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1283         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1284         return AVDTP_CONNECTION_IN_WRONG_STATE;
1285     }
1286 
1287     connection->initiator_transaction_label++;
1288     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
1289     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1290 }
1291 
1292 
1293 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
1294     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1295     if (!connection){
1296         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1297         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1298     }
1299     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1300         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1301         return AVDTP_CONNECTION_IN_WRONG_STATE;
1302     }
1303 
1304     connection->initiator_transaction_label++;
1305     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
1306     connection->initiator_remote_seid = remote_seid;
1307     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1308 }
1309 
1310 
1311 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
1312     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1313     if (!connection){
1314         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1315         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1316     }
1317     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1318         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1319         return AVDTP_CONNECTION_IN_WRONG_STATE;
1320     }
1321 
1322     connection->initiator_transaction_label++;
1323     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
1324     connection->initiator_remote_seid = remote_seid;
1325     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1326 }
1327 
1328 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){
1329     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1330     if (!connection){
1331         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1332         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1333     }
1334     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1335         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1336         return AVDTP_CONNECTION_IN_WRONG_STATE;
1337     }
1338 
1339     connection->initiator_transaction_label++;
1340     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
1341     connection->initiator_remote_seid = remote_seid;
1342     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1343 }
1344 
1345 uint8_t avdtp_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
1346     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1347     if (!connection){
1348         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1349         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1350     }
1351     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1352         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1353         log_error("connection in wrong state, %d, initiator state %d", connection->state, connection->initiator_connection_state);
1354         return AVDTP_CONNECTION_IN_WRONG_STATE;
1355     }
1356     if (connection->configuration_state != AVDTP_CONFIGURATION_STATE_IDLE){
1357         log_info("configuration already started, config state %u", connection->configuration_state);
1358         return AVDTP_CONNECTION_IN_WRONG_STATE;
1359     }
1360 
1361     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
1362     if (!stream_endpoint) {
1363         log_error("No initiator stream endpoint for seid %d", local_seid);
1364         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
1365     }
1366     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_CONFIGURED){
1367         log_error("Stream endpoint seid %d in wrong state %d", local_seid, stream_endpoint->state);
1368         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1369     }
1370 
1371     connection->active_stream_endpoint = (void*) stream_endpoint;
1372     connection->configuration_state = AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED;
1373 
1374     connection->initiator_transaction_label++;
1375     connection->initiator_remote_seid = remote_seid;
1376     connection->initiator_local_seid = local_seid;
1377     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
1378     stream_endpoint->remote_configuration = configuration;
1379     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
1380 
1381     // cache media codec information for SBC
1382     stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type;
1383     if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){
1384         stream_endpoint->media_type = configuration.media_codec.media_type;
1385         (void)memcpy(stream_endpoint->media_codec_sbc_info,
1386                      configuration.media_codec.media_codec_information, 4);
1387     }
1388     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1389 }
1390 
1391 uint8_t avdtp_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
1392     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid);
1393     if (!connection){
1394         log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid);
1395         return AVDTP_CONNECTION_DOES_NOT_EXIST;
1396     }
1397     //TODO: if opened only app capabilities, enable reconfigure for not opened
1398     if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) ||
1399         (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) {
1400         return AVDTP_CONNECTION_IN_WRONG_STATE;
1401     }
1402 
1403     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
1404     if (!stream_endpoint) {
1405         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
1406         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
1407     }
1408 
1409     if (!is_avdtp_remote_seid_registered(stream_endpoint)){
1410         log_error("avdtp_reconfigure: no associated remote sep");
1411         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1412     }
1413 
1414     connection->initiator_transaction_label++;
1415     connection->initiator_remote_seid = remote_seid;
1416     connection->initiator_local_seid = local_seid;
1417     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
1418     stream_endpoint->remote_configuration = configuration;
1419     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
1420     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1421 }
1422 
1423 void    avdtp_set_preferred_sampling_frequeny(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency){
1424     stream_endpoint->preferred_sampling_frequency = sampling_frequency;
1425 }
1426 
1427 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
1428     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1429     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
1430 
1431     uint8_t channel_mode = AVDTP_SBC_STEREO;
1432     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
1433         channel_mode = AVDTP_SBC_JOINT_STEREO;
1434     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
1435         channel_mode = AVDTP_SBC_STEREO;
1436     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
1437         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1438     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1439         channel_mode = AVDTP_SBC_MONO;
1440     }
1441     return channel_mode;
1442 }
1443 
1444 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1445     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1446     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1447 
1448     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1449     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1450         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1451     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1452         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1453     }
1454     return allocation_method;
1455 }
1456 
1457 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1458     if (!stream_endpoint) return 0;
1459     return stream_endpoint->sep.seid;
1460 }
1461 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1462     if (!stream_endpoint) return 0;
1463     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1464     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1465 
1466     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1467     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1468         subbands = AVDTP_SBC_SUBBANDS_8;
1469     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1470         subbands = AVDTP_SBC_SUBBANDS_4;
1471     }
1472     return subbands;
1473 }
1474 
1475 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1476     if (!stream_endpoint) return 0;
1477     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1478     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1479 
1480     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1481     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1482         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1483     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1484         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1485     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1486         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1487     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1488         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1489     }
1490     return block_length;
1491 }
1492 
1493 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1494     if (!stream_endpoint) return 0;
1495     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1496     uint8_t supported_sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1497 
1498     // use preferred sampling frequency if possible
1499     if ((stream_endpoint->preferred_sampling_frequency == 48000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_48000)){
1500         return AVDTP_SBC_48000;
1501     }
1502     if ((stream_endpoint->preferred_sampling_frequency == 44100) && (supported_sampling_frequency_bitmap & AVDTP_SBC_44100)){
1503         return AVDTP_SBC_44100;
1504     }
1505     if ((stream_endpoint->preferred_sampling_frequency == 32000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_32000)){
1506         return AVDTP_SBC_32000;
1507     }
1508     if ((stream_endpoint->preferred_sampling_frequency == 16000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_16000)){
1509         return AVDTP_SBC_16000;
1510     }
1511 
1512     // otherwise, use highest available
1513     if (supported_sampling_frequency_bitmap & AVDTP_SBC_48000){
1514         return AVDTP_SBC_48000;
1515     }
1516     if (supported_sampling_frequency_bitmap & AVDTP_SBC_44100){
1517         return AVDTP_SBC_44100;
1518     }
1519     if (supported_sampling_frequency_bitmap & AVDTP_SBC_32000){
1520         return AVDTP_SBC_32000;
1521     }
1522     if (supported_sampling_frequency_bitmap & AVDTP_SBC_16000){
1523         return AVDTP_SBC_16000;
1524     }
1525     return AVDTP_SBC_44100; // some default
1526 }
1527 
1528 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1529     if (!stream_endpoint) return 0;
1530     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1531     return btstack_min(media_codec[3], remote_max_bitpool_value);
1532 }
1533 
1534 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1535     if (!stream_endpoint) return 0;
1536     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1537     return btstack_max(media_codec[2], remote_min_bitpool_value);
1538 }
1539 
1540 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint){
1541     if (!stream_endpoint) return 0;
1542     if (stream_endpoint->remote_sep.seid == 0) return 0;
1543     if (stream_endpoint->remote_sep.seid > 0x3E) return 0;
1544     return 1;
1545 }
1546