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