xref: /btstack/src/classic/avdtp.c (revision 5370c17f502640793e92e7e7548d61ee7d60c998)
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_info("stream_endpoint_state_machine: 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("stream_endpoint_state_machine: 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("stream_endpoint_state_machine: channel already opened\n");
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 
344                     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED){
345                         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));
346                         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);
347                         break;
348                     }
349 
350                     status = l2cap_event_channel_opened_get_status(packet);
351                     if (status){
352                         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));
353                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
354                         avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, address, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
355                         break;
356                     }
357 
358                     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
359                     stream_endpoint->connection = connection;
360                     stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
361                     stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
362                     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));
363                     avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, address, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
364                     break;
365 
366                 case L2CAP_EVENT_CHANNEL_CLOSED:
367                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
368                     if (stream_endpoint->l2cap_media_cid == local_cid){
369                         avdtp_streaming_emit_connection_released(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint));
370                         stream_endpoint->l2cap_media_cid = 0;
371                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
372                         stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
373                         stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
374                         stream_endpoint->remote_sep_index = 0;
375                         break;
376                     }
377                     if (stream_endpoint->l2cap_recovery_cid == local_cid){
378                         log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
379                         stream_endpoint->l2cap_recovery_cid = 0;
380                         break;
381                     }
382 
383                     if (stream_endpoint->l2cap_reporting_cid == local_cid){
384                         log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
385                         stream_endpoint->l2cap_reporting_cid = 0;
386                         break;
387                     }
388                     break;
389                 default:
390                     break;
391             }
392             break;
393         default:
394             break;
395     }
396 }
397 
398 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
399     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(sdp_query_context->avdtp_cid, sdp_query_context);
400     if (!connection) {
401         log_error("avdtp: sdp query, connection with 0x%02x cid not found", sdp_query_context->avdtp_cid);
402         return;
403     }
404     if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return;
405 
406     UNUSED(packet_type);
407     UNUSED(channel);
408     UNUSED(size);
409 
410     des_iterator_t des_list_it;
411     des_iterator_t prot_it;
412     uint8_t status;
413 
414     switch (hci_event_packet_get_type(packet)){
415         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
416             // Handle new SDP record
417             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
418                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
419                 // log_info("SDP Record: Nr: %d", record_id);
420             }
421 
422             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
423                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
424 
425                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
426 
427                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
428                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
429                             if (de_get_element_type(attribute_value) != DE_DES) break;
430                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
431                                 uint8_t * element = des_iterator_get_element(&des_list_it);
432                                 if (de_get_element_type(element) != DE_UUID) continue;
433                                 uint32_t uuid = de_get_uuid32(element);
434                                 switch (uuid){
435                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
436                                         if (sdp_query_context->query_role == AVDTP_SOURCE) {
437                                             sdp_query_context->role_supported = 1;
438                                             break;
439                                         }
440                                         // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
441                                         // avdtp_remote_uuid = uuid;
442                                         break;
443                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
444                                         if (sdp_query_context->query_role == AVDTP_SINK) {
445                                             sdp_query_context->role_supported = 1;
446                                             break;
447                                         }
448                                         // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
449                                         // avdtp_remote_uuid = uuid;
450                                         break;
451                                     default:
452                                         break;
453                                 }
454                             }
455                             break;
456 
457                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
458                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
459                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
460                                     uint8_t       *des_element;
461                                     uint8_t       *element;
462                                     uint32_t       uuid;
463 
464                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
465 
466                                     des_element = des_iterator_get_element(&des_list_it);
467                                     des_iterator_init(&prot_it, des_element);
468                                     element = des_iterator_get_element(&prot_it);
469 
470                                     if (de_get_element_type(element) != DE_UUID) continue;
471 
472                                     uuid = de_get_uuid32(element);
473                                     switch (uuid){
474                                         case BLUETOOTH_PROTOCOL_L2CAP:
475                                             if (!des_iterator_has_more(&prot_it)) continue;
476                                             des_iterator_next(&prot_it);
477                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_l2cap_psm);
478                                             break;
479                                         case BLUETOOTH_PROTOCOL_AVDTP:
480                                             if (!des_iterator_has_more(&prot_it)) continue;
481                                             des_iterator_next(&prot_it);
482                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_version);
483                                             break;
484                                         default:
485                                             break;
486                                     }
487                                 }
488                             }
489                             break;
490                         default:
491                             break;
492                     }
493                 }
494             } else {
495                 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));
496             }
497             break;
498 
499         case SDP_EVENT_QUERY_COMPLETE:
500             status = sdp_event_query_complete_get_status(packet);
501             if (status != ERROR_CODE_SUCCESS){
502                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, status);
503                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
504                 btstack_memory_avdtp_connection_free(connection);
505                 log_info("AVDTP: SDP query failed with status 0x%02x.", status);
506                 break;
507             }
508             if (!sdp_query_context->role_supported){
509                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
510                 btstack_memory_avdtp_connection_free(connection);
511                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
512                 log_info("AVDTP: SDP query, remote device does not support required role.");
513                 break;
514             }
515             if (!sdp_query_context->avdtp_l2cap_psm) {
516                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*)connection);
517                 btstack_memory_avdtp_connection_free(connection);
518                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST);
519                 log_info("AVDTP: SDP query, no l2cap psm found.");
520                 break;
521             }
522             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
523             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
524             break;
525     }
526 }
527 
528 
529 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
530     bd_addr_t event_addr;
531     uint16_t psm;
532     uint16_t local_cid;
533     uint8_t  status;
534     avdtp_stream_endpoint_t * stream_endpoint = NULL;
535     avdtp_connection_t * connection = NULL;
536     btstack_linked_list_t * avdtp_connections = &context->connections;
537     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
538     handle_media_data = context->handle_media_data;
539     // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet));
540     switch (packet_type) {
541         case L2CAP_DATA_PACKET:
542             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
543             if (connection){
544                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
545                 break;
546             }
547 
548             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
549             if (!stream_endpoint){
550                 if (!connection) break;
551                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
552                 break;
553             }
554 
555             if (stream_endpoint->connection){
556                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
557                     stream_endpoint_state_machine(stream_endpoint->connection, stream_endpoint, L2CAP_DATA_PACKET, 0, packet, size, context);
558                     break;
559                 }
560             }
561 
562             if (channel == stream_endpoint->l2cap_media_cid){
563                 if (handle_media_data){
564                     (*handle_media_data)(stream_endpoint, packet, size);
565                 }
566                 break;
567             }
568 
569             if (channel == stream_endpoint->l2cap_reporting_cid){
570                 // TODO
571                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
572             } else if (channel == stream_endpoint->l2cap_recovery_cid){
573                 // TODO
574                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
575             } else {
576                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
577             }
578             break;
579 
580         case HCI_EVENT_PACKET:
581             switch (hci_event_packet_get_type(packet)) {
582                 case L2CAP_EVENT_INCOMING_CONNECTION:
583                     l2cap_event_incoming_connection_get_address(packet, event_addr);
584                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
585                     connection = avdtp_connection_for_bd_addr(event_addr, context);
586 
587                     if (!connection || connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED){
588                         connection = avdtp_create_connection(event_addr, context);
589                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
590                         log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d", connection, connection->state);
591                         l2cap_accept_connection(local_cid);
592                         break;
593                     }
594 
595                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
596                     if (!stream_endpoint) {
597                         log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->local_seid);
598                         break;
599                     }
600 
601                     if (stream_endpoint->l2cap_media_cid == 0){
602                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
603                         l2cap_accept_connection(local_cid);
604                         break;
605                     }
606                     break;
607 
608                 case L2CAP_EVENT_CHANNEL_OPENED:
609                     status = l2cap_event_channel_opened_get_status(packet);
610                     // inform about new l2cap connection
611                     l2cap_event_channel_opened_get_address(packet, event_addr);
612                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
613                     if (status){
614                         log_error("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
615                         if (connection){
616                             connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
617                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, l2cap_event_channel_opened_get_status(packet));
618                             break;
619                         }
620                     }
621 
622                     psm = l2cap_event_channel_opened_get_psm(packet);
623                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
624                         log_error("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
625                         return;
626                     }
627                     // log_info("L2CAP_EVENT_CHANNEL_OPENED: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x",
628                            // bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_handle(packet), psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet));
629 
630                     if (psm != BLUETOOTH_PROTOCOL_AVDTP) break;
631 
632                     connection = avdtp_connection_for_bd_addr(event_addr, context);
633                     if (!connection) break;
634 
635                     if (connection->l2cap_signaling_cid == 0) {
636                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) break;
637                         connection->l2cap_signaling_cid = local_cid;
638                         connection->local_seid = 0;
639                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
640                         log_info(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p, avdtp_cid 0x%02x", connection, connection->avdtp_cid);
641                         avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, 0);
642                         break;
643                     }
644 
645                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
646                     if (!stream_endpoint){
647                         log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
648                         return;
649                     }
650                     stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_OPENED, packet, size, context);
651                     break;
652 
653                 case L2CAP_EVENT_CHANNEL_CLOSED:
654                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
655                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
656                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
657                     log_info("received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, %p, %p\n", local_cid, connection, stream_endpoint);
658 
659                     if (stream_endpoint){
660                         if (!connection){
661                             connection = stream_endpoint->connection;
662                         }
663                         stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_CLOSED, packet, size, context);
664                         break;
665                     }
666 
667                     if (connection){
668                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
669                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
670                         btstack_linked_list_iterator_t it;
671                         btstack_linked_list_iterator_init(&it, stream_endpoints);
672                         while (btstack_linked_list_iterator_has_next(&it)){
673                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
674 
675                             if (_stream_endpoint->connection == connection){
676                                 avdtp_initialize_stream_endpoint(_stream_endpoint);
677                             }
678                         }
679                         btstack_memory_avdtp_connection_free(connection);
680                         break;
681                     }
682                     break;
683 
684                 case HCI_EVENT_DISCONNECTION_COMPLETE:
685                     break;
686 
687                 case L2CAP_EVENT_CAN_SEND_NOW:
688                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
689                     if (!connection) {
690                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
691                         if (!stream_endpoint->connection) break;
692                         connection = stream_endpoint->connection;
693                     }
694                     avdtp_handle_can_send_now(connection, channel, context);
695                     break;
696                 default:
697                     log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet));
698                     break;
699             }
700             break;
701 
702         default:
703             // other packet type
704             break;
705     }
706 }
707 
708 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
709     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
710     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
711     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE) return AVDTP_CONNECTION_IN_WRONG_STATE;
712     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return AVDTP_CONNECTION_IN_WRONG_STATE;
713 
714     connection->disconnect = 1;
715     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
716     return ERROR_CODE_SUCCESS;
717 }
718 
719 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
720     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
721     if (!connection){
722         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
723         return AVDTP_CONNECTION_DOES_NOT_EXIST;
724     }
725     if (avdtp_find_remote_sep(connection, remote_seid) == 0xFF){
726         log_error("avdtp_media_connect: no remote sep for seid %d found", remote_seid);
727         return AVDTP_SEID_DOES_NOT_EXIST;
728     }
729 
730     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
731         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
732         return AVDTP_CONNECTION_IN_WRONG_STATE;
733     }
734 
735     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
736     if (!stream_endpoint) {
737         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
738         return AVDTP_SEID_DOES_NOT_EXIST;
739     }
740 
741     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
742     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return AVDTP_SEID_DOES_NOT_EXIST;
743 
744     connection->initiator_transaction_label++;
745     connection->remote_seid = remote_seid;
746     connection->local_seid = local_seid;
747     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
748     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
749     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
750     return ERROR_CODE_SUCCESS;
751 }
752 
753 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
754     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
755     if (!connection){
756         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
757         return AVDTP_CONNECTION_DOES_NOT_EXIST;
758     }
759 
760     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
761     if (!stream_endpoint) {
762         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
763         return AVDTP_SEID_DOES_NOT_EXIST;
764     }
765 
766     if (stream_endpoint->l2cap_media_cid == 0){
767         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
768         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
769     }
770 
771     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream){
772         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
773     }
774 
775     stream_endpoint->start_stream = 1;
776     connection->local_seid = local_seid;
777     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
778     return ERROR_CODE_SUCCESS;
779 }
780 
781 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
782     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
783     if (!connection){
784         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
785         return AVDTP_CONNECTION_DOES_NOT_EXIST;
786     }
787 
788     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
789     if (!stream_endpoint) {
790         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
791         return AVDTP_SEID_DOES_NOT_EXIST;
792     }
793 
794     if (stream_endpoint->l2cap_media_cid == 0){
795         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
796         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
797     }
798     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return ERROR_CODE_SUCCESS;
799 
800     stream_endpoint->stop_stream = 1;
801     connection->local_seid = local_seid;
802     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
803     return ERROR_CODE_SUCCESS;
804 }
805 
806 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
807     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
808     if (!connection){
809         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
810         return AVDTP_CONNECTION_DOES_NOT_EXIST;
811     }
812 
813     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
814     if (!stream_endpoint) {
815         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
816         return AVDTP_SEID_DOES_NOT_EXIST;
817     }
818 
819     if (stream_endpoint->l2cap_media_cid == 0){
820         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
821         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
822     }
823     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return ERROR_CODE_SUCCESS;
824 
825     stream_endpoint->abort_stream = 1;
826     connection->local_seid = local_seid;
827     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
828     return ERROR_CODE_SUCCESS;
829 }
830 
831 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
832     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
833     if (!connection){
834         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
835         return AVDTP_CONNECTION_DOES_NOT_EXIST;
836     }
837 
838     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
839     if (!stream_endpoint) {
840         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
841         return AVDTP_SEID_DOES_NOT_EXIST;
842     }
843 
844     if (stream_endpoint->l2cap_media_cid == 0){
845         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
846         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
847     }
848     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return ERROR_CODE_SUCCESS;
849 
850     stream_endpoint->suspend_stream = 1;
851     connection->local_seid = local_seid;
852     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
853     return ERROR_CODE_SUCCESS;
854 }
855 
856 void avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
857     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
858     if (!connection){
859         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
860         return;
861     }
862     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
863 
864     switch (connection->initiator_connection_state){
865         case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE:
866             connection->initiator_transaction_label++;
867             connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
868             avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
869             break;
870         default:
871             log_error("avdtp_discover_stream_endpoints: wrong state");
872             break;
873     }
874 }
875 
876 
877 void avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
878     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
879     if (!connection){
880         log_error("avdtp_get_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
881         return;
882     }
883     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
884     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
885     connection->initiator_transaction_label++;
886     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
887     connection->remote_seid = remote_seid;
888     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
889 }
890 
891 
892 void avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
893     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
894     if (!connection){
895         log_error("avdtp_get_all_capabilities: no connection for AVDTP cid 0x%02x found", avdtp_cid);
896         return;
897     }
898     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
899     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
900     connection->initiator_transaction_label++;
901     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
902     connection->remote_seid = remote_seid;
903     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
904 }
905 
906 void avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
907     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
908     if (!connection){
909         log_error("avdtp_get_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
910         return;
911     }
912     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
913     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
914     connection->initiator_transaction_label++;
915     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
916     connection->remote_seid = remote_seid;
917     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
918 }
919 
920 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){
921     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
922     if (!connection){
923         log_error("avdtp_set_configuration: no connection for AVDTP cid 0x%02x found", avdtp_cid);
924         return;
925     }
926     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return;
927     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
928 
929     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
930     if (!stream_endpoint) {
931         log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d", local_seid);
932         return;
933     }
934 
935     connection->initiator_transaction_label++;
936     connection->remote_seid = remote_seid;
937     connection->local_seid = local_seid;
938     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
939     stream_endpoint->remote_configuration = configuration;
940     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
941     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
942 }
943 
944 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){
945     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
946     if (!connection){
947         log_error("avdtp_reconfigure: no connection for AVDTP cid 0x%02x found", avdtp_cid);
948         return;
949     }
950     //TODO: if opened only app capabilities, enable reconfigure for not opened
951     if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return;
952     if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return;
953 
954     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
955     if (!stream_endpoint) {
956         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
957         return;
958     }
959 
960     if (stream_endpoint->remote_sep_index == 0xFF){
961         log_error("avdtp_reconfigure: no associated remote sep");
962         return;
963     }
964 
965     connection->initiator_transaction_label++;
966     connection->remote_seid = remote_seid;
967     connection->local_seid = local_seid;
968     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
969     stream_endpoint->remote_configuration = configuration;
970     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
971     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
972 }
973 
974 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){
975     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
976     if (!connection){
977         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
978         return 0;
979     }
980     return connection->remote_seps_num;
981 }
982 
983 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){
984     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
985     if (!connection){
986         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
987         return NULL;
988     }
989     return &connection->remote_seps[index];
990 }
991 
992 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){
993     UNUSED(packet_size);
994     if (storage_size < 4) {
995         log_error("storage must have 4 bytes");
996         return;
997     }
998     uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
999     uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
1000     uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
1001     uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
1002 
1003     uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
1004     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));
1005     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));
1006 
1007     config_storage[0] = (sampling_frequency << 4) | channel_mode;
1008     config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method;
1009     config_storage[2] = min_bitpool_value;
1010     config_storage[3] = max_bitpool_value;
1011 
1012     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
1013     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
1014     stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
1015     stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size;
1016     stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage;
1017 }
1018 
1019 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
1020     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1021     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
1022 
1023     uint8_t channel_mode = AVDTP_SBC_STEREO;
1024     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
1025         channel_mode = AVDTP_SBC_JOINT_STEREO;
1026     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
1027         channel_mode = AVDTP_SBC_STEREO;
1028     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
1029         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1030     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1031         channel_mode = AVDTP_SBC_MONO;
1032     }
1033     return channel_mode;
1034 }
1035 
1036 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1037     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1038     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1039 
1040     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1041     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1042         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1043     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1044         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1045     }
1046     return allocation_method;
1047 }
1048 
1049 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1050     if (!stream_endpoint) return 0;
1051     return stream_endpoint->sep.seid;
1052 }
1053 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1054     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1055     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1056 
1057     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1058     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1059         subbands = AVDTP_SBC_SUBBANDS_8;
1060     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1061         subbands = AVDTP_SBC_SUBBANDS_4;
1062     }
1063     return subbands;
1064 }
1065 
1066 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1067     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1068     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1069 
1070     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1071     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1072         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1073     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1074         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1075     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1076         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1077     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1078         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1079     }
1080     return block_length;
1081 }
1082 
1083 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1084     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1085     uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1086 
1087     uint8_t sampling_frequency = AVDTP_SBC_44100;
1088     if (sampling_frequency_bitmap & AVDTP_SBC_48000){
1089         sampling_frequency = AVDTP_SBC_48000;
1090     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){
1091         sampling_frequency = AVDTP_SBC_44100;
1092     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){
1093         sampling_frequency = AVDTP_SBC_32000;
1094     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){
1095         sampling_frequency = AVDTP_SBC_16000;
1096     }
1097     return sampling_frequency;
1098 }
1099 
1100 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1101     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1102     return btstack_min(media_codec[3], remote_max_bitpool_value);
1103 }
1104 
1105 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1106     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1107     return btstack_max(media_codec[2], remote_min_bitpool_value);
1108 }
1109