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