xref: /btstack/test/le_audio/le_audio_broadcast_sink.c (revision bb81690e3b4d17bd783229112790b2019d41d0c5)
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"
72be81b159SMatthias Ringwald #include "le-audio/le_audio_base_parser.h"
735deb0bb6SMatthias Ringwald #include "le-audio/gatt-service/broadcast_audio_scan_service_server.h"
74efd5f6d0SMatthias Ringwald 
75efd5f6d0SMatthias Ringwald #include "le_audio_broadcast_sink.h"
76*bb81690eSMatthias Ringwald #include "le_audio_demo_util_sink.h"
777a5bad53SMatthias Ringwald 
7810277393SMatthias Ringwald // max config
7910277393SMatthias Ringwald #define MAX_NUM_BIS 2
8010277393SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480
8110277393SMatthias Ringwald 
8210277393SMatthias Ringwald // playback
8310277393SMatthias Ringwald #define MAX_NUM_LC3_FRAMES   5
8410277393SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4
8510277393SMatthias Ringwald #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE)
8610277393SMatthias Ringwald 
8710277393SMatthias Ringwald static void show_usage(void);
8810277393SMatthias Ringwald 
8910277393SMatthias Ringwald static const char * filename_wav = "le_audio_broadcast_sink.wav";
9010277393SMatthias Ringwald 
9110277393SMatthias Ringwald static enum {
9210277393SMatthias Ringwald     APP_W4_WORKING,
9310277393SMatthias Ringwald     APP_W4_BROADCAST_ADV,
9410277393SMatthias Ringwald     APP_W4_PA_AND_BIG_INFO,
95b3a520acSMatthias Ringwald     APP_W4_BROADCAST_CODE,
9610277393SMatthias Ringwald     APP_W4_BIG_SYNC_ESTABLISHED,
9710277393SMatthias Ringwald     APP_STREAMING,
9810277393SMatthias Ringwald     APP_IDLE
9910277393SMatthias Ringwald } app_state = APP_W4_WORKING;
10010277393SMatthias Ringwald 
101efd5f6d0SMatthias Ringwald static const uint8_t adv_sid = 0;
102efd5f6d0SMatthias Ringwald static le_advertising_set_t le_advertising_set;
103efd5f6d0SMatthias Ringwald static uint8_t adv_handle = 0;
104efd5f6d0SMatthias Ringwald 
105dcd1707aSDirk Helbig static le_extended_advertising_parameters_t extended_params = {
106efd5f6d0SMatthias Ringwald         .advertising_event_properties = 1,  // connectable
107efd5f6d0SMatthias Ringwald         .primary_advertising_interval_min = 0x4b0, // 750 ms
108efd5f6d0SMatthias Ringwald         .primary_advertising_interval_max = 0x4b0, // 750 ms
109efd5f6d0SMatthias Ringwald         .primary_advertising_channel_map = 7,
110dcd1707aSDirk Helbig         .own_address_type = BD_ADDR_TYPE_LE_PUBLIC,
111efd5f6d0SMatthias Ringwald         .peer_address_type = 0,
112efd5f6d0SMatthias Ringwald         .peer_address =  { 0 },
113efd5f6d0SMatthias Ringwald         .advertising_filter_policy = 0,
114efd5f6d0SMatthias Ringwald         .advertising_tx_power = 10, // 10 dBm
115efd5f6d0SMatthias Ringwald         .primary_advertising_phy = 1, // LE 1M PHY
116efd5f6d0SMatthias Ringwald         .secondary_advertising_max_skip = 0,
117efd5f6d0SMatthias Ringwald         .secondary_advertising_phy = 1, // LE 1M PHY
118efd5f6d0SMatthias Ringwald         .advertising_sid = adv_sid,
119efd5f6d0SMatthias Ringwald         .scan_request_notification_enable = 0,
120efd5f6d0SMatthias Ringwald };
121efd5f6d0SMatthias Ringwald 
122efd5f6d0SMatthias Ringwald static const uint8_t extended_adv_data[] = {
1233b0d944cSMatthias Ringwald         // 16 bit service data, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE,
1243b0d944cSMatthias Ringwald         3, BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID,
12581a67409SMatthias Ringwald             ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE & 0xff, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE >> 8,
12681a67409SMatthias Ringwald        // name
12781a67409SMatthias Ringwald         5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'i', 'n', 'k'
12881a67409SMatthias Ringwald };
12981a67409SMatthias Ringwald 
13060d70dccSMatthias Ringwald #define BASS_NUM_CLIENTS 1
13160d70dccSMatthias Ringwald #define BASS_NUM_SOURCES 1
1327a236728SMatthias Ringwald static bass_source_data_t       bass_source_new;
13360d70dccSMatthias Ringwald static bass_server_source_t     bass_sources[BASS_NUM_SOURCES];
134bebf6b57SMatthias Ringwald static bass_server_connection_t bass_clients[BASS_NUM_CLIENTS];
13560d70dccSMatthias Ringwald 
13610277393SMatthias Ringwald //
13710277393SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
138efd5f6d0SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
13910277393SMatthias Ringwald 
14010277393SMatthias Ringwald static bool have_base;
14110277393SMatthias Ringwald static bool have_big_info;
1420707de65SMatthias Ringwald static bool have_broadcast_code;
143ffbedad9SMatthias Ringwald static bool standalone_mode;
1440707de65SMatthias Ringwald static uint32_t bis_sync_mask;
14510277393SMatthias Ringwald 
1464a5b1c65SMatthias Ringwald uint16_t samples_received;
1474a5b1c65SMatthias Ringwald uint16_t samples_dropped;
14810277393SMatthias Ringwald 
14910277393SMatthias Ringwald // remote info
15010277393SMatthias Ringwald static char remote_name[20];
15110277393SMatthias Ringwald static bd_addr_t remote;
15210277393SMatthias Ringwald static bd_addr_type_t remote_type;
15310277393SMatthias Ringwald static uint8_t remote_sid;
15410277393SMatthias Ringwald static bool count_mode;
15510277393SMatthias Ringwald static bool pts_mode;
1562fe0253dSMatthias Ringwald static bool nrf5340_audio_demo;
1572fe0253dSMatthias Ringwald 
15810277393SMatthias Ringwald 
15910277393SMatthias Ringwald // broadcast info
16010277393SMatthias Ringwald static const uint8_t    big_handle = 1;
16110277393SMatthias Ringwald static hci_con_handle_t sync_handle;
16210277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS];
163ac95ea81SMatthias Ringwald static uint8_t          encryption;
1640707de65SMatthias Ringwald static uint8_t          broadcast_code[16];
16510277393SMatthias Ringwald 
1662de3ad61SMatthias Ringwald static btstack_timer_source_t broadcast_sink_pa_sync_timer;
1672de3ad61SMatthias Ringwald 
1685c0d69beSMatthias Ringwald // BIG Sync
1695c0d69beSMatthias Ringwald static le_audio_big_sync_t        big_sync_storage;
1705c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params;
1715c0d69beSMatthias Ringwald 
17210277393SMatthias Ringwald // lc3 codec config
1734a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz;
1742fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration;
17510277393SMatthias Ringwald static uint16_t number_samples_per_frame;
17610277393SMatthias Ringwald static uint16_t octets_per_frame;
17710277393SMatthias Ringwald static uint8_t  num_bis;
17810277393SMatthias Ringwald 
17910277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
1802fe0253dSMatthias Ringwald     // nRF534_audio quirk - no BASE in periodic advertisement
1812fe0253dSMatthias Ringwald     if (nrf5340_audio_demo){
1822fe0253dSMatthias Ringwald         // hard coded config LC3
1832fe0253dSMatthias Ringwald         // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame
1842fe0253dSMatthias Ringwald         count_mode = 0;
1852fe0253dSMatthias Ringwald         pts_mode   = 0;
1862fe0253dSMatthias Ringwald         num_bis    = 1;
1872fe0253dSMatthias Ringwald         sampling_frequency_hz = 48000;
1882fe0253dSMatthias Ringwald         frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US;
1892fe0253dSMatthias Ringwald         octets_per_frame = 120;
1902fe0253dSMatthias Ringwald         have_base = true;
1912fe0253dSMatthias Ringwald         return;
1922fe0253dSMatthias Ringwald     }
1932fe0253dSMatthias Ringwald 
19410277393SMatthias Ringwald     // periodic advertisement contains the BASE
19510277393SMatthias Ringwald     // TODO: BASE might be split across multiple advertisements
19610277393SMatthias Ringwald     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
19710277393SMatthias Ringwald     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
1988206d906SMatthias Ringwald     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
1998206d906SMatthias Ringwald 
2008206d906SMatthias Ringwald     if (adv_status != 0) {
2018206d906SMatthias Ringwald         printf("Periodic Advertisement (status %u): ", adv_status);
2028206d906SMatthias Ringwald         printf_hexdump(adv_data, adv_size);
2038206d906SMatthias Ringwald         return;
2048206d906SMatthias Ringwald     }
20510277393SMatthias Ringwald 
206be81b159SMatthias Ringwald     le_audio_base_parser_t parser;
207be81b159SMatthias Ringwald     bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size);
208be81b159SMatthias Ringwald     if (ok == false){
209be81b159SMatthias Ringwald         return;
210be81b159SMatthias Ringwald     }
21110277393SMatthias Ringwald     have_base = true;
212be81b159SMatthias Ringwald 
21310277393SMatthias Ringwald     printf("BASE:\n");
214be81b159SMatthias Ringwald     uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser);
21510277393SMatthias Ringwald     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
216be81b159SMatthias Ringwald     uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser);
2177a236728SMatthias Ringwald     // Cache in new source struct
2187a236728SMatthias Ringwald     bass_source_new.subgroups_num = num_subgroups;
21910277393SMatthias Ringwald     printf("- num subgroups: %u\n", num_subgroups);
22010277393SMatthias Ringwald     uint8_t i;
22110277393SMatthias Ringwald     for (i=0;i<num_subgroups;i++){
22210277393SMatthias Ringwald         // Level 2: Subgroup Level
223109dd080SMatthias Ringwald         printf("  - Subgroup %u\n", i);
224be81b159SMatthias Ringwald         num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser);
22510277393SMatthias Ringwald         printf("    - num bis[%u]: %u\n", i, num_bis);
226109dd080SMatthias Ringwald         uint8_t codec_specific_configuration_length = le_audio_base_parser_subgroup_get_codec_specific_configuration_length(&parser);
227be81b159SMatthias Ringwald         const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser);
22810277393SMatthias Ringwald         printf("    - codec specific config[%u]: ", i);
22910277393SMatthias Ringwald         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
23010277393SMatthias Ringwald         // parse config to get sampling frequency and frame duration
23110277393SMatthias Ringwald         uint8_t codec_offset = 0;
23210277393SMatthias Ringwald         while ((codec_offset + 1) < codec_specific_configuration_length){
23310277393SMatthias Ringwald             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
23410277393SMatthias Ringwald             uint8_t ltv_type = codec_specific_configuration[codec_offset];
23510277393SMatthias Ringwald             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
23610277393SMatthias Ringwald             uint8_t sampling_frequency_index;
23710277393SMatthias Ringwald             uint8_t frame_duration_index;
23810277393SMatthias Ringwald             switch (ltv_type){
23910277393SMatthias Ringwald                 case 0x01: // sampling frequency
24010277393SMatthias Ringwald                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
24110277393SMatthias Ringwald                     // TODO: check range
24210277393SMatthias Ringwald                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
2434a5b1c65SMatthias Ringwald                     printf("      - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
24410277393SMatthias Ringwald                     break;
24510277393SMatthias Ringwald                 case 0x02: // 0 = 7.5, 1 = 10 ms
24610277393SMatthias Ringwald                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
2472fd68da2SMatthias Ringwald                     frame_duration = (frame_duration_index == 0) ? BTSTACK_LC3_FRAME_DURATION_7500US : BTSTACK_LC3_FRAME_DURATION_10000US;
2482fd68da2SMatthias Ringwald                     printf("      - frame duration[%u]: %s ms\n", i, (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? "7.5" : "10");
24910277393SMatthias Ringwald                     break;
25010277393SMatthias Ringwald                 case 0x04:  // octets per coding frame
25110277393SMatthias Ringwald                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
25210277393SMatthias Ringwald                     printf("      - octets per codec frame[%u]: %u\n", i, octets_per_frame);
25310277393SMatthias Ringwald                     break;
25410277393SMatthias Ringwald                 default:
25510277393SMatthias Ringwald                     break;
25610277393SMatthias Ringwald             }
25710277393SMatthias Ringwald             codec_offset += ltv_len;
25810277393SMatthias Ringwald         }
259be81b159SMatthias Ringwald         uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser);
260be81b159SMatthias Ringwald         const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser);
26110277393SMatthias Ringwald         printf("    - meta data[%u]: ", i);
26210277393SMatthias Ringwald         printf_hexdump(meta_data, metadata_length);
26310277393SMatthias Ringwald         uint8_t k;
26410277393SMatthias Ringwald         for (k=0;k<num_bis;k++){
265109dd080SMatthias Ringwald             printf("      - BIS %u\n", k);
26610277393SMatthias Ringwald             // Level 3: BIS Level
267be81b159SMatthias Ringwald             uint8_t bis_index =  le_audio_base_parser_bis_get_index(&parser);
268b3a520acSMatthias Ringwald             if ((bis_index == 0) || (bis_index > 30)){
269b3a520acSMatthias Ringwald                 continue;
270b3a520acSMatthias Ringwald             }
2717a236728SMatthias Ringwald 
2720707de65SMatthias Ringwald             // collect bis sync mask
273b3a520acSMatthias Ringwald             bis_sync_mask |=  1 << (bis_index - 1);
2747a236728SMatthias Ringwald 
275be81b159SMatthias Ringwald             bass_source_new.subgroups[i].metadata_length = 0;
276be81b159SMatthias Ringwald             memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
277be81b159SMatthias Ringwald 
27810277393SMatthias Ringwald             printf("        - bis index[%u][%u]: %u\n", i, k, bis_index);
279be81b159SMatthias Ringwald             codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
280be81b159SMatthias Ringwald             codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser);
28110277393SMatthias Ringwald             printf("        - codec specific config[%u][%u]: ", i, k);
282be81b159SMatthias Ringwald             printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
283be81b159SMatthias Ringwald             // parse next BIS
284be81b159SMatthias Ringwald             le_audio_base_parser_bis_next(&parser);
28510277393SMatthias Ringwald         }
286be81b159SMatthias Ringwald 
287be81b159SMatthias Ringwald         // parse next subgroup
288be81b159SMatthias Ringwald         le_audio_base_parser_subgroup_next(&parser);
28910277393SMatthias Ringwald     }
29010277393SMatthias Ringwald }
29110277393SMatthias Ringwald 
29210277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){
29310277393SMatthias Ringwald     printf("BIG Info advertising report\n");
29410277393SMatthias Ringwald     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
295ac95ea81SMatthias Ringwald     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
296ac95ea81SMatthias Ringwald     if (encryption) {
297b3a520acSMatthias Ringwald         printf("Stream is encrypted\n");
298ac95ea81SMatthias Ringwald     }
29910277393SMatthias Ringwald     have_big_info = true;
30010277393SMatthias Ringwald }
30110277393SMatthias Ringwald 
30210277393SMatthias Ringwald static void enter_create_big_sync(void){
30310277393SMatthias Ringwald     // stop scanning
30410277393SMatthias Ringwald     gap_stop_scan();
30510277393SMatthias Ringwald 
306*bb81690eSMatthias Ringwald     // init sink
307*bb81690eSMatthias Ringwald     uint16_t iso_interval_1250us = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 6 : 8;
308*bb81690eSMatthias Ringwald     uint8_t  pre_transmission_offset = 1;
309*bb81690eSMatthias Ringwald     le_audio_demo_util_sink_configure_broadcast(num_bis, 1, sampling_frequency_hz, frame_duration, octets_per_frame,
310*bb81690eSMatthias Ringwald                                               iso_interval_1250us, pre_transmission_offset);
31110277393SMatthias Ringwald 
3125c0d69beSMatthias Ringwald     big_sync_params.big_handle = big_handle;
3135c0d69beSMatthias Ringwald     big_sync_params.sync_handle = sync_handle;
314ac95ea81SMatthias Ringwald     big_sync_params.encryption = encryption;
315ac95ea81SMatthias Ringwald     if (encryption) {
316ac95ea81SMatthias Ringwald         memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16);
317ac95ea81SMatthias Ringwald     } else {
3185c0d69beSMatthias Ringwald         memset(big_sync_params.broadcast_code, 0, 16);
319ac95ea81SMatthias Ringwald     }
3205c0d69beSMatthias Ringwald     big_sync_params.mse = 0;
3215c0d69beSMatthias Ringwald     big_sync_params.big_sync_timeout_10ms = 100;
3225c0d69beSMatthias Ringwald     big_sync_params.num_bis = num_bis;
3235c0d69beSMatthias Ringwald     uint8_t i;
3245c0d69beSMatthias Ringwald     printf("BIG Create Sync for BIS: ");
3255c0d69beSMatthias Ringwald     for (i=0;i<num_bis;i++){
3265c0d69beSMatthias Ringwald         big_sync_params.bis_indices[i] = i + 1;
3275c0d69beSMatthias Ringwald         printf("%u ", big_sync_params.bis_indices[i]);
3285c0d69beSMatthias Ringwald     }
3295c0d69beSMatthias Ringwald     printf("\n");
3305c0d69beSMatthias Ringwald     app_state = APP_W4_BIG_SYNC_ESTABLISHED;
3315c0d69beSMatthias Ringwald     gap_big_sync_create(&big_sync_storage, &big_sync_params);
33210277393SMatthias Ringwald }
33310277393SMatthias Ringwald 
334111967cbSMatthias Ringwald static void start_scanning() {
335111967cbSMatthias Ringwald     app_state = APP_W4_BROADCAST_ADV;
336400a29ddSMatthias Ringwald     have_base = false;
337400a29ddSMatthias Ringwald     have_big_info = false;
338111967cbSMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 0);
339111967cbSMatthias Ringwald     gap_start_scan();
340111967cbSMatthias Ringwald     printf("Start scan..\n");
341111967cbSMatthias Ringwald }
342111967cbSMatthias Ringwald 
34381a67409SMatthias Ringwald static void setup_advertising() {
344dcd1707aSDirk Helbig     bd_addr_t local_addr;
345dcd1707aSDirk Helbig     gap_local_bd_addr(local_addr);
3466d708481SDirk Helbig     bool local_address_invalid = btstack_is_null_bd_addr( local_addr );
347dcd1707aSDirk Helbig     if( local_address_invalid ) {
348dcd1707aSDirk Helbig         extended_params.own_address_type = BD_ADDR_TYPE_LE_RANDOM;
349dcd1707aSDirk Helbig     }
350efd5f6d0SMatthias Ringwald     gap_extended_advertising_setup(&le_advertising_set, &extended_params, &adv_handle);
351dcd1707aSDirk Helbig     if( local_address_invalid ) {
352dcd1707aSDirk Helbig         bd_addr_t random_address = { 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01 };
353dcd1707aSDirk Helbig         gap_extended_advertising_set_random_address( adv_handle, random_address );
354dcd1707aSDirk Helbig     }
355efd5f6d0SMatthias Ringwald     gap_extended_advertising_set_adv_data(adv_handle, sizeof(extended_adv_data), extended_adv_data);
356efd5f6d0SMatthias Ringwald     gap_extended_advertising_start(adv_handle, 0, 0);
35781a67409SMatthias Ringwald }
35881a67409SMatthias Ringwald 
359b3a520acSMatthias Ringwald static void got_base_and_big_info() {
360b3a520acSMatthias Ringwald     // add source
361b3a520acSMatthias Ringwald     if (standalone_mode) {
362b3a520acSMatthias Ringwald         printf("BASS: add Broadcast Source\n");
363b3a520acSMatthias Ringwald         // add source
364b3a520acSMatthias Ringwald         uint8_t source_index = 0;
365abf8479fSMatthias Ringwald         broadcast_audio_scan_service_server_add_source(&bass_source_new, &source_index);
366b3a520acSMatthias Ringwald         broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
367b3a520acSMatthias Ringwald     }
368b3a520acSMatthias Ringwald 
369b3a520acSMatthias Ringwald     if ((encryption == false) || have_broadcast_code){
370b3a520acSMatthias Ringwald         enter_create_big_sync();
371b3a520acSMatthias Ringwald     } else {
372b3a520acSMatthias Ringwald         printf("BASS: request Broadcast Code\n");
373b3a520acSMatthias Ringwald         bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED;
374b3a520acSMatthias Ringwald         broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
375b3a520acSMatthias Ringwald         app_state = APP_W4_BROADCAST_CODE;
376b3a520acSMatthias Ringwald     }
377b3a520acSMatthias Ringwald }
378b3a520acSMatthias Ringwald 
37910277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
38010277393SMatthias Ringwald     UNUSED(channel);
38110277393SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
38210277393SMatthias Ringwald     switch (packet[0]) {
38310277393SMatthias Ringwald         case BTSTACK_EVENT_STATE:
38410277393SMatthias Ringwald             switch(btstack_event_state_get_state(packet)) {
38510277393SMatthias Ringwald                 case HCI_STATE_WORKING:
386400a29ddSMatthias Ringwald                     app_state = APP_IDLE;
3879a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE
388111967cbSMatthias Ringwald                     start_scanning();
3899a0d67ccSMatthias Ringwald #else
3909a0d67ccSMatthias Ringwald                     show_usage();
3919a0d67ccSMatthias Ringwald #endif
39210277393SMatthias Ringwald                     break;
39310277393SMatthias Ringwald                 case HCI_STATE_OFF:
39410277393SMatthias Ringwald                     printf("Goodbye\n");
39510277393SMatthias Ringwald                     exit(0);
39610277393SMatthias Ringwald                     break;
39710277393SMatthias Ringwald                 default:
39810277393SMatthias Ringwald                     break;
39910277393SMatthias Ringwald             }
40010277393SMatthias Ringwald             break;
40110277393SMatthias Ringwald         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
40210277393SMatthias Ringwald         {
40310277393SMatthias Ringwald             if (app_state != APP_W4_BROADCAST_ADV) break;
40410277393SMatthias Ringwald 
40510277393SMatthias Ringwald             gap_event_extended_advertising_report_get_address(packet, remote);
40610277393SMatthias Ringwald             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
40710277393SMatthias Ringwald             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
40810277393SMatthias Ringwald 
40910277393SMatthias Ringwald             ad_context_t context;
41010277393SMatthias Ringwald             bool found = false;
41110277393SMatthias Ringwald             remote_name[0] = '\0';
41210277393SMatthias Ringwald             uint16_t uuid;
41370718632SMatthias Ringwald             uint32_t broadcast_id;
41410277393SMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
41510277393SMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
41610277393SMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
41710277393SMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
41810277393SMatthias Ringwald                 switch (data_type){
41910277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
42010277393SMatthias Ringwald                         uuid = little_endian_read_16(data, 0);
42110277393SMatthias Ringwald                         if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){
42270718632SMatthias Ringwald                             broadcast_id = little_endian_read_24(data, 2);
42310277393SMatthias Ringwald                             found = true;
42410277393SMatthias Ringwald                         }
42510277393SMatthias Ringwald                         break;
42610277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
42710277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
42810277393SMatthias Ringwald                         size = btstack_min(sizeof(remote_name) - 1, size);
42910277393SMatthias Ringwald                         memcpy(remote_name, data, size);
43010277393SMatthias Ringwald                         remote_name[size] = 0;
4312fe0253dSMatthias Ringwald                         // support for nRF5340 Audio DK
4322fe0253dSMatthias Ringwald                         if (strncmp("NRF5340", remote_name, 7) == 0){
4332fe0253dSMatthias Ringwald                             nrf5340_audio_demo = true;
4342fe0253dSMatthias Ringwald                             found = true;
4352fe0253dSMatthias Ringwald                         }
43610277393SMatthias Ringwald                         break;
43710277393SMatthias Ringwald                     default:
43810277393SMatthias Ringwald                         break;
43910277393SMatthias Ringwald                 }
44010277393SMatthias Ringwald             }
44110277393SMatthias Ringwald             if (!found) break;
44210277393SMatthias Ringwald             remote_type = gap_event_extended_advertising_report_get_address_type(packet);
44310277393SMatthias Ringwald             remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
44410277393SMatthias Ringwald             pts_mode = strncmp("PTS-", remote_name, 4) == 0;
44510277393SMatthias Ringwald             count_mode = strncmp("COUNT", remote_name, 5) == 0;
44670718632SMatthias 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);
44710277393SMatthias Ringwald             // ignore other advertisements
44810277393SMatthias Ringwald             gap_whitelist_add(remote_type, remote);
44910277393SMatthias Ringwald             gap_set_scan_params(1, 0x30, 0x30, 1);
45010277393SMatthias Ringwald             // sync to PA
45110277393SMatthias Ringwald             gap_periodic_advertiser_list_clear();
45210277393SMatthias Ringwald             gap_periodic_advertiser_list_add(remote_type, remote, remote_sid);
45310277393SMatthias Ringwald             app_state = APP_W4_PA_AND_BIG_INFO;
45410277393SMatthias Ringwald             printf("Start Periodic Advertising Sync\n");
45510277393SMatthias Ringwald             gap_periodic_advertising_create_sync(0x01, remote_sid, remote_type, remote, 0, 1000, 0);
4567a236728SMatthias Ringwald 
4577a236728SMatthias Ringwald             // setup bass source info
4587a236728SMatthias Ringwald             bass_source_new.address_type = remote_type;
4597a236728SMatthias Ringwald             memcpy(bass_source_new.address, remote, 6);
4607a236728SMatthias Ringwald             bass_source_new.adv_sid = remote_sid;
4617a236728SMatthias Ringwald             bass_source_new.broadcast_id = broadcast_id;
4627a236728SMatthias Ringwald             bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
4637a236728SMatthias Ringwald             bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet);
46410277393SMatthias Ringwald             break;
46510277393SMatthias Ringwald         }
46610277393SMatthias Ringwald 
46710277393SMatthias Ringwald         case HCI_EVENT_LE_META:
46810277393SMatthias Ringwald             switch(hci_event_le_meta_get_subevent_code(packet)) {
46992f80bb0SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED:
470b3a520acSMatthias Ringwald                     // PAST implies broadcast source has been added by client
471ffbedad9SMatthias Ringwald                     printf("Periodic advertising sync transfer received\n");
4722de3ad61SMatthias Ringwald                     btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer);
473ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
47492f80bb0SMatthias Ringwald                     app_state = APP_W4_PA_AND_BIG_INFO;
47592f80bb0SMatthias Ringwald                     break;
47610277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
477400a29ddSMatthias Ringwald                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
478400a29ddSMatthias Ringwald                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
4797a236728SMatthias Ringwald                     app_state = APP_W4_PA_AND_BIG_INFO;
48010277393SMatthias Ringwald                     break;
48110277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
482ffbedad9SMatthias Ringwald                     if (app_state != APP_W4_PA_AND_BIG_INFO) break;
48310277393SMatthias Ringwald                     if (have_base) break;
48410277393SMatthias Ringwald                     handle_periodic_advertisement(packet, size);
485b3a520acSMatthias Ringwald                     if (have_base && have_big_info){
486b3a520acSMatthias Ringwald                         got_base_and_big_info();
48710277393SMatthias Ringwald                     }
48810277393SMatthias Ringwald                     break;
48910277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
490ffbedad9SMatthias Ringwald                     if (app_state != APP_W4_PA_AND_BIG_INFO) break;
49110277393SMatthias Ringwald                     if (have_big_info) break;
49210277393SMatthias Ringwald                     handle_big_info(packet, size);
493b3a520acSMatthias Ringwald                     if (have_base && have_big_info){
494b3a520acSMatthias Ringwald                         got_base_and_big_info();
49510277393SMatthias Ringwald                     }
49610277393SMatthias Ringwald                     break;
497111967cbSMatthias Ringwald                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
498111967cbSMatthias Ringwald                     printf("BIG Sync Lost\n");
499111967cbSMatthias Ringwald                     {
500111967cbSMatthias Ringwald                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
501111967cbSMatthias Ringwald                         if (sink != NULL) {
502111967cbSMatthias Ringwald                             sink->stop_stream();
503111967cbSMatthias Ringwald                             sink->close();
504111967cbSMatthias Ringwald                         }
505111967cbSMatthias Ringwald                     }
506111967cbSMatthias Ringwald                     // start over
507ffbedad9SMatthias Ringwald                     if (!standalone_mode) break;
508111967cbSMatthias Ringwald                     start_scanning();
509111967cbSMatthias Ringwald                     break;
51010277393SMatthias Ringwald                 default:
51110277393SMatthias Ringwald                     break;
51210277393SMatthias Ringwald             }
51310277393SMatthias Ringwald             break;
5145c0d69beSMatthias Ringwald         case HCI_EVENT_META_GAP:
5155c0d69beSMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)){
5165c0d69beSMatthias Ringwald                 case GAP_SUBEVENT_BIG_SYNC_CREATED: {
5175c0d69beSMatthias Ringwald                     printf("BIG Sync created with BIS Connection handles: ");
5185c0d69beSMatthias Ringwald                     uint8_t i;
51910277393SMatthias Ringwald                     for (i=0;i<num_bis;i++){
5205c0d69beSMatthias Ringwald                         bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i);
5215c0d69beSMatthias Ringwald                         printf("0x%04x ", bis_con_handles[i]);
52210277393SMatthias Ringwald                     }
5230707de65SMatthias Ringwald                     printf("\n");
52410277393SMatthias Ringwald                     app_state = APP_STREAMING;
525151e8cb9SMatthias Ringwald                     printf("Start receiving\n");
526ffbedad9SMatthias Ringwald 
527ffbedad9SMatthias Ringwald                     // update BIS Sync state
5280707de65SMatthias Ringwald                     bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask;
529ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
53010277393SMatthias Ringwald                     break;
5315c0d69beSMatthias Ringwald                 }
532e38308a2SMatthias Ringwald                 case GAP_SUBEVENT_BIG_SYNC_STOPPED:
533e38308a2SMatthias Ringwald                     printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet));
534e38308a2SMatthias Ringwald                     break;
5355c0d69beSMatthias Ringwald                 default:
5365c0d69beSMatthias Ringwald                     break;
5375c0d69beSMatthias Ringwald             }
53810277393SMatthias Ringwald             break;
539efd5f6d0SMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
540efd5f6d0SMatthias Ringwald             printf("Just Works requested\n");
541efd5f6d0SMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
542efd5f6d0SMatthias Ringwald             break;
54310277393SMatthias Ringwald         default:
54410277393SMatthias Ringwald             break;
54510277393SMatthias Ringwald     }
54610277393SMatthias Ringwald }
54710277393SMatthias Ringwald 
54810277393SMatthias Ringwald static void show_usage(void){
54910277393SMatthias Ringwald     printf("\n--- LE Audio Broadcast Sink Test Console ---\n");
5509a0d67ccSMatthias Ringwald     printf("s - start scanning\n");
5519a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
5529a0d67ccSMatthias Ringwald     printf("q - use LC3plus decoder if 10 ms ISO interval is used\n");
5539a0d67ccSMatthias Ringwald #endif
5540707de65SMatthias Ringwald     printf("e - set broadcast code to 0x11111111111111111111111111111111\n");
555400a29ddSMatthias Ringwald     printf("t - terminate BIS streams\n");
55610277393SMatthias Ringwald     printf("---\n");
55710277393SMatthias Ringwald }
55810277393SMatthias Ringwald 
55910277393SMatthias Ringwald static void stdin_process(char c){
56010277393SMatthias Ringwald     switch (c){
5619a0d67ccSMatthias Ringwald         case 's':
562400a29ddSMatthias Ringwald             if (app_state != APP_IDLE) break;
563ffbedad9SMatthias Ringwald             standalone_mode = true;
5649a0d67ccSMatthias Ringwald             start_scanning();
5659a0d67ccSMatthias Ringwald             break;
5660707de65SMatthias Ringwald         case 'e':
5670707de65SMatthias Ringwald             printf("Set broadcast code to 0x11111111111111111111111111111111\n");
5680707de65SMatthias Ringwald             memset(broadcast_code, 0x11, 16);
5690707de65SMatthias Ringwald             have_broadcast_code = true;
5700707de65SMatthias Ringwald             break;
5719a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
5729a0d67ccSMatthias Ringwald         case 'q':
573151e8cb9SMatthias Ringwald             printf("Use LC3plus decoder for 10 ms ISO interval...\n");
5749a0d67ccSMatthias Ringwald             request_lc3plus_decoder = true;
5759a0d67ccSMatthias Ringwald             break;
5769a0d67ccSMatthias Ringwald #endif
577400a29ddSMatthias Ringwald         case 't':
578400a29ddSMatthias Ringwald             switch (app_state){
579400a29ddSMatthias Ringwald                 case APP_STREAMING:
580400a29ddSMatthias Ringwald                 case APP_W4_BIG_SYNC_ESTABLISHED:
581400a29ddSMatthias Ringwald                     app_state = APP_IDLE;
582*bb81690eSMatthias Ringwald                     le_audio_demo_util_sink_close();
583400a29ddSMatthias Ringwald                     printf("Terminate BIG SYNC\n");
584400a29ddSMatthias Ringwald                     gap_big_sync_terminate(big_handle);
585400a29ddSMatthias Ringwald                     break;
586400a29ddSMatthias Ringwald                 default:
587400a29ddSMatthias Ringwald                     break;
588400a29ddSMatthias Ringwald             }
589400a29ddSMatthias Ringwald             break;
59010277393SMatthias Ringwald         case '\n':
59110277393SMatthias Ringwald         case '\r':
59210277393SMatthias Ringwald             break;
59310277393SMatthias Ringwald         default:
59410277393SMatthias Ringwald             show_usage();
59510277393SMatthias Ringwald             break;
59610277393SMatthias Ringwald 
59710277393SMatthias Ringwald     }
59810277393SMatthias Ringwald }
5992de3ad61SMatthias Ringwald 
600*bb81690eSMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
601*bb81690eSMatthias Ringwald     // get stream_index from con_handle
602*bb81690eSMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
603*bb81690eSMatthias Ringwald     hci_con_handle_t con_handle = header & 0x0fff;
604*bb81690eSMatthias Ringwald     uint8_t i;
605*bb81690eSMatthias Ringwald     for (i=0;i<num_bis;i++){
606*bb81690eSMatthias Ringwald         if (bis_con_handles[i] == con_handle) {
607*bb81690eSMatthias Ringwald             le_audio_demo_util_sink_receive(i, packet, size);
608*bb81690eSMatthias Ringwald         }
609*bb81690eSMatthias Ringwald     }
610*bb81690eSMatthias Ringwald }
611*bb81690eSMatthias Ringwald 
6122de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){
6132de3ad61SMatthias Ringwald     UNUSED(ts);
6142de3ad61SMatthias Ringwald     printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n");
6152de3ad61SMatthias Ringwald     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST);
6162de3ad61SMatthias Ringwald }
6172de3ad61SMatthias Ringwald 
61860d70dccSMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
61960d70dccSMatthias Ringwald     UNUSED(packet_type);
62060d70dccSMatthias Ringwald     UNUSED(channel);
621ffbedad9SMatthias Ringwald     btstack_assert (packet_type == HCI_EVENT_PACKET);
622ffbedad9SMatthias Ringwald     btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META);
623ffbedad9SMatthias Ringwald     printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet));
624ffbedad9SMatthias Ringwald     printf_hexdump(packet, size);
625ffbedad9SMatthias Ringwald     switch (hci_event_gattservice_meta_get_subevent_code(packet)){
626bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED:
627ffbedad9SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n",
628bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_source_id(packet),
629bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_pa_sync(packet));
630bebf6b57SMatthias Ringwald             switch (gattservice_subevent_bass_server_source_added_get_pa_sync(packet)){
6312de3ad61SMatthias Ringwald                 case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE:
6322de3ad61SMatthias Ringwald                     printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n");
633ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST);
6342de3ad61SMatthias Ringwald                     // start timeout
6352de3ad61SMatthias Ringwald                     btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler);
6362de3ad61SMatthias Ringwald                     btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000);   // 10 seconds
6372de3ad61SMatthias Ringwald                     btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer);
6382de3ad61SMatthias Ringwald                     break;
6392de3ad61SMatthias Ringwald             }
640ffbedad9SMatthias Ringwald             break;
641bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED:
642ffbedad9SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n",
643bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_source_id(packet),
644bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_pa_sync(packet));
645ffbedad9SMatthias Ringwald             // handle 'bis sync == 0'
646ffbedad9SMatthias Ringwald             printf("PA Sync %u, bis_sync[0] = %u\n", bass_sources[0].data.pa_sync, bass_sources[0].data.subgroups[0].bis_sync);
647ffbedad9SMatthias Ringwald             if (bass_sources[0].data.subgroups[0].bis_sync == 0){
648ffbedad9SMatthias Ringwald                 printf("Simulate BIS Sync has stopped\n");
649ffbedad9SMatthias Ringwald                 bass_sources[0].data.subgroups[0].bis_sync_state = 0;
650ffbedad9SMatthias Ringwald                 broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
651ffbedad9SMatthias Ringwald             }
6520707de65SMatthias Ringwald             break;
653bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE:
654bebf6b57SMatthias Ringwald             gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code);
6550707de65SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: ");
6560707de65SMatthias Ringwald             printf_hexdump(broadcast_code, 16);
6570707de65SMatthias Ringwald             bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING;
6580707de65SMatthias Ringwald             if (have_base && have_big_info){
6590707de65SMatthias Ringwald                 enter_create_big_sync();
6600707de65SMatthias Ringwald             }
6610707de65SMatthias Ringwald             break;
662ffbedad9SMatthias Ringwald         default:
663ffbedad9SMatthias Ringwald             break;
664ffbedad9SMatthias Ringwald     }
66560d70dccSMatthias Ringwald }
66610277393SMatthias Ringwald 
66710277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
66810277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
66910277393SMatthias Ringwald     (void) argv;
67010277393SMatthias Ringwald     (void) argc;
67110277393SMatthias Ringwald 
672efd5f6d0SMatthias Ringwald     l2cap_init();
673efd5f6d0SMatthias Ringwald     sm_init();
674efd5f6d0SMatthias Ringwald 
675efd5f6d0SMatthias Ringwald     // setup ATT server
676efd5f6d0SMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
677efd5f6d0SMatthias Ringwald 
67810277393SMatthias Ringwald     // register for HCI events
67910277393SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
68010277393SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
68110277393SMatthias Ringwald 
68210277393SMatthias Ringwald     // register for ISO Packet
68310277393SMatthias Ringwald     hci_register_iso_packet_handler(&iso_packet_handler);
68410277393SMatthias Ringwald 
685efd5f6d0SMatthias Ringwald     // register for SM events
686efd5f6d0SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
687efd5f6d0SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
688efd5f6d0SMatthias Ringwald 
68960d70dccSMatthias Ringwald     // setup BASS Server
69060d70dccSMatthias Ringwald     broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients);
69160d70dccSMatthias Ringwald     broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler);
69260d70dccSMatthias Ringwald 
69333bea8deSMatthias Ringwald     // setup advertising and allow to receive periodic advertising sync trasnfers
69433bea8deSMatthias Ringwald     setup_advertising();
69533bea8deSMatthias Ringwald     gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0);
69633bea8deSMatthias Ringwald 
69710277393SMatthias Ringwald     // turn on!
69810277393SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
69910277393SMatthias Ringwald 
70010277393SMatthias Ringwald     btstack_stdin_setup(stdin_process);
70110277393SMatthias Ringwald     return 0;
70210277393SMatthias Ringwald }
703