110277393SMatthias Ringwald /* 210277393SMatthias Ringwald * Copyright (C) 2022 BlueKitchen GmbH 310277393SMatthias Ringwald * 410277393SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 510277393SMatthias Ringwald * modification, are permitted provided that the following conditions 610277393SMatthias Ringwald * are met: 710277393SMatthias Ringwald * 810277393SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 910277393SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1010277393SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1110277393SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1210277393SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1310277393SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1410277393SMatthias Ringwald * contributors may be used to endorse or promote products derived 1510277393SMatthias Ringwald * from this software without specific prior written permission. 1610277393SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1710277393SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1810277393SMatthias Ringwald * monetary gain. 1910277393SMatthias Ringwald * 2010277393SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2110277393SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2210277393SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2310277393SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2410277393SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2510277393SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2610277393SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2710277393SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2810277393SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2910277393SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3010277393SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3110277393SMatthias Ringwald * SUCH DAMAGE. 3210277393SMatthias Ringwald * 3310277393SMatthias Ringwald * Please inquire about commercial licensing options at 3410277393SMatthias Ringwald * [email protected] 3510277393SMatthias Ringwald * 3610277393SMatthias Ringwald */ 3710277393SMatthias Ringwald 3810277393SMatthias Ringwald #define BTSTACK_FILE__ "le_audio_broadcast_sink.c" 3910277393SMatthias Ringwald 4010277393SMatthias Ringwald /* 4110277393SMatthias Ringwald * LE Audio Broadcast Sink 4210277393SMatthias Ringwald */ 4310277393SMatthias Ringwald 4410277393SMatthias Ringwald 4510277393SMatthias Ringwald #include "btstack_config.h" 4610277393SMatthias Ringwald 4710277393SMatthias Ringwald #include <stdint.h> 4810277393SMatthias Ringwald #include <stdio.h> 4910277393SMatthias Ringwald #include <stdlib.h> 5010277393SMatthias Ringwald #include <string.h> 5110277393SMatthias Ringwald #include <inttypes.h> 5210277393SMatthias Ringwald 5310277393SMatthias Ringwald #include "ad_parser.h" 54efd5f6d0SMatthias Ringwald #include "ble/att_server.h" 55efd5f6d0SMatthias Ringwald #include "ble/sm.h" 5610277393SMatthias Ringwald #include "bluetooth_data_types.h" 5710277393SMatthias Ringwald #include "bluetooth_gatt.h" 5810277393SMatthias Ringwald #include "btstack_debug.h" 5910277393SMatthias Ringwald #include "btstack_audio.h" 6010277393SMatthias Ringwald #include "btstack_event.h" 6110277393SMatthias Ringwald #include "btstack_run_loop.h" 6210277393SMatthias Ringwald #include "btstack_ring_buffer.h" 6310277393SMatthias Ringwald #include "btstack_stdin.h" 6410277393SMatthias Ringwald #include "btstack_util.h" 6510277393SMatthias Ringwald #include "gap.h" 6610277393SMatthias Ringwald #include "hci.h" 6710277393SMatthias Ringwald #include "hci_cmd.h" 682fd68da2SMatthias Ringwald #include "btstack_lc3.h" 69e40ee29aSMatthias Ringwald #include "btstack_lc3_google.h" 709a0d67ccSMatthias Ringwald #include "btstack_lc3plus_fraunhofer.h" 71efd5f6d0SMatthias Ringwald #include "l2cap.h" 72ff59851cSMilanka Ringwald #include "le-audio/le_audio_util.h" 73be81b159SMatthias Ringwald #include "le-audio/le_audio_base_parser.h" 745deb0bb6SMatthias Ringwald #include "le-audio/gatt-service/broadcast_audio_scan_service_server.h" 75efd5f6d0SMatthias Ringwald 76efd5f6d0SMatthias Ringwald #include "le_audio_broadcast_sink.h" 77bb81690eSMatthias Ringwald #include "le_audio_demo_util_sink.h" 787a5bad53SMatthias Ringwald 7910277393SMatthias Ringwald // max config 8010277393SMatthias Ringwald #define MAX_NUM_BIS 2 8110277393SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480 8210277393SMatthias Ringwald 8310277393SMatthias Ringwald // playback 8410277393SMatthias Ringwald #define MAX_NUM_LC3_FRAMES 5 8510277393SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4 8610277393SMatthias Ringwald #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE) 8710277393SMatthias Ringwald 8810277393SMatthias Ringwald static void show_usage(void); 8910277393SMatthias Ringwald 9010277393SMatthias Ringwald static const char * filename_wav = "le_audio_broadcast_sink.wav"; 9110277393SMatthias Ringwald 9210277393SMatthias Ringwald static enum { 9310277393SMatthias Ringwald APP_W4_WORKING, 9410277393SMatthias Ringwald APP_W4_BROADCAST_ADV, 9510277393SMatthias Ringwald APP_W4_PA_AND_BIG_INFO, 96b3a520acSMatthias Ringwald APP_W4_BROADCAST_CODE, 9710277393SMatthias Ringwald APP_W4_BIG_SYNC_ESTABLISHED, 9810277393SMatthias Ringwald APP_STREAMING, 9910277393SMatthias Ringwald APP_IDLE 10010277393SMatthias Ringwald } app_state = APP_W4_WORKING; 10110277393SMatthias Ringwald 102efd5f6d0SMatthias Ringwald static const uint8_t adv_sid = 0; 103efd5f6d0SMatthias Ringwald static le_advertising_set_t le_advertising_set; 104efd5f6d0SMatthias Ringwald static uint8_t adv_handle = 0; 105efd5f6d0SMatthias Ringwald 106dcd1707aSDirk Helbig static le_extended_advertising_parameters_t extended_params = { 107efd5f6d0SMatthias Ringwald .advertising_event_properties = 1, // connectable 108efd5f6d0SMatthias Ringwald .primary_advertising_interval_min = 0x4b0, // 750 ms 109efd5f6d0SMatthias Ringwald .primary_advertising_interval_max = 0x4b0, // 750 ms 110efd5f6d0SMatthias Ringwald .primary_advertising_channel_map = 7, 111dcd1707aSDirk Helbig .own_address_type = BD_ADDR_TYPE_LE_PUBLIC, 112efd5f6d0SMatthias Ringwald .peer_address_type = 0, 113efd5f6d0SMatthias Ringwald .peer_address = { 0 }, 114efd5f6d0SMatthias Ringwald .advertising_filter_policy = 0, 115efd5f6d0SMatthias Ringwald .advertising_tx_power = 10, // 10 dBm 116efd5f6d0SMatthias Ringwald .primary_advertising_phy = 1, // LE 1M PHY 117efd5f6d0SMatthias Ringwald .secondary_advertising_max_skip = 0, 118efd5f6d0SMatthias Ringwald .secondary_advertising_phy = 1, // LE 1M PHY 119efd5f6d0SMatthias Ringwald .advertising_sid = adv_sid, 120efd5f6d0SMatthias Ringwald .scan_request_notification_enable = 0, 121efd5f6d0SMatthias Ringwald }; 122efd5f6d0SMatthias Ringwald 123efd5f6d0SMatthias Ringwald static const uint8_t extended_adv_data[] = { 1243b0d944cSMatthias Ringwald // 16 bit service data, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE, 1253b0d944cSMatthias Ringwald 3, BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID, 12681a67409SMatthias Ringwald ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE & 0xff, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE >> 8, 12781a67409SMatthias Ringwald // name 12881a67409SMatthias Ringwald 5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'i', 'n', 'k' 12981a67409SMatthias Ringwald }; 13081a67409SMatthias Ringwald 13160d70dccSMatthias Ringwald #define BASS_NUM_CLIENTS 1 13260d70dccSMatthias Ringwald #define BASS_NUM_SOURCES 1 1337a236728SMatthias Ringwald static bass_source_data_t bass_source_new; 13460d70dccSMatthias Ringwald static bass_server_source_t bass_sources[BASS_NUM_SOURCES]; 135bebf6b57SMatthias Ringwald static bass_server_connection_t bass_clients[BASS_NUM_CLIENTS]; 13660d70dccSMatthias Ringwald 13710277393SMatthias Ringwald // 13810277393SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 139efd5f6d0SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 14010277393SMatthias Ringwald 14110277393SMatthias Ringwald static bool have_base; 14210277393SMatthias Ringwald static bool have_big_info; 1430707de65SMatthias Ringwald static bool have_broadcast_code; 144ffbedad9SMatthias Ringwald static bool standalone_mode; 1450707de65SMatthias Ringwald static uint32_t bis_sync_mask; 14610277393SMatthias Ringwald 1474a5b1c65SMatthias Ringwald uint16_t samples_received; 1484a5b1c65SMatthias Ringwald uint16_t samples_dropped; 14910277393SMatthias Ringwald 15010277393SMatthias Ringwald // remote info 15110277393SMatthias Ringwald static char remote_name[20]; 15210277393SMatthias Ringwald static bd_addr_t remote; 15310277393SMatthias Ringwald static bd_addr_type_t remote_type; 15410277393SMatthias Ringwald static uint8_t remote_sid; 15510277393SMatthias Ringwald static bool count_mode; 15610277393SMatthias Ringwald static bool pts_mode; 1572fe0253dSMatthias Ringwald static bool nrf5340_audio_demo; 1582fe0253dSMatthias Ringwald 15910277393SMatthias Ringwald 16010277393SMatthias Ringwald // broadcast info 16110277393SMatthias Ringwald static const uint8_t big_handle = 1; 16210277393SMatthias Ringwald static hci_con_handle_t sync_handle; 16310277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS]; 164ac95ea81SMatthias Ringwald static uint8_t encryption; 1650707de65SMatthias Ringwald static uint8_t broadcast_code[16]; 16610277393SMatthias Ringwald 1672de3ad61SMatthias Ringwald static btstack_timer_source_t broadcast_sink_pa_sync_timer; 1682de3ad61SMatthias Ringwald 1695c0d69beSMatthias Ringwald // BIG Sync 1705c0d69beSMatthias Ringwald static le_audio_big_sync_t big_sync_storage; 1715c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params; 1725c0d69beSMatthias Ringwald 17310277393SMatthias Ringwald // lc3 codec config 1744a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz; 1752fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration; 17610277393SMatthias Ringwald static uint16_t number_samples_per_frame; 17710277393SMatthias Ringwald static uint16_t octets_per_frame; 17810277393SMatthias Ringwald static uint8_t num_bis; 17910277393SMatthias Ringwald 18010277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ 1812fe0253dSMatthias Ringwald // nRF534_audio quirk - no BASE in periodic advertisement 1822fe0253dSMatthias Ringwald if (nrf5340_audio_demo){ 1832fe0253dSMatthias Ringwald // hard coded config LC3 1842fe0253dSMatthias Ringwald // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame 1852fe0253dSMatthias Ringwald count_mode = 0; 1862fe0253dSMatthias Ringwald pts_mode = 0; 1872fe0253dSMatthias Ringwald num_bis = 1; 1882fe0253dSMatthias Ringwald sampling_frequency_hz = 48000; 1892fe0253dSMatthias Ringwald frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US; 1902fe0253dSMatthias Ringwald octets_per_frame = 120; 1912fe0253dSMatthias Ringwald have_base = true; 1922fe0253dSMatthias Ringwald return; 1932fe0253dSMatthias Ringwald } 1942fe0253dSMatthias Ringwald 19510277393SMatthias Ringwald // periodic advertisement contains the BASE 19610277393SMatthias Ringwald // TODO: BASE might be split across multiple advertisements 19710277393SMatthias Ringwald const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); 19810277393SMatthias Ringwald uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); 1998206d906SMatthias Ringwald uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); 2008206d906SMatthias Ringwald 2018206d906SMatthias Ringwald if (adv_status != 0) { 2028206d906SMatthias Ringwald printf("Periodic Advertisement (status %u): ", adv_status); 2038206d906SMatthias Ringwald printf_hexdump(adv_data, adv_size); 2048206d906SMatthias Ringwald return; 2058206d906SMatthias Ringwald } 20610277393SMatthias Ringwald 207be81b159SMatthias Ringwald le_audio_base_parser_t parser; 208be81b159SMatthias Ringwald bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size); 209be81b159SMatthias Ringwald if (ok == false){ 210be81b159SMatthias Ringwald return; 211be81b159SMatthias Ringwald } 21210277393SMatthias Ringwald have_base = true; 213be81b159SMatthias Ringwald 21410277393SMatthias Ringwald printf("BASE:\n"); 215be81b159SMatthias Ringwald uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser); 21610277393SMatthias Ringwald printf("- presentation delay: %"PRIu32" us\n", presentation_delay); 217be81b159SMatthias Ringwald uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser); 2187a236728SMatthias Ringwald // Cache in new source struct 2197a236728SMatthias Ringwald bass_source_new.subgroups_num = num_subgroups; 22010277393SMatthias Ringwald printf("- num subgroups: %u\n", num_subgroups); 22110277393SMatthias Ringwald uint8_t i; 22210277393SMatthias Ringwald for (i=0;i<num_subgroups;i++){ 22310277393SMatthias Ringwald // Level 2: Subgroup Level 224109dd080SMatthias Ringwald printf(" - Subgroup %u\n", i); 225be81b159SMatthias Ringwald num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser); 22610277393SMatthias Ringwald printf(" - num bis[%u]: %u\n", i, num_bis); 227109dd080SMatthias Ringwald uint8_t codec_specific_configuration_length = le_audio_base_parser_subgroup_get_codec_specific_configuration_length(&parser); 228be81b159SMatthias Ringwald const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser); 22910277393SMatthias Ringwald printf(" - codec specific config[%u]: ", i); 23010277393SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 23110277393SMatthias Ringwald // parse config to get sampling frequency and frame duration 23210277393SMatthias Ringwald uint8_t codec_offset = 0; 23310277393SMatthias Ringwald while ((codec_offset + 1) < codec_specific_configuration_length){ 23410277393SMatthias Ringwald uint8_t ltv_len = codec_specific_configuration[codec_offset++]; 23510277393SMatthias Ringwald uint8_t ltv_type = codec_specific_configuration[codec_offset]; 23610277393SMatthias Ringwald const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 }; 23710277393SMatthias Ringwald uint8_t sampling_frequency_index; 23810277393SMatthias Ringwald uint8_t frame_duration_index; 23910277393SMatthias Ringwald switch (ltv_type){ 24010277393SMatthias Ringwald case 0x01: // sampling frequency 24110277393SMatthias Ringwald sampling_frequency_index = codec_specific_configuration[codec_offset+1]; 24210277393SMatthias Ringwald // TODO: check range 24310277393SMatthias Ringwald sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1]; 2444a5b1c65SMatthias Ringwald printf(" - sampling frequency[%u]: %u\n", i, sampling_frequency_hz); 24510277393SMatthias Ringwald break; 24610277393SMatthias Ringwald case 0x02: // 0 = 7.5, 1 = 10 ms 24710277393SMatthias Ringwald frame_duration_index = codec_specific_configuration[codec_offset+1]; 248ff59851cSMilanka Ringwald frame_duration = le_audio_util_get_btstack_lc3_frame_duration(frame_duration_index); 249ff59851cSMilanka Ringwald printf(" - frame duration[%u]: %u us\n", i, le_audio_get_frame_duration_us(frame_duration_index)); 25010277393SMatthias Ringwald break; 25110277393SMatthias Ringwald case 0x04: // octets per coding frame 25210277393SMatthias Ringwald octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1); 25310277393SMatthias Ringwald printf(" - octets per codec frame[%u]: %u\n", i, octets_per_frame); 25410277393SMatthias Ringwald break; 25510277393SMatthias Ringwald default: 25610277393SMatthias Ringwald break; 25710277393SMatthias Ringwald } 25810277393SMatthias Ringwald codec_offset += ltv_len; 25910277393SMatthias Ringwald } 260be81b159SMatthias Ringwald uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser); 261be81b159SMatthias Ringwald const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser); 26210277393SMatthias Ringwald printf(" - meta data[%u]: ", i); 26310277393SMatthias Ringwald printf_hexdump(meta_data, metadata_length); 26410277393SMatthias Ringwald uint8_t k; 26510277393SMatthias Ringwald for (k=0;k<num_bis;k++){ 266109dd080SMatthias Ringwald printf(" - BIS %u\n", k); 26710277393SMatthias Ringwald // Level 3: BIS Level 268be81b159SMatthias Ringwald uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser); 269b3a520acSMatthias Ringwald if ((bis_index == 0) || (bis_index > 30)){ 270b3a520acSMatthias Ringwald continue; 271b3a520acSMatthias Ringwald } 2727a236728SMatthias Ringwald 2730707de65SMatthias Ringwald // collect bis sync mask 274b3a520acSMatthias Ringwald bis_sync_mask |= 1 << (bis_index - 1); 2757a236728SMatthias Ringwald 276be81b159SMatthias Ringwald bass_source_new.subgroups[i].metadata_length = 0; 277be81b159SMatthias Ringwald memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t)); 278be81b159SMatthias Ringwald 27910277393SMatthias Ringwald printf(" - bis index[%u][%u]: %u\n", i, k, bis_index); 280be81b159SMatthias Ringwald codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser); 281be81b159SMatthias Ringwald codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser); 28210277393SMatthias Ringwald printf(" - codec specific config[%u][%u]: ", i, k); 283be81b159SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 284be81b159SMatthias Ringwald // parse next BIS 285be81b159SMatthias Ringwald le_audio_base_parser_bis_next(&parser); 28610277393SMatthias Ringwald } 287be81b159SMatthias Ringwald 288be81b159SMatthias Ringwald // parse next subgroup 289be81b159SMatthias Ringwald le_audio_base_parser_subgroup_next(&parser); 29010277393SMatthias Ringwald } 29110277393SMatthias Ringwald } 29210277393SMatthias Ringwald 29310277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){ 29410277393SMatthias Ringwald printf("BIG Info advertising report\n"); 29510277393SMatthias Ringwald sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); 296ac95ea81SMatthias Ringwald encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet); 297ac95ea81SMatthias Ringwald if (encryption) { 298b3a520acSMatthias Ringwald printf("Stream is encrypted\n"); 299ac95ea81SMatthias Ringwald } 30010277393SMatthias Ringwald have_big_info = true; 30110277393SMatthias Ringwald } 30210277393SMatthias Ringwald 30310277393SMatthias Ringwald static void enter_create_big_sync(void){ 30410277393SMatthias Ringwald // stop scanning 30510277393SMatthias Ringwald gap_stop_scan(); 30610277393SMatthias Ringwald 307bb81690eSMatthias Ringwald // init sink 308bb81690eSMatthias Ringwald uint16_t iso_interval_1250us = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 6 : 8; 309bb81690eSMatthias Ringwald uint8_t pre_transmission_offset = 1; 310bb81690eSMatthias Ringwald le_audio_demo_util_sink_configure_broadcast(num_bis, 1, sampling_frequency_hz, frame_duration, octets_per_frame, 311bb81690eSMatthias Ringwald iso_interval_1250us, pre_transmission_offset); 31210277393SMatthias Ringwald 3135c0d69beSMatthias Ringwald big_sync_params.big_handle = big_handle; 3145c0d69beSMatthias Ringwald big_sync_params.sync_handle = sync_handle; 315ac95ea81SMatthias Ringwald big_sync_params.encryption = encryption; 316ac95ea81SMatthias Ringwald if (encryption) { 317ac95ea81SMatthias Ringwald memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16); 318ac95ea81SMatthias Ringwald } else { 3195c0d69beSMatthias Ringwald memset(big_sync_params.broadcast_code, 0, 16); 320ac95ea81SMatthias Ringwald } 3215c0d69beSMatthias Ringwald big_sync_params.mse = 0; 3225c0d69beSMatthias Ringwald big_sync_params.big_sync_timeout_10ms = 100; 3235c0d69beSMatthias Ringwald big_sync_params.num_bis = num_bis; 3245c0d69beSMatthias Ringwald uint8_t i; 3255c0d69beSMatthias Ringwald printf("BIG Create Sync for BIS: "); 3265c0d69beSMatthias Ringwald for (i=0;i<num_bis;i++){ 3275c0d69beSMatthias Ringwald big_sync_params.bis_indices[i] = i + 1; 3285c0d69beSMatthias Ringwald printf("%u ", big_sync_params.bis_indices[i]); 3295c0d69beSMatthias Ringwald } 3305c0d69beSMatthias Ringwald printf("\n"); 3315c0d69beSMatthias Ringwald app_state = APP_W4_BIG_SYNC_ESTABLISHED; 3325c0d69beSMatthias Ringwald gap_big_sync_create(&big_sync_storage, &big_sync_params); 33310277393SMatthias Ringwald } 33410277393SMatthias Ringwald 335111967cbSMatthias Ringwald static void start_scanning() { 336111967cbSMatthias Ringwald app_state = APP_W4_BROADCAST_ADV; 337400a29ddSMatthias Ringwald have_base = false; 338400a29ddSMatthias Ringwald have_big_info = false; 339111967cbSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 0); 340111967cbSMatthias Ringwald gap_start_scan(); 341111967cbSMatthias Ringwald printf("Start scan..\n"); 342111967cbSMatthias Ringwald } 343111967cbSMatthias Ringwald 34481a67409SMatthias Ringwald static void setup_advertising() { 345dcd1707aSDirk Helbig bd_addr_t local_addr; 346dcd1707aSDirk Helbig gap_local_bd_addr(local_addr); 3476d708481SDirk Helbig bool local_address_invalid = btstack_is_null_bd_addr( local_addr ); 348dcd1707aSDirk Helbig if( local_address_invalid ) { 349dcd1707aSDirk Helbig extended_params.own_address_type = BD_ADDR_TYPE_LE_RANDOM; 350dcd1707aSDirk Helbig } 351efd5f6d0SMatthias Ringwald gap_extended_advertising_setup(&le_advertising_set, &extended_params, &adv_handle); 352dcd1707aSDirk Helbig if( local_address_invalid ) { 353dcd1707aSDirk Helbig bd_addr_t random_address = { 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01 }; 354dcd1707aSDirk Helbig gap_extended_advertising_set_random_address( adv_handle, random_address ); 355dcd1707aSDirk Helbig } 356efd5f6d0SMatthias Ringwald gap_extended_advertising_set_adv_data(adv_handle, sizeof(extended_adv_data), extended_adv_data); 357efd5f6d0SMatthias Ringwald gap_extended_advertising_start(adv_handle, 0, 0); 35881a67409SMatthias Ringwald } 35981a67409SMatthias Ringwald 360b3a520acSMatthias Ringwald static void got_base_and_big_info() { 361b3a520acSMatthias Ringwald // add source 362b3a520acSMatthias Ringwald if (standalone_mode) { 363b3a520acSMatthias Ringwald printf("BASS: add Broadcast Source\n"); 364b3a520acSMatthias Ringwald // add source 365b3a520acSMatthias Ringwald uint8_t source_index = 0; 366abf8479fSMatthias Ringwald broadcast_audio_scan_service_server_add_source(&bass_source_new, &source_index); 367b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 368b3a520acSMatthias Ringwald } 369b3a520acSMatthias Ringwald 370*db75eafdSMatthias Ringwald // update num subgroups from BASE 371*db75eafdSMatthias Ringwald bass_sources[0].data.subgroups_num = bass_source_new.subgroups_num; 372*db75eafdSMatthias Ringwald 373b3a520acSMatthias Ringwald if ((encryption == false) || have_broadcast_code){ 374b3a520acSMatthias Ringwald enter_create_big_sync(); 375b3a520acSMatthias Ringwald } else { 376b3a520acSMatthias Ringwald printf("BASS: request Broadcast Code\n"); 377b3a520acSMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED; 378b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 379b3a520acSMatthias Ringwald app_state = APP_W4_BROADCAST_CODE; 380b3a520acSMatthias Ringwald } 381b3a520acSMatthias Ringwald } 382b3a520acSMatthias Ringwald 383ec363a3dSMatthias Ringwald static void start_pa_sync(bd_addr_type_t source_type, bd_addr_t source_addr) { 384ec363a3dSMatthias Ringwald // ignore other advertisements 385ec363a3dSMatthias Ringwald gap_whitelist_add(source_type, source_addr); 386ec363a3dSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 1); 387ec363a3dSMatthias Ringwald // sync to PA 388ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_clear(); 389ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_add(source_type, source_addr, remote_sid); 390ec363a3dSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 391ec363a3dSMatthias Ringwald printf("Start Periodic Advertising Sync\n"); 392ec363a3dSMatthias Ringwald gap_periodic_advertising_create_sync(0x01, remote_sid, source_type, source_addr, 0, 1000, 0); 393ec363a3dSMatthias Ringwald gap_start_scan(); 394ec363a3dSMatthias Ringwald } 395ec363a3dSMatthias Ringwald 3967c819d90SMatthias Ringwald static void stop_pa_sync(void) { 3977c819d90SMatthias Ringwald app_state = APP_IDLE; 3987c819d90SMatthias Ringwald le_audio_demo_util_sink_close(); 3997c819d90SMatthias Ringwald printf("Terminate BIG SYNC\n"); 4007c819d90SMatthias Ringwald gap_big_sync_terminate(big_handle); 4017c819d90SMatthias Ringwald int i; 4027c819d90SMatthias Ringwald for (i=0;i<num_bis;i++){ 4037c819d90SMatthias Ringwald if (bis_con_handles[i] != HCI_CON_HANDLE_INVALID) { 4047c819d90SMatthias Ringwald gap_disconnect(bis_con_handles[i]); 4057c819d90SMatthias Ringwald } 4067c819d90SMatthias Ringwald } 4077c819d90SMatthias Ringwald 4087c819d90SMatthias Ringwald } 4097c819d90SMatthias Ringwald 41010277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 41110277393SMatthias Ringwald UNUSED(channel); 41210277393SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 41310277393SMatthias Ringwald switch (packet[0]) { 41410277393SMatthias Ringwald case BTSTACK_EVENT_STATE: 41510277393SMatthias Ringwald switch(btstack_event_state_get_state(packet)) { 41610277393SMatthias Ringwald case HCI_STATE_WORKING: 417400a29ddSMatthias Ringwald app_state = APP_IDLE; 41859cc662fSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync transfers 41959cc662fSMatthias Ringwald setup_advertising(); 42059cc662fSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 42159cc662fSMatthias Ringwald 4229a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE 423111967cbSMatthias Ringwald start_scanning(); 4249a0d67ccSMatthias Ringwald #else 4259a0d67ccSMatthias Ringwald show_usage(); 4269a0d67ccSMatthias Ringwald #endif 42710277393SMatthias Ringwald break; 42810277393SMatthias Ringwald case HCI_STATE_OFF: 42910277393SMatthias Ringwald printf("Goodbye\n"); 43010277393SMatthias Ringwald exit(0); 43110277393SMatthias Ringwald break; 43210277393SMatthias Ringwald default: 43310277393SMatthias Ringwald break; 43410277393SMatthias Ringwald } 43510277393SMatthias Ringwald break; 43610277393SMatthias Ringwald case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 43710277393SMatthias Ringwald { 43810277393SMatthias Ringwald if (app_state != APP_W4_BROADCAST_ADV) break; 43910277393SMatthias Ringwald 44010277393SMatthias Ringwald gap_event_extended_advertising_report_get_address(packet, remote); 44110277393SMatthias Ringwald uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 44210277393SMatthias Ringwald const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 44310277393SMatthias Ringwald 44410277393SMatthias Ringwald ad_context_t context; 44510277393SMatthias Ringwald bool found = false; 44610277393SMatthias Ringwald remote_name[0] = '\0'; 44710277393SMatthias Ringwald uint16_t uuid; 44870718632SMatthias Ringwald uint32_t broadcast_id; 44910277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 450*db75eafdSMatthias Ringwald const uint8_t data_type = ad_iterator_get_data_type(&context); 451*db75eafdSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 45210277393SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 45310277393SMatthias Ringwald switch (data_type){ 45410277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 45510277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 45610277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){ 45770718632SMatthias Ringwald broadcast_id = little_endian_read_24(data, 2); 45810277393SMatthias Ringwald found = true; 45910277393SMatthias Ringwald } 46010277393SMatthias Ringwald break; 46110277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 46210277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 463*db75eafdSMatthias Ringwald data_size = btstack_min(sizeof(remote_name) - 1, data_size); 464*db75eafdSMatthias Ringwald memcpy(remote_name, data, data_size); 465*db75eafdSMatthias Ringwald remote_name[data_size] = 0; 4662fe0253dSMatthias Ringwald // support for nRF5340 Audio DK 4672fe0253dSMatthias Ringwald if (strncmp("NRF5340", remote_name, 7) == 0){ 4682fe0253dSMatthias Ringwald nrf5340_audio_demo = true; 4692fe0253dSMatthias Ringwald found = true; 4702fe0253dSMatthias Ringwald } 47110277393SMatthias Ringwald break; 47210277393SMatthias Ringwald default: 47310277393SMatthias Ringwald break; 47410277393SMatthias Ringwald } 47510277393SMatthias Ringwald } 47610277393SMatthias Ringwald if (!found) break; 47710277393SMatthias Ringwald remote_type = gap_event_extended_advertising_report_get_address_type(packet); 47810277393SMatthias Ringwald remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 47910277393SMatthias Ringwald pts_mode = strncmp("PTS-", remote_name, 4) == 0; 48010277393SMatthias Ringwald count_mode = strncmp("COUNT", remote_name, 5) == 0; 48170718632SMatthias Ringwald printf("Broadcast sink found, addr %s, name: '%s' (pts-mode: %u, count: %u), Broadcast_ID 0%06x\n", bd_addr_to_str(remote), remote_name, pts_mode, count_mode, broadcast_id); 4827a236728SMatthias Ringwald 4837a236728SMatthias Ringwald // setup bass source info 4847a236728SMatthias Ringwald bass_source_new.address_type = remote_type; 4857a236728SMatthias Ringwald memcpy(bass_source_new.address, remote, 6); 4867a236728SMatthias Ringwald bass_source_new.adv_sid = remote_sid; 4877a236728SMatthias Ringwald bass_source_new.broadcast_id = broadcast_id; 4887a236728SMatthias Ringwald bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE; 4897a236728SMatthias Ringwald bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet); 490ec363a3dSMatthias Ringwald 491ec363a3dSMatthias Ringwald // start pa sync 492ec363a3dSMatthias Ringwald start_pa_sync(remote_type, remote); 49310277393SMatthias Ringwald break; 49410277393SMatthias Ringwald } 49510277393SMatthias Ringwald 49610277393SMatthias Ringwald case HCI_EVENT_LE_META: 49710277393SMatthias Ringwald switch(hci_event_le_meta_get_subevent_code(packet)) { 49892f80bb0SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED: 499b3a520acSMatthias Ringwald // PAST implies broadcast source has been added by client 500ffbedad9SMatthias Ringwald printf("Periodic advertising sync transfer received\n"); 5012de3ad61SMatthias Ringwald btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer); 502ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 50392f80bb0SMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 50492f80bb0SMatthias Ringwald break; 50510277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 506400a29ddSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet); 507400a29ddSMatthias Ringwald printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle); 5087a236728SMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 50910277393SMatthias Ringwald break; 51010277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 511ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 51210277393SMatthias Ringwald if (have_base) break; 51310277393SMatthias Ringwald handle_periodic_advertisement(packet, size); 514b3a520acSMatthias Ringwald if (have_base && have_big_info){ 515b3a520acSMatthias Ringwald got_base_and_big_info(); 51610277393SMatthias Ringwald } 51710277393SMatthias Ringwald break; 51810277393SMatthias Ringwald case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 519ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 52010277393SMatthias Ringwald if (have_big_info) break; 52110277393SMatthias Ringwald handle_big_info(packet, size); 522b3a520acSMatthias Ringwald if (have_base && have_big_info){ 523b3a520acSMatthias Ringwald got_base_and_big_info(); 52410277393SMatthias Ringwald } 52510277393SMatthias Ringwald break; 526111967cbSMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 527111967cbSMatthias Ringwald printf("BIG Sync Lost\n"); 528111967cbSMatthias Ringwald { 529111967cbSMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 530111967cbSMatthias Ringwald if (sink != NULL) { 531111967cbSMatthias Ringwald sink->stop_stream(); 532111967cbSMatthias Ringwald sink->close(); 533111967cbSMatthias Ringwald } 534111967cbSMatthias Ringwald } 535111967cbSMatthias Ringwald // start over 536ffbedad9SMatthias Ringwald if (!standalone_mode) break; 537111967cbSMatthias Ringwald start_scanning(); 538111967cbSMatthias Ringwald break; 53910277393SMatthias Ringwald default: 54010277393SMatthias Ringwald break; 54110277393SMatthias Ringwald } 54210277393SMatthias Ringwald break; 5435c0d69beSMatthias Ringwald case HCI_EVENT_META_GAP: 5445c0d69beSMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)){ 5455c0d69beSMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_CREATED: { 5465c0d69beSMatthias Ringwald printf("BIG Sync created with BIS Connection handles: "); 5475c0d69beSMatthias Ringwald uint8_t i; 54810277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 5495c0d69beSMatthias Ringwald bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i); 5505c0d69beSMatthias Ringwald printf("0x%04x ", bis_con_handles[i]); 55110277393SMatthias Ringwald } 5520707de65SMatthias Ringwald printf("\n"); 55310277393SMatthias Ringwald app_state = APP_STREAMING; 554151e8cb9SMatthias Ringwald printf("Start receiving\n"); 555ffbedad9SMatthias Ringwald 556ffbedad9SMatthias Ringwald // update BIS Sync state 5570707de65SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask; 558ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 55910277393SMatthias Ringwald break; 5605c0d69beSMatthias Ringwald } 561e38308a2SMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_STOPPED: 562e38308a2SMatthias Ringwald printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet)); 563e38308a2SMatthias Ringwald break; 5645c0d69beSMatthias Ringwald default: 5655c0d69beSMatthias Ringwald break; 5665c0d69beSMatthias Ringwald } 56710277393SMatthias Ringwald break; 568efd5f6d0SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 569efd5f6d0SMatthias Ringwald printf("Just Works requested\n"); 570efd5f6d0SMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 571efd5f6d0SMatthias Ringwald break; 57210277393SMatthias Ringwald default: 57310277393SMatthias Ringwald break; 57410277393SMatthias Ringwald } 57510277393SMatthias Ringwald } 57610277393SMatthias Ringwald 57710277393SMatthias Ringwald static void show_usage(void){ 57810277393SMatthias Ringwald printf("\n--- LE Audio Broadcast Sink Test Console ---\n"); 5799a0d67ccSMatthias Ringwald printf("s - start scanning\n"); 5809a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 5819a0d67ccSMatthias Ringwald printf("q - use LC3plus decoder if 10 ms ISO interval is used\n"); 5829a0d67ccSMatthias Ringwald #endif 5830707de65SMatthias Ringwald printf("e - set broadcast code to 0x11111111111111111111111111111111\n"); 584400a29ddSMatthias Ringwald printf("t - terminate BIS streams\n"); 58510277393SMatthias Ringwald printf("---\n"); 58610277393SMatthias Ringwald } 58710277393SMatthias Ringwald 58810277393SMatthias Ringwald static void stdin_process(char c){ 58910277393SMatthias Ringwald switch (c){ 5909a0d67ccSMatthias Ringwald case 's': 591400a29ddSMatthias Ringwald if (app_state != APP_IDLE) break; 592ffbedad9SMatthias Ringwald standalone_mode = true; 5939a0d67ccSMatthias Ringwald start_scanning(); 5949a0d67ccSMatthias Ringwald break; 5950707de65SMatthias Ringwald case 'e': 5960707de65SMatthias Ringwald printf("Set broadcast code to 0x11111111111111111111111111111111\n"); 5970707de65SMatthias Ringwald memset(broadcast_code, 0x11, 16); 5980707de65SMatthias Ringwald have_broadcast_code = true; 5990707de65SMatthias Ringwald break; 6009a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 6019a0d67ccSMatthias Ringwald case 'q': 602151e8cb9SMatthias Ringwald printf("Use LC3plus decoder for 10 ms ISO interval...\n"); 603f7bf5159SMatthias Ringwald le_audio_demo_util_sink_enable_lc3plus(true); 6049a0d67ccSMatthias Ringwald break; 6059a0d67ccSMatthias Ringwald #endif 606400a29ddSMatthias Ringwald case 't': 607400a29ddSMatthias Ringwald switch (app_state){ 608400a29ddSMatthias Ringwald case APP_STREAMING: 609400a29ddSMatthias Ringwald case APP_W4_BIG_SYNC_ESTABLISHED: 6107c819d90SMatthias Ringwald stop_pa_sync(); 611400a29ddSMatthias Ringwald break; 612400a29ddSMatthias Ringwald default: 613400a29ddSMatthias Ringwald break; 614400a29ddSMatthias Ringwald } 615400a29ddSMatthias Ringwald break; 61610277393SMatthias Ringwald case '\n': 61710277393SMatthias Ringwald case '\r': 61810277393SMatthias Ringwald break; 61910277393SMatthias Ringwald default: 62010277393SMatthias Ringwald show_usage(); 62110277393SMatthias Ringwald break; 62210277393SMatthias Ringwald 62310277393SMatthias Ringwald } 62410277393SMatthias Ringwald } 6252de3ad61SMatthias Ringwald 626bb81690eSMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 627bb81690eSMatthias Ringwald // get stream_index from con_handle 628bb81690eSMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 629bb81690eSMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 630bb81690eSMatthias Ringwald uint8_t i; 631bb81690eSMatthias Ringwald for (i=0;i<num_bis;i++){ 632bb81690eSMatthias Ringwald if (bis_con_handles[i] == con_handle) { 633bb81690eSMatthias Ringwald le_audio_demo_util_sink_receive(i, packet, size); 634bb81690eSMatthias Ringwald } 635bb81690eSMatthias Ringwald } 636bb81690eSMatthias Ringwald } 637bb81690eSMatthias Ringwald 6382de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){ 6392de3ad61SMatthias Ringwald UNUSED(ts); 6402de3ad61SMatthias Ringwald printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n"); 6412de3ad61SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST); 6422de3ad61SMatthias Ringwald } 6432de3ad61SMatthias Ringwald 64460d70dccSMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 64560d70dccSMatthias Ringwald UNUSED(packet_type); 64660d70dccSMatthias Ringwald UNUSED(channel); 647ffbedad9SMatthias Ringwald btstack_assert (packet_type == HCI_EVENT_PACKET); 648ffbedad9SMatthias Ringwald btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META); 649ec363a3dSMatthias Ringwald uint8_t source_id; 650ffbedad9SMatthias Ringwald printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet)); 651ffbedad9SMatthias Ringwald printf_hexdump(packet, size); 652ffbedad9SMatthias Ringwald switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 653bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED: 654ec363a3dSMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 655ffbedad9SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n", 656ec363a3dSMatthias Ringwald source_id, 657bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); 658bebf6b57SMatthias Ringwald switch (gattservice_subevent_bass_server_source_added_get_pa_sync(packet)){ 6592de3ad61SMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE: 6602de3ad61SMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n"); 661ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST); 6622de3ad61SMatthias Ringwald // start timeout 6632de3ad61SMatthias Ringwald btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler); 6642de3ad61SMatthias Ringwald btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000); // 10 seconds 6652de3ad61SMatthias Ringwald btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer); 6662de3ad61SMatthias Ringwald break; 667ec363a3dSMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_NOT_AVAILABLE: 668ec363a3dSMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Start Periodic Advertising Sync\n"); 669ec363a3dSMatthias Ringwald start_pa_sync(bass_sources[source_id].data.address_type, bass_sources[source_id].data.address); 670ec363a3dSMatthias Ringwald break; 6712de3ad61SMatthias Ringwald } 672ffbedad9SMatthias Ringwald break; 673bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED: 674ffbedad9SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n", 675bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_source_id(packet), 676bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); 677ffbedad9SMatthias Ringwald // handle 'bis sync == 0' 678ffbedad9SMatthias Ringwald printf("PA Sync %u, bis_sync[0] = %u\n", bass_sources[0].data.pa_sync, bass_sources[0].data.subgroups[0].bis_sync); 679ffbedad9SMatthias Ringwald if (bass_sources[0].data.subgroups[0].bis_sync == 0){ 6807c819d90SMatthias Ringwald printf("Stop PA Sync\n"); 6817c819d90SMatthias Ringwald stop_pa_sync(); 682ffbedad9SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = 0; 6837c819d90SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NOT_SYNCHRONIZED_TO_PA); 684ffbedad9SMatthias Ringwald } 6850707de65SMatthias Ringwald break; 686ec363a3dSMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED: 687ec363a3dSMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED, source_id 0x%04x\n", 688ec363a3dSMatthias Ringwald gattservice_subevent_bass_server_source_deleted_get_source_id(packet)); 689ec363a3dSMatthias Ringwald break; 690bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE: 691bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code); 6920707de65SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: "); 6930707de65SMatthias Ringwald printf_hexdump(broadcast_code, 16); 6940707de65SMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING; 6950707de65SMatthias Ringwald if (have_base && have_big_info){ 6960707de65SMatthias Ringwald enter_create_big_sync(); 6970707de65SMatthias Ringwald } 6980707de65SMatthias Ringwald break; 699ffbedad9SMatthias Ringwald default: 700ffbedad9SMatthias Ringwald break; 701ffbedad9SMatthias Ringwald } 70260d70dccSMatthias Ringwald } 70310277393SMatthias Ringwald 70410277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 70510277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 70610277393SMatthias Ringwald (void) argv; 70710277393SMatthias Ringwald (void) argc; 70810277393SMatthias Ringwald 709efd5f6d0SMatthias Ringwald l2cap_init(); 710efd5f6d0SMatthias Ringwald sm_init(); 711efd5f6d0SMatthias Ringwald 712efd5f6d0SMatthias Ringwald // setup ATT server 713efd5f6d0SMatthias Ringwald att_server_init(profile_data, NULL, NULL); 714efd5f6d0SMatthias Ringwald 71510277393SMatthias Ringwald // register for HCI events 71610277393SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 71710277393SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 71810277393SMatthias Ringwald 71910277393SMatthias Ringwald // register for ISO Packet 72010277393SMatthias Ringwald hci_register_iso_packet_handler(&iso_packet_handler); 72110277393SMatthias Ringwald 722efd5f6d0SMatthias Ringwald // register for SM events 723efd5f6d0SMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 724efd5f6d0SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 725efd5f6d0SMatthias Ringwald 72660d70dccSMatthias Ringwald // setup BASS Server 72760d70dccSMatthias Ringwald broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients); 72860d70dccSMatthias Ringwald broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler); 72960d70dccSMatthias Ringwald 73033bea8deSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync trasnfers 73133bea8deSMatthias Ringwald setup_advertising(); 73233bea8deSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 73333bea8deSMatthias Ringwald 734a2dc544dSMatthias Ringwald // setup audio processing 735a2dc544dSMatthias Ringwald le_audio_demo_util_sink_init("le_audio_broadcast_sink.wav"); 736a2dc544dSMatthias Ringwald 73710277393SMatthias Ringwald // turn on! 73810277393SMatthias Ringwald hci_power_control(HCI_POWER_ON); 73910277393SMatthias Ringwald 74010277393SMatthias Ringwald btstack_stdin_setup(stdin_process); 74110277393SMatthias Ringwald return 0; 74210277393SMatthias Ringwald } 743