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 <string.h> 43 44 #include "bluetooth_psm.h" 45 #include "bluetooth_sdp.h" 46 #include "btstack_debug.h" 47 #include "btstack_event.h" 48 #include "btstack_memory.h" 49 #include "classic/avdtp.h" 50 #include "classic/avdtp_acceptor.h" 51 #include "classic/avdtp_initiator.h" 52 #include "classic/avdtp_util.h" 53 #include "classic/sdp_client.h" 54 #include "classic/sdp_util.h" 55 56 btstack_linked_list_t stream_endpoints; 57 58 static btstack_packet_handler_t avdtp_source_callback; 59 static btstack_packet_handler_t avdtp_sink_callback; 60 static btstack_context_callback_registration_t avdtp_handle_sdp_client_query_request; 61 62 static uint16_t sdp_query_context_avdtp_cid = 0; 63 64 static uint16_t stream_endpoints_id_counter = 0; 65 66 static btstack_linked_list_t connections; 67 static uint16_t transaction_id_counter = 0; 68 69 static int record_id = -1; 70 static uint8_t attribute_value[45]; 71 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 72 73 static void (*avdtp_sink_handle_media_data)(uint8_t local_seid, uint8_t *packet, uint16_t size); 74 75 static uint16_t avdtp_cid_counter = 0; 76 77 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 78 79 btstack_packet_handler_t 80 avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t *stream_endpoint) { 81 return (stream_endpoint->sep.type == AVDTP_SOURCE) ? avdtp_source_callback : avdtp_sink_callback; 82 } 83 84 void avdtp_emit_sink_and_source(uint8_t * packet, uint16_t size){ 85 if (avdtp_source_callback != NULL){ 86 (*avdtp_source_callback)(HCI_EVENT_PACKET, 0, packet, size); 87 } 88 if (avdtp_sink_callback != NULL){ 89 (*avdtp_sink_callback)(HCI_EVENT_PACKET, 0, packet, size); 90 } 91 } 92 93 void avdtp_emit_source(uint8_t * packet, uint16_t size){ 94 if (avdtp_source_callback != NULL){ 95 (*avdtp_source_callback)(HCI_EVENT_PACKET, 0, packet, size); 96 } 97 } 98 99 btstack_linked_list_t * avdtp_get_stream_endpoints(void){ 100 return &stream_endpoints; 101 } 102 103 btstack_linked_list_t * avdtp_get_connections(void){ 104 return &connections; 105 } 106 107 static avdtp_connection_t * avdtp_get_connection_for_bd_addr(bd_addr_t addr){ 108 btstack_linked_list_iterator_t it; 109 btstack_linked_list_iterator_init(&it, &connections); 110 while (btstack_linked_list_iterator_has_next(&it)){ 111 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 112 if (memcmp(addr, connection->remote_addr, 6) != 0) continue; 113 return connection; 114 } 115 return NULL; 116 } 117 118 avdtp_connection_t * avdtp_get_connection_for_avdtp_cid(uint16_t avdtp_cid){ 119 btstack_linked_list_iterator_t it; 120 btstack_linked_list_iterator_init(&it, &connections); 121 while (btstack_linked_list_iterator_has_next(&it)){ 122 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 123 if (connection->avdtp_cid != avdtp_cid) continue; 124 return connection; 125 } 126 return NULL; 127 } 128 129 130 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_seid(uint16_t seid){ 131 btstack_linked_list_iterator_t it; 132 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 133 while (btstack_linked_list_iterator_has_next(&it)){ 134 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 135 if (stream_endpoint->sep.seid == seid){ 136 return stream_endpoint; 137 } 138 } 139 return NULL; 140 } 141 142 avdtp_connection_t * avdtp_get_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid){ 143 btstack_linked_list_iterator_t it; 144 btstack_linked_list_iterator_init(&it, &connections); 145 while (btstack_linked_list_iterator_has_next(&it)){ 146 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 147 if (connection->l2cap_signaling_cid != l2cap_cid) continue; 148 return connection; 149 } 150 return NULL; 151 } 152 153 static avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_l2cap_cid(uint16_t l2cap_cid){ 154 btstack_linked_list_iterator_t it; 155 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 156 while (btstack_linked_list_iterator_has_next(&it)){ 157 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 158 if (stream_endpoint->l2cap_media_cid == l2cap_cid){ 159 return stream_endpoint; 160 } 161 if (stream_endpoint->l2cap_reporting_cid == l2cap_cid){ 162 return stream_endpoint; 163 } 164 if (stream_endpoint->l2cap_recovery_cid == l2cap_cid){ 165 return stream_endpoint; 166 } 167 } 168 return NULL; 169 } 170 171 static avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_for_signaling_cid(uint16_t l2cap_cid){ 172 btstack_linked_list_iterator_t it; 173 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 174 while (btstack_linked_list_iterator_has_next(&it)){ 175 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 176 if (stream_endpoint->connection){ 177 if (stream_endpoint->connection->l2cap_signaling_cid == l2cap_cid){ 178 return stream_endpoint; 179 } 180 } 181 } 182 return NULL; 183 } 184 185 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_with_seid(uint8_t seid){ 186 btstack_linked_list_iterator_t it; 187 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 188 while (btstack_linked_list_iterator_has_next(&it)){ 189 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 190 if (stream_endpoint->sep.seid == seid){ 191 return stream_endpoint; 192 } 193 } 194 return NULL; 195 } 196 197 avdtp_stream_endpoint_t * avdtp_get_stream_endpoint_associated_with_acp_seid(uint16_t acp_seid){ 198 btstack_linked_list_iterator_t it; 199 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 200 while (btstack_linked_list_iterator_has_next(&it)){ 201 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 202 if (stream_endpoint->remote_sep.seid == acp_seid){ 203 return stream_endpoint; 204 } 205 } 206 return NULL; 207 } 208 209 uint16_t avdtp_get_next_transaction_label(void){ 210 transaction_id_counter++; 211 if (transaction_id_counter == 16){ 212 transaction_id_counter = 1; 213 } 214 return transaction_id_counter; 215 } 216 217 static avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, uint16_t cid){ 218 avdtp_connection_t * connection = btstack_memory_avdtp_connection_get(); 219 if (!connection){ 220 log_error("Not enough memory to create connection"); 221 return NULL; 222 } 223 connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; 224 connection->initiator_transaction_label = avdtp_get_next_transaction_label(); 225 connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE; 226 connection->a2dp_source_discover_seps = false; 227 connection->avdtp_cid = cid; 228 (void)memcpy(connection->remote_addr, remote_addr, 6); 229 230 btstack_linked_list_add(&connections, (btstack_linked_item_t *) connection); 231 return connection; 232 } 233 234 static uint16_t avdtp_get_next_cid(void){ 235 if (avdtp_cid_counter == 0xffff) { 236 avdtp_cid_counter = 1; 237 } else { 238 avdtp_cid_counter++; 239 } 240 return avdtp_cid_counter; 241 } 242 243 static uint16_t avdtp_get_next_local_seid(void){ 244 if (stream_endpoints_id_counter == 0xffff) { 245 stream_endpoints_id_counter = 1; 246 } else { 247 stream_endpoints_id_counter++; 248 } 249 return stream_endpoints_id_counter; 250 } 251 252 static void avdtp_handle_start_sdp_client_query(void * context){ 253 UNUSED(context); 254 255 btstack_linked_list_iterator_t it; 256 btstack_linked_list_iterator_init(&it, &connections); 257 while (btstack_linked_list_iterator_has_next(&it)){ 258 avdtp_connection_t * connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 259 260 switch (connection->state){ 261 case AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SOURCE: 262 connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE; 263 break; 264 case AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SINK: 265 connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE; 266 break; 267 case AVDTP_SIGNALING_CONNECTION_OPENED: 268 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES) continue; 269 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W4_SDP_QUERY_COMPLETE_THEN_GET_ALL_CAPABILITIES; 270 break; 271 default: 272 continue; 273 } 274 sdp_query_context_avdtp_cid = connection->avdtp_cid; 275 sdp_client_query_uuid16(&avdtp_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP); 276 return; 277 } 278 } 279 280 uint8_t avdtp_connect(bd_addr_t remote, avdtp_role_t role, uint16_t * avdtp_cid){ 281 avdtp_connection_t * connection = avdtp_get_connection_for_bd_addr(remote); 282 if (connection){ 283 return ERROR_CODE_COMMAND_DISALLOWED; 284 } 285 286 uint16_t cid = avdtp_get_next_cid(); 287 if (avdtp_cid != NULL) { 288 *avdtp_cid = cid; 289 } 290 291 connection = avdtp_create_connection(remote, cid); 292 if (!connection) return BTSTACK_MEMORY_ALLOC_FAILED; 293 294 connection->avdtp_cid = cid; 295 296 connection->avdtp_l2cap_psm = 0; 297 connection->avdtp_version = 0; 298 connection->sink_supported = false; 299 connection->source_supported = false; 300 301 switch (role){ 302 case AVDTP_ROLE_SOURCE: 303 connection->state = AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SINK; 304 break; 305 case AVDTP_ROLE_SINK: 306 connection->state = AVDTP_SIGNALING_W2_SEND_SDP_QUERY_FOR_REMOTE_SOURCE; 307 break; 308 default: 309 btstack_assert(false); 310 return ERROR_CODE_COMMAND_DISALLOWED; 311 } 312 313 avdtp_handle_sdp_client_query_request.callback = &avdtp_handle_start_sdp_client_query; 314 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 315 (void) sdp_client_register_query_callback(&avdtp_handle_sdp_client_query_request); 316 return ERROR_CODE_SUCCESS; 317 } 318 319 320 void avdtp_register_sink_packet_handler(btstack_packet_handler_t callback){ 321 btstack_assert(callback != NULL); 322 avdtp_sink_callback = callback; 323 } 324 325 void avdtp_register_source_packet_handler(btstack_packet_handler_t callback){ 326 btstack_assert(callback != NULL); 327 avdtp_source_callback = callback; 328 } 329 330 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){ 331 if (!stream_endpoint){ 332 log_error("Stream endpoint with given seid is not registered."); 333 return; 334 } 335 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1); 336 stream_endpoint->sep.registered_service_categories = bitmap; 337 } 338 339 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){ 340 if (!stream_endpoint){ 341 log_error("Stream endpoint with given seid is not registered."); 342 return; 343 } 344 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1); 345 stream_endpoint->sep.registered_service_categories = bitmap; 346 } 347 348 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){ 349 if (!stream_endpoint){ 350 log_error("Stream endpoint with given seid is not registered."); 351 return; 352 } 353 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1); 354 stream_endpoint->sep.registered_service_categories = bitmap; 355 } 356 357 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 358 if (!stream_endpoint){ 359 log_error("Stream endpoint with given seid is not registered."); 360 return; 361 } 362 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1); 363 stream_endpoint->sep.registered_service_categories = bitmap; 364 stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733 365 stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size; 366 stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets; 367 } 368 369 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){ 370 if (!stream_endpoint){ 371 log_error("Stream endpoint with given seid is not registered."); 372 return; 373 } 374 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1); 375 stream_endpoint->sep.registered_service_categories = bitmap; 376 stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type; 377 (void)memcpy(stream_endpoint->sep.capabilities.content_protection.cp_type_value, 378 cp_type_value, 379 btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN)); 380 stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = btstack_min(cp_type_value_len, AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN); 381 } 382 383 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){ 384 if (!stream_endpoint){ 385 log_error("Stream endpoint with given seid is not registered."); 386 return; 387 } 388 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1); 389 stream_endpoint->sep.registered_service_categories = bitmap; 390 stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch; 391 stream_endpoint->sep.capabilities.header_compression.media = media; 392 stream_endpoint->sep.capabilities.header_compression.recovery = recovery; 393 } 394 395 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){ 396 if (!stream_endpoint){ 397 log_error("Stream endpoint with given seid is not registered."); 398 return; 399 } 400 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1); 401 stream_endpoint->sep.registered_service_categories = bitmap; 402 stream_endpoint->sep.capabilities.media_codec.media_type = media_type; 403 stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type; 404 stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info; 405 stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len; 406 } 407 408 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){ 409 if (!stream_endpoint){ 410 log_error("Stream endpoint with given seid is not registered."); 411 return; 412 } 413 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1); 414 stream_endpoint->sep.registered_service_categories = bitmap; 415 stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation; 416 } 417 418 void avdtp_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){ 419 avdtp_sink_handle_media_data = callback; 420 } 421 422 /* START: tracking can send now requests pro l2cap cid */ 423 void avdtp_handle_can_send_now(avdtp_connection_t *connection, uint16_t l2cap_cid) { 424 if (connection->wait_to_send_acceptor){ 425 log_debug("call avdtp_acceptor_stream_config_subsm_run"); 426 connection->wait_to_send_acceptor = 0; 427 avdtp_acceptor_stream_config_subsm_run(connection); 428 } else if (connection->wait_to_send_initiator){ 429 log_debug("call avdtp_initiator_stream_config_subsm_run"); 430 connection->wait_to_send_initiator = 0; 431 avdtp_initiator_stream_config_subsm_run(connection); 432 } 433 434 // re-register 435 bool more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator; 436 log_debug("ask for more to send %d: acc-%d, ini-%d", more_to_send, connection->wait_to_send_acceptor, connection->wait_to_send_initiator); 437 438 if (more_to_send){ 439 l2cap_request_can_send_now_event(l2cap_cid); 440 } 441 } 442 /* END: tracking can send now requests pro l2cap cid */ 443 444 445 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){ 446 avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get(); 447 if (!stream_endpoint){ 448 log_error("Not enough memory to create stream endpoint"); 449 return NULL; 450 } 451 stream_endpoint->sep.seid = avdtp_get_next_local_seid(); 452 stream_endpoint->sep.media_type = media_type; 453 stream_endpoint->sep.type = sep_type; 454 btstack_linked_list_add(avdtp_get_stream_endpoints(), (btstack_linked_item_t *) stream_endpoint); 455 return stream_endpoint; 456 } 457 458 void avdtp_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 459 btstack_linked_list_remove(avdtp_get_stream_endpoints(), (btstack_linked_item_t* ) stream_endpoint); 460 btstack_memory_avdtp_stream_endpoint_free(stream_endpoint); 461 } 462 463 static void 464 handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t *connection, uint8_t *packet, uint16_t size) { 465 if (size < 2) return; 466 467 uint16_t offset; 468 avdtp_message_type_t message_type = avdtp_get_signaling_packet_type(packet); 469 switch (message_type){ 470 case AVDTP_CMD_MSG: 471 offset = avdtp_read_signaling_header(&connection->acceptor_signaling_packet, packet, size); 472 avdtp_acceptor_stream_config_subsm(connection, packet, size, offset); 473 break; 474 default: 475 offset = avdtp_read_signaling_header(&connection->initiator_signaling_packet, packet, size); 476 avdtp_initiator_stream_config_subsm(connection, packet, size, offset); 477 break; 478 } 479 } 480 481 static void avdtp_handle_sdp_client_query_attribute_value(avdtp_connection_t * connection, uint8_t *packet){ 482 des_iterator_t des_list_it; 483 des_iterator_t prot_it; 484 485 // Handle new SDP record 486 if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) { 487 record_id = sdp_event_query_attribute_byte_get_record_id(packet); 488 // log_info("SDP Record: Nr: %d", record_id); 489 } 490 491 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 492 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 493 494 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 495 496 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 497 498 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 499 if (de_get_element_type(attribute_value) != DE_DES) break; 500 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 501 uint8_t * element = des_iterator_get_element(&des_list_it); 502 if (de_get_element_type(element) != DE_UUID) continue; 503 uint32_t uuid = de_get_uuid32(element); 504 switch (uuid){ 505 case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE: 506 connection->source_supported = true; 507 log_info("source_supported"); 508 break; 509 case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK: 510 connection->sink_supported = true; 511 log_info("sink_supported"); 512 break; 513 default: 514 break; 515 } 516 } 517 break; 518 519 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 520 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 521 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 522 uint8_t *des_element; 523 uint8_t *element; 524 uint32_t uuid; 525 526 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 527 528 des_element = des_iterator_get_element(&des_list_it); 529 des_iterator_init(&prot_it, des_element); 530 element = des_iterator_get_element(&prot_it); 531 532 if (de_get_element_type(element) != DE_UUID) continue; 533 534 uuid = de_get_uuid32(element); 535 des_iterator_next(&prot_it); 536 // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both 537 switch (uuid){ 538 case BLUETOOTH_PROTOCOL_L2CAP: 539 if (!des_iterator_has_more(&prot_it)) continue; 540 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->avdtp_l2cap_psm); 541 break; 542 case BLUETOOTH_PROTOCOL_AVDTP: 543 if (!des_iterator_has_more(&prot_it)) continue; 544 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->avdtp_version); 545 log_info("avdtp version 0x%02x", connection->avdtp_version); 546 break; 547 default: 548 break; 549 } 550 } 551 break; 552 553 default: 554 break; 555 } 556 } 557 } else { 558 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)); 559 } 560 561 } 562 563 static void avdtp_finalize_connection(avdtp_connection_t * connection){ 564 btstack_run_loop_remove_timer(&connection->retry_timer); 565 btstack_linked_list_remove(&connections, (btstack_linked_item_t*) connection); 566 btstack_memory_avdtp_connection_free(connection); 567 } 568 569 static void avdtp_handle_sdp_query_failed(avdtp_connection_t * connection, uint8_t status){ 570 switch (connection->state){ 571 case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE: 572 case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE: 573 avdtp_signaling_emit_connection_established(connection->avdtp_cid, connection->remote_addr, connection->con_handle, status); 574 break; 575 576 case AVDTP_SIGNALING_CONNECTION_OPENED: 577 // SDP query failed: try query that must be supported 578 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 579 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 580 return; 581 582 default: 583 btstack_assert(false); 584 break; 585 } 586 avdtp_finalize_connection(connection); 587 sdp_query_context_avdtp_cid = 0; 588 log_info("SDP query failed with status 0x%02x.", status); 589 } 590 591 static void avdtp_handle_sdp_query_succeeded(avdtp_connection_t * connection){ 592 log_info("avdtp_handle_sdp_query_succeeded: state %d", connection->state); 593 594 switch (connection->state){ 595 case AVDTP_SIGNALING_CONNECTION_OPENED: 596 if (connection->avdtp_version < 0x0103){ 597 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 598 } else { 599 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES; 600 } 601 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 602 break; 603 default: 604 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 605 l2cap_create_channel(avdtp_packet_handler, connection->remote_addr, connection->avdtp_l2cap_psm, l2cap_max_mtu(), NULL); 606 break; 607 } 608 } 609 610 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 611 UNUSED(packet_type); 612 UNUSED(channel); 613 UNUSED(size); 614 615 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(sdp_query_context_avdtp_cid); 616 if (!connection) { 617 log_error("SDP query, connection with 0x%02x cid not found", sdp_query_context_avdtp_cid); 618 return; 619 } 620 621 uint8_t status = ERROR_CODE_SUCCESS; 622 switch (connection->state){ 623 case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SINK_COMPLETE: 624 switch (hci_event_packet_get_type(packet)){ 625 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 626 avdtp_handle_sdp_client_query_attribute_value(connection, packet); 627 return; 628 case SDP_EVENT_QUERY_COMPLETE: 629 status = sdp_event_query_complete_get_status(packet); 630 if (status != ERROR_CODE_SUCCESS) break; 631 if (!connection->sink_supported) { 632 status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 633 break; 634 } 635 if (connection->avdtp_l2cap_psm == 0) { 636 status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 637 break; 638 } 639 break; 640 default: 641 btstack_assert(false); 642 return; 643 } 644 break; 645 case AVDTP_SIGNALING_W4_SDP_QUERY_FOR_REMOTE_SOURCE_COMPLETE: 646 switch (hci_event_packet_get_type(packet)){ 647 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 648 avdtp_handle_sdp_client_query_attribute_value(connection, packet); 649 return; 650 case SDP_EVENT_QUERY_COMPLETE: 651 status = sdp_event_query_complete_get_status(packet); 652 if (status != ERROR_CODE_SUCCESS) break; 653 if (!connection->source_supported) { 654 status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 655 break; 656 } 657 if (connection->avdtp_l2cap_psm == 0) { 658 status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 659 break; 660 } 661 break; 662 default: 663 btstack_assert(false); 664 return; 665 } 666 break; 667 668 case AVDTP_SIGNALING_CONNECTION_OPENED: 669 switch (hci_event_packet_get_type(packet)){ 670 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 671 avdtp_handle_sdp_client_query_attribute_value(connection, packet); 672 return; 673 case SDP_EVENT_QUERY_COMPLETE: 674 status = sdp_event_query_complete_get_status(packet); 675 break; 676 default: 677 btstack_assert(false); 678 return; 679 } 680 break; 681 682 default: 683 // bail out, we must have had an incoming connection in the meantime; just trigger next sdp query on complete 684 if (hci_event_packet_get_type(packet) == SDP_EVENT_QUERY_COMPLETE){ 685 (void) sdp_client_register_query_callback(&avdtp_handle_sdp_client_query_request); 686 } 687 return; 688 } 689 690 if (status == ERROR_CODE_SUCCESS){ 691 avdtp_handle_sdp_query_succeeded(connection); 692 } else { 693 avdtp_handle_sdp_query_failed(connection, status); 694 } 695 696 // register the SDP Query request to check if there is another connection waiting for the query 697 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 698 (void) sdp_client_register_query_callback(&avdtp_handle_sdp_client_query_request); 699 } 700 701 static avdtp_connection_t * avdtp_handle_incoming_connection(avdtp_connection_t * connection, bd_addr_t event_addr, hci_con_handle_t con_handle, uint16_t local_cid){ 702 if (connection == NULL){ 703 uint16_t cid = avdtp_get_next_cid(); 704 connection = avdtp_create_connection(event_addr, cid); 705 } 706 707 if (connection) { 708 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 709 connection->l2cap_signaling_cid = local_cid; 710 connection->con_handle = con_handle; 711 btstack_run_loop_remove_timer(&connection->retry_timer); 712 } 713 return connection; 714 } 715 716 static void avdtp_retry_timer_timeout_handler(btstack_timer_source_t * timer){ 717 uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 718 719 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 720 if (connection == NULL) return; 721 722 if (connection->state == AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY){ 723 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 724 l2cap_create_channel(&avdtp_packet_handler, connection->remote_addr, connection->avdtp_l2cap_psm, l2cap_max_mtu(), NULL); 725 } 726 } 727 728 static void avdtp_retry_timer_start(avdtp_connection_t * connection){ 729 btstack_run_loop_set_timer_handler(&connection->retry_timer, avdtp_retry_timer_timeout_handler); 730 btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avdtp_cid); 731 732 // add some jitter/randomness to reconnect delay 733 uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F); 734 btstack_run_loop_set_timer(&connection->retry_timer, timeout); 735 btstack_run_loop_add_timer(&connection->retry_timer); 736 } 737 738 739 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 740 bd_addr_t event_addr; 741 uint16_t psm; 742 uint16_t local_cid; 743 uint8_t status; 744 uint16_t l2cap_mtu; 745 hci_con_handle_t con_handle; 746 747 bool accept_streaming_connection; 748 bool outoing_signaling_active; 749 bool decline_connection; 750 751 avdtp_stream_endpoint_t * stream_endpoint = NULL; 752 avdtp_connection_t * connection = NULL; 753 754 switch (packet_type) { 755 case L2CAP_DATA_PACKET: 756 connection = avdtp_get_connection_for_l2cap_signaling_cid(channel); 757 if (connection){ 758 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 759 break; 760 } 761 762 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel); 763 if (!stream_endpoint){ 764 if (!connection) break; 765 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 766 break; 767 } 768 769 if (stream_endpoint->connection){ 770 if (channel == stream_endpoint->connection->l2cap_signaling_cid){ 771 handle_l2cap_data_packet_for_signaling_connection(stream_endpoint->connection, packet, size); 772 break; 773 } 774 } 775 776 if (channel == stream_endpoint->l2cap_media_cid){ 777 btstack_assert(avdtp_sink_handle_media_data); 778 (*avdtp_sink_handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size); 779 break; 780 } 781 782 if (channel == stream_endpoint->l2cap_reporting_cid){ 783 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED"); 784 } else if (channel == stream_endpoint->l2cap_recovery_cid){ 785 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED"); 786 } else { 787 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel); 788 } 789 break; 790 791 case HCI_EVENT_PACKET: 792 switch (hci_event_packet_get_type(packet)) { 793 794 case L2CAP_EVENT_INCOMING_CONNECTION: 795 l2cap_event_incoming_connection_get_address(packet, event_addr); 796 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 797 con_handle = l2cap_event_incoming_connection_get_handle(packet); 798 outoing_signaling_active = false; 799 accept_streaming_connection = false; 800 801 connection = avdtp_get_connection_for_bd_addr(event_addr); 802 if (connection != NULL){ 803 switch (connection->state){ 804 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED: 805 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED: 806 outoing_signaling_active = true; 807 connection->incoming_declined = true; 808 break; 809 case AVDTP_SIGNALING_CONNECTION_OPENED: 810 outoing_signaling_active = true; 811 accept_streaming_connection = true; 812 break; 813 default: 814 break; 815 } 816 } 817 log_info("incoming: %s, outoing_signaling_active %d, accept_streaming_connection %d", 818 bd_addr_to_str(event_addr), outoing_signaling_active, accept_streaming_connection); 819 820 decline_connection = outoing_signaling_active && !accept_streaming_connection; 821 if (outoing_signaling_active == false){ 822 connection = avdtp_handle_incoming_connection(connection, event_addr, con_handle, local_cid); 823 if (connection == NULL){ 824 decline_connection = true; 825 } 826 } else if (accept_streaming_connection){ 827 if ((connection == NULL) || (connection->configuration_state != AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED)) { 828 decline_connection = true; 829 } else { 830 // now, we're only dealing with media connections that are created by remote side - we're acceptor here 831 stream_endpoint = avdtp_get_stream_endpoint_with_seid(connection->acceptor_local_seid); 832 if ((stream_endpoint == NULL) || (stream_endpoint->l2cap_media_cid != 0) ) { 833 decline_connection = true; 834 } 835 } 836 } 837 838 if (decline_connection){ 839 l2cap_decline_connection(local_cid); 840 } else { 841 l2cap_accept_connection(local_cid); 842 } 843 break; 844 845 case L2CAP_EVENT_CHANNEL_OPENED: 846 847 psm = l2cap_event_channel_opened_get_psm(packet); 848 if (psm != BLUETOOTH_PSM_AVDTP){ 849 log_info("Unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED "); 850 return; 851 } 852 853 status = l2cap_event_channel_opened_get_status(packet); 854 // inform about new l2cap connection 855 l2cap_event_channel_opened_get_address(packet, event_addr); 856 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 857 l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 858 connection = avdtp_get_connection_for_bd_addr(event_addr); 859 if (connection == NULL){ 860 log_info("L2CAP_EVENT_CHANNEL_OPENED: no connection found for %s", bd_addr_to_str(event_addr)); 861 break; 862 } 863 864 con_handle = l2cap_event_channel_opened_get_handle(packet); 865 866 switch (connection->state){ 867 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED: 868 switch (status){ 869 case ERROR_CODE_SUCCESS: 870 connection->l2cap_signaling_cid = local_cid; 871 connection->incoming_declined = false; 872 connection->l2cap_mtu = l2cap_mtu; 873 connection->con_handle = con_handle; 874 connection->state = AVDTP_SIGNALING_CONNECTION_OPENED; 875 log_info("Connection opened l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x, con_handle 0x%02x", connection->l2cap_signaling_cid, connection->avdtp_cid, con_handle); 876 avdtp_signaling_emit_connection_established(connection->avdtp_cid, event_addr, con_handle, status); 877 return; 878 case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES: 879 if (connection->incoming_declined == true) { 880 log_info("Connection was declined, and the outgoing failed"); 881 connection->state = AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY; 882 connection->incoming_declined = false; 883 avdtp_retry_timer_start(connection); 884 return; 885 } 886 break; 887 default: 888 log_info("Connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 889 break; 890 } 891 avdtp_signaling_emit_connection_established(connection->avdtp_cid, event_addr, con_handle, status); 892 avdtp_finalize_connection(connection); 893 break; 894 895 case AVDTP_SIGNALING_CONNECTION_OPENED: 896 stream_endpoint = avdtp_get_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid); 897 if (!stream_endpoint){ 898 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found for signaling cid 0x%02x", connection->l2cap_signaling_cid); 899 return; 900 } 901 if (status != ERROR_CODE_SUCCESS){ 902 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)); 903 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE; 904 avdtp_streaming_emit_connection_established(stream_endpoint, status); 905 break; 906 } 907 switch (stream_endpoint->state){ 908 case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED: 909 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 910 stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet); 911 stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet); 912 913 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)); 914 avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_SUCCESS); 915 break; 916 default: 917 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)); 918 avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_COMMAND_DISALLOWED); 919 break; 920 } 921 break; 922 923 default: 924 log_info("L2CAP connection to %s ignored: status code 0x%02x, connection state %d", bd_addr_to_str(event_addr), status, connection->state); 925 break; 926 } 927 break; 928 929 case L2CAP_EVENT_CHANNEL_CLOSED: 930 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 931 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(local_cid); 932 connection = avdtp_get_connection_for_l2cap_signaling_cid(local_cid); 933 934 log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint); 935 936 if (stream_endpoint){ 937 if (stream_endpoint->l2cap_media_cid == local_cid){ 938 connection = stream_endpoint->connection; 939 if (connection) { 940 avdtp_streaming_emit_connection_released(stream_endpoint, 941 connection->avdtp_cid, 942 avdtp_local_seid(stream_endpoint)); 943 } 944 avdtp_reset_stream_endpoint(stream_endpoint); 945 connection->configuration_state = AVDTP_CONFIGURATION_STATE_IDLE; 946 break; 947 } 948 if (stream_endpoint->l2cap_recovery_cid == local_cid){ 949 log_info("L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid); 950 stream_endpoint->l2cap_recovery_cid = 0; 951 break; 952 } 953 954 if (stream_endpoint->l2cap_reporting_cid == local_cid){ 955 log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid); 956 stream_endpoint->l2cap_reporting_cid = 0; 957 break; 958 } 959 } 960 961 if (connection){ 962 btstack_linked_list_iterator_t it; 963 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 964 while (btstack_linked_list_iterator_has_next(&it)){ 965 stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 966 if (stream_endpoint->connection == connection){ 967 avdtp_reset_stream_endpoint(stream_endpoint); 968 } 969 } 970 avdtp_signaling_emit_connection_released(connection->avdtp_cid); 971 avdtp_finalize_connection(connection); 972 break; 973 } 974 break; 975 976 case L2CAP_EVENT_CAN_SEND_NOW: 977 log_debug("avdtp_packet_handler, L2CAP_EVENT_CAN_SEND_NOW l2cap_cid 0x%02x", channel); 978 connection = avdtp_get_connection_for_l2cap_signaling_cid(channel); 979 if (!connection) { 980 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel); 981 if (!stream_endpoint->connection) break; 982 connection = stream_endpoint->connection; 983 } 984 avdtp_handle_can_send_now(connection, channel); 985 break; 986 default: 987 log_info("Unknown HCI event type %02x", hci_event_packet_get_type(packet)); 988 break; 989 } 990 break; 991 992 default: 993 // other packet type 994 break; 995 } 996 } 997 998 uint8_t avdtp_disconnect(uint16_t avdtp_cid){ 999 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1000 if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1001 1002 if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS; 1003 1004 btstack_linked_list_iterator_t it; 1005 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 1006 1007 while (btstack_linked_list_iterator_has_next(&it)){ 1008 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 1009 if (stream_endpoint->connection != connection) continue; 1010 1011 switch (stream_endpoint->state){ 1012 case AVDTP_STREAM_ENDPOINT_OPENED: 1013 case AVDTP_STREAM_ENDPOINT_STREAMING: 1014 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED; 1015 l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0); 1016 break; 1017 default: 1018 break; 1019 } 1020 } 1021 1022 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED; 1023 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1024 return ERROR_CODE_SUCCESS; 1025 } 1026 1027 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){ 1028 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1029 if (!connection){ 1030 log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid); 1031 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1032 } 1033 1034 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) { 1035 log_error("avdtp_media_connect: wrong connection state %d", connection->state); 1036 return ERROR_CODE_COMMAND_DISALLOWED; 1037 } 1038 1039 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1040 if (!stream_endpoint) { 1041 log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid); 1042 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1043 } 1044 1045 if (stream_endpoint->remote_sep.seid != remote_seid){ 1046 log_error("avdtp_media_connect: no remote sep with seid %d registered with the stream endpoint", remote_seid); 1047 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1048 } 1049 1050 if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return ERROR_CODE_COMMAND_DISALLOWED; 1051 1052 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1053 connection->initiator_remote_seid = remote_seid; 1054 connection->initiator_local_seid = local_seid; 1055 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM; 1056 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM; 1057 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1058 return ERROR_CODE_SUCCESS; 1059 } 1060 1061 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1062 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1063 if (!connection){ 1064 log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1065 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1066 } 1067 1068 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1069 if (!stream_endpoint) { 1070 log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid); 1071 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1072 } 1073 1074 if (stream_endpoint->l2cap_media_cid == 0){ 1075 log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1076 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1077 } 1078 1079 if (!is_avdtp_remote_seid_registered(stream_endpoint)){ 1080 log_error("avdtp_media_connect: no remote sep registered with the stream endpoint"); 1081 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1082 } 1083 1084 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->start_stream == 1){ 1085 return ERROR_CODE_COMMAND_DISALLOWED; 1086 } 1087 1088 stream_endpoint->start_stream = 1; 1089 connection->initiator_local_seid = local_seid; 1090 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1091 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1092 return ERROR_CODE_SUCCESS; 1093 } 1094 1095 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1096 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1097 if (!connection){ 1098 log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1099 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1100 } 1101 1102 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1103 if (!stream_endpoint) { 1104 log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid); 1105 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1106 } 1107 1108 if (stream_endpoint->l2cap_media_cid == 0){ 1109 log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1110 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1111 } 1112 1113 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->close_stream){ 1114 return ERROR_CODE_COMMAND_DISALLOWED; 1115 } 1116 1117 stream_endpoint->close_stream = 1; 1118 connection->initiator_local_seid = local_seid; 1119 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1120 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1121 return ERROR_CODE_SUCCESS; 1122 } 1123 1124 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1125 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1126 if (!connection){ 1127 log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1128 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1129 } 1130 1131 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1132 if (!stream_endpoint) { 1133 log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid); 1134 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1135 } 1136 1137 if (stream_endpoint->l2cap_media_cid == 0){ 1138 log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1139 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1140 } 1141 1142 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->abort_stream){ 1143 return ERROR_CODE_COMMAND_DISALLOWED; 1144 } 1145 1146 stream_endpoint->abort_stream = 1; 1147 connection->initiator_local_seid = local_seid; 1148 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1149 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1150 return ERROR_CODE_SUCCESS; 1151 } 1152 1153 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1154 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1155 if (!connection){ 1156 log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1157 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1158 } 1159 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1160 if (!stream_endpoint) { 1161 log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid); 1162 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1163 } 1164 1165 if (stream_endpoint->l2cap_media_cid == 0){ 1166 log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1167 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1168 } 1169 1170 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->suspend_stream){ 1171 return ERROR_CODE_COMMAND_DISALLOWED; 1172 } 1173 1174 stream_endpoint->suspend_stream = 1; 1175 connection->initiator_local_seid = local_seid; 1176 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1177 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1178 return ERROR_CODE_SUCCESS; 1179 } 1180 1181 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid){ 1182 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1183 if (!connection){ 1184 log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid); 1185 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1186 } 1187 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1188 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1189 return ERROR_CODE_COMMAND_DISALLOWED; 1190 } 1191 1192 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1193 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS; 1194 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1195 } 1196 1197 1198 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 1199 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1200 if (!connection){ 1201 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1202 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1203 } 1204 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1205 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1206 return ERROR_CODE_COMMAND_DISALLOWED; 1207 } 1208 1209 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1210 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 1211 connection->initiator_remote_seid = remote_seid; 1212 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1213 } 1214 1215 1216 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 1217 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1218 if (!connection){ 1219 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1220 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1221 } 1222 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1223 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1224 return ERROR_CODE_COMMAND_DISALLOWED; 1225 } 1226 1227 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1228 connection->initiator_remote_seid = remote_seid; 1229 1230 if (connection->avdtp_version == 0){ 1231 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES; 1232 avdtp_handle_sdp_client_query_request.callback = &avdtp_handle_start_sdp_client_query; 1233 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1234 (void) sdp_client_register_query_callback(&avdtp_handle_sdp_client_query_request); 1235 return ERROR_CODE_SUCCESS; 1236 } else { 1237 // AVDTP version lower then 1.3 supports only get capabilities command 1238 if (connection->avdtp_version < 0x103){ 1239 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 1240 } else { 1241 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES; 1242 } 1243 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1244 } 1245 } 1246 1247 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){ 1248 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1249 if (!connection){ 1250 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1251 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1252 } 1253 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1254 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1255 return ERROR_CODE_COMMAND_DISALLOWED; 1256 } 1257 1258 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1259 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION; 1260 connection->initiator_remote_seid = remote_seid; 1261 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1262 } 1263 1264 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){ 1265 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1266 if (!connection){ 1267 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1268 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1269 } 1270 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1271 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1272 log_error("connection in wrong state, %d, initiator state %d", connection->state, connection->initiator_connection_state); 1273 return ERROR_CODE_COMMAND_DISALLOWED; 1274 } 1275 if (connection->configuration_state != AVDTP_CONFIGURATION_STATE_IDLE){ 1276 log_info("configuration already started, config state %u", connection->configuration_state); 1277 return ERROR_CODE_COMMAND_DISALLOWED; 1278 } 1279 1280 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 1281 if (!stream_endpoint) { 1282 log_error("No initiator stream endpoint for seid %d", local_seid); 1283 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1284 } 1285 if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_CONFIGURED){ 1286 log_error("Stream endpoint seid %d in wrong state %d", local_seid, stream_endpoint->state); 1287 return ERROR_CODE_COMMAND_DISALLOWED; 1288 } 1289 1290 connection->active_stream_endpoint = (void*) stream_endpoint; 1291 connection->configuration_state = AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED; 1292 1293 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1294 connection->initiator_remote_seid = remote_seid; 1295 connection->initiator_local_seid = local_seid; 1296 stream_endpoint->remote_configuration_bitmap = configured_services_bitmap; 1297 stream_endpoint->remote_configuration = configuration; 1298 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION; 1299 1300 log_debug("SE %p, initiator_config_state: 0x%02x", stream_endpoint->initiator_config_state); 1301 1302 // cache media codec information for SBC 1303 stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type; 1304 if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){ 1305 stream_endpoint->media_type = configuration.media_codec.media_type; 1306 (void)memcpy(stream_endpoint->media_codec_sbc_info, 1307 configuration.media_codec.media_codec_information, 4); 1308 } 1309 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1310 } 1311 1312 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){ 1313 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1314 if (!connection){ 1315 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1316 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1317 } 1318 //TODO: if opened only app capabilities, enable reconfigure for not opened 1319 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1320 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1321 return ERROR_CODE_COMMAND_DISALLOWED; 1322 } 1323 1324 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 1325 if (!stream_endpoint) { 1326 log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid); 1327 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1328 } 1329 1330 if (!is_avdtp_remote_seid_registered(stream_endpoint)){ 1331 log_error("avdtp_reconfigure: no associated remote sep"); 1332 return ERROR_CODE_COMMAND_DISALLOWED; 1333 } 1334 1335 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1336 connection->initiator_remote_seid = remote_seid; 1337 connection->initiator_local_seid = local_seid; 1338 stream_endpoint->remote_configuration_bitmap = configured_services_bitmap; 1339 stream_endpoint->remote_configuration = configuration; 1340 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID; 1341 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1342 } 1343 1344 void avdtp_set_preferred_sampling_frequeny(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency){ 1345 stream_endpoint->preferred_sampling_frequency = sampling_frequency; 1346 } 1347 1348 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){ 1349 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1350 uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap; 1351 1352 uint8_t channel_mode = AVDTP_SBC_STEREO; 1353 if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){ 1354 channel_mode = AVDTP_SBC_JOINT_STEREO; 1355 } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){ 1356 channel_mode = AVDTP_SBC_STEREO; 1357 } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){ 1358 channel_mode = AVDTP_SBC_DUAL_CHANNEL; 1359 } else if (channel_mode_bitmap & AVDTP_SBC_MONO){ 1360 channel_mode = AVDTP_SBC_MONO; 1361 } 1362 return channel_mode; 1363 } 1364 1365 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){ 1366 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1367 uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap; 1368 1369 uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 1370 if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){ 1371 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 1372 } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){ 1373 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR; 1374 } 1375 return allocation_method; 1376 } 1377 1378 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){ 1379 if (!stream_endpoint) return 0; 1380 return stream_endpoint->sep.seid; 1381 } 1382 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){ 1383 if (!stream_endpoint) return 0; 1384 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1385 uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap; 1386 1387 uint8_t subbands = AVDTP_SBC_SUBBANDS_8; 1388 if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){ 1389 subbands = AVDTP_SBC_SUBBANDS_8; 1390 } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){ 1391 subbands = AVDTP_SBC_SUBBANDS_4; 1392 } 1393 return subbands; 1394 } 1395 1396 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){ 1397 if (!stream_endpoint) return 0; 1398 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1399 uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap; 1400 1401 uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16; 1402 if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){ 1403 block_length = AVDTP_SBC_BLOCK_LENGTH_16; 1404 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){ 1405 block_length = AVDTP_SBC_BLOCK_LENGTH_12; 1406 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){ 1407 block_length = AVDTP_SBC_BLOCK_LENGTH_8; 1408 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){ 1409 block_length = AVDTP_SBC_BLOCK_LENGTH_4; 1410 } 1411 return block_length; 1412 } 1413 1414 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){ 1415 if (!stream_endpoint) return 0; 1416 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1417 uint8_t supported_sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap; 1418 1419 // use preferred sampling frequency if possible 1420 if ((stream_endpoint->preferred_sampling_frequency == 48000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_48000)){ 1421 return AVDTP_SBC_48000; 1422 } 1423 if ((stream_endpoint->preferred_sampling_frequency == 44100) && (supported_sampling_frequency_bitmap & AVDTP_SBC_44100)){ 1424 return AVDTP_SBC_44100; 1425 } 1426 if ((stream_endpoint->preferred_sampling_frequency == 32000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_32000)){ 1427 return AVDTP_SBC_32000; 1428 } 1429 if ((stream_endpoint->preferred_sampling_frequency == 16000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_16000)){ 1430 return AVDTP_SBC_16000; 1431 } 1432 1433 // otherwise, use highest available 1434 if (supported_sampling_frequency_bitmap & AVDTP_SBC_48000){ 1435 return AVDTP_SBC_48000; 1436 } 1437 if (supported_sampling_frequency_bitmap & AVDTP_SBC_44100){ 1438 return AVDTP_SBC_44100; 1439 } 1440 if (supported_sampling_frequency_bitmap & AVDTP_SBC_32000){ 1441 return AVDTP_SBC_32000; 1442 } 1443 if (supported_sampling_frequency_bitmap & AVDTP_SBC_16000){ 1444 return AVDTP_SBC_16000; 1445 } 1446 return AVDTP_SBC_44100; // some default 1447 } 1448 1449 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){ 1450 if (!stream_endpoint) return 0; 1451 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1452 return btstack_min(media_codec[3], remote_max_bitpool_value); 1453 } 1454 1455 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){ 1456 if (!stream_endpoint) return 0; 1457 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1458 return btstack_max(media_codec[2], remote_min_bitpool_value); 1459 } 1460 1461 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint){ 1462 if (!stream_endpoint) return 0; 1463 if (stream_endpoint->remote_sep.seid == 0) return 0; 1464 if (stream_endpoint->remote_sep.seid > 0x3E) return 0; 1465 return 1; 1466 } 1467 1468 void avdtp_init(void){ 1469 static bool l2cap_registered = false; 1470 if (!l2cap_registered){ 1471 l2cap_registered = true; 1472 l2cap_register_service(&avdtp_packet_handler, BLUETOOTH_PSM_AVDTP, 0xffff, gap_get_security_level()); 1473 } 1474 } 1475