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