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