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