xref: /btstack/test/le_audio/le_audio_broadcast_sink.c (revision bebf6b57a4f7ab67a9a48c3445b8ff5ef4391a04)
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"
764a5b1c65SMatthias Ringwald 
774a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
7810277393SMatthias Ringwald #include "wav_util.h"
794a5b1c65SMatthias Ringwald #endif
8010277393SMatthias Ringwald 
817a5bad53SMatthias Ringwald // #define DEBUG_PLC
827a5bad53SMatthias Ringwald #ifdef DEBUG_PLC
837a5bad53SMatthias Ringwald #define printf_plc(...) printf(__VA_ARGS__)
847a5bad53SMatthias Ringwald #else
857a5bad53SMatthias Ringwald #define printf_plc(...)  (void)(0);
867a5bad53SMatthias Ringwald #endif
877a5bad53SMatthias Ringwald 
8810277393SMatthias Ringwald // max config
8910277393SMatthias Ringwald #define MAX_NUM_BIS 2
9010277393SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480
9110277393SMatthias Ringwald 
9210277393SMatthias Ringwald // playback
9310277393SMatthias Ringwald #define MAX_NUM_LC3_FRAMES   5
9410277393SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4
9510277393SMatthias Ringwald #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE)
9610277393SMatthias Ringwald 
9710277393SMatthias Ringwald // analysis
9810277393SMatthias Ringwald #define PACKET_PREFIX_LEN 10
9910277393SMatthias Ringwald 
10010277393SMatthias Ringwald #define ANSI_COLOR_RED     "\x1b[31m"
10110277393SMatthias Ringwald #define ANSI_COLOR_GREEN   "\x1b[32m"
10210277393SMatthias Ringwald #define ANSI_COLOR_YELLOW  "\x1b[33m"
10310277393SMatthias Ringwald #define ANSI_COLOR_BLUE    "\x1b[34m"
10410277393SMatthias Ringwald #define ANSI_COLOR_MAGENTA "\x1b[35m"
10510277393SMatthias Ringwald #define ANSI_COLOR_CYAN    "\x1b[36m"
10610277393SMatthias Ringwald #define ANSI_COLOR_RESET   "\x1b[0m"
10710277393SMatthias Ringwald 
10810277393SMatthias Ringwald static void show_usage(void);
10910277393SMatthias Ringwald 
11010277393SMatthias Ringwald static const char * filename_wav = "le_audio_broadcast_sink.wav";
11110277393SMatthias Ringwald 
11210277393SMatthias Ringwald static enum {
11310277393SMatthias Ringwald     APP_W4_WORKING,
11410277393SMatthias Ringwald     APP_W4_BROADCAST_ADV,
11510277393SMatthias Ringwald     APP_W4_PA_AND_BIG_INFO,
116b3a520acSMatthias Ringwald     APP_W4_BROADCAST_CODE,
11710277393SMatthias Ringwald     APP_W4_BIG_SYNC_ESTABLISHED,
11810277393SMatthias Ringwald     APP_STREAMING,
11910277393SMatthias Ringwald     APP_IDLE
12010277393SMatthias Ringwald } app_state = APP_W4_WORKING;
12110277393SMatthias Ringwald 
122efd5f6d0SMatthias Ringwald static const uint8_t adv_sid = 0;
123efd5f6d0SMatthias Ringwald static le_advertising_set_t le_advertising_set;
124efd5f6d0SMatthias Ringwald static uint8_t adv_handle = 0;
125efd5f6d0SMatthias Ringwald 
126efd5f6d0SMatthias Ringwald static const le_extended_advertising_parameters_t extended_params = {
127efd5f6d0SMatthias Ringwald         .advertising_event_properties = 1,  // connectable
128efd5f6d0SMatthias Ringwald         .primary_advertising_interval_min = 0x4b0, // 750 ms
129efd5f6d0SMatthias Ringwald         .primary_advertising_interval_max = 0x4b0, // 750 ms
130efd5f6d0SMatthias Ringwald         .primary_advertising_channel_map = 7,
131efd5f6d0SMatthias Ringwald         .own_address_type = 0,
132efd5f6d0SMatthias Ringwald         .peer_address_type = 0,
133efd5f6d0SMatthias Ringwald         .peer_address =  { 0 },
134efd5f6d0SMatthias Ringwald         .advertising_filter_policy = 0,
135efd5f6d0SMatthias Ringwald         .advertising_tx_power = 10, // 10 dBm
136efd5f6d0SMatthias Ringwald         .primary_advertising_phy = 1, // LE 1M PHY
137efd5f6d0SMatthias Ringwald         .secondary_advertising_max_skip = 0,
138efd5f6d0SMatthias Ringwald         .secondary_advertising_phy = 1, // LE 1M PHY
139efd5f6d0SMatthias Ringwald         .advertising_sid = adv_sid,
140efd5f6d0SMatthias Ringwald         .scan_request_notification_enable = 0,
141efd5f6d0SMatthias Ringwald };
142efd5f6d0SMatthias Ringwald 
143efd5f6d0SMatthias Ringwald static const uint8_t extended_adv_data[] = {
1443b0d944cSMatthias Ringwald         // 16 bit service data, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE,
1453b0d944cSMatthias Ringwald         3, BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID,
14681a67409SMatthias Ringwald             ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE & 0xff, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE >> 8,
14781a67409SMatthias Ringwald        // name
14881a67409SMatthias Ringwald         5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'i', 'n', 'k'
14981a67409SMatthias Ringwald };
15081a67409SMatthias Ringwald 
15160d70dccSMatthias Ringwald #define BASS_NUM_CLIENTS 1
15260d70dccSMatthias Ringwald #define BASS_NUM_SOURCES 1
1537a236728SMatthias Ringwald static bass_source_data_t       bass_source_new;
15460d70dccSMatthias Ringwald static bass_server_source_t     bass_sources[BASS_NUM_SOURCES];
155*bebf6b57SMatthias Ringwald static bass_server_connection_t bass_clients[BASS_NUM_CLIENTS];
15660d70dccSMatthias Ringwald 
15710277393SMatthias Ringwald //
15810277393SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
159efd5f6d0SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
16010277393SMatthias Ringwald 
16110277393SMatthias Ringwald static bool have_base;
16210277393SMatthias Ringwald static bool have_big_info;
163ffbedad9SMatthias Ringwald static bool have_past;
1640707de65SMatthias Ringwald static bool have_broadcast_code;
165ffbedad9SMatthias Ringwald static bool standalone_mode;
1660707de65SMatthias Ringwald static uint32_t bis_sync_mask;
16710277393SMatthias Ringwald 
16810277393SMatthias Ringwald uint32_t last_samples_report_ms;
1694a5b1c65SMatthias Ringwald uint16_t samples_received;
1704a5b1c65SMatthias Ringwald uint16_t samples_dropped;
17110277393SMatthias Ringwald uint16_t frames_per_second[MAX_NUM_BIS];
17210277393SMatthias Ringwald 
17310277393SMatthias Ringwald // remote info
17410277393SMatthias Ringwald static char remote_name[20];
17510277393SMatthias Ringwald static bd_addr_t remote;
17610277393SMatthias Ringwald static bd_addr_type_t remote_type;
17710277393SMatthias Ringwald static uint8_t remote_sid;
17810277393SMatthias Ringwald static bool count_mode;
17910277393SMatthias Ringwald static bool pts_mode;
1802fe0253dSMatthias Ringwald static bool nrf5340_audio_demo;
1812fe0253dSMatthias Ringwald 
18210277393SMatthias Ringwald 
18310277393SMatthias Ringwald // broadcast info
18410277393SMatthias Ringwald static const uint8_t    big_handle = 1;
18510277393SMatthias Ringwald static hci_con_handle_t sync_handle;
18610277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS];
18710277393SMatthias Ringwald static unsigned int     next_bis_index;
188ac95ea81SMatthias Ringwald static uint8_t          encryption;
1890707de65SMatthias Ringwald static uint8_t          broadcast_code[16];
19010277393SMatthias Ringwald 
1912de3ad61SMatthias Ringwald static btstack_timer_source_t broadcast_sink_pa_sync_timer;
1922de3ad61SMatthias Ringwald 
19310277393SMatthias Ringwald // analysis
1941716d43dSMatthias Ringwald static bool     last_packet_received_big;
1951716d43dSMatthias Ringwald static uint16_t last_packet_sequence_big;
196ae7e4c11SMatthias Ringwald static bool     last_packet_received[MAX_NUM_BIS];
19710277393SMatthias Ringwald static uint16_t last_packet_sequence[MAX_NUM_BIS];
19810277393SMatthias Ringwald static uint32_t last_packet_time_ms[MAX_NUM_BIS];
19910277393SMatthias Ringwald static uint8_t  last_packet_prefix[MAX_NUM_BIS * PACKET_PREFIX_LEN];
20010277393SMatthias Ringwald 
2015c0d69beSMatthias Ringwald // BIG Sync
2025c0d69beSMatthias Ringwald static le_audio_big_sync_t        big_sync_storage;
2035c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params;
2045c0d69beSMatthias Ringwald 
20510277393SMatthias Ringwald // lc3 writer
20610277393SMatthias Ringwald static uint32_t lc3_frames;
20710277393SMatthias Ringwald 
20810277393SMatthias Ringwald // lc3 codec config
2094a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz;
2102fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration;
21110277393SMatthias Ringwald static uint16_t number_samples_per_frame;
21210277393SMatthias Ringwald static uint16_t octets_per_frame;
21310277393SMatthias Ringwald static uint8_t  num_bis;
21410277393SMatthias Ringwald 
21510277393SMatthias Ringwald // lc3 decoder
2169a0d67ccSMatthias Ringwald static bool request_lc3plus_decoder = false;
2179a0d67ccSMatthias Ringwald static bool use_lc3plus_decoder = false;
2182fd68da2SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder;
21910277393SMatthias Ringwald static int16_t pcm[MAX_NUM_BIS * MAX_SAMPLES_PER_FRAME];
22010277393SMatthias Ringwald 
2219a0d67ccSMatthias Ringwald static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_NUM_BIS];
2229a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
2239a0d67ccSMatthias Ringwald static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_NUM_BIS];
2249a0d67ccSMatthias Ringwald #endif
2259a0d67ccSMatthias Ringwald static void * decoder_contexts[MAX_NR_BIS];
2269a0d67ccSMatthias Ringwald 
22710277393SMatthias Ringwald // playback
22810277393SMatthias Ringwald static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE];
22910277393SMatthias Ringwald static btstack_ring_buffer_t playback_buffer;
23010277393SMatthias Ringwald 
2311716d43dSMatthias Ringwald static btstack_timer_source_t next_packet_timer;
23222d64f35SMatthias Ringwald static uint16_t               cached_iso_sdu_len;
23322d64f35SMatthias Ringwald static bool                   have_pcm[MAX_NUM_BIS];
23422d64f35SMatthias Ringwald 
23510277393SMatthias Ringwald static void le_audio_broadcast_sink_playback(int16_t * buffer, uint16_t num_samples){
23610277393SMatthias Ringwald     // called from lower-layer but guaranteed to be on main thread
23710277393SMatthias Ringwald     uint32_t bytes_needed = num_samples * num_bis * 2;
23810277393SMatthias Ringwald 
23910277393SMatthias Ringwald     static bool underrun = true;
24010277393SMatthias Ringwald 
24110277393SMatthias Ringwald     log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / ( num_bis * 2));
24210277393SMatthias Ringwald 
24310277393SMatthias Ringwald     if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)){
24410277393SMatthias Ringwald         memset(buffer, 0, bytes_needed);
24510277393SMatthias Ringwald         if (underrun == false){
24610277393SMatthias Ringwald             log_info("Playback underrun");
24710277393SMatthias Ringwald             underrun = true;
24810277393SMatthias Ringwald         }
24910277393SMatthias Ringwald         return;
25010277393SMatthias Ringwald     }
25110277393SMatthias Ringwald 
25210277393SMatthias Ringwald     if (underrun){
25310277393SMatthias Ringwald         underrun = false;
25410277393SMatthias Ringwald         log_info("Playback started");
25510277393SMatthias Ringwald     }
25610277393SMatthias Ringwald     uint32_t bytes_read;
25710277393SMatthias Ringwald     btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
25810277393SMatthias Ringwald     btstack_assert(bytes_read == bytes_needed);
25910277393SMatthias Ringwald }
26010277393SMatthias Ringwald 
26110277393SMatthias Ringwald static void setup_lc3_decoder(void){
26210277393SMatthias Ringwald     uint8_t channel;
26310277393SMatthias Ringwald     for (channel = 0 ; channel < num_bis ; channel++){
2649a0d67ccSMatthias Ringwald         // pick decoder
2659a0d67ccSMatthias Ringwald         void * decoder_context = NULL;
2669a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
2679a0d67ccSMatthias Ringwald         if (use_lc3plus_decoder){
2689a0d67ccSMatthias Ringwald             decoder_context = &fraunhofer_decoder_contexts[channel];
2699a0d67ccSMatthias Ringwald             lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context);
2709a0d67ccSMatthias Ringwald         }
2719a0d67ccSMatthias Ringwald         else
2729a0d67ccSMatthias Ringwald #endif
2739a0d67ccSMatthias Ringwald         {
2749a0d67ccSMatthias Ringwald             decoder_context = &google_decoder_contexts[channel];
275e40ee29aSMatthias Ringwald             lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context);
2769a0d67ccSMatthias Ringwald         }
2779a0d67ccSMatthias Ringwald         decoder_contexts[channel] = decoder_context;
278da364eecSMatthias Ringwald         lc3_decoder->configure(decoder_context, sampling_frequency_hz, frame_duration, octets_per_frame);
27910277393SMatthias Ringwald     }
280dc0d751cSMatthias Ringwald     number_samples_per_frame = btstack_lc3_samples_per_frame(sampling_frequency_hz, frame_duration);
28110277393SMatthias Ringwald     btstack_assert(number_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
28210277393SMatthias Ringwald }
28310277393SMatthias Ringwald 
28410277393SMatthias Ringwald static void close_files(void){
2854a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
28610277393SMatthias Ringwald     printf("Close files\n");
28710277393SMatthias Ringwald     wav_writer_close();
2884a5b1c65SMatthias Ringwald #endif
289e38308a2SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
290e38308a2SMatthias Ringwald     if (sink != NULL){
291e38308a2SMatthias Ringwald         sink->stop_stream();
292e38308a2SMatthias Ringwald         sink->close();
293e38308a2SMatthias Ringwald     }
29410277393SMatthias Ringwald }
29510277393SMatthias Ringwald 
29610277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
2972fe0253dSMatthias Ringwald     // nRF534_audio quirk - no BASE in periodic advertisement
2982fe0253dSMatthias Ringwald     if (nrf5340_audio_demo){
2992fe0253dSMatthias Ringwald         // hard coded config LC3
3002fe0253dSMatthias Ringwald         // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame
3012fe0253dSMatthias Ringwald         count_mode = 0;
3022fe0253dSMatthias Ringwald         pts_mode   = 0;
3032fe0253dSMatthias Ringwald         num_bis    = 1;
3042fe0253dSMatthias Ringwald         sampling_frequency_hz = 48000;
3052fe0253dSMatthias Ringwald         frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US;
3062fe0253dSMatthias Ringwald         octets_per_frame = 120;
3072fe0253dSMatthias Ringwald         have_base = true;
3082fe0253dSMatthias Ringwald         return;
3092fe0253dSMatthias Ringwald     }
3102fe0253dSMatthias Ringwald 
31110277393SMatthias Ringwald     // periodic advertisement contains the BASE
31210277393SMatthias Ringwald     // TODO: BASE might be split across multiple advertisements
31310277393SMatthias Ringwald     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
31410277393SMatthias Ringwald     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
3158206d906SMatthias Ringwald     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
3168206d906SMatthias Ringwald 
3178206d906SMatthias Ringwald     if (adv_status != 0) {
3188206d906SMatthias Ringwald         printf("Periodic Advertisement (status %u): ", adv_status);
3198206d906SMatthias Ringwald         printf_hexdump(adv_data, adv_size);
3208206d906SMatthias Ringwald         return;
3218206d906SMatthias Ringwald     }
32210277393SMatthias Ringwald 
323be81b159SMatthias Ringwald     le_audio_base_parser_t parser;
324be81b159SMatthias Ringwald     bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size);
325be81b159SMatthias Ringwald     if (ok == false){
326be81b159SMatthias Ringwald         return;
327be81b159SMatthias Ringwald     }
32810277393SMatthias Ringwald     have_base = true;
329be81b159SMatthias Ringwald 
33010277393SMatthias Ringwald     printf("BASE:\n");
331be81b159SMatthias Ringwald     uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser);
33210277393SMatthias Ringwald     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
333be81b159SMatthias Ringwald     uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser);
3347a236728SMatthias Ringwald     // Cache in new source struct
3357a236728SMatthias Ringwald     bass_source_new.subgroups_num = num_subgroups;
33610277393SMatthias Ringwald     printf("- num subgroups: %u\n", num_subgroups);
33710277393SMatthias Ringwald     uint8_t i;
33810277393SMatthias Ringwald     for (i=0;i<num_subgroups;i++){
33910277393SMatthias Ringwald         // Level 2: Subgroup Level
340109dd080SMatthias Ringwald         printf("  - Subgroup %u\n", i);
341be81b159SMatthias Ringwald         num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser);
34210277393SMatthias Ringwald         printf("    - num bis[%u]: %u\n", i, num_bis);
343109dd080SMatthias Ringwald         uint8_t codec_specific_configuration_length = le_audio_base_parser_subgroup_get_codec_specific_configuration_length(&parser);
344be81b159SMatthias Ringwald         const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser);
34510277393SMatthias Ringwald         printf("    - codec specific config[%u]: ", i);
34610277393SMatthias Ringwald         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
34710277393SMatthias Ringwald         // parse config to get sampling frequency and frame duration
34810277393SMatthias Ringwald         uint8_t codec_offset = 0;
34910277393SMatthias Ringwald         while ((codec_offset + 1) < codec_specific_configuration_length){
35010277393SMatthias Ringwald             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
35110277393SMatthias Ringwald             uint8_t ltv_type = codec_specific_configuration[codec_offset];
35210277393SMatthias Ringwald             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
35310277393SMatthias Ringwald             uint8_t sampling_frequency_index;
35410277393SMatthias Ringwald             uint8_t frame_duration_index;
35510277393SMatthias Ringwald             switch (ltv_type){
35610277393SMatthias Ringwald                 case 0x01: // sampling frequency
35710277393SMatthias Ringwald                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
35810277393SMatthias Ringwald                     // TODO: check range
35910277393SMatthias Ringwald                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
3604a5b1c65SMatthias Ringwald                     printf("      - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
36110277393SMatthias Ringwald                     break;
36210277393SMatthias Ringwald                 case 0x02: // 0 = 7.5, 1 = 10 ms
36310277393SMatthias Ringwald                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
3642fd68da2SMatthias Ringwald                     frame_duration = (frame_duration_index == 0) ? BTSTACK_LC3_FRAME_DURATION_7500US : BTSTACK_LC3_FRAME_DURATION_10000US;
3652fd68da2SMatthias Ringwald                     printf("      - frame duration[%u]: %s ms\n", i, (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? "7.5" : "10");
36610277393SMatthias Ringwald                     break;
36710277393SMatthias Ringwald                 case 0x04:  // octets per coding frame
36810277393SMatthias Ringwald                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
36910277393SMatthias Ringwald                     printf("      - octets per codec frame[%u]: %u\n", i, octets_per_frame);
37010277393SMatthias Ringwald                     break;
37110277393SMatthias Ringwald                 default:
37210277393SMatthias Ringwald                     break;
37310277393SMatthias Ringwald             }
37410277393SMatthias Ringwald             codec_offset += ltv_len;
37510277393SMatthias Ringwald         }
376be81b159SMatthias Ringwald         uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser);
377be81b159SMatthias Ringwald         const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser);
37810277393SMatthias Ringwald         printf("    - meta data[%u]: ", i);
37910277393SMatthias Ringwald         printf_hexdump(meta_data, metadata_length);
38010277393SMatthias Ringwald         uint8_t k;
38110277393SMatthias Ringwald         for (k=0;k<num_bis;k++){
382109dd080SMatthias Ringwald             printf("      - BIS %u\n", k);
38310277393SMatthias Ringwald             // Level 3: BIS Level
384be81b159SMatthias Ringwald             uint8_t bis_index =  le_audio_base_parser_bis_get_index(&parser);
385b3a520acSMatthias Ringwald             if ((bis_index == 0) || (bis_index > 30)){
386b3a520acSMatthias Ringwald                 continue;
387b3a520acSMatthias Ringwald             }
3887a236728SMatthias Ringwald 
3890707de65SMatthias Ringwald             // collect bis sync mask
390b3a520acSMatthias Ringwald             bis_sync_mask |=  1 << (bis_index - 1);
3917a236728SMatthias Ringwald 
392be81b159SMatthias Ringwald             bass_source_new.subgroups[i].metadata_length = 0;
393be81b159SMatthias Ringwald             memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
394be81b159SMatthias Ringwald 
39510277393SMatthias Ringwald             printf("        - bis index[%u][%u]: %u\n", i, k, bis_index);
396be81b159SMatthias Ringwald             codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
397be81b159SMatthias Ringwald             codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser);
39810277393SMatthias Ringwald             printf("        - codec specific config[%u][%u]: ", i, k);
399be81b159SMatthias Ringwald             printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
400be81b159SMatthias Ringwald             // parse next BIS
401be81b159SMatthias Ringwald             le_audio_base_parser_bis_next(&parser);
40210277393SMatthias Ringwald         }
403be81b159SMatthias Ringwald 
404be81b159SMatthias Ringwald         // parse next subgroup
405be81b159SMatthias Ringwald         le_audio_base_parser_subgroup_next(&parser);
40610277393SMatthias Ringwald     }
40710277393SMatthias Ringwald }
40810277393SMatthias Ringwald 
40910277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){
41010277393SMatthias Ringwald     printf("BIG Info advertising report\n");
41110277393SMatthias Ringwald     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
412ac95ea81SMatthias Ringwald     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
413ac95ea81SMatthias Ringwald     if (encryption) {
414b3a520acSMatthias Ringwald         printf("Stream is encrypted\n");
415ac95ea81SMatthias Ringwald     }
41610277393SMatthias Ringwald     have_big_info = true;
41710277393SMatthias Ringwald }
41810277393SMatthias Ringwald 
41910277393SMatthias Ringwald static void enter_create_big_sync(void){
42010277393SMatthias Ringwald     // stop scanning
42110277393SMatthias Ringwald     gap_stop_scan();
42210277393SMatthias Ringwald 
4239a0d67ccSMatthias Ringwald     // switch to lc3plus if requested and possible
4249a0d67ccSMatthias Ringwald     use_lc3plus_decoder = request_lc3plus_decoder && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US);
4259a0d67ccSMatthias Ringwald 
42610277393SMatthias Ringwald     // init decoder
42710277393SMatthias Ringwald     setup_lc3_decoder();
42810277393SMatthias Ringwald 
4299a0d67ccSMatthias Ringwald     printf("Configure: %u channels, sampling rate %u, samples per frame %u, lc3plus %u\n", num_bis, sampling_frequency_hz, number_samples_per_frame, use_lc3plus_decoder);
43010277393SMatthias Ringwald 
4314a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
43210277393SMatthias Ringwald     // create wav file
43310277393SMatthias Ringwald     printf("WAV file: %s\n", filename_wav);
43410277393SMatthias Ringwald     wav_writer_open(filename_wav, num_bis, sampling_frequency_hz);
4354a5b1c65SMatthias Ringwald #endif
43610277393SMatthias Ringwald 
43710277393SMatthias Ringwald     // init playback buffer
43810277393SMatthias Ringwald     btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
43910277393SMatthias Ringwald 
44010277393SMatthias Ringwald     // start playback
44110277393SMatthias Ringwald     // PTS 8.2 sends stereo at half speed for stereo, for now playback at half speed
44210277393SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
44310277393SMatthias Ringwald     if (sink != NULL){
4444a5b1c65SMatthias Ringwald         uint16_t playback_speed;
44510277393SMatthias Ringwald         if ((num_bis > 1) && pts_mode){
44610277393SMatthias Ringwald             playback_speed = sampling_frequency_hz / num_bis;
44710277393SMatthias Ringwald             printf("PTS workaround: playback at %u hz\n", playback_speed);
44810277393SMatthias Ringwald         } else {
44910277393SMatthias Ringwald             playback_speed = sampling_frequency_hz;
45010277393SMatthias Ringwald         };
45110277393SMatthias Ringwald         sink->init(num_bis, sampling_frequency_hz, le_audio_broadcast_sink_playback);
45210277393SMatthias Ringwald         sink->start_stream();
45310277393SMatthias Ringwald     }
45410277393SMatthias Ringwald 
4555c0d69beSMatthias Ringwald     big_sync_params.big_handle = big_handle;
4565c0d69beSMatthias Ringwald     big_sync_params.sync_handle = sync_handle;
457ac95ea81SMatthias Ringwald     big_sync_params.encryption = encryption;
458ac95ea81SMatthias Ringwald     if (encryption) {
459ac95ea81SMatthias Ringwald         memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16);
460ac95ea81SMatthias Ringwald     } else {
4615c0d69beSMatthias Ringwald         memset(big_sync_params.broadcast_code, 0, 16);
462ac95ea81SMatthias Ringwald     }
4635c0d69beSMatthias Ringwald     big_sync_params.mse = 0;
4645c0d69beSMatthias Ringwald     big_sync_params.big_sync_timeout_10ms = 100;
4655c0d69beSMatthias Ringwald     big_sync_params.num_bis = num_bis;
4665c0d69beSMatthias Ringwald     uint8_t i;
4675c0d69beSMatthias Ringwald     printf("BIG Create Sync for BIS: ");
4685c0d69beSMatthias Ringwald     for (i=0;i<num_bis;i++){
4695c0d69beSMatthias Ringwald         big_sync_params.bis_indices[i] = i + 1;
4705c0d69beSMatthias Ringwald         printf("%u ", big_sync_params.bis_indices[i]);
4715c0d69beSMatthias Ringwald     }
4725c0d69beSMatthias Ringwald     printf("\n");
4735c0d69beSMatthias Ringwald     app_state = APP_W4_BIG_SYNC_ESTABLISHED;
4745c0d69beSMatthias Ringwald     gap_big_sync_create(&big_sync_storage, &big_sync_params);
47510277393SMatthias Ringwald }
47610277393SMatthias Ringwald 
477111967cbSMatthias Ringwald static void start_scanning() {
478111967cbSMatthias Ringwald     app_state = APP_W4_BROADCAST_ADV;
479400a29ddSMatthias Ringwald     have_base = false;
480400a29ddSMatthias Ringwald     have_big_info = false;
481111967cbSMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 0);
482111967cbSMatthias Ringwald     gap_start_scan();
483111967cbSMatthias Ringwald     printf("Start scan..\n");
484111967cbSMatthias Ringwald }
485111967cbSMatthias Ringwald 
48681a67409SMatthias Ringwald static void setup_advertising() {
487efd5f6d0SMatthias Ringwald     gap_extended_advertising_setup(&le_advertising_set, &extended_params, &adv_handle);
488efd5f6d0SMatthias Ringwald     gap_extended_advertising_set_adv_data(adv_handle, sizeof(extended_adv_data), extended_adv_data);
489efd5f6d0SMatthias Ringwald     gap_extended_advertising_start(adv_handle, 0, 0);
49081a67409SMatthias Ringwald }
49181a67409SMatthias Ringwald 
492b3a520acSMatthias Ringwald static void got_base_and_big_info() {
493b3a520acSMatthias Ringwald     // add source
494b3a520acSMatthias Ringwald     if (standalone_mode) {
495b3a520acSMatthias Ringwald         printf("BASS: add Broadcast Source\n");
496b3a520acSMatthias Ringwald         // add source
497b3a520acSMatthias Ringwald         uint8_t source_index = 0;
498b3a520acSMatthias Ringwald         broadcast_audio_scan_service_server_add_source(bass_source_new, &source_index);
499b3a520acSMatthias Ringwald         broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
500b3a520acSMatthias Ringwald     }
501b3a520acSMatthias Ringwald 
502b3a520acSMatthias Ringwald     if ((encryption == false) || have_broadcast_code){
503b3a520acSMatthias Ringwald         enter_create_big_sync();
504b3a520acSMatthias Ringwald     } else {
505b3a520acSMatthias Ringwald         printf("BASS: request Broadcast Code\n");
506b3a520acSMatthias Ringwald         bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED;
507b3a520acSMatthias Ringwald         broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
508b3a520acSMatthias Ringwald         app_state = APP_W4_BROADCAST_CODE;
509b3a520acSMatthias Ringwald     }
510b3a520acSMatthias Ringwald }
511b3a520acSMatthias Ringwald 
51210277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
51310277393SMatthias Ringwald     UNUSED(channel);
51410277393SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
51510277393SMatthias Ringwald     switch (packet[0]) {
51610277393SMatthias Ringwald         case BTSTACK_EVENT_STATE:
51710277393SMatthias Ringwald             switch(btstack_event_state_get_state(packet)) {
51810277393SMatthias Ringwald                 case HCI_STATE_WORKING:
519400a29ddSMatthias Ringwald                     app_state = APP_IDLE;
5209a0d67ccSMatthias Ringwald #ifdef ENABLE_DEMO_MODE
521111967cbSMatthias Ringwald                     start_scanning();
5229a0d67ccSMatthias Ringwald #else
5239a0d67ccSMatthias Ringwald                     show_usage();
5249a0d67ccSMatthias Ringwald #endif
52510277393SMatthias Ringwald                     break;
52610277393SMatthias Ringwald                 case HCI_STATE_OFF:
52710277393SMatthias Ringwald                     printf("Goodbye\n");
52810277393SMatthias Ringwald                     exit(0);
52910277393SMatthias Ringwald                     break;
53010277393SMatthias Ringwald                 default:
53110277393SMatthias Ringwald                     break;
53210277393SMatthias Ringwald             }
53310277393SMatthias Ringwald             break;
53410277393SMatthias Ringwald         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
53510277393SMatthias Ringwald         {
53610277393SMatthias Ringwald             if (app_state != APP_W4_BROADCAST_ADV) break;
53710277393SMatthias Ringwald 
53810277393SMatthias Ringwald             gap_event_extended_advertising_report_get_address(packet, remote);
53910277393SMatthias Ringwald             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
54010277393SMatthias Ringwald             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
54110277393SMatthias Ringwald 
54210277393SMatthias Ringwald             ad_context_t context;
54310277393SMatthias Ringwald             bool found = false;
54410277393SMatthias Ringwald             remote_name[0] = '\0';
54510277393SMatthias Ringwald             uint16_t uuid;
54670718632SMatthias Ringwald             uint32_t broadcast_id;
54710277393SMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
54810277393SMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
54910277393SMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
55010277393SMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
55110277393SMatthias Ringwald                 switch (data_type){
55210277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
55310277393SMatthias Ringwald                         uuid = little_endian_read_16(data, 0);
55410277393SMatthias Ringwald                         if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){
55570718632SMatthias Ringwald                             broadcast_id = little_endian_read_24(data, 2);
55610277393SMatthias Ringwald                             found = true;
55710277393SMatthias Ringwald                         }
55810277393SMatthias Ringwald                         break;
55910277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
56010277393SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
56110277393SMatthias Ringwald                         size = btstack_min(sizeof(remote_name) - 1, size);
56210277393SMatthias Ringwald                         memcpy(remote_name, data, size);
56310277393SMatthias Ringwald                         remote_name[size] = 0;
5642fe0253dSMatthias Ringwald                         // support for nRF5340 Audio DK
5652fe0253dSMatthias Ringwald                         if (strncmp("NRF5340", remote_name, 7) == 0){
5662fe0253dSMatthias Ringwald                             nrf5340_audio_demo = true;
5672fe0253dSMatthias Ringwald                             found = true;
5682fe0253dSMatthias Ringwald                         }
56910277393SMatthias Ringwald                         break;
57010277393SMatthias Ringwald                     default:
57110277393SMatthias Ringwald                         break;
57210277393SMatthias Ringwald                 }
57310277393SMatthias Ringwald             }
57410277393SMatthias Ringwald             if (!found) break;
57510277393SMatthias Ringwald             remote_type = gap_event_extended_advertising_report_get_address_type(packet);
57610277393SMatthias Ringwald             remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
57710277393SMatthias Ringwald             pts_mode = strncmp("PTS-", remote_name, 4) == 0;
57810277393SMatthias Ringwald             count_mode = strncmp("COUNT", remote_name, 5) == 0;
57970718632SMatthias 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);
58010277393SMatthias Ringwald             // ignore other advertisements
58110277393SMatthias Ringwald             gap_whitelist_add(remote_type, remote);
58210277393SMatthias Ringwald             gap_set_scan_params(1, 0x30, 0x30, 1);
58310277393SMatthias Ringwald             // sync to PA
58410277393SMatthias Ringwald             gap_periodic_advertiser_list_clear();
58510277393SMatthias Ringwald             gap_periodic_advertiser_list_add(remote_type, remote, remote_sid);
58610277393SMatthias Ringwald             app_state = APP_W4_PA_AND_BIG_INFO;
58710277393SMatthias Ringwald             printf("Start Periodic Advertising Sync\n");
58810277393SMatthias Ringwald             gap_periodic_advertising_create_sync(0x01, remote_sid, remote_type, remote, 0, 1000, 0);
5897a236728SMatthias Ringwald 
5907a236728SMatthias Ringwald             // setup bass source info
5917a236728SMatthias Ringwald             bass_source_new.address_type = remote_type;
5927a236728SMatthias Ringwald             memcpy(bass_source_new.address, remote, 6);
5937a236728SMatthias Ringwald             bass_source_new.adv_sid = remote_sid;
5947a236728SMatthias Ringwald             bass_source_new.broadcast_id = broadcast_id;
5957a236728SMatthias Ringwald             bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
5967a236728SMatthias Ringwald             bass_source_new.pa_interval = gap_event_extended_advertising_report_get_periodic_advertising_interval(packet);
59710277393SMatthias Ringwald             break;
59810277393SMatthias Ringwald         }
59910277393SMatthias Ringwald 
60010277393SMatthias Ringwald         case HCI_EVENT_LE_META:
60110277393SMatthias Ringwald             switch(hci_event_le_meta_get_subevent_code(packet)) {
60292f80bb0SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECEIVED:
603b3a520acSMatthias Ringwald                     // PAST implies broadcast source has been added by client
604ffbedad9SMatthias Ringwald                     printf("Periodic advertising sync transfer received\n");
6052de3ad61SMatthias Ringwald                     btstack_run_loop_remove_timer(&broadcast_sink_pa_sync_timer);
606ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
60792f80bb0SMatthias Ringwald                     app_state = APP_W4_PA_AND_BIG_INFO;
60892f80bb0SMatthias Ringwald                     break;
60910277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
610400a29ddSMatthias Ringwald                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
611400a29ddSMatthias Ringwald                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
6127a236728SMatthias Ringwald                     app_state = APP_W4_PA_AND_BIG_INFO;
61310277393SMatthias Ringwald                     break;
61410277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
615ffbedad9SMatthias Ringwald                     if (app_state != APP_W4_PA_AND_BIG_INFO) break;
61610277393SMatthias Ringwald                     if (have_base) break;
61710277393SMatthias Ringwald                     handle_periodic_advertisement(packet, size);
618b3a520acSMatthias Ringwald                     if (have_base && have_big_info){
619b3a520acSMatthias Ringwald                         got_base_and_big_info();
62010277393SMatthias Ringwald                     }
62110277393SMatthias Ringwald                     break;
62210277393SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
623ffbedad9SMatthias Ringwald                     if (app_state != APP_W4_PA_AND_BIG_INFO) break;
62410277393SMatthias Ringwald                     if (have_big_info) break;
62510277393SMatthias Ringwald                     handle_big_info(packet, size);
626b3a520acSMatthias Ringwald                     if (have_base && have_big_info){
627b3a520acSMatthias Ringwald                         got_base_and_big_info();
62810277393SMatthias Ringwald                     }
62910277393SMatthias Ringwald                     break;
630111967cbSMatthias Ringwald                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
631111967cbSMatthias Ringwald                     printf("BIG Sync Lost\n");
632111967cbSMatthias Ringwald                     {
633111967cbSMatthias Ringwald                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
634111967cbSMatthias Ringwald                         if (sink != NULL) {
635111967cbSMatthias Ringwald                             sink->stop_stream();
636111967cbSMatthias Ringwald                             sink->close();
637111967cbSMatthias Ringwald                         }
638111967cbSMatthias Ringwald                     }
639111967cbSMatthias Ringwald                     // start over
640ffbedad9SMatthias Ringwald                     if (!standalone_mode) break;
641111967cbSMatthias Ringwald                     start_scanning();
642111967cbSMatthias Ringwald                     break;
64310277393SMatthias Ringwald                 default:
64410277393SMatthias Ringwald                     break;
64510277393SMatthias Ringwald             }
64610277393SMatthias Ringwald             break;
6475c0d69beSMatthias Ringwald         case HCI_EVENT_META_GAP:
6485c0d69beSMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)){
6495c0d69beSMatthias Ringwald                 case GAP_SUBEVENT_BIG_SYNC_CREATED: {
6505c0d69beSMatthias Ringwald                     printf("BIG Sync created with BIS Connection handles: ");
6515c0d69beSMatthias Ringwald                     uint8_t i;
65210277393SMatthias Ringwald                     for (i=0;i<num_bis;i++){
6535c0d69beSMatthias Ringwald                         bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i);
6545c0d69beSMatthias Ringwald                         printf("0x%04x ", bis_con_handles[i]);
65510277393SMatthias Ringwald                     }
6560707de65SMatthias Ringwald                     printf("\n");
65710277393SMatthias Ringwald                     app_state = APP_STREAMING;
6581716d43dSMatthias Ringwald                     last_packet_received_big = false;
65910277393SMatthias Ringwald                     last_samples_report_ms = btstack_run_loop_get_time_ms();
660151e8cb9SMatthias Ringwald                     memset(last_packet_sequence, 0, sizeof(last_packet_sequence));
661ae7e4c11SMatthias Ringwald                     memset(last_packet_received, 0, sizeof(last_packet_received));
6621716d43dSMatthias Ringwald                     memset(pcm, 0, sizeof(pcm));
663151e8cb9SMatthias Ringwald                     printf("Start receiving\n");
664ffbedad9SMatthias Ringwald 
665ffbedad9SMatthias Ringwald                     // update BIS Sync state
6660707de65SMatthias Ringwald                     bass_sources[0].data.subgroups[0].bis_sync_state = bis_sync_mask;
667ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
66810277393SMatthias Ringwald                     break;
6695c0d69beSMatthias Ringwald                 }
670e38308a2SMatthias Ringwald                 case GAP_SUBEVENT_BIG_SYNC_STOPPED:
671e38308a2SMatthias Ringwald                     printf("BIG Sync stopped, big_handle 0x%02x\n", gap_subevent_big_sync_stopped_get_big_handle(packet));
672823ca66cSMatthias Ringwald                     btstack_run_loop_remove_timer(&next_packet_timer);
673e38308a2SMatthias Ringwald                     break;
6745c0d69beSMatthias Ringwald                 default:
6755c0d69beSMatthias Ringwald                     break;
6765c0d69beSMatthias Ringwald             }
67710277393SMatthias Ringwald             break;
678efd5f6d0SMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
679efd5f6d0SMatthias Ringwald             printf("Just Works requested\n");
680efd5f6d0SMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
681efd5f6d0SMatthias Ringwald             break;
68210277393SMatthias Ringwald         default:
68310277393SMatthias Ringwald             break;
68410277393SMatthias Ringwald     }
68510277393SMatthias Ringwald }
68610277393SMatthias Ringwald 
687fc31f34cSMatthias Ringwald static void store_samples_in_ringbuffer(void){
68822d64f35SMatthias Ringwald     // check if we have all channels
68922d64f35SMatthias Ringwald     uint8_t bis_channel;
69022d64f35SMatthias Ringwald     for (bis_channel = 0; bis_channel < num_bis ; bis_channel++){
69122d64f35SMatthias Ringwald         if (have_pcm[bis_channel] == false) return;
69222d64f35SMatthias Ringwald     }
693fc31f34cSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
694fc31f34cSMatthias Ringwald     // write wav samples
695fc31f34cSMatthias Ringwald     wav_writer_write_int16(num_bis * number_samples_per_frame, pcm);
696fc31f34cSMatthias Ringwald #endif
697fc31f34cSMatthias Ringwald     // store samples in playback buffer
698fc31f34cSMatthias Ringwald     uint32_t bytes_to_store = num_bis * number_samples_per_frame * 2;
699fc31f34cSMatthias Ringwald     samples_received += number_samples_per_frame;
700fc31f34cSMatthias Ringwald     if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) {
701fc31f34cSMatthias Ringwald         btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm, bytes_to_store);
702fc31f34cSMatthias Ringwald     } else {
7037a5bad53SMatthias Ringwald         //printf("Samples dropped\n");
704fc31f34cSMatthias Ringwald         samples_dropped += number_samples_per_frame;
705fc31f34cSMatthias Ringwald     }
70622d64f35SMatthias Ringwald     // reset
70722d64f35SMatthias Ringwald     for (bis_channel = 0; bis_channel < num_bis ; bis_channel++){
70822d64f35SMatthias Ringwald         have_pcm[bis_channel] = false;
70922d64f35SMatthias Ringwald     }
71022d64f35SMatthias Ringwald }
71122d64f35SMatthias Ringwald 
712ba8a6634SMatthias Ringwald static void plc_do(uint8_t bis_channel) {// inject packet
713ba8a6634SMatthias Ringwald     uint8_t tmp_BEC_detect;
714ba8a6634SMatthias Ringwald     uint8_t BFI = 1;
715da364eecSMatthias Ringwald     (void) lc3_decoder->decode_signed_16(decoder_contexts[bis_channel], NULL, BFI,
716ba8a6634SMatthias Ringwald                                          &pcm[bis_channel], num_bis,
717ba8a6634SMatthias Ringwald                                          &tmp_BEC_detect);
718ba8a6634SMatthias Ringwald 
719ba8a6634SMatthias Ringwald     have_pcm[bis_channel] = true;
720ba8a6634SMatthias Ringwald     store_samples_in_ringbuffer();
721ba8a6634SMatthias Ringwald }
72222d64f35SMatthias Ringwald 
7237a5bad53SMatthias Ringwald //
7247a5bad53SMatthias Ringwald // Perform PLC for packets missing in previous intervals
7257a5bad53SMatthias Ringwald //
7267a5bad53SMatthias Ringwald // assumptions:
7277a5bad53SMatthias Ringwald // - packet sequence number is monotonic increasing
7287a5bad53SMatthias Ringwald // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed
7297a5bad53SMatthias Ringwald static void plc_check(uint16_t packet_sequence_number) {
7307a5bad53SMatthias Ringwald     while (last_packet_sequence_big != packet_sequence_number){
7311716d43dSMatthias Ringwald         uint8_t i;
7321716d43dSMatthias Ringwald         for (i=0;i<num_bis;i++){
7331716d43dSMatthias Ringwald             // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start
7341716d43dSMatthias Ringwald             if (last_packet_received[i] == false){
7357a5bad53SMatthias Ringwald                 printf_plc("- BIS #%u, very first packet missing\n", i);
7361716d43dSMatthias Ringwald                 have_pcm[i] = true;
7371716d43dSMatthias Ringwald                 store_samples_in_ringbuffer();
7381716d43dSMatthias Ringwald 
7391716d43dSMatthias Ringwald                 last_packet_received[i] = true;
7401716d43dSMatthias Ringwald                 last_packet_sequence[i] = last_packet_sequence_big;
7411716d43dSMatthias Ringwald                 continue;
7421716d43dSMatthias Ringwald             }
7431716d43dSMatthias Ringwald 
7441716d43dSMatthias Ringwald             // missing packet if big sequence counter is higher than bis sequence counter
7451716d43dSMatthias Ringwald             if (btstack_time16_delta(last_packet_sequence_big, last_packet_sequence[i]) > 0) {
7467a5bad53SMatthias Ringwald                 printf_plc("- BIS #%u, PLC for %u\n", i, last_packet_sequence_big);
7471716d43dSMatthias Ringwald                 plc_do(i);
7487a5bad53SMatthias Ringwald                 btstack_assert((last_packet_sequence[i] + 1) == last_packet_sequence_big);
7491716d43dSMatthias Ringwald                 last_packet_sequence[i] = last_packet_sequence_big;
7501716d43dSMatthias Ringwald             }
7511716d43dSMatthias Ringwald         }
7521716d43dSMatthias Ringwald         last_packet_sequence_big++;
7531716d43dSMatthias Ringwald     }
7541716d43dSMatthias Ringwald }
7551716d43dSMatthias Ringwald 
75622d64f35SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) {
757e38308a2SMatthias Ringwald     if (app_state != APP_STREAMING) return;
758e38308a2SMatthias Ringwald 
759151e8cb9SMatthias Ringwald     // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets
76022d64f35SMatthias Ringwald     uint32_t frame_duration_ms = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 8 : 10;
7611716d43dSMatthias Ringwald     btstack_run_loop_set_timer(timer, frame_duration_ms);
7621716d43dSMatthias Ringwald     btstack_run_loop_set_timer_handler(timer, plc_timeout);
7631716d43dSMatthias Ringwald     btstack_run_loop_add_timer(timer);
76422d64f35SMatthias Ringwald 
7651716d43dSMatthias Ringwald     // assume no packet received in iso interval
7667a5bad53SMatthias Ringwald     plc_check(last_packet_sequence_big + 1);
767fc31f34cSMatthias Ringwald }
768fc31f34cSMatthias Ringwald 
76910277393SMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
77010277393SMatthias Ringwald 
771e38308a2SMatthias Ringwald     if (app_state != APP_STREAMING) return;
772e38308a2SMatthias Ringwald 
77310277393SMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
77410277393SMatthias Ringwald     hci_con_handle_t con_handle = header & 0x0fff;
77510277393SMatthias Ringwald     uint8_t pb_flag = (header >> 12) & 3;
77610277393SMatthias Ringwald     uint8_t ts_flag = (header >> 14) & 1;
77710277393SMatthias Ringwald     uint16_t iso_load_len = little_endian_read_16(packet, 2);
77810277393SMatthias Ringwald 
77910277393SMatthias Ringwald     uint16_t offset = 4;
78010277393SMatthias Ringwald     uint32_t time_stamp = 0;
78110277393SMatthias Ringwald     if (ts_flag){
78210277393SMatthias Ringwald         uint32_t time_stamp = little_endian_read_32(packet, offset);
78310277393SMatthias Ringwald         offset += 4;
78410277393SMatthias Ringwald     }
78510277393SMatthias Ringwald 
78622d64f35SMatthias Ringwald     uint32_t receive_time_ms = btstack_run_loop_get_time_ms();
78722d64f35SMatthias Ringwald 
78810277393SMatthias Ringwald     uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
78910277393SMatthias Ringwald     offset += 2;
79010277393SMatthias Ringwald 
79110277393SMatthias Ringwald     uint16_t header_2 = little_endian_read_16(packet, offset);
79210277393SMatthias Ringwald     uint16_t iso_sdu_length = header_2 & 0x3fff;
79310277393SMatthias Ringwald     uint8_t packet_status_flag = (uint8_t) (header_2 >> 14);
79410277393SMatthias Ringwald     offset += 2;
79510277393SMatthias Ringwald 
79610277393SMatthias Ringwald     if (iso_sdu_length == 0) return;
79710277393SMatthias Ringwald 
79810277393SMatthias Ringwald     // infer channel from con handle - only works for up to 2 channels
79910277393SMatthias Ringwald     uint8_t bis_channel = (con_handle == bis_con_handles[0]) ? 0 : 1;
80010277393SMatthias Ringwald 
80110277393SMatthias Ringwald     if (count_mode){
80210277393SMatthias Ringwald         // check for missing packet
80310277393SMatthias Ringwald         uint16_t last_seq_no = last_packet_sequence[bis_channel];
80410277393SMatthias Ringwald         bool packet_missed = (last_seq_no != 0) && ((last_seq_no + 1) != packet_sequence_number);
80510277393SMatthias Ringwald         if (packet_missed){
80610277393SMatthias Ringwald             // print last packet
80710277393SMatthias Ringwald             printf("\n");
8084a5b1c65SMatthias Ringwald             printf("%04x %10"PRIu32" %u ", last_seq_no, last_packet_time_ms[bis_channel], bis_channel);
80910277393SMatthias Ringwald             printf_hexdump(&last_packet_prefix[num_bis*PACKET_PREFIX_LEN], PACKET_PREFIX_LEN);
81010277393SMatthias Ringwald             last_seq_no++;
81110277393SMatthias Ringwald 
81210277393SMatthias Ringwald             printf(ANSI_COLOR_RED);
81310277393SMatthias Ringwald             while (last_seq_no < packet_sequence_number){
81410277393SMatthias Ringwald                 printf("%04x            %u MISSING\n", last_seq_no, bis_channel);
81510277393SMatthias Ringwald                 last_seq_no++;
81610277393SMatthias Ringwald             }
81710277393SMatthias Ringwald             printf(ANSI_COLOR_RESET);
81810277393SMatthias Ringwald 
81910277393SMatthias Ringwald             // print current packet
8203daa4bc7SMatthias Ringwald             printf("%04x %10"PRIu32" %u ", packet_sequence_number, receive_time_ms, bis_channel);
82110277393SMatthias Ringwald             printf_hexdump(&packet[offset], PACKET_PREFIX_LEN);
82210277393SMatthias Ringwald         }
82310277393SMatthias Ringwald 
82410277393SMatthias Ringwald         // cache current packet
82510277393SMatthias Ringwald         memcpy(&last_packet_prefix[num_bis*PACKET_PREFIX_LEN], &packet[offset], PACKET_PREFIX_LEN);
82610277393SMatthias Ringwald 
82710277393SMatthias Ringwald     } else {
82810277393SMatthias Ringwald 
8297a5bad53SMatthias Ringwald 
830ae7e4c11SMatthias Ringwald         if (last_packet_received[bis_channel]) {
8317a5bad53SMatthias Ringwald 
8327a5bad53SMatthias Ringwald             printf_plc("BIS #%u, receive %u\n", bis_channel, packet_sequence_number);
8337a5bad53SMatthias Ringwald 
8341716d43dSMatthias Ringwald             int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number,
8351716d43dSMatthias Ringwald                                                                  last_packet_sequence[bis_channel]);
836151e8cb9SMatthias Ringwald             if (packet_sequence_delta < 1) {
837ae7e4c11SMatthias Ringwald                 // drop delayed packet that had already been generated by PLC
8387a5bad53SMatthias Ringwald                 printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n",
839151e8cb9SMatthias Ringwald                        packet_sequence_number, last_packet_sequence[bis_channel]);
840151e8cb9SMatthias Ringwald                 return;
841151e8cb9SMatthias Ringwald             }
8427a5bad53SMatthias Ringwald             // simple check
8437a5bad53SMatthias Ringwald             if (packet_sequence_number != last_packet_sequence[bis_channel] + 1) {
8447a5bad53SMatthias Ringwald                 printf_plc("- BIS #%u, missing %u\n", bis_channel, last_packet_sequence[bis_channel] + 1);
8457a5bad53SMatthias Ringwald             }
846ae7e4c11SMatthias Ringwald         } else {
8477a5bad53SMatthias Ringwald 
8487a5bad53SMatthias Ringwald             printf_plc("BIS %u, first packet seq number %u\n", bis_channel, packet_sequence_number);
8497a5bad53SMatthias Ringwald 
8501716d43dSMatthias Ringwald             if (!last_packet_received_big) {
8511716d43dSMatthias Ringwald                 // track sequence number of very first received packet
8521716d43dSMatthias Ringwald                 last_packet_received_big = true;
8531716d43dSMatthias Ringwald                 last_packet_sequence_big = packet_sequence_number;
8541716d43dSMatthias Ringwald             }
855ae7e4c11SMatthias Ringwald             last_packet_received[bis_channel] = true;
856151e8cb9SMatthias Ringwald         }
857151e8cb9SMatthias Ringwald 
8587a5bad53SMatthias Ringwald         plc_check(packet_sequence_number);
8591716d43dSMatthias Ringwald 
86010277393SMatthias Ringwald         // decode codec frame
86110277393SMatthias Ringwald         uint8_t tmp_BEC_detect;
86210277393SMatthias Ringwald         uint8_t BFI = 0;
863da364eecSMatthias Ringwald         (void) lc3_decoder->decode_signed_16(decoder_contexts[bis_channel], &packet[offset], BFI,
864c3e2434dSMatthias Ringwald                                    &pcm[bis_channel], num_bis,
86510277393SMatthias Ringwald                                    &tmp_BEC_detect);
86622d64f35SMatthias Ringwald         have_pcm[bis_channel] = true;
867fc31f34cSMatthias Ringwald         store_samples_in_ringbuffer();
86810277393SMatthias Ringwald 
86910277393SMatthias Ringwald         lc3_frames++;
87010277393SMatthias Ringwald         frames_per_second[bis_channel]++;
87110277393SMatthias Ringwald 
87222d64f35SMatthias Ringwald         // PLC
87322d64f35SMatthias Ringwald         cached_iso_sdu_len = iso_sdu_length;
87422d64f35SMatthias Ringwald         uint32_t frame_duration_ms = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 8 : 10;
8751716d43dSMatthias Ringwald         uint32_t timeout_ms = frame_duration_ms * 5 / 2;
8761716d43dSMatthias Ringwald         btstack_run_loop_remove_timer(&next_packet_timer);
8771716d43dSMatthias Ringwald         btstack_run_loop_set_timer(&next_packet_timer, timeout_ms);
8781716d43dSMatthias Ringwald         btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout);
8791716d43dSMatthias Ringwald         btstack_run_loop_add_timer(&next_packet_timer);
88022d64f35SMatthias Ringwald 
88110277393SMatthias Ringwald         uint32_t time_ms = btstack_run_loop_get_time_ms();
8822fe0253dSMatthias Ringwald         if (btstack_time_delta(time_ms, last_samples_report_ms) >= 1000){
88310277393SMatthias Ringwald             last_samples_report_ms = time_ms;
8844a5b1c65SMatthias Ringwald             printf("LC3 Frames: %4u - ", (int) (lc3_frames / num_bis));
88510277393SMatthias Ringwald             uint8_t i;
88610277393SMatthias Ringwald             for (i=0;i<num_bis;i++){
88710277393SMatthias Ringwald                 printf("%u ", frames_per_second[i]);
88810277393SMatthias Ringwald                 frames_per_second[i] = 0;
88910277393SMatthias Ringwald             }
89010277393SMatthias Ringwald             printf(" frames per second, dropped %u of %u\n", samples_dropped, samples_received);
89110277393SMatthias Ringwald             samples_received = 0;
89210277393SMatthias Ringwald             samples_dropped  =  0;
89310277393SMatthias Ringwald         }
8943daa4bc7SMatthias Ringwald     }
89510277393SMatthias Ringwald 
8963daa4bc7SMatthias Ringwald     last_packet_time_ms[bis_channel]  = receive_time_ms;
8973daa4bc7SMatthias Ringwald     last_packet_sequence[bis_channel] = packet_sequence_number;
89810277393SMatthias Ringwald }
89910277393SMatthias Ringwald 
90010277393SMatthias Ringwald static void show_usage(void){
90110277393SMatthias Ringwald     printf("\n--- LE Audio Broadcast Sink Test Console ---\n");
9029a0d67ccSMatthias Ringwald     printf("s - start scanning\n");
9039a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
9049a0d67ccSMatthias Ringwald     printf("q - use LC3plus decoder if 10 ms ISO interval is used\n");
9059a0d67ccSMatthias Ringwald #endif
9060707de65SMatthias Ringwald     printf("e - set broadcast code to 0x11111111111111111111111111111111\n");
907400a29ddSMatthias Ringwald     printf("t - terminate BIS streams\n");
90810277393SMatthias Ringwald     printf("---\n");
90910277393SMatthias Ringwald }
91010277393SMatthias Ringwald 
91110277393SMatthias Ringwald static void stdin_process(char c){
91210277393SMatthias Ringwald     switch (c){
9139a0d67ccSMatthias Ringwald         case 's':
914400a29ddSMatthias Ringwald             if (app_state != APP_IDLE) break;
915ffbedad9SMatthias Ringwald             standalone_mode = true;
9169a0d67ccSMatthias Ringwald             start_scanning();
9179a0d67ccSMatthias Ringwald             break;
9180707de65SMatthias Ringwald         case 'e':
9190707de65SMatthias Ringwald             printf("Set broadcast code to 0x11111111111111111111111111111111\n");
9200707de65SMatthias Ringwald             memset(broadcast_code, 0x11, 16);
9210707de65SMatthias Ringwald             have_broadcast_code = true;
9220707de65SMatthias Ringwald             break;
9239a0d67ccSMatthias Ringwald #ifdef HAVE_LC3PLUS
9249a0d67ccSMatthias Ringwald         case 'q':
925151e8cb9SMatthias Ringwald             printf("Use LC3plus decoder for 10 ms ISO interval...\n");
9269a0d67ccSMatthias Ringwald             request_lc3plus_decoder = true;
9279a0d67ccSMatthias Ringwald             break;
9289a0d67ccSMatthias Ringwald #endif
929400a29ddSMatthias Ringwald         case 't':
930400a29ddSMatthias Ringwald             switch (app_state){
931400a29ddSMatthias Ringwald                 case APP_STREAMING:
932400a29ddSMatthias Ringwald                 case APP_W4_BIG_SYNC_ESTABLISHED:
933400a29ddSMatthias Ringwald                     app_state = APP_IDLE;
934823ca66cSMatthias Ringwald                     btstack_run_loop_remove_timer(&next_packet_timer);
935400a29ddSMatthias Ringwald                     close_files();
936400a29ddSMatthias Ringwald                     printf("Terminate BIG SYNC\n");
937400a29ddSMatthias Ringwald                     gap_big_sync_terminate(big_handle);
938400a29ddSMatthias Ringwald                     break;
939400a29ddSMatthias Ringwald                 default:
940400a29ddSMatthias Ringwald                     break;
941400a29ddSMatthias Ringwald             }
942400a29ddSMatthias Ringwald             break;
94310277393SMatthias Ringwald         case '\n':
94410277393SMatthias Ringwald         case '\r':
94510277393SMatthias Ringwald             break;
94610277393SMatthias Ringwald         default:
94710277393SMatthias Ringwald             show_usage();
94810277393SMatthias Ringwald             break;
94910277393SMatthias Ringwald 
95010277393SMatthias Ringwald     }
95110277393SMatthias Ringwald }
9522de3ad61SMatthias Ringwald 
9532de3ad61SMatthias Ringwald static void broadcast_sync_pa_sync_timeout_handler(btstack_timer_source_t * ts){
9542de3ad61SMatthias Ringwald     UNUSED(ts);
9552de3ad61SMatthias Ringwald     printf("PAST Timeout -> LE_AUDIO_PA_SYNC_STATE_NO_PAST\n");
9562de3ad61SMatthias Ringwald     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST);
9572de3ad61SMatthias Ringwald }
9582de3ad61SMatthias Ringwald 
95960d70dccSMatthias Ringwald static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
96060d70dccSMatthias Ringwald     UNUSED(packet_type);
96160d70dccSMatthias Ringwald     UNUSED(channel);
962ffbedad9SMatthias Ringwald     btstack_assert (packet_type == HCI_EVENT_PACKET);
963ffbedad9SMatthias Ringwald     btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META);
964ffbedad9SMatthias Ringwald     printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet));
965ffbedad9SMatthias Ringwald     printf_hexdump(packet, size);
966ffbedad9SMatthias Ringwald     switch (hci_event_gattservice_meta_get_subevent_code(packet)){
967*bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED:
968ffbedad9SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n",
969*bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_source_id(packet),
970*bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_pa_sync(packet));
971*bebf6b57SMatthias Ringwald             switch (gattservice_subevent_bass_server_source_added_get_pa_sync(packet)){
9722de3ad61SMatthias Ringwald                 case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE:
9732de3ad61SMatthias Ringwald                     printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n");
974ffbedad9SMatthias Ringwald                     broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST);
9752de3ad61SMatthias Ringwald                     // start timeout
9762de3ad61SMatthias Ringwald                     btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler);
9772de3ad61SMatthias Ringwald                     btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000);   // 10 seconds
9782de3ad61SMatthias Ringwald                     btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer);
9792de3ad61SMatthias Ringwald                     break;
9802de3ad61SMatthias Ringwald             }
981ffbedad9SMatthias Ringwald             break;
982*bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED:
983ffbedad9SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n",
984*bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_source_id(packet),
985*bebf6b57SMatthias Ringwald                    gattservice_subevent_bass_server_source_added_get_pa_sync(packet));
986ffbedad9SMatthias Ringwald             // handle 'bis sync == 0'
987ffbedad9SMatthias Ringwald             printf("PA Sync %u, bis_sync[0] = %u\n", bass_sources[0].data.pa_sync, bass_sources[0].data.subgroups[0].bis_sync);
988ffbedad9SMatthias Ringwald             if (bass_sources[0].data.subgroups[0].bis_sync == 0){
989ffbedad9SMatthias Ringwald                 printf("Simulate BIS Sync has stopped\n");
990ffbedad9SMatthias Ringwald                 bass_sources[0].data.subgroups[0].bis_sync_state = 0;
991ffbedad9SMatthias Ringwald                 broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA);
992ffbedad9SMatthias Ringwald             }
9930707de65SMatthias Ringwald             break;
994*bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE:
995*bebf6b57SMatthias Ringwald             gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code);
9960707de65SMatthias Ringwald             printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: ");
9970707de65SMatthias Ringwald             printf_hexdump(broadcast_code, 16);
9980707de65SMatthias Ringwald             bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING;
9990707de65SMatthias Ringwald             if (have_base && have_big_info){
10000707de65SMatthias Ringwald                 enter_create_big_sync();
10010707de65SMatthias Ringwald             }
10020707de65SMatthias Ringwald             break;
1003ffbedad9SMatthias Ringwald         default:
1004ffbedad9SMatthias Ringwald             break;
1005ffbedad9SMatthias Ringwald     }
100660d70dccSMatthias Ringwald }
100710277393SMatthias Ringwald 
100810277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
100910277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
101010277393SMatthias Ringwald     (void) argv;
101110277393SMatthias Ringwald     (void) argc;
101210277393SMatthias Ringwald 
1013efd5f6d0SMatthias Ringwald     l2cap_init();
1014efd5f6d0SMatthias Ringwald     sm_init();
1015efd5f6d0SMatthias Ringwald 
1016efd5f6d0SMatthias Ringwald     // setup ATT server
1017efd5f6d0SMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
1018efd5f6d0SMatthias Ringwald 
101910277393SMatthias Ringwald     // register for HCI events
102010277393SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
102110277393SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
102210277393SMatthias Ringwald 
102310277393SMatthias Ringwald     // register for ISO Packet
102410277393SMatthias Ringwald     hci_register_iso_packet_handler(&iso_packet_handler);
102510277393SMatthias Ringwald 
1026efd5f6d0SMatthias Ringwald     // register for SM events
1027efd5f6d0SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
1028efd5f6d0SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
1029efd5f6d0SMatthias Ringwald 
103060d70dccSMatthias Ringwald     // setup BASS Server
103160d70dccSMatthias Ringwald     broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients);
103260d70dccSMatthias Ringwald     broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler);
103360d70dccSMatthias Ringwald 
103433bea8deSMatthias Ringwald     // setup advertising and allow to receive periodic advertising sync trasnfers
103533bea8deSMatthias Ringwald     setup_advertising();
103633bea8deSMatthias Ringwald     gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0);
103733bea8deSMatthias Ringwald 
103810277393SMatthias Ringwald     // turn on!
103910277393SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
104010277393SMatthias Ringwald 
104110277393SMatthias Ringwald     btstack_stdin_setup(stdin_process);
104210277393SMatthias Ringwald     return 0;
104310277393SMatthias Ringwald }
1044