xref: /btstack/src/classic/avdtp.c (revision e6c9673776c7f85e24c76da7f4b1b83031102d2b)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avdtp.c"
39 
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 #include "classic/avdtp.h"
48 #include "classic/avdtp_util.h"
49 #include "classic/avdtp_acceptor.h"
50 #include "classic/avdtp_initiator.h"
51 
52 #define CONFIGURATION_TIMEOUT_MS 300
53 
54 static int record_id = -1;
55 static uint8_t   attribute_value[1000];
56 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
57 
58 // typedef struct {
59 //     btstack_linked_list_t * avdtp_connections;
60 //     avdtp_connection_t * connection;
61 //     btstack_packet_handler_t avdtp_callback;
62 //     avdtp_sep_type_t query_role;
63 //     btstack_packet_handler_t packet_handler;
64 //     uint16_t avdtp_l2cap_psm;
65 //     uint16_t avdtp_version;
66 //     uint8_t  role_supported;
67 // } avdtp_sdp_query_context_t;
68 
69 static avdtp_context_t * sdp_query_context;
70 static uint16_t avdtp_cid_counter = 0x55;
71 
72 static void (*handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size);
73 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
74 
75 void avdtp_configuration_timeout_handler(btstack_timer_source_t * timer){
76     avdtp_connection_t * connection = (avdtp_connection_t *) btstack_run_loop_get_timer_context(timer);
77     if (!connection){
78         log_error("Context of avdtp_configuration_timeout_handler is NULL");
79         return;
80     }
81     avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t*) connection->active_stream_endpoint;
82     if (!stream_endpoint) {
83         log_error("avdtp_configuration_timeout_handler: no initiator stream endpoint for seid %d", connection->local_seid);
84         return;
85     }
86     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE) return;
87     connection->is_configuration_initiated_locally = 1;
88     connection->is_initiator = 1;
89     connection->initiator_transaction_label++;
90     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
91     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
92 }
93 
94 void avdtp_configuration_timer_start(avdtp_connection_t * connection){
95     avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t*) connection->active_stream_endpoint;
96     if (!stream_endpoint) {
97         log_error("avdtp_configuration_timeout_handler: no initiator stream endpoint for seid %d", connection->local_seid);
98         return;
99     }
100     if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_CONFIGURATION_SUBSTATEMACHINE) return;
101 
102     btstack_run_loop_remove_timer(&connection->configuration_timer);
103     btstack_run_loop_set_timer_handler(&connection->configuration_timer, avdtp_configuration_timeout_handler);
104     btstack_run_loop_set_timer_context(&connection->configuration_timer, connection);
105     btstack_run_loop_set_timer(&connection->configuration_timer, CONFIGURATION_TIMEOUT_MS);
106     btstack_run_loop_add_timer(&connection->configuration_timer);
107 }
108 
109 void avdtp_configuration_timer_stop(avdtp_connection_t * connection){
110     btstack_run_loop_remove_timer(&connection->configuration_timer);
111 }
112 
113 static uint16_t avdtp_get_next_initiator_transaction_label(avdtp_context_t * context){
114     context->initiator_transaction_id_counter++;
115     if (context->initiator_transaction_id_counter == 0){
116         context->initiator_transaction_id_counter = 1;
117     }
118     return context->initiator_transaction_id_counter;
119 }
120 
121 static uint16_t avdtp_get_next_avdtp_cid(void){
122     avdtp_cid_counter++;
123     if (avdtp_cid_counter == 0){
124         avdtp_cid_counter = 1;
125     }
126     return avdtp_cid_counter;
127 }
128 
129 static uint16_t avdtp_get_next_local_seid(avdtp_context_t * context){
130     context->stream_endpoints_id_counter++;
131     if (context->stream_endpoints_id_counter == 0){
132         context->stream_endpoints_id_counter = 1;
133     }
134     return context->stream_endpoints_id_counter;
135 }
136 
137 uint8_t avdtp_connect(bd_addr_t remote, avdtp_sep_type_t query_role, avdtp_context_t * avdtp_context, uint16_t * avdtp_cid){
138     sdp_query_context = avdtp_context;
139     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(remote, avdtp_context);
140     if (!connection){
141         connection = avdtp_create_connection(remote, avdtp_context);
142         if (!connection){
143             log_error("avdtp: not enough memory to create connection");
144             return BTSTACK_MEMORY_ALLOC_FAILED;
145         }
146     }
147 
148     *avdtp_cid = connection->avdtp_cid;
149     if (!avdtp_cid) {
150         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
151     }
152     avdtp_context->avdtp_cid = connection->avdtp_cid;
153 
154     uint8_t err;
155     switch (connection->state){
156         case AVDTP_SIGNALING_CONNECTION_IDLE:
157             connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE;
158             connection->is_initiator = 1;
159             log_info("avdtp_connect: role is_initiator %d", connection->is_initiator);
160             sdp_query_context = avdtp_context;
161             avdtp_context->avdtp_l2cap_psm = 0;
162             avdtp_context->avdtp_version = 0;
163             avdtp_context->query_role = query_role;
164             err = sdp_client_query_uuid16(&avdtp_handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_AVDTP);
165             if (err != ERROR_CODE_SUCCESS){
166                 connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
167                 btstack_linked_list_remove(&avdtp_context->connections, (btstack_linked_item_t*) connection);
168                 btstack_memory_avdtp_connection_free(connection);
169             }
170             return err;
171         case AVDTP_SIGNALING_CONNECTION_OPENED:{
172             avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid, avdtp_context);
173             if (stream_endpoint){
174                 avdtp_streaming_emit_connection_established(avdtp_context->avdtp_callback, connection->avdtp_cid, remote, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
175                 break;
176             }
177             avdtp_signaling_emit_connection_established(avdtp_context->avdtp_callback, connection->avdtp_cid, connection->remote_addr, ERROR_CODE_SUCCESS);
178             break;
179         }
180         default:
181             log_error("avdtp_connect: sink in wrong state");
182             return AVDTP_CONNECTION_IN_WRONG_STATE;
183 
184     }
185     return ERROR_CODE_SUCCESS;
186 }
187 
188 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){
189     if (!stream_endpoint){
190         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
191         return;
192     }
193     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1);
194     stream_endpoint->sep.registered_service_categories = bitmap;
195 }
196 
197 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
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_REPORTING, 1);
203     stream_endpoint->sep.registered_service_categories = bitmap;
204 }
205 
206 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){
207     if (!stream_endpoint){
208         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
209         return;
210     }
211     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1);
212     stream_endpoint->sep.registered_service_categories = bitmap;
213 }
214 
215 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
216     if (!stream_endpoint){
217         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
218         return;
219     }
220     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1);
221     stream_endpoint->sep.registered_service_categories = bitmap;
222     stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733
223     stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size;
224     stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets;
225 }
226 
227 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){
228     if (!stream_endpoint){
229         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
230         return;
231     }
232     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1);
233     stream_endpoint->sep.registered_service_categories = bitmap;
234     stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type;
235     memcpy(stream_endpoint->sep.capabilities.content_protection.cp_type_value, cp_type_value, btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN));
236     stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN);
237 }
238 
239 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){
240     if (!stream_endpoint){
241         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
242         return;
243     }
244     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1);
245     stream_endpoint->sep.registered_service_categories = bitmap;
246     stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch;
247     stream_endpoint->sep.capabilities.header_compression.media = media;
248     stream_endpoint->sep.capabilities.header_compression.recovery = recovery;
249 }
250 
251 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){
252     if (!stream_endpoint){
253         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
254         return;
255     }
256     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1);
257     stream_endpoint->sep.registered_service_categories = bitmap;
258     stream_endpoint->sep.capabilities.media_codec.media_type = media_type;
259     stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type;
260     stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info;
261     stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len;
262 }
263 
264 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){
265     if (!stream_endpoint){
266         log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered");
267         return;
268     }
269     uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1);
270     stream_endpoint->sep.registered_service_categories = bitmap;
271     stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation;
272 }
273 
274 
275 /* START: tracking can send now requests pro l2cap cid */
276 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){
277     if (connection->wait_to_send_acceptor){
278         connection->wait_to_send_acceptor = 0;
279         avdtp_acceptor_stream_config_subsm_run(connection, context);
280     } else if (connection->wait_to_send_initiator){
281         connection->wait_to_send_initiator = 0;
282         avdtp_initiator_stream_config_subsm_run(connection, context);
283     } else if (connection->wait_to_send_self){
284         connection->wait_to_send_self = 0;
285         if (connection->disconnect){
286             btstack_linked_list_iterator_t it;
287             btstack_linked_list_iterator_init(&it, &context->stream_endpoints);
288             while (btstack_linked_list_iterator_has_next(&it)){
289                 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
290                 if (stream_endpoint->connection == connection){
291                     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){
292                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED;
293                         avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
294                         l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0);
295                         return;
296                     }
297                 }
298             }
299             connection->disconnect = 0;
300             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED;
301             l2cap_disconnect(connection->l2cap_signaling_cid, 0);
302             return;
303         }
304     }
305 
306     // re-register
307     int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self;
308     if (more_to_send){
309         l2cap_request_can_send_now_event(l2cap_cid);
310     }
311 }
312 /* END: tracking can send now requests pro l2cap cid */
313 
314 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){
315     avdtp_connection_t * connection = btstack_memory_avdtp_connection_get();
316     if (!connection){
317         log_error("avdtp: not enough memory to create connection");
318         return NULL;
319     }
320     memset(connection, 0, sizeof(avdtp_connection_t));
321     connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
322     connection->initiator_transaction_label = avdtp_get_next_initiator_transaction_label(context);
323     connection->avdtp_cid = avdtp_get_next_avdtp_cid();
324     context->avdtp_cid = connection->avdtp_cid;
325     memcpy(connection->remote_addr, remote_addr, 6);
326     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
327     return connection;
328 }
329 
330 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){
331     avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get();
332     if (!stream_endpoint){
333         log_error("avdtp: not enough memory to create stream endpoint");
334         return NULL;
335     }
336     memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t));
337     stream_endpoint->sep.seid = avdtp_get_next_local_seid(context);
338     stream_endpoint->sep.media_type = media_type;
339     stream_endpoint->sep.type = sep_type;
340     btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint);
341     return stream_endpoint;
342 }
343 
344 
345 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){
346     int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size);
347     switch (connection->signaling_packet.message_type){
348         case AVDTP_CMD_MSG:
349             avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context);
350             break;
351         default:
352             avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context);
353             break;
354     }
355 }
356 
357 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
358     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(sdp_query_context->avdtp_cid, sdp_query_context);
359     if (!connection) {
360         log_error("avdtp: sdp query, connection with 0x%02x cid not found", sdp_query_context->avdtp_cid);
361         return;
362     }
363     if (connection->state != AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return;
364 
365     UNUSED(packet_type);
366     UNUSED(channel);
367     UNUSED(size);
368 
369     des_iterator_t des_list_it;
370     des_iterator_t prot_it;
371     uint8_t status;
372 
373     switch (hci_event_packet_get_type(packet)){
374         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
375             // Handle new SDP record
376             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
377                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
378                 // log_info("SDP Record: Nr: %d", record_id);
379             }
380 
381             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
382                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
383 
384                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
385 
386                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
387                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
388                             if (de_get_element_type(attribute_value) != DE_DES) break;
389                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
390                                 uint8_t * element = des_iterator_get_element(&des_list_it);
391                                 if (de_get_element_type(element) != DE_UUID) continue;
392                                 uint32_t uuid = de_get_uuid32(element);
393                                 switch (uuid){
394                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE:
395                                         if (sdp_query_context->query_role == AVDTP_SOURCE) {
396                                             sdp_query_context->role_supported = 1;
397                                             break;
398                                         }
399                                         // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
400                                         // avdtp_remote_uuid = uuid;
401                                         break;
402                                     case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK:
403                                         if (sdp_query_context->query_role == AVDTP_SINK) {
404                                             sdp_query_context->role_supported = 1;
405                                             break;
406                                         }
407                                         // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
408                                         // avdtp_remote_uuid = uuid;
409                                         break;
410                                     default:
411                                         break;
412                                 }
413                             }
414                             break;
415 
416                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
417                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
418                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
419                                     uint8_t       *des_element;
420                                     uint8_t       *element;
421                                     uint32_t       uuid;
422 
423                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
424 
425                                     des_element = des_iterator_get_element(&des_list_it);
426                                     des_iterator_init(&prot_it, des_element);
427                                     element = des_iterator_get_element(&prot_it);
428 
429                                     if (de_get_element_type(element) != DE_UUID) continue;
430 
431                                     uuid = de_get_uuid32(element);
432                                     switch (uuid){
433                                         case BLUETOOTH_PROTOCOL_L2CAP:
434                                             if (!des_iterator_has_more(&prot_it)) continue;
435                                             des_iterator_next(&prot_it);
436                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_l2cap_psm);
437                                             break;
438                                         case BLUETOOTH_PROTOCOL_AVDTP:
439                                             if (!des_iterator_has_more(&prot_it)) continue;
440                                             des_iterator_next(&prot_it);
441                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avdtp_version);
442                                             break;
443                                         default:
444                                             break;
445                                     }
446                                 }
447                             }
448                             break;
449                         default:
450                             break;
451                     }
452                 }
453             } else {
454                 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));
455             }
456             break;
457 
458         case SDP_EVENT_QUERY_COMPLETE:
459             status = sdp_event_query_complete_get_status(packet);
460             if (status != ERROR_CODE_SUCCESS){
461                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, status);
462                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
463                 btstack_memory_avdtp_connection_free(connection);
464                 log_info("AVDTP: SDP query failed with status 0x%02x.", status);
465                 break;
466             }
467             if (!sdp_query_context->role_supported){
468                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection);
469                 btstack_memory_avdtp_connection_free(connection);
470                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
471                 log_info("AVDTP: SDP query, remote device does not support required role.");
472                 break;
473             }
474             if (!sdp_query_context->avdtp_l2cap_psm) {
475                 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*)connection);
476                 btstack_memory_avdtp_connection_free(connection);
477                 avdtp_signaling_emit_connection_established(sdp_query_context->avdtp_callback, sdp_query_context->avdtp_cid, connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST);
478                 log_info("AVDTP: SDP query, no l2cap psm found.");
479                 break;
480             }
481             connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
482             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avdtp_l2cap_psm, l2cap_max_mtu(), NULL);
483             break;
484     }
485 }
486 
487 
488 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){
489     bd_addr_t event_addr;
490     uint16_t psm;
491     uint16_t local_cid;
492     uint8_t  status;
493     avdtp_stream_endpoint_t * stream_endpoint = NULL;
494     avdtp_connection_t * connection = NULL;
495     btstack_linked_list_t * avdtp_connections = &context->connections;
496     btstack_linked_list_t * stream_endpoints =  &context->stream_endpoints;
497     handle_media_data = context->handle_media_data;
498     // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet));
499     switch (packet_type) {
500         case L2CAP_DATA_PACKET:
501             connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
502             if (connection){
503                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
504                 break;
505             }
506 
507             stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
508             if (!stream_endpoint){
509                 if (!connection) break;
510                 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context);
511                 break;
512             }
513 
514             if (stream_endpoint->connection){
515                 if (channel == stream_endpoint->connection->l2cap_signaling_cid){
516                     int offset = avdtp_read_signaling_header(&stream_endpoint->connection->signaling_packet, packet, size);
517                     if (stream_endpoint->connection->signaling_packet.message_type == AVDTP_CMD_MSG){
518                         avdtp_acceptor_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
519                     } else {
520                         avdtp_initiator_stream_config_subsm(stream_endpoint->connection, packet, size, offset, context);
521                     }
522                     break;
523                 }
524             }
525 
526             if (channel == stream_endpoint->l2cap_media_cid){
527                 if (handle_media_data){
528                     (*handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size);
529                 }
530                 break;
531             }
532 
533             if (channel == stream_endpoint->l2cap_reporting_cid){
534                 // TODO
535                 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED");
536             } else if (channel == stream_endpoint->l2cap_recovery_cid){
537                 // TODO
538                 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED");
539             } else {
540                 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel);
541             }
542             break;
543 
544         case HCI_EVENT_PACKET:
545             switch (hci_event_packet_get_type(packet)) {
546                 case L2CAP_EVENT_INCOMING_CONNECTION:
547                     l2cap_event_incoming_connection_get_address(packet, event_addr);
548                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
549                     connection = avdtp_connection_for_bd_addr(event_addr, context);
550 
551                     if (!connection || connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED){
552                         connection = avdtp_create_connection(event_addr, context);
553                         connection->is_initiator = 0;
554                         log_info("L2CAP_EVENT_INCOMING_CONNECTION: role is_initiator %d", connection->is_initiator);
555                         connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
556                         log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d, avdtp cid 0x%02x", connection, connection->state, connection->avdtp_cid);
557                         l2cap_accept_connection(local_cid);
558                         break;
559                     }
560 
561                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
562                     if (!stream_endpoint) {
563                         log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->local_seid);
564                         break;
565                     }
566 
567                     if (stream_endpoint->l2cap_media_cid == 0){
568                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break;
569                         l2cap_accept_connection(local_cid);
570                         break;
571                     }
572                     break;
573 
574                 case L2CAP_EVENT_CHANNEL_OPENED:
575                     psm = l2cap_event_channel_opened_get_psm(packet);
576                     if (psm != BLUETOOTH_PROTOCOL_AVDTP){
577                         log_info("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED");
578                         return;
579                     }
580 
581                     status = l2cap_event_channel_opened_get_status(packet);
582                     // inform about new l2cap connection
583                     l2cap_event_channel_opened_get_address(packet, event_addr);
584                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
585                     connection = avdtp_connection_for_bd_addr(event_addr, context);
586 
587                     log_info("L2CAP_EVENT_CHANNEL_OPENED: status %d, cid 0x%02x , signaling connection %p \n", status, local_cid, connection);
588                     connection = avdtp_connection_for_bd_addr(event_addr, context);
589                     if (!connection){
590                         log_info("L2CAP_EVENT_CHANNEL_OPENED 2: status %d, signaling connection %p \n", status, connection);
591                         break;
592                     }
593 
594                     if (connection->l2cap_signaling_cid == 0) {
595                         if (status){
596                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
597                             connection->state = AVDTP_SIGNALING_CONNECTION_IDLE;
598                             connection->l2cap_signaling_cid = 0;
599                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, l2cap_event_channel_opened_get_status(packet));
600                             break;
601                         }
602                         if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) {
603                             log_info("L2CAP connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
604                             avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, AVDTP_CONNECTION_IN_WRONG_STATE);
605                             break;
606                         }
607                         connection->l2cap_signaling_cid = local_cid;
608                         connection->local_seid = 0;
609                         connection->state = AVDTP_SIGNALING_CONNECTION_OPENED;
610                         log_info(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p, l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x", connection, local_cid, connection->avdtp_cid);
611                         avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, 0);
612                         break;
613                     }
614 
615                     stream_endpoint = avdtp_stream_endpoint_for_seid(connection->local_seid, context);
616                     if (!stream_endpoint){
617                         log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found");
618                         return;
619                     }
620 
621                     if (stream_endpoint->l2cap_media_cid == 0){
622                         status = l2cap_event_channel_opened_get_status(packet);
623                         if (status){
624                             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));
625                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
626                             avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), status);
627                             break;
628                         }
629                         if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED){
630                             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));
631                             avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE);
632                             break;
633                         }
634 
635                         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED;
636                         stream_endpoint->connection = connection;
637                         stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet);
638                         stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet);
639 
640                         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));
641                         avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->avdtp_cid, event_addr, avdtp_local_seid(stream_endpoint), avdtp_remote_seid(stream_endpoint), 0);
642                         return;
643                     }
644                     break;
645 
646                 case L2CAP_EVENT_CHANNEL_CLOSED:
647                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
648                     connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context);
649                     stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context);
650                     log_info("received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint);
651 
652                     if (stream_endpoint){
653                         if (stream_endpoint->l2cap_media_cid == local_cid){
654                             connection = stream_endpoint->connection;
655                             if (connection) {
656                                 avdtp_streaming_emit_connection_released(context->avdtp_callback, connection->avdtp_cid, avdtp_local_seid(stream_endpoint));
657                             }
658                             stream_endpoint->l2cap_media_cid = 0;
659                             stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE;
660                             stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE;
661                             stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE;
662                             stream_endpoint->remote_sep_index = 0;
663                             if (connection && connection->disconnect){
664                                 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
665                             }
666                             break;
667                         }
668                         if (stream_endpoint->l2cap_recovery_cid == local_cid){
669                             log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid);
670                             stream_endpoint->l2cap_recovery_cid = 0;
671                             break;
672                         }
673 
674                         if (stream_endpoint->l2cap_reporting_cid == local_cid){
675                             log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid);
676                             stream_endpoint->l2cap_reporting_cid = 0;
677                             break;
678                         }
679                     }
680 
681                     if (connection){
682                         btstack_run_loop_remove_timer(&connection->configuration_timer);
683                         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
684 
685                         btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection);
686                         btstack_linked_list_iterator_t it;
687                         btstack_linked_list_iterator_init(&it, stream_endpoints);
688                         while (btstack_linked_list_iterator_has_next(&it)){
689                             avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it);
690 
691                             if (_stream_endpoint->connection == connection){
692                                 avdtp_initialize_stream_endpoint(_stream_endpoint);
693                             }
694                         }
695                         btstack_memory_avdtp_connection_free(connection);
696                         break;
697                     }
698 
699                     break;
700 
701                 case HCI_EVENT_DISCONNECTION_COMPLETE:
702                     break;
703 
704                 case L2CAP_EVENT_CAN_SEND_NOW:
705                     connection = avdtp_connection_for_l2cap_signaling_cid(channel, context);
706                     if (!connection) {
707                         stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context);
708                         if (!stream_endpoint->connection) break;
709                         connection = stream_endpoint->connection;
710                     }
711                     avdtp_handle_can_send_now(connection, channel, context);
712                     break;
713                 default:
714                     log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet));
715                     break;
716             }
717             break;
718 
719         default:
720             // other packet type
721             break;
722     }
723 }
724 
725 uint8_t avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){
726     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
727     if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED;
728     if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE){
729         avdtp_signaling_emit_connection_released(context->avdtp_callback, connection->avdtp_cid);
730         return ERROR_CODE_SUCCESS;
731     }
732     if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS;
733 
734     connection->disconnect = 1;
735     avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid);
736     return ERROR_CODE_SUCCESS;
737 }
738 
739 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, avdtp_context_t * context){
740     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
741     if (!connection){
742         log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid);
743         return AVDTP_CONNECTION_DOES_NOT_EXIST;
744     }
745     if (avdtp_find_remote_sep(connection, remote_seid) == 0xFF){
746         log_error("avdtp_media_connect: no remote sep for seid %d found", remote_seid);
747         return AVDTP_SEID_DOES_NOT_EXIST;
748     }
749 
750     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) {
751         log_error("avdtp_media_connect: wrong connection state %d", connection->state);
752         return AVDTP_CONNECTION_IN_WRONG_STATE;
753     }
754 
755     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
756     if (!stream_endpoint) {
757         log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid);
758         return AVDTP_SEID_DOES_NOT_EXIST;
759     }
760 
761     if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
762     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return AVDTP_SEID_DOES_NOT_EXIST;
763 
764     connection->initiator_transaction_label++;
765     connection->remote_seid = remote_seid;
766     connection->local_seid = local_seid;
767     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM;
768     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM;
769     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
770     return ERROR_CODE_SUCCESS;
771 }
772 
773 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
774     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
775     if (!connection){
776         log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
777         return AVDTP_CONNECTION_DOES_NOT_EXIST;
778     }
779 
780     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
781     if (!stream_endpoint) {
782         log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid);
783         return AVDTP_SEID_DOES_NOT_EXIST;
784     }
785 
786     if (stream_endpoint->l2cap_media_cid == 0){
787         log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid);
788         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
789     }
790 
791     if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream){
792         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
793     }
794 
795     stream_endpoint->start_stream = 1;
796     connection->local_seid = local_seid;
797     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
798     return ERROR_CODE_SUCCESS;
799 }
800 
801 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
802     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
803     if (!connection){
804         log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
805         return AVDTP_CONNECTION_DOES_NOT_EXIST;
806     }
807 
808     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
809     if (!stream_endpoint) {
810         log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid);
811         return AVDTP_SEID_DOES_NOT_EXIST;
812     }
813 
814     if (stream_endpoint->l2cap_media_cid == 0){
815         log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid);
816         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
817     }
818     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return ERROR_CODE_SUCCESS;
819 
820     stream_endpoint->stop_stream = 1;
821     connection->local_seid = local_seid;
822     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
823     return ERROR_CODE_SUCCESS;
824 }
825 
826 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
827     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
828     if (!connection){
829         log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
830         return AVDTP_CONNECTION_DOES_NOT_EXIST;
831     }
832 
833     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
834     if (!stream_endpoint) {
835         log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid);
836         return AVDTP_SEID_DOES_NOT_EXIST;
837     }
838 
839     if (stream_endpoint->l2cap_media_cid == 0){
840         log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid);
841         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
842     }
843     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return ERROR_CODE_SUCCESS;
844 
845     stream_endpoint->abort_stream = 1;
846     connection->local_seid = local_seid;
847     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
848     return ERROR_CODE_SUCCESS;
849 }
850 
851 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid, avdtp_context_t * context){
852     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
853     if (!connection){
854         log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid);
855         return AVDTP_CONNECTION_DOES_NOT_EXIST;
856     }
857     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(local_seid, context);
858     if (!stream_endpoint) {
859         log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid);
860         return AVDTP_SEID_DOES_NOT_EXIST;
861     }
862 
863     if (stream_endpoint->l2cap_media_cid == 0){
864         log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid);
865         return AVDTP_MEDIA_CONNECTION_DOES_NOT_EXIST;
866     }
867     if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return ERROR_CODE_SUCCESS;
868 
869     stream_endpoint->suspend_stream = 1;
870     connection->local_seid = local_seid;
871     avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
872     return ERROR_CODE_SUCCESS;
873 }
874 
875 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){
876     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
877     if (!connection){
878         log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid);
879         return AVDTP_CONNECTION_DOES_NOT_EXIST;
880     }
881     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
882         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
883         return AVDTP_CONNECTION_IN_WRONG_STATE;
884     }
885 
886     connection->initiator_transaction_label++;
887     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS;
888     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
889 }
890 
891 
892 uint8_t avdtp_get_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: no connection for AVDTP cid 0x%02x found", avdtp_cid);
896         return AVDTP_CONNECTION_DOES_NOT_EXIST;
897     }
898     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
899         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
900         return AVDTP_CONNECTION_IN_WRONG_STATE;
901     }
902 
903     connection->initiator_transaction_label++;
904     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES;
905     connection->remote_seid = remote_seid;
906     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
907 }
908 
909 
910 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
911     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
912     if (!connection){
913         log_error("avdtp: no connection for AVDTP cid 0x%02x found", avdtp_cid);
914         return AVDTP_CONNECTION_DOES_NOT_EXIST;
915     }
916     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
917         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
918         return AVDTP_CONNECTION_IN_WRONG_STATE;
919     }
920 
921     connection->initiator_transaction_label++;
922     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES;
923     connection->remote_seid = remote_seid;
924     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
925 }
926 
927 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid, avdtp_context_t * context){
928     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
929     if (!connection){
930         log_error("avdtp: no connection for AVDTP cid 0x%02x found", avdtp_cid);
931         return AVDTP_CONNECTION_DOES_NOT_EXIST;
932     }
933     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
934         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
935         return AVDTP_CONNECTION_IN_WRONG_STATE;
936     }
937 
938     connection->initiator_transaction_label++;
939     connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION;
940     connection->remote_seid = remote_seid;
941     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
942 }
943 
944 uint8_t 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){
945     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
946     if (!connection){
947         log_error("avdtp: no connection for AVDTP cid 0x%02x found", avdtp_cid);
948         return AVDTP_CONNECTION_DOES_NOT_EXIST;
949     }
950     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
951         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
952         return AVDTP_CONNECTION_IN_WRONG_STATE;
953     }
954 
955     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
956     if (!stream_endpoint) {
957         log_error("avdtp: no initiator stream endpoint for seid %d", local_seid);
958         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
959     }
960     if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_CONFIGURED){
961         log_error("avdtp: stream endpoint seid %d in wrong state", local_seid);
962         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
963     }
964     connection->active_stream_endpoint = (void*) stream_endpoint;
965     connection->is_configuration_initiated_locally = 1;
966     connection->is_initiator = 1;
967 
968     connection->initiator_transaction_label++;
969     connection->remote_seid = remote_seid;
970     connection->local_seid = local_seid;
971     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
972     stream_endpoint->remote_configuration = configuration;
973     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION;
974 
975     // cache media codec information for SBC
976     stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type;
977     if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){
978         stream_endpoint->media_type = configuration.media_codec.media_type;
979         memcpy(stream_endpoint->media_codec_sbc_info, configuration.media_codec.media_codec_information, 4);
980     }
981     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
982 }
983 
984 uint8_t 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){
985     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
986     if (!connection){
987         log_error("avdtp: no connection for AVDTP cid 0x%02x found", avdtp_cid);
988         return AVDTP_CONNECTION_DOES_NOT_EXIST;
989     }
990     //TODO: if opened only app capabilities, enable reconfigure for not opened
991     if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED ||
992         connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) {
993         return AVDTP_CONNECTION_IN_WRONG_STATE;
994     }
995 
996     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, context);
997     if (!stream_endpoint) {
998         log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid);
999         return AVDTP_STREAM_ENDPOINT_DOES_NOT_EXIST;
1000     }
1001 
1002     if (stream_endpoint->remote_sep_index == 0xFF){
1003         log_error("avdtp_reconfigure: no associated remote sep");
1004         return AVDTP_STREAM_ENDPOINT_IN_WRONG_STATE;
1005     }
1006     connection->initiator_transaction_label++;
1007     connection->remote_seid = remote_seid;
1008     connection->local_seid = local_seid;
1009     stream_endpoint->remote_configuration_bitmap = configured_services_bitmap;
1010     stream_endpoint->remote_configuration = configuration;
1011     stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID;
1012     return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid);
1013 }
1014 
1015 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){
1016     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1017     if (!connection){
1018         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
1019         return 0;
1020     }
1021     return connection->remote_seps_num;
1022 }
1023 
1024 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){
1025     avdtp_connection_t * connection = avdtp_connection_for_avdtp_cid(avdtp_cid, context);
1026     if (!connection){
1027         log_error("avdtp_suspend: no connection for AVDTP cid 0x%02x found", avdtp_cid);
1028         return NULL;
1029     }
1030     return &connection->remote_seps[index];
1031 }
1032 
1033 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){
1034     UNUSED(packet_size);
1035     if (storage_size < 4) {
1036         log_error("storage must have 4 bytes");
1037         return;
1038     }
1039     uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
1040     uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
1041     uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
1042     uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
1043 
1044     uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
1045     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));
1046     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));
1047 
1048     config_storage[0] = (sampling_frequency << 4) | channel_mode;
1049     config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method;
1050     config_storage[2] = min_bitpool_value;
1051     config_storage[3] = max_bitpool_value;
1052 
1053     stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
1054     stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
1055     stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
1056     stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size;
1057     stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage;
1058 }
1059 
1060 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){
1061     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1062     uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap;
1063 
1064     uint8_t channel_mode = AVDTP_SBC_STEREO;
1065     if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){
1066         channel_mode = AVDTP_SBC_JOINT_STEREO;
1067     } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){
1068         channel_mode = AVDTP_SBC_STEREO;
1069     } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){
1070         channel_mode = AVDTP_SBC_DUAL_CHANNEL;
1071     } else if (channel_mode_bitmap & AVDTP_SBC_MONO){
1072         channel_mode = AVDTP_SBC_MONO;
1073     }
1074     return channel_mode;
1075 }
1076 
1077 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){
1078     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1079     uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap;
1080 
1081     uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1082     if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){
1083         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS;
1084     } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){
1085         allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR;
1086     }
1087     return allocation_method;
1088 }
1089 
1090 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){
1091     if (!stream_endpoint) return 0;
1092     return stream_endpoint->sep.seid;
1093 }
1094 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){
1095     if (!stream_endpoint) return 0;
1096     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1097     uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap;
1098 
1099     uint8_t subbands = AVDTP_SBC_SUBBANDS_8;
1100     if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){
1101         subbands = AVDTP_SBC_SUBBANDS_8;
1102     } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){
1103         subbands = AVDTP_SBC_SUBBANDS_4;
1104     }
1105     return subbands;
1106 }
1107 
1108 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){
1109     if (!stream_endpoint) return 0;
1110     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1111     uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap;
1112 
1113     uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1114     if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){
1115         block_length = AVDTP_SBC_BLOCK_LENGTH_16;
1116     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){
1117         block_length = AVDTP_SBC_BLOCK_LENGTH_12;
1118     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){
1119         block_length = AVDTP_SBC_BLOCK_LENGTH_8;
1120     } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){
1121         block_length = AVDTP_SBC_BLOCK_LENGTH_4;
1122     }
1123     return block_length;
1124 }
1125 
1126 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){
1127     if (!stream_endpoint) return 0;
1128     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1129     uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap;
1130 
1131     uint8_t sampling_frequency = AVDTP_SBC_44100;
1132     if (sampling_frequency_bitmap & AVDTP_SBC_48000){
1133         sampling_frequency = AVDTP_SBC_48000;
1134     } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){
1135         sampling_frequency = AVDTP_SBC_44100;
1136     } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){
1137         sampling_frequency = AVDTP_SBC_32000;
1138     } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){
1139         sampling_frequency = AVDTP_SBC_16000;
1140     }
1141     return sampling_frequency;
1142 }
1143 
1144 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){
1145     if (!stream_endpoint) return 0;
1146     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1147     return btstack_min(media_codec[3], remote_max_bitpool_value);
1148 }
1149 
1150 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){
1151     if (!stream_endpoint) return 0;
1152     uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information;
1153     return btstack_max(media_codec[2], remote_min_bitpool_value);
1154 }
1155