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 370b3a520acSMatthias Ringwald if ((encryption == false) || have_broadcast_code){ 371b3a520acSMatthias Ringwald enter_create_big_sync(); 372b3a520acSMatthias Ringwald } else { 373b3a520acSMatthias Ringwald printf("BASS: request Broadcast Code\n"); 374b3a520acSMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED; 375b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 376b3a520acSMatthias Ringwald app_state = APP_W4_BROADCAST_CODE; 377b3a520acSMatthias Ringwald } 378b3a520acSMatthias Ringwald } 379b3a520acSMatthias Ringwald 380ec363a3dSMatthias Ringwald static void start_pa_sync(bd_addr_type_t source_type, bd_addr_t source_addr) { 381ec363a3dSMatthias Ringwald // ignore other advertisements 382ec363a3dSMatthias Ringwald gap_whitelist_add(source_type, source_addr); 383ec363a3dSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 1); 384ec363a3dSMatthias Ringwald // sync to PA 385ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_clear(); 386ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_add(source_type, source_addr, remote_sid); 387ec363a3dSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 388ec363a3dSMatthias Ringwald printf("Start Periodic Advertising Sync\n"); 389ec363a3dSMatthias Ringwald gap_periodic_advertising_create_sync(0x01, remote_sid, source_type, source_addr, 0, 1000, 0); 390ec363a3dSMatthias Ringwald gap_start_scan(); 391ec363a3dSMatthias Ringwald } 392ec363a3dSMatthias Ringwald 393*7c819d90SMatthias Ringwald static void stop_pa_sync(void) { 394*7c819d90SMatthias Ringwald app_state = APP_IDLE; 395*7c819d90SMatthias Ringwald le_audio_demo_util_sink_close(); 396*7c819d90SMatthias Ringwald printf("Terminate BIG SYNC\n"); 397*7c819d90SMatthias Ringwald gap_big_sync_terminate(big_handle); 398*7c819d90SMatthias Ringwald int i; 399*7c819d90SMatthias Ringwald for (i=0;i<num_bis;i++){ 400*7c819d90SMatthias Ringwald if (bis_con_handles[i] != HCI_CON_HANDLE_INVALID) { 401*7c819d90SMatthias Ringwald gap_disconnect(bis_con_handles[i]); 402*7c819d90SMatthias Ringwald } 403*7c819d90SMatthias Ringwald } 404*7c819d90SMatthias Ringwald 405*7c819d90SMatthias Ringwald } 406*7c819d90SMatthias Ringwald 40710277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 40810277393SMatthias Ringwald UNUSED(channel); 40910277393SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 41010277393SMatthias Ringwald switch (packet[0]) { 41110277393SMatthias Ringwald case BTSTACK_EVENT_STATE: 41210277393SMatthias Ringwald switch(btstack_event_state_get_state(packet)) { 41310277393SMatthias Ringwald case HCI_STATE_WORKING: 414400a29ddSMatthias Ringwald app_state = APP_IDLE; 41559cc662fSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync transfers 41659cc662fSMatthias Ringwald setup_advertising(); 41759cc662fSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 41859cc662fSMatthias Ringwald 4199a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE 420111967cbSMatthias Ringwald start_scanning(); 4219a0d67ccSMatthias Ringwald #else 4229a0d67ccSMatthias Ringwald show_usage(); 4239a0d67ccSMatthias Ringwald #endif 42410277393SMatthias Ringwald break; 42510277393SMatthias Ringwald case HCI_STATE_OFF: 42610277393SMatthias Ringwald printf("Goodbye\n"); 42710277393SMatthias Ringwald exit(0); 42810277393SMatthias Ringwald break; 42910277393SMatthias Ringwald default: 43010277393SMatthias Ringwald break; 43110277393SMatthias Ringwald } 43210277393SMatthias Ringwald break; 43310277393SMatthias Ringwald case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 43410277393SMatthias Ringwald { 43510277393SMatthias Ringwald if (app_state != APP_W4_BROADCAST_ADV) break; 43610277393SMatthias Ringwald 43710277393SMatthias Ringwald gap_event_extended_advertising_report_get_address(packet, remote); 43810277393SMatthias Ringwald uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 43910277393SMatthias Ringwald const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 44010277393SMatthias Ringwald 44110277393SMatthias Ringwald ad_context_t context; 44210277393SMatthias Ringwald bool found = false; 44310277393SMatthias Ringwald remote_name[0] = '\0'; 44410277393SMatthias Ringwald uint16_t uuid; 44570718632SMatthias Ringwald uint32_t broadcast_id; 44610277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 44710277393SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 44810277393SMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 44910277393SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 45010277393SMatthias Ringwald switch (data_type){ 45110277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 45210277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 45310277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){ 45470718632SMatthias Ringwald broadcast_id = little_endian_read_24(data, 2); 45510277393SMatthias Ringwald found = true; 45610277393SMatthias Ringwald } 45710277393SMatthias Ringwald break; 45810277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 45910277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 46010277393SMatthias Ringwald size = btstack_min(sizeof(remote_name) - 1, size); 46110277393SMatthias Ringwald memcpy(remote_name, data, size); 46210277393SMatthias Ringwald remote_name[size] = 0; 4632fe0253dSMatthias Ringwald // support for nRF5340 Audio DK 4642fe0253dSMatthias Ringwald if (strncmp("NRF5340", remote_name, 7) == 0){ 4652fe0253dSMatthias Ringwald nrf5340_audio_demo = true; 4662fe0253dSMatthias Ringwald found = true; 4672fe0253dSMatthias Ringwald } 46810277393SMatthias Ringwald break; 46910277393SMatthias Ringwald default: 47010277393SMatthias Ringwald break; 47110277393SMatthias Ringwald } 47210277393SMatthias Ringwald } 47310277393SMatthias Ringwald if (!found) break; 47410277393SMatthias Ringwald remote_type = gap_event_extended_advertising_report_get_address_type(packet); 47510277393SMatthias Ringwald remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 47610277393SMatthias Ringwald pts_mode = strncmp("PTS-", remote_name, 4) == 0; 47710277393SMatthias Ringwald count_mode = strncmp("COUNT", remote_name, 5) == 0; 47870718632SMatthias 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); 4797a236728SMatthias Ringwald 4807a236728SMatthias Ringwald // setup bass source info 4817a236728SMatthias Ringwald bass_source_new.address_type = remote_type; 4827a236728SMatthias Ringwald memcpy(bass_source_new.address, remote, 6); 4837a236728SMatthias Ringwald bass_source_new.adv_sid = remote_sid; 4847a236728SMatthias Ringwald bass_source_new.broadcast_id = broadcast_id; 4857a236728SMatthias Ringwald bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE; 4867a236728SMatthias Ringwald bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet); 487ec363a3dSMatthias Ringwald 488ec363a3dSMatthias Ringwald // start pa sync 489ec363a3dSMatthias Ringwald start_pa_sync(remote_type, remote); 49010277393SMatthias Ringwald break; 49110277393SMatthias Ringwald } 49210277393SMatthias Ringwald 49310277393SMatthias Ringwald case HCI_EVENT_LE_META: 49410277393SMatthias Ringwald switch(hci_event_le_meta_get_subevent_code(packet)) { 49592f80bb0SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED: 496b3a520acSMatthias Ringwald // PAST implies broadcast source has been added by client 497ffbedad9SMatthias Ringwald printf("Periodic advertising sync transfer received\n"); 4982de3ad61SMatthias Ringwald btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer); 499ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 50092f80bb0SMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 50192f80bb0SMatthias Ringwald break; 50210277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 503400a29ddSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet); 504400a29ddSMatthias Ringwald printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle); 5057a236728SMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 50610277393SMatthias Ringwald break; 50710277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 508ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 50910277393SMatthias Ringwald if (have_base) break; 51010277393SMatthias Ringwald handle_periodic_advertisement(packet, size); 511b3a520acSMatthias Ringwald if (have_base && have_big_info){ 512b3a520acSMatthias Ringwald got_base_and_big_info(); 51310277393SMatthias Ringwald } 51410277393SMatthias Ringwald break; 51510277393SMatthias Ringwald case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 516ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 51710277393SMatthias Ringwald if (have_big_info) break; 51810277393SMatthias Ringwald handle_big_info(packet, size); 519b3a520acSMatthias Ringwald if (have_base && have_big_info){ 520b3a520acSMatthias Ringwald got_base_and_big_info(); 52110277393SMatthias Ringwald } 52210277393SMatthias Ringwald break; 523111967cbSMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 524111967cbSMatthias Ringwald printf("BIG Sync Lost\n"); 525111967cbSMatthias Ringwald { 526111967cbSMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 527111967cbSMatthias Ringwald if (sink != NULL) { 528111967cbSMatthias Ringwald sink->stop_stream(); 529111967cbSMatthias Ringwald sink->close(); 530111967cbSMatthias Ringwald } 531111967cbSMatthias Ringwald } 532111967cbSMatthias Ringwald // start over 533ffbedad9SMatthias Ringwald if (!standalone_mode) break; 534111967cbSMatthias Ringwald start_scanning(); 535111967cbSMatthias Ringwald break; 53610277393SMatthias Ringwald default: 53710277393SMatthias Ringwald break; 53810277393SMatthias Ringwald } 53910277393SMatthias Ringwald break; 5405c0d69beSMatthias Ringwald case HCI_EVENT_META_GAP: 5415c0d69beSMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)){ 5425c0d69beSMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_CREATED: { 5435c0d69beSMatthias Ringwald printf("BIG Sync created with BIS Connection handles: "); 5445c0d69beSMatthias Ringwald uint8_t i; 54510277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 5465c0d69beSMatthias Ringwald bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i); 5475c0d69beSMatthias Ringwald printf("0x%04x ", bis_con_handles[i]); 54810277393SMatthias Ringwald } 5490707de65SMatthias Ringwald printf("\n"); 55010277393SMatthias Ringwald app_state = APP_STREAMING; 551151e8cb9SMatthias Ringwald printf("Start receiving\n"); 552ffbedad9SMatthias Ringwald 553ffbedad9SMatthias Ringwald // update BIS Sync state 5540707de65SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask; 555ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 55610277393SMatthias Ringwald break; 5575c0d69beSMatthias Ringwald } 558e38308a2SMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_STOPPED: 559e38308a2SMatthias Ringwald printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet)); 560e38308a2SMatthias Ringwald break; 5615c0d69beSMatthias Ringwald default: 5625c0d69beSMatthias Ringwald break; 5635c0d69beSMatthias Ringwald } 56410277393SMatthias Ringwald break; 565efd5f6d0SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 566efd5f6d0SMatthias Ringwald printf("Just Works requested\n"); 567efd5f6d0SMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 568efd5f6d0SMatthias Ringwald break; 56910277393SMatthias Ringwald default: 57010277393SMatthias Ringwald break; 57110277393SMatthias Ringwald } 57210277393SMatthias Ringwald } 57310277393SMatthias Ringwald 57410277393SMatthias Ringwald static void show_usage(void){ 57510277393SMatthias Ringwald printf("\n--- LE Audio Broadcast Sink Test Console ---\n"); 5769a0d67ccSMatthias Ringwald printf("s - start scanning\n"); 5779a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 5789a0d67ccSMatthias Ringwald printf("q - use LC3plus decoder if 10 ms ISO interval is used\n"); 5799a0d67ccSMatthias Ringwald #endif 5800707de65SMatthias Ringwald printf("e - set broadcast code to 0x11111111111111111111111111111111\n"); 581400a29ddSMatthias Ringwald printf("t - terminate BIS streams\n"); 58210277393SMatthias Ringwald printf("---\n"); 58310277393SMatthias Ringwald } 58410277393SMatthias Ringwald 58510277393SMatthias Ringwald static void stdin_process(char c){ 58610277393SMatthias Ringwald switch (c){ 5879a0d67ccSMatthias Ringwald case 's': 588400a29ddSMatthias Ringwald if (app_state != APP_IDLE) break; 589ffbedad9SMatthias Ringwald standalone_mode = true; 5909a0d67ccSMatthias Ringwald start_scanning(); 5919a0d67ccSMatthias Ringwald break; 5920707de65SMatthias Ringwald case 'e': 5930707de65SMatthias Ringwald printf("Set broadcast code to 0x11111111111111111111111111111111\n"); 5940707de65SMatthias Ringwald memset(broadcast_code, 0x11, 16); 5950707de65SMatthias Ringwald have_broadcast_code = true; 5960707de65SMatthias Ringwald break; 5979a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 5989a0d67ccSMatthias Ringwald case 'q': 599151e8cb9SMatthias Ringwald printf("Use LC3plus decoder for 10 ms ISO interval...\n"); 600f7bf5159SMatthias Ringwald le_audio_demo_util_sink_enable_lc3plus(true); 6019a0d67ccSMatthias Ringwald break; 6029a0d67ccSMatthias Ringwald #endif 603400a29ddSMatthias Ringwald case 't': 604400a29ddSMatthias Ringwald switch (app_state){ 605400a29ddSMatthias Ringwald case APP_STREAMING: 606400a29ddSMatthias Ringwald case APP_W4_BIG_SYNC_ESTABLISHED: 607*7c819d90SMatthias Ringwald stop_pa_sync(); 608400a29ddSMatthias Ringwald break; 609400a29ddSMatthias Ringwald default: 610400a29ddSMatthias Ringwald break; 611400a29ddSMatthias Ringwald } 612400a29ddSMatthias Ringwald break; 61310277393SMatthias Ringwald case '\n': 61410277393SMatthias Ringwald case '\r': 61510277393SMatthias Ringwald break; 61610277393SMatthias Ringwald default: 61710277393SMatthias Ringwald show_usage(); 61810277393SMatthias Ringwald break; 61910277393SMatthias Ringwald 62010277393SMatthias Ringwald } 62110277393SMatthias Ringwald } 6222de3ad61SMatthias Ringwald 623bb81690eSMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 624bb81690eSMatthias Ringwald // get stream_index from con_handle 625bb81690eSMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 626bb81690eSMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 627bb81690eSMatthias Ringwald uint8_t i; 628bb81690eSMatthias Ringwald for (i=0;i<num_bis;i++){ 629bb81690eSMatthias Ringwald if (bis_con_handles[i] == con_handle) { 630bb81690eSMatthias Ringwald le_audio_demo_util_sink_receive(i, packet, size); 631bb81690eSMatthias Ringwald } 632bb81690eSMatthias Ringwald } 633bb81690eSMatthias Ringwald } 634bb81690eSMatthias Ringwald 6352de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){ 6362de3ad61SMatthias Ringwald UNUSED(ts); 6372de3ad61SMatthias Ringwald printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n"); 6382de3ad61SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST); 6392de3ad61SMatthias Ringwald } 6402de3ad61SMatthias Ringwald 64160d70dccSMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 64260d70dccSMatthias Ringwald UNUSED(packet_type); 64360d70dccSMatthias Ringwald UNUSED(channel); 644ffbedad9SMatthias Ringwald btstack_assert (packet_type == HCI_EVENT_PACKET); 645ffbedad9SMatthias Ringwald btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META); 646ec363a3dSMatthias Ringwald uint8_t source_id; 647ffbedad9SMatthias Ringwald printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet)); 648ffbedad9SMatthias Ringwald printf_hexdump(packet, size); 649ffbedad9SMatthias Ringwald switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 650bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED: 651ec363a3dSMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 652ffbedad9SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n", 653ec363a3dSMatthias Ringwald source_id, 654bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); 655bebf6b57SMatthias Ringwald switch (gattservice_subevent_bass_server_source_added_get_pa_sync(packet)){ 6562de3ad61SMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE: 6572de3ad61SMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n"); 658ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST); 6592de3ad61SMatthias Ringwald // start timeout 6602de3ad61SMatthias Ringwald btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler); 6612de3ad61SMatthias Ringwald btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000); // 10 seconds 6622de3ad61SMatthias Ringwald btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer); 6632de3ad61SMatthias Ringwald break; 664ec363a3dSMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_NOT_AVAILABLE: 665ec363a3dSMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Start Periodic Advertising Sync\n"); 666ec363a3dSMatthias Ringwald start_pa_sync(bass_sources[source_id].data.address_type, bass_sources[source_id].data.address); 667ec363a3dSMatthias Ringwald break; 6682de3ad61SMatthias Ringwald } 669ffbedad9SMatthias Ringwald break; 670bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED: 671ffbedad9SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n", 672bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_source_id(packet), 673bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); 674ffbedad9SMatthias Ringwald // handle 'bis sync == 0' 675ffbedad9SMatthias Ringwald printf("PA Sync %u, bis_sync[0] = %u\n", bass_sources[0].data.pa_sync, bass_sources[0].data.subgroups[0].bis_sync); 676ffbedad9SMatthias Ringwald if (bass_sources[0].data.subgroups[0].bis_sync == 0){ 677*7c819d90SMatthias Ringwald printf("Stop PA Sync\n"); 678*7c819d90SMatthias Ringwald stop_pa_sync(); 679ffbedad9SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = 0; 680*7c819d90SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NOT_SYNCHRONIZED_TO_PA); 681ffbedad9SMatthias Ringwald } 6820707de65SMatthias Ringwald break; 683ec363a3dSMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED: 684ec363a3dSMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED, source_id 0x%04x\n", 685ec363a3dSMatthias Ringwald gattservice_subevent_bass_server_source_deleted_get_source_id(packet)); 686ec363a3dSMatthias Ringwald break; 687bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE: 688bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code); 6890707de65SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: "); 6900707de65SMatthias Ringwald printf_hexdump(broadcast_code, 16); 6910707de65SMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING; 6920707de65SMatthias Ringwald if (have_base && have_big_info){ 6930707de65SMatthias Ringwald enter_create_big_sync(); 6940707de65SMatthias Ringwald } 6950707de65SMatthias Ringwald break; 696ffbedad9SMatthias Ringwald default: 697ffbedad9SMatthias Ringwald break; 698ffbedad9SMatthias Ringwald } 69960d70dccSMatthias Ringwald } 70010277393SMatthias Ringwald 70110277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 70210277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 70310277393SMatthias Ringwald (void) argv; 70410277393SMatthias Ringwald (void) argc; 70510277393SMatthias Ringwald 706efd5f6d0SMatthias Ringwald l2cap_init(); 707efd5f6d0SMatthias Ringwald sm_init(); 708efd5f6d0SMatthias Ringwald 709efd5f6d0SMatthias Ringwald // setup ATT server 710efd5f6d0SMatthias Ringwald att_server_init(profile_data, NULL, NULL); 711efd5f6d0SMatthias Ringwald 71210277393SMatthias Ringwald // register for HCI events 71310277393SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 71410277393SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 71510277393SMatthias Ringwald 71610277393SMatthias Ringwald // register for ISO Packet 71710277393SMatthias Ringwald hci_register_iso_packet_handler(&iso_packet_handler); 71810277393SMatthias Ringwald 719efd5f6d0SMatthias Ringwald // register for SM events 720efd5f6d0SMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 721efd5f6d0SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 722efd5f6d0SMatthias Ringwald 72360d70dccSMatthias Ringwald // setup BASS Server 72460d70dccSMatthias Ringwald broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients); 72560d70dccSMatthias Ringwald broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler); 72660d70dccSMatthias Ringwald 72733bea8deSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync trasnfers 72833bea8deSMatthias Ringwald setup_advertising(); 72933bea8deSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 73033bea8deSMatthias Ringwald 731a2dc544dSMatthias Ringwald // setup audio processing 732a2dc544dSMatthias Ringwald le_audio_demo_util_sink_init("le_audio_broadcast_sink.wav"); 733a2dc544dSMatthias Ringwald 73410277393SMatthias Ringwald // turn on! 73510277393SMatthias Ringwald hci_power_control(HCI_POWER_ON); 73610277393SMatthias Ringwald 73710277393SMatthias Ringwald btstack_stdin_setup(stdin_process); 73810277393SMatthias Ringwald return 0; 73910277393SMatthias Ringwald } 740