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