1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "avrcp_browsing.c" 39 40 #include <stdint.h> 41 #include <string.h> 42 43 #include "bluetooth_psm.h" 44 #include "bluetooth_sdp.h" 45 #include "btstack_debug.h" 46 #include "btstack_event.h" 47 #include "btstack_memory.h" 48 #include "classic/sdp_client.h" 49 #include "classic/sdp_util.h" 50 #include "classic/avrcp_browsing.h" 51 52 typedef struct { 53 uint16_t browsing_cid; 54 uint16_t browsing_l2cap_psm; 55 uint16_t browsing_version; 56 } avrcp_browsing_sdp_query_context_t; 57 58 static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 59 60 static btstack_packet_handler_t avrcp_browsing_callback; 61 static avrcp_browsing_sdp_query_context_t sdp_query_context; 62 63 static bool l2cap_browsing_service_registered; 64 static btstack_packet_handler_t avrcp_browsing_controller_packet_handler; 65 static btstack_packet_handler_t avrcp_browsing_target_packet_handler; 66 static btstack_context_callback_registration_t avrcp_browsing_handle_sdp_client_query_request; 67 68 static bd_addr_t avrcp_browsing_sdp_addr; 69 70 void avrcp_browsing_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){ 71 connection->wait_to_send = true; 72 l2cap_request_can_send_now_event(l2cap_cid); 73 } 74 75 static void avrcp_retry_timer_timeout_handler(btstack_timer_source_t * timer){ 76 uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 77 avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 78 if (connection_controller == NULL) return; 79 avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 80 if (connection_target == NULL) return; 81 82 if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) return; 83 84 if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W2_L2CAP_RETRY){ 85 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 86 connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 87 88 l2cap_create_ertm_channel(avrcp_browsing_packet_handler, connection_controller->remote_addr, connection_controller->browsing_l2cap_psm, 89 &connection_controller->browsing_connection->ertm_config, 90 connection_controller->browsing_connection->ertm_buffer, 91 connection_controller->browsing_connection->ertm_buffer_size, NULL); 92 } 93 } 94 95 static void avrcp_retry_timer_start(avrcp_connection_t * connection){ 96 btstack_run_loop_set_timer_handler(&connection->retry_timer, avrcp_retry_timer_timeout_handler); 97 btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avrcp_cid); 98 99 // add some jitter/randomness to reconnect delay 100 uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F); 101 btstack_run_loop_set_timer(&connection->retry_timer, timeout); 102 103 btstack_run_loop_add_timer(&connection->retry_timer); 104 } 105 106 // AVRCP Browsing Service functions 107 static void avrcp_browsing_finalize_connection(avrcp_connection_t * connection){ 108 btstack_run_loop_remove_timer(&connection->retry_timer); 109 btstack_memory_avrcp_browsing_connection_free(connection->browsing_connection); 110 connection->browsing_connection = NULL; 111 } 112 113 static void avrcp_browsing_emit_connection_established(uint16_t browsing_cid, bd_addr_t addr, uint8_t status){ 114 btstack_assert(avrcp_browsing_callback != NULL); 115 116 uint8_t event[12]; 117 int pos = 0; 118 event[pos++] = HCI_EVENT_AVRCP_META; 119 event[pos++] = sizeof(event) - 2; 120 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED; 121 event[pos++] = status; 122 reverse_bd_addr(addr,&event[pos]); 123 pos += 6; 124 little_endian_store_16(event, pos, browsing_cid); 125 pos += 2; 126 (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 127 } 128 129 static void avrcp_browsing_emit_incoming_connection(uint16_t browsing_cid, bd_addr_t addr){ 130 btstack_assert(avrcp_browsing_callback != NULL); 131 132 uint8_t event[11]; 133 int pos = 0; 134 event[pos++] = HCI_EVENT_AVRCP_META; 135 event[pos++] = sizeof(event) - 2; 136 event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION; 137 reverse_bd_addr(addr,&event[pos]); 138 pos += 6; 139 little_endian_store_16(event, pos, browsing_cid); 140 pos += 2; 141 (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 142 } 143 144 static void avrcp_browsing_emit_connection_closed(uint16_t browsing_cid){ 145 btstack_assert(avrcp_browsing_callback != NULL); 146 147 uint8_t event[5]; 148 int pos = 0; 149 event[pos++] = HCI_EVENT_AVRCP_META; 150 event[pos++] = sizeof(event) - 2; 151 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED; 152 little_endian_store_16(event, pos, browsing_cid); 153 pos += 2; 154 (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 155 } 156 157 158 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection, uint16_t avrcp_browsing_cid){ 159 avrcp_browsing_connection_t * browsing_connection = btstack_memory_avrcp_browsing_connection_get(); 160 if (!browsing_connection){ 161 log_error("Not enough memory to create browsing connection"); 162 return NULL; 163 } 164 browsing_connection->state = AVCTP_CONNECTION_IDLE; 165 browsing_connection->transaction_label = 0xFF; 166 167 avrcp_connection->avrcp_browsing_cid = avrcp_browsing_cid; 168 avrcp_connection->browsing_connection = browsing_connection; 169 170 log_info("avrcp_browsing_create_connection, avrcp cid 0x%02x", avrcp_connection->avrcp_browsing_cid); 171 return browsing_connection; 172 } 173 174 static void avrcp_browsing_configure_ertm(avrcp_browsing_connection_t * browsing_connection, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){ 175 browsing_connection->ertm_buffer = ertm_buffer; 176 browsing_connection->ertm_buffer_size = ertm_buffer_size; 177 178 if (ertm_buffer_size > 0) { 179 (void)memcpy(&browsing_connection->ertm_config, ertm_config, 180 sizeof(l2cap_ertm_config_t)); 181 log_info("avrcp_browsing_configure_ertm"); 182 } 183 } 184 185 static avrcp_browsing_connection_t * avrcp_browsing_handle_incoming_connection(avrcp_connection_t * connection, uint16_t local_cid, uint16_t avrcp_browsing_cid){ 186 if (connection->browsing_connection == NULL){ 187 avrcp_browsing_create_connection(connection, avrcp_browsing_cid); 188 } 189 if (connection->browsing_connection) { 190 connection->browsing_connection->l2cap_browsing_cid = local_cid; 191 connection->browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION; 192 btstack_run_loop_remove_timer(&connection->retry_timer); 193 } 194 return connection->browsing_connection; 195 } 196 197 static void avrcp_browsing_handle_open_connection_for_role(avrcp_connection_t * connection, uint16_t local_cid){ 198 connection->browsing_connection->l2cap_browsing_cid = local_cid; 199 connection->browsing_connection->incoming_declined = false; 200 connection->browsing_connection->state = AVCTP_CONNECTION_OPENED; 201 log_info("L2CAP_EVENT_CHANNEL_OPENED browsing_avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role); 202 } 203 204 static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){ 205 return (avrcp_frame_type_t)((header & 0x02) >> 1); 206 } 207 208 static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 209 UNUSED(channel); 210 UNUSED(size); 211 bd_addr_t event_addr; 212 uint16_t local_cid; 213 uint8_t status; 214 bool decline_connection; 215 bool outoing_active; 216 217 avrcp_connection_t * connection_controller; 218 avrcp_connection_t * connection_target; 219 220 switch (packet_type){ 221 case L2CAP_DATA_PACKET: 222 switch (avrcp_get_frame_type(packet[0])){ 223 case AVRCP_RESPONSE_FRAME: 224 (*avrcp_browsing_controller_packet_handler)(packet_type, channel, packet, size); 225 break; 226 case AVRCP_COMMAND_FRAME: 227 default: // make compiler happy 228 (*avrcp_browsing_target_packet_handler)(packet_type, channel, packet, size); 229 break; 230 } 231 break; 232 case HCI_EVENT_PACKET: 233 btstack_assert(avrcp_browsing_controller_packet_handler != NULL); 234 btstack_assert(avrcp_browsing_target_packet_handler != NULL); 235 236 switch (hci_event_packet_get_type(packet)) { 237 238 case L2CAP_EVENT_INCOMING_CONNECTION: 239 btstack_assert(avrcp_browsing_controller_packet_handler != NULL); 240 btstack_assert(avrcp_browsing_target_packet_handler != NULL); 241 242 l2cap_event_incoming_connection_get_address(packet, event_addr); 243 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 244 outoing_active = false; 245 246 connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr); 247 connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr); 248 249 if (connection_target == NULL || connection_controller == NULL) { 250 l2cap_decline_connection(local_cid); 251 return; 252 } 253 254 if (connection_target->browsing_connection != NULL){ 255 if (connection_target->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){ 256 outoing_active = true; 257 connection_target->browsing_connection->incoming_declined = true; 258 } 259 } 260 261 if (connection_controller->browsing_connection != NULL){ 262 if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) { 263 outoing_active = true; 264 connection_controller->browsing_connection->incoming_declined = true; 265 } 266 } 267 268 decline_connection = outoing_active; 269 if (decline_connection == false){ 270 uint16_t avrcp_browsing_cid; 271 if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)){ 272 avrcp_browsing_cid = avrcp_get_next_cid(AVRCP_CONTROLLER); 273 } else { 274 avrcp_browsing_cid = connection_controller->avrcp_browsing_cid; 275 } 276 277 // create two connection objects (both) 278 connection_target->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_target, local_cid, avrcp_browsing_cid); 279 connection_controller->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_controller, local_cid, avrcp_browsing_cid); 280 281 if ((connection_target->browsing_connection == NULL) || (connection_controller->browsing_connection == NULL)){ 282 decline_connection = true; 283 if (connection_target->browsing_connection) { 284 avrcp_browsing_finalize_connection(connection_target); 285 } 286 if (connection_controller->browsing_connection) { 287 avrcp_browsing_finalize_connection(connection_controller); 288 } 289 } 290 } 291 if (decline_connection){ 292 l2cap_decline_connection(local_cid); 293 } else { 294 log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION browsing_avrcp_cid 0x%02x", connection_controller->avrcp_browsing_cid); 295 avrcp_browsing_emit_incoming_connection(connection_controller->avrcp_browsing_cid, event_addr); 296 } 297 break; 298 299 case L2CAP_EVENT_CHANNEL_OPENED: 300 l2cap_event_channel_opened_get_address(packet, event_addr); 301 status = l2cap_event_channel_opened_get_status(packet); 302 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 303 304 connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr); 305 connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr); 306 307 // incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION 308 // outgoing: structs are cteated in avrcp_connect() and avrcp_browsing_connect() 309 if ((connection_controller == NULL) || (connection_target == NULL)) { 310 break; 311 } 312 if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) { 313 break; 314 } 315 316 switch (status){ 317 case ERROR_CODE_SUCCESS: 318 avrcp_browsing_handle_open_connection_for_role(connection_target, local_cid); 319 avrcp_browsing_handle_open_connection_for_role(connection_controller, local_cid); 320 avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status); 321 return; 322 case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES: 323 if (connection_controller->browsing_connection->incoming_declined == true){ 324 log_info("Incoming browsing connection was declined, and the outgoing failed"); 325 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RETRY; 326 connection_controller->browsing_connection->incoming_declined = false; 327 connection_target->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RETRY; 328 connection_target->browsing_connection->incoming_declined = false; 329 avrcp_retry_timer_start(connection_controller); 330 return; 331 } 332 break; 333 default: 334 break; 335 } 336 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 337 avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status); 338 avrcp_browsing_finalize_connection(connection_controller); 339 avrcp_browsing_finalize_connection(connection_target); 340 break; 341 342 case L2CAP_EVENT_CHANNEL_CLOSED: 343 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 344 345 connection_controller = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid); 346 connection_target = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid); 347 if ((connection_controller == NULL) || (connection_target == NULL)) { 348 break; 349 } 350 if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) { 351 break; 352 } 353 avrcp_browsing_emit_connection_closed(connection_controller->avrcp_browsing_cid); 354 avrcp_browsing_finalize_connection(connection_controller); 355 avrcp_browsing_finalize_connection(connection_target); 356 break; 357 358 case L2CAP_EVENT_CAN_SEND_NOW: 359 local_cid = l2cap_event_can_send_now_get_local_cid(packet); 360 connection_target = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid); 361 if ((connection_target != NULL) && (connection_target->browsing_connection != NULL) && connection_target->browsing_connection->wait_to_send) { 362 connection_target->browsing_connection->wait_to_send = false; 363 (*avrcp_browsing_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size); 364 break; 365 } 366 connection_controller = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid); 367 if ((connection_controller != NULL) && (connection_controller->browsing_connection != NULL) && connection_controller->browsing_connection->wait_to_send) { 368 connection_controller->browsing_connection->wait_to_send = false; 369 (*avrcp_browsing_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size); 370 break; 371 } 372 break; 373 374 default: 375 break; 376 } 377 break; 378 default: 379 break; 380 } 381 382 } 383 384 void avrcp_browsing_init(void){ 385 if (l2cap_browsing_service_registered) return; 386 int status = l2cap_register_service(&avrcp_browsing_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2); 387 388 if (status != ERROR_CODE_SUCCESS) return; 389 l2cap_browsing_service_registered = true; 390 } 391 392 void avrcp_browsing_deinit(void){ 393 avrcp_browsing_callback = NULL; 394 l2cap_browsing_service_registered = false; 395 avrcp_browsing_controller_packet_handler = NULL; 396 avrcp_browsing_target_packet_handler = NULL; 397 (void) memset(&sdp_query_context, 0, sizeof(avrcp_browsing_sdp_query_context_t)); 398 (void) memset(avrcp_browsing_sdp_addr, 0, 6); 399 } 400 401 static void avrcp_browsing_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 402 UNUSED(packet_type); 403 UNUSED(channel); 404 UNUSED(size); 405 406 avrcp_connection_t * avrcp_target_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, sdp_query_context.browsing_cid); 407 avrcp_connection_t * avrcp_controller_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, sdp_query_context.browsing_cid); 408 409 if ((avrcp_target_connection == NULL) || (avrcp_target_connection->browsing_connection == NULL)) return; 410 if (avrcp_target_connection->browsing_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; 411 412 if ((avrcp_controller_connection == NULL) || (avrcp_controller_connection->browsing_connection == NULL)) return; 413 if (avrcp_controller_connection->browsing_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; 414 415 416 uint8_t status; 417 uint16_t browsing_l2cap_psm; 418 419 switch (hci_event_packet_get_type(packet)){ 420 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 421 avrcp_handle_sdp_client_query_attribute_value(packet); 422 break; 423 424 case SDP_EVENT_QUERY_COMPLETE: 425 status = sdp_event_query_complete_get_status(packet); 426 427 if (status != ERROR_CODE_SUCCESS){ 428 avrcp_browsing_emit_connection_established(avrcp_target_connection->avrcp_browsing_cid, avrcp_browsing_sdp_addr, status); 429 avrcp_browsing_finalize_connection(avrcp_target_connection); 430 avrcp_browsing_finalize_connection(avrcp_controller_connection); 431 break; 432 } 433 434 browsing_l2cap_psm = avrcp_sdp_query_browsing_l2cap_psm(); 435 if (!browsing_l2cap_psm){ 436 avrcp_browsing_emit_connection_established(avrcp_target_connection->avrcp_browsing_cid, avrcp_browsing_sdp_addr, SDP_SERVICE_NOT_FOUND); 437 avrcp_browsing_finalize_connection(avrcp_target_connection); 438 avrcp_browsing_finalize_connection(avrcp_controller_connection); 439 break; 440 } 441 442 l2cap_create_ertm_channel(avrcp_browsing_packet_handler, avrcp_browsing_sdp_addr, browsing_l2cap_psm, 443 &avrcp_controller_connection->browsing_connection->ertm_config, 444 avrcp_controller_connection->browsing_connection->ertm_buffer, 445 avrcp_controller_connection->browsing_connection->ertm_buffer_size, NULL); 446 break; 447 448 default: 449 break; 450 } 451 // register the SDP Query request to check if there is another connection waiting for the query 452 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 453 (void) sdp_client_register_query_callback(&avrcp_browsing_handle_sdp_client_query_request); 454 } 455 456 static void avrcp_browsing_handle_start_sdp_client_query(void * context){ 457 UNUSED(context); 458 // TODO 459 460 btstack_linked_list_t connections = avrcp_get_connections(); 461 btstack_linked_list_iterator_t it; 462 btstack_linked_list_iterator_init(&it, &connections); 463 while (btstack_linked_list_iterator_has_next(&it)){ 464 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 465 if (connection->browsing_connection == NULL) continue; 466 if (connection->browsing_connection->state != AVCTP_CONNECTION_W2_SEND_SDP_QUERY) continue; 467 connection->browsing_connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; 468 469 // prevent triggering SDP query twice (for each role once) 470 avrcp_connection_t * connection_with_opposite_role; 471 switch (connection->role){ 472 case AVRCP_CONTROLLER: 473 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, connection->avrcp_cid); 474 break; 475 case AVRCP_TARGET: 476 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, connection->avrcp_cid); 477 break; 478 default: 479 btstack_assert(false); 480 return; 481 } 482 if (connection->browsing_connection != NULL){ 483 connection_with_opposite_role->browsing_connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; 484 } 485 486 sdp_query_context.browsing_l2cap_psm = 0; 487 sdp_query_context.browsing_version = 0; 488 sdp_query_context.browsing_cid = connection->avrcp_browsing_cid; 489 490 sdp_client_query_uuid16(&avrcp_browsing_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP); 491 return; 492 } 493 } 494 495 496 uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){ 497 btstack_assert(avrcp_browsing_controller_packet_handler != NULL); 498 btstack_assert(avrcp_browsing_target_packet_handler != NULL); 499 500 avrcp_connection_t * connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr); 501 if (!connection_controller){ 502 return ERROR_CODE_COMMAND_DISALLOWED; 503 } 504 avrcp_connection_t * connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr); 505 if (!connection_target){ 506 return ERROR_CODE_COMMAND_DISALLOWED; 507 } 508 509 if (connection_controller->browsing_connection){ 510 return ERROR_CODE_COMMAND_DISALLOWED; 511 } 512 if (connection_target->browsing_connection){ 513 return ERROR_CODE_COMMAND_DISALLOWED; 514 } 515 516 uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER); 517 518 connection_controller->browsing_connection = avrcp_browsing_create_connection(connection_controller, cid); 519 if (!connection_controller->browsing_connection) return BTSTACK_MEMORY_ALLOC_FAILED; 520 521 connection_target->browsing_connection = avrcp_browsing_create_connection(connection_target, cid); 522 if (!connection_target->browsing_connection){ 523 avrcp_browsing_finalize_connection(connection_controller); 524 return BTSTACK_MEMORY_ALLOC_FAILED; 525 } 526 avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config); 527 avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config); 528 529 if (avrcp_browsing_cid != NULL){ 530 *avrcp_browsing_cid = cid; 531 } 532 533 if (connection_controller->browsing_l2cap_psm == 0){ 534 memcpy(avrcp_browsing_sdp_addr, remote_addr, 6); 535 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY; 536 connection_target->browsing_connection->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY; 537 avrcp_browsing_handle_sdp_client_query_request.callback = &avrcp_browsing_handle_start_sdp_client_query; 538 539 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 540 (void) sdp_client_register_query_callback(&avrcp_browsing_handle_sdp_client_query_request); 541 return ERROR_CODE_SUCCESS; 542 } else { 543 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 544 connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 545 546 return l2cap_create_ertm_channel(avrcp_browsing_packet_handler, remote_addr, connection_controller->browsing_l2cap_psm, 547 &connection_controller->browsing_connection->ertm_config, 548 connection_controller->browsing_connection->ertm_buffer, 549 connection_controller->browsing_connection->ertm_buffer_size, NULL); 550 } 551 } 552 553 uint8_t avrcp_browsing_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){ 554 avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid); 555 if (!connection_controller){ 556 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 557 } 558 avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 559 if (!connection_target){ 560 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 561 } 562 563 if (!connection_controller->browsing_connection){ 564 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 565 } 566 if (!connection_target->browsing_connection){ 567 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 568 } 569 570 if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){ 571 return ERROR_CODE_COMMAND_DISALLOWED; 572 } 573 574 avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config); 575 avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config); 576 577 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 578 connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 579 580 l2cap_accept_ertm_connection(connection_controller->browsing_connection->l2cap_browsing_cid, 581 &connection_controller->browsing_connection->ertm_config, 582 connection_controller->browsing_connection->ertm_buffer, 583 connection_controller->browsing_connection->ertm_buffer_size); 584 return ERROR_CODE_SUCCESS; 585 } 586 587 588 uint8_t avrcp_browsing_decline_incoming_connection(uint16_t avrcp_browsing_cid){ 589 avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid); 590 if (!connection_controller){ 591 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 592 } 593 avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 594 if (!connection_target){ 595 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 596 } 597 598 if (!connection_controller->browsing_connection){ 599 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 600 } 601 if (!connection_target->browsing_connection){ 602 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 603 } 604 605 if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){ 606 return ERROR_CODE_COMMAND_DISALLOWED; 607 } 608 609 l2cap_decline_connection(connection_controller->browsing_connection->l2cap_browsing_cid); 610 611 avrcp_browsing_finalize_connection(connection_controller); 612 avrcp_browsing_finalize_connection(connection_target); 613 return ERROR_CODE_SUCCESS; 614 } 615 616 uint8_t avrcp_browsing_disconnect(uint16_t avrcp_browsing_cid){ 617 avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid); 618 if (!connection_controller){ 619 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 620 } 621 avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 622 if (!connection_target){ 623 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 624 } 625 626 if (!connection_controller->browsing_connection){ 627 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 628 } 629 if (!connection_target->browsing_connection){ 630 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 631 } 632 633 l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0); 634 return ERROR_CODE_SUCCESS; 635 } 636 637 void avrcp_browsing_register_controller_packet_handler(btstack_packet_handler_t callback){ 638 avrcp_browsing_controller_packet_handler = callback; 639 } 640 641 void avrcp_browsing_register_target_packet_handler(btstack_packet_handler_t callback){ 642 avrcp_browsing_target_packet_handler = callback; 643 } 644 645 void avrcp_browsing_register_packet_handler(btstack_packet_handler_t callback){ 646 btstack_assert(callback != NULL); 647 avrcp_browsing_callback = callback; 648 } 649