1 /* 2 * Copyright (C) 2022 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 BLUEKITCHEN 24 * GMBH 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 #include <stdint.h> 39 #include <string.h> 40 #include "a2dp.h" 41 #include "l2cap.h" 42 #include "classic/sdp_util.h" 43 #include "classic/avdtp_source.h" 44 #include "classic/a2dp_source.h" 45 #include "btstack_event.h" 46 #include "bluetooth_sdp.h" 47 #include "bluetooth_psm.h" 48 49 #define BTSTACK_FILE__ "a2dp.c" 50 51 #include <stddef.h> 52 #include "bluetooth.h" 53 #include "classic/a2dp.h" 54 #include "classic/avdtp_util.h" 55 #include "btstack_debug.h" 56 57 // ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG has been replaced by ENABLE_A2DP_EXPLICIT_CONFIG which is valid for both roles 58 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 59 #error "Please define ENABLE_A2DP_EXPLICIT_CONFIG instead of ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG" 60 #endif 61 62 #define AVDTP_MAX_SEP_NUM 10 63 #define A2DP_SET_CONFIG_DELAY_MS 200 64 65 static void a2dp_config_process_discover_seps_with_next_waiting_connection(void); 66 67 // higher layer callbacks 68 static btstack_packet_handler_t a2dp_source_callback; 69 static btstack_packet_handler_t a2dp_sink_callback; 70 71 // config process - singletons using sep_discovery_cid is used as mutex 72 static avdtp_role_t a2dp_config_process_role; 73 static uint16_t a2dp_config_process_sep_discovery_cid; 74 static uint16_t a2dp_config_process_sep_discovery_count; 75 static uint16_t a2dp_config_process_sep_discovery_index; 76 static avdtp_sep_t a2dp_config_process_sep_discovery_seps[AVDTP_MAX_SEP_NUM]; 77 static btstack_timer_source_t a2dp_config_process_set_config_timer; 78 static bool a2dp_config_process_set_config_timer_active; 79 80 void a2dp_init(void) { 81 } 82 83 void a2dp_deinit(void){ 84 a2dp_config_process_sep_discovery_cid = 0; 85 a2dp_source_callback = NULL; 86 a2dp_sink_callback = NULL; 87 } 88 89 void a2dp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_class_uuid, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 90 uint8_t* attribute; 91 de_create_sequence(service); 92 93 // 0x0000 "Service Record Handle" 94 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 95 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 96 97 // 0x0001 "Service Class ID List" 98 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 99 attribute = de_push_sequence(service); 100 { 101 de_add_number(attribute, DE_UUID, DE_SIZE_16, service_class_uuid); 102 } 103 de_pop_sequence(service, attribute); 104 105 // 0x0004 "Protocol Descriptor List" 106 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 107 attribute = de_push_sequence(service); 108 { 109 uint8_t* l2cpProtocol = de_push_sequence(attribute); 110 { 111 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 112 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP); 113 } 114 de_pop_sequence(attribute, l2cpProtocol); 115 116 uint8_t* avProtocol = de_push_sequence(attribute); 117 { 118 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 119 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 120 } 121 de_pop_sequence(attribute, avProtocol); 122 } 123 de_pop_sequence(service, attribute); 124 125 // 0x0005 "Public Browse Group" 126 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 127 attribute = de_push_sequence(service); 128 { 129 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 130 } 131 de_pop_sequence(service, attribute); 132 133 // 0x0009 "Bluetooth Profile Descriptor List" 134 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 135 attribute = de_push_sequence(service); 136 { 137 uint8_t *a2dProfile = de_push_sequence(attribute); 138 { 139 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION); 140 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 141 } 142 de_pop_sequence(attribute, a2dProfile); 143 } 144 de_pop_sequence(service, attribute); 145 146 147 // 0x0100 "Service Name" 148 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 149 de_add_data(service, DE_STRING, (uint16_t) strlen(service_name), (uint8_t *) service_name); 150 151 // 0x0100 "Provider Name" 152 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 153 de_add_data(service, DE_STRING, (uint16_t)strlen(service_provider_name), (uint8_t *) service_provider_name); 154 155 // 0x0311 "Supported Features" 156 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 157 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 158 } 159 160 uint8_t a2dp_subevent_id_for_avdtp_subevent_id(uint8_t subevent){ 161 switch (subevent){ 162 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 163 return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION; 164 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION: 165 return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION; 166 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION: 167 return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION; 168 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION: 169 return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION; 170 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 171 return A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION; 172 default: 173 btstack_unreachable(); 174 return 0; 175 } 176 } 177 178 static void a2dp_replace_subevent_id_and_emit(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size, uint8_t subevent_id){ 179 UNUSED(size); 180 btstack_assert(callback != NULL); 181 // cache orig event and subevent id 182 uint8_t orig_event_id = packet[0]; 183 uint8_t orig_subevent_id = packet[2]; 184 // execute callback 185 packet[0] = HCI_EVENT_A2DP_META; 186 packet[2] = subevent_id; 187 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 188 // restore id 189 packet[0] = orig_event_id; 190 packet[2] = orig_subevent_id; 191 } 192 193 void a2dp_register_source_packet_handler(btstack_packet_handler_t callback){ 194 btstack_assert(callback != NULL); 195 a2dp_source_callback = callback; 196 } 197 198 void a2dp_register_sink_packet_handler(btstack_packet_handler_t callback){ 199 btstack_assert(callback != NULL); 200 a2dp_sink_callback = callback; 201 } 202 203 void a2dp_replace_subevent_id_and_emit_source(uint8_t * packet, uint16_t size, uint8_t subevent_id) { 204 a2dp_replace_subevent_id_and_emit(a2dp_source_callback, packet, size, subevent_id); 205 } 206 207 void a2dp_replace_subevent_id_and_emit_sink(uint8_t *packet, uint16_t size, uint8_t subevent_id) { 208 a2dp_replace_subevent_id_and_emit(a2dp_sink_callback, packet, size, subevent_id); 209 } 210 211 static void a2dp_replace_subevent_id_and_emit_for_role(avdtp_role_t role, uint8_t * packet, uint16_t size, uint8_t subevent_id) { 212 if (role == AVDTP_ROLE_SOURCE){ 213 a2dp_replace_subevent_id_and_emit_source(packet, size, subevent_id); 214 } else { 215 a2dp_replace_subevent_id_and_emit_sink(packet, size, subevent_id); 216 } 217 } 218 219 static void a2dp_emit_role(avdtp_role_t role, uint8_t * packet, uint16_t size){ 220 if (role == AVDTP_ROLE_SOURCE){ 221 (*a2dp_source_callback)(HCI_EVENT_PACKET, 0, packet, size); 222 } else { 223 (*a2dp_sink_callback)(HCI_EVENT_PACKET, 0, packet, size); 224 } 225 } 226 227 static void a2dp_emit_stream_event_for_role(avdtp_role_t role, uint16_t cid, uint8_t local_seid, uint8_t subevent_id) { 228 uint8_t event[6]; 229 int pos = 0; 230 event[pos++] = HCI_EVENT_A2DP_META; 231 event[pos++] = sizeof(event) - 2; 232 event[pos++] = subevent_id; 233 little_endian_store_16(event, pos, cid); 234 pos += 2; 235 event[pos++] = local_seid; 236 a2dp_emit_role(role, event, sizeof(event)); 237 } 238 239 static void a2dp_emit_stream_reconfigured_role(avdtp_role_t role, uint16_t cid, uint8_t local_seid, uint8_t status){ 240 uint8_t event[7]; 241 int pos = 0; 242 event[pos++] = HCI_EVENT_A2DP_META; 243 event[pos++] = sizeof(event) - 2; 244 event[pos++] = A2DP_SUBEVENT_STREAM_RECONFIGURED; 245 little_endian_store_16(event, pos, cid); 246 pos += 2; 247 event[pos++] = local_seid; 248 event[pos++] = status; 249 a2dp_emit_role(role, event, sizeof(event)); 250 } 251 252 static void a2dp_emit_streaming_connection_failed_for_role(avdtp_role_t role, avdtp_connection_t *connection, uint8_t status) { 253 uint8_t event[14]; 254 int pos = 0; 255 event[pos++] = HCI_EVENT_A2DP_META; 256 event[pos++] = sizeof(event) - 2; 257 event[pos++] = A2DP_SUBEVENT_STREAM_ESTABLISHED; 258 little_endian_store_16(event, pos, connection->avdtp_cid); 259 pos += 2; 260 reverse_bd_addr(connection->remote_addr, &event[pos]); 261 pos += 6; 262 event[pos++] = 0; 263 event[pos++] = 0; 264 event[pos++] = status; 265 a2dp_emit_role(role, event, sizeof(event)); 266 } 267 268 static a2dp_config_process_t * a2dp_config_process_for_role(avdtp_role_t role, avdtp_connection_t *connection){ 269 return (role == AVDTP_ROLE_SOURCE) ? &connection->a2dp_source_config_process : &connection->a2dp_sink_config_process; 270 } 271 272 static void a2dp_config_process_timer_handler(btstack_timer_source_t * timer){ 273 uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 274 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 275 btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, NULL); 276 a2dp_config_process_set_config_timer_active = false; 277 278 log_info("Config timer fired, avdtp_cid 0x%02x", avdtp_cid); 279 280 if (connection == NULL) { 281 a2dp_config_process_discover_seps_with_next_waiting_connection(); 282 return; 283 } 284 a2dp_config_process_t * config_process = a2dp_config_process_for_role(a2dp_config_process_role, connection); 285 if (config_process->stream_endpoint_configured) { 286 a2dp_config_process_discover_seps_with_next_waiting_connection(); 287 return; 288 } 289 290 avdtp_discover_stream_endpoints(avdtp_cid); 291 } 292 293 static void a2dp_config_process_timer_start(uint16_t avdtp_cid){ 294 log_info("Config timer start for cid 0%02x", avdtp_cid); 295 a2dp_config_process_set_config_timer_active = true; 296 btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer); 297 btstack_run_loop_set_timer_handler(&a2dp_config_process_set_config_timer, a2dp_config_process_timer_handler); 298 btstack_run_loop_set_timer(&a2dp_config_process_set_config_timer, A2DP_SET_CONFIG_DELAY_MS); 299 btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, (void *)(uintptr_t)avdtp_cid); 300 btstack_run_loop_add_timer(&a2dp_config_process_set_config_timer); 301 } 302 303 static void a2dp_config_process_timer_restart(void){ 304 log_info("Config timer restart"); 305 btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer); 306 btstack_run_loop_set_timer(&a2dp_config_process_set_config_timer, A2DP_SET_CONFIG_DELAY_MS); 307 btstack_run_loop_add_timer(&a2dp_config_process_set_config_timer); 308 } 309 310 static void a2dp_config_process_timer_stop(void){ 311 if (a2dp_config_process_set_config_timer_active == false) return; 312 log_info("Config timer stop"); 313 btstack_run_loop_remove_timer(&a2dp_config_process_set_config_timer); 314 btstack_run_loop_set_timer_context(&a2dp_config_process_set_config_timer, NULL); 315 a2dp_config_process_set_config_timer_active = false; 316 } 317 318 // Discover seps, both incoming and outgoing 319 static void a2dp_config_process_start_discovering_seps(avdtp_role_t role, avdtp_connection_t *connection) { 320 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 321 322 config_process->state = A2DP_DISCOVER_SEPS; 323 config_process->discover_seps = false; 324 325 a2dp_config_process_role = role; 326 a2dp_config_process_sep_discovery_index = 0; 327 a2dp_config_process_sep_discovery_count = 0; 328 memset(a2dp_config_process_sep_discovery_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM); 329 a2dp_config_process_sep_discovery_cid = connection->avdtp_cid; 330 331 // if we initiated the connection, start config right away, else wait a bit to give remote a chance to do it first 332 if (config_process->outgoing_active){ 333 log_info("discover seps"); 334 avdtp_discover_stream_endpoints(connection->avdtp_cid); 335 } else { 336 log_info("wait a bit, then discover seps"); 337 a2dp_config_process_timer_start(connection->avdtp_cid); 338 } 339 } 340 341 static void a2dp_config_process_discover_seps_with_next_waiting_connection(void){ 342 btstack_assert(a2dp_config_process_sep_discovery_cid == 0); 343 btstack_linked_list_iterator_t it; 344 btstack_linked_list_iterator_init(&it, avdtp_get_connections()); 345 while (btstack_linked_list_iterator_has_next(&it)){ 346 avdtp_connection_t * next_connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 347 if (next_connection->a2dp_source_config_process.discover_seps) { 348 a2dp_config_process_start_discovering_seps(AVDTP_ROLE_SOURCE, next_connection); 349 } 350 if (next_connection->a2dp_sink_config_process.discover_seps) { 351 a2dp_config_process_start_discovering_seps(AVDTP_ROLE_SINK, next_connection); 352 } 353 } 354 } 355 356 void a2dp_config_process_ready_for_sep_discovery(avdtp_role_t role, avdtp_connection_t *connection) { 357 // start discover seps now if: 358 // - outgoing active: signaling for outgoing connection 359 // - outgoing not active: incoming connection and no sep discover ongoing 360 361 // sep discovery active? 362 if (a2dp_config_process_sep_discovery_cid == 0){ 363 a2dp_config_process_start_discovering_seps(role, connection); 364 } else { 365 // post-pone sep discovery 366 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 367 config_process->discover_seps = true; 368 } 369 } 370 371 static void a2dp_config_process_handle_media_configuration(avdtp_role_t role, const uint8_t *packet, uint8_t local_seid) { 372 uint16_t cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet); 373 avdtp_connection_t *connection = avdtp_get_connection_for_avdtp_cid(cid); 374 btstack_assert(connection != NULL); 375 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 376 377 config_process->local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 378 // bail out if local seid invalid 379 if (config_process->local_stream_endpoint == NULL) return; 380 381 // stop timer 382 if (a2dp_config_process_sep_discovery_cid == cid) { 383 a2dp_config_process_timer_stop(); 384 a2dp_config_process_sep_discovery_cid = 0; 385 } 386 387 config_process->stream_endpoint_configured = true; 388 389 switch (config_process->state) { 390 case A2DP_W4_SET_CONFIGURATION: 391 // outgoing: discovery and config of remote sink sep successful, trigger stream open 392 config_process->state = A2DP_W2_OPEN_STREAM_WITH_SEID; 393 break; 394 case A2DP_DISCOVER_SEPS: 395 case A2DP_GET_CAPABILITIES: 396 case A2DP_W2_GET_ALL_CAPABILITIES: 397 case A2DP_DISCOVERY_DONE: 398 case A2DP_W4_GET_CONFIGURATION: 399 // incoming: wait for stream open 400 config_process->state = A2DP_W4_OPEN_STREAM_WITH_SEID; 401 break; 402 default: 403 // wait for configuration after sending reconfigure - keep state 404 break; 405 } 406 } 407 408 void a2dp_config_process_set_config(avdtp_role_t role, avdtp_connection_t *connection) { 409 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 410 uint8_t local_seid = avdtp_stream_endpoint_seid(config_process->local_stream_endpoint); 411 uint8_t remote_seid = config_process->local_stream_endpoint->set_config_remote_seid; 412 log_info("A2DP initiate set configuration locally and wait for response ... local seid 0x%02x, remote seid 0x%02x", 413 local_seid, remote_seid); 414 config_process->state = A2DP_W4_SET_CONFIGURATION; 415 avdtp_set_configuration(connection->avdtp_cid, 416 local_seid, 417 remote_seid, 418 config_process->local_stream_endpoint->remote_configuration_bitmap, 419 config_process->local_stream_endpoint->remote_configuration); 420 } 421 422 static void 423 a2dp_config_process_handle_media_capability(avdtp_role_t role, uint16_t cid, uint8_t a2dp_subevent_id, uint8_t *packet, uint16_t size) { 424 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(cid); 425 btstack_assert(connection != NULL); 426 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 427 if (config_process->state != A2DP_GET_CAPABILITIES) return; 428 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, a2dp_subevent_id); 429 } 430 431 uint8_t a2dp_config_process_config_init(avdtp_role_t role, avdtp_connection_t *connection, uint8_t local_seid, uint8_t remote_seid, 432 avdtp_media_codec_type_t codec_type) { 433 434 // check state 435 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 436 switch (config_process->state){ 437 case A2DP_DISCOVERY_DONE: 438 case A2DP_GET_CAPABILITIES: 439 break; 440 default: 441 return ERROR_CODE_COMMAND_DISALLOWED; 442 } 443 444 // lookup local stream endpoint 445 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 446 if (stream_endpoint == NULL){ 447 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 448 } 449 450 // lookup remote stream endpoint 451 avdtp_sep_t * remote_sep = NULL; 452 uint8_t i; 453 for (i=0; i < a2dp_config_process_sep_discovery_count; i++){ 454 if (a2dp_config_process_sep_discovery_seps[i].seid == remote_seid){ 455 remote_sep = &a2dp_config_process_sep_discovery_seps[i]; 456 } 457 } 458 if (remote_sep == NULL){ 459 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 460 } 461 462 // set media configuration 463 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 464 stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO; 465 stream_endpoint->remote_configuration.media_codec.media_codec_type = codec_type; 466 // remote seid to use 467 stream_endpoint->set_config_remote_seid = remote_seid; 468 // enable delay reporting if supported 469 if (remote_sep->registered_service_categories & (1<<AVDTP_DELAY_REPORTING)){ 470 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_DELAY_REPORTING, 1); 471 } 472 473 // suitable stream endpoint found, configure it 474 config_process->local_stream_endpoint = stream_endpoint; 475 config_process->have_config = true; 476 477 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 478 if (config_process->state == A2DP_DISCOVERY_DONE){ 479 config_process->state = A2DP_SET_CONFIGURATION; 480 } 481 #endif 482 483 return ERROR_CODE_SUCCESS; 484 } 485 void a2dp_config_process_avdtp_event_handler(avdtp_role_t role, uint8_t *packet, uint16_t size) { 486 uint16_t cid; 487 avdtp_connection_t * connection; 488 a2dp_config_process_t * config_process; 489 uint8_t signal_identifier; 490 uint8_t status; 491 uint8_t local_seid; 492 uint8_t remote_seid; 493 494 switch (hci_event_avdtp_meta_get_subevent_code(packet)){ 495 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 496 cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 497 connection = avdtp_get_connection_for_avdtp_cid(cid); 498 btstack_assert(connection != NULL); 499 config_process = a2dp_config_process_for_role(role, connection); 500 501 status = avdtp_subevent_signaling_connection_established_get_status(packet); 502 if (status != ERROR_CODE_SUCCESS){ 503 // notify about connection error only if we're initiator 504 if (config_process->outgoing_active){ 505 log_info("A2DP signaling connection failed status 0x%02x", status); 506 config_process->outgoing_active = false; 507 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 508 // also emit streaming connection failed 509 a2dp_emit_streaming_connection_failed_for_role(role, connection, status); 510 } 511 break; 512 } 513 log_info("A2DP signaling connection established avdtp_cid 0x%02x", cid); 514 config_process->state = A2DP_CONNECTED; 515 516 // notify app 517 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 518 519 // Windows 10 as Source starts SEP discovery after 1500 ms, but only if it did not get a Discover command 520 // If BTstack is configured for both roles, we need to avoid sending Discover command in Source Role for outgoing Sink connections 521 522 // For this, we trigger SEP discovery if: 523 // a) this is an outgoing connection 524 // b) this connection wasn't caused by an outgoing connection of the other role 525 if (role == AVDTP_ROLE_SOURCE){ 526 if (connection->a2dp_source_config_process.outgoing_active || !connection->a2dp_sink_config_process.outgoing_active){ 527 a2dp_config_process_ready_for_sep_discovery(AVDTP_ROLE_SOURCE, connection); 528 } 529 } else { 530 if (connection->a2dp_sink_config_process.outgoing_active || !connection->a2dp_source_config_process.outgoing_active){ 531 a2dp_config_process_ready_for_sep_discovery(AVDTP_ROLE_SINK, connection); 532 } 533 } 534 break; 535 536 case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND: 537 cid = avdtp_subevent_signaling_sep_found_get_avdtp_cid(packet); 538 connection = avdtp_get_connection_for_avdtp_cid(cid); 539 btstack_assert(connection != NULL); 540 config_process = a2dp_config_process_for_role(role, connection); 541 542 if (config_process->state == A2DP_DISCOVER_SEPS) { 543 avdtp_sep_t sep; 544 memset(&sep, 0, sizeof(avdtp_sep_t)); 545 sep.seid = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);; 546 sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet); 547 sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet); 548 sep.type = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet); 549 log_info("A2DP Found sep: remote seid 0x%02x, in_use %d, media type %d, sep type %s, index %d", 550 sep.seid, sep.in_use, sep.media_type, sep.type == AVDTP_SOURCE ? "source" : "sink", 551 a2dp_config_process_sep_discovery_count); 552 avdtp_sep_type_t matching_type = (role == AVDTP_ROLE_SOURCE) ? AVDTP_SINK : AVDTP_SOURCE; 553 if ((sep.type == matching_type) && (sep.in_use == false)) { 554 a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_count++] = sep; 555 } 556 } 557 break; 558 559 case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE: 560 cid = avdtp_subevent_signaling_sep_dicovery_done_get_avdtp_cid(packet); 561 connection = avdtp_get_connection_for_avdtp_cid(cid); 562 btstack_assert(connection != NULL); 563 config_process = a2dp_config_process_for_role(role, connection); 564 565 if (config_process->state != A2DP_DISCOVER_SEPS) break; 566 567 if (a2dp_config_process_sep_discovery_count > 0){ 568 config_process->state = A2DP_GET_CAPABILITIES; 569 a2dp_config_process_sep_discovery_index = 0; 570 config_process->have_config = false; 571 } else { 572 if (config_process->outgoing_active){ 573 config_process->outgoing_active = false; 574 connection = avdtp_get_connection_for_avdtp_cid(cid); 575 btstack_assert(connection != NULL); 576 a2dp_emit_streaming_connection_failed_for_role(role, connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 577 } 578 579 // continue 580 config_process->state = A2DP_CONNECTED; 581 a2dp_config_process_sep_discovery_cid = 0; 582 a2dp_config_process_discover_seps_with_next_waiting_connection(); 583 } 584 break; 585 586 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY: 587 cid = avdtp_subevent_signaling_media_codec_sbc_capability_get_avdtp_cid(packet); 588 connection = avdtp_get_connection_for_avdtp_cid(cid); 589 btstack_assert(connection != NULL); 590 config_process = a2dp_config_process_for_role(role, connection); 591 592 if (config_process->state != A2DP_GET_CAPABILITIES) break; 593 594 // forward codec capability 595 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY); 596 597 #ifndef ENABLE_A2DP_EXPLICIT_CONFIG 598 // select SEP if none configured yet 599 if (config_process->have_config == false){ 600 // find SBC stream endpoint 601 avdtp_sep_type_t required_sep_type = (role == AVDTP_ROLE_SOURCE) ? AVDTP_SOURCE : AVDTP_SINK; 602 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_source_stream_endpoint_for_media_codec_and_type(AVDTP_CODEC_SBC, required_sep_type); 603 if (stream_endpoint != NULL){ 604 // choose SBC config params 605 avdtp_configuration_sbc_t configuration; 606 configuration.sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet)); 607 configuration.channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet)); 608 configuration.block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet)); 609 configuration.subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet)); 610 configuration.allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet)); 611 configuration.max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); 612 configuration.min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet)); 613 614 // and pre-select this endpoint 615 local_seid = avdtp_stream_endpoint_seid(stream_endpoint); 616 remote_seid = avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet); 617 a2dp_config_process_set_sbc(role, cid, local_seid, remote_seid, &configuration); 618 } 619 } 620 #endif 621 break; 622 // forward codec capability 623 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY: 624 cid = avdtp_subevent_signaling_media_codec_mpeg_audio_capability_get_avdtp_cid(packet); 625 a2dp_config_process_handle_media_capability(role, cid, 626 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY, 627 packet, size); 628 break; 629 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY: 630 cid = avdtp_subevent_signaling_media_codec_mpeg_aac_capability_get_avdtp_cid(packet); 631 a2dp_config_process_handle_media_capability(role, cid, 632 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY, 633 packet, size); 634 break; 635 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY: 636 cid = avdtp_subevent_signaling_media_codec_atrac_capability_get_avdtp_cid(packet); 637 a2dp_config_process_handle_media_capability(role, cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY, 638 packet, 639 size); 640 break; 641 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY: 642 cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet); 643 a2dp_config_process_handle_media_capability(role, cid, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY, 644 packet, 645 size); 646 break; 647 648 // not forwarded 649 case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY: 650 case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY: 651 case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY: 652 case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY: 653 case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY: 654 case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY: 655 break; 656 657 case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY: 658 cid = avdtp_subevent_signaling_delay_reporting_capability_get_avdtp_cid(packet); 659 connection = avdtp_get_connection_for_avdtp_cid(cid); 660 btstack_assert(connection != NULL); 661 config_process = a2dp_config_process_for_role(role, connection); 662 log_info("received AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, cid 0x%02x, state %d", cid, config_process->state); 663 664 if (config_process->state != A2DP_GET_CAPABILITIES) break; 665 666 // store delay reporting capability 667 a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_index].registered_service_categories |= 1 << AVDTP_DELAY_REPORTING; 668 669 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY); 670 break; 671 672 case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE: 673 cid = avdtp_subevent_signaling_capabilities_done_get_avdtp_cid(packet); 674 connection = avdtp_get_connection_for_avdtp_cid(cid); 675 btstack_assert(connection != NULL); 676 config_process = a2dp_config_process_for_role(role, connection); 677 678 if (config_process->state != A2DP_GET_CAPABILITIES) break; 679 680 // forward capabilities done for endpoint 681 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE); 682 683 // endpoint was not suitable, check next one if possible 684 a2dp_config_process_sep_discovery_index++; 685 686 if (a2dp_config_process_sep_discovery_index >= a2dp_config_process_sep_discovery_count){ 687 688 // emit 'all capabilities for all seps reported' 689 uint8_t event[6]; 690 uint8_t pos = 0; 691 event[pos++] = HCI_EVENT_A2DP_META; 692 event[pos++] = sizeof(event) - 2; 693 event[pos++] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_COMPLETE; 694 little_endian_store_16(event, pos, cid); 695 a2dp_emit_role(role, event, sizeof(event)); 696 697 // do we have a valid config? 698 if (config_process->have_config){ 699 config_process->state = A2DP_SET_CONFIGURATION; 700 config_process->have_config = false; 701 break; 702 } 703 704 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 705 config_process->state = A2DP_DISCOVERY_DONE; 706 // TODO call a2dp_discover_seps_with_next_waiting_connection? 707 break; 708 #endif 709 710 // we didn't find a suitable SBC stream endpoint, sorry. 711 if (config_process->outgoing_active){ 712 config_process->outgoing_active = false; 713 connection = avdtp_get_connection_for_avdtp_cid(cid); 714 btstack_assert(connection != NULL); 715 a2dp_emit_streaming_connection_failed_for_role(role, connection, 716 ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 717 } 718 config_process->state = A2DP_CONNECTED; 719 a2dp_config_process_sep_discovery_cid = 0; 720 a2dp_config_process_discover_seps_with_next_waiting_connection(); 721 } 722 break; 723 724 // forward codec configuration 725 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 726 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 727 a2dp_config_process_handle_media_configuration(role, packet, local_seid); 728 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, 729 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION); 730 break; 731 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION: 732 local_seid = avdtp_subevent_signaling_media_codec_mpeg_audio_configuration_get_local_seid(packet); 733 a2dp_config_process_handle_media_configuration(role, packet, local_seid); 734 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, 735 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION); 736 break; 737 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION: 738 local_seid = avdtp_subevent_signaling_media_codec_mpeg_aac_configuration_get_local_seid(packet); 739 a2dp_config_process_handle_media_configuration(role, packet, local_seid); 740 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, 741 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION); 742 break; 743 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION: 744 local_seid = avdtp_subevent_signaling_media_codec_atrac_configuration_get_local_seid(packet); 745 a2dp_config_process_handle_media_configuration(role, packet, local_seid); 746 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, 747 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION); 748 break; 749 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 750 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 751 a2dp_config_process_handle_media_configuration(role, packet, local_seid); 752 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, 753 A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION); 754 break; 755 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 756 cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); 757 connection = avdtp_get_connection_for_avdtp_cid(cid); 758 btstack_assert(connection != NULL); 759 config_process = a2dp_config_process_for_role(role, connection); 760 761 if (config_process->state != A2DP_W4_OPEN_STREAM_WITH_SEID) break; 762 763 config_process->outgoing_active = false; 764 status = avdtp_subevent_streaming_connection_established_get_status(packet); 765 if (status != ERROR_CODE_SUCCESS){ 766 log_info("A2DP source streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status); 767 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 768 break; 769 } 770 771 log_info("A2DP source streaming connection established --- avdtp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x", cid, 772 avdtp_subevent_streaming_connection_established_get_local_seid(packet), 773 avdtp_subevent_streaming_connection_established_get_remote_seid(packet)); 774 config_process->state = A2DP_STREAMING_OPENED; 775 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 776 break; 777 778 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 779 cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet); 780 connection = avdtp_get_connection_for_avdtp_cid(cid); 781 btstack_assert(connection != NULL); 782 config_process = a2dp_config_process_for_role(role, connection); 783 784 // restart set config timer while remote is active for current cid 785 if (a2dp_config_process_set_config_timer_active && 786 (avdtp_subevent_signaling_accept_get_is_initiator(packet) == 0) && 787 (cid == a2dp_config_process_sep_discovery_cid)){ 788 789 a2dp_config_process_timer_restart(); 790 break; 791 } 792 793 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 794 795 log_info("A2DP cmd %s accepted, global state %d, cid 0x%02x", avdtp_si2str(signal_identifier), config_process->state, cid); 796 797 switch (config_process->state){ 798 case A2DP_GET_CAPABILITIES: 799 remote_seid = a2dp_config_process_sep_discovery_seps[a2dp_config_process_sep_discovery_index].seid; 800 log_info("A2DP get capabilities for remote seid 0x%02x", remote_seid); 801 avdtp_get_all_capabilities(cid, remote_seid, role); 802 return; 803 804 case A2DP_SET_CONFIGURATION: 805 a2dp_config_process_set_config(role, connection); 806 return; 807 808 case A2DP_W2_OPEN_STREAM_WITH_SEID: 809 log_info("A2DP open stream ... local seid 0x%02x, active remote seid 0x%02x", 810 avdtp_stream_endpoint_seid(connection->a2dp_source_config_process.local_stream_endpoint), 811 config_process->local_stream_endpoint->remote_sep.seid); 812 config_process->state = A2DP_W4_OPEN_STREAM_WITH_SEID; 813 avdtp_open_stream(cid, 814 avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 815 config_process->local_stream_endpoint->remote_sep.seid); 816 break; 817 818 case A2DP_W2_RECONFIGURE_WITH_SEID: 819 log_info("A2DP reconfigured ... local seid 0x%02x, active remote seid 0x%02x", 820 avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 821 config_process->local_stream_endpoint->remote_sep.seid); 822 a2dp_emit_stream_reconfigured_role(role, cid, avdtp_stream_endpoint_seid( 823 config_process->local_stream_endpoint), ERROR_CODE_SUCCESS); 824 config_process->state = A2DP_STREAMING_OPENED; 825 break; 826 827 case A2DP_STREAMING_OPENED: 828 switch (signal_identifier){ 829 case AVDTP_SI_START: 830 a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 831 A2DP_SUBEVENT_STREAM_STARTED); 832 break; 833 case AVDTP_SI_SUSPEND: 834 a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 835 A2DP_SUBEVENT_STREAM_SUSPENDED); 836 break; 837 case AVDTP_SI_ABORT: 838 case AVDTP_SI_CLOSE: 839 a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 840 A2DP_SUBEVENT_STREAM_STOPPED); 841 break; 842 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION 843 case AVDTP_SI_ACCEPT_START: 844 a2dp_emit_stream_event_for_role(role, cid, avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 845 A2DP_SUBEVENT_START_STREAM_REQUESTED); 846 break; 847 #endif 848 default: 849 break; 850 } 851 break; 852 853 default: 854 break; 855 } 856 break; 857 858 case AVDTP_SUBEVENT_SIGNALING_REJECT: 859 cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet); 860 connection = avdtp_get_connection_for_avdtp_cid(cid); 861 btstack_assert(connection != NULL); 862 config_process = a2dp_config_process_for_role(role, connection); 863 864 if (avdtp_subevent_signaling_reject_get_is_initiator(packet) == 0) break; 865 866 switch (config_process->state) { 867 case A2DP_W2_RECONFIGURE_WITH_SEID: 868 log_info("A2DP reconfigure failed ... local seid 0x%02x, active remote seid 0x%02x", 869 avdtp_stream_endpoint_seid(config_process->local_stream_endpoint), 870 config_process->local_stream_endpoint->remote_sep.seid); 871 a2dp_emit_stream_reconfigured_role(role, cid, avdtp_stream_endpoint_seid( 872 config_process->local_stream_endpoint), ERROR_CODE_UNSPECIFIED_ERROR); 873 config_process->state = A2DP_STREAMING_OPENED; 874 break; 875 default: 876 config_process->state = A2DP_CONNECTED; 877 break; 878 } 879 880 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 881 break; 882 883 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 884 cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet); 885 connection = avdtp_get_connection_for_avdtp_cid(cid); 886 btstack_assert(connection != NULL); 887 config_process = a2dp_config_process_for_role(role, connection); 888 889 if (avdtp_subevent_signaling_general_reject_get_is_initiator(packet) == 0) break; 890 891 config_process->state = A2DP_CONNECTED; 892 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 893 break; 894 895 case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: 896 cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet); 897 connection = avdtp_get_connection_for_avdtp_cid(cid); 898 btstack_assert(connection != NULL); 899 config_process = a2dp_config_process_for_role(role, connection); 900 901 config_process->state = A2DP_CONFIGURED; 902 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_STREAM_RELEASED); 903 break; 904 905 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 906 cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet); 907 connection = avdtp_get_connection_for_avdtp_cid(cid); 908 btstack_assert(connection != NULL); 909 config_process = a2dp_config_process_for_role(role, connection); 910 911 // connect/release are passed on to app 912 if (a2dp_config_process_sep_discovery_cid == cid){ 913 a2dp_config_process_timer_stop(); 914 config_process->stream_endpoint_configured = false; 915 config_process->local_stream_endpoint = NULL; 916 917 config_process->state = A2DP_IDLE; 918 a2dp_config_process_sep_discovery_cid = 0; 919 } 920 a2dp_replace_subevent_id_and_emit_for_role(role, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED); 921 break; 922 923 default: 924 break; 925 } 926 } 927 928 uint8_t a2dp_config_process_set_sbc(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){ 929 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 930 if (connection == NULL){ 931 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 932 } 933 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 934 935 uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_SBC); 936 if (status != 0) { 937 return status; 938 } 939 // set config in reserved buffer 940 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info; 941 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 942 avdtp_config_sbc_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 943 944 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 945 a2dp_config_process_set_config(role, connection); 946 #endif 947 948 return ERROR_CODE_SUCCESS; 949 } 950 951 uint8_t a2dp_config_process_set_mpeg_audio(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){ 952 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 953 if (connection == NULL){ 954 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 955 } 956 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 957 958 uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_1_2_AUDIO); 959 if (status != 0) { 960 return status; 961 } 962 963 // set config in reserved buffer 964 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *)config_process->local_stream_endpoint->media_codec_info; 965 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 966 avdtp_config_mpeg_audio_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 967 968 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 969 a2dp_config_process_set_config(role, connection); 970 #endif 971 972 return ERROR_CODE_SUCCESS; 973 } 974 975 uint8_t a2dp_config_process_set_mpeg_aac(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){ 976 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 977 if (connection == NULL){ 978 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 979 } 980 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 981 982 uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_2_4_AAC); 983 if (status != 0) { 984 return status; 985 } 986 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info; 987 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 6; 988 avdtp_config_mpeg_aac_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 989 990 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 991 a2dp_config_process_set_config(role, connection); 992 #endif 993 994 return ERROR_CODE_SUCCESS; 995 } 996 997 uint8_t a2dp_config_process_set_atrac(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){ 998 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 999 if (connection == NULL){ 1000 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1001 } 1002 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 1003 1004 uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_ATRAC_FAMILY); 1005 if (status != 0) { 1006 return status; 1007 } 1008 1009 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) config_process->local_stream_endpoint->media_codec_info; 1010 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 7; 1011 avdtp_config_atrac_store(config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 1012 1013 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 1014 a2dp_config_process_set_config(role, connection); 1015 #endif 1016 1017 return ERROR_CODE_SUCCESS; 1018 } 1019 1020 uint8_t a2dp_config_process_set_other(avdtp_role_t role, uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, 1021 const uint8_t * media_codec_information, uint8_t media_codec_information_len){ 1022 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 1023 if (connection == NULL){ 1024 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1025 } 1026 a2dp_config_process_t * config_process = a2dp_config_process_for_role(role, connection); 1027 1028 uint8_t status = a2dp_config_process_config_init(role, connection, local_seid, remote_seid, AVDTP_CODEC_NON_A2DP); 1029 if (status != 0) { 1030 return status; 1031 } 1032 1033 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) media_codec_information; 1034 config_process->local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_information_len; 1035 1036 #ifdef ENABLE_A2DP_EXPLICIT_CONFIG 1037 a2dp_config_process_set_config(role, connection); 1038 #endif 1039 1040 return status; 1041 } 1042