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 // todo: connect to scan delegator 243 app_state = APP_W4_SCAN_DELEGATOR_CONNECTION; 244 gap_connect(scan_delegator, scan_delegator_type); 245 } 246 247 static void handle_big_info(const uint8_t * packet, uint16_t size){ 248 printf("BIG Info advertising report\n"); 249 sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); 250 encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet); 251 if (encryption) { 252 printf("Stream is encrypted\n"); 253 } 254 have_big_info = true; 255 } 256 257 static void add_source() {// setup bass source info 258 printf("BASS Client: add source with BIS Sync 0x%04x\n", bass_source_data.subgroups[0].bis_sync_state); 259 bass_source_data.address_type = broadcast_source_type; 260 memcpy(bass_source_data.address, broadcast_source, 6); 261 bass_source_data.adv_sid = broadcast_source_sid; 262 bass_source_data.broadcast_id = broadcast_id; 263 bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE; 264 bass_source_data.pa_interval = broadcast_source_pa_interval; 265 // bass_source_new.subgroups_num set in BASE parser 266 // bass_source_new.subgroup[i].* set in BASE parser 267 268 // add bass source 269 broadcast_audio_scan_service_client_add_source(bass_cid, &bass_source_data); 270 } 271 272 static void bass_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 273 UNUSED(channel); 274 UNUSED(size); 275 276 if (packet_type != HCI_EVENT_PACKET) return; 277 if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) return; 278 279 const bass_source_data_t * source_data; 280 281 switch (hci_event_gattservice_meta_get_subevent_code(packet)) { 282 case GATTSERVICE_SUBEVENT_BASS_CLIENT_CONNECTED: 283 if (gattservice_subevent_bass_client_connected_get_status(packet) != ERROR_CODE_SUCCESS){ 284 printf("BASS client connection failed, cid 0x%02x, con_handle 0x%02x, status 0x%02x\n", 285 bass_cid, scan_delegator_handle, 286 gattservice_subevent_bass_client_connected_get_status(packet)); 287 return; 288 } 289 printf("BASS client connected, cid 0x%02x\n", bass_cid); 290 291 if ((have_big_info == false) || (have_base == false)) break; 292 if (manual_mode) return; 293 294 bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask; 295 bass_source_data.subgroups[0].bis_sync = bis_sync_mask; 296 add_source(); 297 break; 298 case GATTSERVICE_SUBEVENT_BASS_CLIENT_SOURCE_OPERATION_COMPLETE: 299 if (gattservice_subevent_bass_client_source_operation_complete_get_status(packet) != ERROR_CODE_SUCCESS){ 300 printf("BASS client source operation failed, status 0x%02x\n", gattservice_subevent_bass_client_source_operation_complete_get_status(packet)); 301 return; 302 } 303 304 if ( gattservice_subevent_bass_client_source_operation_complete_get_opcode(packet) == (uint8_t)BASS_OPCODE_ADD_SOURCE ){ 305 // TODO: set state to 'wait for source_id" 306 printf("BASS client add source operation completed, wait for source_id\n"); 307 } 308 break; 309 case GATTSERVICE_SUBEVENT_BASS_CLIENT_NOTIFICATION_COMPLETE: 310 // store source_id 311 bass_source_id = gattservice_subevent_bass_client_notification_complete_get_source_id(packet); 312 printf("BASS client notification, source_id = 0x%02x\n", bass_source_id); 313 source_data = broadcast_audio_scan_service_client_get_source_data(bass_cid, bass_source_id); 314 btstack_assert(source_data != NULL); 315 316 switch (source_data->pa_sync_state){ 317 case LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST: 318 // start pa sync transfer 319 printf("LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST -> Start PAST\n"); 320 // TODO: unclear why this needs to be shifted for PAST with TS to get test green 321 uint16_t service_data = bass_source_id << 8; 322 gap_periodic_advertising_sync_transfer_send(scan_delegator_handle, service_data, sync_handle); 323 break; 324 default: 325 break; 326 } 327 328 // check if Broadcast Code is requested 329 if (manual_mode == false){ 330 le_audio_big_encryption_t big_encryption; 331 uint8_t bad_code[16]; 332 broadcast_audio_scan_service_client_get_encryption_state(bass_cid, bass_source_id, &big_encryption, bad_code); 333 if (big_encryption == LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED){ 334 printf("LE_AUDIO_BIG_ENCRYPTION_BROADCAST_CODE_REQUIRED -> send Broadcast Code\n"); 335 broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code); 336 } 337 } 338 break; 339 default: 340 break; 341 } 342 } 343 344 static void le_audio_broadcast_assistant_start_periodic_sync() { 345 printf("Start Periodic Advertising Sync\n"); 346 347 // ignore other advertisements 348 gap_set_scan_params(1, 0x30, 0x30, 1); 349 // sync to PA 350 gap_periodic_advertiser_list_clear(); 351 gap_periodic_advertiser_list_add(broadcast_source_type, broadcast_source, broadcast_source_sid); 352 app_state = APP_W4_PA_AND_BIG_INFO; 353 gap_periodic_advertising_create_sync(0x01, broadcast_source_sid, broadcast_source_type, broadcast_source, 0, 1000, 0); 354 } 355 356 static void 357 le_audio_broadcast_assistant_found_scan_delegator(bd_addr_type_t *addr_type, const uint8_t *addr, 358 const char *adv_name) { 359 memcpy(scan_delegator, addr, 6); 360 scan_delegator_type = (*addr_type); 361 btstack_strcpy(scan_delegator_name, sizeof(scan_delegator_name), (const char *) adv_name); 362 printf("Broadcast sink found, addr %s, name: '%s'\n", bd_addr_to_str(scan_delegator), scan_delegator_name); 363 gap_whitelist_add(scan_delegator_type, scan_delegator); 364 } 365 366 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 367 UNUSED(channel); 368 if (packet_type != HCI_EVENT_PACKET) return; 369 switch (packet[0]) { 370 case BTSTACK_EVENT_STATE: 371 switch(btstack_event_state_get_state(packet)) { 372 case HCI_STATE_WORKING: 373 app_state = APP_IDLE; 374 #ifdef ENABLE_DEMO_MODE 375 start_scanning(); 376 #else 377 show_usage(); 378 #endif 379 break; 380 case HCI_STATE_OFF: 381 printf("Goodbye\n"); 382 exit(0); 383 break; 384 default: 385 break; 386 } 387 break; 388 case GAP_EVENT_ADVERTISING_REPORT:{ 389 if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break; 390 391 uint8_t adv_size = gap_event_advertising_report_get_data_length(packet); 392 const uint8_t * adv_data = gap_event_advertising_report_get_data(packet); 393 394 ad_context_t context; 395 bool found_scan_delegator = false; 396 char adv_name[31]; 397 adv_name[0] = 0; 398 for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 399 uint8_t data_type = ad_iterator_get_data_type(&context); 400 uint8_t size = ad_iterator_get_data_len(&context); 401 const uint8_t *data = ad_iterator_get_data(&context); 402 switch (data_type){ 403 case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 404 case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 405 size = btstack_min(sizeof(adv_name) - 1, size); 406 memcpy(adv_name, data, size); 407 adv_name[size] = 0; 408 // detect scan delegator by name 409 if (strncmp(adv_name, test_device_name, sizeof(test_device_name)) == 0){ 410 found_scan_delegator = true; 411 } 412 break; 413 default: 414 break; 415 } 416 } 417 418 if (found_scan_delegator == false) break; 419 420 if ((have_scan_delegator == false) && found_scan_delegator){ 421 have_scan_delegator = true; 422 bd_addr_t addr; 423 gap_event_advertising_report_get_address(packet, addr); 424 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_advertising_report_get_address_type(packet); 425 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name); 426 } 427 428 if ((have_broadcast_source && have_scan_delegator) == false) break; 429 430 le_audio_broadcast_assistant_start_periodic_sync(); 431 break; 432 } 433 case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 434 { 435 if (app_state != APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV) break; 436 437 uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 438 const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 439 440 ad_context_t context; 441 bool found_scan_delegator = false; 442 bool found_broadcast_source = false; 443 char adv_name[31]; 444 adv_name[0] = 0; 445 446 uint16_t uuid; 447 for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 448 uint8_t data_type = ad_iterator_get_data_type(&context); 449 uint8_t size = ad_iterator_get_data_len(&context); 450 const uint8_t *data = ad_iterator_get_data(&context); 451 switch (data_type){ 452 case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 453 uuid = little_endian_read_16(data, 0); 454 switch (uuid){ 455 case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE: 456 broadcast_id = little_endian_read_24(data, 2); 457 found_broadcast_source = true; 458 break; 459 case ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE: 460 found_scan_delegator = true; 461 break; 462 default: 463 break; 464 } 465 break; 466 case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 467 case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 468 size = btstack_min(sizeof(adv_name) - 1, size); 469 memcpy(adv_name, data, size); 470 adv_name[size] = 0; 471 break; 472 default: 473 break; 474 } 475 } 476 477 if ((found_scan_delegator == false) && (found_broadcast_source == false)) break; 478 479 if ((have_scan_delegator == false) && found_scan_delegator){ 480 have_scan_delegator = true; 481 bd_addr_t addr; 482 gap_event_extended_advertising_report_get_address(packet, addr); 483 bd_addr_type_t addr_type = (bd_addr_type_t) gap_event_extended_advertising_report_get_address_type(packet); 484 le_audio_broadcast_assistant_found_scan_delegator(&addr_type, addr, adv_name); 485 } 486 487 if ((have_broadcast_source == false) && found_broadcast_source){ 488 have_broadcast_source = true; 489 gap_event_extended_advertising_report_get_address(packet, broadcast_source); 490 broadcast_source_type = gap_event_extended_advertising_report_get_address_type(packet); 491 broadcast_source_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 492 btstack_strcpy(broadcast_source_name, sizeof(broadcast_source_name), (const char *) adv_name); 493 printf("Broadcast source found, addr %s, name: '%s'\n", bd_addr_to_str(broadcast_source), broadcast_source_name); 494 gap_whitelist_add(broadcast_source_type, broadcast_source); 495 } 496 497 if ((have_broadcast_source && have_scan_delegator) == false) break; 498 499 le_audio_broadcast_assistant_start_periodic_sync(); 500 break; 501 } 502 503 case HCI_EVENT_LE_META: 504 switch(hci_event_le_meta_get_subevent_code(packet)) { 505 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 506 sync_handle = hci_subevent_le_periodic_advertising_sync_establishment_get_sync_handle(packet); 507 broadcast_source_pa_interval = hci_subevent_le_periodic_advertising_sync_establishment_get_periodic_advertising_interval(packet); 508 printf("Periodic advertising sync with handle 0x%04x established\n", sync_handle); 509 break; 510 case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 511 if (have_base) break; 512 handle_periodic_advertisement(packet, size); 513 if (have_base & have_big_info){ 514 have_base_and_big_info(); 515 } 516 break; 517 case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 518 if (have_big_info) break; 519 handle_big_info(packet, size); 520 if (have_base & have_big_info){ 521 have_base_and_big_info(); 522 } 523 break; 524 case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 525 printf("BIG Sync Lost\n"); 526 { 527 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 528 if (sink != NULL) { 529 sink->stop_stream(); 530 sink->close(); 531 } 532 } 533 // start over 534 start_scanning(); 535 break; 536 default: 537 break; 538 } 539 break; 540 case HCI_EVENT_META_GAP: 541 switch (hci_event_gap_meta_get_subevent_code(packet)){ 542 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: 543 if (gap_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break; 544 scan_delegator_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 545 printf("Connection complete, handle 0x%04x\n", scan_delegator_handle); 546 broadcast_audio_scan_service_client_connect(&bass_connection, 547 bass_sources, 548 BASS_CLIENT_NUM_SOURCES, 549 scan_delegator_handle, 550 &bass_cid); 551 break; 552 default: 553 break; 554 } 555 break; 556 case SM_EVENT_JUST_WORKS_REQUEST: 557 printf("Just Works requested\n"); 558 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 559 break; 560 default: 561 break; 562 } 563 } 564 565 static void show_usage(void){ 566 printf("\n--- LE Audio Broadcast Assistant Test Console ---\n"); 567 printf("s - setup LE Broadcast Sink with Broadcast Source via Scan Delegator\n"); 568 printf("c - scan and connect to Scan Delegator\n"); 569 printf("a - add source with BIS Sync 0x%08x\n", bis_sync_mask); 570 printf("A - add source with BIS Sync 0x00000000 (do not sync)\n"); 571 printf("m - modify source to PA Sync = 0, bis sync = 0x00000000\n"); 572 printf("b - send Broadcast Code: "); 573 printf_hexdump(broadcast_code, sizeof(broadcast_code)); 574 printf("r - remove source\n"); 575 printf("---\n"); 576 } 577 578 static void stdin_process(char c){ 579 switch (c){ 580 case 's': 581 if (app_state != APP_IDLE) break; 582 manual_mode = false; 583 start_scanning(); 584 break; 585 case 'c': 586 manual_mode = true; 587 start_scanning(); 588 break; 589 case 'a': 590 bass_source_data.subgroups[0].bis_sync_state = bis_sync_mask; 591 bass_source_data.subgroups[0].bis_sync = bis_sync_mask; 592 add_source(); 593 break; 594 case 'A': 595 bass_source_data.subgroups[0].bis_sync_state = 0; 596 bass_source_data.subgroups[0].bis_sync = 0; 597 add_source(); 598 break; 599 case 'm': 600 bass_source_data.pa_sync = LE_AUDIO_PA_SYNC_DO_NOT_SYNCHRONIZE_TO_PA; 601 bass_source_data.subgroups[0].bis_sync_state = 0; 602 broadcast_audio_scan_service_client_modify_source(bass_cid, bass_source_id, &bass_source_data); 603 break; 604 case 'r': 605 broadcast_audio_scan_service_client_remove_source(bass_cid, bass_source_id); 606 break; 607 case 'b': 608 broadcast_audio_scan_service_client_set_broadcast_code(bass_cid, bass_source_id, broadcast_code); 609 break; 610 case '\n': 611 case '\r': 612 break; 613 default: 614 show_usage(); 615 break; 616 617 } 618 } 619 620 int btstack_main(int argc, const char * argv[]); 621 int btstack_main(int argc, const char * argv[]){ 622 (void) argv; 623 (void) argc; 624 625 l2cap_init(); 626 sm_init(); 627 gatt_client_init(); 628 629 // register for HCI events 630 hci_event_callback_registration.callback = &packet_handler; 631 hci_add_event_handler(&hci_event_callback_registration); 632 633 // register for SM events 634 sm_event_callback_registration.callback = &packet_handler; 635 sm_add_event_handler(&sm_event_callback_registration); 636 637 broadcast_audio_scan_service_client_init(&bass_packet_handler); 638 639 // turn on! 640 hci_power_control(HCI_POWER_ON); 641 642 btstack_stdin_setup(stdin_process); 643 return 0; 644 } 645