xref: /btstack/src/classic/avdtp.c (revision dc01ccc6b23fb84d78b2e6c1f4a02f3559d9bd6a)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avdtp.c"
39 
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 #include "avdtp.h"
48 #include "avdtp_util.h"
49 #include "avdtp_acceptor.h"
50 #include "avdtp_initiator.h"
51 
52 static int record_id = -1;
53 static uint8_t   attribute_value[1000];
54 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
55 
56 // typedef struct {
57 //     btstack_linked_list_t * avdtp_connections;
58 //     avdtp_connection_t * connection;
59 //     btstack_packet_handler_t avdtp_callback;
60 //     avdtp_sep_type_t query_role;
61 //     btstack_packet_handler_t packet_handler;
62 //     uint16_t avdtp_l2cap_psm;
63 //     uint16_t avdtp_version;
64 //     uint8_t  role_supported;
65 // } avdtp_sdp_query_context_t;
66 
67 static avdtp_context_t * sdp_query_context;
68 static uint16_t avdtp_cid_counter = 0;
69 
70 static void (*handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
71 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
72 
73 static uint16_t avdtp_get_next_initiator_transaction_label(avdtp_context_t * context){
74     context->initiator_transaction_id_counter++;
75     if (context->initiator_transaction_id_counter == 0){
76         context->initiator_transaction_id_counter = 1;
77     }
78     return context->initiator_transaction_id_counter;
79 }
80 
81 static uint16_t avdtp_get_next_avdtp_cid(void){
82     avdtp_cid_counter++;
83     if (avdtp_cid_counter == 0){
84         avdtp_cid_counter = 1;
85     }
86     return avdtp_cid_counter;
87 }
88 
89 static uint16_t avdtp_get_next_local_seid(avdtp_context_t * context){
90     context->stream_endpoints_id_counter++;
91     if (context->stream_endpoints_id_counter == 0){
92         context->stream_endpoints_id_counter = 1;
93     }
94     return context->stream_endpoints_id_counter;
95 }
96 
97 uint8_t avdtp_connect(bd_addr_t remote, avdtp_sep_type_t query_role, avdtp_context_t * avdtp_context, uint16_t * avdtp_cid){
98     sdp_query_context = avdtp_context;
99     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(remote, avdtp_context);
100     if (!connection){
101         connection = avdtp_create_connection(remote, avdtp_context);
102         if (!connection){
103             log_error("avdtp: not enough memory to create connection");
104             return BTSTACK_MEMORY_ALLOC_FAILED;
105         }
106     }
107 
108     *avdtp_cid = connection->avdtp_cid;
109     if (!avdtp_cid) {
110         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
111     }
112     avdtp_context->avdtp_cid = connection->avdtp_cid;
113 
114     uint8_t err;
115     switch (connection->state){
116         case AVDTP_SIGNALING_CONNECTION_IDLE:
117             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE;
118             sdp_query_context = avdtp_context;
119             avdtp_context->avdtp_l2cap_psm = 0;
120             avdtp_context->avdtp_version = 0;
121             avdtp_context->query_role = query_role;
122             err = sdp_client_query_uuid16(&avdtp_handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_AVDTP);
123             if (err != ERROR_CODE_SUCCESS){
124                 connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
125                 btstack_linked_list_remove(&avdtp_context->connections, (btstack_linked_item_t*) connection);
126                 btstack_memory_avdtp_connection_free(connection);
127             }
128             return err;
129         case AVDTP_SIGNALING_CONNECTION_OPENED:{
130             avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid, avdtp_context);
131             if (stream_endpoint){
132                 avdtp_streaming_emit_connection_established(avdtp_context->avdtp_callback, connection->avdtp_cid, remote, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
133                 break;
134             }
135             avdtp_signaling_emit_connection_established(avdtp_context->avdtp_callback, avdtp_context->avdtp_cid, connection->remote_addr, ERROR_CODE_SUCCESS);
136             break;
137         }
138         default:
139             log_error("avdtp_connect: sink in wrong state");
140             return AVDTP_CONNECTION_IN_WRONG_STATE;
141 
142     }
143     return ERROR_CODE_SUCCESS;
144 }
145 
146 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){
147     if (!stream_endpoint){
148         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
149         return;
150     }
151     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
152     stream_endpoint->sep.registered_service_categories = bitmap;
153 }
154 
155 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
156     if (!stream_endpoint){
157         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
158         return;
159     }
160     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1);
161     stream_endpoint->sep.registered_service_categories = bitmap;
162 }
163 
164 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
165     if (!stream_endpoint){
166         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
167         return;
168     }
169     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
170     stream_endpoint->sep.registered_service_categories = bitmap;
171 }
172 
173 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
174     if (!stream_endpoint){
175         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
176         return;
177     }
178     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
179     stream_endpoint->sep.registered_service_categories = bitmap;
180     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
181     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
182     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
183 }
184 
185 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){
186     if (!stream_endpoint){
187         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
188         return;
189     }
190     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
191     stream_endpoint->sep.registered_service_categories = bitmap;
192     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
193     stream_endpoint->sep.capabilities.content_protection.cp_type_value = cp_type_value;
194     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = cp_type_value_len;
195 }
196 
197 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
198     if (!stream_endpoint){
199         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
200         return;
201     }
202     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
203     stream_endpoint->sep.registered_service_categories = bitmap;
204     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
205     stream_endpoint->sep.capabilities.header_compression.media = media;
206     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
207 }
208 
209 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){
210     if (!stream_endpoint){
211         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
212         return;
213     }
214     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
215     stream_endpoint->sep.registered_service_categories = bitmap;
216     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
217     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
218     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
219     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
220 }
221 
222 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
223     if (!stream_endpoint){
224         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
225         return;
226     }
227     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
228     stream_endpoint->sep.registered_service_categories = bitmap;
229     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
230 }
231 
232 
233 /* START: tracking can send now requests pro l2cap cid */
234 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
235     if (connection->wait_to_send_acceptor){
236         connection->wait_to_send_acceptor = 0;
237         avdtp_acceptor_stream_config_subsm_run(connection, context);
238     } else if (connection->wait_to_send_initiator){
239         connection->wait_to_send_initiator = 0;
240         avdtp_initiator_stream_config_subsm_run(connection, context);
241     } else if (connection->wait_to_send_self){
242         connection->wait_to_send_self = 0;
243         if (connection->disconnect){
244             btstack_linked_list_iterator_t it;
245             btstack_linked_list_iterator_init(&it, &context->stream_endpoints);
246             while (btstack_linked_list_iterator_has_next(&it)){
247                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
248                 if (stream_endpoint->connection == connection){
249                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
250                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
251                         avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
252                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
253                         return;
254                     }
255                 }
256             }
257             connection->disconnect = 0;
258             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
259             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
260             return;
261         }
262     }
263 
264     // re-register
265     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
266     if (more_to_send){
267         l2cap_request_can_send_now_event(l2cap_cid);
268     }
269 }
270 /* END: tracking can send now requests pro l2cap cid */
271 
272 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){
273     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
274     if (!connection){
275         log_error("avdtp: not enough memory to create connection");
276         return NULL;
277     }
278     memset(connection, 0, sizeof(avdtp_connection_t));
279     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
280     connection->initiator_transaction_label = avdtp_get_next_initiator_transaction_label(context);
281     connection->avdtp_cid = avdtp_get_next_avdtp_cid();
282     memcpy(connection->remote_addr, remote_addr, 6);
283     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
284     return connection;
285 }
286 
287 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){
288     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
289     if (!stream_endpoint){
290         log_error("avdtp: not enough memory to create stream endpoint");
291         return NULL;
292     }
293     memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t));
294     stream_endpoint->sep.seid = avdtp_get_next_local_seid(context);
295     stream_endpoint->sep.media_type = media_type;
296     stream_endpoint->sep.type = sep_type;
297     btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
298     return stream_endpoint;
299 }
300 
301 
302 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){
303     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
304     switch (connection->signaling_packet.message_type){
305         case AVDTP_CMD_MSG:
306             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
307             break;
308         default:
309             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
310             break;
311     }
312 }
313 
314 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
315     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(sdp_query_context->avdtp_cid, sdp_query_context);
316     if (!connection) {
317         log_error("avdtp: sdp query, connection with 0x%02x cid not found", sdp_query_context->avdtp_cid);
318         return;
319     }
320     if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return;
321 
322     UNUSED(packet_type);
323     UNUSED(channel);
324     UNUSED(size);
325 
326     des_iterator_t des_list_it;
327     des_iterator_t prot_it;
328     uint8_t status;
329 
330     switch (hci_event_packet_get_type(packet)){
331         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
332             // Handle new SDP record
333             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
334                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
335                 // log_info("SDP Record: Nr: %d", record_id);
336             }
337 
338             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
339                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
340 
341                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
342 
343                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
344                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
345                             if (de_get_element_type(attribute_value) != DE_DES) break;
346                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
347                                 uint8_t * element = des_iterator_get_element(&des_list_it);
348                                 if (de_get_element_type(element) != DE_UUID) continue;
349                                 uint32_t uuid = de_get_uuid32(element);
350                                 switch (uuid){
351                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
352                                         if (sdp_query_context->query_role == AVDTP_SOURCE) {
353                                             sdp_query_context->role_supported = 1;
354                                             break;
355                                         }
356                                         // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
357                                         // avdtp_remote_uuid = uuid;
358                                         break;
359                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
360                                         if (sdp_query_context->query_role == AVDTP_SINK) {
361                                             sdp_query_context->role_supported = 1;
362                                             break;
363                                         }
364                                         // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
365                                         // avdtp_remote_uuid = uuid;
366                                         break;
367                                     default:
368                                         break;
369                                 }
370                             }
371                             break;
372 
373                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
374                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
375                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
376                                     uint8_t       *des_element;
377                                     uint8_t       *element;
378                                     uint32_t       uuid;
379 
380                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
381 
382                                     des_element = des_iterator_get_element(&des_list_it);
383                                     des_iterator_init(&prot_it, des_element);
384                                     element = des_iterator_get_element(&prot_it);
385 
386                                     if (de_get_element_type(element) != DE_UUID) continue;
387 
388                                     uuid = de_get_uuid32(element);
389                                     switch (uuid){
390                                         case BLUETOOTH_PROTOCOL_L2CAP:
391                                             if (!des_iterator_has_more(&prot_it)) continue;
392                                             des_iterator_next(&prot_it);
393                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_l2cap_psm);
394                                             break;
395                                         case BLUETOOTH_PROTOCOL_AVDTP:
396                                             if (!des_iterator_has_more(&prot_it)) continue;
397                                             des_iterator_next(&prot_it);
398                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_version);
399                                             break;
400                                         default:
401                                             break;
402                                     }
403                                 }
404                             }
405                             break;
406                         default:
407                             break;
408                     }
409                 }
410             } else {
411                 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));
412             }
413             break;
414 
415         case SDP_EVENT_QUERY_COMPLETE:
416             status = sdp_event_query_complete_get_status(packet);
417             if (status != ERROR_CODE_SUCCESS){
418                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, status);
419                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
420                 btstack_memory_avdtp_connection_free(connection);
421                 log_info("AVDTP: SDP query failed with status 0x%02x.", status);
422                 break;
423             }
424             if (!sdp_query_context->role_supported){
425                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
426                 btstack_memory_avdtp_connection_free(connection);
427                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
428                 log_info("AVDTP: SDP query, remote device does not support required role.");
429                 break;
430             }
431             if (!sdp_query_context->avdtp_l2cap_psm) {
432                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*)connection);
433                 btstack_memory_avdtp_connection_free(connection);
434                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST);
435                 log_info("AVDTP: SDP query, no l2cap psm found.");
436                 break;
437             }
438             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
439             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
440             break;
441     }
442 }
443 
444 
445 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
446     bd_addr_t event_addr;
447     uint16_t psm;
448     uint16_t local_cid;
449     uint8_t  status;
450     avdtp_stream_endpoint_t * stream_endpoint = NULL;
451     avdtp_connection_t * connection = NULL;
452     btstack_linked_list_t * avdtp_connections = &context->connections;
453     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
454     handle_media_data = context->handle_media_data;
455     // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet));
456     switch (packet_type) {
457         case L2CAP_DATA_PACKET:
458             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
459             if (connection){
460                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
461                 break;
462             }
463 
464             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
465             if (!stream_endpoint){
466                 if (!connection) break;
467                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
468                 break;
469             }
470 
471             if (stream_endpoint->connection){
472                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
473                     int offset = avdtp_read_signaling_header(&stream_endpoint->connection->signaling_packet, packet, size);
474                     if (stream_endpoint->connection->signaling_packet.message_type == AVDTP_CMD_MSG){
475                         avdtp_acceptor_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
476                     } else {
477                         avdtp_initiator_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
478                     }
479                     break;
480                 }
481             }
482 
483             if (channel == stream_endpoint->l2cap_media_cid){
484                 if (handle_media_data){
485                     (*handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size);
486                 }
487                 break;
488             }
489 
490             if (channel == stream_endpoint->l2cap_reporting_cid){
491                 // TODO
492                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
493             } else if (channel == stream_endpoint->l2cap_recovery_cid){
494                 // TODO
495                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
496             } else {
497                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
498             }
499             break;
500 
501         case HCI_EVENT_PACKET:
502             switch (hci_event_packet_get_type(packet)) {
503                 case L2CAP_EVENT_INCOMING_CONNECTION:
504                     l2cap_event_incoming_connection_get_address(packet, event_addr);
505                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
506                     connection = avdtp_connection_for_bd_addr(event_addr, context);
507 
508                     if (!connection || connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED){
509                         connection = avdtp_create_connection(event_addr, context);
510                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
511                         log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d", connection, connection->state);
512                         l2cap_accept_connection(local_cid);
513                         break;
514                     }
515 
516                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
517                     if (!stream_endpoint) {
518                         log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->local_seid);
519                         break;
520                     }
521 
522                     if (stream_endpoint->l2cap_media_cid == 0){
523                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
524                         l2cap_accept_connection(local_cid);
525                         break;
526                     }
527                     break;
528 
529                 case L2CAP_EVENT_CHANNEL_OPENED:
530                     psm = l2cap_event_channel_opened_get_psm(packet);
531                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
532                         log_info("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
533                         return;
534                     }
535 
536                     status = l2cap_event_channel_opened_get_status(packet);
537                     // inform about new l2cap connection
538                     l2cap_event_channel_opened_get_address(packet, event_addr);
539                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
540                     connection = avdtp_connection_for_bd_addr(event_addr, context);
541 
542                     log_info("L2CAP_EVENT_CHANNEL_OPENED: status %d, cid 0x%02x , signaling connection %p \n", status, local_cid, connection);
543                     connection = avdtp_connection_for_bd_addr(event_addr, context);
544                     if (!connection){
545                         log_info("L2CAP_EVENT_CHANNEL_OPENED 2: status %d, signaling connection %p \n", status, connection);
546                         break;
547                     }
548 
549                     if (connection->l2cap_signaling_cid == 0) {
550                         if (status){
551                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
552                             connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
553                             connection->l2cap_signaling_cid = 0;
554                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, l2cap_event_channel_opened_get_status(packet));
555                             break;
556                         }
557                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) {
558                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
559                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, AVDTP_CONNECTION_IN_WRONG_STATE);
560                             break;
561                         }
562                         connection->l2cap_signaling_cid = local_cid;
563                         connection->local_seid = 0;
564                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
565                         log_info(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p, avdtp_cid 0x%02x", connection, local_cid);
566                         avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, 0);
567                         break;
568                     }
569 
570                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
571                     if (!stream_endpoint){
572                         log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
573                         return;
574                     }
575 
576                     if (stream_endpoint->l2cap_media_cid == 0){
577                         status = l2cap_event_channel_opened_get_status(packet);
578                         if (status){
579                             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, context->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
580                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
581                             avdtp_streaming_emit_connection_established(context->avdtp_callback, context->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
582                             break;
583                         }
584                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED){
585                             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));
586                             avdtp_streaming_emit_connection_established(context->avdtp_callback, context->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
587                             break;
588                         }
589 
590                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
591                         stream_endpoint->connection = connection;
592                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
593                         stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
594 
595                         log_info(" -> AVDTP_STREAM_ENDPOINT_OPENED, avdtp cid 0x%02x, l2cap_media_cid 0x%02x, local seid %d, remote seid %d", context->avdtp_cid, stream_endpoint->l2cap_media_cid, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint));
596                         avdtp_streaming_emit_connection_established(context->avdtp_callback, context->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
597                         return;
598                     }
599                     break;
600 
601                 case L2CAP_EVENT_CHANNEL_CLOSED:
602                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
603                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
604                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
605                     log_info("received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
606 
607                     if (connection){
608                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
609                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
610                         btstack_linked_list_iterator_t it;
611                         btstack_linked_list_iterator_init(&it, stream_endpoints);
612                         while (btstack_linked_list_iterator_has_next(&it)){
613                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
614 
615                             if (_stream_endpoint->connection == connection){
616                                 avdtp_initialize_stream_endpoint(_stream_endpoint);
617                             }
618                         }
619                         btstack_memory_avdtp_connection_free(connection);
620                         break;
621                     }
622 
623                     if (stream_endpoint){
624                         if (stream_endpoint->l2cap_media_cid == local_cid){
625                             connection = stream_endpoint->connection;
626                             avdtp_streaming_emit_connection_released(context->avdtp_callback, context->avdtp_cid, avdtp_local_seid(stream_endpoint));
627                             stream_endpoint->l2cap_media_cid = 0;
628                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
629                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
630                             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
631                             stream_endpoint->remote_sep_index = 0;
632                             if (connection && connection->disconnect){
633                                 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
634                             }
635                             break;
636                         }
637                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
638                             log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
639                             stream_endpoint->l2cap_recovery_cid = 0;
640                             break;
641                         }
642 
643                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
644                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
645                             stream_endpoint->l2cap_reporting_cid = 0;
646                             break;
647                         }
648                     }
649                     break;
650 
651                 case HCI_EVENT_DISCONNECTION_COMPLETE:
652                     break;
653 
654                 case L2CAP_EVENT_CAN_SEND_NOW:
655                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
656                     if (!connection) {
657                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
658                         if (!stream_endpoint->connection) break;
659                         connection = stream_endpoint->connection;
660                     }
661                     avdtp_handle_can_send_now(connection, channel, context);
662                     break;
663                 default:
664                     log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet));
665                     break;
666             }
667             break;
668 
669         default:
670             // other packet type
671             break;
672     }
673 }
674 
675 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
676     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
677     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
678     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE){
679         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
680         return ERROR_CODE_SUCCESS;
681     }
682     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
683 
684     connection->disconnect = 1;
685     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
686     return ERROR_CODE_SUCCESS;
687 }
688 
689 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
690     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
691     if (!connection){
692         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
693         return AVDTP_CONNECTION_DOES_NOT_EXIST;
694     }
695     if (avdtp_find_remote_sep(connection, remote_seid) == 0xFF){
696         log_error("avdtp_media_connect: no remote sep for seid %d found", remote_seid);
697         return AVDTP_SEID_DOES_NOT_EXIST;
698     }
699 
700     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
701         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
702         return AVDTP_CONNECTION_IN_WRONG_STATE;
703     }
704 
705     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
706     if (!stream_endpoint) {
707         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
708         return AVDTP_SEID_DOES_NOT_EXIST;
709     }
710 
711     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
712     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return AVDTP_SEID_DOES_NOT_EXIST;
713 
714     connection->initiator_transaction_label++;
715     connection->remote_seid = remote_seid;
716     connection->local_seid = local_seid;
717     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
718     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
719     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
720     return ERROR_CODE_SUCCESS;
721 }
722 
723 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
724     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
725     if (!connection){
726         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
727         return AVDTP_CONNECTION_DOES_NOT_EXIST;
728     }
729 
730     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
731     if (!stream_endpoint) {
732         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
733         return AVDTP_SEID_DOES_NOT_EXIST;
734     }
735 
736     if (stream_endpoint->l2cap_media_cid == 0){
737         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
738         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
739     }
740 
741     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream){
742         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
743     }
744 
745     stream_endpoint->start_stream = 1;
746     connection->local_seid = local_seid;
747     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
748     return ERROR_CODE_SUCCESS;
749 }
750 
751 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
752     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
753     if (!connection){
754         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
755         return AVDTP_CONNECTION_DOES_NOT_EXIST;
756     }
757 
758     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
759     if (!stream_endpoint) {
760         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
761         return AVDTP_SEID_DOES_NOT_EXIST;
762     }
763 
764     if (stream_endpoint->l2cap_media_cid == 0){
765         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
766         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
767     }
768     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return ERROR_CODE_SUCCESS;
769 
770     stream_endpoint->stop_stream = 1;
771     connection->local_seid = local_seid;
772     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
773     return ERROR_CODE_SUCCESS;
774 }
775 
776 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
777     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
778     if (!connection){
779         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
780         return AVDTP_CONNECTION_DOES_NOT_EXIST;
781     }
782 
783     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
784     if (!stream_endpoint) {
785         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
786         return AVDTP_SEID_DOES_NOT_EXIST;
787     }
788 
789     if (stream_endpoint->l2cap_media_cid == 0){
790         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
791         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
792     }
793     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return ERROR_CODE_SUCCESS;
794 
795     stream_endpoint->abort_stream = 1;
796     connection->local_seid = local_seid;
797     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
798     return ERROR_CODE_SUCCESS;
799 }
800 
801 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
802     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
803     if (!connection){
804         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
805         return AVDTP_CONNECTION_DOES_NOT_EXIST;
806     }
807 
808     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
809     if (!stream_endpoint) {
810         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
811         return AVDTP_SEID_DOES_NOT_EXIST;
812     }
813 
814     if (stream_endpoint->l2cap_media_cid == 0){
815         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
816         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
817     }
818     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return ERROR_CODE_SUCCESS;
819 
820     stream_endpoint->suspend_stream = 1;
821     connection->local_seid = local_seid;
822     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
823     return ERROR_CODE_SUCCESS;
824 }
825 
826 void avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
827     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
828     if (!connection){
829         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
830         return;
831     }
832     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
833 
834     switch (connection->initiator_connection_state){
835         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
836             connection->initiator_transaction_label++;
837             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
838             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
839             break;
840         default:
841             log_error("avdtp_discover_stream_endpoints: wrong state");
842             break;
843     }
844 }
845 
846 
847 void avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
848     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
849     if (!connection){
850         log_error("avdtp_get_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
851         return;
852     }
853     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
854     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
855     connection->initiator_transaction_label++;
856     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
857     connection->remote_seid = remote_seid;
858     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
859 }
860 
861 
862 void avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
863     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
864     if (!connection){
865         log_error("avdtp_get_all_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
866         return;
867     }
868     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
869     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
870     connection->initiator_transaction_label++;
871     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
872     connection->remote_seid = remote_seid;
873     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
874 }
875 
876 void avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
877     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
878     if (!connection){
879         log_error("avdtp_get_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
880         return;
881     }
882     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
883     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
884     connection->initiator_transaction_label++;
885     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
886     connection->remote_seid = remote_seid;
887     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
888 }
889 
890 void avdtp_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
891     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
892     if (!connection){
893         log_error("avdtp_set_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
894         return;
895     }
896     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
897     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
898 
899     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
900     if (!stream_endpoint) {
901         log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d", local_seid);
902         return;
903     }
904 
905     connection->initiator_transaction_label++;
906     connection->remote_seid = remote_seid;
907     connection->local_seid = local_seid;
908     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
909     stream_endpoint->remote_configuration = configuration;
910     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
911     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
912 }
913 
914 void avdtp_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){
915     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
916     if (!connection){
917         log_error("avdtp_reconfigure: no connection for AVDTP cid 0x%02x found", avdtp_cid);
918         return;
919     }
920     //TODO: if opened only app capabilities, enable reconfigure for not opened
921     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
922     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
923 
924     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
925     if (!stream_endpoint) {
926         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
927         return;
928     }
929 
930     if (stream_endpoint->remote_sep_index == 0xFF){
931         log_error("avdtp_reconfigure: no associated remote sep");
932         return;
933     }
934 
935     connection->initiator_transaction_label++;
936     connection->remote_seid = remote_seid;
937     connection->local_seid = local_seid;
938     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
939     stream_endpoint->remote_configuration = configuration;
940     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
941     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
942 }
943 
944 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){
945     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
946     if (!connection){
947         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
948         return 0;
949     }
950     return connection->remote_seps_num;
951 }
952 
953 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){
954     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
955     if (!connection){
956         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
957         return NULL;
958     }
959     return &connection->remote_seps[index];
960 }
961 
962 void avdtp_initialize_sbc_configuration_storage(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * config_storage, uint16_t storage_size, uint8_t * packet, uint16_t packet_size){
963     UNUSED(packet_size);
964     if (storage_size < 4) {
965         log_error("storage must have 4 bytes");
966         return;
967     }
968     uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
969     uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
970     uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
971     uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
972 
973     uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
974     uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
975     uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
976 
977     config_storage[0] = (sampling_frequency << 4) | channel_mode;
978     config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method;
979     config_storage[2] = min_bitpool_value;
980     config_storage[3] = max_bitpool_value;
981 
982     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
983     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
984     stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
985     stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size;
986     stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage;
987 }
988 
989 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
990     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
991     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
992 
993     uint8_t channel_mode = AVDTP_SBC_STEREO;
994     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
995         channel_mode = AVDTP_SBC_JOINT_STEREO;
996     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
997         channel_mode = AVDTP_SBC_STEREO;
998     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
999         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1000     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1001         channel_mode = AVDTP_SBC_MONO;
1002     }
1003     return channel_mode;
1004 }
1005 
1006 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1007     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1008     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1009 
1010     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1011     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1012         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1013     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1014         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1015     }
1016     return allocation_method;
1017 }
1018 
1019 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1020     if (!stream_endpoint) return 0;
1021     return stream_endpoint->sep.seid;
1022 }
1023 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1024     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1025     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1026 
1027     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1028     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1029         subbands = AVDTP_SBC_SUBBANDS_8;
1030     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1031         subbands = AVDTP_SBC_SUBBANDS_4;
1032     }
1033     return subbands;
1034 }
1035 
1036 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1037     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1038     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1039 
1040     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1041     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1042         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1043     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1044         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1045     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1046         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1047     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1048         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1049     }
1050     return block_length;
1051 }
1052 
1053 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1054     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1055     uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1056 
1057     uint8_t sampling_frequency = AVDTP_SBC_44100;
1058     if (sampling_frequency_bitmap & AVDTP_SBC_48000){
1059         sampling_frequency = AVDTP_SBC_48000;
1060     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){
1061         sampling_frequency = AVDTP_SBC_44100;
1062     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){
1063         sampling_frequency = AVDTP_SBC_32000;
1064     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){
1065         sampling_frequency = AVDTP_SBC_16000;
1066     }
1067     return sampling_frequency;
1068 }
1069 
1070 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1071     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1072     return btstack_min(media_codec[3], remote_max_bitpool_value);
1073 }
1074 
1075 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1076     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1077     return btstack_max(media_codec[2], remote_min_bitpool_value);
1078 }
1079