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 <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "btstack.h" 46 #include "classic/avrcp.h" 47 #include "classic/avrcp_controller.h" 48 49 #define PSM_AVCTP BLUETOOTH_PROTOCOL_AVCTP 50 #define PSM_AVCTP_BROWSING 0x001b 51 52 /* 53 Category 1: Player/Recorder 54 Category 2: Monitor/Amplifier 55 Category 3: Tuner 56 Category 4: Menu 57 */ 58 59 /* controller supported features 60 Bit 0 = Category 1 61 Bit 1 = Category 2 62 Bit 2 = Category 3 63 Bit 3 = Category 4 64 Bit 4-5 = RFA 65 Bit 6 = Supports browsing 66 Bit 7-15 = RFA 67 The bits for supported categories are set to 1. Others are set to 0. 68 */ 69 70 /* target supported features 71 Bit 0 = Category 1 72 Bit 1 = Category 2 73 Bit 2 = Category 3 74 Bit 3 = Category 4 75 Bit 4 = Player Application Settings. Bit 0 should be set for this bit to be set. 76 Bit 5 = Group Navigation. Bit 0 should be set for this bit to be set. 77 Bit 6 = Supports browsing*4 78 Bit 7 = Supports multiple media player applications 79 Bit 8-15 = RFA 80 The bits for supported categories are set to 1. Others are set to 0. 81 */ 82 83 static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service"; 84 static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider"; 85 static const char * default_avrcp_target_service_name = "BTstack AVRCP Target Service"; 86 static const char * default_avrcp_target_service_provider_name = "BTstack AVRCP Target Service Provider"; 87 88 static uint16_t avrcp_cid_counter = 0; 89 90 static avrcp_context_t * sdp_query_context; 91 static uint8_t attribute_value[1000]; 92 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 93 94 95 static const char * avrcp_subunit_type_name[] = { 96 "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER", 97 "CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE", 98 "VENDOR_UNIQUE", "RESERVED_FOR_ALL_SUBUNIT_TYPES", 99 "EXTENDED_TO_NEXT_BYTE", "UNIT", "ERROR" 100 }; 101 102 const char * avrcp_subunit2str(uint16_t index){ 103 if (index <= 11) return avrcp_subunit_type_name[index]; 104 if (index >= 0x1C && index <= 0x1F) return avrcp_subunit_type_name[index - 0x10]; 105 return avrcp_subunit_type_name[16]; 106 } 107 108 static const char * avrcp_event_name[] = { 109 "ERROR", "PLAYBACK_STATUS_CHANGED", 110 "TRACK_CHANGED", "TRACK_REACHED_END", "TRACK_REACHED_START", 111 "PLAYBACK_POS_CHANGED", "BATT_STATUS_CHANGED", "SYSTEM_STATUS_CHANGED", 112 "PLAYER_APPLICATION_SETTING_CHANGED", "NOW_PLAYING_CONTENT_CHANGED", 113 "AVAILABLE_PLAYERS_CHANGED", "ADDRESSED_PLAYER_CHANGED", "UIDS_CHANGED", "VOLUME_CHANGED" 114 }; 115 const char * avrcp_event2str(uint16_t index){ 116 if (index <= 0x0d) return avrcp_event_name[index]; 117 return avrcp_event_name[0]; 118 } 119 120 static const char * avrcp_operation_name[] = { 121 "NOT SUPPORTED", // 0x3B 122 "SKIP", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED", 123 "VOLUME_UP", "VOLUME_DOWN", "MUTE", "PLAY", "STOP", "PAUSE", "NOT SUPPORTED", 124 "REWIND", "FAST_FORWARD", "NOT SUPPORTED", "FORWARD", "BACKWARD" // 0x4C 125 }; 126 const char * avrcp_operation2str(uint8_t index){ 127 if (index >= 0x3B && index <= 0x4C) return avrcp_operation_name[index - 0x3B]; 128 return avrcp_operation_name[0]; 129 } 130 131 static const char * avrcp_media_attribute_id_name[] = { 132 "NONE", "TITLE", "ARTIST", "ALBUM", "TRACK", "TOTAL TRACKS", "GENRE", "SONG LENGTH" 133 }; 134 const char * avrcp_attribute2str(uint8_t index){ 135 if (index >= 1 && index <= 7) return avrcp_media_attribute_id_name[index]; 136 return avrcp_media_attribute_id_name[0]; 137 } 138 139 static const char * avrcp_play_status_name[] = { 140 "STOPPED", "PLAYING", "PAUSED", "FORWARD SEEK", "REVERSE SEEK", 141 "ERROR" // 0xFF 142 }; 143 const char * avrcp_play_status2str(uint8_t index){ 144 if (index >= 1 && index <= 4) return avrcp_play_status_name[index]; 145 return avrcp_play_status_name[5]; 146 } 147 148 static const char * avrcp_ctype_name[] = { 149 "CONTROL", 150 "STATUS", 151 "SPECIFIC_INQUIRY", 152 "NOTIFY", 153 "GENERAL_INQUIRY", 154 "RESERVED5", 155 "RESERVED6", 156 "RESERVED7", 157 "NOT IMPLEMENTED IN REMOTE", 158 "ACCEPTED BY REMOTE", 159 "REJECTED BY REMOTE", 160 "IN_TRANSITION", 161 "IMPLEMENTED_STABLE", 162 "CHANGED_STABLE", 163 "RESERVED", 164 "INTERIM" 165 }; 166 const char * avrcp_ctype2str(uint8_t index){ 167 if (index < sizeof(avrcp_ctype_name)){ 168 return avrcp_ctype_name[index]; 169 } 170 return "NONE"; 171 } 172 173 static const char * avrcp_shuffle_mode_name[] = { 174 "SHUFFLE OFF", 175 "SHUFFLE ALL TRACKS", 176 "SHUFFLE GROUP" 177 }; 178 179 const char * avrcp_shuffle2str(uint8_t index){ 180 if (index >= 1 && index <= 3) return avrcp_shuffle_mode_name[index-1]; 181 return "NONE"; 182 } 183 184 static const char * avrcp_repeat_mode_name[] = { 185 "REPEAT OFF", 186 "REPEAT SINGLE TRACK", 187 "REPEAT ALL TRACKS", 188 "REPEAT GROUP" 189 }; 190 191 const char * avrcp_repeat2str(uint8_t index){ 192 if (index >= 1 && index <= 4) return avrcp_repeat_mode_name[index-1]; 193 return "NONE"; 194 } 195 196 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){ 197 uint8_t cmd_opcode_index = 5; 198 if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED; 199 return packet[cmd_opcode_index]; 200 } 201 202 void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, 203 const char * service_name, const char * service_provider_name){ 204 uint8_t* attribute; 205 de_create_sequence(service); 206 207 // 0x0000 "Service Record Handle" 208 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 209 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 210 211 // 0x0001 "Service Class ID List" 212 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 213 attribute = de_push_sequence(service); 214 { 215 if (controller){ 216 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 217 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER); 218 } else { 219 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET); 220 } 221 } 222 de_pop_sequence(service, attribute); 223 224 // 0x0004 "Protocol Descriptor List" 225 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 226 attribute = de_push_sequence(service); 227 { 228 uint8_t* l2cpProtocol = de_push_sequence(attribute); 229 { 230 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 231 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); 232 } 233 de_pop_sequence(attribute, l2cpProtocol); 234 235 uint8_t* avctpProtocol = de_push_sequence(attribute); 236 { 237 de_add_number(avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // avctpProtocol_service 238 de_add_number(avctpProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 239 } 240 de_pop_sequence(attribute, avctpProtocol); 241 } 242 de_pop_sequence(service, attribute); 243 244 // 0x0005 "Public Browse Group" 245 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 246 attribute = de_push_sequence(service); 247 { 248 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 249 } 250 de_pop_sequence(service, attribute); 251 252 // 0x0009 "Bluetooth Profile Descriptor List" 253 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 254 attribute = de_push_sequence(service); 255 { 256 uint8_t *avrcProfile = de_push_sequence(attribute); 257 { 258 de_add_number(avrcProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 259 de_add_number(avrcProfile, DE_UINT, DE_SIZE_16, 0x0105); 260 } 261 de_pop_sequence(attribute, avrcProfile); 262 } 263 de_pop_sequence(service, attribute); 264 265 // 0x000d "Additional Bluetooth Profile Descriptor List" 266 if (browsing){ 267 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); 268 attribute = de_push_sequence(service); 269 { 270 uint8_t * des = de_push_sequence(attribute); 271 { 272 uint8_t* browsing_l2cpProtocol = de_push_sequence(des); 273 { 274 de_add_number(browsing_l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 275 de_add_number(browsing_l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_AVCTP_BROWSING); 276 } 277 de_pop_sequence(des, browsing_l2cpProtocol); 278 279 uint8_t* browsing_avctpProtocol = de_push_sequence(des); 280 { 281 de_add_number(browsing_avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // browsing_avctpProtocol_service 282 de_add_number(browsing_avctpProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 283 } 284 de_pop_sequence(des, browsing_avctpProtocol); 285 } 286 de_pop_sequence(attribute, des); 287 } 288 de_pop_sequence(service, attribute); 289 } 290 291 292 // 0x0100 "Service Name" 293 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 294 if (service_name){ 295 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 296 } else { 297 if (controller){ 298 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name); 299 } else { 300 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_name), (uint8_t *) default_avrcp_target_service_name); 301 } 302 } 303 304 // 0x0100 "Provider Name" 305 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 306 if (service_provider_name){ 307 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 308 } else { 309 if (controller){ 310 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name); 311 } else { 312 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_provider_name), (uint8_t *) default_avrcp_target_service_provider_name); 313 } 314 } 315 316 // 0x0311 "Supported Features" 317 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 318 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 319 } 320 321 avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){ 322 btstack_linked_list_iterator_t it; 323 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 324 while (btstack_linked_list_iterator_has_next(&it)){ 325 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 326 if (memcmp(addr, connection->remote_addr, 6) != 0) continue; 327 return connection; 328 } 329 return NULL; 330 } 331 332 avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 333 btstack_linked_list_iterator_t it; 334 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 335 while (btstack_linked_list_iterator_has_next(&it)){ 336 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 337 if (connection->l2cap_signaling_cid != l2cap_cid) continue; 338 return connection; 339 } 340 return NULL; 341 } 342 343 avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context){ 344 btstack_linked_list_iterator_t it; 345 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 346 while (btstack_linked_list_iterator_has_next(&it)){ 347 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 348 if (connection->avrcp_cid != avrcp_cid) continue; 349 return connection; 350 } 351 return NULL; 352 } 353 354 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){ 355 connection->wait_to_send = 1; 356 l2cap_request_can_send_now_event(l2cap_cid); 357 } 358 359 360 uint16_t avrcp_get_next_cid(void){ 361 avrcp_cid_counter++; 362 if (avrcp_cid_counter == 0){ 363 avrcp_cid_counter = 1; 364 } 365 return avrcp_cid_counter; 366 } 367 368 static avrcp_connection_t * avrcp_create_connection(bd_addr_t remote_addr, avrcp_context_t * context){ 369 avrcp_connection_t * connection = btstack_memory_avrcp_connection_get(); 370 if (!connection){ 371 log_error("avrcp: not enough memory to create connection"); 372 return NULL; 373 } 374 memset(connection, 0, sizeof(avrcp_connection_t)); 375 connection->state = AVCTP_CONNECTION_IDLE; 376 connection->transaction_label = 0xFF; 377 connection->max_num_fragments = 0xFF; 378 connection->avrcp_cid = avrcp_get_next_cid(); 379 memcpy(connection->remote_addr, remote_addr, 6); 380 btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection); 381 return connection; 382 } 383 384 static void avrcp_finalize_connection(avrcp_connection_t * connection){ 385 btstack_linked_list_remove(&sdp_query_context->connections, (btstack_linked_item_t*) connection); 386 btstack_memory_avrcp_connection_free(connection); 387 } 388 389 void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status){ 390 if (!callback) return; 391 uint8_t event[12]; 392 int pos = 0; 393 event[pos++] = HCI_EVENT_AVRCP_META; 394 event[pos++] = sizeof(event) - 2; 395 event[pos++] = AVRCP_SUBEVENT_CONNECTION_ESTABLISHED; 396 event[pos++] = status; 397 reverse_bd_addr(addr,&event[pos]); 398 pos += 6; 399 little_endian_store_16(event, pos, avrcp_cid); 400 pos += 2; 401 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 402 } 403 404 void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid){ 405 if (!callback) return; 406 uint8_t event[5]; 407 int pos = 0; 408 event[pos++] = HCI_EVENT_AVRCP_META; 409 event[pos++] = sizeof(event) - 2; 410 event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED; 411 little_endian_store_16(event, pos, avrcp_cid); 412 pos += 2; 413 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 414 } 415 416 void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 417 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(sdp_query_context->avrcp_cid, sdp_query_context); 418 if (!connection) return; 419 if (connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return; 420 421 UNUSED(packet_type); 422 UNUSED(channel); 423 UNUSED(size); 424 uint8_t status; 425 des_iterator_t des_list_it; 426 des_iterator_t prot_it; 427 // uint32_t avdtp_remote_uuid = 0; 428 429 switch (hci_event_packet_get_type(packet)){ 430 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 431 // Handle new SDP record 432 if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context->record_id) { 433 sdp_query_context->record_id = sdp_event_query_attribute_byte_get_record_id(packet); 434 sdp_query_context->parse_sdp_record = 0; 435 // log_info("SDP Record: Nr: %d", record_id); 436 } 437 438 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 439 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 440 441 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 442 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 443 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 444 if (de_get_element_type(attribute_value) != DE_DES) break; 445 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 446 uint8_t * element = des_iterator_get_element(&des_list_it); 447 if (de_get_element_type(element) != DE_UUID) continue; 448 uint32_t uuid = de_get_uuid32(element); 449 switch (uuid){ 450 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET: 451 if (sdp_query_context->role == AVRCP_CONTROLLER) { 452 sdp_query_context->parse_sdp_record = 1; 453 break; 454 } 455 break; 456 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL: 457 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER: 458 if (sdp_query_context->role == AVRCP_TARGET) { 459 sdp_query_context->parse_sdp_record = 1; 460 break; 461 } 462 break; 463 default: 464 break; 465 } 466 } 467 break; 468 469 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { 470 if (!sdp_query_context->parse_sdp_record) break; 471 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 472 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 473 uint8_t *des_element; 474 uint8_t *element; 475 uint32_t uuid; 476 477 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 478 479 des_element = des_iterator_get_element(&des_list_it); 480 des_iterator_init(&prot_it, des_element); 481 element = des_iterator_get_element(&prot_it); 482 483 if (de_get_element_type(element) != DE_UUID) continue; 484 485 uuid = de_get_uuid32(element); 486 switch (uuid){ 487 case BLUETOOTH_PROTOCOL_L2CAP: 488 if (!des_iterator_has_more(&prot_it)) continue; 489 des_iterator_next(&prot_it); 490 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_l2cap_psm); 491 break; 492 case BLUETOOTH_PROTOCOL_AVCTP: 493 if (!des_iterator_has_more(&prot_it)) continue; 494 des_iterator_next(&prot_it); 495 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_version); 496 break; 497 default: 498 break; 499 } 500 } 501 } 502 break; 503 case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: { 504 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 505 if (!sdp_query_context->parse_sdp_record) break; 506 if (de_get_element_type(attribute_value) != DE_DES) break; 507 508 des_iterator_t des_list_0_it; 509 uint8_t *element_0; 510 511 des_iterator_init(&des_list_0_it, attribute_value); 512 element_0 = des_iterator_get_element(&des_list_0_it); 513 514 for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 515 uint8_t *des_element; 516 uint8_t *element; 517 uint32_t uuid; 518 519 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 520 521 des_element = des_iterator_get_element(&des_list_it); 522 des_iterator_init(&prot_it, des_element); 523 element = des_iterator_get_element(&prot_it); 524 525 if (de_get_element_type(element) != DE_UUID) continue; 526 527 uuid = de_get_uuid32(element); 528 switch (uuid){ 529 case BLUETOOTH_PROTOCOL_L2CAP: 530 if (!des_iterator_has_more(&prot_it)) continue; 531 des_iterator_next(&prot_it); 532 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_l2cap_psm); 533 break; 534 case BLUETOOTH_PROTOCOL_AVCTP: 535 if (!des_iterator_has_more(&prot_it)) continue; 536 des_iterator_next(&prot_it); 537 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_version); 538 break; 539 default: 540 break; 541 } 542 } 543 } 544 break; 545 default: 546 break; 547 } 548 } 549 } else { 550 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)); 551 } 552 break; 553 554 case SDP_EVENT_QUERY_COMPLETE:{ 555 status = sdp_event_query_complete_get_status(packet); 556 557 if (status != ERROR_CODE_SUCCESS){ 558 log_info("AVRCP: SDP query failed with status 0x%02x.", status); 559 avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, status); 560 avrcp_finalize_connection(connection); 561 break; 562 } 563 564 if (!sdp_query_context->avrcp_l2cap_psm){ 565 connection->state = AVCTP_CONNECTION_IDLE; 566 log_info("AVRCP: no suitable service found"); 567 avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND); 568 avrcp_finalize_connection(connection); 569 break; 570 } 571 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 572 l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avrcp_l2cap_psm, l2cap_max_mtu(), NULL); 573 break; 574 } 575 } 576 } 577 578 void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ 579 UNUSED(channel); 580 UNUSED(size); 581 bd_addr_t event_addr; 582 uint16_t local_cid; 583 uint8_t status; 584 avrcp_connection_t * connection = NULL; 585 uint16_t psm; 586 587 if (packet_type != HCI_EVENT_PACKET) return; 588 589 switch (hci_event_packet_get_type(packet)) { 590 case HCI_EVENT_DISCONNECTION_COMPLETE: 591 avrcp_emit_connection_closed(context->avrcp_callback, 0); 592 break; 593 case L2CAP_EVENT_INCOMING_CONNECTION: 594 l2cap_event_incoming_connection_get_address(packet, event_addr); 595 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 596 connection = avrcp_create_connection(event_addr, context); 597 if (!connection) { 598 log_error("Failed to alloc connection structure"); 599 l2cap_decline_connection(local_cid); 600 break; 601 } 602 603 psm = l2cap_event_incoming_connection_get_psm(packet); 604 if (psm == PSM_AVCTP){ 605 if (!connection->l2cap_signaling_cid){ 606 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 607 connection->l2cap_signaling_cid = local_cid; 608 log_info("L2CAP_EVENT_INCOMING_CONNECTION avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->avrcp_cid, connection->l2cap_signaling_cid); 609 l2cap_accept_connection(local_cid); 610 break; 611 } 612 } 613 log_info("L2CAP_EVENT_INCOMING_CONNECTION local_cid 0x%02x, psm 0x%2x, decline connection", local_cid, psm); 614 l2cap_decline_connection(local_cid); 615 break; 616 617 case L2CAP_EVENT_CHANNEL_OPENED: 618 l2cap_event_channel_opened_get_address(packet, event_addr); 619 status = l2cap_event_channel_opened_get_status(packet); 620 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 621 622 connection = get_avrcp_connection_for_bd_addr(event_addr, context); 623 if (!connection){ 624 // TODO: validate if this cannot happen. If not, drop disconnect call 625 log_error("AVRCP connection lookup failed"); 626 l2cap_disconnect(local_cid, 0); // reason isn't used 627 break; 628 } 629 if (status != ERROR_CODE_SUCCESS){ 630 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 631 avrcp_emit_connection_established(context->avrcp_callback, connection->avrcp_cid, event_addr, status); 632 avrcp_finalize_connection(connection); 633 break; 634 } 635 636 psm = l2cap_event_channel_opened_get_psm(packet); 637 if (psm == PSM_AVCTP){ 638 connection->l2cap_signaling_cid = local_cid; 639 connection->l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 640 connection->song_length_ms = 0xFFFFFFFF; 641 connection->song_position_ms = 0xFFFFFFFF; 642 connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR; 643 644 log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->avrcp_cid, connection->l2cap_signaling_cid); 645 connection->state = AVCTP_CONNECTION_OPENED; 646 avrcp_emit_connection_established(context->avrcp_callback, connection->avrcp_cid, event_addr, ERROR_CODE_SUCCESS); 647 } 648 break; 649 650 case L2CAP_EVENT_CHANNEL_CLOSED: 651 // data: event (8), len(8), channel (16) 652 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 653 connection = get_avrcp_connection_for_l2cap_signaling_cid(local_cid, context); 654 if (connection){ 655 avrcp_emit_connection_closed(context->avrcp_callback, connection->avrcp_cid); 656 avrcp_finalize_connection(connection); 657 break; 658 } 659 break; 660 default: 661 break; 662 } 663 } 664 665 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid){ 666 avrcp_connection_t * connection = get_avrcp_connection_for_bd_addr(bd_addr, context); 667 if (connection){ 668 return ERROR_CODE_COMMAND_DISALLOWED; 669 } 670 connection = avrcp_create_connection(bd_addr, context); 671 if (!connection){ 672 log_error("avrcp: could not allocate connection struct."); 673 return BTSTACK_MEMORY_ALLOC_FAILED; 674 } 675 676 if (avrcp_cid){ 677 *avrcp_cid = connection->avrcp_cid; 678 } 679 680 connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE; 681 682 context->avrcp_l2cap_psm = 0; 683 context->avrcp_version = 0; 684 context->avrcp_cid = connection->avrcp_cid; 685 connection->browsing_l2cap_psm = 0; 686 sdp_query_context = context; 687 688 uint8_t status = sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, bd_addr, BLUETOOTH_PROTOCOL_AVCTP); 689 690 // free connection struct in case of SDP connection error 691 if (status){ 692 log_info("AVRCP: SDP query failed with status 0x%02x.", status); 693 avrcp_finalize_connection(connection); 694 } 695 696 return status; 697 } 698