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, 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, 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 btstack_run_loop_remove_timer(&connection->retry_timer); 711 } 712 return connection; 713 } 714 715 static void avdtp_retry_timer_timeout_handler(btstack_timer_source_t * timer){ 716 uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 717 718 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 719 if (connection == NULL) return; 720 721 if (connection->state == AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY){ 722 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 723 l2cap_create_channel(&avdtp_packet_handler, connection->remote_addr, connection->avdtp_l2cap_psm, l2cap_max_mtu(), NULL); 724 } 725 } 726 727 static void avdtp_retry_timer_start(avdtp_connection_t * connection){ 728 btstack_run_loop_set_timer_handler(&connection->retry_timer, avdtp_retry_timer_timeout_handler); 729 btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avdtp_cid); 730 731 // add some jitter/randomness to reconnect delay 732 uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F); 733 btstack_run_loop_set_timer(&connection->retry_timer, timeout); 734 btstack_run_loop_add_timer(&connection->retry_timer); 735 } 736 737 738 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 739 bd_addr_t event_addr; 740 uint16_t psm; 741 uint16_t local_cid; 742 uint8_t status; 743 uint16_t l2cap_mtu; 744 745 bool accept_streaming_connection; 746 bool outoing_signaling_active; 747 bool decline_connection; 748 749 avdtp_stream_endpoint_t * stream_endpoint = NULL; 750 avdtp_connection_t * connection = NULL; 751 752 switch (packet_type) { 753 case L2CAP_DATA_PACKET: 754 connection = avdtp_get_connection_for_l2cap_signaling_cid(channel); 755 if (connection){ 756 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 757 break; 758 } 759 760 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel); 761 if (!stream_endpoint){ 762 if (!connection) break; 763 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 764 break; 765 } 766 767 if (stream_endpoint->connection){ 768 if (channel == stream_endpoint->connection->l2cap_signaling_cid){ 769 handle_l2cap_data_packet_for_signaling_connection(stream_endpoint->connection, packet, size); 770 break; 771 } 772 } 773 774 if (channel == stream_endpoint->l2cap_media_cid){ 775 btstack_assert(avdtp_sink_handle_media_data); 776 (*avdtp_sink_handle_media_data)(avdtp_local_seid(stream_endpoint), packet, size); 777 break; 778 } 779 780 if (channel == stream_endpoint->l2cap_reporting_cid){ 781 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED"); 782 } else if (channel == stream_endpoint->l2cap_recovery_cid){ 783 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED"); 784 } else { 785 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel); 786 } 787 break; 788 789 case HCI_EVENT_PACKET: 790 switch (hci_event_packet_get_type(packet)) { 791 792 case L2CAP_EVENT_INCOMING_CONNECTION: 793 l2cap_event_incoming_connection_get_address(packet, event_addr); 794 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 795 796 outoing_signaling_active = false; 797 accept_streaming_connection = false; 798 799 connection = avdtp_get_connection_for_bd_addr(event_addr); 800 if (connection != NULL){ 801 switch (connection->state){ 802 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED: 803 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED: 804 outoing_signaling_active = true; 805 connection->incoming_declined = true; 806 break; 807 case AVDTP_SIGNALING_CONNECTION_OPENED: 808 outoing_signaling_active = true; 809 accept_streaming_connection = true; 810 break; 811 default: 812 break; 813 } 814 } 815 log_info("incoming: %s, outoing_signaling_active %d, accept_streaming_connection %d", 816 bd_addr_to_str(event_addr), outoing_signaling_active, accept_streaming_connection); 817 818 decline_connection = outoing_signaling_active && !accept_streaming_connection; 819 if (outoing_signaling_active == false){ 820 connection = avdtp_handle_incoming_connection(connection, event_addr, local_cid); 821 if (connection == NULL){ 822 decline_connection = true; 823 } 824 } else if (accept_streaming_connection){ 825 if ((connection == NULL) || (connection->configuration_state != AVDTP_CONFIGURATION_STATE_REMOTE_CONFIGURED)) { 826 decline_connection = true; 827 } else { 828 // now, we're only dealing with media connections that are created by remote side - we're acceptor here 829 stream_endpoint = avdtp_get_stream_endpoint_with_seid(connection->acceptor_local_seid); 830 if ((stream_endpoint == NULL) || (stream_endpoint->l2cap_media_cid != 0) ) { 831 decline_connection = true; 832 } 833 } 834 } 835 836 if (decline_connection){ 837 l2cap_decline_connection(local_cid); 838 } else { 839 l2cap_accept_connection(local_cid); 840 } 841 break; 842 843 case L2CAP_EVENT_CHANNEL_OPENED: 844 845 psm = l2cap_event_channel_opened_get_psm(packet); 846 if (psm != BLUETOOTH_PSM_AVDTP){ 847 log_info("Unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED "); 848 return; 849 } 850 851 status = l2cap_event_channel_opened_get_status(packet); 852 // inform about new l2cap connection 853 l2cap_event_channel_opened_get_address(packet, event_addr); 854 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 855 l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 856 connection = avdtp_get_connection_for_bd_addr(event_addr); 857 if (connection == NULL){ 858 log_info("L2CAP_EVENT_CHANNEL_OPENED: no connection found for %s", bd_addr_to_str(event_addr)); 859 break; 860 } 861 862 switch (connection->state){ 863 case AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED: 864 switch (status){ 865 case ERROR_CODE_SUCCESS: 866 connection->l2cap_signaling_cid = local_cid; 867 connection->incoming_declined = false; 868 connection->l2cap_mtu = l2cap_mtu; 869 connection->state = AVDTP_SIGNALING_CONNECTION_OPENED; 870 log_info("Connection opened l2cap_signaling_cid 0x%02x, avdtp_cid 0x%02x", connection->l2cap_signaling_cid, connection->avdtp_cid); 871 avdtp_signaling_emit_connection_established(connection->avdtp_cid, event_addr, 872 status); 873 return; 874 case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES: 875 if (connection->incoming_declined == true) { 876 log_info("Connection was declined, and the outgoing failed"); 877 connection->state = AVDTP_SIGNALING_CONNECTION_W2_L2CAP_RETRY; 878 connection->incoming_declined = false; 879 avdtp_retry_timer_start(connection); 880 return; 881 } 882 break; 883 default: 884 log_info("Connection to %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 885 break; 886 } 887 avdtp_signaling_emit_connection_established(connection->avdtp_cid, event_addr, status); 888 avdtp_finalize_connection(connection); 889 break; 890 891 case AVDTP_SIGNALING_CONNECTION_OPENED: 892 stream_endpoint = avdtp_get_stream_endpoint_for_signaling_cid(connection->l2cap_signaling_cid); 893 if (!stream_endpoint){ 894 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found for signaling cid 0x%02x", connection->l2cap_signaling_cid); 895 return; 896 } 897 if (status != ERROR_CODE_SUCCESS){ 898 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)); 899 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE; 900 avdtp_streaming_emit_connection_established(stream_endpoint, status); 901 break; 902 } 903 switch (stream_endpoint->state){ 904 case AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED: 905 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 906 stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet); 907 stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet); 908 909 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)); 910 avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_SUCCESS); 911 break; 912 default: 913 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)); 914 avdtp_streaming_emit_connection_established(stream_endpoint, ERROR_CODE_COMMAND_DISALLOWED); 915 break; 916 } 917 break; 918 919 default: 920 log_info("L2CAP connection to %s ignored: status code 0x%02x, connection state %d", bd_addr_to_str(event_addr), status, connection->state); 921 break; 922 } 923 break; 924 925 case L2CAP_EVENT_CHANNEL_CLOSED: 926 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 927 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(local_cid); 928 connection = avdtp_get_connection_for_l2cap_signaling_cid(local_cid); 929 930 log_info("Received L2CAP_EVENT_CHANNEL_CLOSED, cid 0x%2x, connection %p, stream_endpoint %p", local_cid, connection, stream_endpoint); 931 932 if (stream_endpoint){ 933 if (stream_endpoint->l2cap_media_cid == local_cid){ 934 connection = stream_endpoint->connection; 935 if (connection) { 936 avdtp_streaming_emit_connection_released(stream_endpoint, 937 connection->avdtp_cid, 938 avdtp_local_seid(stream_endpoint)); 939 } 940 avdtp_reset_stream_endpoint(stream_endpoint); 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 HCI_EVENT_DISCONNECTION_COMPLETE: 972 break; 973 974 case L2CAP_EVENT_CAN_SEND_NOW: 975 log_debug("avdtp_packet_handler, L2CAP_EVENT_CAN_SEND_NOW l2cap_cid 0x%02x", channel); 976 connection = avdtp_get_connection_for_l2cap_signaling_cid(channel); 977 if (!connection) { 978 stream_endpoint = avdtp_get_stream_endpoint_for_l2cap_cid(channel); 979 if (!stream_endpoint->connection) break; 980 connection = stream_endpoint->connection; 981 } 982 avdtp_handle_can_send_now(connection, channel); 983 break; 984 default: 985 log_info("Unknown HCI event type %02x", hci_event_packet_get_type(packet)); 986 break; 987 } 988 break; 989 990 default: 991 // other packet type 992 break; 993 } 994 } 995 996 uint8_t avdtp_disconnect(uint16_t avdtp_cid){ 997 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 998 if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 999 1000 if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return ERROR_CODE_SUCCESS; 1001 1002 btstack_linked_list_iterator_t it; 1003 btstack_linked_list_iterator_init(&it, avdtp_get_stream_endpoints()); 1004 1005 while (btstack_linked_list_iterator_has_next(&it)){ 1006 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 1007 if (stream_endpoint->connection != connection) continue; 1008 1009 switch (stream_endpoint->state){ 1010 case AVDTP_STREAM_ENDPOINT_OPENED: 1011 case AVDTP_STREAM_ENDPOINT_STREAMING: 1012 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED; 1013 l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0); 1014 break; 1015 default: 1016 break; 1017 } 1018 } 1019 1020 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED; 1021 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1022 return ERROR_CODE_SUCCESS; 1023 } 1024 1025 uint8_t avdtp_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){ 1026 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1027 if (!connection){ 1028 log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid); 1029 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1030 } 1031 1032 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) { 1033 log_error("avdtp_media_connect: wrong connection state %d", connection->state); 1034 return ERROR_CODE_COMMAND_DISALLOWED; 1035 } 1036 1037 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1038 if (!stream_endpoint) { 1039 log_error("avdtp_media_connect: no stream_endpoint with seid %d found", local_seid); 1040 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1041 } 1042 1043 if (stream_endpoint->remote_sep.seid != remote_seid){ 1044 log_error("avdtp_media_connect: no remote sep with seid %d registered with the stream endpoint", remote_seid); 1045 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1046 } 1047 1048 if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return ERROR_CODE_COMMAND_DISALLOWED; 1049 1050 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1051 connection->initiator_remote_seid = remote_seid; 1052 connection->initiator_local_seid = local_seid; 1053 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM; 1054 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM; 1055 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1056 return ERROR_CODE_SUCCESS; 1057 } 1058 1059 uint8_t avdtp_start_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1060 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1061 if (!connection){ 1062 log_error("avdtp_start_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1063 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1064 } 1065 1066 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1067 if (!stream_endpoint) { 1068 log_error("avdtp_start_stream: no stream_endpoint with seid %d found", local_seid); 1069 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1070 } 1071 1072 if (stream_endpoint->l2cap_media_cid == 0){ 1073 log_error("avdtp_start_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1074 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1075 } 1076 1077 if (!is_avdtp_remote_seid_registered(stream_endpoint)){ 1078 log_error("avdtp_media_connect: no remote sep registered with the stream endpoint"); 1079 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1080 } 1081 1082 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->start_stream == 1){ 1083 return ERROR_CODE_COMMAND_DISALLOWED; 1084 } 1085 1086 stream_endpoint->start_stream = 1; 1087 connection->initiator_local_seid = local_seid; 1088 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1089 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1090 return ERROR_CODE_SUCCESS; 1091 } 1092 1093 uint8_t avdtp_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1094 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1095 if (!connection){ 1096 log_error("avdtp_stop_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1097 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1098 } 1099 1100 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1101 if (!stream_endpoint) { 1102 log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", local_seid); 1103 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1104 } 1105 1106 if (stream_endpoint->l2cap_media_cid == 0){ 1107 log_error("avdtp_stop_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1108 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1109 } 1110 1111 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->stop_stream){ 1112 return ERROR_CODE_COMMAND_DISALLOWED; 1113 } 1114 1115 stream_endpoint->stop_stream = 1; 1116 connection->initiator_local_seid = local_seid; 1117 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1118 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1119 return ERROR_CODE_SUCCESS; 1120 } 1121 1122 uint8_t avdtp_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1123 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1124 if (!connection){ 1125 log_error("avdtp_abort_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1126 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1127 } 1128 1129 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1130 if (!stream_endpoint) { 1131 log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", local_seid); 1132 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1133 } 1134 1135 if (stream_endpoint->l2cap_media_cid == 0){ 1136 log_error("avdtp_abort_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1137 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1138 } 1139 1140 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->abort_stream){ 1141 return ERROR_CODE_COMMAND_DISALLOWED; 1142 } 1143 1144 stream_endpoint->abort_stream = 1; 1145 connection->initiator_local_seid = local_seid; 1146 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1147 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1148 return ERROR_CODE_SUCCESS; 1149 } 1150 1151 uint8_t avdtp_suspend_stream(uint16_t avdtp_cid, uint8_t local_seid){ 1152 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1153 if (!connection){ 1154 log_error("avdtp_suspend_stream: no connection for signaling cid 0x%02x found", avdtp_cid); 1155 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1156 } 1157 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_with_seid(local_seid); 1158 if (!stream_endpoint) { 1159 log_error("avdtp_suspend_stream: no stream_endpoint with seid %d found", local_seid); 1160 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1161 } 1162 1163 if (stream_endpoint->l2cap_media_cid == 0){ 1164 log_error("avdtp_suspend_stream: no media connection for stream_endpoint with seid %d found", local_seid); 1165 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1166 } 1167 1168 if (!is_avdtp_remote_seid_registered(stream_endpoint) || stream_endpoint->suspend_stream){ 1169 return ERROR_CODE_COMMAND_DISALLOWED; 1170 } 1171 1172 stream_endpoint->suspend_stream = 1; 1173 connection->initiator_local_seid = local_seid; 1174 connection->initiator_remote_seid = stream_endpoint->remote_sep.seid; 1175 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1176 return ERROR_CODE_SUCCESS; 1177 } 1178 1179 uint8_t avdtp_discover_stream_endpoints(uint16_t avdtp_cid){ 1180 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1181 if (!connection){ 1182 log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid); 1183 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1184 } 1185 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1186 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1187 return ERROR_CODE_COMMAND_DISALLOWED; 1188 } 1189 1190 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1191 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS; 1192 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1193 } 1194 1195 1196 uint8_t avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 1197 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1198 if (!connection){ 1199 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1200 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1201 } 1202 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1203 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1204 return ERROR_CODE_COMMAND_DISALLOWED; 1205 } 1206 1207 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1208 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 1209 connection->initiator_remote_seid = remote_seid; 1210 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1211 } 1212 1213 1214 uint8_t avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){ 1215 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1216 if (!connection){ 1217 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1218 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1219 } 1220 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1221 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1222 return ERROR_CODE_COMMAND_DISALLOWED; 1223 } 1224 1225 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1226 connection->initiator_remote_seid = remote_seid; 1227 1228 if (connection->avdtp_version == 0){ 1229 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_SEND_SDP_QUERY_THEN_GET_ALL_CAPABILITIES; 1230 avdtp_handle_sdp_client_query_request.callback = &avdtp_handle_start_sdp_client_query; 1231 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1232 (void) sdp_client_register_query_callback(&avdtp_handle_sdp_client_query_request); 1233 return ERROR_CODE_SUCCESS; 1234 } else { 1235 // AVDTP version lower then 1.3 supports only get capabilities command 1236 if (connection->avdtp_version < 0x103){ 1237 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 1238 } else { 1239 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES; 1240 } 1241 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1242 } 1243 } 1244 1245 uint8_t avdtp_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){ 1246 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1247 if (!connection){ 1248 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1249 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1250 } 1251 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1252 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1253 return ERROR_CODE_COMMAND_DISALLOWED; 1254 } 1255 1256 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1257 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION; 1258 connection->initiator_remote_seid = remote_seid; 1259 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1260 } 1261 1262 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){ 1263 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1264 if (!connection){ 1265 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1266 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1267 } 1268 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1269 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1270 log_error("connection in wrong state, %d, initiator state %d", connection->state, connection->initiator_connection_state); 1271 return ERROR_CODE_COMMAND_DISALLOWED; 1272 } 1273 if (connection->configuration_state != AVDTP_CONFIGURATION_STATE_IDLE){ 1274 log_info("configuration already started, config state %u", connection->configuration_state); 1275 return ERROR_CODE_COMMAND_DISALLOWED; 1276 } 1277 1278 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 1279 if (!stream_endpoint) { 1280 log_error("No initiator stream endpoint for seid %d", local_seid); 1281 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1282 } 1283 if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_CONFIGURED){ 1284 log_error("Stream endpoint seid %d in wrong state %d", local_seid, stream_endpoint->state); 1285 return ERROR_CODE_COMMAND_DISALLOWED; 1286 } 1287 1288 connection->active_stream_endpoint = (void*) stream_endpoint; 1289 connection->configuration_state = AVDTP_CONFIGURATION_STATE_LOCAL_INITIATED; 1290 1291 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1292 connection->initiator_remote_seid = remote_seid; 1293 connection->initiator_local_seid = local_seid; 1294 stream_endpoint->remote_configuration_bitmap = configured_services_bitmap; 1295 stream_endpoint->remote_configuration = configuration; 1296 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION; 1297 1298 // cache media codec information for SBC 1299 stream_endpoint->media_codec_type = configuration.media_codec.media_codec_type; 1300 if (configuration.media_codec.media_codec_type == AVDTP_CODEC_SBC){ 1301 stream_endpoint->media_type = configuration.media_codec.media_type; 1302 (void)memcpy(stream_endpoint->media_codec_sbc_info, 1303 configuration.media_codec.media_codec_information, 4); 1304 } 1305 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1306 } 1307 1308 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){ 1309 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1310 if (!connection){ 1311 log_error("No connection for AVDTP cid 0x%02x found", avdtp_cid); 1312 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1313 } 1314 //TODO: if opened only app capabilities, enable reconfigure for not opened 1315 if ((connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) || 1316 (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE)) { 1317 return ERROR_CODE_COMMAND_DISALLOWED; 1318 } 1319 1320 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 1321 if (!stream_endpoint) { 1322 log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", local_seid); 1323 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1324 } 1325 1326 if (!is_avdtp_remote_seid_registered(stream_endpoint)){ 1327 log_error("avdtp_reconfigure: no associated remote sep"); 1328 return ERROR_CODE_COMMAND_DISALLOWED; 1329 } 1330 1331 connection->initiator_transaction_label= avdtp_get_next_transaction_label(); 1332 connection->initiator_remote_seid = remote_seid; 1333 connection->initiator_local_seid = local_seid; 1334 stream_endpoint->remote_configuration_bitmap = configured_services_bitmap; 1335 stream_endpoint->remote_configuration = configuration; 1336 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID; 1337 return avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 1338 } 1339 1340 void avdtp_set_preferred_sampling_frequeny(avdtp_stream_endpoint_t * stream_endpoint, uint32_t sampling_frequency){ 1341 stream_endpoint->preferred_sampling_frequency = sampling_frequency; 1342 } 1343 1344 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){ 1345 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1346 uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap; 1347 1348 uint8_t channel_mode = AVDTP_SBC_STEREO; 1349 if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){ 1350 channel_mode = AVDTP_SBC_JOINT_STEREO; 1351 } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){ 1352 channel_mode = AVDTP_SBC_STEREO; 1353 } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){ 1354 channel_mode = AVDTP_SBC_DUAL_CHANNEL; 1355 } else if (channel_mode_bitmap & AVDTP_SBC_MONO){ 1356 channel_mode = AVDTP_SBC_MONO; 1357 } 1358 return channel_mode; 1359 } 1360 1361 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){ 1362 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1363 uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap; 1364 1365 uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 1366 if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){ 1367 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 1368 } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){ 1369 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR; 1370 } 1371 return allocation_method; 1372 } 1373 1374 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){ 1375 if (!stream_endpoint) return 0; 1376 return stream_endpoint->sep.seid; 1377 } 1378 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){ 1379 if (!stream_endpoint) return 0; 1380 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1381 uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap; 1382 1383 uint8_t subbands = AVDTP_SBC_SUBBANDS_8; 1384 if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){ 1385 subbands = AVDTP_SBC_SUBBANDS_8; 1386 } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){ 1387 subbands = AVDTP_SBC_SUBBANDS_4; 1388 } 1389 return subbands; 1390 } 1391 1392 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){ 1393 if (!stream_endpoint) return 0; 1394 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1395 uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap; 1396 1397 uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16; 1398 if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){ 1399 block_length = AVDTP_SBC_BLOCK_LENGTH_16; 1400 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){ 1401 block_length = AVDTP_SBC_BLOCK_LENGTH_12; 1402 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){ 1403 block_length = AVDTP_SBC_BLOCK_LENGTH_8; 1404 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){ 1405 block_length = AVDTP_SBC_BLOCK_LENGTH_4; 1406 } 1407 return block_length; 1408 } 1409 1410 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){ 1411 if (!stream_endpoint) return 0; 1412 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1413 uint8_t supported_sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap; 1414 1415 // use preferred sampling frequency if possible 1416 if ((stream_endpoint->preferred_sampling_frequency == 48000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_48000)){ 1417 return AVDTP_SBC_48000; 1418 } 1419 if ((stream_endpoint->preferred_sampling_frequency == 44100) && (supported_sampling_frequency_bitmap & AVDTP_SBC_44100)){ 1420 return AVDTP_SBC_44100; 1421 } 1422 if ((stream_endpoint->preferred_sampling_frequency == 32000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_32000)){ 1423 return AVDTP_SBC_32000; 1424 } 1425 if ((stream_endpoint->preferred_sampling_frequency == 16000) && (supported_sampling_frequency_bitmap & AVDTP_SBC_16000)){ 1426 return AVDTP_SBC_16000; 1427 } 1428 1429 // otherwise, use highest available 1430 if (supported_sampling_frequency_bitmap & AVDTP_SBC_48000){ 1431 return AVDTP_SBC_48000; 1432 } 1433 if (supported_sampling_frequency_bitmap & AVDTP_SBC_44100){ 1434 return AVDTP_SBC_44100; 1435 } 1436 if (supported_sampling_frequency_bitmap & AVDTP_SBC_32000){ 1437 return AVDTP_SBC_32000; 1438 } 1439 if (supported_sampling_frequency_bitmap & AVDTP_SBC_16000){ 1440 return AVDTP_SBC_16000; 1441 } 1442 return AVDTP_SBC_44100; // some default 1443 } 1444 1445 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){ 1446 if (!stream_endpoint) return 0; 1447 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 1448 return btstack_min(media_codec[3], remote_max_bitpool_value); 1449 } 1450 1451 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_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_max(media_codec[2], remote_min_bitpool_value); 1455 } 1456 1457 uint8_t is_avdtp_remote_seid_registered(avdtp_stream_endpoint_t * stream_endpoint){ 1458 if (!stream_endpoint) return 0; 1459 if (stream_endpoint->remote_sep.seid == 0) return 0; 1460 if (stream_endpoint->remote_sep.seid > 0x3E) return 0; 1461 return 1; 1462 } 1463 1464 void avdtp_init(void){ 1465 static bool l2cap_registered = false; 1466 if (!l2cap_registered){ 1467 l2cap_registered = true; 1468 l2cap_register_service(&avdtp_packet_handler, BLUETOOTH_PSM_AVDTP, 0xffff, gap_get_security_level()); 1469 } 1470 } 1471