xref: /btstack/test/le_audio/le_audio_broadcast_assistant.c (revision 28ef06ceb7a4439c64631de03859fda899e529c5)
1d15b3374SMatthias Ringwald /*
2d15b3374SMatthias Ringwald  * Copyright (C) 2022 BlueKitchen GmbH
3d15b3374SMatthias Ringwald  *
4d15b3374SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5d15b3374SMatthias Ringwald  * modification, are permitted provided that the following conditions
6d15b3374SMatthias Ringwald  * are met:
7d15b3374SMatthias Ringwald  *
8d15b3374SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9d15b3374SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10d15b3374SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11d15b3374SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12d15b3374SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13d15b3374SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14d15b3374SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15d15b3374SMatthias Ringwald  *    from this software without specific prior written permission.
16d15b3374SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17d15b3374SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18d15b3374SMatthias Ringwald  *    monetary gain.
19d15b3374SMatthias Ringwald  *
20d15b3374SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21d15b3374SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22d15b3374SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23d15b3374SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24d15b3374SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25d15b3374SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26d15b3374SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27d15b3374SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28d15b3374SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29d15b3374SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30d15b3374SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31d15b3374SMatthias Ringwald  * SUCH DAMAGE.
32d15b3374SMatthias Ringwald  *
33d15b3374SMatthias Ringwald  * Please inquire about commercial licensing options at
34d15b3374SMatthias Ringwald  * [email protected]
35d15b3374SMatthias Ringwald  *
36d15b3374SMatthias Ringwald  */
37d15b3374SMatthias Ringwald 
38d15b3374SMatthias Ringwald #define BTSTACK_FILE__ "le_audio_broadcast_assistant.c"
39d15b3374SMatthias Ringwald 
40d15b3374SMatthias Ringwald /*
41d15b3374SMatthias Ringwald  * LE Audio Broadcast Assistant
42d15b3374SMatthias Ringwald  */
43d15b3374SMatthias Ringwald 
44d15b3374SMatthias Ringwald 
45d15b3374SMatthias Ringwald #include "btstack_config.h"
46d15b3374SMatthias Ringwald 
47d15b3374SMatthias Ringwald #include <stdint.h>
48d15b3374SMatthias Ringwald #include <stdio.h>
49d15b3374SMatthias Ringwald #include <stdlib.h>
50d15b3374SMatthias Ringwald #include <string.h>
51d15b3374SMatthias Ringwald #include <inttypes.h>
52d15b3374SMatthias Ringwald 
53d15b3374SMatthias Ringwald #include "ad_parser.h"
5432089ce4SMatthias Ringwald #include "ble/sm.h"
55d15b3374SMatthias Ringwald #include "bluetooth_data_types.h"
56d15b3374SMatthias Ringwald #include "bluetooth_gatt.h"
57d15b3374SMatthias Ringwald #include "btstack_audio.h"
58a22ed910SMatthias Ringwald #include "btstack_debug.h"
59d15b3374SMatthias Ringwald #include "btstack_event.h"
60d15b3374SMatthias Ringwald #include "btstack_lc3.h"
61d15b3374SMatthias Ringwald #include "btstack_run_loop.h"
62d15b3374SMatthias Ringwald #include "btstack_stdin.h"
63d15b3374SMatthias Ringwald #include "btstack_util.h"
64d15b3374SMatthias Ringwald #include "gap.h"
65d15b3374SMatthias Ringwald #include "hci.h"
6632089ce4SMatthias Ringwald #include "l2cap.h"
675deb0bb6SMatthias Ringwald #include "le-audio/gatt-service/broadcast_audio_scan_service_client.h"
68ff59851cSMilanka Ringwald #include "le-audio/le_audio_util.h"
69be81b159SMatthias Ringwald #include "le-audio/le_audio_base_parser.h"
70d15b3374SMatthias Ringwald 
71d15b3374SMatthias Ringwald static void show_usage(void);
72d15b3374SMatthias Ringwald 
73d15b3374SMatthias Ringwald static enum {
74d15b3374SMatthias Ringwald     APP_W4_WORKING,
75d15b3374SMatthias Ringwald     APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV,
76d15b3374SMatthias Ringwald     APP_W4_PA_AND_BIG_INFO,
77d15b3374SMatthias Ringwald     APP_W4_BIG_SYNC_ESTABLISHED,
78d15b3374SMatthias Ringwald     APP_W4_SCAN_DELEGATOR_CONNECTION,
79d15b3374SMatthias Ringwald     APP_IDLE
80d15b3374SMatthias Ringwald } app_state = APP_W4_WORKING;
81d15b3374SMatthias Ringwald 
82d15b3374SMatthias Ringwald //
83d15b3374SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
8432089ce4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
85d15b3374SMatthias Ringwald 
86d15b3374SMatthias Ringwald static bool have_scan_delegator;
87d15b3374SMatthias Ringwald static bool have_broadcast_source;
88d15b3374SMatthias Ringwald static bool have_base;
89d15b3374SMatthias Ringwald static bool have_big_info;
90c3d56f70SMatthias Ringwald static bool manual_mode;
91d15b3374SMatthias Ringwald 
92d15b3374SMatthias Ringwald // broadcast sink info
93d15b3374SMatthias Ringwald static char broadcast_source_name[20];
94d15b3374SMatthias Ringwald static bd_addr_t broadcast_source;
95d15b3374SMatthias Ringwald static bd_addr_type_t broadcast_source_type;
96d15b3374SMatthias Ringwald static uint8_t broadcast_source_sid;
97d15b3374SMatthias Ringwald static uint32_t broadcast_id;
98d15b3374SMatthias Ringwald static uint16_t broadcast_source_pa_interval;
99d15b3374SMatthias Ringwald 
100d15b3374SMatthias Ringwald // broadcast info
101d15b3374SMatthias Ringwald static hci_con_handle_t sync_handle;
102d15b3374SMatthias Ringwald static uint8_t          encryption;
103166aee19SMatthias Ringwald static uint8_t          broadcast_code [] = {0x01, 0x02, 0x68, 0x05, 0x53, 0xF1, 0x41, 0x5A, 0xA2, 0x65, 0xBB, 0xAF, 0xC6, 0xEA, 0x03, 0xB8, };
104d15b3374SMatthias Ringwald static uint8_t          num_bis;
1056b11d590SMatthias Ringwald static uint32_t         bis_sync_mask;
106d15b3374SMatthias Ringwald static uint32_t         sampling_frequency_hz;
107d15b3374SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration;
108d15b3374SMatthias Ringwald static uint16_t octets_per_frame;
109d15b3374SMatthias Ringwald 
110d15b3374SMatthias Ringwald // scan delegator info
111d15b3374SMatthias Ringwald static char scan_delegator_name[20];
112d15b3374SMatthias Ringwald static bd_addr_t scan_delegator;
113d15b3374SMatthias Ringwald static bd_addr_type_t scan_delegator_type;
114d15b3374SMatthias Ringwald static hci_con_handle_t scan_delegator_handle;
115d15b3374SMatthias Ringwald 
116d15b3374SMatthias Ringwald // BASS
117d15b3374SMatthias Ringwald #define BASS_CLIENT_NUM_SOURCES 1
118d15b3374SMatthias Ringwald static bass_client_connection_t bass_connection;
119d15b3374SMatthias Ringwald static bass_client_source_t bass_sources[BASS_CLIENT_NUM_SOURCES];
120d0b1d302SMatthias Ringwald static bass_source_data_t bass_source_data;
121d15b3374SMatthias Ringwald static uint16_t bass_cid;
1226216cd2aSMatthias Ringwald static uint8_t  bass_source_id;
123d15b3374SMatthias Ringwald 
12489d964feSMatthias Ringwald // test device name
12589d964feSMatthias Ringwald static const char test_device_name[] = "OnePlus Buds Pro 2";
12689d964feSMatthias Ringwald 
127d15b3374SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
128d15b3374SMatthias Ringwald 
129d15b3374SMatthias Ringwald     // periodic advertisement contains the BASE
130d15b3374SMatthias Ringwald     // TODO: BASE might be split across multiple advertisements
131d15b3374SMatthias Ringwald     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
132d15b3374SMatthias Ringwald     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
133d15b3374SMatthias Ringwald     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
134d15b3374SMatthias Ringwald 
135d15b3374SMatthias Ringwald     if (adv_status != 0) {
136d15b3374SMatthias Ringwald         printf("Periodic Advertisement (status %u): ", adv_status);
137d15b3374SMatthias Ringwald         printf_hexdump(adv_data, adv_size);
138d15b3374SMatthias Ringwald         return;
139d15b3374SMatthias Ringwald     }
140d15b3374SMatthias Ringwald 
141be81b159SMatthias Ringwald     le_audio_base_parser_t parser;
142be81b159SMatthias Ringwald     bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size);
143be81b159SMatthias Ringwald     if (ok == false){
144be81b159SMatthias Ringwald         return;
145be81b159SMatthias Ringwald     }
146d15b3374SMatthias Ringwald     have_base = true;
147be81b159SMatthias Ringwald 
148d15b3374SMatthias Ringwald     printf("BASE:\n");
149be81b159SMatthias Ringwald     uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser);
150d15b3374SMatthias Ringwald     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
151be81b159SMatthias Ringwald     uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser);
152d15b3374SMatthias Ringwald     // Cache in new source struct
153d0b1d302SMatthias Ringwald     bass_source_data.subgroups_num = num_subgroups;
154d15b3374SMatthias Ringwald     printf("- num subgroups: %u\n", num_subgroups);
155d15b3374SMatthias Ringwald     uint8_t i;
156d15b3374SMatthias Ringwald     for (i=0;i<num_subgroups;i++){
157d15b3374SMatthias Ringwald 
158d15b3374SMatthias Ringwald         // Cache in new source struct
159d0b1d302SMatthias Ringwald         bass_source_data.subgroups[i].bis_sync = 0;
160d15b3374SMatthias Ringwald 
161d15b3374SMatthias Ringwald         // Level 2: Subgroup Level
162be81b159SMatthias Ringwald         num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser);
163d15b3374SMatthias Ringwald         printf("  - num bis[%u]: %u\n", i, num_bis);
164be81b159SMatthias Ringwald         uint8_t codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
165be81b159SMatthias Ringwald         const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser);
166d15b3374SMatthias Ringwald         printf("  - codec specific config[%u]: ", i);
167d15b3374SMatthias Ringwald         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
168d15b3374SMatthias Ringwald         // parse config to get sampling frequency and frame duration
169d15b3374SMatthias Ringwald         uint8_t codec_offset = 0;
170d15b3374SMatthias Ringwald         while ((codec_offset + 1) < codec_specific_configuration_length){
171d15b3374SMatthias Ringwald             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
172d15b3374SMatthias Ringwald             uint8_t ltv_type = codec_specific_configuration[codec_offset];
173d15b3374SMatthias Ringwald             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
174d15b3374SMatthias Ringwald             uint8_t sampling_frequency_index;
175d15b3374SMatthias Ringwald             uint8_t frame_duration_index;
176d15b3374SMatthias Ringwald             switch (ltv_type){
177d15b3374SMatthias Ringwald                 case 0x01: // sampling frequency
178d15b3374SMatthias Ringwald                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
179d15b3374SMatthias Ringwald                     // TODO: check range
180d15b3374SMatthias Ringwald                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
181d15b3374SMatthias Ringwald                     printf("    - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
182d15b3374SMatthias Ringwald                     break;
183d15b3374SMatthias Ringwald                 case 0x02: // 0 = 7.5, 1 = 10 ms
184d15b3374SMatthias Ringwald                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
185ff59851cSMilanka Ringwald                     frame_duration = le_audio_util_get_btstack_lc3_frame_duration(frame_duration_index);
186ff59851cSMilanka Ringwald                     printf("    - frame duration[%u]: %u us\n", i, le_audio_get_frame_duration_us(frame_duration_index));
187d15b3374SMatthias Ringwald                     break;
188d15b3374SMatthias Ringwald                 case 0x04:  // octets per coding frame
189d15b3374SMatthias Ringwald                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
190d15b3374SMatthias Ringwald                     printf("    - octets per codec frame[%u]: %u\n", i, octets_per_frame);
191d15b3374SMatthias Ringwald                     break;
192d15b3374SMatthias Ringwald                 default:
193d15b3374SMatthias Ringwald                     break;
194d15b3374SMatthias Ringwald             }
195d15b3374SMatthias Ringwald             codec_offset += ltv_len;
196d15b3374SMatthias Ringwald         }
197be81b159SMatthias Ringwald         uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser);
198be81b159SMatthias Ringwald         const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser);
199d15b3374SMatthias Ringwald         printf("  - meta data[%u]: ", i);
200d15b3374SMatthias Ringwald         printf_hexdump(meta_data, metadata_length);
201d15b3374SMatthias Ringwald         uint8_t k;
202d15b3374SMatthias Ringwald         for (k=0;k<num_bis;k++){
203d15b3374SMatthias Ringwald             // Level 3: BIS Level
204be81b159SMatthias Ringwald             uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser);
205d15b3374SMatthias Ringwald 
2066216cd2aSMatthias Ringwald             // check value
2076216cd2aSMatthias Ringwald             // double check message
2086216cd2aSMatthias Ringwald 
209d15b3374SMatthias Ringwald             // Cache in new source struct
2106b11d590SMatthias Ringwald             bis_sync_mask |= 1 << (bis_index-1);
2116b11d590SMatthias Ringwald 
212d0b1d302SMatthias Ringwald             bass_source_data.subgroups[i].metadata_length = 0;
213d0b1d302SMatthias Ringwald             memset(&bass_source_data.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
214d15b3374SMatthias Ringwald 
215d15b3374SMatthias Ringwald             printf("    - bis index[%u][%u]: %u\n", i, k, bis_index);
216be81b159SMatthias Ringwald             codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
217be81b159SMatthias Ringwald             codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser);
218d15b3374SMatthias Ringwald             printf("    - codec specific config[%u][%u]: ", i, k);
219be81b159SMatthias Ringwald             printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
220be81b159SMatthias Ringwald 
221be81b159SMatthias Ringwald             // parse next BIS
222be81b159SMatthias Ringwald             le_audio_base_parser_bis_next(&parser);
223d15b3374SMatthias Ringwald         }
224be81b159SMatthias Ringwald 
225be81b159SMatthias Ringwald         // parse next subgroup
226be81b159SMatthias Ringwald         le_audio_base_parser_subgroup_next(&parser);
227d15b3374SMatthias Ringwald     }
228d15b3374SMatthias Ringwald }
229d15b3374SMatthias Ringwald 
230d15b3374SMatthias Ringwald static void start_scanning() {
231d15b3374SMatthias Ringwald     app_state = APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV;
232d15b3374SMatthias Ringwald     have_base = false;
233d15b3374SMatthias Ringwald     have_big_info = false;
234d15b3374SMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 0);
235d15b3374SMatthias Ringwald     gap_start_scan();
236d15b3374SMatthias Ringwald     printf("Start scan..\n");
237d15b3374SMatthias Ringwald }
238d15b3374SMatthias Ringwald 
239d15b3374SMatthias Ringwald static void have_base_and_big_info(void){
24032089ce4SMatthias Ringwald     printf("Connecting to Scan Delegator/Sink!\n");
241d15b3374SMatthias Ringwald 
242*28ef06ceSMatthias Ringwald     // stop scanning
243*28ef06ceSMatthias Ringwald     gap_stop_scan();
244*28ef06ceSMatthias Ringwald 
245d15b3374SMatthias Ringwald     // todo: connect to scan delegator
246d15b3374SMatthias Ringwald     app_state = APP_W4_SCAN_DELEGATOR_CONNECTION;
247d15b3374SMatthias Ringwald     gap_connect(scan_delegator, scan_delegator_type);
248d15b3374SMatthias Ringwald }
249d15b3374SMatthias Ringwald 
250d15b3374SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){
251d15b3374SMatthias Ringwald     printf("BIG Info advertising report\n");
252d15b3374SMatthias Ringwald     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
253d15b3374SMatthias Ringwald     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
254d15b3374SMatthias Ringwald     if (encryption) {
255d15b3374SMatthias Ringwald         printf("Stream is encrypted\n");
256d15b3374SMatthias Ringwald     }
257d15b3374SMatthias Ringwald     have_big_info = true;
258d15b3374SMatthias Ringwald }
259c3d56f70SMatthias Ringwald 
260c3d56f70SMatthias Ringwald static void add_source() {// setup bass source info
261d0b1d302SMatthias Ringwald     printf("BASS Client: add source with BIS Sync 0x%04x\n", bass_source_data.subgroups[0].bis_sync_state);
262d0b1d302SMatthias Ringwald     bass_source_data.address_type = broadcast_source_type;
263d0b1d302SMatthias Ringwald     memcpy(bass_source_data.address, broadcast_source, 6);
264d0b1d302SMatthias Ringwald     bass_source_data.adv_sid = broadcast_source_sid;
265d0b1d302SMatthias Ringwald     bass_source_data.broadcast_id = broadcast_id;
266d0b1d302SMatthias Ringwald     bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
267d0b1d302SMatthias Ringwald     bass_source_data.pa_interval = broadcast_source_pa_interval;
268c3d56f70SMatthias Ringwald     // bass_source_new.subgroups_num set in BASE parser
269c3d56f70SMatthias Ringwald     // bass_source_new.subgroup[i].* set in BASE parser
270c3d56f70SMatthias Ringwald 
271c3d56f70SMatthias Ringwald     // add bass source
272d0b1d302SMatthias Ringwald     broadcast_audio_scan_service_client_add_source(bass_cid, &bass_source_data);
273c3d56f70SMatthias Ringwald }
274c3d56f70SMatthias Ringwald 
275d15b3374SMatthias Ringwald static void bass_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
276d15b3374SMatthias Ringwald     UNUSED(channel);
277d15b3374SMatthias Ringwald     UNUSED(size);
278d15b3374SMatthias Ringwald 
279d15b3374SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
280d15b3374SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) return;
281d15b3374SMatthias Ringwald 
282e8a39bfbSMatthias Ringwald     const bass_source_data_t * source_data;
283e8a39bfbSMatthias Ringwald 
284d15b3374SMatthias Ringwald     switch (hci_event_gattservice_meta_get_subevent_code(packet)) {
285bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_CONNECTED:
286bebf6b57SMatthias Ringwald             if (gattservice_subevent_bass_client_connected_get_status(packet) != ERROR_CODE_SUCCESS){
287d15b3374SMatthias Ringwald                 printf("BASS client connection failed, cid 0x%02x, con_handle 0x%02x, status 0x%02x\n",
288d15b3374SMatthias Ringwald                        bass_cid, scan_delegator_handle,
289bebf6b57SMatthias Ringwald                        gattservice_subevent_bass_client_connected_get_status(packet));
290d15b3374SMatthias Ringwald                 return;
291d15b3374SMatthias Ringwald             }
292d15b3374SMatthias Ringwald             printf("BASS client connected, cid 0x%02x\n", bass_cid);
293d15b3374SMatthias Ringwald 
29437d87e7fSMatthias Ringwald             if ((have_big_info == false) || (have_base == false)) break;
295c3d56f70SMatthias Ringwald             if (manual_mode) return;
29637d87e7fSMatthias Ringwald 
297d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
298d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
2996b11d590SMatthias Ringwald             add_source();
300d15b3374SMatthias Ringwald             break;
301bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_SOURCE_OPERATION_COMPLETE:
302bebf6b57SMatthias Ringwald             if (gattservice_subevent_bass_client_source_operation_complete_get_status(packet) != ERROR_CODE_SUCCESS){
303bebf6b57SMatthias Ringwald                 printf("BASS client source operation failed, status 0x%02x\n", gattservice_subevent_bass_client_source_operation_complete_get_status(packet));
304d15b3374SMatthias Ringwald                 return;
305d15b3374SMatthias Ringwald             }
306d15b3374SMatthias Ringwald 
307bebf6b57SMatthias Ringwald             if ( gattservice_subevent_bass_client_source_operation_complete_get_opcode(packet) == (uint8_t)BASS_OPCODE_ADD_SOURCE ){
3086216cd2aSMatthias Ringwald                 // TODO: set state to 'wait for source_id"
3096216cd2aSMatthias Ringwald                 printf("BASS client add source operation completed, wait for source_id\n");
3106216cd2aSMatthias Ringwald             }
3116216cd2aSMatthias Ringwald             break;
312bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_NOTIFICATION_COMPLETE:
3136216cd2aSMatthias Ringwald             // store source_id
314bebf6b57SMatthias Ringwald             bass_source_id = gattservice_subevent_bass_client_notification_complete_get_source_id(packet);
3156216cd2aSMatthias Ringwald             printf("BASS client notification, source_id = 0x%02x\n", bass_source_id);
316a22ed910SMatthias Ringwald             source_data = broadcast_audio_scan_service_client_get_source_data(bass_cid, bass_source_id);
317a22ed910SMatthias Ringwald             btstack_assert(source_data != NULL);
318d15b3374SMatthias Ringwald 
319e8a39bfbSMatthias Ringwald             switch (source_data->pa_sync_state){
320e8a39bfbSMatthias Ringwald                 case LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST:
321e8a39bfbSMatthias Ringwald                     // start pa sync transfer
3226216cd2aSMatthias Ringwald                     printf("LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST -> Start PAST\n");
323e8a39bfbSMatthias Ringwald                      // TODO: unclear why this needs to be shifted for PAST with TS to get test green
324e8a39bfbSMatthias Ringwald                     uint16_t service_data = bass_source_id << 8;
325d15b3374SMatthias Ringwald                     gap_periodic_advertising_sync_transfer_send(scan_delegator_handle, service_data, sync_handle);
326e8a39bfbSMatthias Ringwald                     break;
327e8a39bfbSMatthias Ringwald                 default:
328e8a39bfbSMatthias Ringwald                     break;
329d15b3374SMatthias Ringwald             }
330e8a39bfbSMatthias Ringwald 
331a934fe9bSMatthias Ringwald             // check if Broadcast Code is requested
332a934fe9bSMatthias Ringwald             if (manual_mode == false){
333a934fe9bSMatthias Ringwald                 le_audio_big_encryption_t big_encryption;
334a934fe9bSMatthias Ringwald                 uint8_t bad_code[16];
335a934fe9bSMatthias Ringwald                 broadcast_audio_scan_service_client_get_encryption_state(bass_cid, bass_source_id, &big_encryption, bad_code);
336a934fe9bSMatthias Ringwald                 if (big_encryption == LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED){
337a934fe9bSMatthias Ringwald                     printf("LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED -> send Broadcast Code\n");
338a934fe9bSMatthias Ringwald                     broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
339a934fe9bSMatthias Ringwald                 }
340a934fe9bSMatthias Ringwald             }
341d15b3374SMatthias Ringwald             break;
342d15b3374SMatthias Ringwald         default:
343d15b3374SMatthias Ringwald             break;
344d15b3374SMatthias Ringwald     }
345d15b3374SMatthias Ringwald }
346d15b3374SMatthias Ringwald 
3472a0db9deSMatthias Ringwald static void le_audio_broadcast_assistant_start_periodic_sync() {
3482a0db9deSMatthias Ringwald     printf("Start Periodic Advertising Sync\n");
3492a0db9deSMatthias Ringwald 
3502a0db9deSMatthias Ringwald     // ignore other advertisements
3512a0db9deSMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 1);
3522a0db9deSMatthias Ringwald     // sync to PA
3532a0db9deSMatthias Ringwald     gap_periodic_advertiser_list_clear();
3542a0db9deSMatthias Ringwald     gap_periodic_advertiser_list_add(broadcast_source_type, broadcast_source, broadcast_source_sid);
3552a0db9deSMatthias Ringwald     app_state = APP_W4_PA_AND_BIG_INFO;
3562a0db9deSMatthias Ringwald     gap_periodic_advertising_create_sync(0x01, broadcast_source_sid, broadcast_source_type, broadcast_source, 0, 1000, 0);
3572a0db9deSMatthias Ringwald }
3582a0db9deSMatthias Ringwald 
3592a0db9deSMatthias Ringwald static void
3602a0db9deSMatthias Ringwald le_audio_broadcast_assistant_found_scan_delegator(bd_addr_type_t *addr_type, const uint8_t *addr,
3612a0db9deSMatthias Ringwald                                                   const char *adv_name) {
3622a0db9deSMatthias Ringwald     memcpy(scan_delegator, addr, 6);
3632a0db9deSMatthias Ringwald     scan_delegator_type = (*addr_type);
3642a0db9deSMatthias Ringwald     btstack_strcpy(scan_delegator_name,  sizeof(scan_delegator_name), (const char *) adv_name);
3652a0db9deSMatthias Ringwald     printf("Broadcast sink found, addr %s, name: '%s'\n", bd_addr_to_str(scan_delegator), scan_delegator_name);
3662a0db9deSMatthias Ringwald     gap_whitelist_add(scan_delegator_type, scan_delegator);
3672a0db9deSMatthias Ringwald }
3682a0db9deSMatthias Ringwald 
369d15b3374SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
370d15b3374SMatthias Ringwald     UNUSED(channel);
371d15b3374SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
372d15b3374SMatthias Ringwald     switch (packet[0]) {
373d15b3374SMatthias Ringwald         case BTSTACK_EVENT_STATE:
374d15b3374SMatthias Ringwald             switch(btstack_event_state_get_state(packet)) {
375d15b3374SMatthias Ringwald                 case HCI_STATE_WORKING:
376d15b3374SMatthias Ringwald                     app_state = APP_IDLE;
377d15b3374SMatthias Ringwald #ifdef ENABLE_DEMO_MODE
378d15b3374SMatthias Ringwald                     start_scanning();
379d15b3374SMatthias Ringwald #else
380d15b3374SMatthias Ringwald                     show_usage();
381d15b3374SMatthias Ringwald #endif
382d15b3374SMatthias Ringwald                     break;
383d15b3374SMatthias Ringwald                 case HCI_STATE_OFF:
384d15b3374SMatthias Ringwald                     printf("Goodbye\n");
385d15b3374SMatthias Ringwald                     exit(0);
386d15b3374SMatthias Ringwald                     break;
387d15b3374SMatthias Ringwald                 default:
388d15b3374SMatthias Ringwald                     break;
389d15b3374SMatthias Ringwald             }
390d15b3374SMatthias Ringwald             break;
3912a0db9deSMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:{
3922a0db9deSMatthias Ringwald             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
3932a0db9deSMatthias Ringwald 
3942a0db9deSMatthias Ringwald             uint8_t adv_size = gap_event_advertising_report_get_data_length(packet);
3952a0db9deSMatthias Ringwald             const uint8_t * adv_data = gap_event_advertising_report_get_data(packet);
3962a0db9deSMatthias Ringwald 
3972a0db9deSMatthias Ringwald             ad_context_t context;
3982a0db9deSMatthias Ringwald             bool found_scan_delegator = false;
3992a0db9deSMatthias Ringwald             char adv_name[31];
4002a0db9deSMatthias Ringwald             adv_name[0] = 0;
4012a0db9deSMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
4022a0db9deSMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
4032a0db9deSMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
4042a0db9deSMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
4052a0db9deSMatthias Ringwald                 switch (data_type){
4062a0db9deSMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
4072a0db9deSMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
4082a0db9deSMatthias Ringwald                         size = btstack_min(sizeof(adv_name) - 1, size);
4092a0db9deSMatthias Ringwald                         memcpy(adv_name, data, size);
4102a0db9deSMatthias Ringwald                         adv_name[size] = 0;
4112a0db9deSMatthias Ringwald                         // detect scan delegator by name
4122a0db9deSMatthias Ringwald                         if (strncmp(adv_name, test_device_name, sizeof(test_device_name)) == 0){
4132a0db9deSMatthias Ringwald                             found_scan_delegator = true;
4142a0db9deSMatthias Ringwald                         }
4152a0db9deSMatthias Ringwald                         break;
4162a0db9deSMatthias Ringwald                     default:
4172a0db9deSMatthias Ringwald                         break;
4182a0db9deSMatthias Ringwald                 }
4192a0db9deSMatthias Ringwald             }
4202a0db9deSMatthias Ringwald 
4212a0db9deSMatthias Ringwald             if (found_scan_delegator == false) break;
4222a0db9deSMatthias Ringwald 
4232a0db9deSMatthias Ringwald             if ((have_scan_delegator == false) && found_scan_delegator){
4242a0db9deSMatthias Ringwald                 have_scan_delegator = true;
4252a0db9deSMatthias Ringwald                 bd_addr_t addr;
4262a0db9deSMatthias Ringwald                 gap_event_advertising_report_get_address(packet, addr);
4272a0db9deSMatthias Ringwald                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_advertising_report_get_address_type(packet);
4282a0db9deSMatthias Ringwald                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
4292a0db9deSMatthias Ringwald             }
4302a0db9deSMatthias Ringwald 
4312a0db9deSMatthias Ringwald             if ((have_broadcast_source && have_scan_delegator) == false) break;
4322a0db9deSMatthias Ringwald 
4332a0db9deSMatthias Ringwald             le_audio_broadcast_assistant_start_periodic_sync();
4342a0db9deSMatthias Ringwald             break;
4352a0db9deSMatthias Ringwald         }
436d15b3374SMatthias Ringwald         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
437d15b3374SMatthias Ringwald         {
438d15b3374SMatthias Ringwald             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
439d15b3374SMatthias Ringwald 
440d15b3374SMatthias Ringwald             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
441d15b3374SMatthias Ringwald             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
442d15b3374SMatthias Ringwald 
443d15b3374SMatthias Ringwald             ad_context_t context;
444d15b3374SMatthias Ringwald             bool found_scan_delegator = false;
445d15b3374SMatthias Ringwald             bool found_broadcast_source = false;
44689d964feSMatthias Ringwald             char adv_name[31];
4472a0db9deSMatthias Ringwald             adv_name[0] = 0;
448d15b3374SMatthias Ringwald 
449d15b3374SMatthias Ringwald             uint16_t uuid;
450d15b3374SMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
451d15b3374SMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
452d15b3374SMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
453d15b3374SMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
454d15b3374SMatthias Ringwald                 switch (data_type){
455d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
456d15b3374SMatthias Ringwald                         uuid = little_endian_read_16(data, 0);
457d15b3374SMatthias Ringwald                         switch (uuid){
458d15b3374SMatthias Ringwald                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE:
459d15b3374SMatthias Ringwald                                 broadcast_id = little_endian_read_24(data, 2);
460d15b3374SMatthias Ringwald                                 found_broadcast_source = true;
461d15b3374SMatthias Ringwald                                 break;
462d15b3374SMatthias Ringwald                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE:
463d15b3374SMatthias Ringwald                                 found_scan_delegator = true;
464d15b3374SMatthias Ringwald                                 break;
465d15b3374SMatthias Ringwald                             default:
466d15b3374SMatthias Ringwald                                 break;
467d15b3374SMatthias Ringwald                         }
468d15b3374SMatthias Ringwald                         break;
469d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
470d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
471d15b3374SMatthias Ringwald                         size = btstack_min(sizeof(adv_name) - 1, size);
472d15b3374SMatthias Ringwald                         memcpy(adv_name, data, size);
473d15b3374SMatthias Ringwald                         adv_name[size] = 0;
474d15b3374SMatthias Ringwald                         break;
475d15b3374SMatthias Ringwald                     default:
476d15b3374SMatthias Ringwald                         break;
477d15b3374SMatthias Ringwald                 }
478d15b3374SMatthias Ringwald             }
47932089ce4SMatthias Ringwald 
480d15b3374SMatthias Ringwald             if ((found_scan_delegator == false) && (found_broadcast_source == false)) break;
48132089ce4SMatthias Ringwald 
482d15b3374SMatthias Ringwald             if ((have_scan_delegator == false) && found_scan_delegator){
483d15b3374SMatthias Ringwald                 have_scan_delegator = true;
4842a0db9deSMatthias Ringwald                 bd_addr_t addr;
4852a0db9deSMatthias Ringwald                 gap_event_extended_advertising_report_get_address(packet, addr);
4862a0db9deSMatthias Ringwald                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_extended_advertising_report_get_address_type(packet);
4872a0db9deSMatthias Ringwald                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
488d15b3374SMatthias Ringwald             }
489d15b3374SMatthias Ringwald 
490d15b3374SMatthias Ringwald             if ((have_broadcast_source == false) && found_broadcast_source){
491d15b3374SMatthias Ringwald                 have_broadcast_source = true;
492d15b3374SMatthias Ringwald                 gap_event_extended_advertising_report_get_address(packet, broadcast_source);
493d15b3374SMatthias Ringwald                 broadcast_source_type = gap_event_extended_advertising_report_get_address_type(packet);
494d15b3374SMatthias Ringwald                 broadcast_source_sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
495d15b3374SMatthias Ringwald                 btstack_strcpy(broadcast_source_name,  sizeof(broadcast_source_name), (const char *) adv_name);
49632089ce4SMatthias Ringwald                 printf("Broadcast source found, addr %s, name: '%s'\n", bd_addr_to_str(broadcast_source), broadcast_source_name);
497d15b3374SMatthias Ringwald                 gap_whitelist_add(broadcast_source_type, broadcast_source);
498d15b3374SMatthias Ringwald             }
499d15b3374SMatthias Ringwald 
500d15b3374SMatthias Ringwald             if ((have_broadcast_source && have_scan_delegator) == false) break;
501d15b3374SMatthias Ringwald 
5022a0db9deSMatthias Ringwald             le_audio_broadcast_assistant_start_periodic_sync();
503d15b3374SMatthias Ringwald             break;
504d15b3374SMatthias Ringwald         }
505d15b3374SMatthias Ringwald 
506d15b3374SMatthias Ringwald         case HCI_EVENT_LE_META:
507d15b3374SMatthias Ringwald             switch(hci_event_le_meta_get_subevent_code(packet)) {
508d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
509d15b3374SMatthias Ringwald                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
510d15b3374SMatthias Ringwald                     broadcast_source_pa_interval = hci_subevent_le_periodic_advertising_sync_establishment_get_periodic_advertising_interval(packet);
511d15b3374SMatthias Ringwald                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
512d15b3374SMatthias Ringwald                     break;
513d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
514d15b3374SMatthias Ringwald                     if (have_base) break;
515d15b3374SMatthias Ringwald                     handle_periodic_advertisement(packet, size);
516d15b3374SMatthias Ringwald                     if (have_base & have_big_info){
517d15b3374SMatthias Ringwald                         have_base_and_big_info();
518d15b3374SMatthias Ringwald                     }
519d15b3374SMatthias Ringwald                     break;
520d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
521d15b3374SMatthias Ringwald                     if (have_big_info) break;
522d15b3374SMatthias Ringwald                     handle_big_info(packet, size);
523d15b3374SMatthias Ringwald                     if (have_base & have_big_info){
524d15b3374SMatthias Ringwald                         have_base_and_big_info();
525d15b3374SMatthias Ringwald                     }
526d15b3374SMatthias Ringwald                     break;
527d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
528d15b3374SMatthias Ringwald                     printf("BIG Sync Lost\n");
529d15b3374SMatthias Ringwald                     {
530d15b3374SMatthias Ringwald                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
531d15b3374SMatthias Ringwald                         if (sink != NULL) {
532d15b3374SMatthias Ringwald                             sink->stop_stream();
533d15b3374SMatthias Ringwald                             sink->close();
534d15b3374SMatthias Ringwald                         }
535d15b3374SMatthias Ringwald                     }
536d15b3374SMatthias Ringwald                     // start over
537d15b3374SMatthias Ringwald                     start_scanning();
538d15b3374SMatthias Ringwald                     break;
5394e150227SMatthias Ringwald                 default:
5404e150227SMatthias Ringwald                     break;
5414e150227SMatthias Ringwald             }
5424e150227SMatthias Ringwald             break;
5434e150227SMatthias Ringwald         case HCI_EVENT_META_GAP:
5444e150227SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)){
5454e150227SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
5464e150227SMatthias Ringwald                     if (gap_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break;
5474e150227SMatthias Ringwald                     scan_delegator_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
548d15b3374SMatthias Ringwald                     printf("Connection complete, handle 0x%04x\n", scan_delegator_handle);
549d15b3374SMatthias Ringwald                     broadcast_audio_scan_service_client_connect(&bass_connection,
550d15b3374SMatthias Ringwald                                                                 bass_sources,
551d15b3374SMatthias Ringwald                                                                 BASS_CLIENT_NUM_SOURCES,
552d15b3374SMatthias Ringwald                                                                 scan_delegator_handle,
553d15b3374SMatthias Ringwald                                                                 &bass_cid);
554d15b3374SMatthias Ringwald                     break;
555d15b3374SMatthias Ringwald                 default:
556d15b3374SMatthias Ringwald                     break;
557d15b3374SMatthias Ringwald             }
558d15b3374SMatthias Ringwald             break;
55932089ce4SMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
56032089ce4SMatthias Ringwald             printf("Just Works requested\n");
56132089ce4SMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
56232089ce4SMatthias Ringwald             break;
563d15b3374SMatthias Ringwald         default:
564d15b3374SMatthias Ringwald             break;
565d15b3374SMatthias Ringwald     }
566d15b3374SMatthias Ringwald }
567d15b3374SMatthias Ringwald 
568d15b3374SMatthias Ringwald static void show_usage(void){
569d15b3374SMatthias Ringwald     printf("\n--- LE Audio Broadcast Assistant Test Console ---\n");
570c3d56f70SMatthias Ringwald     printf("s - setup LE Broadcast Sink with Broadcast Source via Scan Delegator\n");
571c3d56f70SMatthias Ringwald     printf("c - scan and connect to Scan Delegator\n");
5726b11d590SMatthias Ringwald     printf("a - add source with BIS Sync 0x%08x\n", bis_sync_mask);
5736b11d590SMatthias Ringwald     printf("A - add source with BIS Sync 0x00000000 (do not sync)\n");
5746b11d590SMatthias Ringwald     printf("m - modify source to PA Sync = 0, bis sync = 0x00000000\n");
575222be8a8SMatthias Ringwald     printf("b - send Broadcast Code: ");
576222be8a8SMatthias Ringwald     printf_hexdump(broadcast_code, sizeof(broadcast_code));
577c3d56f70SMatthias Ringwald     printf("r - remove source\n");
578d15b3374SMatthias Ringwald     printf("---\n");
579d15b3374SMatthias Ringwald }
580d15b3374SMatthias Ringwald 
581d15b3374SMatthias Ringwald static void stdin_process(char c){
582d15b3374SMatthias Ringwald     switch (c){
583d15b3374SMatthias Ringwald         case 's':
584d15b3374SMatthias Ringwald             if (app_state != APP_IDLE) break;
585c3d56f70SMatthias Ringwald             manual_mode = false;
586d15b3374SMatthias Ringwald             start_scanning();
587d15b3374SMatthias Ringwald             break;
58837d87e7fSMatthias Ringwald         case 'c':
589c3d56f70SMatthias Ringwald             manual_mode = true;
590c3d56f70SMatthias Ringwald             start_scanning();
59137d87e7fSMatthias Ringwald             break;
592c3d56f70SMatthias Ringwald         case 'a':
593d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
594d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
5956b11d590SMatthias Ringwald             add_source();
5966b11d590SMatthias Ringwald             break;
5976b11d590SMatthias Ringwald         case 'A':
598d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = 0;
599d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = 0;
600c3d56f70SMatthias Ringwald             add_source();
60137d87e7fSMatthias Ringwald             break;
6026216cd2aSMatthias Ringwald         case 'm':
603d0b1d302SMatthias Ringwald             bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA;
604d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = 0;
605d0b1d302SMatthias Ringwald             broadcast_audio_scan_service_client_modify_source(bass_cid, bass_source_id, &bass_source_data);
606c3d56f70SMatthias Ringwald             break;
607c3d56f70SMatthias Ringwald         case 'r':
608585f85feSMatthias Ringwald             broadcast_audio_scan_service_client_remove_source(bass_cid, bass_source_id);
609c3d56f70SMatthias Ringwald             break;
610222be8a8SMatthias Ringwald         case 'b':
611e8a39bfbSMatthias Ringwald             broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
612222be8a8SMatthias Ringwald             break;
613d15b3374SMatthias Ringwald         case '\n':
614d15b3374SMatthias Ringwald         case '\r':
615d15b3374SMatthias Ringwald             break;
616d15b3374SMatthias Ringwald         default:
617d15b3374SMatthias Ringwald             show_usage();
618d15b3374SMatthias Ringwald             break;
619d15b3374SMatthias Ringwald 
620d15b3374SMatthias Ringwald     }
621d15b3374SMatthias Ringwald }
622d15b3374SMatthias Ringwald 
623d15b3374SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
624d15b3374SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
625d15b3374SMatthias Ringwald     (void) argv;
626d15b3374SMatthias Ringwald     (void) argc;
627d15b3374SMatthias Ringwald 
62832089ce4SMatthias Ringwald     l2cap_init();
62932089ce4SMatthias Ringwald     sm_init();
63032089ce4SMatthias Ringwald     gatt_client_init();
63132089ce4SMatthias Ringwald 
632d15b3374SMatthias Ringwald     // register for HCI events
633d15b3374SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
634d15b3374SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
635d15b3374SMatthias Ringwald 
63632089ce4SMatthias Ringwald     // register for SM events
63732089ce4SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
63832089ce4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
63932089ce4SMatthias Ringwald 
64032089ce4SMatthias Ringwald     broadcast_audio_scan_service_client_init(&bass_packet_handler);
641d15b3374SMatthias Ringwald 
642d15b3374SMatthias Ringwald     // turn on!
643d15b3374SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
644d15b3374SMatthias Ringwald 
645d15b3374SMatthias Ringwald     btstack_stdin_setup(stdin_process);
646d15b3374SMatthias Ringwald     return 0;
647d15b3374SMatthias Ringwald }
648