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