14953c773SMatthias Ringwald /* 24953c773SMatthias Ringwald * Copyright (C) 2019 BlueKitchen GmbH 34953c773SMatthias Ringwald * 44953c773SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 54953c773SMatthias Ringwald * modification, are permitted provided that the following conditions 64953c773SMatthias Ringwald * are met: 74953c773SMatthias Ringwald * 84953c773SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 94953c773SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 104953c773SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 114953c773SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 124953c773SMatthias Ringwald * documentation and/or other materials provided with the distribution. 134953c773SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 144953c773SMatthias Ringwald * contributors may be used to endorse or promote products derived 154953c773SMatthias Ringwald * from this software without specific prior written permission. 164953c773SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 174953c773SMatthias Ringwald * personal benefit and not for any commercial purpose or for 184953c773SMatthias Ringwald * monetary gain. 194953c773SMatthias Ringwald * 204953c773SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 214953c773SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 224953c773SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 234953c773SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 244953c773SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 254953c773SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 264953c773SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 274953c773SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 284953c773SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 294953c773SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 304953c773SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314953c773SMatthias Ringwald * SUCH DAMAGE. 324953c773SMatthias Ringwald * 334953c773SMatthias Ringwald * Please inquire about commercial licensing options at 344953c773SMatthias Ringwald * [email protected] 354953c773SMatthias Ringwald * 364953c773SMatthias Ringwald */ 374953c773SMatthias Ringwald 384953c773SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 394953c773SMatthias Ringwald 404953c773SMatthias Ringwald // ***************************************************************************** 414953c773SMatthias Ringwald // 424953c773SMatthias Ringwald // minimal setup for HCI code 434953c773SMatthias Ringwald // 444953c773SMatthias Ringwald // ***************************************************************************** 454953c773SMatthias Ringwald 464953c773SMatthias Ringwald #include <QCoreApplication> 474953c773SMatthias Ringwald 484953c773SMatthias Ringwald #include <stdint.h> 494953c773SMatthias Ringwald #include <stdio.h> 504953c773SMatthias Ringwald #include <stdlib.h> 514953c773SMatthias Ringwald #include <string.h> 524953c773SMatthias Ringwald #include <signal.h> 534953c773SMatthias Ringwald 544953c773SMatthias Ringwald #include "btstack_config.h" 554953c773SMatthias Ringwald 564953c773SMatthias Ringwald #include "bluetooth_company_id.h" 574953c773SMatthias Ringwald #include "btstack_debug.h" 584953c773SMatthias Ringwald #include "btstack_event.h" 594953c773SMatthias Ringwald #include "ble/le_device_db_tlv.h" 604953c773SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 614953c773SMatthias Ringwald #include "btstack_memory.h" 624953c773SMatthias Ringwald #include "btstack_run_loop.h" 634953c773SMatthias Ringwald #include "btstack_run_loop_qt.h" 64793a0509SMatthias Ringwald #incldue "btstack_uart.h" 654953c773SMatthias Ringwald #include "hal_led.h" 664953c773SMatthias Ringwald #include "hci.h" 674953c773SMatthias Ringwald #include "hci_dump.h" 687435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 69c8dfe071SMatthias Ringwald #include "hci_transport.h" 70c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 714953c773SMatthias Ringwald #include "btstack_stdin.h" 724953c773SMatthias Ringwald #include "btstack_audio.h" 734953c773SMatthias Ringwald #include "btstack_tlv_posix.h" 744953c773SMatthias Ringwald #include "btstack_uart_block.h" 754953c773SMatthias Ringwald 764953c773SMatthias Ringwald // chipsets 774953c773SMatthias Ringwald #include "btstack_chipset_bcm.h" 784953c773SMatthias Ringwald #include "btstack_chipset_csr.h" 794953c773SMatthias Ringwald #include "btstack_chipset_cc256x.h" 804953c773SMatthias Ringwald 814953c773SMatthias Ringwald #ifdef Q_OS_WIN 82*00f9da3aSMatthias Ringwald #include "btstack_stdin_windows.h" 83*00f9da3aSMatthias Ringwald #else 84*00f9da3aSMatthias Ringwald #include <signal.h> 85*00f9da3aSMatthias Ringwald #include "btstack_signal.h" 86*00f9da3aSMatthias Ringwald #endif 87*00f9da3aSMatthias Ringwald 88*00f9da3aSMatthias Ringwald #ifdef Q_OS_WIN 894953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack" 904953c773SMatthias Ringwald #else 914953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 924953c773SMatthias Ringwald #endif 934953c773SMatthias Ringwald 944953c773SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 954953c773SMatthias Ringwald static char tlv_db_path[100]; 964953c773SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 974953c773SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 984953c773SMatthias Ringwald static bd_addr_t local_addr; 994953c773SMatthias Ringwald static int is_bcm; 1004953c773SMatthias Ringwald 101*00f9da3aSMatthias Ringwald // shutdown 102*00f9da3aSMatthias Ringwald static bool shutdown_triggered; 103*00f9da3aSMatthias Ringwald 1044953c773SMatthias Ringwald extern "C" int btstack_main(int argc, const char * argv[]); 1054953c773SMatthias Ringwald 1064953c773SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 1074953c773SMatthias Ringwald 1084953c773SMatthias Ringwald static bd_addr_t static_address; 1094953c773SMatthias Ringwald static int using_static_address; 1104953c773SMatthias Ringwald 1114953c773SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1124953c773SMatthias Ringwald 1134953c773SMatthias Ringwald // H4 1144953c773SMatthias Ringwald static hci_transport_config_uart_t config = { 1154953c773SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 1164953c773SMatthias Ringwald 115200, 1174953c773SMatthias Ringwald 0, // main baudrate 1184953c773SMatthias Ringwald 1, // flow control 1194953c773SMatthias Ringwald NULL, 1204953c773SMatthias Ringwald }; 1214953c773SMatthias Ringwald 1224953c773SMatthias Ringwald static void use_fast_uart(void){ 1234953c773SMatthias Ringwald printf("Using 921600 baud.\n"); 1244953c773SMatthias Ringwald config.baudrate_main = 921600; 1254953c773SMatthias Ringwald } 1264953c773SMatthias Ringwald 1274953c773SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 1284953c773SMatthias Ringwald printf("Local version information:\n"); 1294953c773SMatthias Ringwald uint16_t hci_version = packet[6]; 1304953c773SMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7); 1314953c773SMatthias Ringwald uint16_t lmp_version = packet[9]; 1324953c773SMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 1334953c773SMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 1344953c773SMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 1354953c773SMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 1364953c773SMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 1374953c773SMatthias Ringwald printf("- LMP Subversion 0x%04x\n", lmp_subversion); 1384953c773SMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 1394953c773SMatthias Ringwald switch (manufacturer){ 1404953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 1414953c773SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision); 1424953c773SMatthias Ringwald use_fast_uart(); 1434953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance()); 1444953c773SMatthias Ringwald break; 1454953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC: 1464953c773SMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n"); 1474953c773SMatthias Ringwald if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){ 1484953c773SMatthias Ringwald printf("Error: LMP Subversion does not match initscript! "); 1494953c773SMatthias Ringwald printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer"); 1504953c773SMatthias Ringwald printf("Please update CMakeLists.txt to include the appropriate bluetooth_init_cc256???.c file\n"); 1514953c773SMatthias Ringwald exit(10); 1524953c773SMatthias Ringwald } 1534953c773SMatthias Ringwald use_fast_uart(); 1544953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 1554953c773SMatthias Ringwald #ifdef ENABLE_EHCILL 1564953c773SMatthias Ringwald printf("eHCILL enabled.\n"); 1574953c773SMatthias Ringwald #else 1584953c773SMatthias Ringwald printf("eHCILL disable.\n"); 1594953c773SMatthias Ringwald #endif 1604953c773SMatthias Ringwald 1614953c773SMatthias Ringwald break; 1624953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: 1634953c773SMatthias Ringwald printf("Broadcom/Cypress - using BCM driver.\n"); 1644953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 1654953c773SMatthias Ringwald use_fast_uart(); 1664953c773SMatthias Ringwald is_bcm = 1; 1674953c773SMatthias Ringwald break; 1684953c773SMatthias Ringwald default: 1694953c773SMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n"); 1704953c773SMatthias Ringwald break; 1714953c773SMatthias Ringwald } 1724953c773SMatthias Ringwald } 1734953c773SMatthias Ringwald 1744953c773SMatthias Ringwald 1754953c773SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1764953c773SMatthias Ringwald UNUSED(channel); 1774953c773SMatthias Ringwald UNUSED(size); 1784953c773SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1794953c773SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 1804953c773SMatthias Ringwald case BTSTACK_EVENT_STATE: 181*00f9da3aSMatthias Ringwald switch (btstack_event_state_get_state(packet)){ 182*00f9da3aSMatthias Ringwald case HCI_STATE_WORKING: 1834953c773SMatthias Ringwald gap_local_bd_addr(local_addr); 1844953c773SMatthias Ringwald if (using_static_address) { 1854953c773SMatthias Ringwald memcpy(local_addr, static_address, 6); 1864953c773SMatthias Ringwald } 1874953c773SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 1884953c773SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1894953c773SMatthias Ringwald #ifndef Q_OS_WIN 1904953c773SMatthias Ringwald // bd_addr_to_str use ":" which is not allowed in windows file names 1914953c773SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 1924953c773SMatthias Ringwald #endif 1934953c773SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1944953c773SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1954953c773SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 1964953c773SMatthias Ringwald #ifdef ENABLE_CLASSIC 1974953c773SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 1984953c773SMatthias Ringwald #endif 1994953c773SMatthias Ringwald #ifdef ENABLE_BLE 2004953c773SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 2014953c773SMatthias Ringwald #endif 2024953c773SMatthias Ringwald break; 203*00f9da3aSMatthias Ringwald case HCI_STATE_OFF: 204*00f9da3aSMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context); 205*00f9da3aSMatthias Ringwald if (!shutdown_triggered) break; 206*00f9da3aSMatthias Ringwald // reset stdin 207*00f9da3aSMatthias Ringwald btstack_stdin_reset(); 208*00f9da3aSMatthias Ringwald log_info("Good bye, see you.\n"); 209*00f9da3aSMatthias Ringwald exit(0); 210*00f9da3aSMatthias Ringwald break; 211*00f9da3aSMatthias Ringwald default: 212*00f9da3aSMatthias Ringwald break; 213*00f9da3aSMatthias Ringwald } 214*00f9da3aSMatthias Ringwald break; 2154953c773SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 2164953c773SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 2174953c773SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 2184953c773SMatthias Ringwald // terminate, name 248 chars 2194953c773SMatthias Ringwald packet[6+248] = 0; 2204953c773SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 2214953c773SMatthias Ringwald if (is_bcm){ 2224953c773SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 2234953c773SMatthias Ringwald } 2244953c773SMatthias Ringwald } 2254953c773SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 2264953c773SMatthias Ringwald local_version_information_handler(packet); 2274953c773SMatthias Ringwald } 2284953c773SMatthias Ringwald break; 2294953c773SMatthias Ringwald default: 2304953c773SMatthias Ringwald break; 2314953c773SMatthias Ringwald } 2324953c773SMatthias Ringwald } 2334953c773SMatthias Ringwald 234*00f9da3aSMatthias Ringwald static void trigger_shutdown(void){ 2354953c773SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 2364953c773SMatthias Ringwald log_info("sigint_handler: shutting down"); 237*00f9da3aSMatthias Ringwald shutdown_triggered = true; 2384953c773SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 2394953c773SMatthias Ringwald } 2404953c773SMatthias Ringwald 2414953c773SMatthias Ringwald static int led_state = 0; 2424953c773SMatthias Ringwald void hal_led_toggle(void){ 2434953c773SMatthias Ringwald led_state = 1 - led_state; 2444953c773SMatthias Ringwald printf("LED State %u\n", led_state); 2454953c773SMatthias Ringwald } 2464953c773SMatthias Ringwald 2474953c773SMatthias Ringwald #define USB_MAX_PATH_LEN 7 2484953c773SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 2494953c773SMatthias Ringwald 2504953c773SMatthias Ringwald int main(int argc, char * argv[]){ 2514953c773SMatthias Ringwald 2524953c773SMatthias Ringwald QCoreApplication a(argc, argv); 2534953c773SMatthias Ringwald 2544953c773SMatthias Ringwald /// GET STARTED with BTstack /// 2554953c773SMatthias Ringwald btstack_memory_init(); 2564953c773SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_qt_get_instance()); 2574953c773SMatthias Ringwald 2587435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 2594953c773SMatthias Ringwald char pklg_path[100]; 2604953c773SMatthias Ringwald #ifdef Q_OS_WIN 2614953c773SMatthias Ringwald strcpy(pklg_path, "hci_dump.pklg"); 2624953c773SMatthias Ringwald #else 2634953c773SMatthias Ringwald strcpy(pklg_path, "/tmp/hci_dump.pklg"); 2644953c773SMatthias Ringwald #endif 2657435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 2667435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 2677435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 2684953c773SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 2694953c773SMatthias Ringwald 2704953c773SMatthias Ringwald // init HCI 2714953c773SMatthias Ringwald #ifdef Q_OS_WIN 2724953c773SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance(); 27322e4935bSMatthias Ringwald config.device_name = "\\\\.\\COM7"; 2744953c773SMatthias Ringwald #else 275793a0509SMatthias Ringwald const btstack_uart_t * uart_driver = btstack_uart_posix_instance(); 27622e4935bSMatthias Ringwald config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT 2774953c773SMatthias Ringwald #endif 278793a0509SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 2794953c773SMatthias Ringwald hci_init(transport, (void*) &config); 2804953c773SMatthias Ringwald 2814953c773SMatthias Ringwald #ifdef HAVE_PORTAUDIO 2824953c773SMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 2834953c773SMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 2844953c773SMatthias Ringwald #endif 2854953c773SMatthias Ringwald 2864953c773SMatthias Ringwald // inform about BTstack state 2874953c773SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 2884953c773SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 2894953c773SMatthias Ringwald 290*00f9da3aSMatthias Ringwald // register callback for CTRL-c 291*00f9da3aSMatthias Ringwald #ifdef Q_OS_WIN 292*00f9da3aSMatthias Ringwald btstack_stdin_windows_init(); 293*00f9da3aSMatthias Ringwald btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown); 294*00f9da3aSMatthias Ringwald #else 295*00f9da3aSMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown); 296*00f9da3aSMatthias Ringwald #endif 2974953c773SMatthias Ringwald 2984953c773SMatthias Ringwald // setup app 2994953c773SMatthias Ringwald btstack_main(argc, (const char **) argv); 3004953c773SMatthias Ringwald 3014953c773SMatthias Ringwald // enter Qt run loop 3024953c773SMatthias Ringwald return a.exec(); 3034953c773SMatthias Ringwald } 304