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