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__ "a2dp_sink_demo.c" 39 40 /* 41 * a2dp_sink_demo.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(a2dp_sink_demo): Receive audio stream and control its playback. 46 * 47 * @text This A2DP Sink example demonstrates how to use the A2DP Sink service to 48 * receive an audio data stream from a remote A2DP Source device. In addition, 49 * the AVRCP Controller is used to get information on currently played media, 50 * such are title, artist and album, as well as to control the playback, 51 * i.e. to play, stop, repeat, etc. 52 * 53 * @test To test with a remote device, e.g. a mobile phone, 54 * pair from the remote device with the demo, then start playing music on the remote device. 55 * Alternatively, set the device_addr_string to the Bluetooth address of your 56 * remote device in the code, and call connect from the UI. 57 * 58 * @test To controll the playback, tap SPACE on the console to show the available 59 * AVRCP commands. 60 */ 61 // ***************************************************************************** 62 63 #include <inttypes.h> 64 #include <stdint.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 69 #include "btstack.h" 70 #include "btstack_resample.h" 71 72 //#define AVRCP_BROWSING_ENABLED 73 74 #ifdef HAVE_BTSTACK_STDIN 75 #include "btstack_stdin.h" 76 #endif 77 78 #include "btstack_ring_buffer.h" 79 80 #ifdef HAVE_POSIX_FILE_IO 81 #include "wav_util.h" 82 #define STORE_TO_SBC_FILE 83 #define STORE_TO_WAV_FILE 84 #endif 85 86 #define NUM_CHANNELS 2 87 #define BYTES_PER_FRAME (2*NUM_CHANNELS) 88 #define MAX_SBC_FRAME_SIZE 120 89 90 // SBC Decoder for WAV file or live playback 91 static btstack_sbc_decoder_state_t state; 92 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 93 94 // ring buffer for SBC Frames 95 // below 30: add samples, 30-40: fine, above 40: drop samples 96 #define OPTIMAL_FRAMES_MIN 30 97 #define OPTIMAL_FRAMES_MAX 40 98 #define ADDITIONAL_FRAMES 20 99 static uint8_t sbc_frame_storage[(OPTIMAL_FRAMES_MAX + ADDITIONAL_FRAMES) * MAX_SBC_FRAME_SIZE]; 100 static btstack_ring_buffer_t sbc_frame_ring_buffer; 101 static unsigned int sbc_frame_size; 102 103 // rest buffer for not fully used sbc frames, with additional frames for resampling 104 static uint8_t decoded_audio_storage[(128+16) * BYTES_PER_FRAME]; 105 static btstack_ring_buffer_t decoded_audio_ring_buffer; 106 107 // 108 static int audio_stream_started; 109 110 // temp storage of lower-layer request 111 static int16_t * request_buffer; 112 static int request_frames; 113 114 #define STORE_FROM_PLAYBACK 115 116 // WAV File 117 #ifdef STORE_TO_WAV_FILE 118 static int frame_count = 0; 119 static char * wav_filename = "avdtp_sink.wav"; 120 #endif 121 122 #ifdef STORE_TO_SBC_FILE 123 static FILE * sbc_file; 124 static char * sbc_filename = "avdtp_sink.sbc"; 125 #endif 126 127 static int volume_percentage = 0; 128 129 typedef struct { 130 // bitmaps 131 uint8_t sampling_frequency_bitmap; 132 uint8_t channel_mode_bitmap; 133 uint8_t block_length_bitmap; 134 uint8_t subbands_bitmap; 135 uint8_t allocation_method_bitmap; 136 uint8_t min_bitpool_value; 137 uint8_t max_bitpool_value; 138 } adtvp_media_codec_information_sbc_t; 139 140 typedef struct { 141 int reconfigure; 142 int num_channels; 143 int sampling_frequency; 144 int channel_mode; 145 int block_length; 146 int subbands; 147 int allocation_method; 148 int min_bitpool_value; 149 int max_bitpool_value; 150 int frames_per_buffer; 151 } avdtp_media_codec_configuration_sbc_t; 152 153 static uint8_t events_num = 1; 154 155 static uint8_t events[] = { 156 AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED 157 }; 158 159 static uint8_t companies_num = 1; 160 static uint8_t companies[] = { 161 0x00, 0x19, 0x58 //BT SIG registered CompanyID 162 }; 163 #ifdef HAVE_BTSTACK_STDIN 164 // pts: 165 static const char * device_addr_string = "00:1B:DC:08:E2:72"; 166 // mac 2013: static const char * device_addr_string = "84:38:35:65:d1:15"; 167 // iPhone 5S: 168 // static const char * device_addr_string = "54:E4:3A:26:A2:39"; 169 #endif 170 171 static uint8_t sdp_avdtp_sink_service_buffer[150]; 172 static uint8_t sdp_avrcp_target_service_buffer[150]; 173 static avdtp_media_codec_configuration_sbc_t sbc_configuration; 174 static uint16_t a2dp_cid = 0; 175 static uint8_t a2dp_local_seid = 0; 176 static uint8_t value[100]; 177 178 static btstack_packet_callback_registration_t hci_event_callback_registration; 179 180 static int media_initialized = 0; 181 182 #ifdef HAVE_BTSTACK_STDIN 183 static bd_addr_t device_addr; 184 #endif 185 186 static uint16_t avrcp_controller_cid = 0; 187 static uint16_t avrcp_target_cid = 0; 188 static uint8_t avrcp_connected = 0; 189 static uint8_t sdp_avrcp_controller_service_buffer[200]; 190 191 static uint8_t media_sbc_codec_capabilities[] = { 192 0xFF,//(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 193 0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 194 2, 53 195 }; 196 197 static uint8_t media_sbc_codec_configuration[] = { 198 (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 199 (AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 200 2, 53 201 }; 202 203 static btstack_resample_t resample_instance; 204 205 /* @section Main Application Setup 206 * 207 * @text The Listing MainConfiguration shows how to setup AD2P Sink and AVRCP controller services. 208 * To announce A2DP Sink and AVRCP Controller services, you need to create corresponding 209 * SDP records and register them with the SDP service. 210 * You'll also need to register several packet handlers: 211 * - a2dp_sink_packet_handler - handles events on stream connection status (established, released), the media codec configuration, and, the status of the stream itself (opened, paused, stopped). 212 * - handle_l2cap_media_data_packet - used to receive streaming data. If STORE_TO_WAV_FILE directive (check btstack_config.h) is used, the SBC decoder will be used to decode the SBC data into PCM frames. The resulting PCM frames are then processed in the SBC Decoder callback. 213 * - stdin_process callback - used to trigger AVRCP commands to the A2DP Source device, such are get now playing info, start, stop, volume control. Requires HAVE_BTSTACK_STDIN. 214 * - avrcp_controller_packet_handler - used to receive answers for AVRCP commands, 215 * 216 * @text Note, currently only the SBC codec is supported. 217 * If you want to store the audio data in a file, you'll need to define STORE_TO_WAV_FILE. 218 * If STORE_TO_WAV_FILE directive is defined, the SBC decoder needs to get initialized when a2dp_sink_packet_handler receives event A2DP_SUBEVENT_STREAM_STARTED. 219 * The initialization of the SBC decoder requires a callback that handles PCM data: 220 * - handle_pcm_data - handles PCM audio frames. Here, they are stored a in wav file if STORE_TO_WAV_FILE is defined, and/or played using the audio library. 221 */ 222 223 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP Controller services */ 224 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size); 225 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 226 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 227 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 228 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size); 229 230 static int a2dp_and_avrcp_setup(void){ 231 232 l2cap_init(); 233 // Initialize AVDTP Sink 234 a2dp_sink_init(); 235 a2dp_sink_register_packet_handler(&a2dp_sink_packet_handler); 236 a2dp_sink_register_media_handler(&handle_l2cap_media_data_packet); 237 238 avdtp_stream_endpoint_t * local_stream_endpoint = a2dp_sink_create_stream_endpoint(AVDTP_AUDIO, 239 AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), 240 media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration)); 241 if (!local_stream_endpoint){ 242 printf("A2DP Sink: not enough memory to create local stream endpoint\n"); 243 return 1; 244 } 245 a2dp_local_seid = avdtp_local_seid(local_stream_endpoint); 246 // Initialize AVRCP Controller 247 avrcp_controller_init(); 248 avrcp_controller_register_packet_handler(&avrcp_controller_packet_handler); 249 250 // Initialize AVRCP Target 251 avrcp_target_init(); 252 avrcp_target_register_packet_handler(&avrcp_target_packet_handler); 253 254 // Initialize SDP 255 sdp_init(); 256 // setup AVDTP sink 257 memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer)); 258 a2dp_sink_create_sdp_record(sdp_avdtp_sink_service_buffer, 0x10001, 1, NULL, NULL); 259 sdp_register_service(sdp_avdtp_sink_service_buffer); 260 261 // setup AVRCP Controller 262 memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer)); 263 uint16_t controller_supported_features = (1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_CATEGORY_PLAYER_OR_RECORDER); 264 #ifdef AVRCP_BROWSING_ENABLED 265 controller_supported_features |= (1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_BROWSING); 266 #endif 267 avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, 0x10002, controller_supported_features, NULL, NULL); 268 sdp_register_service(sdp_avrcp_controller_service_buffer); 269 270 // setup AVRCP Target 271 memset(sdp_avrcp_target_service_buffer, 0, sizeof(sdp_avrcp_target_service_buffer)); 272 uint16_t target_supported_features = (1 << AVRCP_TARGET_SUPPORTED_FEATURE_CATEGORY_MONITOR_OR_AMPLIFIER); 273 avrcp_target_create_sdp_record(sdp_avrcp_target_service_buffer, 0x10003, target_supported_features, NULL, NULL); 274 sdp_register_service(sdp_avrcp_target_service_buffer); 275 276 gap_set_local_name("A2DP Sink Demo 00:00:00:00:00:00"); 277 gap_discoverable_control(1); 278 gap_set_class_of_device(0x200408); 279 280 /* Register for HCI events */ 281 hci_event_callback_registration.callback = &hci_packet_handler; 282 hci_add_event_handler(&hci_event_callback_registration); 283 284 #ifdef HAVE_POSIX_FILE_IO 285 if (!btstack_audio_sink_get_instance()){ 286 printf("No audio playback.\n"); 287 } else { 288 printf("Audio playback supported.\n"); 289 } 290 #ifdef STORE_TO_WAV_FILE 291 printf("Audio will be stored to \'%s\' file.\n", wav_filename); 292 #endif 293 #endif 294 return 0; 295 } 296 297 static void playback_handler(int16_t * buffer, uint16_t num_frames){ 298 299 #ifdef STORE_TO_WAV_FILE 300 int wav_samples = num_frames * NUM_CHANNELS; 301 int16_t * wav_buffer = buffer; 302 #endif 303 304 // called from lower-layer but guaranteed to be on main thread 305 if (sbc_frame_size == 0){ 306 memset(buffer, 0, num_frames * BYTES_PER_FRAME); 307 return; 308 } 309 310 // first fill from resampled audio 311 uint32_t bytes_read; 312 btstack_ring_buffer_read(&decoded_audio_ring_buffer, (uint8_t *) buffer, num_frames * BYTES_PER_FRAME, &bytes_read); 313 buffer += bytes_read / NUM_CHANNELS; 314 num_frames -= bytes_read / BYTES_PER_FRAME; 315 316 // then start decoding sbc frames using request_* globals 317 request_buffer = buffer; 318 request_frames = num_frames; 319 while (request_frames && btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) >= sbc_frame_size){ 320 // decode frame 321 uint8_t sbc_frame[MAX_SBC_FRAME_SIZE]; 322 btstack_ring_buffer_read(&sbc_frame_ring_buffer, sbc_frame, sbc_frame_size, &bytes_read); 323 btstack_sbc_decoder_process_data(&state, 0, sbc_frame, sbc_frame_size); 324 } 325 326 #ifdef STORE_TO_WAV_FILE 327 wav_writer_write_int16(wav_samples, wav_buffer); 328 #endif 329 } 330 331 static void handle_pcm_data(int16_t * data, int num_frames, int num_channels, int sample_rate, void * context){ 332 UNUSED(sample_rate); 333 UNUSED(context); 334 UNUSED(num_channels); // must be stereo == 2 335 336 const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 337 if (!audio_sink){ 338 #ifdef STORE_TO_WAV_FILE 339 wav_writer_write_int16(num_frames * NUM_CHANNELS, data); 340 #endif 341 return; 342 } 343 344 // resample into request buffer - add some additional space for resampling 345 int16_t output_buffer[(128+16) * NUM_CHANNELS]; // 16 * 8 * 2 346 uint32_t resampled_frames = btstack_resample_block(&resample_instance, data, num_frames, output_buffer); 347 348 // store data in btstack_audio buffer first 349 int frames_to_copy = btstack_min(resampled_frames, request_frames); 350 memcpy(request_buffer, output_buffer, frames_to_copy * BYTES_PER_FRAME); 351 request_frames -= frames_to_copy; 352 request_buffer += frames_to_copy * NUM_CHANNELS; 353 354 // and rest in ring buffer 355 int frames_to_store = resampled_frames - frames_to_copy; 356 if (frames_to_store){ 357 int status = btstack_ring_buffer_write(&decoded_audio_ring_buffer, (uint8_t *)&output_buffer[frames_to_copy * NUM_CHANNELS], frames_to_store * BYTES_PER_FRAME); 358 if (status){ 359 printf("Error storing samples in PCM ring buffer!!!\n"); 360 } 361 } 362 } 363 364 static int media_processing_init(avdtp_media_codec_configuration_sbc_t configuration){ 365 if (media_initialized) return 0; 366 367 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 368 369 #ifdef STORE_TO_WAV_FILE 370 wav_writer_open(wav_filename, configuration.num_channels, configuration.sampling_frequency); 371 #endif 372 373 #ifdef STORE_TO_SBC_FILE 374 sbc_file = fopen(sbc_filename, "wb"); 375 #endif 376 377 btstack_ring_buffer_init(&sbc_frame_ring_buffer, sbc_frame_storage, sizeof(sbc_frame_storage)); 378 btstack_ring_buffer_init(&decoded_audio_ring_buffer, decoded_audio_storage, sizeof(decoded_audio_storage)); 379 btstack_resample_init(&resample_instance, configuration.num_channels); 380 381 // setup audio playback 382 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 383 if (audio){ 384 audio->init(NUM_CHANNELS, configuration.sampling_frequency, &playback_handler); 385 } 386 387 audio_stream_started = 0; 388 media_initialized = 1; 389 return 0; 390 } 391 392 static void media_processing_start(void){ 393 if (!media_initialized) return; 394 // setup audio playback 395 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 396 if (audio){ 397 audio->start_stream(); 398 } 399 audio_stream_started = 1; 400 } 401 402 static void media_processing_pause(void){ 403 if (!media_initialized) return; 404 // stop audio playback 405 audio_stream_started = 0; 406 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 407 if (audio){ 408 audio->stop_stream(); 409 } 410 } 411 412 static void media_processing_close(void){ 413 if (!media_initialized) return; 414 media_initialized = 0; 415 audio_stream_started = 0; 416 sbc_frame_size = 0; 417 418 #ifdef STORE_TO_WAV_FILE 419 wav_writer_close(); 420 int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 421 422 printf("WAV Writer: Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, state.good_frames_nr, total_frames_nr - state.good_frames_nr); 423 printf("WAV Writer: Written %d frames to wav file: %s\n", frame_count, wav_filename); 424 #endif 425 426 #ifdef STORE_TO_SBC_FILE 427 fclose(sbc_file); 428 #endif 429 430 // stop audio playback 431 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 432 if (audio){ 433 printf("close stream\n"); 434 audio->close(); 435 } 436 } 437 438 /* @section Handle Media Data Packet 439 * 440 * @text Media data packets, in this case the audio data, are received through the handle_l2cap_media_data_packet callback. 441 * Currently, only the SBC media codec is supported. Hence, the media data consists of the media packet header and the SBC packet. 442 * The SBC frame will be stored in a ring buffer for later processing (instead of decoding it to PCM right away which would require a much larger buffer) 443 * If the audio stream wasn't started already and there are enough SBC frames in the ring buffer, start playback. 444 */ 445 446 static int read_media_data_header(uint8_t * packet, int size, int * offset, avdtp_media_packet_header_t * media_header); 447 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header); 448 449 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size){ 450 UNUSED(seid); 451 int pos = 0; 452 453 avdtp_media_packet_header_t media_header; 454 if (!read_media_data_header(packet, size, &pos, &media_header)) return; 455 456 avdtp_sbc_codec_header_t sbc_header; 457 if (!read_sbc_header(packet, size, &pos, &sbc_header)) return; 458 459 #ifdef STORE_TO_SBC_FILE 460 fwrite(packet+pos, size-pos, 1, sbc_file); 461 #endif 462 463 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 464 // process data right away if there's no audio implementation active, e.g. on posix systems to store as .wav 465 if (!audio){ 466 btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos); 467 return; 468 } 469 470 // store sbc frame size for buffer management 471 sbc_frame_size = (size-pos)/ sbc_header.num_frames; 472 473 int status = btstack_ring_buffer_write(&sbc_frame_ring_buffer, packet+pos, size-pos); 474 if (status){ 475 printf("Error storing samples in SBC ring buffer!!!\n"); 476 } 477 478 // decide on audio sync drift based on number of sbc frames in queue 479 int sbc_frames_in_buffer = btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) / sbc_frame_size; 480 uint32_t resampling_factor; 481 482 // nomimal factor (fixed-point 2^16) and compensation offset 483 uint32_t nomimal_factor = 0x10000; 484 uint32_t compensation = 0x00100; 485 486 if (sbc_frames_in_buffer < OPTIMAL_FRAMES_MIN){ 487 resampling_factor = nomimal_factor - compensation; // stretch samples 488 } else if (sbc_frames_in_buffer <= OPTIMAL_FRAMES_MAX){ 489 resampling_factor = nomimal_factor; // nothing to do 490 } else { 491 resampling_factor = nomimal_factor + compensation; // compress samples 492 } 493 494 btstack_resample_set_factor(&resample_instance, resampling_factor); 495 496 // dump 497 // printf("%6u %03u %05x\n", (int) btstack_run_loop_get_time_ms(), sbc_frames_in_buffer, resampling_factor); 498 // log_info("%03u %05x", sbc_frames_in_buffer, resampling_factor); 499 500 // start stream if enough frames buffered 501 if (!audio_stream_started && sbc_frames_in_buffer >= OPTIMAL_FRAMES_MIN){ 502 audio_stream_started = 1; 503 // setup audio playback 504 if (audio){ 505 audio->start_stream(); 506 } 507 } 508 } 509 510 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header){ 511 int sbc_header_len = 12; // without crc 512 int pos = *offset; 513 514 if (size - pos < sbc_header_len){ 515 printf("Not enough data to read SBC header, expected %d, received %d\n", sbc_header_len, size-pos); 516 return 0; 517 } 518 519 sbc_header->fragmentation = get_bit16(packet[pos], 7); 520 sbc_header->starting_packet = get_bit16(packet[pos], 6); 521 sbc_header->last_packet = get_bit16(packet[pos], 5); 522 sbc_header->num_frames = packet[pos] & 0x0f; 523 pos++; 524 // printf("SBC HEADER: num_frames %u, fragmented %u, start %u, stop %u\n", sbc_header.num_frames, sbc_header.fragmentation, sbc_header.starting_packet, sbc_header.last_packet); 525 *offset = pos; 526 return 1; 527 } 528 529 static int read_media_data_header(uint8_t *packet, int size, int *offset, avdtp_media_packet_header_t *media_header){ 530 int media_header_len = 12; // without crc 531 int pos = *offset; 532 533 if (size - pos < media_header_len){ 534 printf("Not enough data to read media packet header, expected %d, received %d\n", media_header_len, size-pos); 535 return 0; 536 } 537 538 media_header->version = packet[pos] & 0x03; 539 media_header->padding = get_bit16(packet[pos],2); 540 media_header->extension = get_bit16(packet[pos],3); 541 media_header->csrc_count = (packet[pos] >> 4) & 0x0F; 542 pos++; 543 544 media_header->marker = get_bit16(packet[pos],0); 545 media_header->payload_type = (packet[pos] >> 1) & 0x7F; 546 pos++; 547 548 media_header->sequence_number = big_endian_read_16(packet, pos); 549 pos+=2; 550 551 media_header->timestamp = big_endian_read_32(packet, pos); 552 pos+=4; 553 554 media_header->synchronization_source = big_endian_read_32(packet, pos); 555 pos+=4; 556 *offset = pos; 557 return 1; 558 } 559 560 static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){ 561 printf(" - num_channels: %d\n", configuration.num_channels); 562 printf(" - sampling_frequency: %d\n", configuration.sampling_frequency); 563 printf(" - channel_mode: %d\n", configuration.channel_mode); 564 printf(" - block_length: %d\n", configuration.block_length); 565 printf(" - subbands: %d\n", configuration.subbands); 566 printf(" - allocation_method: %d\n", configuration.allocation_method); 567 printf(" - bitpool_value [%d, %d] \n", configuration.min_bitpool_value, configuration.max_bitpool_value); 568 printf("\n"); 569 } 570 571 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 572 UNUSED(channel); 573 UNUSED(size); 574 uint16_t local_cid; 575 uint8_t status = 0xFF; 576 bd_addr_t adress; 577 578 if (packet_type != HCI_EVENT_PACKET) return; 579 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 580 switch (packet[2]){ 581 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { 582 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 583 if (avrcp_controller_cid != 0 && avrcp_controller_cid != local_cid) { 584 printf("AVRCP Controller: Connection failed, expected 0x%02X cid, received 0x%02X\n", avrcp_controller_cid, local_cid); 585 return; 586 } 587 588 status = avrcp_subevent_connection_established_get_status(packet); 589 if (status != ERROR_CODE_SUCCESS){ 590 printf("AVRCP Controller: Connection failed: status 0x%02x\n", status); 591 avrcp_controller_cid = 0; 592 return; 593 } 594 595 avrcp_controller_cid = local_cid; 596 avrcp_connected = 1; 597 avrcp_subevent_connection_established_get_bd_addr(packet, adress); 598 printf("AVRCP Controller: Connected to %s, cid 0x%02x\n", bd_addr_to_str(adress), avrcp_controller_cid); 599 600 // automatically enable notifications 601 avrcp_controller_enable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED); 602 avrcp_controller_enable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED); 603 avrcp_controller_enable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 604 return; 605 } 606 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 607 printf("AVRCP Controller: Channel released: cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 608 avrcp_controller_cid = 0; 609 avrcp_connected = 0; 610 return; 611 default: 612 break; 613 } 614 615 status = packet[5]; 616 if (!avrcp_controller_cid) return; 617 618 // ignore INTERIM status 619 if (status == AVRCP_CTYPE_RESPONSE_INTERIM){ 620 switch (packet[2]){ 621 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED:{ 622 uint32_t playback_position_ms = avrcp_subevent_notification_playback_pos_changed_get_playback_position_ms(packet); 623 if (playback_position_ms == AVRCP_NO_TRACK_SELECTED_PLAYBACK_POSITION_CHANGED){ 624 printf("AVRCP Controller: playback position changed, no track is selected\n"); 625 } 626 break; 627 } 628 default: 629 break; 630 } 631 return; 632 } 633 634 memset(value, 0, sizeof(value)); 635 switch (packet[2]){ 636 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED: 637 printf("AVRCP Controller: Playback position changed, position %d ms\n", (unsigned int) avrcp_subevent_notification_playback_pos_changed_get_playback_position_ms(packet)); 638 break; 639 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED: 640 printf("AVRCP Controller: Playback status changed %s\n", avrcp_play_status2str(avrcp_subevent_notification_playback_status_changed_get_play_status(packet))); 641 return; 642 case AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED: 643 printf("AVRCP Controller: Playing content changed\n"); 644 return; 645 case AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED: 646 printf("AVRCP Controller: Track changed\n"); 647 return; 648 case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED: 649 printf("AVRCP Controller: Absolute volume changed %d\n", avrcp_subevent_notification_volume_changed_get_absolute_volume(packet)); 650 return; 651 case AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED: 652 printf("AVRCP Controller: Changed\n"); 653 return; 654 case AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE:{ 655 uint8_t shuffle_mode = avrcp_subevent_shuffle_and_repeat_mode_get_shuffle_mode(packet); 656 uint8_t repeat_mode = avrcp_subevent_shuffle_and_repeat_mode_get_repeat_mode(packet); 657 printf("AVRCP Controller: %s, %s\n", avrcp_shuffle2str(shuffle_mode), avrcp_repeat2str(repeat_mode)); 658 break; 659 } 660 case AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO: 661 printf("AVRCP Controller: Track: %d\n", avrcp_subevent_now_playing_track_info_get_track(packet)); 662 break; 663 664 case AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO: 665 printf("AVRCP Controller: Total Tracks: %d\n", avrcp_subevent_now_playing_total_tracks_info_get_total_tracks(packet)); 666 break; 667 668 case AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO: 669 if (avrcp_subevent_now_playing_title_info_get_value_len(packet) > 0){ 670 memcpy(value, avrcp_subevent_now_playing_title_info_get_value(packet), avrcp_subevent_now_playing_title_info_get_value_len(packet)); 671 printf("AVRCP Controller: Title: %s\n", value); 672 } 673 break; 674 675 case AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO: 676 if (avrcp_subevent_now_playing_artist_info_get_value_len(packet) > 0){ 677 memcpy(value, avrcp_subevent_now_playing_artist_info_get_value(packet), avrcp_subevent_now_playing_artist_info_get_value_len(packet)); 678 printf("AVRCP Controller: Artist: %s\n", value); 679 } 680 break; 681 682 case AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO: 683 if (avrcp_subevent_now_playing_album_info_get_value_len(packet) > 0){ 684 memcpy(value, avrcp_subevent_now_playing_album_info_get_value(packet), avrcp_subevent_now_playing_album_info_get_value_len(packet)); 685 printf("AVRCP Controller: Album: %s\n", value); 686 } 687 break; 688 689 case AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO: 690 if (avrcp_subevent_now_playing_genre_info_get_value_len(packet) > 0){ 691 memcpy(value, avrcp_subevent_now_playing_genre_info_get_value(packet), avrcp_subevent_now_playing_genre_info_get_value_len(packet)); 692 printf("AVRCP Controller: Genre: %s\n", value); 693 } 694 break; 695 696 case AVRCP_SUBEVENT_PLAY_STATUS: 697 printf("AVRCP Controller: Song length %"PRIu32" ms, Song position %"PRIu32" ms, Play status %s\n", 698 avrcp_subevent_play_status_get_song_length(packet), 699 avrcp_subevent_play_status_get_song_position(packet), 700 avrcp_play_status2str(avrcp_subevent_play_status_get_play_status(packet))); 701 break; 702 703 case AVRCP_SUBEVENT_OPERATION_COMPLETE: 704 printf("AVRCP Controller: %s done\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 705 break; 706 707 case AVRCP_SUBEVENT_OPERATION_START: 708 printf("AVRCP Controller: Start %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 709 break; 710 711 default: 712 printf("AVRCP Controller: Event 0x%02x is not parsed\n", packet[2]); 713 break; 714 } 715 } 716 717 718 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 719 UNUSED(channel); 720 UNUSED(size); 721 bd_addr_t address; 722 uint16_t local_cid; 723 uint8_t status = ERROR_CODE_SUCCESS; 724 725 if (packet_type != HCI_EVENT_PACKET) return; 726 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 727 728 int volume; 729 730 switch (packet[2]){ 731 case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED: 732 volume = avrcp_subevent_notification_volume_changed_get_absolute_volume(packet); 733 volume_percentage = volume * 100 / 127; 734 printf("AVRCP Target : Volume set to %d%% (%d)\n", volume_percentage, volume); 735 break; 736 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: 737 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 738 if (avrcp_target_cid != 0 && avrcp_target_cid != local_cid) { 739 printf("AVRCP Target : Connection failed, expected 0x%02X cid, received 0x%02X\n", avrcp_target_cid, local_cid); 740 return; 741 } 742 743 status = avrcp_subevent_connection_established_get_status(packet); 744 if (status != ERROR_CODE_SUCCESS){ 745 printf("AVRCP Target : Connection failed: status 0x%02x\n", status); 746 avrcp_target_cid = 0; 747 return; 748 } 749 avrcp_target_cid = local_cid; 750 avrcp_subevent_connection_established_get_bd_addr(packet, address); 751 printf("AVRCP Target : Connected to %s, cid 0x%02x\n", bd_addr_to_str(address), avrcp_target_cid); 752 break; 753 case AVRCP_SUBEVENT_EVENT_IDS_QUERY: 754 status = avrcp_target_supported_events(avrcp_target_cid, events_num, events, sizeof(events)); 755 break; 756 case AVRCP_SUBEVENT_COMPANY_IDS_QUERY: 757 status = avrcp_target_supported_companies(avrcp_target_cid, companies_num, companies, sizeof(companies)); 758 break; 759 case AVRCP_SUBEVENT_OPERATION:{ 760 avrcp_operation_id_t operation_id = avrcp_subevent_operation_get_operation_id(packet); 761 // if (!media_tracker.connected) break; 762 switch (operation_id){ 763 case AVRCP_OPERATION_ID_PLAY: 764 printf("AVRCP Target : PLAY\n"); 765 break; 766 case AVRCP_OPERATION_ID_PAUSE: 767 printf("AVRCP Target : PAUSE\n"); 768 break; 769 case AVRCP_OPERATION_ID_STOP: 770 printf("AVRCP Target : STOP\n"); 771 break; 772 case AVRCP_OPERATION_ID_REWIND: 773 printf("AVRCP Target : REWIND\n"); 774 break; 775 case AVRCP_OPERATION_ID_FAST_FORWARD: 776 printf("AVRCP Target : FAST_FORWARD\n"); 777 break; 778 case AVRCP_OPERATION_ID_FORWARD: 779 printf("AVRCP Target : FORWARD\n"); 780 break; 781 case AVRCP_OPERATION_ID_BACKWARD: 782 printf("AVRCP Target : BACKWARD\n"); 783 break; 784 case AVRCP_OPERATION_ID_SKIP: 785 printf("AVRCP Target : SKIP\n"); 786 break; 787 case AVRCP_OPERATION_ID_MUTE: 788 printf("AVRCP Target : MUTE\n"); 789 break; 790 case AVRCP_OPERATION_ID_CHANNEL_UP: 791 printf("AVRCP Target : CHANNEL_UP\n"); 792 break; 793 case AVRCP_OPERATION_ID_CHANNEL_DOWN: 794 printf("AVRCP Target : CHANNEL_DOWN\n"); 795 break; 796 case AVRCP_OPERATION_ID_SELECT: 797 printf("AVRCP Target : SELECT\n"); 798 break; 799 case AVRCP_OPERATION_ID_UP: 800 printf("AVRCP Target : UP\n"); 801 break; 802 case AVRCP_OPERATION_ID_DOWN: 803 printf("AVRCP Target : DOWN\n"); 804 break; 805 case AVRCP_OPERATION_ID_LEFT: 806 printf("AVRCP Target : LEFT\n"); 807 break; 808 case AVRCP_OPERATION_ID_RIGHT: 809 printf("AVRCP Target : RIGTH\n"); 810 break; 811 case AVRCP_OPERATION_ID_ROOT_MENU: 812 printf("AVRCP Target : ROOT_MENU\n"); 813 break; 814 default: 815 return; 816 } 817 break; 818 } 819 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 820 printf("AVRCP Target : Disconnected, cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 821 return; 822 default: 823 printf("AVRCP Target : Event 0x%02x is not parsed\n", packet[2]); 824 break; 825 } 826 } 827 828 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 829 UNUSED(channel); 830 UNUSED(size); 831 if (packet_type != HCI_EVENT_PACKET) return; 832 if (hci_event_packet_get_type(packet) == HCI_EVENT_PIN_CODE_REQUEST) { 833 bd_addr_t address; 834 printf("Pin code request - using '0000'\n"); 835 hci_event_pin_code_request_get_bd_addr(packet, address); 836 gap_pin_code_response(address, "0000"); 837 } 838 } 839 840 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 841 UNUSED(channel); 842 UNUSED(size); 843 uint16_t cid; 844 bd_addr_t address; 845 uint8_t status; 846 847 if (packet_type != HCI_EVENT_PACKET) return; 848 if (hci_event_packet_get_type(packet) != HCI_EVENT_A2DP_META) return; 849 850 switch (packet[2]){ 851 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 852 printf("A2DP Sink : Received non SBC codec - not implemented\n"); 853 break; 854 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ 855 printf("A2DP Sink : Received SBC codec configuration\n"); 856 sbc_configuration.reconfigure = a2dp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 857 sbc_configuration.num_channels = a2dp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet); 858 sbc_configuration.sampling_frequency = a2dp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); 859 sbc_configuration.channel_mode = a2dp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet); 860 sbc_configuration.block_length = a2dp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet); 861 sbc_configuration.subbands = a2dp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet); 862 sbc_configuration.allocation_method = a2dp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet); 863 sbc_configuration.min_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); 864 sbc_configuration.max_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); 865 sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length; 866 dump_sbc_configuration(sbc_configuration); 867 868 if (sbc_configuration.reconfigure){ 869 media_processing_close(); 870 } 871 // prepare media processing 872 media_processing_init(sbc_configuration); 873 break; 874 } 875 case A2DP_SUBEVENT_STREAM_ESTABLISHED: 876 a2dp_subevent_stream_established_get_bd_addr(packet, address); 877 status = a2dp_subevent_stream_established_get_status(packet); 878 cid = a2dp_subevent_stream_established_get_a2dp_cid(packet); 879 if (!a2dp_cid){ 880 // incoming connection 881 a2dp_cid = cid; 882 } else if (cid != a2dp_cid) { 883 break; 884 } 885 if (status){ 886 printf("A2DP Sink : Streaming connection failed, status 0x%02x\n", status); 887 break; 888 } 889 890 memcpy(device_addr, address, 6); 891 a2dp_local_seid = a2dp_subevent_stream_established_get_local_seid(packet); 892 printf("A2DP Sink : Streaming connection is established, address %s, cid 0x%02X, local seid %d\n", bd_addr_to_str(address), a2dp_cid, a2dp_local_seid); 893 break; 894 895 case A2DP_SUBEVENT_STREAM_STARTED: 896 cid = a2dp_subevent_stream_started_get_a2dp_cid(packet); 897 if (cid != a2dp_cid) break; 898 a2dp_local_seid = a2dp_subevent_stream_started_get_local_seid(packet); 899 printf("A2DP Sink : Stream started\n"); 900 media_processing_start(); 901 break; 902 903 case A2DP_SUBEVENT_STREAM_SUSPENDED: 904 cid = a2dp_subevent_stream_suspended_get_a2dp_cid(packet); 905 if (cid != a2dp_cid) break; 906 a2dp_local_seid = a2dp_subevent_stream_suspended_get_local_seid(packet); 907 printf("A2DP Sink : Stream paused\n"); 908 media_processing_pause(); 909 break; 910 911 case A2DP_SUBEVENT_STREAM_RELEASED: 912 a2dp_local_seid = a2dp_subevent_stream_released_get_local_seid(packet); 913 printf("A2DP Sink : Stream released\n"); 914 media_processing_close(); 915 break; 916 case A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 917 cid = a2dp_subevent_signaling_connection_released_get_a2dp_cid(packet); 918 printf("A2DP Sink : Signaling connection released\n"); 919 media_processing_close(); 920 break; 921 default: 922 printf("A2DP Sink : Not parsed 0x%02x\n", packet[2]); 923 break; 924 } 925 } 926 927 #ifdef HAVE_BTSTACK_STDIN 928 static void show_usage(void){ 929 bd_addr_t iut_address; 930 gap_local_bd_addr(iut_address); 931 printf("\n--- Bluetooth AVDTP Sink/AVRCP Connection Test Console %s ---\n", bd_addr_to_str(iut_address)); 932 printf("b - AVDTP Sink create connection to addr %s\n", bd_addr_to_str(device_addr)); 933 printf("B - AVDTP Sink disconnect\n"); 934 printf("c - AVRCP Controller create connection to addr %s\n", bd_addr_to_str(device_addr)); 935 printf("C - AVRCP Controller disconnect\n"); 936 printf("d - AVRCP Target create connection to addr %s\n", bd_addr_to_str(device_addr)); 937 printf("D - AVRCP Target disconnect\n"); 938 939 printf("w - delay report\n"); 940 941 printf("\n--- Bluetooth AVRCP Commands %s ---\n", bd_addr_to_str(iut_address)); 942 printf("O - get play status\n"); 943 printf("j - get now playing info\n"); 944 printf("k - play\n"); 945 printf("K - stop\n"); 946 printf("L - pause\n"); 947 printf("u - start fast forward\n"); 948 printf("U - stop fast forward\n"); 949 printf("n - start rewind\n"); 950 printf("N - stop rewind\n"); 951 printf("i - forward\n"); 952 printf("I - backward\n"); 953 printf("M - mute\n"); 954 printf("r - skip\n"); 955 printf("q - query repeat and shuffle mode\n"); 956 printf("v - repeat single track\n"); 957 printf("x - repeat all tracks\n"); 958 printf("X - disable repeat mode\n"); 959 printf("z - shuffle all tracks\n"); 960 printf("Z - disable shuffle mode\n"); 961 962 printf("a/A - register/deregister TRACK_CHANGED\n"); 963 printf("R/P - register/deregister PLAYBACK_POS_CHANGED\n"); 964 965 printf("\n--- Volume Control ---\n"); 966 printf("t - volume up for 10 percent\n"); 967 printf("T - volume down for 10 percent\n"); 968 969 printf("---\n"); 970 } 971 #endif 972 973 #ifdef HAVE_BTSTACK_STDIN 974 static void stdin_process(char cmd){ 975 uint8_t status = ERROR_CODE_SUCCESS; 976 977 switch (cmd){ 978 case 'b': 979 status = a2dp_sink_establish_stream(device_addr, a2dp_local_seid, &a2dp_cid); 980 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); 981 break; 982 case 'B': 983 printf(" - AVDTP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 984 status = avdtp_sink_disconnect(a2dp_cid); 985 break; 986 case 'c': 987 printf(" - Create AVRCP Controller connection to addr %s.\n", bd_addr_to_str(device_addr)); 988 status = avrcp_controller_connect(device_addr, &avrcp_controller_cid); 989 break; 990 case 'C': 991 printf(" - AVRCP Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 992 status = avrcp_controller_disconnect(avrcp_controller_cid); 993 break; 994 case 'd': 995 printf(" - Create AVRCP Target connection to addr %s.\n", bd_addr_to_str(device_addr)); 996 status = avrcp_target_connect(device_addr, &avrcp_target_cid); 997 break; 998 case 'D': 999 printf(" - AVRCP Target disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 1000 status = avrcp_target_disconnect(avrcp_target_cid); 1001 break; 1002 1003 case '\n': 1004 case '\r': 1005 break; 1006 case 'w': 1007 printf("Send delay report\n"); 1008 avdtp_sink_delay_report(a2dp_cid, a2dp_local_seid, 100); 1009 break; 1010 // Volume Control 1011 case 't': 1012 volume_percentage = volume_percentage <= 90 ? volume_percentage + 10 : 100; 1013 printf(" - volume up for 10 percent, %d%% (%d) \n", volume_percentage, volume_percentage * 127 / 100); 1014 status = avrcp_target_volume_changed(avrcp_target_cid, volume_percentage * 127 / 100); 1015 break; 1016 case 'T': 1017 volume_percentage = volume_percentage >= 10 ? volume_percentage - 10 : 0; 1018 printf(" - volume down for 10 percent, %d%% (%d) \n", volume_percentage, volume_percentage * 127 / 100); 1019 status = avrcp_target_volume_changed(avrcp_target_cid, volume_percentage * 127 / 100); 1020 break; 1021 1022 case 'O': 1023 printf(" - get play status\n"); 1024 status = avrcp_controller_get_play_status(avrcp_controller_cid); 1025 break; 1026 case 'j': 1027 printf(" - get now playing info\n"); 1028 status = avrcp_controller_get_now_playing_info(avrcp_controller_cid); 1029 break; 1030 case 'k': 1031 printf(" - play\n"); 1032 status = avrcp_controller_play(avrcp_controller_cid); 1033 break; 1034 case 'K': 1035 printf(" - stop\n"); 1036 status = avrcp_controller_stop(avrcp_controller_cid); 1037 break; 1038 case 'L': 1039 printf(" - pause\n"); 1040 status = avrcp_controller_pause(avrcp_controller_cid); 1041 break; 1042 case 'u': 1043 printf(" - start fast forward\n"); 1044 status = avrcp_controller_press_and_hold_fast_forward(avrcp_controller_cid); 1045 break; 1046 case 'U': 1047 printf(" - stop fast forward\n"); 1048 status = avrcp_controller_release_press_and_hold_cmd(avrcp_controller_cid); 1049 break; 1050 case 'n': 1051 printf(" - start rewind\n"); 1052 status = avrcp_controller_press_and_hold_rewind(avrcp_controller_cid); 1053 break; 1054 case 'N': 1055 printf(" - stop rewind\n"); 1056 status = avrcp_controller_release_press_and_hold_cmd(avrcp_controller_cid); 1057 break; 1058 case 'i': 1059 printf(" - forward\n"); 1060 status = avrcp_controller_forward(avrcp_controller_cid); 1061 break; 1062 case 'I': 1063 printf(" - backward\n"); 1064 status = avrcp_controller_backward(avrcp_controller_cid); 1065 break; 1066 case 'M': 1067 printf(" - mute\n"); 1068 status = avrcp_controller_mute(avrcp_controller_cid); 1069 break; 1070 case 'r': 1071 printf(" - skip\n"); 1072 status = avrcp_controller_skip(avrcp_controller_cid); 1073 break; 1074 case 'q': 1075 printf(" - query repeat and shuffle mode\n"); 1076 status = avrcp_controller_query_shuffle_and_repeat_modes(avrcp_controller_cid); 1077 break; 1078 case 'v': 1079 printf(" - repeat single track\n"); 1080 status = avrcp_controller_set_repeat_mode(avrcp_controller_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK); 1081 break; 1082 case 'x': 1083 printf(" - repeat all tracks\n"); 1084 status = avrcp_controller_set_repeat_mode(avrcp_controller_cid, AVRCP_REPEAT_MODE_ALL_TRACKS); 1085 break; 1086 case 'X': 1087 printf(" - disable repeat mode\n"); 1088 status = avrcp_controller_set_repeat_mode(avrcp_controller_cid, AVRCP_REPEAT_MODE_OFF); 1089 break; 1090 case 'z': 1091 printf(" - shuffle all tracks\n"); 1092 status = avrcp_controller_set_shuffle_mode(avrcp_controller_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS); 1093 break; 1094 case 'Z': 1095 printf(" - disable shuffle mode\n"); 1096 status = avrcp_controller_set_shuffle_mode(avrcp_controller_cid, AVRCP_SHUFFLE_MODE_OFF); 1097 break; 1098 case 'a': 1099 printf("AVRCP: enable notification TRACK_CHANGED\n"); 1100 avrcp_controller_enable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 1101 break; 1102 case 'A': 1103 printf("AVRCP: disable notification TRACK_CHANGED\n"); 1104 avrcp_controller_disable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 1105 break; 1106 case 'R': 1107 printf("AVRCP: enable notification PLAYBACK_POS_CHANGED\n"); 1108 avrcp_controller_enable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED); 1109 break; 1110 case 'P': 1111 printf("AVRCP: disable notification PLAYBACK_POS_CHANGED\n"); 1112 avrcp_controller_disable_notification(avrcp_controller_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED); 1113 break; 1114 1115 default: 1116 show_usage(); 1117 return; 1118 } 1119 if (status != ERROR_CODE_SUCCESS){ 1120 printf("Could not perform command, status 0x%2x\n", status); 1121 } 1122 } 1123 #endif 1124 1125 int btstack_main(int argc, const char * argv[]); 1126 int btstack_main(int argc, const char * argv[]){ 1127 UNUSED(argc); 1128 (void)argv; 1129 1130 a2dp_and_avrcp_setup(); 1131 1132 #ifdef HAVE_BTSTACK_STDIN 1133 // parse human readable Bluetooth address 1134 sscanf_bd_addr(device_addr_string, device_addr); 1135 btstack_stdin_setup(stdin_process); 1136 #endif 1137 1138 // turn on! 1139 printf("Starting BTstack ...\n"); 1140 hci_power_control(HCI_POWER_ON); 1141 return 0; 1142 } 1143