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