1*4953c773SMatthias Ringwald /* 2*4953c773SMatthias Ringwald * Copyright (C) 2019 BlueKitchen GmbH 3*4953c773SMatthias Ringwald * 4*4953c773SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*4953c773SMatthias Ringwald * modification, are permitted provided that the following conditions 6*4953c773SMatthias Ringwald * are met: 7*4953c773SMatthias Ringwald * 8*4953c773SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*4953c773SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*4953c773SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*4953c773SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*4953c773SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*4953c773SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*4953c773SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*4953c773SMatthias Ringwald * from this software without specific prior written permission. 16*4953c773SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*4953c773SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*4953c773SMatthias Ringwald * monetary gain. 19*4953c773SMatthias Ringwald * 20*4953c773SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*4953c773SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*4953c773SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*4953c773SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*4953c773SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*4953c773SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*4953c773SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*4953c773SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*4953c773SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*4953c773SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*4953c773SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*4953c773SMatthias Ringwald * SUCH DAMAGE. 32*4953c773SMatthias Ringwald * 33*4953c773SMatthias Ringwald * Please inquire about commercial licensing options at 34*4953c773SMatthias Ringwald * [email protected] 35*4953c773SMatthias Ringwald * 36*4953c773SMatthias Ringwald */ 37*4953c773SMatthias Ringwald 38*4953c773SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39*4953c773SMatthias Ringwald 40*4953c773SMatthias Ringwald // ***************************************************************************** 41*4953c773SMatthias Ringwald // 42*4953c773SMatthias Ringwald // minimal setup for HCI code 43*4953c773SMatthias Ringwald // 44*4953c773SMatthias Ringwald // ***************************************************************************** 45*4953c773SMatthias Ringwald 46*4953c773SMatthias Ringwald #include <QCoreApplication> 47*4953c773SMatthias Ringwald 48*4953c773SMatthias Ringwald #include <stdint.h> 49*4953c773SMatthias Ringwald #include <stdio.h> 50*4953c773SMatthias Ringwald #include <stdlib.h> 51*4953c773SMatthias Ringwald #include <string.h> 52*4953c773SMatthias Ringwald #include <signal.h> 53*4953c773SMatthias Ringwald 54*4953c773SMatthias Ringwald #include "btstack_config.h" 55*4953c773SMatthias Ringwald 56*4953c773SMatthias Ringwald #include "bluetooth_company_id.h" 57*4953c773SMatthias Ringwald #include "btstack_debug.h" 58*4953c773SMatthias Ringwald #include "btstack_event.h" 59*4953c773SMatthias Ringwald #include "ble/le_device_db_tlv.h" 60*4953c773SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 61*4953c773SMatthias Ringwald #include "btstack_memory.h" 62*4953c773SMatthias Ringwald #include "btstack_run_loop.h" 63*4953c773SMatthias Ringwald #include "btstack_run_loop_qt.h" 64*4953c773SMatthias Ringwald #include "hal_led.h" 65*4953c773SMatthias Ringwald #include "hci.h" 66*4953c773SMatthias Ringwald #include "hci_dump.h" 67*4953c773SMatthias Ringwald #include "btstack_stdin.h" 68*4953c773SMatthias Ringwald #include "btstack_audio.h" 69*4953c773SMatthias Ringwald #include "btstack_tlv_posix.h" 70*4953c773SMatthias Ringwald #include "btstack_uart_block.h" 71*4953c773SMatthias Ringwald 72*4953c773SMatthias Ringwald // chipsets 73*4953c773SMatthias Ringwald #include "btstack_chipset_bcm.h" 74*4953c773SMatthias Ringwald #include "btstack_chipset_csr.h" 75*4953c773SMatthias Ringwald #include "btstack_chipset_cc256x.h" 76*4953c773SMatthias Ringwald 77*4953c773SMatthias Ringwald #ifdef Q_OS_WIN 78*4953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack" 79*4953c773SMatthias Ringwald #else 80*4953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 81*4953c773SMatthias Ringwald #endif 82*4953c773SMatthias Ringwald 83*4953c773SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 84*4953c773SMatthias Ringwald static char tlv_db_path[100]; 85*4953c773SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 86*4953c773SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 87*4953c773SMatthias Ringwald static bd_addr_t local_addr; 88*4953c773SMatthias Ringwald static int is_bcm; 89*4953c773SMatthias Ringwald 90*4953c773SMatthias Ringwald extern "C" int btstack_main(int argc, const char * argv[]); 91*4953c773SMatthias Ringwald 92*4953c773SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 93*4953c773SMatthias Ringwald 94*4953c773SMatthias Ringwald static bd_addr_t static_address; 95*4953c773SMatthias Ringwald static int using_static_address; 96*4953c773SMatthias Ringwald 97*4953c773SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 98*4953c773SMatthias Ringwald 99*4953c773SMatthias Ringwald // H4 100*4953c773SMatthias Ringwald static hci_transport_config_uart_t config = { 101*4953c773SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 102*4953c773SMatthias Ringwald 115200, 103*4953c773SMatthias Ringwald 0, // main baudrate 104*4953c773SMatthias Ringwald 1, // flow control 105*4953c773SMatthias Ringwald NULL, 106*4953c773SMatthias Ringwald }; 107*4953c773SMatthias Ringwald 108*4953c773SMatthias Ringwald static void use_fast_uart(void){ 109*4953c773SMatthias Ringwald printf("Using 921600 baud.\n"); 110*4953c773SMatthias Ringwald config.baudrate_main = 921600; 111*4953c773SMatthias Ringwald } 112*4953c773SMatthias Ringwald 113*4953c773SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 114*4953c773SMatthias Ringwald printf("Local version information:\n"); 115*4953c773SMatthias Ringwald uint16_t hci_version = packet[6]; 116*4953c773SMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7); 117*4953c773SMatthias Ringwald uint16_t lmp_version = packet[9]; 118*4953c773SMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 119*4953c773SMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 120*4953c773SMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 121*4953c773SMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 122*4953c773SMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 123*4953c773SMatthias Ringwald printf("- LMP Subversion 0x%04x\n", lmp_subversion); 124*4953c773SMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 125*4953c773SMatthias Ringwald switch (manufacturer){ 126*4953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 127*4953c773SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision); 128*4953c773SMatthias Ringwald use_fast_uart(); 129*4953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance()); 130*4953c773SMatthias Ringwald break; 131*4953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC: 132*4953c773SMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n"); 133*4953c773SMatthias Ringwald if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){ 134*4953c773SMatthias Ringwald printf("Error: LMP Subversion does not match initscript! "); 135*4953c773SMatthias Ringwald printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer"); 136*4953c773SMatthias Ringwald printf("Please update CMakeLists.txt to include the appropriate bluetooth_init_cc256???.c file\n"); 137*4953c773SMatthias Ringwald exit(10); 138*4953c773SMatthias Ringwald } 139*4953c773SMatthias Ringwald use_fast_uart(); 140*4953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 141*4953c773SMatthias Ringwald #ifdef ENABLE_EHCILL 142*4953c773SMatthias Ringwald printf("eHCILL enabled.\n"); 143*4953c773SMatthias Ringwald #else 144*4953c773SMatthias Ringwald printf("eHCILL disable.\n"); 145*4953c773SMatthias Ringwald #endif 146*4953c773SMatthias Ringwald 147*4953c773SMatthias Ringwald break; 148*4953c773SMatthias Ringwald case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: 149*4953c773SMatthias Ringwald printf("Broadcom/Cypress - using BCM driver.\n"); 150*4953c773SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 151*4953c773SMatthias Ringwald use_fast_uart(); 152*4953c773SMatthias Ringwald is_bcm = 1; 153*4953c773SMatthias Ringwald break; 154*4953c773SMatthias Ringwald default: 155*4953c773SMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n"); 156*4953c773SMatthias Ringwald break; 157*4953c773SMatthias Ringwald } 158*4953c773SMatthias Ringwald } 159*4953c773SMatthias Ringwald 160*4953c773SMatthias Ringwald 161*4953c773SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 162*4953c773SMatthias Ringwald UNUSED(channel); 163*4953c773SMatthias Ringwald UNUSED(size); 164*4953c773SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 165*4953c773SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 166*4953c773SMatthias Ringwald case BTSTACK_EVENT_STATE: 167*4953c773SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 168*4953c773SMatthias Ringwald gap_local_bd_addr(local_addr); 169*4953c773SMatthias Ringwald if (using_static_address){ 170*4953c773SMatthias Ringwald memcpy(local_addr, static_address, 6); 171*4953c773SMatthias Ringwald } 172*4953c773SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 173*4953c773SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 174*4953c773SMatthias Ringwald #ifndef Q_OS_WIN 175*4953c773SMatthias Ringwald // bd_addr_to_str use ":" which is not allowed in windows file names 176*4953c773SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 177*4953c773SMatthias Ringwald #endif 178*4953c773SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 179*4953c773SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 180*4953c773SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 181*4953c773SMatthias Ringwald #ifdef ENABLE_CLASSIC 182*4953c773SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 183*4953c773SMatthias Ringwald #endif 184*4953c773SMatthias Ringwald #ifdef ENABLE_BLE 185*4953c773SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 186*4953c773SMatthias Ringwald #endif 187*4953c773SMatthias Ringwald break; 188*4953c773SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 189*4953c773SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 190*4953c773SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 191*4953c773SMatthias Ringwald // terminate, name 248 chars 192*4953c773SMatthias Ringwald packet[6+248] = 0; 193*4953c773SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 194*4953c773SMatthias Ringwald if (is_bcm){ 195*4953c773SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 196*4953c773SMatthias Ringwald } 197*4953c773SMatthias Ringwald } 198*4953c773SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 199*4953c773SMatthias Ringwald local_version_information_handler(packet); 200*4953c773SMatthias Ringwald } 201*4953c773SMatthias Ringwald break; 202*4953c773SMatthias Ringwald default: 203*4953c773SMatthias Ringwald break; 204*4953c773SMatthias Ringwald } 205*4953c773SMatthias Ringwald } 206*4953c773SMatthias Ringwald 207*4953c773SMatthias Ringwald static void sigint_handler(int param){ 208*4953c773SMatthias Ringwald UNUSED(param); 209*4953c773SMatthias Ringwald 210*4953c773SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 211*4953c773SMatthias Ringwald log_info("sigint_handler: shutting down"); 212*4953c773SMatthias Ringwald 213*4953c773SMatthias Ringwald // reset anyway 214*4953c773SMatthias Ringwald btstack_stdin_reset(); 215*4953c773SMatthias Ringwald 216*4953c773SMatthias Ringwald // power down 217*4953c773SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 218*4953c773SMatthias Ringwald hci_close(); 219*4953c773SMatthias Ringwald log_info("Good bye, see you.\n"); 220*4953c773SMatthias Ringwald exit(0); 221*4953c773SMatthias Ringwald } 222*4953c773SMatthias Ringwald 223*4953c773SMatthias Ringwald static int led_state = 0; 224*4953c773SMatthias Ringwald void hal_led_toggle(void){ 225*4953c773SMatthias Ringwald led_state = 1 - led_state; 226*4953c773SMatthias Ringwald printf("LED State %u\n", led_state); 227*4953c773SMatthias Ringwald } 228*4953c773SMatthias Ringwald 229*4953c773SMatthias Ringwald #define USB_MAX_PATH_LEN 7 230*4953c773SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 231*4953c773SMatthias Ringwald 232*4953c773SMatthias Ringwald int main(int argc, char * argv[]){ 233*4953c773SMatthias Ringwald 234*4953c773SMatthias Ringwald QCoreApplication a(argc, argv); 235*4953c773SMatthias Ringwald 236*4953c773SMatthias Ringwald /// GET STARTED with BTstack /// 237*4953c773SMatthias Ringwald btstack_memory_init(); 238*4953c773SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_qt_get_instance()); 239*4953c773SMatthias Ringwald 240*4953c773SMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 241*4953c773SMatthias Ringwald char pklg_path[100]; 242*4953c773SMatthias Ringwald #ifdef Q_OS_WIN 243*4953c773SMatthias Ringwald strcpy(pklg_path, "hci_dump.pklg"); 244*4953c773SMatthias Ringwald #else 245*4953c773SMatthias Ringwald strcpy(pklg_path, "/tmp/hci_dump.pklg"); 246*4953c773SMatthias Ringwald #endif 247*4953c773SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 248*4953c773SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 249*4953c773SMatthias Ringwald 250*4953c773SMatthias Ringwald // init HCI 251*4953c773SMatthias Ringwald #ifdef Q_OS_WIN 252*4953c773SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance(); 253*4953c773SMatthias Ringwald config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT 254*4953c773SMatthias Ringwald #else 255*4953c773SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance(); 256*4953c773SMatthias Ringwald config.device_name = "\\\\.\\COM7"; 257*4953c773SMatthias Ringwald #endif 258*4953c773SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 259*4953c773SMatthias Ringwald hci_init(transport, (void*) &config); 260*4953c773SMatthias Ringwald 261*4953c773SMatthias Ringwald #ifdef HAVE_PORTAUDIO 262*4953c773SMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 263*4953c773SMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 264*4953c773SMatthias Ringwald #endif 265*4953c773SMatthias Ringwald 266*4953c773SMatthias Ringwald // inform about BTstack state 267*4953c773SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 268*4953c773SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 269*4953c773SMatthias Ringwald 270*4953c773SMatthias Ringwald // handle CTRL-c 271*4953c773SMatthias Ringwald signal(SIGINT, sigint_handler); 272*4953c773SMatthias Ringwald 273*4953c773SMatthias Ringwald // setup app 274*4953c773SMatthias Ringwald btstack_main(argc, (const char **) argv); 275*4953c773SMatthias Ringwald 276*4953c773SMatthias Ringwald // enter Qt run loop 277*4953c773SMatthias Ringwald return a.exec(); 278*4953c773SMatthias Ringwald } 279