xref: /btstack/test/le_audio/le_audio_broadcast_assistant.c (revision 6b11d590a414522259c899e43eb3db94ccf35719)
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/gatt-service/broadcast_audio_scan_service_client.h"
55 #include "ble/sm.h"
56 #include "bluetooth_data_types.h"
57 #include "bluetooth_gatt.h"
58 #include "btstack_audio.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 
68 static void show_usage(void);
69 
70 static enum {
71     APP_W4_WORKING,
72     APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV,
73     APP_W4_PA_AND_BIG_INFO,
74     APP_W4_BIG_SYNC_ESTABLISHED,
75     APP_W4_SCAN_DELEGATOR_CONNECTION,
76     APP_IDLE
77 } app_state = APP_W4_WORKING;
78 
79 //
80 static btstack_packet_callback_registration_t hci_event_callback_registration;
81 static btstack_packet_callback_registration_t sm_event_callback_registration;
82 
83 static bool have_scan_delegator;
84 static bool have_broadcast_source;
85 static bool have_base;
86 static bool have_big_info;
87 static bool manual_mode;
88 
89 // broadcast sink info
90 static char broadcast_source_name[20];
91 static bd_addr_t broadcast_source;
92 static bd_addr_type_t broadcast_source_type;
93 static uint8_t broadcast_source_sid;
94 static uint32_t broadcast_id;
95 static uint16_t broadcast_source_pa_interval;
96 
97 // broadcast info
98 static hci_con_handle_t sync_handle;
99 static uint8_t          encryption;
100 static uint8_t          broadcast_code [] = {0x01, 0x02, 0x68, 0x05, 0x53, 0xF1, 0x41, 0x5A, 0xA2, 0x65, 0xBB, 0xAF, 0xC6, 0xEA, 0x03, 0xB8, };
101 static uint8_t          num_bis;
102 static uint32_t         bis_sync_mask;
103 static uint32_t         sampling_frequency_hz;
104 static btstack_lc3_frame_duration_t frame_duration;
105 static uint16_t octets_per_frame;
106 
107 // scan delegator info
108 static char scan_delegator_name[20];
109 static bd_addr_t scan_delegator;
110 static bd_addr_type_t scan_delegator_type;
111 static hci_con_handle_t scan_delegator_handle;
112 
113 // BASS
114 #define BASS_CLIENT_NUM_SOURCES 1
115 static bass_client_connection_t bass_connection;
116 static bass_client_source_t bass_sources[BASS_CLIENT_NUM_SOURCES];
117 static bass_source_data_t bass_source_new;
118 static uint16_t bass_cid;
119 static uint8_t  bass_source_id;
120 
121 static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){
122 
123     // periodic advertisement contains the BASE
124     // TODO: BASE might be split across multiple advertisements
125     const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet);
126     uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet);
127     uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet);
128 
129     if (adv_status != 0) {
130         printf("Periodic Advertisement (status %u): ", adv_status);
131         printf_hexdump(adv_data, adv_size);
132         return;
133     }
134 
135     ad_context_t context;
136     for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
137         uint8_t data_type = ad_iterator_get_data_type(&context);
138         // TODO: avoid out-of-bounds read
139         // uint8_t data_size = ad_iterator_get_data_len(&context);
140         const uint8_t * data = ad_iterator_get_data(&context);
141         uint16_t uuid;
142         switch (data_type){
143             case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
144                 uuid = little_endian_read_16(data, 0);
145                 if (uuid == ORG_BLUETOOTH_SERVICE_BASIC_AUDIO_ANNOUNCEMENT_SERVICE){
146                     have_base = true;
147                     // Level 1: Group Level
148                     const uint8_t * base_data = &data[2];
149                     // TODO: avoid out-of-bounds read
150                     // uint16_t base_len = data_size - 2;
151                     printf("BASE:\n");
152                     uint32_t presentation_delay = little_endian_read_24(base_data, 0);
153                     printf("- presentation delay: %"PRIu32" us\n", presentation_delay);
154                     uint8_t num_subgroups = base_data[3];
155                     // Cache in new source struct
156                     bass_source_new.subgroups_num = num_subgroups;
157                     printf("- num subgroups: %u\n", num_subgroups);
158                     uint8_t i;
159                     uint16_t offset = 4;
160                     for (i=0;i<num_subgroups;i++){
161 
162                         // Cache in new source struct
163                         bass_source_new.subgroups[i].bis_sync = 0;
164 
165                         // Level 2: Subgroup Level
166                         num_bis = base_data[offset++];
167                         printf("  - num bis[%u]: %u\n", i, num_bis);
168                         // codec_id: coding format = 0x06, vendor and coded id = 0
169                         offset += 5;
170                         uint8_t codec_specific_configuration_length = base_data[offset++];
171                         const uint8_t * codec_specific_configuration = &base_data[offset];
172                         printf("  - codec specific config[%u]: ", i);
173                         printf_hexdump(codec_specific_configuration, codec_specific_configuration_length);
174                         // parse config to get sampling frequency and frame duration
175                         uint8_t codec_offset = 0;
176                         while ((codec_offset + 1) < codec_specific_configuration_length){
177                             uint8_t ltv_len = codec_specific_configuration[codec_offset++];
178                             uint8_t ltv_type = codec_specific_configuration[codec_offset];
179                             const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 };
180                             uint8_t sampling_frequency_index;
181                             uint8_t frame_duration_index;
182                             switch (ltv_type){
183                                 case 0x01: // sampling frequency
184                                     sampling_frequency_index = codec_specific_configuration[codec_offset+1];
185                                     // TODO: check range
186                                     sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1];
187                                     printf("    - sampling frequency[%u]: %u\n", i, sampling_frequency_hz);
188                                     break;
189                                 case 0x02: // 0 = 7.5, 1 = 10 ms
190                                     frame_duration_index =  codec_specific_configuration[codec_offset+1];
191                                     frame_duration = (frame_duration_index == 0) ? BTSTACK_LC3_FRAME_DURATION_7500US : BTSTACK_LC3_FRAME_DURATION_10000US;
192                                     printf("    - frame duration[%u]: %s ms\n", i, (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? "7.5" : "10");
193                                     break;
194                                 case 0x04:  // octets per coding frame
195                                     octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1);
196                                     printf("    - octets per codec frame[%u]: %u\n", i, octets_per_frame);
197                                     break;
198                                 default:
199                                     break;
200                             }
201                             codec_offset += ltv_len;
202                         }
203                         //
204                         offset += codec_specific_configuration_length;
205                         uint8_t metadata_length = base_data[offset++];
206                         const uint8_t * meta_data = &base_data[offset];
207                         offset += metadata_length;
208                         printf("  - meta data[%u]: ", i);
209                         printf_hexdump(meta_data, metadata_length);
210                         uint8_t k;
211                         for (k=0;k<num_bis;k++){
212                             // Level 3: BIS Level
213                             uint8_t bis_index = base_data[offset++];
214 
215                             // check value
216                             // double check message
217 
218                             // Cache in new source struct
219                             bis_sync_mask |= 1 << (bis_index-1);
220 
221                             bass_source_new.subgroups[i].metadata_length = 0;
222                             memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t));
223 
224                             printf("    - bis index[%u][%u]: %u\n", i, k, bis_index);
225                             uint8_t codec_specific_configuration_length2 = base_data[offset++];
226                             const uint8_t * codec_specific_configuration2 = &base_data[offset];
227                             printf("    - codec specific config[%u][%u]: ", i, k);
228                             printf_hexdump(codec_specific_configuration2, codec_specific_configuration_length2);
229                             offset += codec_specific_configuration_length2;
230                         }
231                     }
232                 }
233                 break;
234             default:
235                 break;
236         }
237     }
238 }
239 
240 static void start_scanning() {
241     app_state = APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV;
242     have_base = false;
243     have_big_info = false;
244     gap_set_scan_params(1, 0x30, 0x30, 0);
245     gap_start_scan();
246     printf("Start scan..\n");
247 }
248 
249 static void have_base_and_big_info(void){
250     printf("Connecting to Scan Delegator/Sink!\n");
251 
252     // todo: connect to scan delegator
253     app_state = APP_W4_SCAN_DELEGATOR_CONNECTION;
254     gap_connect(scan_delegator, scan_delegator_type);
255 }
256 
257 static void handle_big_info(const uint8_t * packet, uint16_t size){
258     printf("BIG Info advertising report\n");
259     sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet);
260     encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet);
261     if (encryption) {
262         printf("Stream is encrypted\n");
263     }
264     have_big_info = true;
265 }
266 
267 static void add_source() {// setup bass source info
268     printf("BASS Client: add source with BIS Sync 0x%04x\n", bass_source_new.subgroups[0].bis_sync_state);
269     bass_source_new.address_type = broadcast_source_type;
270     memcpy(bass_source_new.address, broadcast_source, 6);
271     bass_source_new.adv_sid = broadcast_source_sid;
272     bass_source_new.broadcast_id = broadcast_id;
273     bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE;
274     bass_source_new.pa_interval = broadcast_source_pa_interval;
275     // bass_source_new.subgroups_num set in BASE parser
276     // bass_source_new.subgroup[i].* set in BASE parser
277 
278     // add bass source
279     broadcast_audio_scan_service_client_add_source(bass_cid, &bass_source_new);
280 }
281 
282 static void bass_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
283     UNUSED(channel);
284     UNUSED(size);
285 
286     if (packet_type != HCI_EVENT_PACKET) return;
287     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) return;
288 
289     switch (hci_event_gattservice_meta_get_subevent_code(packet)) {
290         case GATTSERVICE_SUBEVENT_BASS_CONNECTED:
291             if (gattservice_subevent_bass_connected_get_status(packet) != ERROR_CODE_SUCCESS){
292                 printf("BASS client connection failed, cid 0x%02x, con_handle 0x%02x, status 0x%02x\n",
293                        bass_cid, scan_delegator_handle,
294                        gattservice_subevent_bass_connected_get_status(packet));
295                 return;
296             }
297             printf("BASS client connected, cid 0x%02x\n", bass_cid);
298 
299             if ((have_big_info == false) || (have_base == false)) break;
300             if (manual_mode) return;
301 
302             bass_source_new.subgroups[0].bis_sync_state = bis_sync_mask;
303             bass_source_new.subgroups[0].bis_sync       = bis_sync_mask;
304             add_source();
305             break;
306         case GATTSERVICE_SUBEVENT_BASS_SOURCE_OPERATION_COMPLETE:
307             if (gattservice_subevent_bass_source_operation_complete_get_status(packet) != ERROR_CODE_SUCCESS){
308                 printf("BASS client source operation failed, status 0x%02x\n", gattservice_subevent_bass_source_operation_complete_get_status(packet));
309                 return;
310             }
311 
312             if ( gattservice_subevent_bass_source_operation_complete_get_opcode(packet) == (uint8_t)BASS_OPCODE_ADD_SOURCE ){
313                 // TODO: set state to 'wait for source_id"
314                 printf("BASS client add source operation completed, wait for source_id\n");
315             }
316             break;
317         case GATTSERVICE_SUBEVENT_BASS_NOTIFICATION_COMPLETE:
318             // store source_id
319             bass_source_id = gattservice_subevent_bass_notification_complete_get_source_id(packet);
320             printf("BASS client notification, source_id = 0x%02x\n", bass_source_id);
321 
322             // note: should be pa_sync_state field
323             if (bass_sources[0].data.pa_sync == LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST){
324                 // start pa sync
325                 printf("LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST -> Start PAST\n");
326                 uint16_t service_data = 0x100; // bass_source_id;
327                 gap_periodic_advertising_sync_transfer_send(scan_delegator_handle, service_data, sync_handle);
328             }
329             break;
330         default:
331             break;
332     }
333 }
334 
335 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
336     UNUSED(channel);
337     if (packet_type != HCI_EVENT_PACKET) return;
338     switch (packet[0]) {
339         case BTSTACK_EVENT_STATE:
340             switch(btstack_event_state_get_state(packet)) {
341                 case HCI_STATE_WORKING:
342                     app_state = APP_IDLE;
343 #ifdef ENABLE_DEMO_MODE
344                     start_scanning();
345 #else
346                     show_usage();
347 #endif
348                     break;
349                 case HCI_STATE_OFF:
350                     printf("Goodbye\n");
351                     exit(0);
352                     break;
353                 default:
354                     break;
355             }
356             break;
357         case GAP_EVENT_EXTENDED_ADVERTISING_REPORT:
358         {
359             if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break;
360 
361             uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet);
362             const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet);
363 
364             ad_context_t context;
365             bool found_scan_delegator = false;
366             bool found_broadcast_source = false;
367             bool found_name = false;
368             uint8_t adv_name[31];
369 
370             uint16_t uuid;
371             for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
372                 uint8_t data_type = ad_iterator_get_data_type(&context);
373                 uint8_t size = ad_iterator_get_data_len(&context);
374                 const uint8_t *data = ad_iterator_get_data(&context);
375                 switch (data_type){
376                     case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID:
377                         uuid = little_endian_read_16(data, 0);
378                         switch (uuid){
379                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE:
380                                 broadcast_id = little_endian_read_24(data, 2);
381                                 found_broadcast_source = true;
382                                 break;
383                             case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE:
384                                 found_scan_delegator = true;
385                                 break;
386                             default:
387                                 break;
388                         }
389                         break;
390                     case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
391                     case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
392                         size = btstack_min(sizeof(adv_name) - 1, size);
393                         memcpy(adv_name, data, size);
394                         adv_name[size] = 0;
395                         found_name = true;
396                         break;
397                     default:
398                         break;
399                 }
400             }
401 
402             if ((found_scan_delegator == false) && (found_broadcast_source == false)) break;
403 
404             if ((have_scan_delegator == false) && found_scan_delegator){
405                 have_scan_delegator = true;
406                 gap_event_extended_advertising_report_get_address(packet, scan_delegator);
407                 scan_delegator_type = gap_event_extended_advertising_report_get_address_type(packet);
408                 if (found_name){
409                     btstack_strcpy(scan_delegator_name,  sizeof(scan_delegator_name), (const char *) adv_name);
410                 } else {
411                     scan_delegator_name[0] = 0;
412                 }
413                 printf("Broadcast sink found, addr %s, name: '%s'\n", bd_addr_to_str(scan_delegator), scan_delegator_name);
414                 gap_whitelist_add(scan_delegator_type, scan_delegator);
415             }
416 
417             if ((have_broadcast_source == false) && found_broadcast_source){
418                 have_broadcast_source = true;
419                 gap_event_extended_advertising_report_get_address(packet, broadcast_source);
420                 broadcast_source_type = gap_event_extended_advertising_report_get_address_type(packet);
421                 broadcast_source_sid = gap_event_extended_advertising_report_get_advertising_sid(packet);
422                 if (found_name){
423                     btstack_strcpy(broadcast_source_name,  sizeof(broadcast_source_name), (const char *) adv_name);
424                 } else {
425                     broadcast_source_name[0] = 0;
426                 }
427                 printf("Broadcast source found, addr %s, name: '%s'\n", bd_addr_to_str(broadcast_source), broadcast_source_name);
428                 gap_whitelist_add(broadcast_source_type, broadcast_source);
429             }
430 
431              if ((have_broadcast_source && have_scan_delegator) == false) break;
432 
433             printf("Start Periodic Advertising Sync\n");
434 
435             // ignore other advertisements
436             gap_set_scan_params(1, 0x30, 0x30, 1);
437             // sync to PA
438             gap_periodic_advertiser_list_clear();
439             gap_periodic_advertiser_list_add(broadcast_source_type, broadcast_source, broadcast_source_sid);
440             app_state = APP_W4_PA_AND_BIG_INFO;
441             gap_periodic_advertising_create_sync(0x01, broadcast_source_sid, broadcast_source_type, broadcast_source, 0, 1000, 0);
442             break;
443         }
444 
445         case HCI_EVENT_LE_META:
446             switch(hci_event_le_meta_get_subevent_code(packet)) {
447                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT:
448                     sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet);
449                     broadcast_source_pa_interval = hci_subevent_le_periodic_advertising_sync_establishment_get_periodic_advertising_interval(packet);
450                     printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle);
451                     break;
452                 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT:
453                     if (have_base) break;
454                     handle_periodic_advertisement(packet, size);
455                     if (have_base & have_big_info){
456                         have_base_and_big_info();
457                     }
458                     break;
459                 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT:
460                     if (have_big_info) break;
461                     handle_big_info(packet, size);
462                     if (have_base & have_big_info){
463                         have_base_and_big_info();
464                     }
465                     break;
466                 case HCI_SUBEVENT_LE_BIG_SYNC_LOST:
467                     printf("BIG Sync Lost\n");
468                     {
469                         const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
470                         if (sink != NULL) {
471                             sink->stop_stream();
472                             sink->close();
473                         }
474                     }
475                     // start over
476                     start_scanning();
477                     break;
478                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
479                     if (hci_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break;
480                     scan_delegator_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
481                     printf("Connection complete, handle 0x%04x\n", scan_delegator_handle);
482                     broadcast_audio_scan_service_client_connect(&bass_connection,
483                                                                 bass_sources,
484                                                                 BASS_CLIENT_NUM_SOURCES,
485                                                                 scan_delegator_handle,
486                                                                 &bass_cid);
487                     break;
488                 default:
489                     break;
490             }
491             break;
492         case SM_EVENT_JUST_WORKS_REQUEST:
493             printf("Just Works requested\n");
494             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
495             break;
496         default:
497             break;
498     }
499 }
500 
501 static void show_usage(void){
502     printf("\n--- LE Audio Broadcast Assistant Test Console ---\n");
503     printf("s - setup LE Broadcast Sink with Broadcast Source via Scan Delegator\n");
504     printf("c - scan and connect to Scan Delegator\n");
505     printf("a - add source with BIS Sync 0x%08x\n", bis_sync_mask);
506     printf("A - add source with BIS Sync 0x00000000 (do not sync)\n");
507     printf("m - modify source to PA Sync = 0, bis sync = 0x00000000\n");
508     printf("b - send Broadcast Code: ");
509     printf_hexdump(broadcast_code, sizeof(broadcast_code));
510     printf("r - remove source\n");
511     printf("---\n");
512 }
513 
514 static void stdin_process(char c){
515     switch (c){
516         case 's':
517             if (app_state != APP_IDLE) break;
518             manual_mode = false;
519             start_scanning();
520             break;
521         case 'c':
522             manual_mode = true;
523             start_scanning();
524             break;
525         case 'a':
526             bass_source_new.subgroups[0].bis_sync_state = bis_sync_mask;
527             bass_source_new.subgroups[0].bis_sync       = bis_sync_mask;
528             add_source();
529             break;
530         case 'A':
531             bass_source_new.subgroups[0].bis_sync_state = 0;
532             bass_source_new.subgroups[0].bis_sync       = 0;
533             add_source();
534             break;
535         case 'm':
536             memcpy(&bass_source_new, &bass_sources[0].data, sizeof(bass_source_data_t));
537             bass_source_new.pa_sync = LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA;
538             bass_source_new.subgroups[0].bis_sync_state = 0;
539             broadcast_audio_scan_service_client_modify_source(bass_cid, bass_source_id, &bass_source_new);
540             break;
541         case 'r':
542             broadcast_audio_scan_service_client_delete_source(bass_cid, bass_source_id);
543             break;
544         case 'b':
545             // send broadcast code
546             broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, &broadcast_code);
547             break;
548         case '\n':
549         case '\r':
550             break;
551         default:
552             show_usage();
553             break;
554 
555     }
556 }
557 
558 int btstack_main(int argc, const char * argv[]);
559 int btstack_main(int argc, const char * argv[]){
560     (void) argv;
561     (void) argc;
562 
563     l2cap_init();
564     sm_init();
565     gatt_client_init();
566 
567     // register for HCI events
568     hci_event_callback_registration.callback = &packet_handler;
569     hci_add_event_handler(&hci_event_callback_registration);
570 
571     // register for SM events
572     sm_event_callback_registration.callback = &packet_handler;
573     sm_add_event_handler(&sm_event_callback_registration);
574 
575     broadcast_audio_scan_service_client_init(&bass_packet_handler);
576 
577     // turn on!
578     hci_power_control(HCI_POWER_ON);
579 
580     btstack_stdin_setup(stdin_process);
581     return 0;
582 }
583