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