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 <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "btstack.h" 47 #include "avdtp.h" 48 #include "avdtp_util.h" 49 #include "avdtp_acceptor.h" 50 #include "avdtp_initiator.h" 51 52 static int record_id = -1; 53 static uint8_t attribute_value[1000]; 54 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 55 56 typedef struct { 57 avdtp_connection_t * connection; 58 btstack_packet_handler_t avdtp_callback; 59 avdtp_sep_type_t query_role; 60 btstack_packet_handler_t packet_handler; 61 } avdtp_sdp_query_context_t; 62 63 static avdtp_sdp_query_context_t sdp_query_context; 64 65 static void (*handle_media_data)(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size); 66 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 67 68 void avdtp_connect(bd_addr_t remote, avdtp_sep_type_t query_role, avdtp_context_t * avdtp_context){ 69 sdp_query_context.connection = NULL; 70 avdtp_connection_t * connection = avdtp_connection_for_bd_addr(remote, avdtp_context); 71 if (!connection){ 72 connection = avdtp_create_connection(remote, avdtp_context); 73 } 74 if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; 75 connection->state = AVDTP_SIGNALING_W4_SDP_QUERY_COMPLETE; 76 sdp_query_context.connection = connection; 77 sdp_query_context.query_role = query_role; 78 sdp_query_context.avdtp_callback = avdtp_context->avdtp_callback; 79 sdp_query_context.packet_handler = avdtp_context->packet_handler; 80 81 sdp_client_query_uuid16(&avdtp_handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_AVDTP); 82 } 83 84 void avdtp_register_media_transport_category(avdtp_stream_endpoint_t * stream_endpoint){ 85 if (!stream_endpoint){ 86 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 87 return; 88 } 89 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_TRANSPORT, 1); 90 stream_endpoint->sep.registered_service_categories = bitmap; 91 } 92 93 void avdtp_register_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){ 94 if (!stream_endpoint){ 95 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 96 return; 97 } 98 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_REPORTING, 1); 99 stream_endpoint->sep.registered_service_categories = bitmap; 100 } 101 102 void avdtp_register_delay_reporting_category(avdtp_stream_endpoint_t * stream_endpoint){ 103 if (!stream_endpoint){ 104 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 105 return; 106 } 107 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_DELAY_REPORTING, 1); 108 stream_endpoint->sep.registered_service_categories = bitmap; 109 } 110 111 void avdtp_register_recovery_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 112 if (!stream_endpoint){ 113 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 114 return; 115 } 116 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_RECOVERY, 1); 117 stream_endpoint->sep.registered_service_categories = bitmap; 118 stream_endpoint->sep.capabilities.recovery.recovery_type = 0x01; // 0x01 = RFC2733 119 stream_endpoint->sep.capabilities.recovery.maximum_recovery_window_size = maximum_recovery_window_size; 120 stream_endpoint->sep.capabilities.recovery.maximum_number_media_packets = maximum_number_media_packets; 121 } 122 123 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){ 124 if (!stream_endpoint){ 125 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 126 return; 127 } 128 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_CONTENT_PROTECTION, 1); 129 stream_endpoint->sep.registered_service_categories = bitmap; 130 stream_endpoint->sep.capabilities.content_protection.cp_type = cp_type; 131 stream_endpoint->sep.capabilities.content_protection.cp_type_value = cp_type_value; 132 stream_endpoint->sep.capabilities.content_protection.cp_type_value_len = cp_type_value_len; 133 } 134 135 void avdtp_register_header_compression_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t back_ch, uint8_t media, uint8_t recovery){ 136 if (!stream_endpoint){ 137 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 138 return; 139 } 140 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_HEADER_COMPRESSION, 1); 141 stream_endpoint->sep.registered_service_categories = bitmap; 142 stream_endpoint->sep.capabilities.header_compression.back_ch = back_ch; 143 stream_endpoint->sep.capabilities.header_compression.media = media; 144 stream_endpoint->sep.capabilities.header_compression.recovery = recovery; 145 } 146 147 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){ 148 if (!stream_endpoint){ 149 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 150 return; 151 } 152 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MEDIA_CODEC, 1); 153 stream_endpoint->sep.registered_service_categories = bitmap; 154 stream_endpoint->sep.capabilities.media_codec.media_type = media_type; 155 stream_endpoint->sep.capabilities.media_codec.media_codec_type = media_codec_type; 156 stream_endpoint->sep.capabilities.media_codec.media_codec_information = media_codec_info; 157 stream_endpoint->sep.capabilities.media_codec.media_codec_information_len = media_codec_info_len; 158 } 159 160 void avdtp_register_multiplexing_category(avdtp_stream_endpoint_t * stream_endpoint, uint8_t fragmentation){ 161 if (!stream_endpoint){ 162 log_error("avdtp_register_media_transport_category: stream endpoint with given seid is not registered"); 163 return; 164 } 165 uint16_t bitmap = store_bit16(stream_endpoint->sep.registered_service_categories, AVDTP_MULTIPLEXING, 1); 166 stream_endpoint->sep.registered_service_categories = bitmap; 167 stream_endpoint->sep.capabilities.multiplexing_mode.fragmentation = fragmentation; 168 } 169 170 171 /* START: tracking can send now requests pro l2cap cid */ 172 void avdtp_handle_can_send_now(avdtp_connection_t * connection, uint16_t l2cap_cid, avdtp_context_t * context){ 173 if (connection->wait_to_send_acceptor){ 174 connection->wait_to_send_acceptor = 0; 175 avdtp_acceptor_stream_config_subsm_run(connection, context); 176 } else if (connection->wait_to_send_initiator){ 177 connection->wait_to_send_initiator = 0; 178 avdtp_initiator_stream_config_subsm_run(connection, context); 179 } else if (connection->wait_to_send_self){ 180 connection->wait_to_send_self = 0; 181 if (connection->disconnect){ 182 btstack_linked_list_iterator_t it; 183 btstack_linked_list_iterator_init(&it, &context->stream_endpoints); 184 while (btstack_linked_list_iterator_has_next(&it)){ 185 avdtp_stream_endpoint_t * stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 186 if (stream_endpoint->connection == connection){ 187 if (stream_endpoint->state >= AVDTP_STREAM_ENDPOINT_OPENED && stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED){ 188 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_DISCONNECTED; 189 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid); 190 l2cap_disconnect(stream_endpoint->l2cap_media_cid, 0); 191 return; 192 } 193 } 194 } 195 connection->disconnect = 0; 196 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED; 197 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 198 return; 199 } 200 } 201 202 // re-register 203 int more_to_send = connection->wait_to_send_acceptor || connection->wait_to_send_initiator || connection->wait_to_send_self; 204 if (more_to_send){ 205 l2cap_request_can_send_now_event(l2cap_cid); 206 } 207 } 208 /* END: tracking can send now requests pro l2cap cid */ 209 210 avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_context_t * context){ 211 avdtp_connection_t * connection = btstack_memory_avdtp_connection_get(); 212 memset(connection, 0, sizeof(avdtp_connection_t)); 213 connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; 214 connection->initiator_transaction_label++; 215 memcpy(connection->remote_addr, remote_addr, 6); 216 btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection); 217 return connection; 218 } 219 220 avdtp_stream_endpoint_t * avdtp_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type, avdtp_context_t * context){ 221 avdtp_stream_endpoint_t * stream_endpoint = btstack_memory_avdtp_stream_endpoint_get(); 222 memset(stream_endpoint, 0, sizeof(avdtp_stream_endpoint_t)); 223 context->stream_endpoints_id_counter++; 224 stream_endpoint->sep.seid = context->stream_endpoints_id_counter; 225 stream_endpoint->sep.media_type = media_type; 226 stream_endpoint->sep.type = sep_type; 227 btstack_linked_list_add(&context->stream_endpoints, (btstack_linked_item_t *) stream_endpoint); 228 return stream_endpoint; 229 } 230 231 232 static void handle_l2cap_data_packet_for_signaling_connection(avdtp_connection_t * connection, uint8_t *packet, uint16_t size, avdtp_context_t * context){ 233 int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size); 234 switch (connection->signaling_packet.message_type){ 235 case AVDTP_CMD_MSG: 236 avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context); 237 break; 238 default: 239 avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context); 240 break; 241 } 242 } 243 244 static void stream_endpoint_state_machine(avdtp_connection_t * connection, avdtp_stream_endpoint_t * stream_endpoint, uint8_t packet_type, uint8_t event, uint8_t *packet, uint16_t size, avdtp_context_t * context){ 245 uint16_t local_cid; 246 switch (packet_type){ 247 case L2CAP_DATA_PACKET:{ 248 int offset = avdtp_read_signaling_header(&connection->signaling_packet, packet, size); 249 if (connection->signaling_packet.message_type == AVDTP_CMD_MSG){ 250 avdtp_acceptor_stream_config_subsm(connection, packet, size, offset, context); 251 } else { 252 avdtp_initiator_stream_config_subsm(connection, packet, size, offset, context); 253 } 254 break; 255 } 256 case HCI_EVENT_PACKET: 257 switch (event){ 258 case L2CAP_EVENT_CHANNEL_OPENED: 259 if (stream_endpoint->l2cap_media_cid == 0){ 260 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) return; 261 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_OPENED; 262 stream_endpoint->connection = connection; 263 stream_endpoint->l2cap_media_cid = l2cap_event_channel_opened_get_local_cid(packet); 264 stream_endpoint->media_con_handle = l2cap_event_channel_opened_get_handle(packet); 265 log_info(" -> AVDTP_STREAM_ENDPOINT_OPENED, media con handle 0x%02x, l2cap_media_cid 0x%02x", stream_endpoint->media_con_handle, stream_endpoint->l2cap_media_cid); 266 avdtp_streaming_emit_connection_established(context->avdtp_callback, connection->l2cap_signaling_cid, stream_endpoint->sep.seid, connection->remote_seps[stream_endpoint->remote_sep_index].seid, 0); 267 break; 268 } 269 break; 270 case L2CAP_EVENT_CHANNEL_CLOSED: 271 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 272 if (stream_endpoint->l2cap_media_cid == local_cid){ 273 stream_endpoint->l2cap_media_cid = 0; 274 log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED: AVDTP_STREAM_ENDPOINT_IDLE"); 275 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_IDLE; 276 stream_endpoint->acceptor_config_state = AVDTP_ACCEPTOR_STREAM_CONFIG_IDLE; 277 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_STREAM_CONFIG_IDLE; 278 stream_endpoint->remote_sep_index = 0; 279 break; 280 } 281 if (stream_endpoint->l2cap_recovery_cid == local_cid){ 282 log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED recovery cid 0x%0x", local_cid); 283 stream_endpoint->l2cap_recovery_cid = 0; 284 break; 285 } 286 287 if (stream_endpoint->l2cap_reporting_cid == local_cid){ 288 log_info("L2CAP_EVENT_CHANNEL_CLOSED reporting cid 0x%0x", local_cid); 289 stream_endpoint->l2cap_reporting_cid = 0; 290 break; 291 } 292 break; 293 default: 294 break; 295 } 296 break; 297 default: 298 break; 299 } 300 } 301 302 static void avdtp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 303 UNUSED(packet_type); 304 UNUSED(channel); 305 UNUSED(size); 306 307 des_iterator_t des_list_it; 308 des_iterator_t prot_it; 309 uint16_t avdtp_l2cap_psm = 0; 310 uint16_t avdtp_version = 0; 311 // uint32_t avdtp_remote_uuid = 0; 312 313 if (!sdp_query_context.connection) return; 314 315 switch (hci_event_packet_get_type(packet)){ 316 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 317 // Handle new SDP record 318 if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) { 319 record_id = sdp_event_query_attribute_byte_get_record_id(packet); 320 // log_info("SDP Record: Nr: %d", record_id); 321 } 322 323 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 324 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 325 326 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 327 328 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 329 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 330 if (de_get_element_type(attribute_value) != DE_DES) break; 331 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 332 uint8_t * element = des_iterator_get_element(&des_list_it); 333 if (de_get_element_type(element) != DE_UUID) continue; 334 uint32_t uuid = de_get_uuid32(element); 335 switch (uuid){ 336 case BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE: 337 if (sdp_query_context.query_role != AVDTP_SOURCE) { 338 sdp_query_context.connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; 339 avdtp_signaling_emit_connection_established(sdp_query_context.avdtp_callback, sdp_query_context.connection->l2cap_signaling_cid, sdp_query_context.connection->remote_addr, 0); 340 break; 341 } 342 // log_info("SDP Attribute 0x%04x: AVDTP SOURCE protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid); 343 // avdtp_remote_uuid = uuid; 344 break; 345 case BLUETOOTH_SERVICE_CLASS_AUDIO_SINK: 346 if (sdp_query_context.query_role != AVDTP_SINK) { 347 sdp_query_context.connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; 348 avdtp_signaling_emit_connection_established(sdp_query_context.avdtp_callback, sdp_query_context.connection->l2cap_signaling_cid, sdp_query_context.connection->remote_addr, 0); 349 break; 350 } 351 // log_info("SDP Attribute 0x%04x: AVDTP SINK protocol UUID: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid); 352 // avdtp_remote_uuid = uuid; 353 break; 354 default: 355 break; 356 } 357 } 358 break; 359 360 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { 361 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 362 363 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 364 uint8_t *des_element; 365 uint8_t *element; 366 uint32_t uuid; 367 368 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 369 370 des_element = des_iterator_get_element(&des_list_it); 371 des_iterator_init(&prot_it, des_element); 372 element = des_iterator_get_element(&prot_it); 373 374 if (de_get_element_type(element) != DE_UUID) continue; 375 376 uuid = de_get_uuid32(element); 377 switch (uuid){ 378 case BLUETOOTH_PROTOCOL_L2CAP: 379 if (!des_iterator_has_more(&prot_it)) continue; 380 des_iterator_next(&prot_it); 381 de_element_get_uint16(des_iterator_get_element(&prot_it), &avdtp_l2cap_psm); 382 break; 383 case BLUETOOTH_PROTOCOL_AVDTP: 384 if (!des_iterator_has_more(&prot_it)) continue; 385 des_iterator_next(&prot_it); 386 de_element_get_uint16(des_iterator_get_element(&prot_it), &avdtp_version); 387 break; 388 default: 389 break; 390 } 391 } 392 if (!avdtp_l2cap_psm) { 393 sdp_query_context.connection->state = AVDTP_SIGNALING_CONNECTION_IDLE; 394 avdtp_signaling_emit_connection_established(sdp_query_context.avdtp_callback, sdp_query_context.connection->l2cap_signaling_cid, sdp_query_context.connection->remote_addr, L2CAP_SERVICE_DOES_NOT_EXIST); 395 break; 396 } 397 sdp_query_context.connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 398 l2cap_create_channel(sdp_query_context.packet_handler, sdp_query_context.connection->remote_addr, avdtp_l2cap_psm, l2cap_max_mtu(), NULL); 399 } 400 break; 401 default: 402 break; 403 } 404 } 405 } else { 406 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)); 407 } 408 break; 409 410 case SDP_EVENT_QUERY_COMPLETE: 411 log_info("General query done with status %d.", sdp_event_query_complete_get_status(packet)); 412 break; 413 } 414 } 415 416 417 void avdtp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avdtp_context_t * context){ 418 bd_addr_t event_addr; 419 hci_con_handle_t con_handle; 420 uint16_t psm; 421 uint16_t local_cid; 422 avdtp_stream_endpoint_t * stream_endpoint = NULL; 423 avdtp_connection_t * connection = NULL; 424 btstack_linked_list_t * avdtp_connections = &context->connections; 425 btstack_linked_list_t * stream_endpoints = &context->stream_endpoints; 426 handle_media_data = context->handle_media_data; 427 // log_info("avdtp_packet_handler packet type %02x, event %02x ", packet_type, hci_event_packet_get_type(packet)); 428 switch (packet_type) { 429 case L2CAP_DATA_PACKET: 430 connection = avdtp_connection_for_l2cap_signaling_cid(channel, context); 431 if (connection){ 432 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context); 433 break; 434 } 435 436 stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context); 437 if (!stream_endpoint){ 438 if (!connection) break; 439 handle_l2cap_data_packet_for_signaling_connection(connection, packet, size, context); 440 break; 441 } 442 443 if (channel == stream_endpoint->connection->l2cap_signaling_cid){ 444 stream_endpoint_state_machine(stream_endpoint->connection, stream_endpoint, L2CAP_DATA_PACKET, 0, packet, size, context); 445 break; 446 } 447 448 if (channel == stream_endpoint->l2cap_media_cid){ 449 (*handle_media_data)(stream_endpoint, packet, size); 450 break; 451 } 452 453 if (channel == stream_endpoint->l2cap_reporting_cid){ 454 // TODO 455 log_info("L2CAP_DATA_PACKET for reporting: NOT IMPLEMENTED"); 456 } else if (channel == stream_endpoint->l2cap_recovery_cid){ 457 // TODO 458 log_info("L2CAP_DATA_PACKET for recovery: NOT IMPLEMENTED"); 459 } else { 460 log_error("avdtp packet handler L2CAP_DATA_PACKET: local cid 0x%02x not found", channel); 461 } 462 break; 463 464 case HCI_EVENT_PACKET: 465 switch (hci_event_packet_get_type(packet)) { 466 case L2CAP_EVENT_INCOMING_CONNECTION: 467 l2cap_event_incoming_connection_get_address(packet, event_addr); 468 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 469 470 connection = avdtp_connection_for_bd_addr(event_addr, context); 471 472 if (!connection || connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED){ 473 connection = avdtp_create_connection(event_addr, context); 474 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 475 log_info("L2CAP_EVENT_INCOMING_CONNECTION, connection %p, state connection %d", connection, connection->state); 476 l2cap_accept_connection(local_cid); 477 break; 478 } 479 480 stream_endpoint = avdtp_stream_endpoint_for_seid(connection->query_seid, context); 481 if (!stream_endpoint) { 482 log_info("L2CAP_EVENT_INCOMING_CONNECTION no streamendpoint found for seid %d", connection->query_seid); 483 break; 484 } 485 486 if (stream_endpoint->l2cap_media_cid == 0){ 487 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_W4_L2CAP_FOR_MEDIA_CONNECTED) break; 488 l2cap_accept_connection(local_cid); 489 break; 490 } 491 break; 492 493 case L2CAP_EVENT_CHANNEL_OPENED: 494 // inform about new l2cap connection 495 l2cap_event_channel_opened_get_address(packet, event_addr); 496 if (l2cap_event_channel_opened_get_status(packet)){ 497 log_error("L2CAP connection to connection %s failed. status code 0x%02x", 498 bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet)); 499 break; 500 } 501 psm = l2cap_event_channel_opened_get_psm(packet); 502 if (psm != BLUETOOTH_PROTOCOL_AVDTP){ 503 log_error("unexpected PSM - Not implemented yet, avdtp sink: L2CAP_EVENT_CHANNEL_OPENED"); 504 return; 505 } 506 507 con_handle = l2cap_event_channel_opened_get_handle(packet); 508 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 509 510 log_info("L2CAP_EVENT_CHANNEL_OPENED: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x", 511 bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet)); 512 513 if (psm != BLUETOOTH_PROTOCOL_AVDTP) break; 514 515 connection = avdtp_connection_for_bd_addr(event_addr, context); 516 if (!connection) break; 517 518 if (connection->l2cap_signaling_cid == 0) { 519 if (connection->state != AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED) break; 520 connection->l2cap_signaling_cid = local_cid; 521 connection->con_handle = con_handle; 522 connection->query_seid = 0; 523 connection->state = AVDTP_SIGNALING_CONNECTION_OPENED; 524 log_info(" -> AVDTP_SIGNALING_CONNECTION_OPENED, connection %p", connection); 525 avdtp_signaling_emit_connection_established(context->avdtp_callback, connection->l2cap_signaling_cid, event_addr, 0); 526 break; 527 } 528 529 stream_endpoint = avdtp_stream_endpoint_for_seid(connection->query_seid, context); 530 if (!stream_endpoint){ 531 log_info("L2CAP_EVENT_CHANNEL_OPENED: stream_endpoint not found"); 532 return; 533 } 534 stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_OPENED, packet, size, context); 535 break; 536 537 case L2CAP_EVENT_CHANNEL_CLOSED: 538 // data: event (8), len(8), channel (16) 539 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 540 connection = avdtp_connection_for_l2cap_signaling_cid(local_cid, context); 541 log_info(" -> L2CAP_EVENT_CHANNEL_CLOSED signaling cid 0x%0x", local_cid); 542 543 if (connection){ 544 log_info(" -> AVDTP_STREAM_ENDPOINT_IDLE, connection closed"); 545 btstack_linked_list_remove(avdtp_connections, (btstack_linked_item_t*) connection); 546 btstack_linked_list_iterator_t it; 547 btstack_linked_list_iterator_init(&it, stream_endpoints); 548 while (btstack_linked_list_iterator_has_next(&it)){ 549 avdtp_stream_endpoint_t * _stream_endpoint = (avdtp_stream_endpoint_t *)btstack_linked_list_iterator_next(&it); 550 avdtp_initialize_stream_endpoint(_stream_endpoint); 551 } 552 break; 553 } 554 555 stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(local_cid, context); 556 if (!stream_endpoint) return; 557 558 stream_endpoint_state_machine(connection, stream_endpoint, HCI_EVENT_PACKET, L2CAP_EVENT_CHANNEL_CLOSED, packet, size, context); 559 break; 560 561 case HCI_EVENT_DISCONNECTION_COMPLETE: 562 break; 563 564 case L2CAP_EVENT_CAN_SEND_NOW: 565 connection = avdtp_connection_for_l2cap_signaling_cid(channel, context); 566 if (!connection) { 567 stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(channel, context); 568 if (!stream_endpoint->connection) break; 569 connection = stream_endpoint->connection; 570 } 571 avdtp_handle_can_send_now(connection, channel, context); 572 break; 573 default: 574 log_info("unknown HCI event type %02x", hci_event_packet_get_type(packet)); 575 break; 576 } 577 break; 578 579 default: 580 // other packet type 581 break; 582 } 583 } 584 585 void avdtp_disconnect(uint16_t avdtp_cid, avdtp_context_t * context){ 586 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 587 if (!connection) return; 588 if (connection->state == AVDTP_SIGNALING_CONNECTION_IDLE) return; 589 if (connection->state == AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED) return; 590 591 connection->disconnect = 1; 592 avdtp_request_can_send_now_self(connection, connection->l2cap_signaling_cid); 593 } 594 595 void avdtp_open_stream(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid, avdtp_context_t * context){ 596 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 597 if (!connection){ 598 log_error("avdtp_media_connect: no connection for signaling cid 0x%02x found", avdtp_cid); 599 return; 600 } 601 if (avdtp_find_remote_sep(connection, acp_seid) == 0xFF){ 602 log_error("avdtp_media_connect: no remote sep for seid %d found", acp_seid); 603 return; 604 } 605 606 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) { 607 log_error("avdtp_media_connect: wrong connection state %d", connection->state); 608 return; 609 } 610 611 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(int_seid, context); 612 if (!stream_endpoint) { 613 log_error("avdtp_media_connect: no stream_endpoint with seid %d found", int_seid); 614 return; 615 } 616 617 if (stream_endpoint->state < AVDTP_STREAM_ENDPOINT_CONFIGURED) return; 618 if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX) return; 619 620 connection->initiator_transaction_label++; 621 connection->acp_seid = acp_seid; 622 connection->int_seid = stream_endpoint->sep.seid; 623 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_OPEN_STREAM; 624 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_W2_REQUEST_OPEN_STREAM; 625 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 626 } 627 628 void avdtp_start_stream(uint8_t int_seid, avdtp_context_t * context){ 629 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(int_seid, context); 630 if (!stream_endpoint) { 631 log_error("avdtp_start_stream: no stream_endpoint with seid %d found", int_seid); 632 return; 633 } 634 avdtp_connection_t * connection = stream_endpoint->connection; 635 if (!connection){ 636 log_error("avdtp_start_stream: no connection for seid %d found",stream_endpoint->sep.seid); 637 return; 638 } 639 640 if (stream_endpoint->remote_sep_index == AVDTP_INVALID_SEP_INDEX || stream_endpoint->start_stream) return; 641 stream_endpoint->start_stream = 1; 642 connection->int_seid = int_seid; 643 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 644 } 645 646 void avdtp_stop_stream(uint8_t int_seid, avdtp_context_t * context){ 647 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(int_seid, context); 648 if (!stream_endpoint) { 649 log_error("avdtp_stop_stream: no stream_endpoint with seid %d found", int_seid); 650 return; 651 } 652 avdtp_connection_t * connection = stream_endpoint->connection; 653 if (!connection){ 654 log_error("avdtp_stop_stream: no connection for seid %d found",stream_endpoint->sep.seid); 655 return; 656 } 657 if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->stop_stream) return; 658 stream_endpoint->stop_stream = 1; 659 connection->int_seid = int_seid; 660 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 661 } 662 663 void avdtp_abort_stream(uint8_t int_seid, avdtp_context_t * context){ 664 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(int_seid, context); 665 if (!stream_endpoint) { 666 log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", int_seid); 667 return; 668 } 669 avdtp_connection_t * connection = stream_endpoint->connection; 670 671 if (!connection){ 672 log_error("avdtp_abort_stream: no connection for seid %d found",stream_endpoint->sep.seid); 673 return; 674 } 675 if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->abort_stream) return; 676 stream_endpoint->abort_stream = 1; 677 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 678 } 679 680 void avdtp_suspend_stream(uint8_t int_seid, avdtp_context_t * context){ 681 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_with_seid(int_seid, context); 682 if (!stream_endpoint) { 683 log_error("avdtp_abort_stream: no stream_endpoint with seid %d found", int_seid); 684 return; 685 } 686 avdtp_connection_t * connection = stream_endpoint->connection; 687 if (!connection){ 688 log_error("avdtp_abort_stream: no connection for seid %d found",stream_endpoint->sep.seid); 689 return; 690 } 691 if (stream_endpoint->remote_sep_index == 0xFF || stream_endpoint->suspend_stream) return; 692 stream_endpoint->suspend_stream = 1; 693 connection->int_seid = int_seid; 694 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 695 } 696 697 void avdtp_discover_stream_endpoints(uint16_t avdtp_cid, avdtp_context_t * context){ 698 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 699 if (!connection){ 700 log_error("avdtp_discover_stream_endpoints: no connection for signaling cid 0x%02x found", avdtp_cid); 701 return; 702 } 703 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 704 705 switch (connection->initiator_connection_state){ 706 case AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE: 707 connection->initiator_transaction_label++; 708 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_DISCOVER_SEPS; 709 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 710 break; 711 default: 712 log_error("avdtp_discover_stream_endpoints: wrong state"); 713 break; 714 } 715 } 716 717 718 void avdtp_get_capabilities(uint16_t avdtp_cid, uint8_t acp_seid, avdtp_context_t * context){ 719 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 720 if (!connection){ 721 log_error("avdtp_get_capabilities: no connection for signaling cid 0x%02x found", avdtp_cid); 722 return; 723 } 724 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 725 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return; 726 connection->initiator_transaction_label++; 727 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CAPABILITIES; 728 connection->acp_seid = acp_seid; 729 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 730 } 731 732 733 void avdtp_get_all_capabilities(uint16_t avdtp_cid, uint8_t acp_seid, avdtp_context_t * context){ 734 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 735 if (!connection){ 736 log_error("avdtp_get_all_capabilities: no connection for signaling cid 0x%02x found", avdtp_cid); 737 return; 738 } 739 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 740 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return; 741 connection->initiator_transaction_label++; 742 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_ALL_CAPABILITIES; 743 connection->acp_seid = acp_seid; 744 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 745 } 746 747 void avdtp_get_configuration(uint16_t avdtp_cid, uint8_t acp_seid, avdtp_context_t * context){ 748 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 749 if (!connection){ 750 log_error("avdtp_get_configuration: no connection for signaling cid 0x%02x found", avdtp_cid); 751 return; 752 } 753 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 754 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return; 755 connection->initiator_transaction_label++; 756 connection->initiator_connection_state = AVDTP_SIGNALING_CONNECTION_INITIATOR_W2_GET_CONFIGURATION; 757 connection->acp_seid = acp_seid; 758 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 759 } 760 761 void avdtp_set_configuration(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){ 762 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 763 if (!connection){ 764 log_error("avdtp_set_configuration: no connection for signaling cid 0x%02x found", avdtp_cid); 765 return; 766 } 767 if (connection->state != AVDTP_SIGNALING_CONNECTION_OPENED) return; 768 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return; 769 770 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, context); 771 if (!stream_endpoint) { 772 log_error("avdtp_set_configuration: no initiator stream endpoint for seid %d", int_seid); 773 return; 774 } 775 776 connection->initiator_transaction_label++; 777 connection->acp_seid = acp_seid; 778 connection->int_seid = int_seid; 779 stream_endpoint->remote_capabilities_bitmap = configured_services_bitmap; 780 stream_endpoint->remote_capabilities = configuration; 781 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_SET_CONFIGURATION; 782 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 783 } 784 785 void avdtp_reconfigure(uint16_t avdtp_cid, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context){ 786 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 787 if (!connection){ 788 log_error("avdtp_reconfigure: no connection for signaling cid 0x%02x found", avdtp_cid); 789 return; 790 } 791 //TODO: if opened only app capabilities, enable reconfigure for not opened 792 if (connection->state < AVDTP_SIGNALING_CONNECTION_OPENED) return; 793 if (connection->initiator_connection_state != AVDTP_SIGNALING_CONNECTION_INITIATOR_IDLE) return; 794 795 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, context); 796 if (!stream_endpoint) { 797 log_error("avdtp_reconfigure: no initiator stream endpoint for seid %d", int_seid); 798 return; 799 } 800 801 if (stream_endpoint->remote_sep_index == 0xFF){ 802 log_error("avdtp_reconfigure: no associated remote sep"); 803 return; 804 } 805 806 connection->initiator_transaction_label++; 807 connection->acp_seid = acp_seid; 808 connection->int_seid = stream_endpoint->sep.seid; 809 stream_endpoint->remote_capabilities_bitmap = configured_services_bitmap; 810 stream_endpoint->remote_capabilities = configuration; 811 stream_endpoint->initiator_config_state = AVDTP_INITIATOR_W2_RECONFIGURE_STREAM_WITH_SEID; 812 avdtp_request_can_send_now_initiator(connection, connection->l2cap_signaling_cid); 813 } 814 815 uint8_t avdtp_remote_seps_num(uint16_t avdtp_cid, avdtp_context_t * context){ 816 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 817 if (!connection){ 818 log_error("avdtp_suspend: no connection for signaling cid 0x%02x found", avdtp_cid); 819 return 0; 820 } 821 return connection->remote_seps_num; 822 } 823 824 avdtp_sep_t * avdtp_remote_sep(uint16_t avdtp_cid, uint8_t index, avdtp_context_t * context){ 825 avdtp_connection_t * connection = avdtp_connection_for_l2cap_signaling_cid(avdtp_cid, context); 826 if (!connection){ 827 log_error("avdtp_suspend: no connection for signaling cid 0x%02x found", avdtp_cid); 828 return NULL; 829 } 830 return &connection->remote_seps[index]; 831 } 832 833 void avdtp_initialize_sbc_configuration_storage(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * config_storage, uint16_t storage_size, uint8_t * packet, uint16_t packet_size){ 834 UNUSED(packet_size); 835 if (storage_size < 4) { 836 log_error("storage must have 4 bytes"); 837 return; 838 } 839 uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet)); 840 uint8_t channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet)); 841 uint8_t block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet)); 842 uint8_t subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet)); 843 844 uint8_t allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet)); 845 uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); 846 uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet)); 847 848 config_storage[0] = (sampling_frequency << 4) | channel_mode; 849 config_storage[1] = (block_length << 4) | (subbands << 2) | allocation_method; 850 config_storage[2] = min_bitpool_value; 851 config_storage[3] = max_bitpool_value; 852 853 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 854 stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO; 855 stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC; 856 stream_endpoint->remote_configuration.media_codec.media_codec_information_len = storage_size; 857 stream_endpoint->remote_configuration.media_codec.media_codec_information = config_storage; 858 } 859 860 uint8_t avdtp_choose_sbc_channel_mode(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_channel_mode_bitmap){ 861 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 862 uint8_t channel_mode_bitmap = (media_codec[0] & 0x0F) & remote_channel_mode_bitmap; 863 864 uint8_t channel_mode = AVDTP_SBC_STEREO; 865 if (channel_mode_bitmap & AVDTP_SBC_JOINT_STEREO){ 866 channel_mode = AVDTP_SBC_JOINT_STEREO; 867 } else if (channel_mode_bitmap & AVDTP_SBC_STEREO){ 868 channel_mode = AVDTP_SBC_STEREO; 869 } else if (channel_mode_bitmap & AVDTP_SBC_DUAL_CHANNEL){ 870 channel_mode = AVDTP_SBC_DUAL_CHANNEL; 871 } else if (channel_mode_bitmap & AVDTP_SBC_MONO){ 872 channel_mode = AVDTP_SBC_MONO; 873 } 874 return channel_mode; 875 } 876 877 uint8_t avdtp_choose_sbc_allocation_method(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_allocation_method_bitmap){ 878 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 879 uint8_t allocation_method_bitmap = (media_codec[1] & 0x03) & remote_allocation_method_bitmap; 880 881 uint8_t allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 882 if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS){ 883 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS; 884 } else if (allocation_method_bitmap & AVDTP_SBC_ALLOCATION_METHOD_SNR){ 885 allocation_method = AVDTP_SBC_ALLOCATION_METHOD_SNR; 886 } 887 return allocation_method; 888 } 889 890 uint8_t avdtp_stream_endpoint_seid(avdtp_stream_endpoint_t * stream_endpoint){ 891 if (!stream_endpoint) return 0; 892 return stream_endpoint->sep.seid; 893 } 894 uint8_t avdtp_choose_sbc_subbands(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_subbands_bitmap){ 895 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 896 uint8_t subbands_bitmap = ((media_codec[1] >> 2) & 0x03) & remote_subbands_bitmap; 897 898 uint8_t subbands = AVDTP_SBC_SUBBANDS_8; 899 if (subbands_bitmap & AVDTP_SBC_SUBBANDS_8){ 900 subbands = AVDTP_SBC_SUBBANDS_8; 901 } else if (subbands_bitmap & AVDTP_SBC_SUBBANDS_4){ 902 subbands = AVDTP_SBC_SUBBANDS_4; 903 } 904 return subbands; 905 } 906 907 uint8_t avdtp_choose_sbc_block_length(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_block_length_bitmap){ 908 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 909 uint8_t block_length_bitmap = (media_codec[1] >> 4) & remote_block_length_bitmap; 910 911 uint8_t block_length = AVDTP_SBC_BLOCK_LENGTH_16; 912 if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_16){ 913 block_length = AVDTP_SBC_BLOCK_LENGTH_16; 914 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_12){ 915 block_length = AVDTP_SBC_BLOCK_LENGTH_12; 916 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_8){ 917 block_length = AVDTP_SBC_BLOCK_LENGTH_8; 918 } else if (block_length_bitmap & AVDTP_SBC_BLOCK_LENGTH_4){ 919 block_length = AVDTP_SBC_BLOCK_LENGTH_4; 920 } 921 return block_length; 922 } 923 924 uint8_t avdtp_choose_sbc_sampling_frequency(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_sampling_frequency_bitmap){ 925 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 926 uint8_t sampling_frequency_bitmap = (media_codec[0] >> 4) & remote_sampling_frequency_bitmap; 927 928 uint8_t sampling_frequency = AVDTP_SBC_44100; 929 if (sampling_frequency_bitmap & AVDTP_SBC_48000){ 930 sampling_frequency = AVDTP_SBC_48000; 931 } else if (sampling_frequency_bitmap & AVDTP_SBC_44100){ 932 sampling_frequency = AVDTP_SBC_44100; 933 } else if (sampling_frequency_bitmap & AVDTP_SBC_32000){ 934 sampling_frequency = AVDTP_SBC_32000; 935 } else if (sampling_frequency_bitmap & AVDTP_SBC_16000){ 936 sampling_frequency = AVDTP_SBC_16000; 937 } 938 return sampling_frequency; 939 } 940 941 uint8_t avdtp_choose_sbc_max_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_max_bitpool_value){ 942 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 943 return btstack_min(media_codec[3], remote_max_bitpool_value); 944 } 945 946 uint8_t avdtp_choose_sbc_min_bitpool_value(avdtp_stream_endpoint_t * stream_endpoint, uint8_t remote_min_bitpool_value){ 947 uint8_t * media_codec = stream_endpoint->sep.capabilities.media_codec.media_codec_information; 948 return btstack_max(media_codec[2], remote_min_bitpool_value); 949 }