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