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