xref: /btstack/test/le_audio/le_audio_broadcast_assistant.c (revision c69fca8812e0fcf03cbe4f16800685051be1cea0)
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,
75*c69fca88SMatthias Ringwald     APP_W4_BROADCAST_SOURCE_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
93*c69fca88SMatthias Ringwald #define NUM_BROADCAST_SOURCES 5
94*c69fca88SMatthias Ringwald #define BROADCAST_SOURCE_NAME_LEN 20
95*c69fca88SMatthias Ringwald struct {
96*c69fca88SMatthias Ringwald     char name[BROADCAST_SOURCE_NAME_LEN];
97*c69fca88SMatthias Ringwald     bd_addr_t addr;
98*c69fca88SMatthias Ringwald     bd_addr_type_t addr_type;
99*c69fca88SMatthias Ringwald     uint8_t sid;
100*c69fca88SMatthias Ringwald     uint32_t broadcast_id;
101*c69fca88SMatthias Ringwald     uint16_t pa_interval;
102*c69fca88SMatthias Ringwald } broadcast_sources[NUM_BROADCAST_SOURCES];
103*c69fca88SMatthias Ringwald static uint8_t broadcast_source_current = 0;
104d15b3374SMatthias Ringwald 
105d15b3374SMatthias Ringwald // broadcast info
106d15b3374SMatthias Ringwald static hci_con_handle_t sync_handle;
107d15b3374SMatthias Ringwald static uint8_t          encryption;
108166aee19SMatthias Ringwald static uint8_t          broadcast_code [] = {0x01, 0x02, 0x68, 0x05, 0x53, 0xF1, 0x41, 0x5A, 0xA2, 0x65, 0xBB, 0xAF, 0xC6, 0xEA, 0x03, 0xB8, };
109d15b3374SMatthias Ringwald static uint8_t          num_bis;
1106b11d590SMatthias Ringwald static uint32_t         bis_sync_mask;
111d15b3374SMatthias Ringwald static uint32_t         sampling_frequency_hz;
112d15b3374SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration;
113d15b3374SMatthias Ringwald static uint16_t octets_per_frame;
114d15b3374SMatthias Ringwald 
115d15b3374SMatthias Ringwald // scan delegator info
116d15b3374SMatthias Ringwald static char scan_delegator_name[20];
117d15b3374SMatthias Ringwald static bd_addr_t scan_delegator;
118d15b3374SMatthias Ringwald static bd_addr_type_t scan_delegator_type;
119d15b3374SMatthias Ringwald static hci_con_handle_t scan_delegator_handle;
120d15b3374SMatthias Ringwald 
121d15b3374SMatthias Ringwald // BASS
122d15b3374SMatthias Ringwald #define BASS_CLIENT_NUM_SOURCES 1
123d15b3374SMatthias Ringwald static bass_client_connection_t bass_connection;
124d15b3374SMatthias Ringwald static bass_client_source_t bass_sources[BASS_CLIENT_NUM_SOURCES];
125d0b1d302SMatthias Ringwald static bass_source_data_t bass_source_data;
126d15b3374SMatthias Ringwald static uint16_t bass_cid;
1276216cd2aSMatthias Ringwald static uint8_t  bass_source_id;
128d15b3374SMatthias Ringwald 
12989d964feSMatthias Ringwald // test device name
13089d964feSMatthias Ringwald static const char test_device_name[] = "OnePlus Buds Pro 2";
13189d964feSMatthias Ringwald 
132d15b3374SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
133d15b3374SMatthias Ringwald 
134d15b3374SMatthias Ringwald     // periodic advertisement contains the BASE
135d15b3374SMatthias Ringwald     // TODO: BASE might be split across multiple advertisements
136d15b3374SMatthias Ringwald     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
137d15b3374SMatthias Ringwald     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
138d15b3374SMatthias Ringwald     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
139d15b3374SMatthias Ringwald 
140d15b3374SMatthias Ringwald     if (adv_status != 0) {
141d15b3374SMatthias Ringwald         printf("Periodic Advertisement (status %u): ", adv_status);
142d15b3374SMatthias Ringwald         printf_hexdump(adv_data, adv_size);
143d15b3374SMatthias Ringwald         return;
144d15b3374SMatthias Ringwald     }
145d15b3374SMatthias Ringwald 
146be81b159SMatthias Ringwald     le_audio_base_parser_t parser;
147be81b159SMatthias Ringwald     bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size);
148be81b159SMatthias Ringwald     if (ok == false){
149be81b159SMatthias Ringwald         return;
150be81b159SMatthias Ringwald     }
151d15b3374SMatthias Ringwald     have_base = true;
152be81b159SMatthias Ringwald 
153d15b3374SMatthias Ringwald     printf("BASE:\n");
154be81b159SMatthias Ringwald     uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser);
155d15b3374SMatthias Ringwald     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
156be81b159SMatthias Ringwald     uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser);
157d15b3374SMatthias Ringwald     // Cache in new source struct
158d0b1d302SMatthias Ringwald     bass_source_data.subgroups_num = num_subgroups;
159d15b3374SMatthias Ringwald     printf("- num subgroups: %u\n", num_subgroups);
160d15b3374SMatthias Ringwald     uint8_t i;
161d15b3374SMatthias Ringwald     for (i=0;i<num_subgroups;i++){
162d15b3374SMatthias Ringwald 
163d15b3374SMatthias Ringwald         // Cache in new source struct
164d0b1d302SMatthias Ringwald         bass_source_data.subgroups[i].bis_sync = 0;
165d15b3374SMatthias Ringwald 
166d15b3374SMatthias Ringwald         // Level 2: Subgroup Level
167be81b159SMatthias Ringwald         num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser);
168d15b3374SMatthias Ringwald         printf("  - num bis[%u]: %u\n", i, num_bis);
169be81b159SMatthias Ringwald         uint8_t codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
170be81b159SMatthias Ringwald         const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser);
171d15b3374SMatthias Ringwald         printf("  - codec specific config[%u]: ", i);
172d15b3374SMatthias Ringwald         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
173d15b3374SMatthias Ringwald         // parse config to get sampling frequency and frame duration
174d15b3374SMatthias Ringwald         uint8_t codec_offset = 0;
175d15b3374SMatthias Ringwald         while ((codec_offset + 1) < codec_specific_configuration_length){
176d15b3374SMatthias Ringwald             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
177d15b3374SMatthias Ringwald             uint8_t ltv_type = codec_specific_configuration[codec_offset];
178d15b3374SMatthias Ringwald             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
179d15b3374SMatthias Ringwald             uint8_t sampling_frequency_index;
180d15b3374SMatthias Ringwald             uint8_t frame_duration_index;
181d15b3374SMatthias Ringwald             switch (ltv_type){
182d15b3374SMatthias Ringwald                 case 0x01: // sampling frequency
183d15b3374SMatthias Ringwald                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
184d15b3374SMatthias Ringwald                     // TODO: check range
185d15b3374SMatthias Ringwald                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
186d15b3374SMatthias Ringwald                     printf("    - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
187d15b3374SMatthias Ringwald                     break;
188d15b3374SMatthias Ringwald                 case 0x02: // 0 = 7.5, 1 = 10 ms
189d15b3374SMatthias Ringwald                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
190ff59851cSMilanka Ringwald                     frame_duration = le_audio_util_get_btstack_lc3_frame_duration(frame_duration_index);
191ff59851cSMilanka Ringwald                     printf("    - frame duration[%u]: %u us\n", i, le_audio_get_frame_duration_us(frame_duration_index));
192d15b3374SMatthias Ringwald                     break;
193d15b3374SMatthias Ringwald                 case 0x04:  // octets per coding frame
194d15b3374SMatthias Ringwald                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
195d15b3374SMatthias Ringwald                     printf("    - octets per codec frame[%u]: %u\n", i, octets_per_frame);
196d15b3374SMatthias Ringwald                     break;
197d15b3374SMatthias Ringwald                 default:
198d15b3374SMatthias Ringwald                     break;
199d15b3374SMatthias Ringwald             }
200d15b3374SMatthias Ringwald             codec_offset += ltv_len;
201d15b3374SMatthias Ringwald         }
202be81b159SMatthias Ringwald         uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser);
203be81b159SMatthias Ringwald         const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser);
204d15b3374SMatthias Ringwald         printf("  - meta data[%u]: ", i);
205d15b3374SMatthias Ringwald         printf_hexdump(meta_data, metadata_length);
206d15b3374SMatthias Ringwald         uint8_t k;
207d15b3374SMatthias Ringwald         for (k=0;k<num_bis;k++){
208d15b3374SMatthias Ringwald             // Level 3: BIS Level
209be81b159SMatthias Ringwald             uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser);
210d15b3374SMatthias Ringwald 
2116216cd2aSMatthias Ringwald             // check value
2126216cd2aSMatthias Ringwald             // double check message
2136216cd2aSMatthias Ringwald 
214d15b3374SMatthias Ringwald             // Cache in new source struct
2156b11d590SMatthias Ringwald             bis_sync_mask |= 1 << (bis_index-1);
2166b11d590SMatthias Ringwald 
217d0b1d302SMatthias Ringwald             bass_source_data.subgroups[i].metadata_length = 0;
218d0b1d302SMatthias Ringwald             memset(&bass_source_data.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
219d15b3374SMatthias Ringwald 
220d15b3374SMatthias Ringwald             printf("    - bis index[%u][%u]: %u\n", i, k, bis_index);
221be81b159SMatthias Ringwald             codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
222be81b159SMatthias Ringwald             codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser);
223d15b3374SMatthias Ringwald             printf("    - codec specific config[%u][%u]: ", i, k);
224be81b159SMatthias Ringwald             printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
225be81b159SMatthias Ringwald 
226be81b159SMatthias Ringwald             // parse next BIS
227be81b159SMatthias Ringwald             le_audio_base_parser_bis_next(&parser);
228d15b3374SMatthias Ringwald         }
229be81b159SMatthias Ringwald 
230be81b159SMatthias Ringwald         // parse next subgroup
231be81b159SMatthias Ringwald         le_audio_base_parser_subgroup_next(&parser);
232d15b3374SMatthias Ringwald     }
233d15b3374SMatthias Ringwald }
234d15b3374SMatthias Ringwald 
235d15b3374SMatthias Ringwald static void start_scanning() {
236*c69fca88SMatthias Ringwald     app_state = APP_W4_BROADCAST_SOURCE_AND_SCAN_DELEGATOR_ADV;
237d15b3374SMatthias Ringwald     have_base = false;
238d15b3374SMatthias Ringwald     have_big_info = false;
239d15b3374SMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 0);
240d15b3374SMatthias Ringwald     gap_start_scan();
241d15b3374SMatthias Ringwald     printf("Start scan..\n");
242d15b3374SMatthias Ringwald }
243d15b3374SMatthias Ringwald 
244d15b3374SMatthias Ringwald static void have_base_and_big_info(void){
24532089ce4SMatthias Ringwald     printf("Connecting to Scan Delegator/Sink!\n");
246d15b3374SMatthias Ringwald 
24728ef06ceSMatthias Ringwald     // stop scanning
24828ef06ceSMatthias Ringwald     gap_stop_scan();
24928ef06ceSMatthias Ringwald 
250d15b3374SMatthias Ringwald     // todo: connect to scan delegator
251d15b3374SMatthias Ringwald     app_state = APP_W4_SCAN_DELEGATOR_CONNECTION;
252d15b3374SMatthias Ringwald     gap_connect(scan_delegator, scan_delegator_type);
253d15b3374SMatthias Ringwald }
254d15b3374SMatthias Ringwald 
255d15b3374SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){
256d15b3374SMatthias Ringwald     printf("BIG Info advertising report\n");
257d15b3374SMatthias Ringwald     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
258d15b3374SMatthias Ringwald     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
259d15b3374SMatthias Ringwald     if (encryption) {
260d15b3374SMatthias Ringwald         printf("Stream is encrypted\n");
261d15b3374SMatthias Ringwald     }
262d15b3374SMatthias Ringwald     have_big_info = true;
263d15b3374SMatthias Ringwald }
264c3d56f70SMatthias Ringwald 
265c3d56f70SMatthias Ringwald static void add_source() {// setup bass source info
266d0b1d302SMatthias Ringwald     printf("BASS Client: add source with BIS Sync 0x%04x\n", bass_source_data.subgroups[0].bis_sync_state);
267*c69fca88SMatthias Ringwald     bass_source_data.address_type = broadcast_sources[broadcast_source_current].addr_type;
268*c69fca88SMatthias Ringwald     memcpy(bass_source_data.address, broadcast_sources[broadcast_source_current].addr, 6);
269*c69fca88SMatthias Ringwald     bass_source_data.adv_sid = broadcast_sources[broadcast_source_current].sid;
270*c69fca88SMatthias Ringwald     bass_source_data.broadcast_id = broadcast_sources[broadcast_source_current].broadcast_id;
271d0b1d302SMatthias Ringwald     bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
272*c69fca88SMatthias Ringwald     bass_source_data.pa_interval = broadcast_sources[broadcast_source_current].pa_interval;
273c3d56f70SMatthias Ringwald     // bass_source_new.subgroups_num set in BASE parser
274c3d56f70SMatthias Ringwald     // bass_source_new.subgroup[i].* set in BASE parser
275c3d56f70SMatthias Ringwald 
276c3d56f70SMatthias Ringwald     // add bass source
277d0b1d302SMatthias Ringwald     broadcast_audio_scan_service_client_add_source(bass_cid, &bass_source_data);
278c3d56f70SMatthias Ringwald }
279c3d56f70SMatthias Ringwald 
280d15b3374SMatthias Ringwald static void bass_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
281d15b3374SMatthias Ringwald     UNUSED(channel);
282d15b3374SMatthias Ringwald     UNUSED(size);
283d15b3374SMatthias Ringwald 
284d15b3374SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
285d15b3374SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) return;
286d15b3374SMatthias Ringwald 
287e8a39bfbSMatthias Ringwald     const bass_source_data_t * source_data;
288e8a39bfbSMatthias Ringwald 
289d15b3374SMatthias Ringwald     switch (hci_event_gattservice_meta_get_subevent_code(packet)) {
290bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_CONNECTED:
291bebf6b57SMatthias Ringwald             if (gattservice_subevent_bass_client_connected_get_status(packet) != ERROR_CODE_SUCCESS){
292d15b3374SMatthias Ringwald                 printf("BASS client connection failed, cid 0x%02x, con_handle 0x%02x, status 0x%02x\n",
293d15b3374SMatthias Ringwald                        bass_cid, scan_delegator_handle,
294bebf6b57SMatthias Ringwald                        gattservice_subevent_bass_client_connected_get_status(packet));
295d15b3374SMatthias Ringwald                 return;
296d15b3374SMatthias Ringwald             }
297d15b3374SMatthias Ringwald             printf("BASS client connected, cid 0x%02x\n", bass_cid);
298d15b3374SMatthias Ringwald 
29937d87e7fSMatthias Ringwald             if ((have_big_info == false) || (have_base == false)) break;
300c3d56f70SMatthias Ringwald             if (manual_mode) return;
30137d87e7fSMatthias Ringwald 
302d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
303d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
3046b11d590SMatthias Ringwald             add_source();
305d15b3374SMatthias Ringwald             break;
306bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_SOURCE_OPERATION_COMPLETE:
307bebf6b57SMatthias Ringwald             if (gattservice_subevent_bass_client_source_operation_complete_get_status(packet) != ERROR_CODE_SUCCESS){
308bebf6b57SMatthias Ringwald                 printf("BASS client source operation failed, status 0x%02x\n", gattservice_subevent_bass_client_source_operation_complete_get_status(packet));
309d15b3374SMatthias Ringwald                 return;
310d15b3374SMatthias Ringwald             }
311d15b3374SMatthias Ringwald 
312bebf6b57SMatthias Ringwald             if ( gattservice_subevent_bass_client_source_operation_complete_get_opcode(packet) == (uint8_t)BASS_OPCODE_ADD_SOURCE ){
3136216cd2aSMatthias Ringwald                 // TODO: set state to 'wait for source_id"
3146216cd2aSMatthias Ringwald                 printf("BASS client add source operation completed, wait for source_id\n");
3156216cd2aSMatthias Ringwald             }
3166216cd2aSMatthias Ringwald             break;
317bebf6b57SMatthias Ringwald         case GATTSERVICE_SUBEVENT_BASS_CLIENT_NOTIFICATION_COMPLETE:
3186216cd2aSMatthias Ringwald             // store source_id
319bebf6b57SMatthias Ringwald             bass_source_id = gattservice_subevent_bass_client_notification_complete_get_source_id(packet);
3206216cd2aSMatthias Ringwald             printf("BASS client notification, source_id = 0x%02x\n", bass_source_id);
321a22ed910SMatthias Ringwald             source_data = broadcast_audio_scan_service_client_get_source_data(bass_cid, bass_source_id);
322a22ed910SMatthias Ringwald             btstack_assert(source_data != NULL);
323d15b3374SMatthias Ringwald 
324e8a39bfbSMatthias Ringwald             switch (source_data->pa_sync_state){
325e8a39bfbSMatthias Ringwald                 case LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST:
326e8a39bfbSMatthias Ringwald                     // start pa sync transfer
3276216cd2aSMatthias Ringwald                     printf("LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST -> Start PAST\n");
328e8a39bfbSMatthias Ringwald                      // TODO: unclear why this needs to be shifted for PAST with TS to get test green
329e8a39bfbSMatthias Ringwald                     uint16_t service_data = bass_source_id << 8;
330d15b3374SMatthias Ringwald                     gap_periodic_advertising_sync_transfer_send(scan_delegator_handle, service_data, sync_handle);
331e8a39bfbSMatthias Ringwald                     break;
332e8a39bfbSMatthias Ringwald                 default:
333e8a39bfbSMatthias Ringwald                     break;
334d15b3374SMatthias Ringwald             }
335e8a39bfbSMatthias Ringwald 
336a934fe9bSMatthias Ringwald             // check if Broadcast Code is requested
337a934fe9bSMatthias Ringwald             if (manual_mode == false){
338a934fe9bSMatthias Ringwald                 le_audio_big_encryption_t big_encryption;
339a934fe9bSMatthias Ringwald                 uint8_t bad_code[16];
340a934fe9bSMatthias Ringwald                 broadcast_audio_scan_service_client_get_encryption_state(bass_cid, bass_source_id, &big_encryption, bad_code);
341a934fe9bSMatthias Ringwald                 if (big_encryption == LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED){
342a934fe9bSMatthias Ringwald                     printf("LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED -> send Broadcast Code\n");
343a934fe9bSMatthias Ringwald                     broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
344a934fe9bSMatthias Ringwald                 }
345a934fe9bSMatthias Ringwald             }
346d15b3374SMatthias Ringwald             break;
347d15b3374SMatthias Ringwald         default:
348d15b3374SMatthias Ringwald             break;
349d15b3374SMatthias Ringwald     }
350d15b3374SMatthias Ringwald }
351d15b3374SMatthias Ringwald 
3522a0db9deSMatthias Ringwald static void le_audio_broadcast_assistant_start_periodic_sync() {
3532a0db9deSMatthias Ringwald     printf("Start Periodic Advertising Sync\n");
3542a0db9deSMatthias Ringwald 
3552a0db9deSMatthias Ringwald     // ignore other advertisements
3562a0db9deSMatthias Ringwald     gap_set_scan_params(1, 0x30, 0x30, 1);
3572a0db9deSMatthias Ringwald     // sync to PA
3582a0db9deSMatthias Ringwald     gap_periodic_advertiser_list_clear();
359*c69fca88SMatthias Ringwald     gap_periodic_advertiser_list_add(broadcast_sources[broadcast_source_current].addr_type, broadcast_sources[broadcast_source_current].addr, broadcast_sources[broadcast_source_current].sid);
3602a0db9deSMatthias Ringwald     app_state = APP_W4_PA_AND_BIG_INFO;
361*c69fca88SMatthias Ringwald     gap_periodic_advertising_create_sync(0x01, broadcast_sources[broadcast_source_current].sid, broadcast_sources[broadcast_source_current].addr_type, broadcast_sources[broadcast_source_current].addr, 0, 1000, 0);
3622a0db9deSMatthias Ringwald }
3632a0db9deSMatthias Ringwald 
3642a0db9deSMatthias Ringwald static void
3652a0db9deSMatthias Ringwald le_audio_broadcast_assistant_found_scan_delegator(bd_addr_type_t *addr_type, const uint8_t *addr,
3662a0db9deSMatthias Ringwald                                                   const char *adv_name) {
3672a0db9deSMatthias Ringwald     memcpy(scan_delegator, addr, 6);
3682a0db9deSMatthias Ringwald     scan_delegator_type = (*addr_type);
3692a0db9deSMatthias Ringwald     btstack_strcpy(scan_delegator_name,  sizeof(scan_delegator_name), (const char *) adv_name);
3702a0db9deSMatthias Ringwald     printf("Broadcast sink found, addr %s, name: '%s'\n", bd_addr_to_str(scan_delegator), scan_delegator_name);
3712a0db9deSMatthias Ringwald     gap_whitelist_add(scan_delegator_type, scan_delegator);
3722a0db9deSMatthias Ringwald }
3732a0db9deSMatthias Ringwald 
374d15b3374SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
375d15b3374SMatthias Ringwald     UNUSED(channel);
376d15b3374SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
377d15b3374SMatthias Ringwald     switch (packet[0]) {
378d15b3374SMatthias Ringwald         case BTSTACK_EVENT_STATE:
379d15b3374SMatthias Ringwald             switch(btstack_event_state_get_state(packet)) {
380d15b3374SMatthias Ringwald                 case HCI_STATE_WORKING:
381d15b3374SMatthias Ringwald                     app_state = APP_IDLE;
382d15b3374SMatthias Ringwald #ifdef ENABLE_DEMO_MODE
383d15b3374SMatthias Ringwald                     start_scanning();
384d15b3374SMatthias Ringwald #else
385d15b3374SMatthias Ringwald                     show_usage();
386d15b3374SMatthias Ringwald #endif
387d15b3374SMatthias Ringwald                     break;
388d15b3374SMatthias Ringwald                 case HCI_STATE_OFF:
389d15b3374SMatthias Ringwald                     printf("Goodbye\n");
390d15b3374SMatthias Ringwald                     exit(0);
391d15b3374SMatthias Ringwald                     break;
392d15b3374SMatthias Ringwald                 default:
393d15b3374SMatthias Ringwald                     break;
394d15b3374SMatthias Ringwald             }
395d15b3374SMatthias Ringwald             break;
3962a0db9deSMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:{
3972a0db9deSMatthias Ringwald             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
3982a0db9deSMatthias Ringwald 
3992a0db9deSMatthias Ringwald             uint8_t adv_size = gap_event_advertising_report_get_data_length(packet);
4002a0db9deSMatthias Ringwald             const uint8_t * adv_data = gap_event_advertising_report_get_data(packet);
4012a0db9deSMatthias Ringwald 
4022a0db9deSMatthias Ringwald             ad_context_t context;
4032a0db9deSMatthias Ringwald             bool found_scan_delegator = false;
4042a0db9deSMatthias Ringwald             char adv_name[31];
4052a0db9deSMatthias Ringwald             adv_name[0] = 0;
4062a0db9deSMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
4072a0db9deSMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
4082a0db9deSMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
4092a0db9deSMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
4102a0db9deSMatthias Ringwald                 switch (data_type){
4112a0db9deSMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
4122a0db9deSMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
4132a0db9deSMatthias Ringwald                         size = btstack_min(sizeof(adv_name) - 1, size);
4142a0db9deSMatthias Ringwald                         memcpy(adv_name, data, size);
4152a0db9deSMatthias Ringwald                         adv_name[size] = 0;
4162a0db9deSMatthias Ringwald                         // detect scan delegator by name
4172a0db9deSMatthias Ringwald                         if (strncmp(adv_name, test_device_name, sizeof(test_device_name)) == 0){
4182a0db9deSMatthias Ringwald                             found_scan_delegator = true;
4192a0db9deSMatthias Ringwald                         }
4202a0db9deSMatthias Ringwald                         break;
4212a0db9deSMatthias Ringwald                     default:
4222a0db9deSMatthias Ringwald                         break;
4232a0db9deSMatthias Ringwald                 }
4242a0db9deSMatthias Ringwald             }
4252a0db9deSMatthias Ringwald 
4262a0db9deSMatthias Ringwald             if (found_scan_delegator == false) break;
4272a0db9deSMatthias Ringwald 
4282a0db9deSMatthias Ringwald             if ((have_scan_delegator == false) && found_scan_delegator){
4292a0db9deSMatthias Ringwald                 have_scan_delegator = true;
4302a0db9deSMatthias Ringwald                 bd_addr_t addr;
4312a0db9deSMatthias Ringwald                 gap_event_advertising_report_get_address(packet, addr);
4322a0db9deSMatthias Ringwald                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_advertising_report_get_address_type(packet);
4332a0db9deSMatthias Ringwald                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
4342a0db9deSMatthias Ringwald             }
4352a0db9deSMatthias Ringwald 
4362a0db9deSMatthias Ringwald             if ((have_broadcast_source && have_scan_delegator) == false) break;
4372a0db9deSMatthias Ringwald 
4382a0db9deSMatthias Ringwald             le_audio_broadcast_assistant_start_periodic_sync();
4392a0db9deSMatthias Ringwald             break;
4402a0db9deSMatthias Ringwald         }
441d15b3374SMatthias Ringwald         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
442d15b3374SMatthias Ringwald         {
443d15b3374SMatthias Ringwald             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
444d15b3374SMatthias Ringwald 
445d15b3374SMatthias Ringwald             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
446d15b3374SMatthias Ringwald             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
447d15b3374SMatthias Ringwald 
448d15b3374SMatthias Ringwald             ad_context_t context;
449d15b3374SMatthias Ringwald             bool found_scan_delegator = false;
450d15b3374SMatthias Ringwald             bool found_broadcast_source = false;
45189d964feSMatthias Ringwald             char adv_name[31];
4522a0db9deSMatthias Ringwald             adv_name[0] = 0;
453d15b3374SMatthias Ringwald 
454d15b3374SMatthias Ringwald             uint16_t uuid;
455d15b3374SMatthias Ringwald             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
456d15b3374SMatthias Ringwald                 uint8_t data_type = ad_iterator_get_data_type(&context);
457d15b3374SMatthias Ringwald                 uint8_t size = ad_iterator_get_data_len(&context);
458d15b3374SMatthias Ringwald                 const uint8_t *data = ad_iterator_get_data(&context);
459d15b3374SMatthias Ringwald                 switch (data_type){
460d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
461d15b3374SMatthias Ringwald                         uuid = little_endian_read_16(data, 0);
462d15b3374SMatthias Ringwald                         switch (uuid){
463d15b3374SMatthias Ringwald                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE:
464*c69fca88SMatthias Ringwald                                 broadcast_sources[broadcast_source_current].broadcast_id = little_endian_read_24(data, 2);
465d15b3374SMatthias Ringwald                                 found_broadcast_source = true;
466d15b3374SMatthias Ringwald                                 break;
467d15b3374SMatthias Ringwald                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE:
468d15b3374SMatthias Ringwald                                 found_scan_delegator = true;
469d15b3374SMatthias Ringwald                                 break;
470d15b3374SMatthias Ringwald                             default:
471d15b3374SMatthias Ringwald                                 break;
472d15b3374SMatthias Ringwald                         }
473d15b3374SMatthias Ringwald                         break;
474d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
475d15b3374SMatthias Ringwald                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
476d15b3374SMatthias Ringwald                         size = btstack_min(sizeof(adv_name) - 1, size);
477d15b3374SMatthias Ringwald                         memcpy(adv_name, data, size);
478d15b3374SMatthias Ringwald                         adv_name[size] = 0;
479d15b3374SMatthias Ringwald                         break;
480d15b3374SMatthias Ringwald                     default:
481d15b3374SMatthias Ringwald                         break;
482d15b3374SMatthias Ringwald                 }
483d15b3374SMatthias Ringwald             }
48432089ce4SMatthias Ringwald 
485d15b3374SMatthias Ringwald             if ((found_scan_delegator == false) && (found_broadcast_source == false)) break;
48632089ce4SMatthias Ringwald 
487d15b3374SMatthias Ringwald             if ((have_scan_delegator == false) && found_scan_delegator){
488d15b3374SMatthias Ringwald                 have_scan_delegator = true;
4892a0db9deSMatthias Ringwald                 bd_addr_t addr;
4902a0db9deSMatthias Ringwald                 gap_event_extended_advertising_report_get_address(packet, addr);
4912a0db9deSMatthias Ringwald                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_extended_advertising_report_get_address_type(packet);
4922a0db9deSMatthias Ringwald                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
493d15b3374SMatthias Ringwald             }
494d15b3374SMatthias Ringwald 
495d15b3374SMatthias Ringwald             if ((have_broadcast_source == false) && found_broadcast_source){
496d15b3374SMatthias Ringwald                 have_broadcast_source = true;
497*c69fca88SMatthias Ringwald                 gap_event_extended_advertising_report_get_address(packet, broadcast_sources[broadcast_source_current].addr);
498*c69fca88SMatthias Ringwald                 broadcast_sources[broadcast_source_current].addr_type = gap_event_extended_advertising_report_get_address_type(packet);
499*c69fca88SMatthias Ringwald                 broadcast_sources[broadcast_source_current].sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
500*c69fca88SMatthias Ringwald                 btstack_strcpy(broadcast_sources[broadcast_source_current].name,  BROADCAST_SOURCE_NAME_LEN, (const char *) adv_name);
501*c69fca88SMatthias Ringwald                 printf("Broadcast source found, addr %s, name: '%s'\n", bd_addr_to_str(broadcast_sources[broadcast_source_current].addr), broadcast_sources[broadcast_source_current].name);
502*c69fca88SMatthias Ringwald                 gap_whitelist_add(broadcast_sources[broadcast_source_current].addr_type, broadcast_sources[broadcast_source_current].addr);
503d15b3374SMatthias Ringwald             }
504d15b3374SMatthias Ringwald 
505d15b3374SMatthias Ringwald             if ((have_broadcast_source && have_scan_delegator) == false) break;
506d15b3374SMatthias Ringwald 
5072a0db9deSMatthias Ringwald             le_audio_broadcast_assistant_start_periodic_sync();
508d15b3374SMatthias Ringwald             break;
509d15b3374SMatthias Ringwald         }
510d15b3374SMatthias Ringwald 
511d15b3374SMatthias Ringwald         case HCI_EVENT_LE_META:
512d15b3374SMatthias Ringwald             switch(hci_event_le_meta_get_subevent_code(packet)) {
513d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
514d15b3374SMatthias Ringwald                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
515*c69fca88SMatthias Ringwald                     broadcast_sources[broadcast_source_current].pa_interval = hci_subevent_le_periodic_advertising_sync_establishment_get_periodic_advertising_interval(packet);
516d15b3374SMatthias Ringwald                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
517d15b3374SMatthias Ringwald                     break;
518d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
519d15b3374SMatthias Ringwald                     if (have_base) break;
520d15b3374SMatthias Ringwald                     handle_periodic_advertisement(packet, size);
521d15b3374SMatthias Ringwald                     if (have_base & have_big_info){
522d15b3374SMatthias Ringwald                         have_base_and_big_info();
523d15b3374SMatthias Ringwald                     }
524d15b3374SMatthias Ringwald                     break;
525d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
526d15b3374SMatthias Ringwald                     if (have_big_info) break;
527d15b3374SMatthias Ringwald                     handle_big_info(packet, size);
528d15b3374SMatthias Ringwald                     if (have_base & have_big_info){
529d15b3374SMatthias Ringwald                         have_base_and_big_info();
530d15b3374SMatthias Ringwald                     }
531d15b3374SMatthias Ringwald                     break;
532d15b3374SMatthias Ringwald                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
533d15b3374SMatthias Ringwald                     printf("BIG Sync Lost\n");
534d15b3374SMatthias Ringwald                     {
535d15b3374SMatthias Ringwald                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
536d15b3374SMatthias Ringwald                         if (sink != NULL) {
537d15b3374SMatthias Ringwald                             sink->stop_stream();
538d15b3374SMatthias Ringwald                             sink->close();
539d15b3374SMatthias Ringwald                         }
540d15b3374SMatthias Ringwald                     }
541d15b3374SMatthias Ringwald                     // start over
542d15b3374SMatthias Ringwald                     start_scanning();
543d15b3374SMatthias Ringwald                     break;
5444e150227SMatthias Ringwald                 default:
5454e150227SMatthias Ringwald                     break;
5464e150227SMatthias Ringwald             }
5474e150227SMatthias Ringwald             break;
5484e150227SMatthias Ringwald         case HCI_EVENT_META_GAP:
5494e150227SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)){
5504e150227SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
5514e150227SMatthias Ringwald                     if (gap_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break;
5524e150227SMatthias Ringwald                     scan_delegator_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
553d15b3374SMatthias Ringwald                     printf("Connection complete, handle 0x%04x\n", scan_delegator_handle);
554d15b3374SMatthias Ringwald                     broadcast_audio_scan_service_client_connect(&bass_connection,
555d15b3374SMatthias Ringwald                                                                 bass_sources,
556d15b3374SMatthias Ringwald                                                                 BASS_CLIENT_NUM_SOURCES,
557d15b3374SMatthias Ringwald                                                                 scan_delegator_handle,
558d15b3374SMatthias Ringwald                                                                 &bass_cid);
559d15b3374SMatthias Ringwald                     break;
560d15b3374SMatthias Ringwald                 default:
561d15b3374SMatthias Ringwald                     break;
562d15b3374SMatthias Ringwald             }
563d15b3374SMatthias Ringwald             break;
56432089ce4SMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
56532089ce4SMatthias Ringwald             printf("Just Works requested\n");
56632089ce4SMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
56732089ce4SMatthias Ringwald             break;
568d15b3374SMatthias Ringwald         default:
569d15b3374SMatthias Ringwald             break;
570d15b3374SMatthias Ringwald     }
571d15b3374SMatthias Ringwald }
572d15b3374SMatthias Ringwald 
573d15b3374SMatthias Ringwald static void show_usage(void){
574d15b3374SMatthias Ringwald     printf("\n--- LE Audio Broadcast Assistant Test Console ---\n");
575c3d56f70SMatthias Ringwald     printf("s - setup LE Broadcast Sink with Broadcast Source via Scan Delegator\n");
576c3d56f70SMatthias Ringwald     printf("c - scan and connect to Scan Delegator\n");
5776b11d590SMatthias Ringwald     printf("a - add source with BIS Sync 0x%08x\n", bis_sync_mask);
5786b11d590SMatthias Ringwald     printf("A - add source with BIS Sync 0x00000000 (do not sync)\n");
5796b11d590SMatthias Ringwald     printf("m - modify source to PA Sync = 0, bis sync = 0x00000000\n");
580222be8a8SMatthias Ringwald     printf("b - send Broadcast Code: ");
581222be8a8SMatthias Ringwald     printf_hexdump(broadcast_code, sizeof(broadcast_code));
582c3d56f70SMatthias Ringwald     printf("r - remove source\n");
583d15b3374SMatthias Ringwald     printf("---\n");
584d15b3374SMatthias Ringwald }
585d15b3374SMatthias Ringwald 
586d15b3374SMatthias Ringwald static void stdin_process(char c){
587d15b3374SMatthias Ringwald     switch (c){
588d15b3374SMatthias Ringwald         case 's':
589d15b3374SMatthias Ringwald             if (app_state != APP_IDLE) break;
590c3d56f70SMatthias Ringwald             manual_mode = false;
591d15b3374SMatthias Ringwald             start_scanning();
592d15b3374SMatthias Ringwald             break;
59337d87e7fSMatthias Ringwald         case 'c':
594c3d56f70SMatthias Ringwald             manual_mode = true;
595c3d56f70SMatthias Ringwald             start_scanning();
59637d87e7fSMatthias Ringwald             break;
597c3d56f70SMatthias Ringwald         case 'a':
598d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
599d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
6006b11d590SMatthias Ringwald             add_source();
6016b11d590SMatthias Ringwald             break;
6026b11d590SMatthias Ringwald         case 'A':
603d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = 0;
604d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync       = 0;
605c3d56f70SMatthias Ringwald             add_source();
60637d87e7fSMatthias Ringwald             break;
6076216cd2aSMatthias Ringwald         case 'm':
608d0b1d302SMatthias Ringwald             bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA;
609d0b1d302SMatthias Ringwald             bass_source_data.subgroups[0].bis_sync_state = 0;
610d0b1d302SMatthias Ringwald             broadcast_audio_scan_service_client_modify_source(bass_cid, bass_source_id, &bass_source_data);
611c3d56f70SMatthias Ringwald             break;
612c3d56f70SMatthias Ringwald         case 'r':
613585f85feSMatthias Ringwald             broadcast_audio_scan_service_client_remove_source(bass_cid, bass_source_id);
614c3d56f70SMatthias Ringwald             break;
615222be8a8SMatthias Ringwald         case 'b':
616e8a39bfbSMatthias Ringwald             broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
617222be8a8SMatthias Ringwald             break;
618d15b3374SMatthias Ringwald         case '\n':
619d15b3374SMatthias Ringwald         case '\r':
620d15b3374SMatthias Ringwald             break;
621d15b3374SMatthias Ringwald         default:
622d15b3374SMatthias Ringwald             show_usage();
623d15b3374SMatthias Ringwald             break;
624d15b3374SMatthias Ringwald 
625d15b3374SMatthias Ringwald     }
626d15b3374SMatthias Ringwald }
627d15b3374SMatthias Ringwald 
628d15b3374SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
629d15b3374SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
630d15b3374SMatthias Ringwald     (void) argv;
631d15b3374SMatthias Ringwald     (void) argc;
632d15b3374SMatthias Ringwald 
63332089ce4SMatthias Ringwald     l2cap_init();
63432089ce4SMatthias Ringwald     sm_init();
63532089ce4SMatthias Ringwald     gatt_client_init();
63632089ce4SMatthias Ringwald 
637d15b3374SMatthias Ringwald     // register for HCI events
638d15b3374SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
639d15b3374SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
640d15b3374SMatthias Ringwald 
64132089ce4SMatthias Ringwald     // register for SM events
64232089ce4SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
64332089ce4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
64432089ce4SMatthias Ringwald 
64532089ce4SMatthias Ringwald     broadcast_audio_scan_service_client_init(&bass_packet_handler);
646d15b3374SMatthias Ringwald 
647d15b3374SMatthias Ringwald     // turn on!
648d15b3374SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
649d15b3374SMatthias Ringwald 
650d15b3374SMatthias Ringwald     btstack_stdin_setup(stdin_process);
651d15b3374SMatthias Ringwald     return 0;
652d15b3374SMatthias Ringwald }
653