1 2 /* 3 * Copyright (C) 2016 BlueKitchen GmbH 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the copyright holders nor the names of 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 4. Any redistribution, use, or modification is done solely for 18 * personal benefit and not for any commercial purpose or for 19 * monetary gain. 20 * 21 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 25 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Please inquire about commercial licensing options at 35 * [email protected] 36 * 37 */ 38 39 /** 40 * Supported use cases: 41 * - single incoming connection: sep discovery starts and stream will get setup if remote sink sep with SBC is found 42 * - single outgoing connection: see above 43 * - outgoing and incoming connection to same device: 44 * - if outgoing is triggered first, incoming will get ignored. 45 * - if incoming starts first, start ougoing will fail, but incoming will succeed. 46 * - outgoing and incoming connections to different devices: 47 * - if outgoing is first, incoming gets ignored. 48 * - if incoming starts first SEP discovery will get stopped and outgoing will succeed. 49 */ 50 51 #define BTSTACK_FILE__ "a2dp_source.c" 52 53 #include <stdint.h> 54 #include <string.h> 55 56 #include "bluetooth_psm.h" 57 #include "bluetooth_sdp.h" 58 #include "btstack_debug.h" 59 #include "btstack_event.h" 60 #include "classic/a2dp_source.h" 61 #include "classic/avdtp_source.h" 62 #include "classic/avdtp_util.h" 63 #include "classic/sdp_util.h" 64 #include "l2cap.h" 65 66 #define AVDTP_MAX_SEP_NUM 10 67 #define A2DP_SET_CONFIG_DELAY_MS 150 68 69 static const char * default_a2dp_source_service_name = "BTstack A2DP Source Service"; 70 static const char * default_a2dp_source_service_provider_name = "BTstack A2DP Source Service Provider"; 71 72 static btstack_packet_handler_t a2dp_source_packet_handler_user; 73 static uint8_t (*a2dp_source_media_config_validator)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size); 74 75 // config process - singletons using sep_discovery_cid is used as mutex 76 static uint16_t sep_discovery_cid; 77 static uint16_t sep_discovery_count; 78 static uint16_t sep_discovery_index; 79 static avdtp_sep_t sep_discovery_seps[AVDTP_MAX_SEP_NUM]; 80 static btstack_timer_source_t a2dp_source_set_config_timer; 81 82 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 83 static void a2dp_discover_seps_with_next_waiting_connection(void); 84 85 static void a2dp_source_streaming_emit_connection_failed(avdtp_connection_t *connection, uint8_t status) { 86 uint8_t event[14]; 87 int pos = 0; 88 event[pos++] = HCI_EVENT_A2DP_META; 89 event[pos++] = sizeof(event) - 2; 90 event[pos++] = A2DP_SUBEVENT_STREAM_ESTABLISHED; 91 little_endian_store_16(event, pos, connection->avdtp_cid); 92 pos += 2; 93 reverse_bd_addr(connection->remote_addr, &event[pos]); 94 pos += 6; 95 event[pos++] = 0; 96 event[pos++] = 0; 97 event[pos++] = status; 98 99 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 100 } 101 102 void a2dp_source_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 103 uint8_t* attribute; 104 de_create_sequence(service); 105 106 // 0x0000 "Service Record Handle" 107 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 108 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 109 110 // 0x0001 "Service Class ID List" 111 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 112 attribute = de_push_sequence(service); 113 { 114 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE); 115 } 116 de_pop_sequence(service, attribute); 117 118 // 0x0004 "Protocol Descriptor List" 119 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 120 attribute = de_push_sequence(service); 121 { 122 uint8_t* l2cpProtocol = de_push_sequence(attribute); 123 { 124 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 125 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP); 126 } 127 de_pop_sequence(attribute, l2cpProtocol); 128 129 uint8_t* avProtocol = de_push_sequence(attribute); 130 { 131 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 132 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 133 } 134 de_pop_sequence(attribute, avProtocol); 135 } 136 de_pop_sequence(service, attribute); 137 138 // 0x0005 "Public Browse Group" 139 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 140 attribute = de_push_sequence(service); 141 { 142 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 143 } 144 de_pop_sequence(service, attribute); 145 146 // 0x0009 "Bluetooth Profile Descriptor List" 147 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 148 attribute = de_push_sequence(service); 149 { 150 uint8_t *a2dProfile = de_push_sequence(attribute); 151 { 152 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION); 153 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 154 } 155 de_pop_sequence(attribute, a2dProfile); 156 } 157 de_pop_sequence(service, attribute); 158 159 160 // 0x0100 "Service Name" 161 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 162 if (service_name){ 163 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 164 } else { 165 de_add_data(service, DE_STRING, strlen(default_a2dp_source_service_name), (uint8_t *) default_a2dp_source_service_name); 166 } 167 168 // 0x0100 "Provider Name" 169 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 170 if (service_provider_name){ 171 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 172 } else { 173 de_add_data(service, DE_STRING, strlen(default_a2dp_source_service_provider_name), (uint8_t *) default_a2dp_source_service_provider_name); 174 } 175 176 // 0x0311 "Supported Features" 177 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 178 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 179 } 180 181 static void a2dp_signaling_emit_reconfigured(uint16_t cid, uint8_t local_seid, uint8_t status){ 182 uint8_t event[7]; 183 int pos = 0; 184 event[pos++] = HCI_EVENT_A2DP_META; 185 event[pos++] = sizeof(event) - 2; 186 event[pos++] = A2DP_SUBEVENT_STREAM_RECONFIGURED; 187 little_endian_store_16(event, pos, cid); 188 pos += 2; 189 event[pos++] = local_seid; 190 event[pos++] = status; 191 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 192 } 193 194 static void a2dp_source_set_config_timer_handler(btstack_timer_source_t * timer){ 195 uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 196 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 197 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL); 198 199 if (connection == NULL) { 200 log_info("a2dp_discover_seps_with_next_waiting_connection"); 201 a2dp_discover_seps_with_next_waiting_connection(); 202 return; 203 } 204 205 if (connection->a2dp_source_stream_endpoint_configured) return; 206 avdtp_source_discover_stream_endpoints(avdtp_cid); 207 } 208 209 static void a2dp_source_set_config_timer_start(uint16_t avdtp_cid){ 210 log_info("a2dp_source_set_config_timer_start cid 0%02x", avdtp_cid); 211 btstack_run_loop_remove_timer(&a2dp_source_set_config_timer); 212 btstack_run_loop_set_timer_handler(&a2dp_source_set_config_timer,a2dp_source_set_config_timer_handler); 213 btstack_run_loop_set_timer(&a2dp_source_set_config_timer, A2DP_SET_CONFIG_DELAY_MS); 214 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, (void *)(uintptr_t)avdtp_cid); 215 btstack_run_loop_add_timer(&a2dp_source_set_config_timer); 216 } 217 218 static void a2dp_source_set_config_timer_stop(void){ 219 log_info("a2dp_source_set_config_timer_stop"); 220 btstack_run_loop_remove_timer(&a2dp_source_set_config_timer); 221 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL); 222 } 223 224 // Discover seps, both incoming and outgoing 225 static void a2dp_start_discovering_seps(avdtp_connection_t * connection){ 226 connection->a2dp_source_state = A2DP_DISCOVER_SEPS; 227 connection->a2dp_source_discover_seps = false; 228 229 sep_discovery_index = 0; 230 sep_discovery_count = 0; 231 memset(sep_discovery_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM); 232 sep_discovery_cid = connection->avdtp_cid; 233 234 // if we initiated the connection, start config right away, else wait a bit to give remote a chance to do it first 235 if (connection->a2dp_source_outgoing_active){ 236 log_info("discover seps"); 237 avdtp_source_discover_stream_endpoints(connection->avdtp_cid); 238 } else { 239 log_info("wait a bit, then discover seps"); 240 a2dp_source_set_config_timer_start(connection->avdtp_cid); 241 } 242 } 243 244 static void a2dp_discover_seps_with_next_waiting_connection(void){ 245 btstack_assert(sep_discovery_cid == 0); 246 btstack_linked_list_iterator_t it; 247 btstack_linked_list_iterator_init(&it, avdtp_get_connections()); 248 while (btstack_linked_list_iterator_has_next(&it)){ 249 avdtp_connection_t * next_connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 250 if (!next_connection->a2dp_source_discover_seps) continue; 251 a2dp_start_discovering_seps(next_connection); 252 } 253 } 254 255 static void a2dp_source_ready_for_sep_discovery(avdtp_connection_t * connection){ 256 // start discover seps now if: 257 // - outgoing active: signaling for outgoing connection 258 // - outgoing not active: incoming connection and no sep discover ongoing 259 260 // sep discovery active? 261 if (sep_discovery_cid == 0){ 262 a2dp_start_discovering_seps(connection); 263 } else { 264 // post-pone sep discovery 265 connection->a2dp_source_discover_seps = true; 266 } 267 } 268 269 static void a2dp_handle_received_configuration(const uint8_t *packet, uint8_t local_seid) { 270 uint16_t cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet); 271 avdtp_connection_t *avdtp_connection = avdtp_get_connection_for_avdtp_cid(cid); 272 btstack_assert(avdtp_connection != NULL); 273 avdtp_connection->a2dp_source_local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 274 // bail out if local seid invalid 275 if (!avdtp_connection->a2dp_source_local_stream_endpoint) return; 276 277 // stop timer 278 if (sep_discovery_cid == cid) { 279 a2dp_source_set_config_timer_stop(); 280 sep_discovery_cid = 0; 281 } 282 283 avdtp_connection->a2dp_source_stream_endpoint_configured = true; 284 285 switch (avdtp_connection->a2dp_source_state) { 286 case A2DP_W4_SET_CONFIGURATION: 287 // outgoing: discovery and config of remote sink sep successful, trigger stream open 288 avdtp_connection->a2dp_source_state = A2DP_W2_OPEN_STREAM_WITH_SEID; 289 break; 290 case A2DP_DISCOVER_SEPS: 291 case A2DP_GET_CAPABILITIES: 292 case A2DP_W2_GET_ALL_CAPABILITIES: 293 case A2DP_DISCOVERY_DONE: 294 case A2DP_W4_GET_CONFIGURATION: 295 // incoming: wait for stream open 296 avdtp_connection->a2dp_source_state = A2DP_W4_OPEN_STREAM_WITH_SEID; 297 break; 298 default: 299 // wait for configuration after sending reconfigure - keep state 300 break; 301 } 302 } 303 304 static void a2dp_source_set_config(avdtp_connection_t * connection){ 305 uint8_t remote_seid = connection->a2dp_source_local_stream_endpoint->set_config_remote_seid; 306 log_info("A2DP initiate set configuration locally and wait for response ... local seid 0x%02x, remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), remote_seid); 307 connection->a2dp_source_state = A2DP_W4_SET_CONFIGURATION; 308 avdtp_source_set_configuration(connection->avdtp_cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), remote_seid, connection->a2dp_source_local_stream_endpoint->remote_configuration_bitmap, connection->a2dp_source_local_stream_endpoint->remote_configuration); 309 } 310 311 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 312 UNUSED(channel); 313 UNUSED(size); 314 315 uint16_t cid; 316 avdtp_connection_t * connection; 317 318 uint8_t signal_identifier; 319 uint8_t status; 320 uint8_t local_seid; 321 uint8_t remote_seid; 322 323 if (packet_type != HCI_EVENT_PACKET) return; 324 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return; 325 326 switch (packet[2]){ 327 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 328 cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 329 connection = avdtp_get_connection_for_avdtp_cid(cid); 330 btstack_assert(connection != NULL); 331 332 status = avdtp_subevent_signaling_connection_established_get_status(packet); 333 if (status != ERROR_CODE_SUCCESS){ 334 // notify about connection error only if we're initiator 335 if (connection->a2dp_source_outgoing_active){ 336 log_info("A2DP source signaling connection failed status 0x%02x", status); 337 connection->a2dp_source_outgoing_active = false; 338 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 339 } 340 break; 341 } 342 log_info("A2DP source signaling connection established avdtp_cid 0x%02x", cid); 343 connection->a2dp_source_state = A2DP_CONNECTED; 344 345 // notify app 346 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 347 348 // continue 349 a2dp_source_ready_for_sep_discovery(connection); 350 break; 351 352 case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND: 353 cid = avdtp_subevent_signaling_sep_found_get_avdtp_cid(packet); 354 connection = avdtp_get_connection_for_avdtp_cid(cid); 355 btstack_assert(connection != NULL); 356 357 if (connection->a2dp_source_state == A2DP_DISCOVER_SEPS) { 358 avdtp_sep_t sep; 359 memset(&sep, 0, sizeof(avdtp_sep_t)); 360 sep.seid = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);; 361 sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet); 362 sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet); 363 sep.type = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet); 364 log_info("A2DP Found sep: remote seid 0x%02x, in_use %d, media type %d, sep type %s, index %d", 365 sep.seid, sep.in_use, sep.media_type, sep.type == AVDTP_SOURCE ? "source" : "sink", 366 sep_discovery_count); 367 if ((sep.type == AVDTP_SINK) && (sep.in_use == false)) { 368 sep_discovery_seps[sep_discovery_count++] = sep; 369 } 370 } 371 break; 372 373 case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE: 374 cid = avdtp_subevent_signaling_sep_dicovery_done_get_avdtp_cid(packet); 375 connection = avdtp_get_connection_for_avdtp_cid(cid); 376 btstack_assert(connection != NULL); 377 378 if (connection->a2dp_source_state != A2DP_DISCOVER_SEPS) break; 379 380 if (sep_discovery_count > 0){ 381 connection->a2dp_source_state = A2DP_GET_CAPABILITIES; 382 sep_discovery_index = 0; 383 connection->a2dp_source_have_config = false; 384 } else { 385 if (connection->a2dp_source_outgoing_active){ 386 connection->a2dp_source_outgoing_active = false; 387 connection = avdtp_get_connection_for_avdtp_cid(cid); 388 btstack_assert(connection != NULL); 389 a2dp_source_streaming_emit_connection_failed(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 390 } 391 392 // continue 393 connection->a2dp_source_state = A2DP_CONNECTED; 394 sep_discovery_cid = 0; 395 a2dp_discover_seps_with_next_waiting_connection(); 396 } 397 break; 398 399 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY: 400 cid = avdtp_subevent_signaling_media_codec_sbc_capability_get_avdtp_cid(packet); 401 connection = avdtp_get_connection_for_avdtp_cid(cid); 402 btstack_assert(connection != NULL); 403 404 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 405 406 // forward codec capability 407 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY); 408 409 #ifndef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 410 // select SEP if none configured yet 411 if (connection->a2dp_source_have_config == false){ 412 // find SBC stream endpoint 413 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_source_stream_endpoint_for_media_codec(AVDTP_CODEC_SBC); 414 if (stream_endpoint != NULL){ 415 // choose SBC config params 416 avdtp_configuration_sbc_t configuration; 417 configuration.sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet)); 418 configuration.channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet)); 419 configuration.block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet)); 420 configuration.subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet)); 421 configuration.allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet)); 422 configuration.max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); 423 configuration.min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet)); 424 425 // and pre-select this endpoint 426 local_seid = avdtp_stream_endpoint_seid(stream_endpoint); 427 remote_seid = avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet); 428 a2dp_source_set_config_sbc(cid, local_seid, remote_seid, &configuration); 429 } 430 } 431 #endif 432 break; 433 434 // forward codec capability 435 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY: 436 cid = avdtp_subevent_signaling_media_codec_mpeg_audio_capability_get_avdtp_cid(packet); 437 connection = avdtp_get_connection_for_avdtp_cid(cid); 438 btstack_assert(connection != NULL); 439 440 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 441 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY); 442 break; 443 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY: 444 cid = avdtp_subevent_signaling_media_codec_mpeg_aac_capability_get_avdtp_cid(packet); 445 connection = avdtp_get_connection_for_avdtp_cid(cid); 446 btstack_assert(connection != NULL); 447 448 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 449 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY); 450 break; 451 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY: 452 cid = avdtp_subevent_signaling_media_codec_atrac_capability_get_avdtp_cid(packet); 453 connection = avdtp_get_connection_for_avdtp_cid(cid); 454 btstack_assert(connection != NULL); 455 456 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 457 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY); 458 break; 459 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY: 460 cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet); 461 connection = avdtp_get_connection_for_avdtp_cid(cid); 462 btstack_assert(connection != NULL); 463 464 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 465 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY); 466 break; 467 468 // not forwarded 469 case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY: 470 case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY: 471 case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY: 472 case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY: 473 case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY: 474 case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY: 475 break; 476 477 case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY: 478 cid = avdtp_subevent_signaling_delay_reporting_capability_get_avdtp_cid(packet); 479 connection = avdtp_get_connection_for_avdtp_cid(cid); 480 btstack_assert(connection != NULL); 481 log_info("received AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, cid 0x%02x, state %d", cid, connection->a2dp_source_state); 482 483 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 484 485 // store delay reporting capability 486 sep_discovery_seps[sep_discovery_index].registered_service_categories |= 1 << AVDTP_DELAY_REPORTING; 487 488 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY); 489 break; 490 491 case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE: 492 cid = avdtp_subevent_signaling_capabilities_done_get_avdtp_cid(packet); 493 connection = avdtp_get_connection_for_avdtp_cid(cid); 494 btstack_assert(connection != NULL); 495 496 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 497 498 // forward capabilities done for endpoint 499 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE); 500 501 // endpoint was not suitable, check next one 502 sep_discovery_index++; 503 if (sep_discovery_index >= sep_discovery_count){ 504 505 // emit 'all capabilities for all seps reported' 506 uint8_t event[6]; 507 uint8_t pos = 0; 508 event[pos++] = HCI_EVENT_A2DP_META; 509 event[pos++] = sizeof(event) - 2; 510 event[pos++] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_COMPLETE; 511 little_endian_store_16(event, pos, cid); 512 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 513 514 // do we have a valid config? 515 if (connection->a2dp_source_have_config){ 516 connection->a2dp_source_state = A2DP_SET_CONFIGURATION; 517 connection->a2dp_source_have_config = false; 518 break; 519 } 520 521 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 522 connection->a2dp_source_state = A2DP_DISCOVERY_DONE; 523 // TODO call a2dp_discover_seps_with_next_waiting_connection? 524 break; 525 #else 526 // we didn't find a suitable SBC stream endpoint, sorry. 527 if (connection->a2dp_source_outgoing_active){ 528 connection->a2dp_source_outgoing_active = false; 529 connection = avdtp_get_connection_for_avdtp_cid(cid); 530 btstack_assert(connection != NULL); 531 a2dp_source_streaming_emit_connection_failed(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 532 } 533 connection->a2dp_source_state = A2DP_CONNECTED; 534 sep_discovery_cid = 0; 535 a2dp_discover_seps_with_next_waiting_connection(); 536 #endif 537 } 538 break; 539 540 case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT: 541 cid = avdtp_subevent_signaling_delay_report_get_avdtp_cid(packet); 542 connection = avdtp_get_connection_for_avdtp_cid(cid); 543 btstack_assert(connection != NULL); 544 545 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORT); 546 break; 547 548 // forward codec configuration 549 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 550 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 551 a2dp_handle_received_configuration(packet, local_seid); 552 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION); 553 break; 554 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION: 555 local_seid = avdtp_subevent_signaling_media_codec_mpeg_audio_configuration_get_local_seid(packet); 556 a2dp_handle_received_configuration(packet, local_seid); 557 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION); 558 break; 559 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION: 560 local_seid = avdtp_subevent_signaling_media_codec_mpeg_aac_configuration_get_local_seid(packet); 561 a2dp_handle_received_configuration(packet, local_seid); 562 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION); 563 break; 564 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION: 565 local_seid = avdtp_subevent_signaling_media_codec_atrac_configuration_get_local_seid(packet); 566 a2dp_handle_received_configuration(packet, local_seid); 567 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION); 568 break; 569 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 570 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 571 a2dp_handle_received_configuration(packet, local_seid); 572 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION); 573 break; 574 575 case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW: 576 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW); 577 break; 578 579 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 580 cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); 581 connection = avdtp_get_connection_for_avdtp_cid(cid); 582 btstack_assert(connection != NULL); 583 584 if (connection->a2dp_source_state != A2DP_W4_OPEN_STREAM_WITH_SEID) break; 585 586 connection->a2dp_source_outgoing_active = false; 587 status = avdtp_subevent_streaming_connection_established_get_status(packet); 588 if (status != ERROR_CODE_SUCCESS){ 589 log_info("A2DP source streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status); 590 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 591 break; 592 } 593 594 log_info("A2DP source streaming connection established --- avdtp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x", cid, 595 avdtp_subevent_streaming_connection_established_get_local_seid(packet), 596 avdtp_subevent_streaming_connection_established_get_remote_seid(packet)); 597 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 598 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 599 break; 600 601 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 602 cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet); 603 connection = avdtp_get_connection_for_avdtp_cid(cid); 604 btstack_assert(connection != NULL); 605 606 // reset discovery timer while remote is active for current cid 607 if ((avdtp_subevent_signaling_accept_get_is_initiator(packet) == 0) && (cid == sep_discovery_cid)){ 608 log_info("Reset discovery timer"); 609 a2dp_source_set_config_timer_start(sep_discovery_cid); 610 break; 611 } 612 613 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 614 615 log_info("A2DP cmd %s accepted, global state %d, cid 0x%02x", avdtp_si2str(signal_identifier), connection->a2dp_source_state, cid); 616 617 switch (connection->a2dp_source_state){ 618 case A2DP_GET_CAPABILITIES: 619 remote_seid = sep_discovery_seps[sep_discovery_index].seid; 620 log_info("A2DP get capabilities for remote seid 0x%02x", remote_seid); 621 avdtp_source_get_all_capabilities(cid, remote_seid); 622 return; 623 624 case A2DP_SET_CONFIGURATION: 625 a2dp_source_set_config(connection); 626 return; 627 628 case A2DP_W2_OPEN_STREAM_WITH_SEID: 629 log_info("A2DP open stream ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 630 connection->a2dp_source_state = A2DP_W4_OPEN_STREAM_WITH_SEID; 631 avdtp_source_open_stream(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 632 break; 633 634 case A2DP_W2_RECONFIGURE_WITH_SEID: 635 log_info("A2DP reconfigured ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 636 a2dp_signaling_emit_reconfigured(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), ERROR_CODE_SUCCESS); 637 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 638 break; 639 640 case A2DP_STREAMING_OPENED: 641 switch (signal_identifier){ 642 case AVDTP_SI_START: 643 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_STARTED); 644 break; 645 case AVDTP_SI_SUSPEND: 646 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_SUSPENDED); 647 break; 648 case AVDTP_SI_ABORT: 649 case AVDTP_SI_CLOSE: 650 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_STOPPED); 651 break; 652 default: 653 break; 654 } 655 break; 656 657 default: 658 break; 659 } 660 break; 661 662 case AVDTP_SUBEVENT_SIGNALING_REJECT: 663 cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet); 664 connection = avdtp_get_connection_for_avdtp_cid(cid); 665 btstack_assert(connection != NULL); 666 667 if (avdtp_subevent_signaling_reject_get_is_initiator(packet) == 0) break; 668 669 switch (connection->a2dp_source_state) { 670 case A2DP_W2_RECONFIGURE_WITH_SEID: 671 log_info("A2DP reconfigure failed ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 672 a2dp_signaling_emit_reconfigured(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), ERROR_CODE_UNSPECIFIED_ERROR); 673 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 674 break; 675 default: 676 connection->a2dp_source_state = A2DP_CONNECTED; 677 break; 678 } 679 680 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 681 break; 682 683 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 684 cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet); 685 connection = avdtp_get_connection_for_avdtp_cid(cid); 686 btstack_assert(connection != NULL); 687 688 if (avdtp_subevent_signaling_general_reject_get_is_initiator(packet) == 0) break; 689 690 connection->a2dp_source_state = A2DP_CONNECTED; 691 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 692 break; 693 694 case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: 695 cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet); 696 connection = avdtp_get_connection_for_avdtp_cid(cid); 697 btstack_assert(connection != NULL); 698 699 connection->a2dp_source_state = A2DP_CONFIGURED; 700 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_RELEASED); 701 break; 702 703 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 704 cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet); 705 connection = avdtp_get_connection_for_avdtp_cid(cid); 706 btstack_assert(connection != NULL); 707 708 // connect/release are passed on to app 709 if (sep_discovery_cid == cid){ 710 a2dp_source_set_config_timer_stop(); 711 connection->a2dp_source_stream_endpoint_configured = false; 712 connection->a2dp_source_local_stream_endpoint = NULL; 713 714 connection->a2dp_source_state = A2DP_IDLE; 715 sep_discovery_cid = 0; 716 } 717 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED); 718 break; 719 720 default: 721 break; 722 } 723 } 724 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){ 725 btstack_assert(callback != NULL); 726 727 avdtp_source_register_packet_handler(&a2dp_source_packet_handler_internal); 728 a2dp_source_packet_handler_user = callback; 729 } 730 731 void a2dp_source_init(void){ 732 avdtp_source_init(); 733 } 734 735 void a2dp_source_deinit(void){ 736 avdtp_source_deinit(); 737 sep_discovery_count = 0; 738 } 739 740 avdtp_stream_endpoint_t * a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, 741 const uint8_t *codec_capabilities, uint16_t codec_capabilities_len, 742 uint8_t * codec_configuration, uint16_t codec_configuration_len){ 743 avdtp_stream_endpoint_t * stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type); 744 if (!stream_endpoint){ 745 return NULL; 746 } 747 avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(stream_endpoint)); 748 avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(stream_endpoint), media_type, media_codec_type, 749 codec_capabilities, codec_capabilities_len); 750 avdtp_source_register_delay_reporting_category(avdtp_stream_endpoint_seid(stream_endpoint)); 751 752 // store user codec configuration buffer 753 stream_endpoint->media_codec_type = media_codec_type; 754 stream_endpoint->media_codec_configuration_info = codec_configuration; 755 stream_endpoint->media_codec_configuration_len = codec_configuration_len; 756 757 return stream_endpoint; 758 } 759 760 void a2dp_source_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 761 avdtp_source_finalize_stream_endpoint(stream_endpoint); 762 } 763 764 uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint16_t *avdtp_cid) { 765 766 uint16_t outgoing_cid; 767 768 avdtp_connection_t * connection = avdtp_get_connection_for_bd_addr(remote_addr); 769 if (connection == NULL){ 770 uint8_t status = avdtp_source_connect(remote_addr, &outgoing_cid); 771 if (status != ERROR_CODE_SUCCESS) { 772 // if there's already a connection for for remote addr, avdtp_source_connect fails, 773 // but the stream will get set-up nevertheless 774 return status; 775 } 776 connection = avdtp_get_connection_for_avdtp_cid(outgoing_cid); 777 btstack_assert(connection != NULL); 778 779 // setup state 780 connection->a2dp_source_outgoing_active = true; 781 connection->a2dp_source_state = A2DP_W4_CONNECTED; 782 *avdtp_cid = outgoing_cid; 783 784 } else { 785 if (connection->a2dp_source_outgoing_active || connection->a2dp_source_stream_endpoint_configured) { 786 return ERROR_CODE_COMMAND_DISALLOWED; 787 } 788 789 // check state 790 switch (connection->a2dp_source_state){ 791 case A2DP_IDLE: 792 case A2DP_CONNECTED: 793 // restart process e.g. if there no suitable stream endpoints or they had been in use 794 connection->a2dp_source_outgoing_active = true; 795 *avdtp_cid = connection->avdtp_cid; 796 a2dp_source_ready_for_sep_discovery(connection); 797 break; 798 default: 799 return ERROR_CODE_COMMAND_DISALLOWED; 800 } 801 } 802 return ERROR_CODE_SUCCESS; 803 } 804 805 uint8_t a2dp_source_disconnect(uint16_t avdtp_cid){ 806 return avdtp_disconnect(avdtp_cid); 807 } 808 809 810 uint8_t a2dp_source_start_stream(uint16_t avdtp_cid, uint8_t local_seid){ 811 return avdtp_start_stream(avdtp_cid, local_seid); 812 } 813 814 uint8_t a2dp_source_pause_stream(uint16_t avdtp_cid, uint8_t local_seid){ 815 return avdtp_suspend_stream(avdtp_cid, local_seid); 816 } 817 818 void a2dp_source_stream_endpoint_request_can_send_now(uint16_t avdtp_cid, uint8_t local_seid){ 819 avdtp_source_stream_endpoint_request_can_send_now(avdtp_cid, local_seid); 820 } 821 822 int a2dp_max_media_payload_size(uint16_t avdtp_cid, uint8_t local_seid){ 823 return avdtp_max_media_payload_size(avdtp_cid, local_seid); 824 } 825 826 int a2dp_source_stream_send_media_payload(uint16_t avdtp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){ 827 return avdtp_source_stream_send_media_payload(avdtp_cid, local_seid, storage, num_bytes_to_copy, num_frames, marker); 828 } 829 830 uint8_t a2dp_source_stream_send_media_payload_rtp(uint16_t a2dp_cid, uint8_t local_seid, uint8_t marker, uint8_t * payload, uint16_t payload_size){ 831 return avdtp_source_stream_send_media_payload_rtp(a2dp_cid, local_seid, marker, payload, payload_size); 832 } 833 834 uint8_t a2dp_source_stream_send_media_packet(uint16_t a2dp_cid, uint8_t local_seid, const uint8_t * packet, uint16_t size){ 835 return avdtp_source_stream_send_media_packet(a2dp_cid, local_seid, packet, size); 836 } 837 838 static uint8_t a2dp_source_config_init(avdtp_connection_t *connection, uint8_t local_seid, uint8_t remote_seid, 839 avdtp_media_codec_type_t codec_type) { 840 841 // check state 842 switch (connection->a2dp_source_state){ 843 case A2DP_DISCOVERY_DONE: 844 case A2DP_GET_CAPABILITIES: 845 break; 846 default: 847 return ERROR_CODE_COMMAND_DISALLOWED; 848 } 849 850 // lookup local stream endpoint 851 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 852 if (stream_endpoint == NULL){ 853 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 854 } 855 856 // lookup remote stream endpoint 857 avdtp_sep_t * remote_sep = NULL; 858 uint8_t i; 859 for (i=0; i < sep_discovery_count; i++){ 860 if (sep_discovery_seps[i].seid == remote_seid){ 861 remote_sep = &sep_discovery_seps[i]; 862 } 863 } 864 if (remote_sep == NULL){ 865 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 866 } 867 868 // set media configuration 869 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 870 stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO; 871 stream_endpoint->remote_configuration.media_codec.media_codec_type = codec_type; 872 // remote seid to use 873 stream_endpoint->set_config_remote_seid = remote_seid; 874 // enable delay reporting if supported 875 if (remote_sep->registered_service_categories & (1<<AVDTP_DELAY_REPORTING)){ 876 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_DELAY_REPORTING, 1); 877 } 878 879 // suitable Sink stream endpoint found, configure it 880 connection->a2dp_source_local_stream_endpoint = stream_endpoint; 881 connection->a2dp_source_have_config = true; 882 883 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 884 // continue outgoing configuration 885 if (connection->a2dp_source_state == A2DP_DISCOVERY_DONE){ 886 connection->a2dp_source_state = A2DP_SET_CONFIGURATION; 887 } 888 #endif 889 890 return ERROR_CODE_SUCCESS; 891 } 892 893 uint8_t a2dp_source_set_config_sbc(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){ 894 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 895 if (connection == NULL){ 896 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 897 } 898 899 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_SBC); 900 if (status != 0) { 901 return status; 902 } 903 // set config in reserved buffer 904 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 905 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 906 avdtp_config_sbc_store(connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 907 908 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 909 a2dp_source_set_config(connection); 910 #endif 911 912 return ERROR_CODE_SUCCESS; 913 } 914 915 uint8_t a2dp_source_set_config_mpeg_audio(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){ 916 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 917 if (connection == NULL){ 918 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 919 } 920 921 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_1_2_AUDIO); 922 if (status != 0) { 923 return status; 924 } 925 926 // set config in reserved buffer 927 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *)connection->a2dp_source_local_stream_endpoint->media_codec_info; 928 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 929 avdtp_config_mpeg_audio_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 930 931 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 932 a2dp_source_set_config(connection); 933 #endif 934 935 return ERROR_CODE_SUCCESS; 936 } 937 938 uint8_t a2dp_source_set_config_mpeg_aac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){ 939 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 940 if (connection == NULL){ 941 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 942 } 943 944 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_2_4_AAC); 945 if (status != 0) { 946 return status; 947 } 948 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 949 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 6; 950 avdtp_config_mpeg_aac_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 951 952 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 953 a2dp_source_set_config(connection); 954 #endif 955 956 return ERROR_CODE_SUCCESS; 957 } 958 959 uint8_t a2dp_source_set_config_atrac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){ 960 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 961 if (connection == NULL){ 962 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 963 } 964 965 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_ATRAC_FAMILY); 966 if (status != 0) { 967 return status; 968 } 969 970 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 971 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 7; 972 avdtp_config_atrac_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 973 974 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 975 a2dp_source_set_config(connection); 976 #endif 977 978 return ERROR_CODE_SUCCESS; 979 } 980 981 uint8_t a2dp_source_set_config_other(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, 982 const uint8_t * media_codec_information, uint8_t media_codec_information_len){ 983 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 984 if (connection == NULL){ 985 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 986 } 987 988 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_NON_A2DP); 989 if (status != 0) { 990 return status; 991 } 992 993 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) media_codec_information; 994 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_information_len; 995 996 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 997 a2dp_source_set_config(connection); 998 #endif 999 1000 return status; 1001 } 1002 1003 uint8_t a2dp_source_reconfigure_stream_sampling_frequency(uint16_t avdtp_cid, uint32_t sampling_frequency){ 1004 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1005 if (connection == NULL){ 1006 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1007 } 1008 1009 if (connection->a2dp_source_state != A2DP_STREAMING_OPENED) { 1010 return ERROR_CODE_COMMAND_DISALLOWED; 1011 } 1012 1013 btstack_assert(connection->a2dp_source_local_stream_endpoint != NULL); 1014 1015 log_info("Reconfigure avdtp_cid 0x%02x", avdtp_cid); 1016 1017 avdtp_media_codec_type_t codec_type = connection->a2dp_source_local_stream_endpoint->sep.capabilities.media_codec.media_codec_type; 1018 uint8_t codec_info_len; 1019 switch (codec_type){ 1020 case AVDTP_CODEC_SBC: 1021 codec_info_len = 4; 1022 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1023 avdtp_config_sbc_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1024 break; 1025 case AVDTP_CODEC_MPEG_1_2_AUDIO: 1026 codec_info_len = 4; 1027 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1028 avdtp_config_mpeg_audio_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1029 break; 1030 case AVDTP_CODEC_MPEG_2_4_AAC: 1031 codec_info_len = 6; 1032 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1033 avdtp_config_mpeg_aac_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1034 break; 1035 case AVDTP_CODEC_ATRAC_FAMILY: 1036 codec_info_len = 7; 1037 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1038 avdtp_config_atrac_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1039 break; 1040 default: 1041 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1042 } 1043 1044 avdtp_capabilities_t new_configuration; 1045 new_configuration.media_codec.media_type = AVDTP_AUDIO; 1046 new_configuration.media_codec.media_codec_type = codec_type; 1047 new_configuration.media_codec.media_codec_information_len = codec_info_len; 1048 new_configuration.media_codec.media_codec_information = connection->a2dp_source_local_stream_endpoint->media_codec_info; 1049 1050 // start reconfigure 1051 connection->a2dp_source_state = A2DP_W2_RECONFIGURE_WITH_SEID; 1052 1053 return avdtp_source_reconfigure( 1054 avdtp_cid, 1055 avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), 1056 connection->a2dp_source_local_stream_endpoint->remote_sep.seid, 1057 1 << AVDTP_MEDIA_CODEC, 1058 new_configuration 1059 ); 1060 } 1061 1062 static uint8_t a2dp_source_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size){ 1063 uint8_t error = 0; 1064 if (a2dp_source_media_config_validator == NULL) { 1065 // update subevent id and call validator 1066 uint8_t avdtp_subevent_id = event[2]; 1067 uint8_t a2dp_subevent_id = a2dp_subevent_id_for_avdtp_subevent_id(avdtp_subevent_id); 1068 uint8_t * subevent_field = (uint8_t *) &event[2]; 1069 *subevent_field = a2dp_subevent_id; 1070 error = (*a2dp_source_media_config_validator)(stream_endpoint, event, size); 1071 *subevent_field = avdtp_subevent_id; 1072 } 1073 return error; 1074 } 1075 1076 void a2dp_source_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size)){ 1077 a2dp_source_media_config_validator = callback; 1078 avdtp_source_register_media_config_validator(&a2dp_source_media_config_validator_callback); 1079 } 1080 1081