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