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