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