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