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