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