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