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