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