xref: /btstack/port/qt-h4/main.cpp (revision 22e4935be4956a64753537b906fdf85f736f7bc5)
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"
644953c773SMatthias Ringwald #include "hal_led.h"
654953c773SMatthias Ringwald #include "hci.h"
664953c773SMatthias Ringwald #include "hci_dump.h"
674953c773SMatthias Ringwald #include "btstack_stdin.h"
684953c773SMatthias Ringwald #include "btstack_audio.h"
694953c773SMatthias Ringwald #include "btstack_tlv_posix.h"
704953c773SMatthias Ringwald #include "btstack_uart_block.h"
714953c773SMatthias Ringwald 
724953c773SMatthias Ringwald // chipsets
734953c773SMatthias Ringwald #include "btstack_chipset_bcm.h"
744953c773SMatthias Ringwald #include "btstack_chipset_csr.h"
754953c773SMatthias Ringwald #include "btstack_chipset_cc256x.h"
764953c773SMatthias Ringwald 
774953c773SMatthias Ringwald #ifdef Q_OS_WIN
784953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack"
794953c773SMatthias Ringwald #else
804953c773SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
814953c773SMatthias Ringwald #endif
824953c773SMatthias Ringwald 
834953c773SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
844953c773SMatthias Ringwald static char tlv_db_path[100];
854953c773SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
864953c773SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
874953c773SMatthias Ringwald static bd_addr_t             local_addr;
884953c773SMatthias Ringwald static int is_bcm;
894953c773SMatthias Ringwald 
904953c773SMatthias Ringwald extern "C" int btstack_main(int argc, const char * argv[]);
914953c773SMatthias Ringwald 
924953c773SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
934953c773SMatthias Ringwald 
944953c773SMatthias Ringwald static bd_addr_t static_address;
954953c773SMatthias Ringwald static int using_static_address;
964953c773SMatthias Ringwald 
974953c773SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
984953c773SMatthias Ringwald 
994953c773SMatthias Ringwald // H4
1004953c773SMatthias Ringwald static hci_transport_config_uart_t config = {
1014953c773SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
1024953c773SMatthias Ringwald     115200,
1034953c773SMatthias Ringwald     0,  // main baudrate
1044953c773SMatthias Ringwald     1,  // flow control
1054953c773SMatthias Ringwald     NULL,
1064953c773SMatthias Ringwald };
1074953c773SMatthias Ringwald 
1084953c773SMatthias Ringwald static void use_fast_uart(void){
1094953c773SMatthias Ringwald     printf("Using 921600 baud.\n");
1104953c773SMatthias Ringwald     config.baudrate_main = 921600;
1114953c773SMatthias Ringwald }
1124953c773SMatthias Ringwald 
1134953c773SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
1144953c773SMatthias Ringwald     printf("Local version information:\n");
1154953c773SMatthias Ringwald     uint16_t hci_version    = packet[6];
1164953c773SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
1174953c773SMatthias Ringwald     uint16_t lmp_version    = packet[9];
1184953c773SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
1194953c773SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
1204953c773SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
1214953c773SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
1224953c773SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
1234953c773SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
1244953c773SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
1254953c773SMatthias Ringwald     switch (manufacturer){
1264953c773SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
1274953c773SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
1284953c773SMatthias Ringwald             use_fast_uart();
1294953c773SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
1304953c773SMatthias Ringwald             break;
1314953c773SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
1324953c773SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
1334953c773SMatthias Ringwald             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
1344953c773SMatthias Ringwald                 printf("Error: LMP Subversion does not match initscript! ");
1354953c773SMatthias Ringwald                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
1364953c773SMatthias Ringwald                 printf("Please update CMakeLists.txt to include the appropriate bluetooth_init_cc256???.c file\n");
1374953c773SMatthias Ringwald                 exit(10);
1384953c773SMatthias Ringwald             }
1394953c773SMatthias Ringwald             use_fast_uart();
1404953c773SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
1414953c773SMatthias Ringwald #ifdef ENABLE_EHCILL
1424953c773SMatthias Ringwald             printf("eHCILL enabled.\n");
1434953c773SMatthias Ringwald #else
1444953c773SMatthias Ringwald             printf("eHCILL disable.\n");
1454953c773SMatthias Ringwald #endif
1464953c773SMatthias Ringwald 
1474953c773SMatthias Ringwald             break;
1484953c773SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
1494953c773SMatthias Ringwald             printf("Broadcom/Cypress - using BCM driver.\n");
1504953c773SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
1514953c773SMatthias Ringwald             use_fast_uart();
1524953c773SMatthias Ringwald             is_bcm = 1;
1534953c773SMatthias Ringwald             break;
1544953c773SMatthias Ringwald         default:
1554953c773SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
1564953c773SMatthias Ringwald             break;
1574953c773SMatthias Ringwald     }
1584953c773SMatthias Ringwald }
1594953c773SMatthias Ringwald 
1604953c773SMatthias Ringwald 
1614953c773SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1624953c773SMatthias Ringwald     UNUSED(channel);
1634953c773SMatthias Ringwald     UNUSED(size);
1644953c773SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1654953c773SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
1664953c773SMatthias Ringwald         case BTSTACK_EVENT_STATE:
1674953c773SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
1684953c773SMatthias Ringwald             gap_local_bd_addr(local_addr);
1694953c773SMatthias Ringwald             if (using_static_address){
1704953c773SMatthias Ringwald                 memcpy(local_addr, static_address, 6);
1714953c773SMatthias Ringwald             }
1724953c773SMatthias Ringwald             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
1734953c773SMatthias Ringwald             strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
1744953c773SMatthias Ringwald #ifndef Q_OS_WIN
1754953c773SMatthias Ringwald             // bd_addr_to_str use ":" which is not allowed in windows file names
1764953c773SMatthias Ringwald             strcat(tlv_db_path, bd_addr_to_str(local_addr));
1774953c773SMatthias Ringwald #endif
1784953c773SMatthias Ringwald             strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
1794953c773SMatthias Ringwald             tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
1804953c773SMatthias Ringwald             btstack_tlv_set_instance(tlv_impl, &tlv_context);
1814953c773SMatthias Ringwald #ifdef ENABLE_CLASSIC
1824953c773SMatthias Ringwald             hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
1834953c773SMatthias Ringwald #endif
1844953c773SMatthias Ringwald #ifdef ENABLE_BLE
1854953c773SMatthias Ringwald             le_device_db_tlv_configure(tlv_impl, &tlv_context);
1864953c773SMatthias Ringwald #endif
1874953c773SMatthias Ringwald             break;
1884953c773SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1894953c773SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
1904953c773SMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
1914953c773SMatthias Ringwald                 // terminate, name 248 chars
1924953c773SMatthias Ringwald                 packet[6+248] = 0;
1934953c773SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
1944953c773SMatthias Ringwald                 if (is_bcm){
1954953c773SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
1964953c773SMatthias Ringwald                 }
1974953c773SMatthias Ringwald             }
1984953c773SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
1994953c773SMatthias Ringwald                 local_version_information_handler(packet);
2004953c773SMatthias Ringwald             }
2014953c773SMatthias Ringwald             break;
2024953c773SMatthias Ringwald         default:
2034953c773SMatthias Ringwald             break;
2044953c773SMatthias Ringwald     }
2054953c773SMatthias Ringwald }
2064953c773SMatthias Ringwald 
2074953c773SMatthias Ringwald static void sigint_handler(int param){
2084953c773SMatthias Ringwald     UNUSED(param);
2094953c773SMatthias Ringwald 
2104953c773SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
2114953c773SMatthias Ringwald     log_info("sigint_handler: shutting down");
2124953c773SMatthias Ringwald 
2134953c773SMatthias Ringwald     // reset anyway
2144953c773SMatthias Ringwald     btstack_stdin_reset();
2154953c773SMatthias Ringwald 
2164953c773SMatthias Ringwald     // power down
2174953c773SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
2184953c773SMatthias Ringwald     hci_close();
2194953c773SMatthias Ringwald     log_info("Good bye, see you.\n");
2204953c773SMatthias Ringwald     exit(0);
2214953c773SMatthias Ringwald }
2224953c773SMatthias Ringwald 
2234953c773SMatthias Ringwald static int led_state = 0;
2244953c773SMatthias Ringwald void hal_led_toggle(void){
2254953c773SMatthias Ringwald     led_state = 1 - led_state;
2264953c773SMatthias Ringwald     printf("LED State %u\n", led_state);
2274953c773SMatthias Ringwald }
2284953c773SMatthias Ringwald 
2294953c773SMatthias Ringwald #define USB_MAX_PATH_LEN 7
2304953c773SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
2314953c773SMatthias Ringwald 
2324953c773SMatthias Ringwald int main(int argc, char * argv[]){
2334953c773SMatthias Ringwald 
2344953c773SMatthias Ringwald     QCoreApplication a(argc, argv);
2354953c773SMatthias Ringwald 
2364953c773SMatthias Ringwald     /// GET STARTED with BTstack ///
2374953c773SMatthias Ringwald     btstack_memory_init();
2384953c773SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_qt_get_instance());
2394953c773SMatthias Ringwald 
2404953c773SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
2414953c773SMatthias Ringwald     char pklg_path[100];
2424953c773SMatthias Ringwald #ifdef Q_OS_WIN
2434953c773SMatthias Ringwald     strcpy(pklg_path, "hci_dump.pklg");
2444953c773SMatthias Ringwald #else
2454953c773SMatthias Ringwald     strcpy(pklg_path, "/tmp/hci_dump.pklg");
2464953c773SMatthias Ringwald #endif
2474953c773SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
2484953c773SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
2494953c773SMatthias Ringwald 
2504953c773SMatthias Ringwald     // init HCI
2514953c773SMatthias Ringwald #ifdef Q_OS_WIN
2524953c773SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
253*22e4935bSMatthias Ringwald     config.device_name = "\\\\.\\COM7";
2544953c773SMatthias Ringwald #else
2554953c773SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
256*22e4935bSMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT
2574953c773SMatthias Ringwald #endif
2584953c773SMatthias Ringwald     const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
2594953c773SMatthias Ringwald     hci_init(transport, (void*) &config);
2604953c773SMatthias Ringwald 
2614953c773SMatthias Ringwald #ifdef HAVE_PORTAUDIO
2624953c773SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
2634953c773SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
2644953c773SMatthias Ringwald #endif
2654953c773SMatthias Ringwald 
2664953c773SMatthias Ringwald     // inform about BTstack state
2674953c773SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2684953c773SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2694953c773SMatthias Ringwald 
2704953c773SMatthias Ringwald     // handle CTRL-c
2714953c773SMatthias Ringwald     signal(SIGINT, sigint_handler);
2724953c773SMatthias Ringwald 
2734953c773SMatthias Ringwald     // setup app
2744953c773SMatthias Ringwald     btstack_main(argc, (const char **) argv);
2754953c773SMatthias Ringwald 
2764953c773SMatthias Ringwald     // enter Qt run loop
2774953c773SMatthias Ringwald     return a.exec();
2784953c773SMatthias Ringwald }
279