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_browsing_client.c" 39 40 /* 41 * avrcp_browsing_client.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(avrcp_browsing_client): AVRCP Browsing - Browse Media Players and Media Information 46 * 47 * @text This example demonstrates how to use the AVRCP Controller Browsing service to 48 * browse madia players and media information on a remote AVRCP Source device. 49 * 50 * @text To test with a remote device, e.g. a mobile phone, 51 * pair from the remote device with the demo, then use the UI for browsing. If HAVE_BTSTACK_STDIN is set, 52 * press SPACE on the console to show the available AVDTP and AVRCP commands. 53 * 54 */ 55 // ***************************************************************************** 56 57 #include <stdint.h> 58 #include <inttypes.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 63 #include "btstack.h" 64 65 #ifdef HAVE_BTSTACK_STDIN 66 #include "btstack_stdin.h" 67 #endif 68 69 #define AVRCP_BROWSING_ENABLED 70 71 #define AVRCP_BROWSING_MAX_PLAYERS 10 72 #define AVRCP_BROWSING_MAX_FOLDERS 10 73 #define AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN 30 74 #define AVRCP_BROWSING_MAX_MEDIA_ITEMS 10 75 76 #ifdef HAVE_BTSTACK_STDIN 77 // mac 2011: static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3}; 78 // pts: static bd_addr_t remote = {0x00, 0x1B, 0xDC, 0x08, 0x0A, 0xA5}; 79 // mac 2013: 80 // static const char * device_addr_string = "84:38:35:65:d1:15"; 81 // iPhone 5S: static const char * device_addr_string = "54:E4:3A:26:A2:39"; 82 // phone 2013: 83 // static const char * device_addr_string = "B0:34:95:CB:97:C4"; 84 // iPod 85 // static const char * device_addr_string = "B0:34:95:CB:97:C4"; 86 // iPhone 87 static const char * device_addr_string = "6C:72:E7:10:22:EE"; 88 89 static bd_addr_t device_addr; 90 #endif 91 92 typedef enum { 93 AVRCP_BROWSING_STATE_IDLE, 94 AVRCP_BROWSING_STATE_W4_GET_PLAYERS, 95 AVRCP_BROWSING_STATE_W4_SET_PLAYER, 96 AVRCP_BROWSING_STATE_READY 97 } avrcp_browsing_state_t; 98 99 typedef struct { 100 uint16_t charset; 101 uint8_t depth; 102 uint16_t name_len; 103 char name[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 104 } avrcp_browsing_root_folder_t; 105 106 typedef struct { 107 uint8_t uid[8]; 108 uint16_t name_len; 109 char name[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 110 } avrcp_browsable_item_t; 111 112 typedef struct { 113 uint16_t player_id; 114 uint8_t major_player_type; 115 uint32_t player_sub_type; 116 uint8_t play_status; 117 uint8_t feature_bitmask[16]; 118 uint16_t charset; 119 uint16_t name_len; 120 char name[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 121 } avrcp_browsable_media_player_item_t; 122 123 typedef struct { 124 uint32_t id; 125 uint16_t charset; 126 uint16_t value_len; 127 char value[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 128 } avrcp_browsable_media_element_item_attribute_t; 129 130 typedef struct { 131 uint8_t uid[8]; 132 uint8_t media_type; 133 uint16_t charset; 134 uint16_t name_len; 135 char name[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 136 uint8_t num_attributes; 137 } avrcp_browsable_media_element_item_t; 138 139 static avrcp_browsing_state_t browsing_state = AVRCP_BROWSING_STATE_IDLE; 140 static uint16_t avrcp_cid = 0; 141 static bool avrcp_connected = false; 142 143 static uint16_t browsing_cid = 0; 144 static bool browsing_connected = false; 145 146 static uint8_t sdp_avrcp_browsing_controller_service_buffer[200]; 147 static uint8_t sdp_avdtp_sink_service_buffer[150]; 148 149 static uint16_t a2dp_cid = 0; 150 static uint8_t a2dp_local_seid = 0; 151 152 static uint8_t media_sbc_codec_capabilities[] = { 153 0xFF,//(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 154 0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 155 2, 53 156 }; 157 158 static uint8_t media_sbc_codec_configuration[] = { 159 (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 160 (AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 161 2, 53 162 }; 163 164 static bool browsing_query_active = false; 165 // static avrcp_media_item_context_t media_item_context; 166 167 static int playable_folder_index = 0; 168 169 170 static uint16_t browsing_uid_counter = 0; 171 172 static uint8_t parent_folder_set = 0; 173 static uint8_t parent_folder_uid[8]; 174 static char parent_folder_name[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 175 176 static avrcp_browsable_item_t folders[AVRCP_BROWSING_MAX_FOLDERS]; 177 static int folder_index = -1; 178 179 static avrcp_browsable_media_element_item_t media_element_items[AVRCP_BROWSING_MAX_MEDIA_ITEMS]; 180 static int media_element_item_index = -1; 181 182 static avrcp_browsable_media_player_item_t media_player_items[AVRCP_BROWSING_MAX_MEDIA_ITEMS]; 183 static int media_player_item_index = -1; 184 185 static btstack_packet_callback_registration_t hci_event_callback_registration; 186 187 static uint8_t ertm_buffer[10000]; 188 static l2cap_ertm_config_t ertm_config = { 189 1, // ertm mandatory 190 2, // max transmit, some tests require > 1 191 2000, 192 12000, 193 144, // l2cap ertm mtu 194 4, 195 4, 196 0, // No FCS 197 }; 198 199 200 static inline int next_index(int * index, int max_value){ 201 if ((*index) < max_value){ 202 (*index)++; 203 } else { 204 (*index) = 0; 205 } 206 return (*index); 207 } 208 209 static int next_folder_index(void){ 210 return next_index(&folder_index, AVRCP_BROWSING_MAX_FOLDERS); 211 } 212 213 static int next_media_element_item_index(void){ 214 return next_index(&media_element_item_index, AVRCP_BROWSING_MAX_MEDIA_ITEMS); 215 } 216 static int next_media_player_item_index(void){ 217 return next_index(&media_player_item_index, AVRCP_BROWSING_MAX_MEDIA_ITEMS); 218 } 219 220 /* @section Main Application Setup 221 * 222 * @text The Listing MainConfiguration shows how to setup AVRCP Controller Browsing service. 223 * To announce AVRCP Controller Browsing service, you need to create corresponding 224 * SDP record and register it with the SDP service. 225 * You'll also need to register several packet handlers: 226 * - stdin_process callback - used to trigger AVRCP commands, such are get media players, playlists, albums, etc. Requires HAVE_BTSTACK_STDIN. 227 * - avrcp_browsing_controller_packet_handler - used to receive answers for AVRCP commands. 228 * 229 */ 230 231 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP Controller services */ 232 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 233 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 234 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 235 236 #ifdef HAVE_BTSTACK_STDIN 237 static void stdin_process(char cmd); 238 #endif 239 240 241 int btstack_main(int argc, const char * argv[]); 242 int btstack_main(int argc, const char * argv[]){ 243 (void)argc; 244 (void)argv; 245 246 // Initialize L2CAP. 247 l2cap_init(); 248 249 a2dp_sink_init(); 250 a2dp_sink_register_packet_handler(&a2dp_sink_packet_handler); 251 252 avdtp_stream_endpoint_t * local_stream_endpoint = a2dp_sink_create_stream_endpoint(AVDTP_AUDIO, 253 AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), 254 media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration)); 255 if (!local_stream_endpoint){ 256 printf("A2DP Sink: not enough memory to create local stream endpoint\n"); 257 return 1; 258 } 259 a2dp_local_seid = avdtp_local_seid(local_stream_endpoint); 260 261 // Initialize AVRCP service. 262 avrcp_init(); 263 // Initialize AVRCP Controller & Target Service. 264 avrcp_controller_init(); 265 avrcp_target_init(); 266 267 avrcp_register_packet_handler(&avrcp_packet_handler); 268 avrcp_controller_register_packet_handler(&avrcp_packet_handler); 269 avrcp_target_register_packet_handler(&avrcp_packet_handler); 270 271 // Initialize AVRCP Browsing Service. 272 avrcp_browsing_init(); 273 avrcp_browsing_controller_init(); 274 avrcp_browsing_target_init(); 275 276 // Register for HCI events. 277 avrcp_browsing_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler); 278 avrcp_browsing_target_register_packet_handler(&avrcp_browsing_controller_packet_handler); 279 avrcp_browsing_register_packet_handler(&avrcp_browsing_controller_packet_handler); 280 281 // Initialize SDP. 282 sdp_init(); 283 // setup AVDTP sink 284 memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer)); 285 a2dp_sink_create_sdp_record(sdp_avdtp_sink_service_buffer, 0x10001, AVDTP_SINK_FEATURE_MASK_HEADPHONE, NULL, NULL); 286 sdp_register_service(sdp_avdtp_sink_service_buffer); 287 288 // Create AVRCP service record and register it with SDP. 289 memset(sdp_avrcp_browsing_controller_service_buffer, 0, sizeof(sdp_avrcp_browsing_controller_service_buffer)); 290 291 uint16_t supported_features = AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER; 292 #ifdef AVRCP_BROWSING_ENABLED 293 supported_features |= AVRCP_FEATURE_MASK_BROWSING; 294 #endif 295 avrcp_controller_create_sdp_record(sdp_avrcp_browsing_controller_service_buffer, 0x10002, supported_features, NULL, NULL); 296 sdp_register_service(sdp_avrcp_browsing_controller_service_buffer); 297 298 // Set local name with a template Bluetooth address, that will be automatically 299 // replaced with a actual address once it is available, i.e. when BTstack boots 300 // up and starts talking to a Bluetooth module. 301 gap_set_local_name("AVRCP Browsing Client 00:00:00:00:00:00"); 302 gap_discoverable_control(1); 303 gap_set_class_of_device(0x200408); 304 305 // Register for HCI events. 306 hci_event_callback_registration.callback = &avrcp_browsing_controller_packet_handler; 307 hci_add_event_handler(&hci_event_callback_registration); 308 309 310 #ifdef HAVE_BTSTACK_STDIN 311 // Parse human readable Bluetooth address. 312 sscanf_bd_addr(device_addr_string, device_addr); 313 btstack_stdin_setup(stdin_process); 314 #endif 315 printf("Starting BTstack ...\n"); 316 hci_power_control(HCI_POWER_ON); 317 return 0; 318 } 319 /* LISTING_END */ 320 321 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 322 UNUSED(channel); 323 UNUSED(size); 324 uint16_t local_cid; 325 uint8_t status = 0xFF; 326 bd_addr_t adress; 327 328 if (packet_type != HCI_EVENT_PACKET) return; 329 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 330 switch (packet[2]){ 331 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { 332 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 333 status = avrcp_subevent_connection_established_get_status(packet); 334 if (status != ERROR_CODE_SUCCESS){ 335 printf("AVRCP: Connection failed: status 0x%02x\n", status); 336 avrcp_cid = 0; 337 return; 338 } 339 340 avrcp_cid = local_cid; 341 avrcp_connected = true; 342 avrcp_subevent_connection_established_get_bd_addr(packet, adress); 343 printf("AVRCP: Connected to %s, cid 0x%02x\n", bd_addr_to_str(adress), avrcp_cid); 344 return; 345 } 346 347 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 348 printf("AVRCP: Channel released: cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 349 avrcp_cid = 0; 350 avrcp_connected = false; 351 return; 352 default: 353 break; 354 } 355 } 356 357 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 358 UNUSED(channel); 359 UNUSED(size); 360 bd_addr_t address; 361 uint8_t status; 362 363 if (packet_type != HCI_EVENT_PACKET) return; 364 if (hci_event_packet_get_type(packet) != HCI_EVENT_A2DP_META) return; 365 366 switch (packet[2]){ 367 case A2DP_SUBEVENT_STREAM_ESTABLISHED: 368 a2dp_subevent_stream_established_get_bd_addr(packet, address); 369 status = a2dp_subevent_stream_established_get_status(packet); 370 371 if (status){ 372 printf("A2DP Sink : Streaming connection failed, status 0x%02x\n", status); 373 break; 374 } 375 376 a2dp_cid = a2dp_subevent_stream_established_get_a2dp_cid(packet); 377 memcpy(device_addr, address, 6); 378 printf("A2DP Sink: Connection established, address %s, a2dp_cid 0x%02x\n", bd_addr_to_str(address), a2dp_cid); 379 break; 380 381 case A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 382 printf("A2DP Sink: Connection released\n"); 383 break; 384 385 default: 386 break; 387 } 388 } 389 390 391 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 392 UNUSED(channel); 393 UNUSED(size); 394 bd_addr_t address; 395 uint8_t status; 396 uint16_t pos = 0; 397 uint16_t local_cid; 398 399 // printf("avrcp_browsing_controller_packet_handler packet type 0x%02X, subevent 0x%02X\n", packet_type, packet[2]); 400 switch (packet_type) { 401 case HCI_EVENT_PACKET: 402 switch (packet[0]){ 403 case HCI_EVENT_AVRCP_META: 404 switch (packet[2]){ 405 case AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED: { 406 local_cid = avrcp_subevent_browsing_connection_established_get_browsing_cid(packet); 407 status = avrcp_subevent_browsing_connection_established_get_status(packet); 408 if (status != ERROR_CODE_SUCCESS){ 409 printf("AVRCP: Connection failed: status 0x%02x\n", status); 410 browsing_cid = 0; 411 return; 412 } 413 avrcp_subevent_browsing_connection_established_get_bd_addr(packet, address); 414 browsing_cid = local_cid; 415 browsing_connected = true; 416 printf("AVRCP Browsing: Connection established, address %s, browsing_cid 0x%02x\n", bd_addr_to_str(address), browsing_cid); 417 return; 418 } 419 420 case AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED: 421 printf("AVRCP: Connection released, cid 0x%02x\n", avrcp_subevent_browsing_connection_released_get_browsing_cid(packet)); 422 browsing_cid = 0; 423 browsing_connected = false; 424 return; 425 case AVRCP_SUBEVENT_BROWSING_DONE: 426 427 browsing_query_active = 0; 428 browsing_uid_counter = 0; 429 if (avrcp_subevent_browsing_done_get_browsing_status(packet) != AVRCP_BROWSING_ERROR_CODE_SUCCESS){ 430 printf("AVRCP Browsing query done with browsing status 0x%02x, bluetooth status 0x%02x.\n", 431 avrcp_subevent_browsing_done_get_browsing_status(packet), 432 avrcp_subevent_browsing_done_get_bluetooth_status(packet)); 433 return; 434 } 435 browsing_uid_counter = avrcp_subevent_browsing_done_get_uid_counter(packet); 436 printf("DONE, browsing_uid_counter %d.\n", browsing_uid_counter); 437 438 switch (browsing_state){ 439 case AVRCP_BROWSING_STATE_W4_GET_PLAYERS: 440 if (media_player_item_index < 0) { 441 printf("Get media players first\n"); 442 break; 443 } 444 printf("Set browsed player\n"); 445 browsing_state = AVRCP_BROWSING_STATE_W4_SET_PLAYER; 446 status = avrcp_browsing_controller_set_browsed_player(browsing_cid, media_player_items[0].player_id); 447 if (status != ERROR_CODE_SUCCESS){ 448 printf("Could not set player, status 0x%02X\n", status); 449 status = AVRCP_BROWSING_STATE_W4_GET_PLAYERS; 450 break; 451 } 452 break; 453 case AVRCP_BROWSING_STATE_W4_SET_PLAYER: 454 browsing_state = AVRCP_BROWSING_STATE_READY; 455 break; 456 default: 457 break; 458 } 459 break; 460 default: 461 break; 462 } 463 464 default: 465 break; 466 } 467 break; 468 469 case AVRCP_BROWSING_DATA_PACKET: 470 browsing_query_active = 1; 471 avrcp_browsing_item_type_t data_type = (avrcp_browsing_item_type_t)packet[pos++]; 472 473 switch (data_type){ 474 case AVRCP_BROWSING_MEDIA_ROOT_FOLDER:{ 475 uint16_t folder_name_length = size - pos; 476 char folder_name[AVRCP_MAX_FOLDER_NAME_SIZE]; 477 memcpy(folder_name, &packet[pos], folder_name_length); 478 folder_name[folder_name_length] = 0; 479 printf("Found root folder: name %s \n", folder_name); 480 break; 481 } 482 483 case AVRCP_BROWSING_FOLDER_ITEM:{ 484 int index = next_folder_index(); 485 memcpy(folders[index].uid, packet+pos, 8); 486 487 uint32_t folder_uid_high = big_endian_read_32(packet, pos); 488 pos += 4; 489 uint32_t folder_uid_low = big_endian_read_32(packet, pos); 490 pos += 4; 491 avrcp_browsing_folder_type_t folder_type = packet[pos++]; 492 uint8_t is_playable = packet[pos++]; 493 uint16_t charset = big_endian_read_16(packet, pos); 494 pos += 2; 495 uint16_t displayable_name_length = big_endian_read_16(packet, pos); 496 pos += 2; 497 498 char value[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 499 memset(value, 0, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN); 500 uint16_t value_len = btstack_min(displayable_name_length, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN - 1); 501 memcpy(value, packet+pos, value_len); 502 503 printf("Folder UID 0x%08" PRIx32 "%08" PRIx32 ", type 0x%02x, is_playable %d, charset 0x%02x, name %s\n", 504 folder_uid_high, folder_uid_low, folder_type, is_playable, charset, value); 505 if (is_playable){ 506 playable_folder_index = index; 507 } 508 memcpy(folders[index].name, value, value_len); 509 folders[index].name_len = value_len; 510 break; 511 } 512 513 case AVRCP_BROWSING_MEDIA_PLAYER_ITEM:{ 514 printf("Received media player item: "); 515 uint16_t player_id = big_endian_read_16(packet, pos); 516 pos += 2; 517 avrcp_browsing_media_player_major_type_t major_type = packet[pos++]; 518 avrcp_browsing_media_player_subtype_t subtype = big_endian_read_32(packet, pos); 519 pos += 4; 520 status = packet[pos++]; 521 uint8_t feature_bitmask[16]; 522 memcpy(feature_bitmask, packet, 16); 523 pos += 16; 524 printf("player ID 0x%04x, major_type %d, subtype %d, status %d\n", player_id, major_type, subtype, status); 525 media_player_items[next_media_player_item_index()].player_id = player_id; 526 break; 527 } 528 529 case AVRCP_BROWSING_MEDIA_ELEMENT_ITEM:{ 530 int index = next_media_element_item_index(); 531 memcpy(media_element_items[index].uid, packet+pos, 8); 532 printf("Received media element item UID (index %d): ", index); 533 534 // uint32_t media_uid_high = big_endian_read_32(packet, pos); 535 pos += 4; 536 // uint32_t media_uid_low = big_endian_read_32(packet, pos+4); 537 pos += 4; 538 539 avrcp_browsing_media_type_t media_type = packet[pos++]; 540 uint16_t charset = big_endian_read_16(packet, pos); 541 pos += 2; 542 uint16_t displayable_name_length = big_endian_read_16(packet, pos); 543 pos += 2; 544 545 char value[AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN]; 546 memset(value, 0, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN); 547 uint16_t value_len = btstack_min(displayable_name_length, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN - 1); 548 memcpy(value, packet+pos, value_len); 549 memcpy(media_element_items[index].name, value, value_len); 550 551 pos += displayable_name_length; 552 // printf("Media UID: 0x%08" PRIx32 "%08" PRIx32 ", media_type 0x%02x, charset 0x%02x, actual len %d, name %s\n", media_uid_high, media_uid_low, media_type, charset, value_len, value); 553 554 printf_hexdump(media_element_items[index].uid, 8); 555 uint8_t num_attributes = packet[pos++]; 556 printf(" Media type 0x%02x, charset 0x%02x, actual len %d, name %s, num attributes %d:\n", media_type, charset, value_len, value, num_attributes); 557 558 avrcp_media_item_context_t media_item_context; 559 for (avrcp_media_item_iterator_init(&media_item_context, size-pos, packet+pos); avrcp_media_item_iterator_has_more(&media_item_context); avrcp_media_item_iterator_next(&media_item_context)){ 560 uint32_t attr_id = avrcp_media_item_iterator_get_attr_id(&media_item_context); 561 uint16_t attr_charset = avrcp_media_item_iterator_get_attr_charset(&media_item_context); 562 uint16_t attr_value_length = avrcp_media_item_iterator_get_attr_value_len(&media_item_context); 563 const uint8_t * attr_value = avrcp_media_item_iterator_get_attr_value(&media_item_context); 564 565 memset(value, 0, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN); 566 value_len = btstack_min(attr_value_length, AVRCP_BROWSING_MAX_BROWSABLE_ITEM_NAME_LEN - 1); 567 memcpy(value, attr_value, value_len); 568 569 printf(" - attr ID 0x%08" PRIx32 ", charset 0x%02x, actual len %d, name %s\n", attr_id, attr_charset, value_len, value); 570 } 571 break; 572 } 573 574 case AVRCP_BROWSING_MEDIA_ELEMENT_ITEM_ATTRIBUTE:{ 575 uint8_t num_attributes = packet[pos++]; 576 printf("Num media attributes %d:\n", num_attributes); 577 avrcp_media_item_context_t media_item_context; 578 for (avrcp_media_item_iterator_init(&media_item_context, size-pos, packet+pos); avrcp_media_item_iterator_has_more(&media_item_context); avrcp_media_item_iterator_next(&media_item_context)){ 579 uint32_t attr_id = avrcp_media_item_iterator_get_attr_id(&media_item_context); 580 uint16_t attr_charset = avrcp_media_item_iterator_get_attr_charset(&media_item_context); 581 uint16_t attr_value_length = avrcp_media_item_iterator_get_attr_value_len(&media_item_context); 582 const uint8_t * attr_value = avrcp_media_item_iterator_get_attr_value(&media_item_context); 583 printf(" - attr ID 0x%08" PRIx32 ", charset 0x%02x, actual len %d, name %s\n", attr_id, attr_charset, attr_value_length, attr_value); 584 } 585 } 586 default: 587 printf("AVRCP browsing: unknown browsable item type 0%02x\n", data_type); 588 break; 589 } 590 break; 591 default: 592 break; 593 } 594 } 595 596 #ifdef HAVE_BTSTACK_STDIN 597 static void show_usage(void){ 598 bd_addr_t iut_address; 599 gap_local_bd_addr(iut_address); 600 printf("\n--- Bluetooth AVRCP Browsing Service Test Console %s ---\n", bd_addr_to_str(iut_address)); 601 printf("c - AVRCP Service create connection to addr %s\n", bd_addr_to_str(device_addr)); 602 printf("C - AVRCP Service disconnect\n"); 603 printf("e - AVRCP Browsing Service create connection to addr %s\n", bd_addr_to_str(device_addr)); 604 printf("E - AVRCP Browsing Service disconnect\n"); 605 606 printf("I - Set first found player as addressed player\n"); 607 printf("O - Set first found player as browsed player\n"); 608 609 printf("p - Get media players\n"); 610 printf("Q - Browse folders\n"); 611 printf("P - Go up one level\n"); 612 printf("W - Go down one level\n"); 613 printf("T - Browse media items\n"); 614 printf("---\n"); 615 } 616 #endif 617 618 619 #ifdef HAVE_BTSTACK_STDIN 620 static void stdin_process(char cmd){ 621 uint8_t status = ERROR_CODE_SUCCESS; 622 623 if (cmd != 'a' && cmd != 'A' && cmd != 'c' && cmd != 'C'){ 624 if (browsing_query_active){ 625 printf("Query active, try later!\n"); 626 return; 627 } 628 } 629 630 switch (cmd){ 631 case 'b': 632 status = a2dp_sink_establish_stream(device_addr, a2dp_local_seid, &a2dp_cid); 633 printf(" - Create AVDTP connection to addr %s, and local seid %d, expected cid 0x%02x.\n", bd_addr_to_str(device_addr), a2dp_local_seid, a2dp_cid); 634 break; 635 case 'B': 636 printf(" - AVDTP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 637 a2dp_sink_disconnect(a2dp_cid); 638 break; 639 640 case 'c': 641 printf(" - Connect to AVRCP Service on addr %s.\n", bd_addr_to_str(device_addr)); 642 status = avrcp_connect(device_addr, &avrcp_cid); 643 break; 644 case 'C': 645 if (avrcp_connected){ 646 printf(" - AVRCP Service disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 647 status = avrcp_disconnect(avrcp_cid); 648 break; 649 } 650 printf("AVRCP Service already disconnected\n"); 651 break; 652 653 case 'e': 654 if (!avrcp_connected) { 655 printf(" You must first connect to AVRCP Service on addr %s.\n", bd_addr_to_str(device_addr)); 656 break; 657 } 658 printf(" - Connect to AVRCP Browsing Service at addr %s.\n", bd_addr_to_str(device_addr)); 659 status = avrcp_browsing_connect(device_addr, ertm_buffer, sizeof(ertm_buffer), &ertm_config, &browsing_cid); 660 break; 661 case 'E': 662 if (browsing_connected){ 663 printf(" - AVRCP Browsing Service disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 664 status = avrcp_browsing_disconnect(browsing_cid); 665 break; 666 } 667 printf("AVRCP Browsing Service already disconnected\n"); 668 break; 669 case '\n': 670 case '\r': 671 break; 672 673 default: 674 if (!browsing_connected){ 675 show_usage(); 676 break; 677 } 678 679 switch (cmd) { 680 case 'I': 681 if (media_player_item_index < 0) { 682 printf("AVRCP Browsing:Get media players first\n"); 683 break; 684 } 685 printf("AVRCP Browsing:Set addressed player\n"); 686 status = avrcp_controller_set_addressed_player(avrcp_cid, media_player_items[0].player_id); 687 break; 688 case 'O': 689 if (media_player_item_index < 0) { 690 printf("AVRCP Browsing:Get media players first\n"); 691 break; 692 } 693 printf("Set browsed player\n"); 694 status = avrcp_browsing_controller_set_browsed_player(browsing_cid, media_player_items[0].player_id); 695 break; 696 case 'p': 697 printf("AVRCP Browsing: get media players\n"); 698 media_player_item_index = -1; 699 status = avrcp_browsing_controller_get_media_players(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL); 700 break; 701 case 'Q': 702 printf("AVRCP Browsing: browse folders\n"); 703 folder_index = -1; 704 status = avrcp_browsing_controller_browse_file_system(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL); 705 break; 706 case 'P': 707 printf("AVRCP Browsing: browse media items\n"); 708 avrcp_browsing_controller_browse_media(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL); 709 break; 710 case 'W': 711 printf("AVRCP Browsing: go up one level\n"); 712 status = avrcp_browsing_controller_go_up_one_level(browsing_cid); 713 folder_index = -1; 714 break; 715 case 'T': 716 if (folder_index < 0 && !parent_folder_set){ 717 printf("AVRCP Browsing: no folders available\n"); 718 break; 719 } 720 if (!parent_folder_set){ 721 parent_folder_set = 1; 722 memcpy(parent_folder_name, folders[0].name, folders[0].name_len); 723 memcpy(parent_folder_uid, folders[0].uid, 8); 724 } 725 printf("AVRCP Browsing: go down one level of %s\n", (char *)parent_folder_name); 726 status = avrcp_browsing_controller_go_down_one_level(browsing_cid, parent_folder_uid); 727 folder_index = -1; 728 break; 729 default: 730 show_usage(); 731 break; 732 } 733 break; 734 } 735 736 if (status != ERROR_CODE_SUCCESS){ 737 printf("AVRCP Browsing: Could not perform command, status 0x%2x\n", status); 738 } 739 } 740 #endif 741 /* EXAMPLE_END */ 742