xref: /btstack/test/le_audio/le_audio_broadcast_assistant.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
1 /*
2  * Copyright (C) 2022 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "le_audio_broadcast_assistant.c"
39 
40 /*
41  * LE Audio Broadcast Assistant
42  */
43 
44 
45 #include "btstack_config.h"
46 
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <inttypes.h>
52 
53 #include "ad_parser.h"
54 #include "ble/sm.h"
55 #include "bluetooth_data_types.h"
56 #include "bluetooth_gatt.h"
57 #include "btstack_audio.h"
58 #include "btstack_debug.h"
59 #include "btstack_event.h"
60 #include "btstack_lc3.h"
61 #include "btstack_run_loop.h"
62 #include "btstack_stdin.h"
63 #include "btstack_util.h"
64 #include "gap.h"
65 #include "hci.h"
66 #include "l2cap.h"
67 #include "le-audio/gatt-service/broadcast_audio_scan_service_client.h"
68 #include "le-audio/le_audio_base_parser.h"
69 
70 static void show_usage(void);
71 
72 static enum {
73     APP_W4_WORKING,
74     APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV,
75     APP_W4_PA_AND_BIG_INFO,
76     APP_W4_BIG_SYNC_ESTABLISHED,
77     APP_W4_SCAN_DELEGATOR_CONNECTION,
78     APP_IDLE
79 } app_state = APP_W4_WORKING;
80 
81 //
82 static btstack_packet_callback_registration_t hci_event_callback_registration;
83 static btstack_packet_callback_registration_t sm_event_callback_registration;
84 
85 static bool have_scan_delegator;
86 static bool have_broadcast_source;
87 static bool have_base;
88 static bool have_big_info;
89 static bool manual_mode;
90 
91 // broadcast sink info
92 static char broadcast_source_name[20];
93 static bd_addr_t broadcast_source;
94 static bd_addr_type_t broadcast_source_type;
95 static uint8_t broadcast_source_sid;
96 static uint32_t broadcast_id;
97 static uint16_t broadcast_source_pa_interval;
98 
99 // broadcast info
100 static hci_con_handle_t sync_handle;
101 static uint8_t          encryption;
102 static uint8_t          broadcast_code [] = {0x01, 0x02, 0x68, 0x05, 0x53, 0xF1, 0x41, 0x5A, 0xA2, 0x65, 0xBB, 0xAF, 0xC6, 0xEA, 0x03, 0xB8, };
103 static uint8_t          num_bis;
104 static uint32_t         bis_sync_mask;
105 static uint32_t         sampling_frequency_hz;
106 static btstack_lc3_frame_duration_t frame_duration;
107 static uint16_t octets_per_frame;
108 
109 // scan delegator info
110 static char scan_delegator_name[20];
111 static bd_addr_t scan_delegator;
112 static bd_addr_type_t scan_delegator_type;
113 static hci_con_handle_t scan_delegator_handle;
114 
115 // BASS
116 #define BASS_CLIENT_NUM_SOURCES 1
117 static bass_client_connection_t bass_connection;
118 static bass_client_source_t bass_sources[BASS_CLIENT_NUM_SOURCES];
119 static bass_source_data_t bass_source_data;
120 static uint16_t bass_cid;
121 static uint8_t  bass_source_id;
122 
123 // test device name
124 static const char test_device_name[] = "OnePlus Buds Pro 2";
125 
126 static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
127 
128     // periodic advertisement contains the BASE
129     // TODO: BASE might be split across multiple advertisements
130     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
131     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
132     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
133 
134     if (adv_status != 0) {
135         printf("Periodic Advertisement (status %u): ", adv_status);
136         printf_hexdump(adv_data, adv_size);
137         return;
138     }
139 
140     le_audio_base_parser_t parser;
141     bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size);
142     if (ok == false){
143         return;
144     }
145     have_base = true;
146 
147     printf("BASE:\n");
148     uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser);
149     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
150     uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser);
151     // Cache in new source struct
152     bass_source_data.subgroups_num = num_subgroups;
153     printf("- num subgroups: %u\n", num_subgroups);
154     uint8_t i;
155     for (i=0;i<num_subgroups;i++){
156 
157         // Cache in new source struct
158         bass_source_data.subgroups[i].bis_sync = 0;
159 
160         // Level 2: Subgroup Level
161         num_bis = le_audio_base_parser_subgroup_get_num_bis(&parser);
162         printf("  - num bis[%u]: %u\n", i, num_bis);
163         uint8_t codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
164         const uint8_t * codec_specific_configuration = le_audio_base_parser_subgroup_get_codec_specific_configuration(&parser);
165         printf("  - codec specific config[%u]: ", i);
166         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
167         // parse config to get sampling frequency and frame duration
168         uint8_t codec_offset = 0;
169         while ((codec_offset + 1) < codec_specific_configuration_length){
170             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
171             uint8_t ltv_type = codec_specific_configuration[codec_offset];
172             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
173             uint8_t sampling_frequency_index;
174             uint8_t frame_duration_index;
175             switch (ltv_type){
176                 case 0x01: // sampling frequency
177                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
178                     // TODO: check range
179                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
180                     printf("    - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
181                     break;
182                 case 0x02: // 0 = 7.5, 1 = 10 ms
183                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
184                     frame_duration = (frame_duration_index == 0) ? BTSTACK_LC3_FRAME_DURATION_7500US : BTSTACK_LC3_FRAME_DURATION_10000US;
185                     printf("    - frame duration[%u]: %s ms\n", i, (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? "7.5" : "10");
186                     break;
187                 case 0x04:  // octets per coding frame
188                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
189                     printf("    - octets per codec frame[%u]: %u\n", i, octets_per_frame);
190                     break;
191                 default:
192                     break;
193             }
194             codec_offset += ltv_len;
195         }
196         uint8_t metadata_length = le_audio_base_parser_subgroup_get_metadata_length(&parser);
197         const uint8_t * meta_data = le_audio_base_subgroup_parser_get_metadata(&parser);
198         printf("  - meta data[%u]: ", i);
199         printf_hexdump(meta_data, metadata_length);
200         uint8_t k;
201         for (k=0;k<num_bis;k++){
202             // Level 3: BIS Level
203             uint8_t bis_index = le_audio_base_parser_bis_get_index(&parser);
204 
205             // check value
206             // double check message
207 
208             // Cache in new source struct
209             bis_sync_mask |= 1 << (bis_index-1);
210 
211             bass_source_data.subgroups[i].metadata_length = 0;
212             memset(&bass_source_data.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
213 
214             printf("    - bis index[%u][%u]: %u\n", i, k, bis_index);
215             codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser);
216             codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser);
217             printf("    - codec specific config[%u][%u]: ", i, k);
218             printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
219 
220             // parse next BIS
221             le_audio_base_parser_bis_next(&parser);
222         }
223 
224         // parse next subgroup
225         le_audio_base_parser_subgroup_next(&parser);
226     }
227 }
228 
229 static void start_scanning() {
230     app_state = APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV;
231     have_base = false;
232     have_big_info = false;
233     gap_set_scan_params(1, 0x30, 0x30, 0);
234     gap_start_scan();
235     printf("Start scan..\n");
236 }
237 
238 static void have_base_and_big_info(void){
239     printf("Connecting to Scan Delegator/Sink!\n");
240 
241     // todo: connect to scan delegator
242     app_state = APP_W4_SCAN_DELEGATOR_CONNECTION;
243     gap_connect(scan_delegator, scan_delegator_type);
244 }
245 
246 static void handle_big_info(const uint8_t * packet, uint16_t size){
247     printf("BIG Info advertising report\n");
248     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
249     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
250     if (encryption) {
251         printf("Stream is encrypted\n");
252     }
253     have_big_info = true;
254 }
255 
256 static void add_source() {// setup bass source info
257     printf("BASS Client: add source with BIS Sync 0x%04x\n", bass_source_data.subgroups[0].bis_sync_state);
258     bass_source_data.address_type = broadcast_source_type;
259     memcpy(bass_source_data.address, broadcast_source, 6);
260     bass_source_data.adv_sid = broadcast_source_sid;
261     bass_source_data.broadcast_id = broadcast_id;
262     bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
263     bass_source_data.pa_interval = broadcast_source_pa_interval;
264     // bass_source_new.subgroups_num set in BASE parser
265     // bass_source_new.subgroup[i].* set in BASE parser
266 
267     // add bass source
268     broadcast_audio_scan_service_client_add_source(bass_cid, &bass_source_data);
269 }
270 
271 static void bass_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
272     UNUSED(channel);
273     UNUSED(size);
274 
275     if (packet_type != HCI_EVENT_PACKET) return;
276     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) return;
277 
278     const bass_source_data_t * source_data;
279 
280     switch (hci_event_gattservice_meta_get_subevent_code(packet)) {
281         case GATTSERVICE_SUBEVENT_BASS_CLIENT_CONNECTED:
282             if (gattservice_subevent_bass_client_connected_get_status(packet) != ERROR_CODE_SUCCESS){
283                 printf("BASS client connection failed, cid 0x%02x, con_handle 0x%02x, status 0x%02x\n",
284                        bass_cid, scan_delegator_handle,
285                        gattservice_subevent_bass_client_connected_get_status(packet));
286                 return;
287             }
288             printf("BASS client connected, cid 0x%02x\n", bass_cid);
289 
290             if ((have_big_info == false) || (have_base == false)) break;
291             if (manual_mode) return;
292 
293             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
294             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
295             add_source();
296             break;
297         case GATTSERVICE_SUBEVENT_BASS_CLIENT_SOURCE_OPERATION_COMPLETE:
298             if (gattservice_subevent_bass_client_source_operation_complete_get_status(packet) != ERROR_CODE_SUCCESS){
299                 printf("BASS client source operation failed, status 0x%02x\n", gattservice_subevent_bass_client_source_operation_complete_get_status(packet));
300                 return;
301             }
302 
303             if ( gattservice_subevent_bass_client_source_operation_complete_get_opcode(packet) == (uint8_t)BASS_OPCODE_ADD_SOURCE ){
304                 // TODO: set state to 'wait for source_id"
305                 printf("BASS client add source operation completed, wait for source_id\n");
306             }
307             break;
308         case GATTSERVICE_SUBEVENT_BASS_CLIENT_NOTIFICATION_COMPLETE:
309             // store source_id
310             bass_source_id = gattservice_subevent_bass_client_notification_complete_get_source_id(packet);
311             printf("BASS client notification, source_id = 0x%02x\n", bass_source_id);
312             source_data = broadcast_audio_scan_service_client_get_source_data(bass_cid, bass_source_id);
313             btstack_assert(source_data != NULL);
314 
315             switch (source_data->pa_sync_state){
316                 case LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST:
317                     // start pa sync transfer
318                     printf("LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST -> Start PAST\n");
319                      // TODO: unclear why this needs to be shifted for PAST with TS to get test green
320                     uint16_t service_data = bass_source_id << 8;
321                     gap_periodic_advertising_sync_transfer_send(scan_delegator_handle, service_data, sync_handle);
322                     break;
323                 default:
324                     break;
325             }
326 
327             // check if Broadcast Code is requested
328             if (manual_mode == false){
329                 le_audio_big_encryption_t big_encryption;
330                 uint8_t bad_code[16];
331                 broadcast_audio_scan_service_client_get_encryption_state(bass_cid, bass_source_id, &big_encryption, bad_code);
332                 if (big_encryption == LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED){
333                     printf("LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED -> send Broadcast Code\n");
334                     broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
335                 }
336             }
337             break;
338         default:
339             break;
340     }
341 }
342 
343 static void le_audio_broadcast_assistant_start_periodic_sync() {
344     printf("Start Periodic Advertising Sync\n");
345 
346     // ignore other advertisements
347     gap_set_scan_params(1, 0x30, 0x30, 1);
348     // sync to PA
349     gap_periodic_advertiser_list_clear();
350     gap_periodic_advertiser_list_add(broadcast_source_type, broadcast_source, broadcast_source_sid);
351     app_state = APP_W4_PA_AND_BIG_INFO;
352     gap_periodic_advertising_create_sync(0x01, broadcast_source_sid, broadcast_source_type, broadcast_source, 0, 1000, 0);
353 }
354 
355 static void
356 le_audio_broadcast_assistant_found_scan_delegator(bd_addr_type_t *addr_type, const uint8_t *addr,
357                                                   const char *adv_name) {
358     memcpy(scan_delegator, addr, 6);
359     scan_delegator_type = (*addr_type);
360     btstack_strcpy(scan_delegator_name,  sizeof(scan_delegator_name), (const char *) adv_name);
361     printf("Broadcast sink found, addr %s, name: '%s'\n", bd_addr_to_str(scan_delegator), scan_delegator_name);
362     gap_whitelist_add(scan_delegator_type, scan_delegator);
363 }
364 
365 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
366     UNUSED(channel);
367     if (packet_type != HCI_EVENT_PACKET) return;
368     switch (packet[0]) {
369         case BTSTACK_EVENT_STATE:
370             switch(btstack_event_state_get_state(packet)) {
371                 case HCI_STATE_WORKING:
372                     app_state = APP_IDLE;
373 #ifdef ENABLE_DEMO_MODE
374                     start_scanning();
375 #else
376                     show_usage();
377 #endif
378                     break;
379                 case HCI_STATE_OFF:
380                     printf("Goodbye\n");
381                     exit(0);
382                     break;
383                 default:
384                     break;
385             }
386             break;
387         case GAP_EVENT_ADVERTISING_REPORT:{
388             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
389 
390             uint8_t adv_size = gap_event_advertising_report_get_data_length(packet);
391             const uint8_t * adv_data = gap_event_advertising_report_get_data(packet);
392 
393             ad_context_t context;
394             bool found_scan_delegator = false;
395             char adv_name[31];
396             adv_name[0] = 0;
397             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
398                 uint8_t data_type = ad_iterator_get_data_type(&context);
399                 uint8_t size = ad_iterator_get_data_len(&context);
400                 const uint8_t *data = ad_iterator_get_data(&context);
401                 switch (data_type){
402                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
403                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
404                         size = btstack_min(sizeof(adv_name) - 1, size);
405                         memcpy(adv_name, data, size);
406                         adv_name[size] = 0;
407                         // detect scan delegator by name
408                         if (strncmp(adv_name, test_device_name, sizeof(test_device_name)) == 0){
409                             found_scan_delegator = true;
410                         }
411                         break;
412                     default:
413                         break;
414                 }
415             }
416 
417             if (found_scan_delegator == false) break;
418 
419             if ((have_scan_delegator == false) && found_scan_delegator){
420                 have_scan_delegator = true;
421                 bd_addr_t addr;
422                 gap_event_advertising_report_get_address(packet, addr);
423                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_advertising_report_get_address_type(packet);
424                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
425             }
426 
427             if ((have_broadcast_source && have_scan_delegator) == false) break;
428 
429             le_audio_broadcast_assistant_start_periodic_sync();
430             break;
431         }
432         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
433         {
434             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
435 
436             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
437             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
438 
439             ad_context_t context;
440             bool found_scan_delegator = false;
441             bool found_broadcast_source = false;
442             char adv_name[31];
443             adv_name[0] = 0;
444 
445             uint16_t uuid;
446             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
447                 uint8_t data_type = ad_iterator_get_data_type(&context);
448                 uint8_t size = ad_iterator_get_data_len(&context);
449                 const uint8_t *data = ad_iterator_get_data(&context);
450                 switch (data_type){
451                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
452                         uuid = little_endian_read_16(data, 0);
453                         switch (uuid){
454                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE:
455                                 broadcast_id = little_endian_read_24(data, 2);
456                                 found_broadcast_source = true;
457                                 break;
458                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE:
459                                 found_scan_delegator = true;
460                                 break;
461                             default:
462                                 break;
463                         }
464                         break;
465                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
466                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
467                         size = btstack_min(sizeof(adv_name) - 1, size);
468                         memcpy(adv_name, data, size);
469                         adv_name[size] = 0;
470                         break;
471                     default:
472                         break;
473                 }
474             }
475 
476             if ((found_scan_delegator == false) && (found_broadcast_source == false)) break;
477 
478             if ((have_scan_delegator == false) && found_scan_delegator){
479                 have_scan_delegator = true;
480                 bd_addr_t addr;
481                 gap_event_extended_advertising_report_get_address(packet, addr);
482                 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_extended_advertising_report_get_address_type(packet);
483                 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name);
484             }
485 
486             if ((have_broadcast_source == false) && found_broadcast_source){
487                 have_broadcast_source = true;
488                 gap_event_extended_advertising_report_get_address(packet, broadcast_source);
489                 broadcast_source_type = gap_event_extended_advertising_report_get_address_type(packet);
490                 broadcast_source_sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
491                 btstack_strcpy(broadcast_source_name,  sizeof(broadcast_source_name), (const char *) adv_name);
492                 printf("Broadcast source found, addr %s, name: '%s'\n", bd_addr_to_str(broadcast_source), broadcast_source_name);
493                 gap_whitelist_add(broadcast_source_type, broadcast_source);
494             }
495 
496             if ((have_broadcast_source && have_scan_delegator) == false) break;
497 
498             le_audio_broadcast_assistant_start_periodic_sync();
499             break;
500         }
501 
502         case HCI_EVENT_LE_META:
503             switch(hci_event_le_meta_get_subevent_code(packet)) {
504                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
505                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
506                     broadcast_source_pa_interval = hci_subevent_le_periodic_advertising_sync_establishment_get_periodic_advertising_interval(packet);
507                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
508                     break;
509                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
510                     if (have_base) break;
511                     handle_periodic_advertisement(packet, size);
512                     if (have_base & have_big_info){
513                         have_base_and_big_info();
514                     }
515                     break;
516                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
517                     if (have_big_info) break;
518                     handle_big_info(packet, size);
519                     if (have_base & have_big_info){
520                         have_base_and_big_info();
521                     }
522                     break;
523                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
524                     printf("BIG Sync Lost\n");
525                     {
526                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
527                         if (sink != NULL) {
528                             sink->stop_stream();
529                             sink->close();
530                         }
531                     }
532                     // start over
533                     start_scanning();
534                     break;
535                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
536                     if (hci_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break;
537                     scan_delegator_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
538                     printf("Connection complete, handle 0x%04x\n", scan_delegator_handle);
539                     broadcast_audio_scan_service_client_connect(&bass_connection,
540                                                                 bass_sources,
541                                                                 BASS_CLIENT_NUM_SOURCES,
542                                                                 scan_delegator_handle,
543                                                                 &bass_cid);
544                     break;
545                 default:
546                     break;
547             }
548             break;
549         case SM_EVENT_JUST_WORKS_REQUEST:
550             printf("Just Works requested\n");
551             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
552             break;
553         default:
554             break;
555     }
556 }
557 
558 static void show_usage(void){
559     printf("\n--- LE Audio Broadcast Assistant Test Console ---\n");
560     printf("s - setup LE Broadcast Sink with Broadcast Source via Scan Delegator\n");
561     printf("c - scan and connect to Scan Delegator\n");
562     printf("a - add source with BIS Sync 0x%08x\n", bis_sync_mask);
563     printf("A - add source with BIS Sync 0x00000000 (do not sync)\n");
564     printf("m - modify source to PA Sync = 0, bis sync = 0x00000000\n");
565     printf("b - send Broadcast Code: ");
566     printf_hexdump(broadcast_code, sizeof(broadcast_code));
567     printf("r - remove source\n");
568     printf("---\n");
569 }
570 
571 static void stdin_process(char c){
572     switch (c){
573         case 's':
574             if (app_state != APP_IDLE) break;
575             manual_mode = false;
576             start_scanning();
577             break;
578         case 'c':
579             manual_mode = true;
580             start_scanning();
581             break;
582         case 'a':
583             bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask;
584             bass_source_data.subgroups[0].bis_sync       = bis_sync_mask;
585             add_source();
586             break;
587         case 'A':
588             bass_source_data.subgroups[0].bis_sync_state = 0;
589             bass_source_data.subgroups[0].bis_sync       = 0;
590             add_source();
591             break;
592         case 'm':
593             bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA;
594             bass_source_data.subgroups[0].bis_sync_state = 0;
595             broadcast_audio_scan_service_client_modify_source(bass_cid, bass_source_id, &bass_source_data);
596             break;
597         case 'r':
598             broadcast_audio_scan_service_client_remove_source(bass_cid, bass_source_id);
599             break;
600         case 'b':
601             broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code);
602             break;
603         case '\n':
604         case '\r':
605             break;
606         default:
607             show_usage();
608             break;
609 
610     }
611 }
612 
613 int btstack_main(int argc, const char * argv[]);
614 int btstack_main(int argc, const char * argv[]){
615     (void) argv;
616     (void) argc;
617 
618     l2cap_init();
619     sm_init();
620     gatt_client_init();
621 
622     // register for HCI events
623     hci_event_callback_registration.callback = &packet_handler;
624     hci_add_event_handler(&hci_event_callback_registration);
625 
626     // register for SM events
627     sm_event_callback_registration.callback = &packet_handler;
628     sm_add_event_handler(&sm_event_callback_registration);
629 
630     broadcast_audio_scan_service_client_init(&bass_packet_handler);
631 
632     // turn on!
633     hci_power_control(HCI_POWER_ON);
634 
635     btstack_stdin_setup(stdin_process);
636     return 0;
637 }
638