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 39 #include <stdint.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #include "btstack_config.h" 45 #include "btstack_debug.h" 46 #include "btstack_event.h" 47 #include "btstack_memory.h" 48 #include "btstack_run_loop.h" 49 #include "gap.h" 50 #include "hci.h" 51 #include "hci_cmd.h" 52 #include "hci_dump.h" 53 #include "l2cap.h" 54 #include "classic/avdtp_sink.h" 55 #include "classic/a2dp_sink.h" 56 #include "classic/btstack_sbc.h" 57 #include "classic/avdtp_util.h" 58 #include "classic/avrcp.h" 59 60 #define AVRCP_BROWSING_ENABLED 0 61 62 #ifdef HAVE_BTSTACK_STDIN 63 #include "btstack_stdin.h" 64 #endif 65 66 #ifdef HAVE_AUDIO_DMA 67 #include "btstack_ring_buffer.h" 68 #include "hal_audio_dma.h" 69 #endif 70 71 #ifdef HAVE_PORTAUDIO 72 #include "btstack_ring_buffer.h" 73 #include <portaudio.h> 74 #endif 75 76 #ifdef HAVE_POSIX_FILE_IO 77 #include "wav_util.h" 78 #define STORE_SBC_TO_SBC_FILE 79 #define STORE_SBC_TO_WAV_FILE 80 #endif 81 82 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) || defined(HAVE_AUDIO_DMA) 83 #define DECODE_SBC 84 #endif 85 86 #define NUM_CHANNELS 2 87 #define BYTES_PER_FRAME (2*NUM_CHANNELS) 88 #define MAX_SBC_FRAME_SIZE 120 89 90 // SBC Decoder for WAV file or PortAudio 91 #ifdef DECODE_SBC 92 static btstack_sbc_decoder_state_t state; 93 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 94 #endif 95 96 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 97 #define PREBUFFER_MS 200 98 static int audio_stream_started = 0; 99 static int audio_stream_paused = 0; 100 static btstack_ring_buffer_t ring_buffer; 101 #endif 102 103 #ifdef HAVE_AUDIO_DMA 104 // below 30: add samples, 30-40: fine, above 40: drop samples 105 #define OPTIMAL_FRAMES_MIN 30 106 #define OPTIMAL_FRAMES_MAX 40 107 #define ADDITIONAL_FRAMES 10 108 #define DMA_AUDIO_FRAMES 128 109 #define DMA_MAX_FILL_FRAMES 1 110 #define NUM_AUDIO_BUFFERS 2 111 112 static uint16_t audio_samples[(DMA_AUDIO_FRAMES + DMA_MAX_FILL_FRAMES)*2*NUM_AUDIO_BUFFERS]; 113 static uint16_t audio_samples_len[NUM_AUDIO_BUFFERS]; 114 static uint8_t ring_buffer_storage[(OPTIMAL_FRAMES_MAX + ADDITIONAL_FRAMES) * MAX_SBC_FRAME_SIZE]; 115 static const uint16_t silent_buffer[DMA_AUDIO_FRAMES*2]; 116 static volatile int playback_buffer; 117 static int write_buffer; 118 static uint8_t sbc_frame_size; 119 static int sbc_samples_fix; 120 #endif 121 122 // PortAdudio - live playback 123 #ifdef HAVE_PORTAUDIO 124 #define PA_SAMPLE_TYPE paInt16 125 #define SAMPLE_RATE 48000 126 #define FRAMES_PER_BUFFER 128 127 #define PREBUFFER_BYTES (PREBUFFER_MS*SAMPLE_RATE/1000*BYTES_PER_FRAME) 128 static PaStream * stream; 129 static uint8_t ring_buffer_storage[2*PREBUFFER_BYTES]; 130 static btstack_ring_buffer_t ring_buffer; 131 static int total_num_samples = 0; 132 #endif 133 134 // WAV File 135 #ifdef STORE_SBC_TO_WAV_FILE 136 static int frame_count = 0; 137 static char * wav_filename = "avdtp_sink.wav"; 138 #endif 139 140 #ifdef STORE_SBC_TO_SBC_FILE 141 static FILE * sbc_file; 142 static char * sbc_filename = "avdtp_sink.sbc"; 143 #endif 144 145 typedef struct { 146 // bitmaps 147 uint8_t sampling_frequency_bitmap; 148 uint8_t channel_mode_bitmap; 149 uint8_t block_length_bitmap; 150 uint8_t subbands_bitmap; 151 uint8_t allocation_method_bitmap; 152 uint8_t min_bitpool_value; 153 uint8_t max_bitpool_value; 154 } adtvp_media_codec_information_sbc_t; 155 156 typedef struct { 157 int reconfigure; 158 int num_channels; 159 int sampling_frequency; 160 int channel_mode; 161 int block_length; 162 int subbands; 163 int allocation_method; 164 int min_bitpool_value; 165 int max_bitpool_value; 166 int frames_per_buffer; 167 } avdtp_media_codec_configuration_sbc_t; 168 169 #ifdef HAVE_BTSTACK_STDIN 170 // mac 2011: static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3}; 171 // pts: static bd_addr_t remote = {0x00, 0x1B, 0xDC, 0x08, 0x0A, 0xA5}; 172 // mac 2013: 173 static const char * device_addr_string = "00:1B:DC:08:0A:A5"; 174 #endif 175 176 // bt dongle: -u 02-02 static bd_addr_t remote = {0x00, 0x02, 0x72, 0xDC, 0x31, 0xC1}; 177 178 static uint16_t avdtp_cid = 0; 179 static uint8_t sdp_avdtp_sink_service_buffer[150]; 180 static avdtp_sep_t sep; 181 182 static adtvp_media_codec_information_sbc_t sbc_capability; 183 static avdtp_media_codec_configuration_sbc_t sbc_configuration; 184 static avdtp_stream_endpoint_t * local_stream_endpoint; 185 186 #ifdef HAVE_BTSTACK_STDIN 187 static uint16_t remote_configuration_bitmap; 188 static avdtp_capabilities_t remote_configuration; 189 #endif 190 191 typedef enum { 192 AVDTP_APPLICATION_IDLE, 193 AVDTP_APPLICATION_CONNECTED, 194 AVDTP_APPLICATION_STREAMING 195 } avdtp_application_state_t; 196 197 avdtp_application_state_t app_state = AVDTP_APPLICATION_IDLE; 198 199 static btstack_packet_callback_registration_t hci_event_callback_registration; 200 201 static int media_initialized = 0; 202 203 static bd_addr_t device_addr; 204 static uint16_t avrcp_cid = 0; 205 static uint8_t sdp_avrcp_controller_service_buffer[200]; 206 207 #ifdef HAVE_PORTAUDIO 208 static int patestCallback( const void *inputBuffer, void *outputBuffer, 209 unsigned long framesPerBuffer, 210 const PaStreamCallbackTimeInfo* timeInfo, 211 PaStreamCallbackFlags statusFlags, 212 void *userData ) { 213 214 /** patestCallback is called from different thread, don't use hci_dump / log_info here without additional checks */ 215 216 (void) timeInfo; /* Prevent unused variable warnings. */ 217 (void) statusFlags; 218 (void) inputBuffer; 219 (void) userData; 220 221 int bytes_to_copy = framesPerBuffer * BYTES_PER_FRAME; 222 223 // fill with silence while paused 224 if (audio_stream_paused){ 225 226 if (btstack_ring_buffer_bytes_available(&ring_buffer) < PREBUFFER_BYTES){ 227 // printf("PA: silence\n"); 228 memset(outputBuffer, 0, bytes_to_copy); 229 return 0; 230 } else { 231 // resume playback 232 audio_stream_paused = 0; 233 } 234 } 235 236 // get data from ringbuffer 237 uint32_t bytes_read = 0; 238 btstack_ring_buffer_read(&ring_buffer, outputBuffer, bytes_to_copy, &bytes_read); 239 bytes_to_copy -= bytes_read; 240 241 // fill with 0 if not enough 242 if (bytes_to_copy){ 243 memset(outputBuffer + bytes_read, 0, bytes_to_copy); 244 audio_stream_paused = 1; 245 } 246 return 0; 247 } 248 #endif 249 250 #ifdef HAVE_AUDIO_DMA 251 static int next_buffer(int current){ 252 if (current == NUM_AUDIO_BUFFERS-1) return 0; 253 return current + 1; 254 } 255 static uint8_t * start_of_buffer(int num){ 256 return (uint8_t *) &audio_samples[num * DMA_AUDIO_FRAMES * 2]; 257 } 258 void hal_audio_dma_done(void){ 259 if (audio_stream_paused){ 260 hal_audio_dma_play((const uint8_t *) silent_buffer, DMA_AUDIO_FRAMES*4); 261 return; 262 } 263 // next buffer 264 int next_playback_buffer = next_buffer(playback_buffer); 265 uint8_t * playback_data; 266 if (next_playback_buffer == write_buffer){ 267 268 // TODO: stop codec while playing silence when getting 'stream paused' 269 270 // start playing silence 271 audio_stream_paused = 1; 272 hal_audio_dma_play((const uint8_t *) silent_buffer, DMA_AUDIO_FRAMES*4); 273 printf("%6u - paused - bytes in buffer %u\n", (int) btstack_run_loop_get_time_ms(), btstack_ring_buffer_bytes_available(&ring_buffer)); 274 return; 275 } 276 playback_buffer = next_playback_buffer; 277 playback_data = start_of_buffer(playback_buffer); 278 hal_audio_dma_play(playback_data, audio_samples_len[playback_buffer]); 279 // btstack_run_loop_embedded_trigger(); 280 } 281 #endif 282 283 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) 284 285 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 286 UNUSED(sample_rate); 287 UNUSED(context); 288 289 #ifdef STORE_SBC_TO_WAV_FILE 290 wav_writer_write_int16(num_samples*num_channels, data); 291 frame_count++; 292 #endif 293 294 #ifdef HAVE_PORTAUDIO 295 total_num_samples+=num_samples*num_channels; 296 297 // store pcm samples in ringbuffer 298 btstack_ring_buffer_write(&ring_buffer, (uint8_t *)data, num_samples*num_channels*2); 299 300 if (!audio_stream_started){ 301 audio_stream_paused = 1; 302 /* -- start stream -- */ 303 PaError err = Pa_StartStream(stream); 304 if (err != paNoError){ 305 printf("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err)); 306 return; 307 } 308 audio_stream_started = 1; 309 } 310 #endif 311 312 } 313 #endif 314 315 316 #ifdef HAVE_AUDIO_DMA 317 318 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 319 UNUSED(sample_rate); 320 UNUSED(context); 321 total_num_samples+=num_samples*num_channels; 322 323 // store in ring buffer 324 uint8_t * write_data = start_of_buffer(write_buffer); 325 uint16_t len = num_samples*num_channels*2; 326 memcpy(write_data, data, len); 327 audio_samples_len[write_buffer] = len; 328 329 // add/drop audio frame to fix drift 330 if (sbc_samples_fix > 0){ 331 memcpy(write_data + len, write_data + len - 4, 4); 332 audio_samples_len[write_buffer] += 4; 333 } 334 if (sbc_samples_fix < 0){ 335 audio_samples_len[write_buffer] -= 4; 336 } 337 338 write_buffer = next_buffer(write_buffer); 339 } 340 341 static void hal_audio_dma_process(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){ 342 UNUSED(ds); 343 UNUSED(callback_type); 344 345 if (!media_initialized) return; 346 347 int trigger_resume = 0; 348 if (audio_stream_paused) { 349 if (sbc_frame_size && btstack_ring_buffer_bytes_available(&ring_buffer) >= OPTIMAL_FRAMES_MIN * sbc_frame_size){ 350 trigger_resume = 1; 351 // reset buffers 352 playback_buffer = NUM_AUDIO_BUFFERS - 1; 353 write_buffer = 0; 354 } else { 355 return; 356 } 357 } 358 359 while (playback_buffer != write_buffer && btstack_ring_buffer_bytes_available(&ring_buffer) >= sbc_frame_size ){ 360 uint8_t frame[MAX_SBC_FRAME_SIZE]; 361 uint32_t bytes_read = 0; 362 btstack_ring_buffer_read(&ring_buffer, frame, sbc_frame_size, &bytes_read); 363 btstack_sbc_decoder_process_data(&state, 0, frame, sbc_frame_size); 364 } 365 366 if (trigger_resume){ 367 printf("%6u - resume\n", (int) btstack_run_loop_get_time_ms()); 368 audio_stream_paused = 0; 369 } 370 } 371 372 #endif 373 374 static int media_processing_init(avdtp_media_codec_configuration_sbc_t configuration){ 375 376 #ifdef DECODE_SBC 377 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 378 #endif 379 380 #ifdef STORE_SBC_TO_WAV_FILE 381 wav_writer_open(wav_filename, configuration.num_channels, configuration.sampling_frequency); 382 #endif 383 384 #ifdef STORE_SBC_TO_SBC_FILE 385 sbc_file = fopen(sbc_filename, "wb"); 386 #endif 387 388 #ifdef HAVE_PORTAUDIO 389 // int frames_per_buffer = configuration.frames_per_buffer; 390 PaError err; 391 PaStreamParameters outputParameters; 392 393 /* -- initialize PortAudio -- */ 394 err = Pa_Initialize(); 395 if (err != paNoError){ 396 printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); 397 return err; 398 } 399 /* -- setup input and output -- */ 400 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 401 outputParameters.channelCount = configuration.num_channels; 402 outputParameters.sampleFormat = PA_SAMPLE_TYPE; 403 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; 404 outputParameters.hostApiSpecificStreamInfo = NULL; 405 /* -- setup stream -- */ 406 err = Pa_OpenStream( 407 &stream, 408 NULL, /* &inputParameters */ 409 &outputParameters, 410 configuration.sampling_frequency, 411 0, 412 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 413 patestCallback, /* use callback */ 414 NULL ); 415 416 if (err != paNoError){ 417 printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); 418 return err; 419 } 420 #endif 421 #ifdef HAVE_AUDIO_DMA 422 audio_stream_paused = 1; 423 hal_audio_dma_init(configuration.sampling_frequency); 424 hal_audio_dma_set_audio_played(&hal_audio_dma_done); 425 // start playing silence 426 hal_audio_dma_done(); 427 #endif 428 429 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 430 memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage)); 431 btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage)); 432 audio_stream_started = 0; 433 #endif 434 media_initialized = 1; 435 return 0; 436 } 437 438 static void media_processing_close(void){ 439 if (!media_initialized) return; 440 media_initialized = 0; 441 #ifdef STORE_SBC_TO_WAV_FILE 442 printf(" Close wav writer.\n"); 443 wav_writer_close(); 444 int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 445 446 printf(" Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", total_frames_nr, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr); 447 printf(" Written %d frames to wav file: %s\n\n", frame_count, wav_filename); 448 #endif 449 #ifdef STORE_SBC_TO_SBC_FILE 450 fclose(sbc_file); 451 #endif 452 453 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 454 audio_stream_started = 0; 455 #endif 456 457 #ifdef HAVE_PORTAUDIO 458 PaError err = Pa_StopStream(stream); 459 if (err != paNoError){ 460 printf("Error stopping the stream: \"%s\"\n", Pa_GetErrorText(err)); 461 return; 462 } 463 err = Pa_CloseStream(stream); 464 if (err != paNoError){ 465 printf("Error closing the stream: \"%s\"\n", Pa_GetErrorText(err)); 466 return; 467 } 468 err = Pa_Terminate(); 469 if (err != paNoError){ 470 printf("Error terminating portaudio: \"%s\"\n", Pa_GetErrorText(err)); 471 return; 472 } 473 #endif 474 #ifdef HAVE_AUDIO_DMA 475 hal_audio_dma_close(); 476 #endif 477 } 478 479 static void handle_l2cap_media_data_packet(avdtp_stream_endpoint_t * stream_endpoint, uint8_t *packet, uint16_t size){ 480 481 UNUSED(stream_endpoint); 482 483 int pos = 0; 484 485 avdtp_media_packet_header_t media_header; 486 media_header.version = packet[pos] & 0x03; 487 media_header.padding = get_bit16(packet[pos],2); 488 media_header.extension = get_bit16(packet[pos],3); 489 media_header.csrc_count = (packet[pos] >> 4) & 0x0F; 490 491 pos++; 492 493 media_header.marker = get_bit16(packet[pos],0); 494 media_header.payload_type = (packet[pos] >> 1) & 0x7F; 495 pos++; 496 497 media_header.sequence_number = big_endian_read_16(packet, pos); 498 pos+=2; 499 500 media_header.timestamp = big_endian_read_32(packet, pos); 501 pos+=4; 502 503 media_header.synchronization_source = big_endian_read_32(packet, pos); 504 pos+=4; 505 506 UNUSED(media_header); 507 508 // TODO: read csrc list 509 510 // printf_hexdump( packet, pos ); 511 // printf("MEDIA HEADER: %u timestamp, version %u, padding %u, extension %u, csrc_count %u\n", 512 // media_header.timestamp, media_header.version, media_header.padding, media_header.extension, media_header.csrc_count); 513 // printf("MEDIA HEADER: marker %02x, payload_type %02x, sequence_number %u, synchronization_source %u\n", 514 // media_header.marker, media_header.payload_type, media_header.sequence_number, media_header.synchronization_source); 515 516 avdtp_sbc_codec_header_t sbc_header; 517 sbc_header.fragmentation = get_bit16(packet[pos], 7); 518 sbc_header.starting_packet = get_bit16(packet[pos], 6); 519 sbc_header.last_packet = get_bit16(packet[pos], 5); 520 sbc_header.num_frames = packet[pos] & 0x0f; 521 pos++; 522 523 #ifdef HAVE_AUDIO_DMA 524 // store sbc frame size for buffer management 525 sbc_frame_size = (size-pos)/ sbc_header.num_frames; 526 #endif 527 528 UNUSED(sbc_header); 529 // 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); 530 // printf_hexdump( packet+pos, size-pos ); 531 532 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) 533 btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos); 534 #endif 535 536 #ifdef HAVE_AUDIO_DMA 537 btstack_ring_buffer_write(&ring_buffer, packet+pos, size-pos); 538 539 // decide on audio sync drift based on number of sbc frames in queue 540 int sbc_frames_in_buffer = btstack_ring_buffer_bytes_available(&ring_buffer) / sbc_frame_size; 541 if (sbc_frames_in_buffer < OPTIMAL_FRAMES_MIN){ 542 sbc_samples_fix = 1; // duplicate last sample 543 } else if (sbc_frames_in_buffer <= OPTIMAL_FRAMES_MAX){ 544 sbc_samples_fix = 0; // nothing to do 545 } else { 546 sbc_samples_fix = -1; // drop last sample 547 } 548 549 // dump 550 printf("%6u %03u %d\n", (int) btstack_run_loop_get_time_ms(), sbc_frames_in_buffer, sbc_samples_fix); 551 552 #endif 553 554 #ifdef STORE_SBC_TO_SBC_FILE 555 fwrite(packet+pos, size-pos, 1, sbc_file); 556 #endif 557 } 558 559 static void dump_sbc_capability(adtvp_media_codec_information_sbc_t media_codec_sbc){ 560 printf("Received media codec capability:\n"); 561 printf(" - sampling_frequency: 0x%02x\n", media_codec_sbc.sampling_frequency_bitmap); 562 printf(" - channel_mode: 0x%02x\n", media_codec_sbc.channel_mode_bitmap); 563 printf(" - block_length: 0x%02x\n", media_codec_sbc.block_length_bitmap); 564 printf(" - subbands: 0x%02x\n", media_codec_sbc.subbands_bitmap); 565 printf(" - allocation_method: 0x%02x\n", media_codec_sbc.allocation_method_bitmap); 566 printf(" - bitpool_value [%d, %d] \n", media_codec_sbc.min_bitpool_value, media_codec_sbc.max_bitpool_value); 567 } 568 569 static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){ 570 printf("Received media codec configuration:\n"); 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 } 579 580 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 581 UNUSED(channel); 582 UNUSED(size); 583 bd_addr_t event_addr; 584 uint16_t local_cid; 585 uint8_t status = 0xFF; 586 switch (packet_type) { 587 case HCI_EVENT_PACKET: 588 switch (hci_event_packet_get_type(packet)) { 589 case HCI_EVENT_DISCONNECTION_COMPLETE: 590 // connection closed -> quit test app 591 printf("AVRCP: HCI_EVENT_DISCONNECTION_COMPLETE\n"); 592 break; 593 case HCI_EVENT_AVRCP_META: 594 switch (packet[2]){ 595 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { 596 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 597 if (avrcp_cid != local_cid) { 598 printf("AVRCP Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", avrcp_cid, local_cid); 599 return; 600 } 601 602 status = avrcp_subevent_connection_established_get_status(packet); 603 if (status != ERROR_CODE_SUCCESS){ 604 printf("AVRCP Connection failed: status 0x%02x\n", status); 605 avrcp_cid = 0; 606 return; 607 } 608 avrcp_subevent_connection_established_get_bd_addr(packet, event_addr); 609 printf("Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(event_addr), avrcp_cid); 610 611 // automatically enable notifications 612 avrcp_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED); 613 avrcp_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED); 614 return; 615 } 616 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 617 printf("Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 618 avrcp_cid = 0; 619 return; 620 default: 621 break; 622 } 623 624 status = packet[5]; 625 if (!avrcp_cid) return; 626 627 // avoid printing INTERIM status 628 if (status == AVRCP_CTYPE_RESPONSE_INTERIM) return; 629 630 printf("AVRCP: command status: %s, ", avrcp_ctype2str(status)); 631 switch (packet[2]){ 632 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED: 633 printf("notification, playback status changed %s\n", avrcp_play_status2str(avrcp_subevent_notification_playback_status_changed_get_play_status(packet))); 634 return; 635 case AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED: 636 printf("notification, playing content changed\n"); 637 return; 638 case AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED: 639 printf("notification track changed\n"); 640 return; 641 case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED: 642 printf("notification absolute volume changed %d\n", avrcp_subevent_notification_volume_changed_get_absolute_volume(packet)); 643 return; 644 case AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED: 645 printf("notification changed\n"); 646 return; 647 case AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE:{ 648 uint8_t shuffle_mode = avrcp_subevent_shuffle_and_repeat_mode_get_shuffle_mode(packet); 649 uint8_t repeat_mode = avrcp_subevent_shuffle_and_repeat_mode_get_repeat_mode(packet); 650 printf("%s, %s\n", avrcp_shuffle2str(shuffle_mode), avrcp_repeat2str(repeat_mode)); 651 break; 652 } 653 case AVRCP_SUBEVENT_NOW_PLAYING_INFO:{ 654 uint8_t value[100]; 655 printf("now playing: \n"); 656 if (avrcp_subevent_now_playing_info_get_title_len(packet) > 0){ 657 memcpy(value, avrcp_subevent_now_playing_info_get_title(packet), avrcp_subevent_now_playing_info_get_title_len(packet)); 658 printf(" Title: %s\n", value); 659 } 660 if (avrcp_subevent_now_playing_info_get_album_len(packet) > 0){ 661 memcpy(value, avrcp_subevent_now_playing_info_get_album(packet), avrcp_subevent_now_playing_info_get_album_len(packet)); 662 printf(" Album: %s\n", value); 663 } 664 if (avrcp_subevent_now_playing_info_get_artist_len(packet) > 0){ 665 memcpy(value, avrcp_subevent_now_playing_info_get_artist(packet), avrcp_subevent_now_playing_info_get_artist_len(packet)); 666 printf(" Artist: %s\n", value); 667 } 668 if (avrcp_subevent_now_playing_info_get_genre_len(packet) > 0){ 669 memcpy(value, avrcp_subevent_now_playing_info_get_genre(packet), avrcp_subevent_now_playing_info_get_genre_len(packet)); 670 printf(" Genre: %s\n", value); 671 } 672 printf(" Track: %d\n", avrcp_subevent_now_playing_info_get_track(packet)); 673 printf(" Total nr. tracks: %d\n", avrcp_subevent_now_playing_info_get_total_tracks(packet)); 674 printf(" Song length: %d ms\n", avrcp_subevent_now_playing_info_get_song_length(packet)); 675 break; 676 } 677 case AVRCP_SUBEVENT_PLAY_STATUS: 678 printf("song length: %d ms, song position: %d ms, play status: %s\n", 679 avrcp_subevent_play_status_get_song_length(packet), 680 avrcp_subevent_play_status_get_song_position(packet), 681 avrcp_play_status2str(avrcp_subevent_play_status_get_play_status(packet))); 682 break; 683 case AVRCP_SUBEVENT_OPERATION_COMPLETE: 684 printf("operation done %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 685 break; 686 case AVRCP_SUBEVENT_OPERATION_START: 687 printf("operation start %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 688 break; 689 case AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE: 690 // response to set shuffle and repeat mode 691 printf("\n"); 692 break; 693 default: 694 printf("Not implemented\n"); 695 break; 696 } 697 break; 698 default: 699 break; 700 } 701 break; 702 default: 703 // other packet type 704 break; 705 } 706 707 } 708 709 710 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 711 UNUSED(channel); 712 UNUSED(size); 713 714 bd_addr_t event_addr; 715 switch (packet_type) { 716 717 case HCI_EVENT_PACKET: 718 switch (hci_event_packet_get_type(packet)) { 719 case HCI_EVENT_PIN_CODE_REQUEST: 720 // inform about pin code request 721 printf("Pin code request - using '0000'\n"); 722 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 723 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 724 break; 725 case HCI_EVENT_DISCONNECTION_COMPLETE: 726 // connection closed -> quit test app 727 app_state = AVDTP_APPLICATION_IDLE; 728 printf("\n --- avdtp_test: HCI_EVENT_DISCONNECTION_COMPLETE ---\n"); 729 media_processing_close(); 730 break; 731 case HCI_EVENT_AVDTP_META: 732 switch (packet[2]){ 733 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 734 app_state = AVDTP_APPLICATION_CONNECTED; 735 avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 736 printf("\n --- avdtp_test: AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED, cid 0x%02x ---\n", avdtp_cid); 737 break; 738 case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND: 739 if (app_state < AVDTP_APPLICATION_CONNECTED) return; 740 sep.seid = avdtp_subevent_signaling_sep_found_get_seid(packet); 741 sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet); 742 sep.media_type = avdtp_subevent_signaling_sep_found_get_media_type(packet); 743 sep.type = avdtp_subevent_signaling_sep_found_get_sep_type(packet); 744 printf("Found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", sep.seid, sep.in_use, sep.media_type, sep.type); 745 break; 746 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY: 747 if (app_state < AVDTP_APPLICATION_CONNECTED) return; 748 sbc_capability.sampling_frequency_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet); 749 sbc_capability.channel_mode_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet); 750 sbc_capability.block_length_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet); 751 sbc_capability.subbands_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet); 752 sbc_capability.allocation_method_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet); 753 sbc_capability.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet); 754 sbc_capability.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet); 755 dump_sbc_capability(sbc_capability); 756 break; 757 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ 758 if (app_state < AVDTP_APPLICATION_CONNECTED) return; 759 sbc_configuration.reconfigure = avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 760 sbc_configuration.num_channels = avdtp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet); 761 sbc_configuration.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); 762 sbc_configuration.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet); 763 sbc_configuration.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet); 764 sbc_configuration.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet); 765 sbc_configuration.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet); 766 sbc_configuration.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); 767 sbc_configuration.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); 768 sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length; 769 dump_sbc_configuration(sbc_configuration); 770 // // TODO: use actual config 771 // btstack_sbc_encoder_init(&local_stream_endpoint->sbc_encoder_state, SBC_MODE_STANDARD, 16, 8, 2, 44100, 53); 772 773 if (sbc_configuration.reconfigure){ 774 media_processing_close(); 775 media_processing_init(sbc_configuration); 776 } else { 777 media_processing_init(sbc_configuration); 778 } 779 break; 780 } 781 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 782 app_state = AVDTP_APPLICATION_STREAMING; 783 break; 784 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY: 785 printf(" received non SBC codec. not implemented\n"); 786 break; 787 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 788 break; 789 default: 790 printf(" not implemented\n"); 791 break; 792 } 793 break; 794 default: 795 break; 796 } 797 break; 798 default: 799 // other packet type 800 break; 801 } 802 } 803 804 #ifdef HAVE_BTSTACK_STDIN 805 static void show_usage(void){ 806 bd_addr_t iut_address; 807 gap_local_bd_addr(iut_address); 808 printf("\n--- Bluetooth AVDTP Sink/AVRCP Connection Test Console %s ---\n", bd_addr_to_str(iut_address)); 809 printf("b - AVDTP Sink create connection to addr %s\n", device_addr_string); 810 printf("B - AVDTP Sink disconnect\n"); 811 printf("c - AVRCP create connection to addr %s\n", device_addr_string); 812 printf("C - AVRCP disconnect\n"); 813 814 printf("\n--- Bluetooth AVDTP SINK Commands %s ---\n", bd_addr_to_str(iut_address)); 815 printf("d - discover stream endpoints\n"); 816 printf("g - get capabilities\n"); 817 printf("a - get all capabilities\n"); 818 printf("s - set configuration\n"); 819 printf("f - get configuration\n"); 820 printf("R - reconfigure stream with %d\n", sep.seid); 821 printf("o - open stream with seid %d\n", sep.seid); 822 printf("m - start stream with %d\n", sep.seid); 823 printf("A - abort stream with %d\n", sep.seid); 824 printf("S - stop stream with %d\n", sep.seid); 825 printf("P - suspend stream with %d\n", sep.seid); 826 827 printf("\n--- Bluetooth AVRCP Commands %s ---\n", bd_addr_to_str(iut_address)); 828 printf("O - get play status\n"); 829 printf("j - get now playing info\n"); 830 printf("k - play\n"); 831 printf("K - stop\n"); 832 printf("L - pause\n"); 833 printf("u - start fast forward\n"); 834 printf("U - stop fast forward\n"); 835 printf("n - start rewind\n"); 836 printf("N - stop rewind\n"); 837 printf("i - forward\n"); 838 printf("I - backward\n"); 839 printf("t - volume up\n"); 840 printf("T - volume down\n"); 841 printf("p - absolute volume of 50 percent\n"); 842 printf("M - mute\n"); 843 printf("r - skip\n"); 844 printf("q - query repeat and shuffle mode\n"); 845 printf("v - repeat single track\n"); 846 printf("x - repeat all tracks\n"); 847 printf("X - disable repeat mode\n"); 848 printf("z - shuffle all tracks\n"); 849 printf("Z - disable shuffle mode\n"); 850 851 printf("Ctrl-c - exit\n"); 852 printf("---\n"); 853 } 854 #endif 855 856 static uint8_t media_sbc_codec_capabilities[] = { 857 0xFF,//(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 858 0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 859 2, 53 860 }; 861 862 #ifdef HAVE_BTSTACK_STDIN 863 static uint8_t media_sbc_codec_configuration[] = { 864 (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 865 (AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 866 2, 53 867 }; 868 869 static uint8_t media_sbc_codec_reconfiguration[] = { 870 (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 871 (AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_SNR, 872 2, 53 873 }; 874 875 static void stdin_process(char cmd){ 876 sep.seid = 1; 877 switch (cmd){ 878 case 'b': 879 printf("Creating L2CAP Connection to %s, BLUETOOTH_PROTOCOL_AVDTP\n", device_addr_string); 880 avdtp_sink_connect(device_addr); 881 break; 882 case 'B': 883 printf("Disconnect\n"); 884 avdtp_sink_disconnect(avdtp_cid); 885 break; 886 case 'c': 887 printf(" - Create AVRCP connection to addr %s.\n", bd_addr_to_str(device_addr)); 888 avrcp_controller_connect(device_addr, &avrcp_cid); 889 printf(" assigned avrcp cid 0x%02x\n", avrcp_cid); 890 break; 891 case 'C': 892 printf(" - Disconnect\n"); 893 avrcp_disconnect(avrcp_cid); 894 break; 895 896 case 'd': 897 avdtp_sink_discover_stream_endpoints(avdtp_cid); 898 break; 899 case 'g': 900 avdtp_sink_get_capabilities(avdtp_cid, sep.seid); 901 break; 902 case 'a': 903 avdtp_sink_get_all_capabilities(avdtp_cid, sep.seid); 904 break; 905 case 'f': 906 avdtp_sink_get_configuration(avdtp_cid, sep.seid); 907 break; 908 case 's': 909 remote_configuration_bitmap = store_bit16(remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 910 remote_configuration.media_codec.media_type = AVDTP_AUDIO; 911 remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC; 912 remote_configuration.media_codec.media_codec_information_len = sizeof(media_sbc_codec_configuration); 913 remote_configuration.media_codec.media_codec_information = media_sbc_codec_configuration; 914 avdtp_sink_set_configuration(avdtp_cid, local_stream_endpoint->sep.seid, sep.seid, remote_configuration_bitmap, remote_configuration); 915 break; 916 case 'R': 917 remote_configuration_bitmap = store_bit16(remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 918 remote_configuration.media_codec.media_type = AVDTP_AUDIO; 919 remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC; 920 remote_configuration.media_codec.media_codec_information_len = sizeof(media_sbc_codec_reconfiguration); 921 remote_configuration.media_codec.media_codec_information = media_sbc_codec_reconfiguration; 922 avdtp_sink_reconfigure(avdtp_cid, local_stream_endpoint->sep.seid, sep.seid, remote_configuration_bitmap, remote_configuration); 923 break; 924 case 'o': 925 avdtp_sink_open_stream(avdtp_cid, local_stream_endpoint->sep.seid, sep.seid); 926 break; 927 case 'm': 928 avdtp_sink_start_stream(local_stream_endpoint->sep.seid); 929 break; 930 case 'A': 931 avdtp_sink_abort_stream(local_stream_endpoint->sep.seid); 932 break; 933 case 'S': 934 avdtp_sink_stop_stream(local_stream_endpoint->sep.seid); 935 break; 936 case 'P': 937 avdtp_sink_suspend(local_stream_endpoint->sep.seid); 938 break; 939 940 case '\n': 941 case '\r': 942 break; 943 case 'O': 944 printf(" - get play status\n"); 945 avrcp_get_play_status(avrcp_cid); 946 break; 947 case 'j': 948 printf(" - get now playing info\n"); 949 avrcp_get_now_playing_info(avrcp_cid); 950 break; 951 case 'k': 952 printf(" - play\n"); 953 avrcp_play(avrcp_cid); 954 break; 955 case 'K': 956 printf(" - stop\n"); 957 avrcp_stop(avrcp_cid); 958 break; 959 case 'L': 960 printf(" - pause\n"); 961 avrcp_pause(avrcp_cid); 962 break; 963 case 'u': 964 printf(" - start fast forward\n"); 965 avrcp_start_fast_forward(avrcp_cid); 966 break; 967 case 'U': 968 printf(" - stop fast forward\n"); 969 avrcp_stop_fast_forward(avrcp_cid); 970 break; 971 case 'n': 972 printf(" - start rewind\n"); 973 avrcp_start_rewind(avrcp_cid); 974 break; 975 case 'N': 976 printf(" - stop rewind\n"); 977 avrcp_stop_rewind(avrcp_cid); 978 break; 979 case 'i': 980 printf(" - forward\n"); 981 avrcp_forward(avrcp_cid); 982 break; 983 case 'I': 984 printf(" - backward\n"); 985 avrcp_backward(avrcp_cid); 986 break; 987 case 't': 988 printf(" - volume up\n"); 989 avrcp_volume_up(avrcp_cid); 990 break; 991 case 'T': 992 printf(" - volume down\n"); 993 avrcp_volume_down(avrcp_cid); 994 break; 995 case 'p': 996 printf(" - absolute volume of 50 percent\n"); 997 avrcp_set_absolute_volume(avrcp_cid, 50); 998 break; 999 case 'M': 1000 printf(" - mute\n"); 1001 avrcp_mute(avrcp_cid); 1002 break; 1003 case 'r': 1004 printf(" - skip\n"); 1005 avrcp_skip(avrcp_cid); 1006 break; 1007 case 'q': 1008 printf(" - query repeat and shuffle mode\n"); 1009 avrcp_query_shuffle_and_repeat_modes(avrcp_cid); 1010 break; 1011 case 'v': 1012 printf(" - repeat single track\n"); 1013 avrcp_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK); 1014 break; 1015 case 'x': 1016 printf(" - repeat all tracks\n"); 1017 avrcp_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_ALL_TRACKS); 1018 break; 1019 case 'X': 1020 printf(" - disable repeat mode\n"); 1021 avrcp_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_OFF); 1022 break; 1023 case 'z': 1024 printf(" - shuffle all tracks\n"); 1025 avrcp_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS); 1026 break; 1027 case 'Z': 1028 printf(" - disable shuffle mode\n"); 1029 avrcp_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_OFF); 1030 break; 1031 default: 1032 show_usage(); 1033 break; 1034 1035 } 1036 } 1037 #endif 1038 1039 1040 int btstack_main(int argc, const char * argv[]); 1041 int btstack_main(int argc, const char * argv[]){ 1042 1043 UNUSED(argc); 1044 (void)argv; 1045 1046 /* Register for HCI events */ 1047 hci_event_callback_registration.callback = &packet_handler; 1048 hci_add_event_handler(&hci_event_callback_registration); 1049 1050 l2cap_init(); 1051 // Initialize AVDTP Sink 1052 avdtp_sink_init(); 1053 avdtp_sink_register_packet_handler(&packet_handler); 1054 1055 //#ifndef SMG_BI 1056 local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, AVDTP_AUDIO); 1057 local_stream_endpoint->sep.seid = 1; 1058 avdtp_sink_register_media_transport_category(local_stream_endpoint->sep.seid); 1059 avdtp_sink_register_media_codec_category(local_stream_endpoint->sep.seid, AVDTP_AUDIO, AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities)); 1060 //#endif 1061 // uint8_t cp_type_lsb, uint8_t cp_type_msb, const uint8_t * cp_type_value, uint8_t cp_type_value_len 1062 // avdtp_sink_register_content_protection_category(seid, 2, 2, NULL, 0); 1063 1064 avdtp_sink_register_media_handler(&handle_l2cap_media_data_packet); 1065 printf("reistered media handler\n"); 1066 1067 // Initialize AVRCP COntroller 1068 avrcp_controller_init(); 1069 avrcp_register_packet_handler(&avrcp_packet_handler); 1070 1071 // Initialize SDP 1072 sdp_init(); 1073 // setup AVDTP sink 1074 memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer)); 1075 a2dp_sink_create_sdp_record(sdp_avdtp_sink_service_buffer, 0x10001, 1, NULL, NULL); 1076 sdp_register_service(sdp_avdtp_sink_service_buffer); 1077 1078 // setup AVRCP 1079 memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer)); 1080 avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, 0x10001, AVRCP_BROWSING_ENABLED, 1, NULL, NULL); 1081 sdp_register_service(sdp_avrcp_controller_service_buffer); 1082 1083 1084 gap_set_local_name("BTstack A2DP Sink Test"); 1085 gap_discoverable_control(1); 1086 gap_set_class_of_device(0x200408); 1087 printf("sdp, gap done\n"); 1088 1089 // turn on! 1090 hci_power_control(HCI_POWER_ON); 1091 1092 #ifdef HAVE_AUDIO_DMA 1093 static btstack_data_source_t hal_audio_dma_data_source; 1094 // set up polling data_source 1095 btstack_run_loop_set_data_source_handler(&hal_audio_dma_data_source, &hal_audio_dma_process); 1096 btstack_run_loop_enable_data_source_callbacks(&hal_audio_dma_data_source, DATA_SOURCE_CALLBACK_POLL); 1097 btstack_run_loop_add_data_source(&hal_audio_dma_data_source); 1098 #endif 1099 1100 #ifdef HAVE_BTSTACK_STDIN 1101 // parse human readable Bluetooth address 1102 sscanf_bd_addr(device_addr_string, device_addr); 1103 btstack_stdin_setup(stdin_process); 1104 #endif 1105 1106 return 0; 1107 } 1108