xref: /btstack/src/classic/avdtp.c (revision 360243be41f47158adff357b9fead2686419a2df)
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                         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));
595                         avdtp_streaming_emit_connection_established(context->avdtp_callback, context->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
596                         return;
597                     }
598                     break;
599 
600                 case L2CAP_EVENT_CHANNEL_CLOSED:
601                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
602                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
603                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
604                     log_info("received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
605 
606                     if (connection){
607                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
608                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
609                         btstack_linked_list_iterator_t it;
610                         btstack_linked_list_iterator_init(&it, stream_endpoints);
611                         while (btstack_linked_list_iterator_has_next(&it)){
612                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
613 
614                             if (_stream_endpoint->connection == connection){
615                                 avdtp_initialize_stream_endpoint(_stream_endpoint);
616                             }
617                         }
618                         btstack_memory_avdtp_connection_free(connection);
619                         break;
620                     }
621 
622                     if (stream_endpoint){
623                         if (stream_endpoint->l2cap_media_cid == local_cid){
624                             connection = stream_endpoint->connection;
625                             avdtp_streaming_emit_connection_released(context->avdtp_callback, context->avdtp_cid, avdtp_local_seid(stream_endpoint));
626                             stream_endpoint->l2cap_media_cid = 0;
627                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
628                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
629                             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
630                             stream_endpoint->remote_sep_index = 0;
631                             if (connection && connection->disconnect){
632                                 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
633                             }
634                             break;
635                         }
636                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
637                             log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
638                             stream_endpoint->l2cap_recovery_cid = 0;
639                             break;
640                         }
641 
642                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
643                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
644                             stream_endpoint->l2cap_reporting_cid = 0;
645                             break;
646                         }
647                     }
648                     break;
649 
650                 case HCI_EVENT_DISCONNECTION_COMPLETE:
651                     break;
652 
653                 case L2CAP_EVENT_CAN_SEND_NOW:
654                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
655                     if (!connection) {
656                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
657                         if (!stream_endpoint->connection) break;
658                         connection = stream_endpoint->connection;
659                     }
660                     avdtp_handle_can_send_now(connection, channel, context);
661                     break;
662                 default:
663                     log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet));
664                     break;
665             }
666             break;
667 
668         default:
669             // other packet type
670             break;
671     }
672 }
673 
674 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
675     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
676     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
677     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE){
678         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
679         return ERROR_CODE_SUCCESS;
680     }
681     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
682 
683     connection->disconnect = 1;
684     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
685     return ERROR_CODE_SUCCESS;
686 }
687 
688 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
689     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
690     if (!connection){
691         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
692         return AVDTP_CONNECTION_DOES_NOT_EXIST;
693     }
694     if (avdtp_find_remote_sep(connection, remote_seid) == 0xFF){
695         log_error("avdtp_media_connect: no remote sep for seid %d found", remote_seid);
696         return AVDTP_SEID_DOES_NOT_EXIST;
697     }
698 
699     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
700         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
701         return AVDTP_CONNECTION_IN_WRONG_STATE;
702     }
703 
704     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
705     if (!stream_endpoint) {
706         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
707         return AVDTP_SEID_DOES_NOT_EXIST;
708     }
709 
710     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
711     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return AVDTP_SEID_DOES_NOT_EXIST;
712 
713     connection->initiator_transaction_label++;
714     connection->remote_seid = remote_seid;
715     connection->local_seid = local_seid;
716     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
717     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
718     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
719     return ERROR_CODE_SUCCESS;
720 }
721 
722 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
723     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
724     if (!connection){
725         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
726         return AVDTP_CONNECTION_DOES_NOT_EXIST;
727     }
728 
729     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
730     if (!stream_endpoint) {
731         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
732         return AVDTP_SEID_DOES_NOT_EXIST;
733     }
734 
735     if (stream_endpoint->l2cap_media_cid == 0){
736         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
737         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
738     }
739 
740     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream){
741         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
742     }
743 
744     stream_endpoint->start_stream = 1;
745     connection->local_seid = local_seid;
746     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
747     return ERROR_CODE_SUCCESS;
748 }
749 
750 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
751     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
752     if (!connection){
753         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
754         return AVDTP_CONNECTION_DOES_NOT_EXIST;
755     }
756 
757     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
758     if (!stream_endpoint) {
759         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
760         return AVDTP_SEID_DOES_NOT_EXIST;
761     }
762 
763     if (stream_endpoint->l2cap_media_cid == 0){
764         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
765         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
766     }
767     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return ERROR_CODE_SUCCESS;
768 
769     stream_endpoint->stop_stream = 1;
770     connection->local_seid = local_seid;
771     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
772     return ERROR_CODE_SUCCESS;
773 }
774 
775 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
776     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
777     if (!connection){
778         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
779         return AVDTP_CONNECTION_DOES_NOT_EXIST;
780     }
781 
782     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
783     if (!stream_endpoint) {
784         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
785         return AVDTP_SEID_DOES_NOT_EXIST;
786     }
787 
788     if (stream_endpoint->l2cap_media_cid == 0){
789         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
790         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
791     }
792     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return ERROR_CODE_SUCCESS;
793 
794     stream_endpoint->abort_stream = 1;
795     connection->local_seid = local_seid;
796     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
797     return ERROR_CODE_SUCCESS;
798 }
799 
800 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
801     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
802     if (!connection){
803         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
804         return AVDTP_CONNECTION_DOES_NOT_EXIST;
805     }
806 
807     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
808     if (!stream_endpoint) {
809         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
810         return AVDTP_SEID_DOES_NOT_EXIST;
811     }
812 
813     if (stream_endpoint->l2cap_media_cid == 0){
814         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
815         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
816     }
817     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return ERROR_CODE_SUCCESS;
818 
819     stream_endpoint->suspend_stream = 1;
820     connection->local_seid = local_seid;
821     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
822     return ERROR_CODE_SUCCESS;
823 }
824 
825 void avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
826     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
827     if (!connection){
828         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
829         return;
830     }
831     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
832 
833     switch (connection->initiator_connection_state){
834         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
835             connection->initiator_transaction_label++;
836             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
837             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
838             break;
839         default:
840             log_error("avdtp_discover_stream_endpoints: wrong state");
841             break;
842     }
843 }
844 
845 
846 void avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
847     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
848     if (!connection){
849         log_error("avdtp_get_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
850         return;
851     }
852     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
853     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
854     connection->initiator_transaction_label++;
855     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
856     connection->remote_seid = remote_seid;
857     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
858 }
859 
860 
861 void avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
862     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
863     if (!connection){
864         log_error("avdtp_get_all_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
865         return;
866     }
867     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
868     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
869     connection->initiator_transaction_label++;
870     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
871     connection->remote_seid = remote_seid;
872     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
873 }
874 
875 void avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
876     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
877     if (!connection){
878         log_error("avdtp_get_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
879         return;
880     }
881     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
882     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
883     connection->initiator_transaction_label++;
884     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
885     connection->remote_seid = remote_seid;
886     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
887 }
888 
889 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){
890     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
891     if (!connection){
892         log_error("avdtp_set_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
893         return;
894     }
895     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
896     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
897 
898     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
899     if (!stream_endpoint) {
900         log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d", local_seid);
901         return;
902     }
903 
904     connection->initiator_transaction_label++;
905     connection->remote_seid = remote_seid;
906     connection->local_seid = local_seid;
907     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
908     stream_endpoint->remote_configuration = configuration;
909     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
910     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
911 }
912 
913 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){
914     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
915     if (!connection){
916         log_error("avdtp_reconfigure: no connection for AVDTP cid 0x%02x found", avdtp_cid);
917         return;
918     }
919     //TODO: if opened only app capabilities, enable reconfigure for not opened
920     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
921     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
922 
923     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
924     if (!stream_endpoint) {
925         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
926         return;
927     }
928 
929     if (stream_endpoint->remote_sep_index == 0xFF){
930         log_error("avdtp_reconfigure: no associated remote sep");
931         return;
932     }
933 
934     connection->initiator_transaction_label++;
935     connection->remote_seid = remote_seid;
936     connection->local_seid = local_seid;
937     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
938     stream_endpoint->remote_configuration = configuration;
939     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
940     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
941 }
942 
943 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){
944     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
945     if (!connection){
946         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
947         return 0;
948     }
949     return connection->remote_seps_num;
950 }
951 
952 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){
953     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
954     if (!connection){
955         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
956         return NULL;
957     }
958     return &connection->remote_seps[index];
959 }
960 
961 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){
962     UNUSED(packet_size);
963     if (storage_size < 4) {
964         log_error("storage must have 4 bytes");
965         return;
966     }
967     uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
968     uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
969     uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
970     uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
971 
972     uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
973     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));
974     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));
975 
976     config_storage[0] = (sampling_frequency << 4) | channel_mode;
977     config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method;
978     config_storage[2] = min_bitpool_value;
979     config_storage[3] = max_bitpool_value;
980 
981     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
982     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
983     stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
984     stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size;
985     stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage;
986 }
987 
988 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
989     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
990     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
991 
992     uint8_t channel_mode = AVDTP_SBC_STEREO;
993     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
994         channel_mode = AVDTP_SBC_JOINT_STEREO;
995     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
996         channel_mode = AVDTP_SBC_STEREO;
997     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
998         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
999     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1000         channel_mode = AVDTP_SBC_MONO;
1001     }
1002     return channel_mode;
1003 }
1004 
1005 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1006     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1007     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1008 
1009     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1010     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1011         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1012     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1013         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1014     }
1015     return allocation_method;
1016 }
1017 
1018 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1019     if (!stream_endpoint) return 0;
1020     return stream_endpoint->sep.seid;
1021 }
1022 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1023     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1024     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1025 
1026     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1027     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1028         subbands = AVDTP_SBC_SUBBANDS_8;
1029     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1030         subbands = AVDTP_SBC_SUBBANDS_4;
1031     }
1032     return subbands;
1033 }
1034 
1035 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1036     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1037     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1038 
1039     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1040     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1041         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1042     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1043         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1044     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1045         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1046     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1047         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1048     }
1049     return block_length;
1050 }
1051 
1052 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1053     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1054     uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1055 
1056     uint8_t sampling_frequency = AVDTP_SBC_44100;
1057     if (sampling_frequency_bitmap & AVDTP_SBC_48000){
1058         sampling_frequency = AVDTP_SBC_48000;
1059     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){
1060         sampling_frequency = AVDTP_SBC_44100;
1061     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){
1062         sampling_frequency = AVDTP_SBC_32000;
1063     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){
1064         sampling_frequency = AVDTP_SBC_16000;
1065     }
1066     return sampling_frequency;
1067 }
1068 
1069 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1070     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1071     return btstack_min(media_codec[3], remote_max_bitpool_value);
1072 }
1073 
1074 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1075     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1076     return btstack_max(media_codec[2], remote_min_bitpool_value);
1077 }
1078