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