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 enum { 9110277393SMatthias Ringwald APP_W4_WORKING, 9210277393SMatthias Ringwald APP_W4_BROADCAST_ADV, 9310277393SMatthias Ringwald APP_W4_PA_AND_BIG_INFO, 94b3a520acSMatthias Ringwald APP_W4_BROADCAST_CODE, 9510277393SMatthias Ringwald APP_W4_BIG_SYNC_ESTABLISHED, 9610277393SMatthias Ringwald APP_STREAMING, 9710277393SMatthias Ringwald APP_IDLE 9810277393SMatthias Ringwald } app_state = APP_W4_WORKING; 9910277393SMatthias Ringwald 100efd5f6d0SMatthias Ringwald static const uint8_t adv_sid = 0; 101efd5f6d0SMatthias Ringwald static le_advertising_set_t le_advertising_set; 102efd5f6d0SMatthias Ringwald static uint8_t adv_handle = 0; 103efd5f6d0SMatthias Ringwald 104dcd1707aSDirk Helbig static le_extended_advertising_parameters_t extended_params = { 105efd5f6d0SMatthias Ringwald .advertising_event_properties = 1, // connectable 106efd5f6d0SMatthias Ringwald .primary_advertising_interval_min = 0x4b0, // 750 ms 107efd5f6d0SMatthias Ringwald .primary_advertising_interval_max = 0x4b0, // 750 ms 108efd5f6d0SMatthias Ringwald .primary_advertising_channel_map = 7, 109dcd1707aSDirk Helbig .own_address_type = BD_ADDR_TYPE_LE_PUBLIC, 110efd5f6d0SMatthias Ringwald .peer_address_type = 0, 111efd5f6d0SMatthias Ringwald .peer_address = { 0 }, 112efd5f6d0SMatthias Ringwald .advertising_filter_policy = 0, 113efd5f6d0SMatthias Ringwald .advertising_tx_power = 10, // 10 dBm 114efd5f6d0SMatthias Ringwald .primary_advertising_phy = 1, // LE 1M PHY 115efd5f6d0SMatthias Ringwald .secondary_advertising_max_skip = 0, 116efd5f6d0SMatthias Ringwald .secondary_advertising_phy = 1, // LE 1M PHY 117efd5f6d0SMatthias Ringwald .advertising_sid = adv_sid, 118efd5f6d0SMatthias Ringwald .scan_request_notification_enable = 0, 119efd5f6d0SMatthias Ringwald }; 120efd5f6d0SMatthias Ringwald 121efd5f6d0SMatthias Ringwald static const uint8_t extended_adv_data[] = { 1223b0d944cSMatthias Ringwald // 16 bit service data, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE, 1233b0d944cSMatthias Ringwald 3, BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID, 12481a67409SMatthias Ringwald ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE & 0xff, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE >> 8, 12581a67409SMatthias Ringwald // name 12681a67409SMatthias Ringwald 5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'i', 'n', 'k' 12781a67409SMatthias Ringwald }; 12881a67409SMatthias Ringwald 12960d70dccSMatthias Ringwald #define BASS_NUM_CLIENTS 1 13060d70dccSMatthias Ringwald #define BASS_NUM_SOURCES 1 1317a236728SMatthias Ringwald static bass_source_data_t bass_source_new; 13260d70dccSMatthias Ringwald static bass_server_source_t bass_sources[BASS_NUM_SOURCES]; 133bebf6b57SMatthias Ringwald static bass_server_connection_t bass_clients[BASS_NUM_CLIENTS]; 13460d70dccSMatthias Ringwald 13510277393SMatthias Ringwald // 13610277393SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 137efd5f6d0SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 13810277393SMatthias Ringwald 13910277393SMatthias Ringwald static bool have_base; 14010277393SMatthias Ringwald static bool have_big_info; 1410707de65SMatthias Ringwald static bool have_broadcast_code; 142ffbedad9SMatthias Ringwald static bool standalone_mode; 1430707de65SMatthias Ringwald static uint32_t bis_sync_mask; 14410277393SMatthias Ringwald 1454a5b1c65SMatthias Ringwald uint16_t samples_received; 1464a5b1c65SMatthias Ringwald uint16_t samples_dropped; 14710277393SMatthias Ringwald 14810277393SMatthias Ringwald // remote info 14910277393SMatthias Ringwald static char remote_name[20]; 15010277393SMatthias Ringwald static bd_addr_t remote; 15110277393SMatthias Ringwald static bd_addr_type_t remote_type; 15210277393SMatthias Ringwald static uint8_t remote_sid; 15310277393SMatthias Ringwald static bool count_mode; 15410277393SMatthias Ringwald static bool pts_mode; 1552fe0253dSMatthias Ringwald static bool nrf5340_audio_demo; 1562fe0253dSMatthias Ringwald 157c5491061SMatthias Ringwald // broadcast assistant 158c5491061SMatthias Ringwald static hci_con_handle_t commander_acl_handle; 159c5491061SMatthias Ringwald static bd_addr_t commander_address; 160c5491061SMatthias Ringwald static bd_addr_type_t commander_type; 16110277393SMatthias Ringwald 16210277393SMatthias Ringwald // broadcast info 16310277393SMatthias Ringwald static const uint8_t big_handle = 1; 16410277393SMatthias Ringwald static hci_con_handle_t sync_handle; 16510277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS]; 166ac95ea81SMatthias Ringwald static uint8_t encryption; 1670707de65SMatthias Ringwald static uint8_t broadcast_code[16]; 16810277393SMatthias Ringwald 1692de3ad61SMatthias Ringwald static btstack_timer_source_t broadcast_sink_pa_sync_timer; 1702de3ad61SMatthias Ringwald 1715c0d69beSMatthias Ringwald // BIG Sync 1725c0d69beSMatthias Ringwald static le_audio_big_sync_t big_sync_storage; 1735c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params; 1745c0d69beSMatthias Ringwald 17510277393SMatthias Ringwald // lc3 codec config 1764a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz; 1772fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration; 17810277393SMatthias Ringwald static uint16_t octets_per_frame; 17910277393SMatthias Ringwald static uint8_t num_bis; 18010277393SMatthias Ringwald 18110277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ 1822fe0253dSMatthias Ringwald // nRF534_audio quirk - no BASE in periodic advertisement 1832fe0253dSMatthias Ringwald if (nrf5340_audio_demo){ 1842fe0253dSMatthias Ringwald // hard coded config LC3 1852fe0253dSMatthias Ringwald // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame 1862fe0253dSMatthias Ringwald count_mode = 0; 1872fe0253dSMatthias Ringwald pts_mode = 0; 1882fe0253dSMatthias Ringwald num_bis = 1; 1892fe0253dSMatthias Ringwald sampling_frequency_hz = 48000; 1902fe0253dSMatthias Ringwald frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US; 1912fe0253dSMatthias Ringwald octets_per_frame = 120; 1922fe0253dSMatthias Ringwald have_base = true; 1932fe0253dSMatthias Ringwald return; 1942fe0253dSMatthias Ringwald } 1952fe0253dSMatthias Ringwald 19610277393SMatthias Ringwald // periodic advertisement contains the BASE 19710277393SMatthias Ringwald // TODO: BASE might be split across multiple advertisements 19810277393SMatthias Ringwald const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); 19910277393SMatthias Ringwald uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); 2008206d906SMatthias Ringwald uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); 2018206d906SMatthias Ringwald 2028206d906SMatthias Ringwald if (adv_status != 0) { 2038206d906SMatthias Ringwald printf("Periodic Advertisement (status %u): ", adv_status); 2048206d906SMatthias Ringwald printf_hexdump(adv_data, adv_size); 2058206d906SMatthias Ringwald return; 2068206d906SMatthias Ringwald } 20710277393SMatthias Ringwald 208be81b159SMatthias Ringwald le_audio_base_parser_t parser; 209be81b159SMatthias Ringwald bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size); 210be81b159SMatthias Ringwald if (ok == false){ 211be81b159SMatthias Ringwald return; 212be81b159SMatthias Ringwald } 21310277393SMatthias Ringwald have_base = true; 214be81b159SMatthias Ringwald 21510277393SMatthias Ringwald printf("BASE:\n"); 216be81b159SMatthias Ringwald uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser); 21710277393SMatthias Ringwald printf("- presentation delay: %"PRIu32" us\n", presentation_delay); 218be81b159SMatthias Ringwald uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser); 2197a236728SMatthias Ringwald // Cache in new source struct 2207a236728SMatthias Ringwald bass_source_new.subgroups_num = num_subgroups; 22110277393SMatthias Ringwald printf("- num subgroups: %u\n", num_subgroups); 22210277393SMatthias Ringwald uint8_t i; 22310277393SMatthias Ringwald for (i=0;i<num_subgroups;i++){ 22410277393SMatthias Ringwald // Level 2: Subgroup Level 225109dd080SMatthias Ringwald printf(" - Subgroup %u\n", i); 226be81b159SMatthias Ringwald num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser); 22710277393SMatthias Ringwald printf(" - num bis[%u]: %u\n", i, num_bis); 228109dd080SMatthias Ringwald uint8_t codec_specific_configuration_length = le_audio_base_parser_subgroup_get_codec_specific_configuration_length(&parser); 229be81b159SMatthias Ringwald const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser); 23010277393SMatthias Ringwald printf(" - codec specific config[%u]: ", i); 23110277393SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 23210277393SMatthias Ringwald // parse config to get sampling frequency and frame duration 23310277393SMatthias Ringwald uint8_t codec_offset = 0; 23410277393SMatthias Ringwald while ((codec_offset + 1) < codec_specific_configuration_length){ 23510277393SMatthias Ringwald uint8_t ltv_len = codec_specific_configuration[codec_offset++]; 23610277393SMatthias Ringwald uint8_t ltv_type = codec_specific_configuration[codec_offset]; 23710277393SMatthias Ringwald const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 }; 23810277393SMatthias Ringwald uint8_t sampling_frequency_index; 23910277393SMatthias Ringwald uint8_t frame_duration_index; 24010277393SMatthias Ringwald switch (ltv_type){ 24110277393SMatthias Ringwald case 0x01: // sampling frequency 24210277393SMatthias Ringwald sampling_frequency_index = codec_specific_configuration[codec_offset+1]; 24310277393SMatthias Ringwald // TODO: check range 24410277393SMatthias Ringwald sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1]; 2454a5b1c65SMatthias Ringwald printf(" - sampling frequency[%u]: %u\n", i, sampling_frequency_hz); 24610277393SMatthias Ringwald break; 24710277393SMatthias Ringwald case 0x02: // 0 = 7.5, 1 = 10 ms 24810277393SMatthias Ringwald frame_duration_index = codec_specific_configuration[codec_offset+1]; 249ff59851cSMilanka Ringwald frame_duration = le_audio_util_get_btstack_lc3_frame_duration(frame_duration_index); 250ff59851cSMilanka Ringwald printf(" - frame duration[%u]: %u us\n", i, le_audio_get_frame_duration_us(frame_duration_index)); 25110277393SMatthias Ringwald break; 25210277393SMatthias Ringwald case 0x04: // octets per coding frame 25310277393SMatthias Ringwald octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1); 25410277393SMatthias Ringwald printf(" - octets per codec frame[%u]: %u\n", i, octets_per_frame); 25510277393SMatthias Ringwald break; 25610277393SMatthias Ringwald default: 25710277393SMatthias Ringwald break; 25810277393SMatthias Ringwald } 25910277393SMatthias Ringwald codec_offset += ltv_len; 26010277393SMatthias Ringwald } 261be81b159SMatthias Ringwald uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser); 262be81b159SMatthias Ringwald const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser); 26310277393SMatthias Ringwald printf(" - meta data[%u]: ", i); 26410277393SMatthias Ringwald printf_hexdump(meta_data, metadata_length); 26510277393SMatthias Ringwald uint8_t k; 26610277393SMatthias Ringwald for (k=0;k<num_bis;k++){ 267109dd080SMatthias Ringwald printf(" - BIS %u\n", k); 26810277393SMatthias Ringwald // Level 3: BIS Level 269be81b159SMatthias Ringwald uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser); 270b3a520acSMatthias Ringwald if ((bis_index == 0) || (bis_index > 30)){ 271b3a520acSMatthias Ringwald continue; 272b3a520acSMatthias Ringwald } 2737a236728SMatthias Ringwald 2740707de65SMatthias Ringwald // collect bis sync mask 275b3a520acSMatthias Ringwald bis_sync_mask |= 1 << (bis_index - 1); 2767a236728SMatthias Ringwald 277be81b159SMatthias Ringwald bass_source_new.subgroups[i].metadata_length = 0; 278be81b159SMatthias Ringwald memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t)); 279be81b159SMatthias Ringwald 28010277393SMatthias Ringwald printf(" - bis index[%u][%u]: %u\n", i, k, bis_index); 281be81b159SMatthias Ringwald codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser); 282be81b159SMatthias Ringwald codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser); 28310277393SMatthias Ringwald printf(" - codec specific config[%u][%u]: ", i, k); 284be81b159SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 285be81b159SMatthias Ringwald // parse next BIS 286be81b159SMatthias Ringwald le_audio_base_parser_bis_next(&parser); 28710277393SMatthias Ringwald } 288be81b159SMatthias Ringwald 289be81b159SMatthias Ringwald // parse next subgroup 290be81b159SMatthias Ringwald le_audio_base_parser_subgroup_next(&parser); 29110277393SMatthias Ringwald } 29210277393SMatthias Ringwald } 29310277393SMatthias Ringwald 29410277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){ 29510277393SMatthias Ringwald printf("BIG Info advertising report\n"); 29610277393SMatthias Ringwald sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); 297ac95ea81SMatthias Ringwald encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet); 298ac95ea81SMatthias Ringwald if (encryption) { 299b3a520acSMatthias Ringwald printf("Stream is encrypted\n"); 300ac95ea81SMatthias Ringwald } 30110277393SMatthias Ringwald have_big_info = true; 30210277393SMatthias Ringwald } 30310277393SMatthias Ringwald 30410277393SMatthias Ringwald static void enter_create_big_sync(void){ 30510277393SMatthias Ringwald // stop scanning 30610277393SMatthias Ringwald gap_stop_scan(); 30710277393SMatthias Ringwald 308bb81690eSMatthias Ringwald // init sink 309bb81690eSMatthias Ringwald uint16_t iso_interval_1250us = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 6 : 8; 310bb81690eSMatthias Ringwald uint8_t pre_transmission_offset = 1; 311bb81690eSMatthias Ringwald le_audio_demo_util_sink_configure_broadcast(num_bis, 1, sampling_frequency_hz, frame_duration, octets_per_frame, 312bb81690eSMatthias Ringwald iso_interval_1250us, pre_transmission_offset); 31310277393SMatthias Ringwald 3145c0d69beSMatthias Ringwald big_sync_params.big_handle = big_handle; 3155c0d69beSMatthias Ringwald big_sync_params.sync_handle = sync_handle; 316ac95ea81SMatthias Ringwald big_sync_params.encryption = encryption; 317ac95ea81SMatthias Ringwald if (encryption) { 318ac95ea81SMatthias Ringwald memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16); 319ac95ea81SMatthias Ringwald } else { 3205c0d69beSMatthias Ringwald memset(big_sync_params.broadcast_code, 0, 16); 321ac95ea81SMatthias Ringwald } 3225c0d69beSMatthias Ringwald big_sync_params.mse = 0; 3235c0d69beSMatthias Ringwald big_sync_params.big_sync_timeout_10ms = 100; 3245c0d69beSMatthias Ringwald big_sync_params.num_bis = num_bis; 3255c0d69beSMatthias Ringwald uint8_t i; 3265c0d69beSMatthias Ringwald printf("BIG Create Sync for BIS: "); 3275c0d69beSMatthias Ringwald for (i=0;i<num_bis;i++){ 3285c0d69beSMatthias Ringwald big_sync_params.bis_indices[i] = i + 1; 3295c0d69beSMatthias Ringwald printf("%u ", big_sync_params.bis_indices[i]); 3305c0d69beSMatthias Ringwald } 3315c0d69beSMatthias Ringwald printf("\n"); 3325c0d69beSMatthias Ringwald app_state = APP_W4_BIG_SYNC_ESTABLISHED; 3335c0d69beSMatthias Ringwald gap_big_sync_create(&big_sync_storage, &big_sync_params); 33410277393SMatthias Ringwald } 33510277393SMatthias Ringwald 336111967cbSMatthias Ringwald static void start_scanning() { 337111967cbSMatthias Ringwald app_state = APP_W4_BROADCAST_ADV; 338400a29ddSMatthias Ringwald have_base = false; 339400a29ddSMatthias Ringwald have_big_info = false; 340111967cbSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 0); 341111967cbSMatthias Ringwald gap_start_scan(); 342111967cbSMatthias Ringwald printf("Start scan..\n"); 343111967cbSMatthias Ringwald } 344111967cbSMatthias Ringwald 345c5491061SMatthias Ringwald static void start_advertising(void) { 346c5491061SMatthias Ringwald gap_extended_advertising_start(adv_handle, 0, 0); 347c5491061SMatthias Ringwald } 348c5491061SMatthias Ringwald 349c5491061SMatthias Ringwald static void setup_advertising(void) { 350dcd1707aSDirk Helbig bd_addr_t local_addr; 351dcd1707aSDirk Helbig gap_local_bd_addr(local_addr); 3526d708481SDirk Helbig bool local_address_invalid = btstack_is_null_bd_addr( local_addr ); 353dcd1707aSDirk Helbig if( local_address_invalid ) { 354dcd1707aSDirk Helbig extended_params.own_address_type = BD_ADDR_TYPE_LE_RANDOM; 355dcd1707aSDirk Helbig } 356efd5f6d0SMatthias Ringwald gap_extended_advertising_setup(&le_advertising_set, &extended_params, &adv_handle); 357dcd1707aSDirk Helbig if( local_address_invalid ) { 358dcd1707aSDirk Helbig bd_addr_t random_address = { 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01 }; 359dcd1707aSDirk Helbig gap_extended_advertising_set_random_address( adv_handle, random_address ); 360dcd1707aSDirk Helbig } 361efd5f6d0SMatthias Ringwald gap_extended_advertising_set_adv_data(adv_handle, sizeof(extended_adv_data), extended_adv_data); 362c5491061SMatthias Ringwald start_advertising(); 36381a67409SMatthias Ringwald } 36481a67409SMatthias Ringwald 365b3a520acSMatthias Ringwald static void got_base_and_big_info() { 366b3a520acSMatthias Ringwald // add source 367b3a520acSMatthias Ringwald if (standalone_mode) { 368b3a520acSMatthias Ringwald printf("BASS: add Broadcast Source\n"); 369b3a520acSMatthias Ringwald // add source 370b3a520acSMatthias Ringwald uint8_t source_index = 0; 371abf8479fSMatthias Ringwald broadcast_audio_scan_service_server_add_source(&bass_source_new, &source_index); 372b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 373b3a520acSMatthias Ringwald } 374b3a520acSMatthias Ringwald 375db75eafdSMatthias Ringwald // update num subgroups from BASE 376db75eafdSMatthias Ringwald bass_sources[0].data.subgroups_num = bass_source_new.subgroups_num; 377db75eafdSMatthias Ringwald 378b3a520acSMatthias Ringwald if ((encryption == false) || have_broadcast_code){ 379b3a520acSMatthias Ringwald enter_create_big_sync(); 380b3a520acSMatthias Ringwald } else { 381b3a520acSMatthias Ringwald printf("BASS: request Broadcast Code\n"); 382b3a520acSMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED; 383b3a520acSMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 384b3a520acSMatthias Ringwald app_state = APP_W4_BROADCAST_CODE; 385b3a520acSMatthias Ringwald } 386b3a520acSMatthias Ringwald } 387b3a520acSMatthias Ringwald 388ec363a3dSMatthias Ringwald static void start_pa_sync(bd_addr_type_t source_type, bd_addr_t source_addr) { 389ec363a3dSMatthias Ringwald // ignore other advertisements 390ec363a3dSMatthias Ringwald gap_whitelist_add(source_type, source_addr); 391ec363a3dSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 1); 392ec363a3dSMatthias Ringwald // sync to PA 393ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_clear(); 394ec363a3dSMatthias Ringwald gap_periodic_advertiser_list_add(source_type, source_addr, remote_sid); 395ec363a3dSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 396ec363a3dSMatthias Ringwald printf("Start Periodic Advertising Sync\n"); 397ec363a3dSMatthias Ringwald gap_periodic_advertising_create_sync(0x01, remote_sid, source_type, source_addr, 0, 1000, 0); 398ec363a3dSMatthias Ringwald gap_start_scan(); 399ec363a3dSMatthias Ringwald } 400ec363a3dSMatthias Ringwald 40154e09f5eSMatthias Ringwald static void pa_sync_established() { 40254e09f5eSMatthias Ringwald have_base = false; 40354e09f5eSMatthias Ringwald have_big_info = false; 40454e09f5eSMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 40554e09f5eSMatthias Ringwald } 40654e09f5eSMatthias Ringwald 4077c819d90SMatthias Ringwald static void stop_pa_sync(void) { 4087c819d90SMatthias Ringwald app_state = APP_IDLE; 4097c819d90SMatthias Ringwald le_audio_demo_util_sink_close(); 4107c819d90SMatthias Ringwald printf("Terminate BIG SYNC\n"); 4117c819d90SMatthias Ringwald gap_big_sync_terminate(big_handle); 4127c819d90SMatthias Ringwald int i; 4137c819d90SMatthias Ringwald for (i=0;i<num_bis;i++){ 4147c819d90SMatthias Ringwald if (bis_con_handles[i] != HCI_CON_HANDLE_INVALID) { 4157c819d90SMatthias Ringwald gap_disconnect(bis_con_handles[i]); 4167c819d90SMatthias Ringwald } 4177c819d90SMatthias Ringwald } 4187c819d90SMatthias Ringwald 4197c819d90SMatthias Ringwald } 4207c819d90SMatthias Ringwald 42110277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 42210277393SMatthias Ringwald UNUSED(channel); 42310277393SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 42410277393SMatthias Ringwald switch (packet[0]) { 42510277393SMatthias Ringwald case BTSTACK_EVENT_STATE: 42610277393SMatthias Ringwald switch(btstack_event_state_get_state(packet)) { 42710277393SMatthias Ringwald case HCI_STATE_WORKING: 428400a29ddSMatthias Ringwald app_state = APP_IDLE; 42959cc662fSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync transfers 43059cc662fSMatthias Ringwald setup_advertising(); 43159cc662fSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 43259cc662fSMatthias Ringwald 4339a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE 434111967cbSMatthias Ringwald start_scanning(); 4359a0d67ccSMatthias Ringwald #else 4369a0d67ccSMatthias Ringwald show_usage(); 4379a0d67ccSMatthias Ringwald #endif 43810277393SMatthias Ringwald break; 43910277393SMatthias Ringwald case HCI_STATE_OFF: 44010277393SMatthias Ringwald printf("Goodbye\n"); 44110277393SMatthias Ringwald exit(0); 44210277393SMatthias Ringwald break; 44310277393SMatthias Ringwald default: 44410277393SMatthias Ringwald break; 44510277393SMatthias Ringwald } 44610277393SMatthias Ringwald break; 44710277393SMatthias Ringwald case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 44810277393SMatthias Ringwald { 44910277393SMatthias Ringwald if (app_state != APP_W4_BROADCAST_ADV) break; 45010277393SMatthias Ringwald 45110277393SMatthias Ringwald gap_event_extended_advertising_report_get_address(packet, remote); 45210277393SMatthias Ringwald uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 45310277393SMatthias Ringwald const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 45410277393SMatthias Ringwald 45510277393SMatthias Ringwald ad_context_t context; 45610277393SMatthias Ringwald bool found = false; 45710277393SMatthias Ringwald remote_name[0] = '\0'; 45810277393SMatthias Ringwald uint16_t uuid; 45970718632SMatthias Ringwald uint32_t broadcast_id; 46010277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 461db75eafdSMatthias Ringwald const uint8_t data_type = ad_iterator_get_data_type(&context); 462db75eafdSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 46310277393SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 46410277393SMatthias Ringwald switch (data_type){ 46510277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 46610277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 46710277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){ 46870718632SMatthias Ringwald broadcast_id = little_endian_read_24(data, 2); 46910277393SMatthias Ringwald found = true; 47010277393SMatthias Ringwald } 47110277393SMatthias Ringwald break; 47210277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 47310277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 474db75eafdSMatthias Ringwald data_size = btstack_min(sizeof(remote_name) - 1, data_size); 475db75eafdSMatthias Ringwald memcpy(remote_name, data, data_size); 476db75eafdSMatthias Ringwald remote_name[data_size] = 0; 4772fe0253dSMatthias Ringwald // support for nRF5340 Audio DK 4782fe0253dSMatthias Ringwald if (strncmp("NRF5340", remote_name, 7) == 0){ 4792fe0253dSMatthias Ringwald nrf5340_audio_demo = true; 4802fe0253dSMatthias Ringwald found = true; 4812fe0253dSMatthias Ringwald } 48210277393SMatthias Ringwald break; 48310277393SMatthias Ringwald default: 48410277393SMatthias Ringwald break; 48510277393SMatthias Ringwald } 48610277393SMatthias Ringwald } 48710277393SMatthias Ringwald if (!found) break; 48810277393SMatthias Ringwald remote_type = gap_event_extended_advertising_report_get_address_type(packet); 48910277393SMatthias Ringwald remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 49010277393SMatthias Ringwald pts_mode = strncmp("PTS-", remote_name, 4) == 0; 49110277393SMatthias Ringwald count_mode = strncmp("COUNT", remote_name, 5) == 0; 49270718632SMatthias 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); 4937a236728SMatthias Ringwald 4947a236728SMatthias Ringwald // setup bass source info 4957a236728SMatthias Ringwald bass_source_new.address_type = remote_type; 4967a236728SMatthias Ringwald memcpy(bass_source_new.address, remote, 6); 4977a236728SMatthias Ringwald bass_source_new.adv_sid = remote_sid; 4987a236728SMatthias Ringwald bass_source_new.broadcast_id = broadcast_id; 4997a236728SMatthias Ringwald bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE; 5007a236728SMatthias Ringwald bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet); 501ec363a3dSMatthias Ringwald 502ec363a3dSMatthias Ringwald // start pa sync 503ec363a3dSMatthias Ringwald start_pa_sync(remote_type, remote); 50410277393SMatthias Ringwald break; 50510277393SMatthias Ringwald } 50610277393SMatthias Ringwald 507c5491061SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 508c5491061SMatthias Ringwald // restart advertisements on disconnect 509c5491061SMatthias Ringwald if (hci_event_disconnection_complete_get_connection_handle(packet) == commander_acl_handle) { 510c5491061SMatthias Ringwald printf("Broadcast Assistant disconnected\n"); 511c5491061SMatthias Ringwald commander_acl_handle = HCI_CON_HANDLE_INVALID; 512c5491061SMatthias Ringwald start_advertising(); 513c5491061SMatthias Ringwald } 514c5491061SMatthias Ringwald break; 51510277393SMatthias Ringwald case HCI_EVENT_LE_META: 51610277393SMatthias Ringwald switch(hci_event_le_meta_get_subevent_code(packet)) { 51792f80bb0SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED: 518b3a520acSMatthias Ringwald // PAST implies broadcast source has been added by client 51954e09f5eSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_transfer_received_get_sync_handle(packet); 520ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 52154e09f5eSMatthias Ringwald btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer); 52254e09f5eSMatthias Ringwald printf("Periodic advertising sync transfer with handle 0x%04x received\n", sync_handle); 52354e09f5eSMatthias Ringwald pa_sync_established(); 52492f80bb0SMatthias Ringwald break; 52510277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 526400a29ddSMatthias Ringwald sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet); 527400a29ddSMatthias Ringwald printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle); 52854e09f5eSMatthias Ringwald pa_sync_established(); 52910277393SMatthias Ringwald break; 53010277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 531ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 53210277393SMatthias Ringwald if (have_base) break; 53310277393SMatthias Ringwald handle_periodic_advertisement(packet, size); 534b3a520acSMatthias Ringwald if (have_base && have_big_info){ 535b3a520acSMatthias Ringwald got_base_and_big_info(); 53610277393SMatthias Ringwald } 53710277393SMatthias Ringwald break; 53810277393SMatthias Ringwald case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 539ffbedad9SMatthias Ringwald if (app_state != APP_W4_PA_AND_BIG_INFO) break; 54010277393SMatthias Ringwald if (have_big_info) break; 54110277393SMatthias Ringwald handle_big_info(packet, size); 542b3a520acSMatthias Ringwald if (have_base && have_big_info){ 543b3a520acSMatthias Ringwald got_base_and_big_info(); 54410277393SMatthias Ringwald } 54510277393SMatthias Ringwald break; 546111967cbSMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 547111967cbSMatthias Ringwald printf("BIG Sync Lost\n"); 548111967cbSMatthias Ringwald { 549111967cbSMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 550111967cbSMatthias Ringwald if (sink != NULL) { 551111967cbSMatthias Ringwald sink->stop_stream(); 552111967cbSMatthias Ringwald sink->close(); 553111967cbSMatthias Ringwald } 554111967cbSMatthias Ringwald } 555111967cbSMatthias Ringwald // start over 556ffbedad9SMatthias Ringwald if (!standalone_mode) break; 557111967cbSMatthias Ringwald start_scanning(); 558111967cbSMatthias Ringwald break; 55910277393SMatthias Ringwald default: 56010277393SMatthias Ringwald break; 56110277393SMatthias Ringwald } 56210277393SMatthias Ringwald break; 5635c0d69beSMatthias Ringwald case HCI_EVENT_META_GAP: 5645c0d69beSMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)){ 5654e150227SMatthias Ringwald case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: 5664e150227SMatthias Ringwald commander_acl_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 5674e150227SMatthias Ringwald commander_type = gap_subevent_le_connection_complete_get_peer_address_type(packet); 5684e150227SMatthias Ringwald gap_subevent_le_connection_complete_get_peer_address(packet, commander_address); 5694e150227SMatthias Ringwald printf("Broadcast Assistant connected, handle 0x%04x - %s type %u\n", commander_acl_handle, bd_addr_to_str(commander_address), commander_type); 5704e150227SMatthias Ringwald break; 5715c0d69beSMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_CREATED: { 5725c0d69beSMatthias Ringwald printf("BIG Sync created with BIS Connection handles: "); 5735c0d69beSMatthias Ringwald uint8_t i; 57410277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 5755c0d69beSMatthias Ringwald bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i); 5765c0d69beSMatthias Ringwald printf("0x%04x ", bis_con_handles[i]); 57710277393SMatthias Ringwald } 5780707de65SMatthias Ringwald printf("\n"); 57910277393SMatthias Ringwald app_state = APP_STREAMING; 580151e8cb9SMatthias Ringwald printf("Start receiving\n"); 581ffbedad9SMatthias Ringwald 58254e09f5eSMatthias Ringwald printf("Terminate PA Sync\n"); 58354e09f5eSMatthias Ringwald gap_periodic_advertising_terminate_sync(sync_handle); 58454e09f5eSMatthias Ringwald 585ffbedad9SMatthias Ringwald // update BIS Sync state 5860707de65SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask; 587ffbedad9SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); 58810277393SMatthias Ringwald break; 5895c0d69beSMatthias Ringwald } 590e38308a2SMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_STOPPED: 591e38308a2SMatthias Ringwald printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet)); 592e38308a2SMatthias Ringwald break; 5935c0d69beSMatthias Ringwald default: 5945c0d69beSMatthias Ringwald break; 5955c0d69beSMatthias Ringwald } 59610277393SMatthias Ringwald break; 597efd5f6d0SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 598efd5f6d0SMatthias Ringwald printf("Just Works requested\n"); 599efd5f6d0SMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 600efd5f6d0SMatthias Ringwald break; 60110277393SMatthias Ringwald default: 60210277393SMatthias Ringwald break; 60310277393SMatthias Ringwald } 60410277393SMatthias Ringwald } 60510277393SMatthias Ringwald 60610277393SMatthias Ringwald static void show_usage(void){ 60710277393SMatthias Ringwald printf("\n--- LE Audio Broadcast Sink Test Console ---\n"); 6089a0d67ccSMatthias Ringwald printf("s - start scanning\n"); 6099a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 6109a0d67ccSMatthias Ringwald printf("q - use LC3plus decoder if 10 ms ISO interval is used\n"); 6119a0d67ccSMatthias Ringwald #endif 6120707de65SMatthias Ringwald printf("e - set broadcast code to 0x11111111111111111111111111111111\n"); 613400a29ddSMatthias Ringwald printf("t - terminate BIS streams\n"); 61410277393SMatthias Ringwald printf("---\n"); 61510277393SMatthias Ringwald } 61610277393SMatthias Ringwald 61710277393SMatthias Ringwald static void stdin_process(char c){ 61810277393SMatthias Ringwald switch (c){ 6199a0d67ccSMatthias Ringwald case 's': 620400a29ddSMatthias Ringwald if (app_state != APP_IDLE) break; 621ffbedad9SMatthias Ringwald standalone_mode = true; 6229a0d67ccSMatthias Ringwald start_scanning(); 6239a0d67ccSMatthias Ringwald break; 6240707de65SMatthias Ringwald case 'e': 6250707de65SMatthias Ringwald printf("Set broadcast code to 0x11111111111111111111111111111111\n"); 6260707de65SMatthias Ringwald memset(broadcast_code, 0x11, 16); 6270707de65SMatthias Ringwald have_broadcast_code = true; 6280707de65SMatthias Ringwald break; 6299a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS 6309a0d67ccSMatthias Ringwald case 'q': 631151e8cb9SMatthias Ringwald printf("Use LC3plus decoder for 10 ms ISO interval...\n"); 632f7bf5159SMatthias Ringwald le_audio_demo_util_sink_enable_lc3plus(true); 6339a0d67ccSMatthias Ringwald break; 6349a0d67ccSMatthias Ringwald #endif 635400a29ddSMatthias Ringwald case 't': 636400a29ddSMatthias Ringwald switch (app_state){ 637400a29ddSMatthias Ringwald case APP_STREAMING: 638400a29ddSMatthias Ringwald case APP_W4_BIG_SYNC_ESTABLISHED: 6397c819d90SMatthias Ringwald stop_pa_sync(); 640400a29ddSMatthias Ringwald break; 641400a29ddSMatthias Ringwald default: 642400a29ddSMatthias Ringwald break; 643400a29ddSMatthias Ringwald } 644400a29ddSMatthias Ringwald break; 64510277393SMatthias Ringwald case '\n': 64610277393SMatthias Ringwald case '\r': 64710277393SMatthias Ringwald break; 64810277393SMatthias Ringwald default: 64910277393SMatthias Ringwald show_usage(); 65010277393SMatthias Ringwald break; 65110277393SMatthias Ringwald 65210277393SMatthias Ringwald } 65310277393SMatthias Ringwald } 6542de3ad61SMatthias Ringwald 655bb81690eSMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 656*0c30abb2SDirk Helbig void (*packet_receive)(uint8_t stream_index, uint8_t *packet, uint16_t size) = le_audio_demo_util_sink_receive; 657*0c30abb2SDirk Helbig 658bb81690eSMatthias Ringwald // get stream_index from con_handle 659bb81690eSMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 660bb81690eSMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 661bb81690eSMatthias Ringwald uint8_t i; 662*0c30abb2SDirk Helbig 663*0c30abb2SDirk Helbig if (count_mode){ 664*0c30abb2SDirk Helbig packet_receive = le_audio_demo_util_sink_count; 665*0c30abb2SDirk Helbig } 666*0c30abb2SDirk Helbig 667bb81690eSMatthias Ringwald for (i=0;i<num_bis;i++){ 668bb81690eSMatthias Ringwald if (bis_con_handles[i] == con_handle) { 669*0c30abb2SDirk Helbig packet_receive(i, packet, size); 670bb81690eSMatthias Ringwald } 671bb81690eSMatthias Ringwald } 672bb81690eSMatthias Ringwald } 673bb81690eSMatthias Ringwald 6742de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){ 6752de3ad61SMatthias Ringwald UNUSED(ts); 6762de3ad61SMatthias Ringwald printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n"); 6772de3ad61SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST); 6782de3ad61SMatthias Ringwald } 6792de3ad61SMatthias Ringwald 68002631e97SMatthias Ringwald static void handle_new_pa_sync(uint8_t source_id, le_audio_pa_sync_t pa_sync) { 68102631e97SMatthias Ringwald switch (pa_sync){ 68202631e97SMatthias Ringwald case LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA: 68302631e97SMatthias Ringwald printf("Stop PA Sync\n"); 68402631e97SMatthias Ringwald stop_pa_sync(); 68502631e97SMatthias Ringwald bass_sources[0].data.subgroups[0].bis_sync_state = 0; 68602631e97SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NOT_SYNCHRONIZED_TO_PA); 68702631e97SMatthias Ringwald break; 6882de3ad61SMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE: 6892de3ad61SMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n"); 69002631e97SMatthias Ringwald broadcast_audio_scan_service_server_set_pa_sync_state(source_id, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST); 6912de3ad61SMatthias Ringwald // start timeout 6922de3ad61SMatthias Ringwald btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler); 6932de3ad61SMatthias Ringwald btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000); // 10 seconds 6942de3ad61SMatthias Ringwald btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer); 6952de3ad61SMatthias Ringwald break; 696ec363a3dSMatthias Ringwald case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_NOT_AVAILABLE: 697ec363a3dSMatthias Ringwald printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Start Periodic Advertising Sync\n"); 698ec363a3dSMatthias Ringwald start_pa_sync(bass_sources[source_id].data.address_type, bass_sources[source_id].data.address); 699ec363a3dSMatthias Ringwald break; 70002631e97SMatthias Ringwald default: 70102631e97SMatthias Ringwald break; 7022de3ad61SMatthias Ringwald } 70302631e97SMatthias Ringwald } 70402631e97SMatthias Ringwald 70502631e97SMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 70602631e97SMatthias Ringwald UNUSED(packet_type); 70702631e97SMatthias Ringwald UNUSED(channel); 70802631e97SMatthias Ringwald btstack_assert (packet_type == HCI_EVENT_PACKET); 70902631e97SMatthias Ringwald btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META); 71002631e97SMatthias Ringwald uint8_t source_id; 71102631e97SMatthias Ringwald printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet)); 71202631e97SMatthias Ringwald printf_hexdump(packet, size); 71302631e97SMatthias Ringwald uint8_t pa_sync; 71402631e97SMatthias Ringwald switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 71502631e97SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED: 71602631e97SMatthias Ringwald pa_sync = gattservice_subevent_bass_server_source_added_get_pa_sync(packet); 71702631e97SMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 71802631e97SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n", source_id, pa_sync); 71902631e97SMatthias Ringwald handle_new_pa_sync(source_id, pa_sync); 720ffbedad9SMatthias Ringwald break; 721bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED: 72202631e97SMatthias Ringwald pa_sync = gattservice_subevent_bass_server_source_added_get_pa_sync(packet); 72302631e97SMatthias Ringwald source_id = gattservice_subevent_bass_server_source_added_get_source_id(packet); 72402631e97SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n", source_id, pa_sync); 72502631e97SMatthias Ringwald handle_new_pa_sync(source_id, pa_sync); 7260707de65SMatthias Ringwald break; 727ec363a3dSMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED: 728ec363a3dSMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_DELETED, source_id 0x%04x\n", 729ec363a3dSMatthias Ringwald gattservice_subevent_bass_server_source_deleted_get_source_id(packet)); 730ec363a3dSMatthias Ringwald break; 731bebf6b57SMatthias Ringwald case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE: 732bebf6b57SMatthias Ringwald gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code); 7330707de65SMatthias Ringwald printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: "); 7340707de65SMatthias Ringwald printf_hexdump(broadcast_code, 16); 7350707de65SMatthias Ringwald bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING; 7360707de65SMatthias Ringwald if (have_base && have_big_info){ 7370707de65SMatthias Ringwald enter_create_big_sync(); 7380707de65SMatthias Ringwald } 7390707de65SMatthias Ringwald break; 740ffbedad9SMatthias Ringwald default: 741ffbedad9SMatthias Ringwald break; 742ffbedad9SMatthias Ringwald } 74360d70dccSMatthias Ringwald } 74410277393SMatthias Ringwald 74510277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 74610277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 74710277393SMatthias Ringwald (void) argv; 74810277393SMatthias Ringwald (void) argc; 74910277393SMatthias Ringwald 750efd5f6d0SMatthias Ringwald l2cap_init(); 751efd5f6d0SMatthias Ringwald sm_init(); 752efd5f6d0SMatthias Ringwald 753efd5f6d0SMatthias Ringwald // setup ATT server 754efd5f6d0SMatthias Ringwald att_server_init(profile_data, NULL, NULL); 755efd5f6d0SMatthias Ringwald 75610277393SMatthias Ringwald // register for HCI events 75710277393SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 75810277393SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 75910277393SMatthias Ringwald 76010277393SMatthias Ringwald // register for ISO Packet 76110277393SMatthias Ringwald hci_register_iso_packet_handler(&iso_packet_handler); 76210277393SMatthias Ringwald 763efd5f6d0SMatthias Ringwald // register for SM events 764efd5f6d0SMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 765efd5f6d0SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 766efd5f6d0SMatthias Ringwald 76760d70dccSMatthias Ringwald // setup BASS Server 76860d70dccSMatthias Ringwald broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients); 76960d70dccSMatthias Ringwald broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler); 77060d70dccSMatthias Ringwald 77133bea8deSMatthias Ringwald // setup advertising and allow to receive periodic advertising sync trasnfers 77233bea8deSMatthias Ringwald setup_advertising(); 77333bea8deSMatthias Ringwald gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); 77433bea8deSMatthias Ringwald 775a2dc544dSMatthias Ringwald // setup audio processing 776a2dc544dSMatthias Ringwald le_audio_demo_util_sink_init("le_audio_broadcast_sink.wav"); 777a2dc544dSMatthias Ringwald 77810277393SMatthias Ringwald // turn on! 77910277393SMatthias Ringwald hci_power_control(HCI_POWER_ON); 78010277393SMatthias Ringwald 78110277393SMatthias Ringwald btstack_stdin_setup(stdin_process); 78210277393SMatthias Ringwald return 0; 78310277393SMatthias Ringwald } 784