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