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