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 159c5491061SMatthias Ringwald // broadcast assistant 160c5491061SMatthias Ringwald static hci_con_handle_t commander_acl_handle; 161c5491061SMatthias Ringwald static bd_addr_t commander_address; 162c5491061SMatthias Ringwald static bd_addr_type_t commander_type; 16310277393SMatthias Ringwald 16410277393SMatthias Ringwald // broadcast info 16510277393SMatthias Ringwald static const uint8_t big_handle = 1; 16610277393SMatthias Ringwald static hci_con_handle_t sync_handle; 16710277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS]; 168ac95ea81SMatthias Ringwald static uint8_t encryption; 1690707de65SMatthias Ringwald static uint8_t broadcast_code[16]; 17010277393SMatthias Ringwald 1712de3ad61SMatthias Ringwald static btstack_timer_source_t broadcast_sink_pa_sync_timer; 1722de3ad61SMatthias Ringwald 1735c0d69beSMatthias Ringwald // BIG Sync 1745c0d69beSMatthias Ringwald static le_audio_big_sync_t big_sync_storage; 1755c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params; 1765c0d69beSMatthias Ringwald 17710277393SMatthias Ringwald // lc3 codec config 1784a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz; 1792fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration; 18010277393SMatthias Ringwald static uint16_t number_samples_per_frame; 18110277393SMatthias Ringwald static uint16_t octets_per_frame; 18210277393SMatthias Ringwald static uint8_t num_bis; 18310277393SMatthias Ringwald 18410277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ 1852fe0253dSMatthias Ringwald // nRF534_audio quirk - no BASE in periodic advertisement 1862fe0253dSMatthias Ringwald if (nrf5340_audio_demo){ 1872fe0253dSMatthias Ringwald // hard coded config LC3 1882fe0253dSMatthias Ringwald // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame 1892fe0253dSMatthias Ringwald count_mode = 0; 1902fe0253dSMatthias Ringwald pts_mode = 0; 1912fe0253dSMatthias Ringwald num_bis = 1; 1922fe0253dSMatthias Ringwald sampling_frequency_hz = 48000; 1932fe0253dSMatthias Ringwald frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US; 1942fe0253dSMatthias Ringwald octets_per_frame = 120; 1952fe0253dSMatthias Ringwald have_base = true; 1962fe0253dSMatthias Ringwald return; 1972fe0253dSMatthias Ringwald } 1982fe0253dSMatthias Ringwald 19910277393SMatthias Ringwald // periodic advertisement contains the BASE 20010277393SMatthias Ringwald // TODO: BASE might be split across multiple advertisements 20110277393SMatthias Ringwald const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); 20210277393SMatthias Ringwald uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); 2038206d906SMatthias Ringwald uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); 2048206d906SMatthias Ringwald 2058206d906SMatthias Ringwald if (adv_status != 0) { 2068206d906SMatthias Ringwald printf("Periodic Advertisement (status %u): ", adv_status); 2078206d906SMatthias Ringwald printf_hexdump(adv_data, adv_size); 2088206d906SMatthias Ringwald return; 2098206d906SMatthias Ringwald } 21010277393SMatthias Ringwald 211be81b159SMatthias Ringwald le_audio_base_parser_t parser; 212be81b159SMatthias Ringwald bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size); 213be81b159SMatthias Ringwald if (ok == false){ 214be81b159SMatthias Ringwald return; 215be81b159SMatthias Ringwald } 21610277393SMatthias Ringwald have_base = true; 217be81b159SMatthias Ringwald 21810277393SMatthias Ringwald printf("BASE:\n"); 219be81b159SMatthias Ringwald uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser); 22010277393SMatthias Ringwald printf("- presentation delay: %"PRIu32" us\n", presentation_delay); 221be81b159SMatthias Ringwald uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser); 2227a236728SMatthias Ringwald // Cache in new source struct 2237a236728SMatthias Ringwald bass_source_new.subgroups_num = num_subgroups; 22410277393SMatthias Ringwald printf("- num subgroups: %u\n", num_subgroups); 22510277393SMatthias Ringwald uint8_t i; 22610277393SMatthias Ringwald for (i=0;i<num_subgroups;i++){ 22710277393SMatthias Ringwald // Level 2: Subgroup Level 228109dd080SMatthias Ringwald printf(" - Subgroup %u\n", i); 229be81b159SMatthias Ringwald num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser); 23010277393SMatthias Ringwald printf(" - num bis[%u]: %u\n", i, num_bis); 231109dd080SMatthias Ringwald uint8_t codec_specific_configuration_length = le_audio_base_parser_subgroup_get_codec_specific_configuration_length(&parser); 232be81b159SMatthias Ringwald const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser); 23310277393SMatthias Ringwald printf(" - codec specific config[%u]: ", i); 23410277393SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 23510277393SMatthias Ringwald // parse config to get sampling frequency and frame duration 23610277393SMatthias Ringwald uint8_t codec_offset = 0; 23710277393SMatthias Ringwald while ((codec_offset + 1) < codec_specific_configuration_length){ 23810277393SMatthias Ringwald uint8_t ltv_len = codec_specific_configuration[codec_offset++]; 23910277393SMatthias Ringwald uint8_t ltv_type = codec_specific_configuration[codec_offset]; 24010277393SMatthias Ringwald const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 }; 24110277393SMatthias Ringwald uint8_t sampling_frequency_index; 24210277393SMatthias Ringwald uint8_t frame_duration_index; 24310277393SMatthias Ringwald switch (ltv_type){ 24410277393SMatthias Ringwald case 0x01: // sampling frequency 24510277393SMatthias Ringwald sampling_frequency_index = codec_specific_configuration[codec_offset+1]; 24610277393SMatthias Ringwald // TODO: check range 24710277393SMatthias Ringwald sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1]; 2484a5b1c65SMatthias Ringwald printf(" - sampling frequency[%u]: %u\n", i, sampling_frequency_hz); 24910277393SMatthias Ringwald break; 25010277393SMatthias Ringwald case 0x02: // 0 = 7.5, 1 = 10 ms 25110277393SMatthias Ringwald frame_duration_index = codec_specific_configuration[codec_offset+1]; 252ff59851cSMilanka Ringwald frame_duration = le_audio_util_get_btstack_lc3_frame_duration(frame_duration_index); 253ff59851cSMilanka Ringwald printf(" - frame duration[%u]: %u us\n", i, le_audio_get_frame_duration_us(frame_duration_index)); 25410277393SMatthias Ringwald break; 25510277393SMatthias Ringwald case 0x04: // octets per coding frame 25610277393SMatthias Ringwald octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1); 25710277393SMatthias Ringwald printf(" - octets per codec frame[%u]: %u\n", i, octets_per_frame); 25810277393SMatthias Ringwald break; 25910277393SMatthias Ringwald default: 26010277393SMatthias Ringwald break; 26110277393SMatthias Ringwald } 26210277393SMatthias Ringwald codec_offset += ltv_len; 26310277393SMatthias Ringwald } 264be81b159SMatthias Ringwald uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser); 265be81b159SMatthias Ringwald const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser); 26610277393SMatthias Ringwald printf(" - meta data[%u]: ", i); 26710277393SMatthias Ringwald printf_hexdump(meta_data, metadata_length); 26810277393SMatthias Ringwald uint8_t k; 26910277393SMatthias Ringwald for (k=0;k<num_bis;k++){ 270109dd080SMatthias Ringwald printf(" - BIS %u\n", k); 27110277393SMatthias Ringwald // Level 3: BIS Level 272be81b159SMatthias Ringwald uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser); 273b3a520acSMatthias Ringwald if ((bis_index == 0) || (bis_index > 30)){ 274b3a520acSMatthias Ringwald continue; 275b3a520acSMatthias Ringwald } 2767a236728SMatthias Ringwald 2770707de65SMatthias Ringwald // collect bis sync mask 278b3a520acSMatthias Ringwald bis_sync_mask |= 1 << (bis_index - 1); 2797a236728SMatthias Ringwald 280be81b159SMatthias Ringwald bass_source_new.subgroups[i].metadata_length = 0; 281be81b159SMatthias Ringwald memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t)); 282be81b159SMatthias Ringwald 28310277393SMatthias Ringwald printf(" - bis index[%u][%u]: %u\n", i, k, bis_index); 284be81b159SMatthias Ringwald codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser); 285be81b159SMatthias Ringwald codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser); 28610277393SMatthias Ringwald printf(" - codec specific config[%u][%u]: ", i, k); 287be81b159SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 288be81b159SMatthias Ringwald // parse next BIS 289be81b159SMatthias Ringwald le_audio_base_parser_bis_next(&parser); 29010277393SMatthias Ringwald } 291be81b159SMatthias Ringwald 292be81b159SMatthias Ringwald // parse next subgroup 293be81b159SMatthias Ringwald le_audio_base_parser_subgroup_next(&parser); 29410277393SMatthias Ringwald } 29510277393SMatthias Ringwald } 29610277393SMatthias Ringwald 29710277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){ 29810277393SMatthias Ringwald printf("BIG Info advertising report\n"); 29910277393SMatthias Ringwald sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); 300ac95ea81SMatthias Ringwald encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet); 301ac95ea81SMatthias Ringwald if (encryption) { 302b3a520acSMatthias Ringwald printf("Stream is encrypted\n"); 303ac95ea81SMatthias Ringwald } 30410277393SMatthias Ringwald have_big_info = true; 30510277393SMatthias Ringwald } 30610277393SMatthias Ringwald 30710277393SMatthias Ringwald static void enter_create_big_sync(void){ 30810277393SMatthias Ringwald // stop scanning 30910277393SMatthias Ringwald gap_stop_scan(); 31010277393SMatthias Ringwald 311bb81690eSMatthias Ringwald // init sink 312bb81690eSMatthias Ringwald uint16_t iso_interval_1250us = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 6 : 8; 313bb81690eSMatthias Ringwald uint8_t pre_transmission_offset = 1; 314bb81690eSMatthias Ringwald le_audio_demo_util_sink_configure_broadcast(num_bis, 1, sampling_frequency_hz, frame_duration, octets_per_frame, 315bb81690eSMatthias Ringwald iso_interval_1250us, pre_transmission_offset); 31610277393SMatthias Ringwald 3175c0d69beSMatthias Ringwald big_sync_params.big_handle = big_handle; 3185c0d69beSMatthias Ringwald big_sync_params.sync_handle = sync_handle; 319ac95ea81SMatthias Ringwald big_sync_params.encryption = encryption; 320ac95ea81SMatthias Ringwald if (encryption) { 321ac95ea81SMatthias Ringwald memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16); 322ac95ea81SMatthias Ringwald } else { 3235c0d69beSMatthias Ringwald memset(big_sync_params.broadcast_code, 0, 16); 324ac95ea81SMatthias Ringwald } 3255c0d69beSMatthias Ringwald big_sync_params.mse = 0; 3265c0d69beSMatthias Ringwald big_sync_params.big_sync_timeout_10ms = 100; 3275c0d69beSMatthias Ringwald big_sync_params.num_bis = num_bis; 3285c0d69beSMatthias Ringwald uint8_t i; 3295c0d69beSMatthias Ringwald printf("BIG Create Sync for BIS: "); 3305c0d69beSMatthias Ringwald for (i=0;i<num_bis;i++){ 3315c0d69beSMatthias Ringwald big_sync_params.bis_indices[i] = i + 1; 3325c0d69beSMatthias Ringwald printf("%u ", big_sync_params.bis_indices[i]); 3335c0d69beSMatthias Ringwald } 3345c0d69beSMatthias Ringwald printf("\n"); 3355c0d69beSMatthias Ringwald app_state = APP_W4_BIG_SYNC_ESTABLISHED; 3365c0d69beSMatthias Ringwald gap_big_sync_create(&big_sync_storage, &big_sync_params); 33710277393SMatthias Ringwald } 33810277393SMatthias Ringwald 339111967cbSMatthias Ringwald static void start_scanning() { 340111967cbSMatthias Ringwald app_state = APP_W4_BROADCAST_ADV; 341400a29ddSMatthias Ringwald have_base = false; 342400a29ddSMatthias Ringwald have_big_info = false; 343111967cbSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 0); 344111967cbSMatthias Ringwald gap_start_scan(); 345111967cbSMatthias Ringwald printf("Start scan..\n"); 346111967cbSMatthias Ringwald } 347111967cbSMatthias Ringwald 348c5491061SMatthias Ringwald static void start_advertising(void) { 349c5491061SMatthias Ringwald gap_extended_advertising_start(adv_handle, 0, 0); 350c5491061SMatthias Ringwald } 351c5491061SMatthias Ringwald 352c5491061SMatthias Ringwald static void setup_advertising(void) { 353dcd1707aSDirk Helbig bd_addr_t local_addr; 354dcd1707aSDirk Helbig gap_local_bd_addr(local_addr); 3556d708481SDirk Helbig bool local_address_invalid = btstack_is_null_bd_addr( local_addr ); 356dcd1707aSDirk Helbig if( local_address_invalid ) { 357dcd1707aSDirk Helbig extended_params.own_address_type = BD_ADDR_TYPE_LE_RANDOM; 358dcd1707aSDirk Helbig } 359efd5f6d0SMatthias Ringwald gap_extended_advertising_setup(&le_advertising_set, &extended_params, &adv_handle); 360dcd1707aSDirk Helbig if( local_address_invalid ) { 361dcd1707aSDirk Helbig bd_addr_t random_address = { 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01 }; 362dcd1707aSDirk Helbig gap_extended_advertising_set_random_address( adv_handle, random_address ); 363dcd1707aSDirk Helbig } 364efd5f6d0SMatthias Ringwald gap_extended_advertising_set_adv_data(adv_handle, sizeof(extended_adv_data), extended_adv_data); 365c5491061SMatthias Ringwald start_advertising(); 36681a67409SMatthias Ringwald } 36781a67409SMatthias Ringwald 368b3a520acSMatthias Ringwald static void got_base_and_big_info() { 369b3a520acSMatthias Ringwald // add source 370b3a520acSMatthias Ringwald if (standalone_mode) { 371b3a520acSMatthias Ringwald printf("BASS: add Broadcast Source\n"); 372b3a520acSMatthias Ringwald // add source 373b3a520acSMatthias Ringwald uint8_t source_index = 0; 374abf8479fSMatthias Ringwald broadcast_audio_scan_service_server_add_source(&bass_source_new, &source_index); 375b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 376b3a520acSMatthias Ringwald } 377b3a520acSMatthias Ringwald 378db75eafdSMatthias Ringwald // update num subgroups from BASE 379db75eafdSMatthias Ringwald bass_sources[0].data.subgroups_num = bass_source_new.subgroups_num; 380db75eafdSMatthias Ringwald 381b3a520acSMatthias Ringwald if ((encryption == false) || have_broadcast_code){ 382b3a520acSMatthias Ringwald enter_create_big_sync(); 383b3a520acSMatthias Ringwald } else { 384b3a520acSMatthias Ringwald printf("BASS: request Broadcast Code\n"); 385b3a520acSMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED; 386b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 387b3a520acSMatthias Ringwald app_state = APP_W4_BROADCAST_CODE; 388b3a520acSMatthias Ringwald } 389b3a520acSMatthias Ringwald } 390b3a520acSMatthias Ringwald 391ec363a3dSMatthias Ringwald static void start_pa_sync(bd_addr_type_t source_type, bd_addr_t source_addr) { 392ec363a3dSMatthias Ringwald // ignore other advertisements 393ec363a3dSMatthias Ringwald gap_whitelist_add(source_type, source_addr); 394ec363a3dSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 1); 395ec363a3dSMatthias Ringwald // sync to PA 396ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_clear(); 397ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_add(source_type, source_addr, remote_sid); 398ec363a3dSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 399ec363a3dSMatthias Ringwald printf("Start Periodic Advertising Sync\n"); 400ec363a3dSMatthias Ringwald gap_periodic_advertising_create_sync(0x01, remote_sid, source_type, source_addr, 0, 1000, 0); 401ec363a3dSMatthias Ringwald gap_start_scan(); 402ec363a3dSMatthias Ringwald } 403ec363a3dSMatthias Ringwald 40454e09f5eSMatthias Ringwald static void pa_sync_established() { 40554e09f5eSMatthias Ringwald have_base = false; 40654e09f5eSMatthias Ringwald have_big_info = false; 40754e09f5eSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 40854e09f5eSMatthias Ringwald } 40954e09f5eSMatthias Ringwald 4107c819d90SMatthias Ringwald static void stop_pa_sync(void) { 4117c819d90SMatthias Ringwald app_state = APP_IDLE; 4127c819d90SMatthias Ringwald le_audio_demo_util_sink_close(); 4137c819d90SMatthias Ringwald printf("Terminate BIG SYNC\n"); 4147c819d90SMatthias Ringwald gap_big_sync_terminate(big_handle); 4157c819d90SMatthias Ringwald int i; 4167c819d90SMatthias Ringwald for (i=0;i<num_bis;i++){ 4177c819d90SMatthias Ringwald if (bis_con_handles[i] != HCI_CON_HANDLE_INVALID) { 4187c819d90SMatthias Ringwald gap_disconnect(bis_con_handles[i]); 4197c819d90SMatthias Ringwald } 4207c819d90SMatthias Ringwald } 4217c819d90SMatthias Ringwald 4227c819d90SMatthias Ringwald } 4237c819d90SMatthias Ringwald 42410277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 42510277393SMatthias Ringwald UNUSED(channel); 42610277393SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 42710277393SMatthias Ringwald switch (packet[0]) { 42810277393SMatthias Ringwald case BTSTACK_EVENT_STATE: 42910277393SMatthias Ringwald switch(btstack_event_state_get_state(packet)) { 43010277393SMatthias Ringwald case HCI_STATE_WORKING: 431400a29ddSMatthias Ringwald app_state = APP_IDLE; 43259cc662fSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync transfers 43359cc662fSMatthias Ringwald setup_advertising(); 43459cc662fSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 43559cc662fSMatthias Ringwald 4369a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE 437111967cbSMatthias Ringwald start_scanning(); 4389a0d67ccSMatthias Ringwald #else 4399a0d67ccSMatthias Ringwald show_usage(); 4409a0d67ccSMatthias Ringwald #endif 44110277393SMatthias Ringwald break; 44210277393SMatthias Ringwald case HCI_STATE_OFF: 44310277393SMatthias Ringwald printf("Goodbye\n"); 44410277393SMatthias Ringwald exit(0); 44510277393SMatthias Ringwald break; 44610277393SMatthias Ringwald default: 44710277393SMatthias Ringwald break; 44810277393SMatthias Ringwald } 44910277393SMatthias Ringwald break; 45010277393SMatthias Ringwald case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 45110277393SMatthias Ringwald { 45210277393SMatthias Ringwald if (app_state != APP_W4_BROADCAST_ADV) break; 45310277393SMatthias Ringwald 45410277393SMatthias Ringwald gap_event_extended_advertising_report_get_address(packet, remote); 45510277393SMatthias Ringwald uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 45610277393SMatthias Ringwald const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 45710277393SMatthias Ringwald 45810277393SMatthias Ringwald ad_context_t context; 45910277393SMatthias Ringwald bool found = false; 46010277393SMatthias Ringwald remote_name[0] = '\0'; 46110277393SMatthias Ringwald uint16_t uuid; 46270718632SMatthias Ringwald uint32_t broadcast_id; 46310277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 464db75eafdSMatthias Ringwald const uint8_t data_type = ad_iterator_get_data_type(&context); 465db75eafdSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 46610277393SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 46710277393SMatthias Ringwald switch (data_type){ 46810277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 46910277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 47010277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){ 47170718632SMatthias Ringwald broadcast_id = little_endian_read_24(data, 2); 47210277393SMatthias Ringwald found = true; 47310277393SMatthias Ringwald } 47410277393SMatthias Ringwald break; 47510277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 47610277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 477db75eafdSMatthias Ringwald data_size = btstack_min(sizeof(remote_name) - 1, data_size); 478db75eafdSMatthias Ringwald memcpy(remote_name, data, data_size); 479db75eafdSMatthias Ringwald remote_name[data_size] = 0; 4802fe0253dSMatthias Ringwald // support for nRF5340 Audio DK 4812fe0253dSMatthias Ringwald if (strncmp("NRF5340", remote_name, 7) == 0){ 4822fe0253dSMatthias Ringwald nrf5340_audio_demo = true; 4832fe0253dSMatthias Ringwald found = true; 4842fe0253dSMatthias Ringwald } 48510277393SMatthias Ringwald break; 48610277393SMatthias Ringwald default: 48710277393SMatthias Ringwald break; 48810277393SMatthias Ringwald } 48910277393SMatthias Ringwald } 49010277393SMatthias Ringwald if (!found) break; 49110277393SMatthias Ringwald remote_type = gap_event_extended_advertising_report_get_address_type(packet); 49210277393SMatthias Ringwald remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 49310277393SMatthias Ringwald pts_mode = strncmp("PTS-", remote_name, 4) == 0; 49410277393SMatthias Ringwald count_mode = strncmp("COUNT", remote_name, 5) == 0; 49570718632SMatthias 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); 4967a236728SMatthias Ringwald 4977a236728SMatthias Ringwald // setup bass source info 4987a236728SMatthias Ringwald bass_source_new.address_type = remote_type; 4997a236728SMatthias Ringwald memcpy(bass_source_new.address, remote, 6); 5007a236728SMatthias Ringwald bass_source_new.adv_sid = remote_sid; 5017a236728SMatthias Ringwald bass_source_new.broadcast_id = broadcast_id; 5027a236728SMatthias Ringwald bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE; 5037a236728SMatthias Ringwald bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet); 504ec363a3dSMatthias Ringwald 505ec363a3dSMatthias Ringwald // start pa sync 506ec363a3dSMatthias Ringwald start_pa_sync(remote_type, remote); 50710277393SMatthias Ringwald break; 50810277393SMatthias Ringwald } 50910277393SMatthias Ringwald 510c5491061SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 511c5491061SMatthias Ringwald // restart advertisements on disconnect 512c5491061SMatthias Ringwald if (hci_event_disconnection_complete_get_connection_handle(packet) == commander_acl_handle) { 513c5491061SMatthias Ringwald printf("Broadcast Assistant disconnected\n"); 514c5491061SMatthias Ringwald commander_acl_handle = HCI_CON_HANDLE_INVALID; 515c5491061SMatthias Ringwald start_advertising(); 516c5491061SMatthias Ringwald } 517c5491061SMatthias Ringwald break; 51810277393SMatthias Ringwald case HCI_EVENT_LE_META: 51910277393SMatthias Ringwald switch(hci_event_le_meta_get_subevent_code(packet)) { 52092f80bb0SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED: 521b3a520acSMatthias Ringwald // PAST implies broadcast source has been added by client 52254e09f5eSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_transfer_received_get_sync_handle(packet); 523ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 52454e09f5eSMatthias Ringwald btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer); 52554e09f5eSMatthias Ringwald printf("Periodic advertising sync transfer with handle 0x%04x received\n", sync_handle); 52654e09f5eSMatthias Ringwald pa_sync_established(); 52792f80bb0SMatthias Ringwald break; 52810277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 529400a29ddSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet); 530400a29ddSMatthias Ringwald printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle); 53154e09f5eSMatthias Ringwald pa_sync_established(); 53210277393SMatthias Ringwald break; 53310277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 534ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 53510277393SMatthias Ringwald if (have_base) break; 53610277393SMatthias Ringwald handle_periodic_advertisement(packet, size); 537b3a520acSMatthias Ringwald if (have_base && have_big_info){ 538b3a520acSMatthias Ringwald got_base_and_big_info(); 53910277393SMatthias Ringwald } 54010277393SMatthias Ringwald break; 54110277393SMatthias Ringwald case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 542ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 54310277393SMatthias Ringwald if (have_big_info) break; 54410277393SMatthias Ringwald handle_big_info(packet, size); 545b3a520acSMatthias Ringwald if (have_base && have_big_info){ 546b3a520acSMatthias Ringwald got_base_and_big_info(); 54710277393SMatthias Ringwald } 54810277393SMatthias Ringwald break; 549111967cbSMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 550111967cbSMatthias Ringwald printf("BIG Sync Lost\n"); 551111967cbSMatthias Ringwald { 552111967cbSMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 553111967cbSMatthias Ringwald if (sink != NULL) { 554111967cbSMatthias Ringwald sink->stop_stream(); 555111967cbSMatthias Ringwald sink->close(); 556111967cbSMatthias Ringwald } 557111967cbSMatthias Ringwald } 558111967cbSMatthias Ringwald // start over 559ffbedad9SMatthias Ringwald if (!standalone_mode) break; 560111967cbSMatthias Ringwald start_scanning(); 561111967cbSMatthias Ringwald break; 56210277393SMatthias Ringwald default: 56310277393SMatthias Ringwald break; 56410277393SMatthias Ringwald } 56510277393SMatthias Ringwald break; 5665c0d69beSMatthias Ringwald case HCI_EVENT_META_GAP: 5675c0d69beSMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)){ 568*4e150227SMatthias Ringwald case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: 569*4e150227SMatthias Ringwald commander_acl_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 570*4e150227SMatthias Ringwald commander_type = gap_subevent_le_connection_complete_get_peer_address_type(packet); 571*4e150227SMatthias Ringwald gap_subevent_le_connection_complete_get_peer_address(packet, commander_address); 572*4e150227SMatthias Ringwald printf("Broadcast Assistant connected, handle 0x%04x - %s type %u\n", commander_acl_handle, bd_addr_to_str(commander_address), commander_type); 573*4e150227SMatthias Ringwald break; 5745c0d69beSMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_CREATED: { 5755c0d69beSMatthias Ringwald printf("BIG Sync created with BIS Connection handles: "); 5765c0d69beSMatthias Ringwald uint8_t i; 57710277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 5785c0d69beSMatthias Ringwald bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i); 5795c0d69beSMatthias Ringwald printf("0x%04x ", bis_con_handles[i]); 58010277393SMatthias Ringwald } 5810707de65SMatthias Ringwald printf("\n"); 58210277393SMatthias Ringwald app_state = APP_STREAMING; 583151e8cb9SMatthias Ringwald printf("Start receiving\n"); 584ffbedad9SMatthias Ringwald 58554e09f5eSMatthias Ringwald printf("Terminate PA Sync\n"); 58654e09f5eSMatthias Ringwald gap_periodic_advertising_terminate_sync(sync_handle); 58754e09f5eSMatthias Ringwald 588ffbedad9SMatthias Ringwald // update BIS Sync state 5890707de65SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask; 590ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 59110277393SMatthias Ringwald break; 5925c0d69beSMatthias Ringwald } 593e38308a2SMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_STOPPED: 594e38308a2SMatthias Ringwald printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet)); 595e38308a2SMatthias Ringwald break; 5965c0d69beSMatthias Ringwald default: 5975c0d69beSMatthias Ringwald break; 5985c0d69beSMatthias Ringwald } 59910277393SMatthias Ringwald break; 600efd5f6d0SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 601efd5f6d0SMatthias Ringwald printf("Just Works requested\n"); 602efd5f6d0SMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 603efd5f6d0SMatthias Ringwald break; 60410277393SMatthias Ringwald default: 60510277393SMatthias Ringwald break; 60610277393SMatthias Ringwald } 60710277393SMatthias Ringwald } 60810277393SMatthias Ringwald 60910277393SMatthias Ringwald static void show_usage(void){ 61010277393SMatthias Ringwald printf("\n--- LE Audio Broadcast Sink Test Console ---\n"); 6119a0d67ccSMatthias Ringwald printf("s - start scanning\n"); 6129a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 6139a0d67ccSMatthias Ringwald printf("q - use LC3plus decoder if 10 ms ISO interval is used\n"); 6149a0d67ccSMatthias Ringwald #endif 6150707de65SMatthias Ringwald printf("e - set broadcast code to 0x11111111111111111111111111111111\n"); 616400a29ddSMatthias Ringwald printf("t - terminate BIS streams\n"); 61710277393SMatthias Ringwald printf("---\n"); 61810277393SMatthias Ringwald } 61910277393SMatthias Ringwald 62010277393SMatthias Ringwald static void stdin_process(char c){ 62110277393SMatthias Ringwald switch (c){ 6229a0d67ccSMatthias Ringwald case 's': 623400a29ddSMatthias Ringwald if (app_state != APP_IDLE) break; 624ffbedad9SMatthias Ringwald standalone_mode = true; 6259a0d67ccSMatthias Ringwald start_scanning(); 6269a0d67ccSMatthias Ringwald break; 6270707de65SMatthias Ringwald case 'e': 6280707de65SMatthias Ringwald printf("Set broadcast code to 0x11111111111111111111111111111111\n"); 6290707de65SMatthias Ringwald memset(broadcast_code, 0x11, 16); 6300707de65SMatthias Ringwald have_broadcast_code = true; 6310707de65SMatthias Ringwald break; 6329a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 6339a0d67ccSMatthias Ringwald case 'q': 634151e8cb9SMatthias Ringwald printf("Use LC3plus decoder for 10 ms ISO interval...\n"); 635f7bf5159SMatthias Ringwald le_audio_demo_util_sink_enable_lc3plus(true); 6369a0d67ccSMatthias Ringwald break; 6379a0d67ccSMatthias Ringwald #endif 638400a29ddSMatthias Ringwald case 't': 639400a29ddSMatthias Ringwald switch (app_state){ 640400a29ddSMatthias Ringwald case APP_STREAMING: 641400a29ddSMatthias Ringwald case APP_W4_BIG_SYNC_ESTABLISHED: 6427c819d90SMatthias Ringwald stop_pa_sync(); 643400a29ddSMatthias Ringwald break; 644400a29ddSMatthias Ringwald default: 645400a29ddSMatthias Ringwald break; 646400a29ddSMatthias Ringwald } 647400a29ddSMatthias Ringwald break; 64810277393SMatthias Ringwald case '\n': 64910277393SMatthias Ringwald case '\r': 65010277393SMatthias Ringwald break; 65110277393SMatthias Ringwald default: 65210277393SMatthias Ringwald show_usage(); 65310277393SMatthias Ringwald break; 65410277393SMatthias Ringwald 65510277393SMatthias Ringwald } 65610277393SMatthias Ringwald } 6572de3ad61SMatthias Ringwald 658bb81690eSMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 659bb81690eSMatthias Ringwald // get stream_index from con_handle 660bb81690eSMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 661bb81690eSMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 662bb81690eSMatthias Ringwald uint8_t i; 663bb81690eSMatthias Ringwald for (i=0;i<num_bis;i++){ 664bb81690eSMatthias Ringwald if (bis_con_handles[i] == con_handle) { 665bb81690eSMatthias Ringwald le_audio_demo_util_sink_receive(i, packet, size); 666bb81690eSMatthias Ringwald } 667bb81690eSMatthias Ringwald } 668bb81690eSMatthias Ringwald } 669bb81690eSMatthias Ringwald 6702de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){ 6712de3ad61SMatthias Ringwald UNUSED(ts); 6722de3ad61SMatthias Ringwald printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n"); 6732de3ad61SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST); 6742de3ad61SMatthias Ringwald } 6752de3ad61SMatthias Ringwald 67602631e97SMatthias Ringwald static void handle_new_pa_sync(uint8_t source_id, le_audio_pa_sync_t pa_sync) { 67702631e97SMatthias Ringwald switch (pa_sync){ 67802631e97SMatthias Ringwald case LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA: 67902631e97SMatthias Ringwald printf("Stop PA Sync\n"); 68002631e97SMatthias Ringwald stop_pa_sync(); 68102631e97SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = 0; 68202631e97SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NOT_SYNCHRONIZED_TO_PA); 68302631e97SMatthias Ringwald break; 6842de3ad61SMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE: 6852de3ad61SMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n"); 68602631e97SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(source_id, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST); 6872de3ad61SMatthias Ringwald // start timeout 6882de3ad61SMatthias Ringwald btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler); 6892de3ad61SMatthias Ringwald btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000); // 10 seconds 6902de3ad61SMatthias Ringwald btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer); 6912de3ad61SMatthias Ringwald break; 692ec363a3dSMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_NOT_AVAILABLE: 693ec363a3dSMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Start Periodic Advertising Sync\n"); 694ec363a3dSMatthias Ringwald start_pa_sync(bass_sources[source_id].data.address_type, bass_sources[source_id].data.address); 695ec363a3dSMatthias Ringwald break; 69602631e97SMatthias Ringwald default: 69702631e97SMatthias Ringwald break; 6982de3ad61SMatthias Ringwald } 69902631e97SMatthias Ringwald } 70002631e97SMatthias Ringwald 70102631e97SMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 70202631e97SMatthias Ringwald UNUSED(packet_type); 70302631e97SMatthias Ringwald UNUSED(channel); 70402631e97SMatthias Ringwald btstack_assert (packet_type == HCI_EVENT_PACKET); 70502631e97SMatthias Ringwald btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META); 70602631e97SMatthias Ringwald uint8_t source_id; 70702631e97SMatthias Ringwald printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet)); 70802631e97SMatthias Ringwald printf_hexdump(packet, size); 70902631e97SMatthias Ringwald uint8_t pa_sync; 71002631e97SMatthias Ringwald switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 71102631e97SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED: 71202631e97SMatthias Ringwald pa_sync = gattservice_subevent_bass_server_source_added_get_pa_sync(packet); 71302631e97SMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 71402631e97SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n", source_id, pa_sync); 71502631e97SMatthias Ringwald handle_new_pa_sync(source_id, pa_sync); 716ffbedad9SMatthias Ringwald break; 717bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED: 71802631e97SMatthias Ringwald pa_sync = gattservice_subevent_bass_server_source_added_get_pa_sync(packet); 71902631e97SMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 72002631e97SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n", source_id, pa_sync); 72102631e97SMatthias Ringwald handle_new_pa_sync(source_id, pa_sync); 7220707de65SMatthias Ringwald break; 723ec363a3dSMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED: 724ec363a3dSMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED, source_id 0x%04x\n", 725ec363a3dSMatthias Ringwald gattservice_subevent_bass_server_source_deleted_get_source_id(packet)); 726ec363a3dSMatthias Ringwald break; 727bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE: 728bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code); 7290707de65SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: "); 7300707de65SMatthias Ringwald printf_hexdump(broadcast_code, 16); 7310707de65SMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING; 7320707de65SMatthias Ringwald if (have_base && have_big_info){ 7330707de65SMatthias Ringwald enter_create_big_sync(); 7340707de65SMatthias Ringwald } 7350707de65SMatthias Ringwald break; 736ffbedad9SMatthias Ringwald default: 737ffbedad9SMatthias Ringwald break; 738ffbedad9SMatthias Ringwald } 73960d70dccSMatthias Ringwald } 74010277393SMatthias Ringwald 74110277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 74210277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 74310277393SMatthias Ringwald (void) argv; 74410277393SMatthias Ringwald (void) argc; 74510277393SMatthias Ringwald 746efd5f6d0SMatthias Ringwald l2cap_init(); 747efd5f6d0SMatthias Ringwald sm_init(); 748efd5f6d0SMatthias Ringwald 749efd5f6d0SMatthias Ringwald // setup ATT server 750efd5f6d0SMatthias Ringwald att_server_init(profile_data, NULL, NULL); 751efd5f6d0SMatthias Ringwald 75210277393SMatthias Ringwald // register for HCI events 75310277393SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 75410277393SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 75510277393SMatthias Ringwald 75610277393SMatthias Ringwald // register for ISO Packet 75710277393SMatthias Ringwald hci_register_iso_packet_handler(&iso_packet_handler); 75810277393SMatthias Ringwald 759efd5f6d0SMatthias Ringwald // register for SM events 760efd5f6d0SMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 761efd5f6d0SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 762efd5f6d0SMatthias Ringwald 76360d70dccSMatthias Ringwald // setup BASS Server 76460d70dccSMatthias Ringwald broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients); 76560d70dccSMatthias Ringwald broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler); 76660d70dccSMatthias Ringwald 76733bea8deSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync trasnfers 76833bea8deSMatthias Ringwald setup_advertising(); 76933bea8deSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 77033bea8deSMatthias Ringwald 771a2dc544dSMatthias Ringwald // setup audio processing 772a2dc544dSMatthias Ringwald le_audio_demo_util_sink_init("le_audio_broadcast_sink.wav"); 773a2dc544dSMatthias Ringwald 77410277393SMatthias Ringwald // turn on! 77510277393SMatthias Ringwald hci_power_control(HCI_POWER_ON); 77610277393SMatthias Ringwald 77710277393SMatthias Ringwald btstack_stdin_setup(stdin_process); 77810277393SMatthias Ringwald return 0; 77910277393SMatthias Ringwald } 780