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