/* * Copyright (C) 2022 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define BTSTACK_FILE__ "le_audio_broadcast_assistant.c" /* * LE Audio Broadcast Assistant */ #include "btstack_config.h" #include #include #include #include #include #include "ad_parser.h" #include "ble/gatt-service/broadcast_audio_scan_service_client.h" #include "ble/sm.h" #include "bluetooth_data_types.h" #include "bluetooth_gatt.h" #include "btstack_audio.h" #include "btstack_event.h" #include "btstack_lc3.h" #include "btstack_run_loop.h" #include "btstack_stdin.h" #include "btstack_util.h" #include "gap.h" #include "hci.h" #include "l2cap.h" static void show_usage(void); static enum { APP_W4_WORKING, APP_W4_BROADCAST_SINK_AND_SCAN_DELEGATOR_ADV, APP_W4_PA_AND_BIG_INFO, APP_W4_BIG_SYNC_ESTABLISHED, APP_W4_SCAN_DELEGATOR_CONNECTION, APP_IDLE } app_state = APP_W4_WORKING; // static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t sm_event_callback_registration; static bool have_scan_delegator; static bool have_broadcast_source; static bool have_base; static bool have_big_info; // broadcast sink info static char broadcast_source_name[20]; static bd_addr_t broadcast_source; static bd_addr_type_t broadcast_source_type; static uint8_t broadcast_source_sid; static uint32_t broadcast_id; static uint16_t broadcast_source_pa_interval; // broadcast info static hci_con_handle_t sync_handle; static uint8_t encryption; static uint8_t broadcast_code [] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; static uint8_t num_bis; static uint32_t sampling_frequency_hz; static btstack_lc3_frame_duration_t frame_duration; static uint16_t octets_per_frame; // scan delegator info static char scan_delegator_name[20]; static bd_addr_t scan_delegator; static bd_addr_type_t scan_delegator_type; static hci_con_handle_t scan_delegator_handle; // BASS #define BASS_CLIENT_NUM_SOURCES 1 static bass_client_connection_t bass_connection; static bass_client_source_t bass_sources[BASS_CLIENT_NUM_SOURCES]; static bass_source_data_t bass_source_new; static uint16_t bass_cid; static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ // periodic advertisement contains the BASE // TODO: BASE might be split across multiple advertisements const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); if (adv_status != 0) { printf("Periodic Advertisement (status %u): ", adv_status); printf_hexdump(adv_data, adv_size); return; } ad_context_t context; for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { uint8_t data_type = ad_iterator_get_data_type(&context); // TODO: avoid out-of-bounds read // uint8_t data_size = ad_iterator_get_data_len(&context); const uint8_t * data = ad_iterator_get_data(&context); uint16_t uuid; switch (data_type){ case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: uuid = little_endian_read_16(data, 0); if (uuid == ORG_BLUETOOTH_SERVICE_BASIC_AUDIO_ANNOUNCEMENT_SERVICE){ have_base = true; // Level 1: Group Level const uint8_t * base_data = &data[2]; // TODO: avoid out-of-bounds read // uint16_t base_len = data_size - 2; printf("BASE:\n"); uint32_t presentation_delay = little_endian_read_24(base_data, 0); printf("- presentation delay: %"PRIu32" us\n", presentation_delay); uint8_t num_subgroups = base_data[3]; // Cache in new source struct bass_source_new.subgroups_num = num_subgroups; printf("- num subgroups: %u\n", num_subgroups); uint8_t i; uint16_t offset = 4; for (i=0;istop_stream(); sink->close(); } } // start over start_scanning(); break; case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: if (hci_subevent_le_connection_complete_get_status(packet) != ERROR_CODE_SUCCESS) break; scan_delegator_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); printf("Connection complete, handle 0x%04x\n", scan_delegator_handle); broadcast_audio_scan_service_client_connect(&bass_connection, bass_sources, BASS_CLIENT_NUM_SOURCES, scan_delegator_handle, &bass_cid); break; default: break; } break; case SM_EVENT_JUST_WORKS_REQUEST: printf("Just Works requested\n"); sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); break; default: break; } } static void show_usage(void){ printf("\n--- LE Audio Broadcast Assistant Test Console ---\n"); printf("s - start scanning for LE Broadcast Source & Scan Delegator\n"); printf("---\n"); } static void stdin_process(char c){ switch (c){ case 's': if (app_state != APP_IDLE) break; start_scanning(); break; case '\n': case '\r': break; default: show_usage(); break; } } int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ (void) argv; (void) argc; l2cap_init(); sm_init(); gatt_client_init(); // register for HCI events hci_event_callback_registration.callback = &packet_handler; hci_add_event_handler(&hci_event_callback_registration); // register for SM events sm_event_callback_registration.callback = &packet_handler; sm_add_event_handler(&sm_event_callback_registration); broadcast_audio_scan_service_client_init(&bass_packet_handler); // turn on! hci_power_control(HCI_POWER_ON); btstack_stdin_setup(stdin_process); return 0; }