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.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.h" 51 52 53 typedef struct { 54 uint8_t parse_sdp_record; 55 uint32_t record_id; 56 uint16_t avrcp_cid; 57 uint16_t avrcp_l2cap_psm; 58 uint16_t avrcp_version; 59 60 uint16_t browsing_l2cap_psm; 61 uint16_t browsing_version; 62 } avrcp_sdp_query_context_t; 63 64 65 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 66 67 static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service"; 68 static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider"; 69 static const char * default_avrcp_target_service_name = "BTstack AVRCP Target Service"; 70 static const char * default_avrcp_target_service_provider_name = "BTstack AVRCP Target Service Provider"; 71 72 static uint16_t avrcp_cid_counter; 73 74 static btstack_context_callback_registration_t avrcp_handle_sdp_client_query_request; 75 76 static avrcp_sdp_query_context_t sdp_query_context; 77 78 static btstack_packet_handler_t avrcp_callback; 79 80 static uint8_t attribute_value[45]; 81 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 82 83 static btstack_linked_list_t connections; 84 static btstack_packet_handler_t avrcp_controller_packet_handler; 85 static btstack_packet_handler_t avrcp_target_packet_handler; 86 static bool l2cap_service_registered = false; 87 88 static const char * avrcp_subunit_type_name[] = { 89 "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER", 90 "CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE", 91 "VENDOR_UNIQUE", "RESERVED_FOR_ALL_SUBUNIT_TYPES", 92 "EXTENDED_TO_NEXT_BYTE", "UNIT", "ERROR" 93 }; 94 95 const char * avrcp_subunit2str(uint16_t index){ 96 if (index <= 11) return avrcp_subunit_type_name[index]; 97 if ((index >= 0x1C) && (index <= 0x1F)) return avrcp_subunit_type_name[index - 0x10]; 98 return avrcp_subunit_type_name[16]; 99 } 100 101 static const char * avrcp_event_name[] = { 102 "ERROR", "PLAYBACK_STATUS_CHANGED", 103 "TRACK_CHANGED", "TRACK_REACHED_END", "TRACK_REACHED_START", 104 "PLAYBACK_POS_CHANGED", "BATT_STATUS_CHANGED", "SYSTEM_STATUS_CHANGED", 105 "PLAYER_APPLICATION_SETTING_CHANGED", "NOW_PLAYING_CONTENT_CHANGED", 106 "AVAILABLE_PLAYERS_CHANGED", "ADDRESSED_PLAYER_CHANGED", "UIDS_CHANGED", "VOLUME_CHANGED" 107 }; 108 const char * avrcp_event2str(uint16_t index){ 109 if (index <= 0x0d) return avrcp_event_name[index]; 110 return avrcp_event_name[0]; 111 } 112 113 static const char * avrcp_operation_name[] = { 114 "SKIP", NULL, NULL, NULL, NULL, 115 "VOLUME_UP", "VOLUME_DOWN", "MUTE", "PLAY", "STOP", "PAUSE", NULL, 116 "REWIND", "FAST_FORWARD", NULL, "FORWARD", "BACKWARD" // 0x4C 117 }; 118 119 const char * avrcp_operation2str(uint8_t operation_id){ 120 char * name = NULL; 121 if ((operation_id >= AVRCP_OPERATION_ID_SKIP) && (operation_id <= AVRCP_OPERATION_ID_BACKWARD)){ 122 name = (char *)avrcp_operation_name[operation_id - AVRCP_OPERATION_ID_SKIP]; 123 } 124 if (name == NULL){ 125 static char buffer[13]; 126 snprintf(buffer, sizeof(buffer), "Unknown 0x%02x", operation_id); 127 buffer[sizeof(buffer)-1] = 0; 128 return buffer; 129 } else { 130 return name; 131 } 132 } 133 134 static const char * avrcp_media_attribute_id_name[] = { 135 "NONE", "TITLE", "ARTIST", "ALBUM", "TRACK", "TOTAL TRACKS", "GENRE", "SONG LENGTH" 136 }; 137 const char * avrcp_attribute2str(uint8_t index){ 138 if ((index >= 1) && (index <= 7)) return avrcp_media_attribute_id_name[index]; 139 return avrcp_media_attribute_id_name[0]; 140 } 141 142 static const char * avrcp_play_status_name[] = { 143 "STOPPED", "PLAYING", "PAUSED", "FORWARD SEEK", "REVERSE SEEK", 144 "ERROR" // 0xFF 145 }; 146 const char * avrcp_play_status2str(uint8_t index){ 147 if ((index >= 1) && (index <= 4)) return avrcp_play_status_name[index]; 148 return avrcp_play_status_name[5]; 149 } 150 151 static const char * avrcp_ctype_name[] = { 152 "CONTROL", 153 "STATUS", 154 "SPECIFIC_INQUIRY", 155 "NOTIFY", 156 "GENERAL_INQUIRY", 157 "RESERVED5", 158 "RESERVED6", 159 "RESERVED7", 160 "NOT IMPLEMENTED IN REMOTE", 161 "ACCEPTED BY REMOTE", 162 "REJECTED BY REMOTE", 163 "IN_TRANSITION", 164 "IMPLEMENTED_STABLE", 165 "CHANGED_STABLE", 166 "RESERVED", 167 "INTERIM" 168 }; 169 const char * avrcp_ctype2str(uint8_t index){ 170 if (index < sizeof(avrcp_ctype_name)){ 171 return avrcp_ctype_name[index]; 172 } 173 return "NONE"; 174 } 175 176 static const char * avrcp_shuffle_mode_name[] = { 177 "SHUFFLE OFF", 178 "SHUFFLE ALL TRACKS", 179 "SHUFFLE GROUP" 180 }; 181 182 const char * avrcp_shuffle2str(uint8_t index){ 183 if ((index >= 1) && (index <= 3)) return avrcp_shuffle_mode_name[index-1]; 184 return "NONE"; 185 } 186 187 static const char * avrcp_repeat_mode_name[] = { 188 "REPEAT OFF", 189 "REPEAT SINGLE TRACK", 190 "REPEAT ALL TRACKS", 191 "REPEAT GROUP" 192 }; 193 194 const char * avrcp_repeat2str(uint8_t index){ 195 if ((index >= 1) && (index <= 4)) return avrcp_repeat_mode_name[index-1]; 196 return "NONE"; 197 } 198 199 btstack_linked_list_t avrcp_get_connections(void){ 200 return connections; 201 } 202 203 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){ 204 uint8_t cmd_opcode_index = 5; 205 if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED; 206 return packet[cmd_opcode_index]; 207 } 208 209 void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, 210 const char * service_name, const char * service_provider_name){ 211 uint8_t* attribute; 212 de_create_sequence(service); 213 214 // 0x0000 "Service Record Handle" 215 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 216 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 217 218 // 0x0001 "Service Class ID List" 219 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 220 attribute = de_push_sequence(service); 221 { 222 if (controller){ 223 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 224 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER); 225 } else { 226 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET); 227 } 228 } 229 de_pop_sequence(service, attribute); 230 231 // 0x0004 "Protocol Descriptor List" 232 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 233 attribute = de_push_sequence(service); 234 { 235 uint8_t* l2cpProtocol = de_push_sequence(attribute); 236 { 237 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 238 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVCTP); 239 } 240 de_pop_sequence(attribute, l2cpProtocol); 241 242 uint8_t* avctpProtocol = de_push_sequence(attribute); 243 { 244 de_add_number(avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // avctpProtocol_service 245 de_add_number(avctpProtocol, DE_UINT, DE_SIZE_16, 0x0104); // version 246 } 247 de_pop_sequence(attribute, avctpProtocol); 248 } 249 de_pop_sequence(service, attribute); 250 251 // 0x0005 "Public Browse Group" 252 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 253 attribute = de_push_sequence(service); 254 { 255 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 256 } 257 de_pop_sequence(service, attribute); 258 259 // 0x0009 "Bluetooth Profile Descriptor List" 260 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 261 attribute = de_push_sequence(service); 262 { 263 uint8_t *avrcProfile = de_push_sequence(attribute); 264 { 265 de_add_number(avrcProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 266 de_add_number(avrcProfile, DE_UINT, DE_SIZE_16, 0x0106); 267 } 268 de_pop_sequence(attribute, avrcProfile); 269 } 270 de_pop_sequence(service, attribute); 271 272 // 0x000d "Additional Bluetooth Profile Descriptor List" 273 if (browsing){ 274 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); 275 attribute = de_push_sequence(service); 276 { 277 uint8_t * des = de_push_sequence(attribute); 278 { 279 uint8_t* browsing_l2cpProtocol = de_push_sequence(des); 280 { 281 de_add_number(browsing_l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 282 de_add_number(browsing_l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVCTP_BROWSING); 283 } 284 de_pop_sequence(des, browsing_l2cpProtocol); 285 286 uint8_t* browsing_avctpProtocol = de_push_sequence(des); 287 { 288 de_add_number(browsing_avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // browsing_avctpProtocol_service 289 de_add_number(browsing_avctpProtocol, DE_UINT, DE_SIZE_16, 0x0104); // version 290 } 291 de_pop_sequence(des, browsing_avctpProtocol); 292 } 293 de_pop_sequence(attribute, des); 294 } 295 de_pop_sequence(service, attribute); 296 } 297 298 299 // 0x0100 "Service Name" 300 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 301 if (service_name){ 302 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 303 } else { 304 if (controller){ 305 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name); 306 } else { 307 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_name), (uint8_t *) default_avrcp_target_service_name); 308 } 309 } 310 311 // 0x0100 "Provider Name" 312 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 313 if (service_provider_name){ 314 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 315 } else { 316 if (controller){ 317 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name); 318 } else { 319 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_provider_name), (uint8_t *) default_avrcp_target_service_provider_name); 320 } 321 } 322 323 // 0x0311 "Supported Features" 324 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 325 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 326 } 327 328 avrcp_connection_t * avrcp_get_connection_for_bd_addr_for_role(avrcp_role_t role, bd_addr_t addr){ 329 btstack_linked_list_iterator_t it; 330 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 331 while (btstack_linked_list_iterator_has_next(&it)){ 332 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 333 if (connection->role != role) continue; 334 if (memcmp(addr, connection->remote_addr, 6) != 0) continue; 335 return connection; 336 } 337 return NULL; 338 } 339 340 avrcp_connection_t * avrcp_get_connection_for_l2cap_signaling_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){ 341 btstack_linked_list_iterator_t it; 342 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 343 while (btstack_linked_list_iterator_has_next(&it)){ 344 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 345 if (connection->role != role) continue; 346 if (connection->l2cap_signaling_cid != l2cap_cid) continue; 347 return connection; 348 } 349 return NULL; 350 } 351 352 avrcp_connection_t * avrcp_get_connection_for_avrcp_cid_for_role(avrcp_role_t role, uint16_t avrcp_cid){ 353 btstack_linked_list_iterator_t it; 354 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 355 while (btstack_linked_list_iterator_has_next(&it)){ 356 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 357 if (connection->role != role) continue; 358 if (connection->avrcp_cid != avrcp_cid) continue; 359 return connection; 360 } 361 return NULL; 362 } 363 364 avrcp_connection_t * avrcp_get_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid){ 365 btstack_linked_list_iterator_t it; 366 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 367 while (btstack_linked_list_iterator_has_next(&it)){ 368 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 369 if (connection->role != role) continue; 370 if (connection->avrcp_browsing_cid != browsing_cid) continue; 371 return connection; 372 } 373 return NULL; 374 } 375 376 avrcp_connection_t * avrcp_get_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid){ 377 btstack_linked_list_iterator_t it; 378 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 379 while (btstack_linked_list_iterator_has_next(&it)){ 380 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 381 if (connection->role != role) continue; 382 if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid)) continue; 383 return connection; 384 } 385 return NULL; 386 } 387 388 avrcp_browsing_connection_t * avrcp_get_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){ 389 btstack_linked_list_iterator_t it; 390 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections); 391 while (btstack_linked_list_iterator_has_next(&it)){ 392 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 393 if (connection->role != role) continue; 394 if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != l2cap_cid)) continue; 395 return connection->browsing_connection; 396 } 397 return NULL; 398 } 399 400 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){ 401 connection->wait_to_send = true; 402 l2cap_request_can_send_now_event(l2cap_cid); 403 } 404 405 uint16_t avrcp_get_next_cid(avrcp_role_t role){ 406 do { 407 if (avrcp_cid_counter == 0xffff) { 408 avrcp_cid_counter = 1; 409 } else { 410 avrcp_cid_counter++; 411 } 412 } while (avrcp_get_connection_for_avrcp_cid_for_role(role, avrcp_cid_counter) != NULL) ; 413 return avrcp_cid_counter; 414 } 415 416 417 static avrcp_connection_t * avrcp_create_connection(avrcp_role_t role, bd_addr_t remote_addr){ 418 avrcp_connection_t * connection = btstack_memory_avrcp_connection_get(); 419 if (!connection){ 420 log_error("Not enough memory to create connection for role %d", role); 421 return NULL; 422 } 423 424 connection->state = AVCTP_CONNECTION_IDLE; 425 connection->role = role; 426 427 connection->transaction_id = 0xFF; 428 connection->transaction_id_counter = 0; 429 430 connection->max_num_fragments = 0xFF; 431 log_info("avrcp_create_connection, role %d", role); 432 (void)memcpy(connection->remote_addr, remote_addr, 6); 433 btstack_linked_list_add(&connections, (btstack_linked_item_t *) connection); 434 return connection; 435 } 436 437 static void avrcp_finalize_connection(avrcp_connection_t * connection){ 438 btstack_run_loop_remove_timer(&connection->retry_timer); 439 btstack_linked_list_remove(&connections, (btstack_linked_item_t*) connection); 440 btstack_memory_avrcp_connection_free(connection); 441 } 442 443 static void avrcp_emit_connection_established(uint16_t avrcp_cid, bd_addr_t addr, hci_con_handle_t con_handle, uint8_t status){ 444 btstack_assert(avrcp_callback != NULL); 445 446 uint8_t event[14]; 447 int pos = 0; 448 event[pos++] = HCI_EVENT_AVRCP_META; 449 event[pos++] = sizeof(event) - 2; 450 event[pos++] = AVRCP_SUBEVENT_CONNECTION_ESTABLISHED; 451 event[pos++] = status; 452 little_endian_store_16(event, pos, avrcp_cid); 453 pos += 2; 454 reverse_bd_addr(addr,&event[pos]); 455 pos += 6; 456 little_endian_store_16(event, pos, con_handle); 457 pos += 2; 458 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 459 } 460 461 static void avrcp_emit_connection_closed(uint16_t avrcp_cid){ 462 btstack_assert(avrcp_callback != NULL); 463 464 uint8_t event[5]; 465 int pos = 0; 466 event[pos++] = HCI_EVENT_AVRCP_META; 467 event[pos++] = sizeof(event) - 2; 468 event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED; 469 little_endian_store_16(event, pos, avrcp_cid); 470 pos += 2; 471 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 472 } 473 474 uint16_t avrcp_sdp_query_browsing_l2cap_psm(void){ 475 return sdp_query_context.browsing_l2cap_psm; 476 } 477 478 void avrcp_handle_sdp_client_query_attribute_value(uint8_t *packet){ 479 des_iterator_t des_list_it; 480 des_iterator_t prot_it; 481 482 // Handle new SDP record 483 if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context.record_id) { 484 sdp_query_context.record_id = sdp_event_query_attribute_byte_get_record_id(packet); 485 sdp_query_context.parse_sdp_record = 0; 486 // log_info("SDP Record: Nr: %d", record_id); 487 } 488 489 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 490 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 491 492 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 493 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 494 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 495 if (de_get_element_type(attribute_value) != DE_DES) break; 496 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 497 uint8_t * element = des_iterator_get_element(&des_list_it); 498 if (de_get_element_type(element) != DE_UUID) continue; 499 uint32_t uuid = de_get_uuid32(element); 500 switch (uuid){ 501 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET: 502 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL: 503 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER: 504 sdp_query_context.parse_sdp_record = 1; 505 break; 506 default: 507 break; 508 } 509 } 510 break; 511 512 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { 513 if (!sdp_query_context.parse_sdp_record) break; 514 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 515 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 516 uint8_t *des_element; 517 uint8_t *element; 518 uint32_t uuid; 519 520 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 521 522 des_element = des_iterator_get_element(&des_list_it); 523 des_iterator_init(&prot_it, des_element); 524 element = des_iterator_get_element(&prot_it); 525 526 if (de_get_element_type(element) != DE_UUID) continue; 527 528 uuid = de_get_uuid32(element); 529 des_iterator_next(&prot_it); 530 switch (uuid){ 531 case BLUETOOTH_PROTOCOL_L2CAP: 532 if (!des_iterator_has_more(&prot_it)) continue; 533 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_l2cap_psm); 534 break; 535 case BLUETOOTH_PROTOCOL_AVCTP: 536 if (!des_iterator_has_more(&prot_it)) continue; 537 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_version); 538 break; 539 default: 540 break; 541 } 542 } 543 } 544 break; 545 case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: { 546 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 547 if (!sdp_query_context.parse_sdp_record) break; 548 if (de_get_element_type(attribute_value) != DE_DES) break; 549 550 des_iterator_t des_list_0_it; 551 uint8_t *element_0; 552 553 des_iterator_init(&des_list_0_it, attribute_value); 554 element_0 = des_iterator_get_element(&des_list_0_it); 555 556 for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 557 uint8_t *des_element; 558 uint8_t *element; 559 uint32_t uuid; 560 561 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 562 563 des_element = des_iterator_get_element(&des_list_it); 564 des_iterator_init(&prot_it, des_element); 565 element = des_iterator_get_element(&prot_it); 566 567 if (de_get_element_type(element) != DE_UUID) continue; 568 569 uuid = de_get_uuid32(element); 570 des_iterator_next(&prot_it); 571 switch (uuid){ 572 case BLUETOOTH_PROTOCOL_L2CAP: 573 if (!des_iterator_has_more(&prot_it)) continue; 574 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_l2cap_psm); 575 break; 576 case BLUETOOTH_PROTOCOL_AVCTP: 577 if (!des_iterator_has_more(&prot_it)) continue; 578 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_version); 579 break; 580 default: 581 break; 582 } 583 } 584 } 585 break; 586 default: 587 break; 588 } 589 } 590 } else { 591 log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 592 } 593 } 594 595 static void avrcp_handle_sdp_query_failed(avrcp_connection_t * connection, uint8_t status){ 596 if (connection == NULL) return; 597 log_info("AVRCP: SDP query failed with status 0x%02x.", status); 598 avrcp_emit_connection_established(connection->avrcp_cid, connection->remote_addr, connection->con_handle, status); 599 avrcp_finalize_connection(connection); 600 } 601 602 static void avrcp_handle_sdp_query_succeeded(avrcp_connection_t * connection){ 603 if (connection == NULL) return; 604 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 605 connection->avrcp_l2cap_psm = sdp_query_context.avrcp_l2cap_psm; 606 connection->browsing_version = sdp_query_context.browsing_version; 607 connection->browsing_l2cap_psm = sdp_query_context.browsing_l2cap_psm; 608 } 609 610 static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 611 UNUSED(packet_type); 612 UNUSED(channel); 613 UNUSED(size); 614 615 bool state_ok = true; 616 avrcp_connection_t * avrcp_target_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, sdp_query_context.avrcp_cid); 617 if (!avrcp_target_connection || avrcp_target_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) { 618 state_ok = false; 619 } 620 avrcp_connection_t * avrcp_controller_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, sdp_query_context.avrcp_cid); 621 if (!avrcp_controller_connection || avrcp_controller_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) { 622 state_ok = false; 623 } 624 if (!state_ok){ 625 // something wrong, nevertheless, start next sdp query if this one is complete 626 if (hci_event_packet_get_type(packet) == SDP_EVENT_QUERY_COMPLETE){ 627 (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request); 628 } 629 return; 630 } 631 632 uint8_t status; 633 634 switch (hci_event_packet_get_type(packet)){ 635 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 636 avrcp_handle_sdp_client_query_attribute_value(packet); 637 return; 638 639 case SDP_EVENT_QUERY_COMPLETE: 640 status = sdp_event_query_complete_get_status(packet); 641 642 if (status != ERROR_CODE_SUCCESS){ 643 avrcp_handle_sdp_query_failed(avrcp_controller_connection, status); 644 avrcp_handle_sdp_query_failed(avrcp_target_connection, status); 645 break; 646 } 647 648 if (!sdp_query_context.avrcp_l2cap_psm){ 649 avrcp_handle_sdp_query_failed(avrcp_controller_connection, SDP_SERVICE_NOT_FOUND); 650 avrcp_handle_sdp_query_failed(avrcp_target_connection, SDP_SERVICE_NOT_FOUND); 651 break; 652 } 653 654 avrcp_handle_sdp_query_succeeded(avrcp_controller_connection); 655 avrcp_handle_sdp_query_succeeded(avrcp_target_connection); 656 657 l2cap_create_channel(&avrcp_packet_handler, avrcp_target_connection->remote_addr, sdp_query_context.avrcp_l2cap_psm, l2cap_max_mtu(), NULL); 658 break; 659 660 default: 661 return; 662 } 663 664 // register the SDP Query request to check if there is another connection waiting for the query 665 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 666 (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request); 667 } 668 669 670 static avrcp_connection_t * avrcp_handle_incoming_connection_for_role(avrcp_role_t role, avrcp_connection_t * connection, bd_addr_t event_addr, hci_con_handle_t con_handle, uint16_t local_cid, uint16_t avrcp_cid){ 671 if (connection == NULL){ 672 connection = avrcp_create_connection(role, event_addr); 673 } 674 if (connection) { 675 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 676 connection->l2cap_signaling_cid = local_cid; 677 connection->avrcp_cid = avrcp_cid; 678 connection->con_handle = con_handle; 679 btstack_run_loop_remove_timer(&connection->retry_timer); 680 } 681 return connection; 682 } 683 684 static void avrcp_handle_open_connection(avrcp_connection_t * connection, hci_con_handle_t con_handle, uint16_t local_cid, uint16_t l2cap_mtu){ 685 connection->l2cap_signaling_cid = local_cid; 686 connection->l2cap_mtu = l2cap_mtu; 687 connection->con_handle = con_handle; 688 connection->incoming_declined = false; 689 connection->song_length_ms = 0xFFFFFFFF; 690 connection->song_position_ms = 0xFFFFFFFF; 691 connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR; 692 connection->state = AVCTP_CONNECTION_OPENED; 693 694 log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d, state %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role, connection->state); 695 } 696 697 static void avrcp_retry_timer_timeout_handler(btstack_timer_source_t * timer){ 698 uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 699 avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 700 if (connection_controller == NULL) return; 701 avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 702 if (connection_target == NULL) return; 703 704 if (connection_controller->state == AVCTP_CONNECTION_W2_L2CAP_RETRY){ 705 connection_controller->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 706 connection_target->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 707 l2cap_create_channel(&avrcp_packet_handler, connection_controller->remote_addr, connection_controller->avrcp_l2cap_psm, l2cap_max_mtu(), NULL); 708 } 709 } 710 711 static void avrcp_retry_timer_start(avrcp_connection_t * connection){ 712 btstack_run_loop_set_timer_handler(&connection->retry_timer, avrcp_retry_timer_timeout_handler); 713 btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avrcp_cid); 714 715 // add some jitter/randomness to reconnect delay 716 uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F); 717 btstack_run_loop_set_timer(&connection->retry_timer, timeout); 718 719 btstack_run_loop_add_timer(&connection->retry_timer); 720 } 721 722 static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){ 723 return (avrcp_frame_type_t)((header & 0x02) >> 1); 724 } 725 726 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 727 UNUSED(channel); 728 UNUSED(size); 729 bd_addr_t event_addr; 730 uint16_t local_cid; 731 uint16_t l2cap_mtu; 732 uint8_t status; 733 bool decline_connection; 734 bool outoing_active; 735 hci_con_handle_t con_handle; 736 737 avrcp_connection_t * connection_controller; 738 avrcp_connection_t * connection_target; 739 bool can_send; 740 741 switch (packet_type) { 742 case HCI_EVENT_PACKET: 743 switch (hci_event_packet_get_type(packet)) { 744 745 case L2CAP_EVENT_INCOMING_CONNECTION: 746 btstack_assert(avrcp_controller_packet_handler != NULL); 747 btstack_assert(avrcp_target_packet_handler != NULL); 748 749 l2cap_event_incoming_connection_get_address(packet, event_addr); 750 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 751 con_handle = l2cap_event_incoming_connection_get_handle(packet); 752 753 outoing_active = false; 754 connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr); 755 if (connection_target != NULL){ 756 if (connection_target->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){ 757 outoing_active = true; 758 connection_target->incoming_declined = true; 759 } 760 } 761 762 connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr); 763 if (connection_controller != NULL){ 764 if (connection_controller->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) { 765 outoing_active = true; 766 connection_controller->incoming_declined = true; 767 } 768 } 769 770 decline_connection = outoing_active; 771 if (decline_connection == false){ 772 uint16_t avrcp_cid; 773 if ((connection_controller == NULL) || (connection_target == NULL)){ 774 avrcp_cid = avrcp_get_next_cid(AVRCP_CONTROLLER); 775 } else { 776 avrcp_cid = connection_controller->avrcp_cid; 777 } 778 // create two connection objects (both) 779 connection_target = avrcp_handle_incoming_connection_for_role(AVRCP_TARGET, connection_target, event_addr, con_handle, local_cid, avrcp_cid); 780 connection_controller = avrcp_handle_incoming_connection_for_role(AVRCP_CONTROLLER, connection_controller, event_addr, con_handle, local_cid, avrcp_cid); 781 if ((connection_target == NULL) || (connection_controller == NULL)){ 782 decline_connection = true; 783 if (connection_target) { 784 avrcp_finalize_connection(connection_target); 785 } 786 if (connection_controller) { 787 avrcp_finalize_connection(connection_controller); 788 } 789 } 790 } 791 if (decline_connection){ 792 l2cap_decline_connection(local_cid); 793 } else { 794 log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION local cid 0x%02x, state %d", local_cid, connection_controller->state); 795 l2cap_accept_connection(local_cid); 796 } 797 break; 798 799 case L2CAP_EVENT_CHANNEL_OPENED: 800 l2cap_event_channel_opened_get_address(packet, event_addr); 801 status = l2cap_event_channel_opened_get_status(packet); 802 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 803 l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 804 con_handle = l2cap_event_channel_opened_get_handle(packet); 805 806 connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr); 807 connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr); 808 809 // incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION 810 // outgoing: structs are cteated in avrcp_connect() 811 if ((connection_controller == NULL) || (connection_target == NULL)) { 812 break; 813 } 814 815 switch (status){ 816 case ERROR_CODE_SUCCESS: 817 avrcp_handle_open_connection(connection_target, con_handle, local_cid, l2cap_mtu); 818 avrcp_handle_open_connection(connection_controller, con_handle, local_cid, l2cap_mtu); 819 avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status); 820 return; 821 case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES: 822 if (connection_controller->incoming_declined == true){ 823 log_info("Incoming connection was declined, and the outgoing failed"); 824 connection_controller->state = AVCTP_CONNECTION_W2_L2CAP_RETRY; 825 connection_controller->incoming_declined = false; 826 connection_target->state = AVCTP_CONNECTION_W2_L2CAP_RETRY; 827 connection_target->incoming_declined = false; 828 avrcp_retry_timer_start(connection_controller); 829 return; 830 } 831 break; 832 default: 833 break; 834 } 835 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 836 avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status); 837 avrcp_finalize_connection(connection_controller); 838 avrcp_finalize_connection(connection_target); 839 840 break; 841 842 case L2CAP_EVENT_CHANNEL_CLOSED: 843 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 844 845 connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid); 846 connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid); 847 if ((connection_controller == NULL) || (connection_target == NULL)) { 848 break; 849 } 850 avrcp_emit_connection_closed(connection_controller->avrcp_cid); 851 avrcp_finalize_connection(connection_controller); 852 avrcp_finalize_connection(connection_target); 853 break; 854 855 case L2CAP_EVENT_CAN_SEND_NOW: 856 local_cid = l2cap_event_can_send_now_get_local_cid(packet); 857 can_send = true; 858 859 connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid); 860 if ((connection_target != NULL) && connection_target->wait_to_send){ 861 connection_target->wait_to_send = false; 862 (*avrcp_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size); 863 can_send = false; 864 } 865 866 connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid); 867 if ((connection_controller != NULL) && connection_controller->wait_to_send){ 868 if (can_send){ 869 connection_controller->wait_to_send = false; 870 (*avrcp_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size); 871 } else { 872 l2cap_request_can_send_now_event(local_cid); 873 } 874 } 875 break; 876 877 default: 878 break; 879 } 880 break; 881 882 case L2CAP_DATA_PACKET: 883 switch (avrcp_get_frame_type(packet[0])){ 884 case AVRCP_RESPONSE_FRAME: 885 (*avrcp_controller_packet_handler)(packet_type, channel, packet, size); 886 break; 887 case AVRCP_COMMAND_FRAME: 888 default: // make compiler happy 889 (*avrcp_target_packet_handler)(packet_type, channel, packet, size); 890 break; 891 } 892 break; 893 894 default: 895 break; 896 } 897 } 898 899 uint8_t avrcp_disconnect(uint16_t avrcp_cid){ 900 avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 901 if (!connection_controller){ 902 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 903 } 904 avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 905 if (!connection_target){ 906 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 907 } 908 if (connection_controller->browsing_connection){ 909 l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0); 910 } 911 l2cap_disconnect(connection_controller->l2cap_signaling_cid, 0); 912 return ERROR_CODE_SUCCESS; 913 } 914 915 static void avrcp_handle_start_sdp_client_query(void * context){ 916 UNUSED(context); 917 918 btstack_linked_list_iterator_t it; 919 btstack_linked_list_iterator_init(&it, &connections); 920 while (btstack_linked_list_iterator_has_next(&it)){ 921 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 922 923 if (connection->state != AVCTP_CONNECTION_W2_SEND_SDP_QUERY) continue; 924 connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; 925 926 // prevent triggering SDP query twice (for each role once) 927 avrcp_connection_t * connection_with_opposite_role; 928 switch (connection->role){ 929 case AVRCP_CONTROLLER: 930 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, connection->avrcp_cid); 931 break; 932 case AVRCP_TARGET: 933 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, connection->avrcp_cid); 934 break; 935 default: 936 btstack_assert(false); 937 return; 938 } 939 connection_with_opposite_role->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; 940 941 sdp_query_context.avrcp_l2cap_psm = 0; 942 sdp_query_context.avrcp_version = 0; 943 sdp_query_context.avrcp_cid = connection->avrcp_cid; 944 sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP); 945 return; 946 } 947 } 948 949 uint8_t avrcp_connect(bd_addr_t remote_addr, uint16_t * avrcp_cid){ 950 btstack_assert(avrcp_controller_packet_handler != NULL); 951 btstack_assert(avrcp_target_packet_handler != NULL); 952 953 avrcp_connection_t * connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr); 954 if (connection_controller){ 955 return ERROR_CODE_COMMAND_DISALLOWED; 956 } 957 avrcp_connection_t * connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr); 958 if (connection_target){ 959 return ERROR_CODE_COMMAND_DISALLOWED; 960 } 961 962 uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER); 963 964 connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr); 965 if (!connection_controller) return BTSTACK_MEMORY_ALLOC_FAILED; 966 967 connection_target = avrcp_create_connection(AVRCP_TARGET, remote_addr); 968 if (!connection_target){ 969 avrcp_finalize_connection(connection_controller); 970 return BTSTACK_MEMORY_ALLOC_FAILED; 971 } 972 973 if (avrcp_cid != NULL){ 974 *avrcp_cid = cid; 975 } 976 977 connection_controller->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY; 978 connection_controller->avrcp_cid = cid; 979 980 connection_target->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY; 981 connection_target->avrcp_cid = cid; 982 983 avrcp_handle_sdp_client_query_request.callback = &avrcp_handle_start_sdp_client_query; 984 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 985 (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request); 986 return ERROR_CODE_SUCCESS; 987 } 988 989 void avrcp_init(void){ 990 connections = NULL; 991 if (l2cap_service_registered) return; 992 993 int status = l2cap_register_service(&avrcp_packet_handler, BLUETOOTH_PSM_AVCTP, 0xffff, gap_get_security_level()); 994 if (status != ERROR_CODE_SUCCESS) return; 995 l2cap_service_registered = true; 996 } 997 998 void avrcp_deinit(void){ 999 avrcp_cid_counter = 0; 1000 l2cap_service_registered = false; 1001 1002 (void) memset(&sdp_query_context, 0, sizeof(avrcp_sdp_query_context_t)); 1003 (void) memset(attribute_value,0, sizeof(attribute_value)); 1004 1005 avrcp_callback = NULL; 1006 connections = NULL; 1007 avrcp_controller_packet_handler = NULL; 1008 avrcp_target_packet_handler = NULL; 1009 } 1010 1011 void avrcp_register_controller_packet_handler(btstack_packet_handler_t callback){ 1012 avrcp_controller_packet_handler = callback; 1013 } 1014 1015 void avrcp_register_target_packet_handler(btstack_packet_handler_t callback){ 1016 avrcp_target_packet_handler = callback; 1017 } 1018 1019 void avrcp_register_packet_handler(btstack_packet_handler_t callback){ 1020 btstack_assert(callback != NULL); 1021 avrcp_callback = callback; 1022 } 1023 1024 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 1025 #define FUZZ_CID 0x44 1026 #define FUZZ_CON_HANDLE 0x0001 1027 static bd_addr_t remote_addr = { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 }; 1028 void avrcp_init_fuzz(void){ 1029 // setup avrcp connections for cid 1030 avrcp_connection_t * connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr); 1031 avrcp_connection_t * connection_target = avrcp_create_connection(AVRCP_TARGET, remote_addr); 1032 avrcp_handle_open_connection(connection_controller, FUZZ_CON_HANDLE, FUZZ_CID, 999); 1033 avrcp_handle_open_connection(connection_target, FUZZ_CON_HANDLE, FUZZ_CID, 999); 1034 } 1035 void avrcp_packet_handler_fuzz(uint8_t *packet, uint16_t size){ 1036 avrcp_packet_handler(L2CAP_DATA_PACKET, FUZZ_CID, packet, size); 1037 } 1038 #endif