1 /* 2 * Copyright (C) 2019 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__ "main.c" 39 40 // ***************************************************************************** 41 // 42 // minimal setup for HCI code 43 // 44 // ***************************************************************************** 45 46 #include <QCoreApplication> 47 48 #include <stdint.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <signal.h> 53 54 #include "btstack_config.h" 55 56 #include "bluetooth_company_id.h" 57 #include "btstack_debug.h" 58 #include "btstack_event.h" 59 #include "ble/le_device_db_tlv.h" 60 #include "classic/btstack_link_key_db_tlv.h" 61 #include "btstack_memory.h" 62 #include "btstack_run_loop.h" 63 #include "btstack_run_loop_qt.h" 64 #incldue "btstack_uart.h" 65 #include "hal_led.h" 66 #include "hci.h" 67 #include "hci_dump.h" 68 #include "hci_dump_posix_fs.h" 69 #include "hci_transport.h" 70 #include "hci_transport_h4.h" 71 #include "btstack_stdin.h" 72 #include "btstack_audio.h" 73 #include "btstack_tlv_posix.h" 74 #include "btstack_uart_block.h" 75 76 // chipsets 77 #include "btstack_chipset_bcm.h" 78 #include "btstack_chipset_csr.h" 79 #include "btstack_chipset_cc256x.h" 80 81 #ifdef Q_OS_WIN 82 #include "btstack_stdin_windows.h" 83 #else 84 #include <signal.h> 85 #include "btstack_signal.h" 86 #endif 87 88 #ifdef Q_OS_WIN 89 #define TLV_DB_PATH_PREFIX "btstack" 90 #else 91 #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 92 #endif 93 94 #define TLV_DB_PATH_POSTFIX ".tlv" 95 static char tlv_db_path[100]; 96 static const btstack_tlv_t * tlv_impl; 97 static btstack_tlv_posix_t tlv_context; 98 static bd_addr_t local_addr; 99 static int is_bcm; 100 101 // shutdown 102 static bool shutdown_triggered; 103 104 extern "C" int btstack_main(int argc, const char * argv[]); 105 106 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 107 108 static bd_addr_t static_address; 109 static int using_static_address; 110 111 static btstack_packet_callback_registration_t hci_event_callback_registration; 112 113 // H4 114 static hci_transport_config_uart_t config = { 115 HCI_TRANSPORT_CONFIG_UART, 116 115200, 117 0, // main baudrate 118 1, // flow control 119 NULL, 120 }; 121 122 static void use_fast_uart(void){ 123 printf("Using 921600 baud.\n"); 124 config.baudrate_main = 921600; 125 } 126 127 static void local_version_information_handler(uint8_t * packet){ 128 printf("Local version information:\n"); 129 uint16_t hci_version = packet[6]; 130 uint16_t hci_revision = little_endian_read_16(packet, 7); 131 uint16_t lmp_version = packet[9]; 132 uint16_t manufacturer = little_endian_read_16(packet, 10); 133 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 134 printf("- HCI Version 0x%04x\n", hci_version); 135 printf("- HCI Revision 0x%04x\n", hci_revision); 136 printf("- LMP Version 0x%04x\n", lmp_version); 137 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 138 printf("- Manufacturer 0x%04x\n", manufacturer); 139 switch (manufacturer){ 140 case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 141 printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision); 142 use_fast_uart(); 143 hci_set_chipset(btstack_chipset_csr_instance()); 144 break; 145 case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC: 146 printf("Texas Instruments - CC256x compatible chipset.\n"); 147 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){ 148 printf("Error: LMP Subversion does not match initscript! "); 149 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer"); 150 printf("Please update CMakeLists.txt to include the appropriate bluetooth_init_cc256???.c file\n"); 151 exit(10); 152 } 153 use_fast_uart(); 154 hci_set_chipset(btstack_chipset_cc256x_instance()); 155 #ifdef ENABLE_EHCILL 156 printf("eHCILL enabled.\n"); 157 #else 158 printf("eHCILL disable.\n"); 159 #endif 160 161 break; 162 case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: 163 printf("Broadcom/Cypress - using BCM driver.\n"); 164 hci_set_chipset(btstack_chipset_bcm_instance()); 165 use_fast_uart(); 166 is_bcm = 1; 167 break; 168 default: 169 printf("Unknown manufacturer / manufacturer not supported yet.\n"); 170 break; 171 } 172 } 173 174 175 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 176 UNUSED(channel); 177 UNUSED(size); 178 if (packet_type != HCI_EVENT_PACKET) return; 179 switch (hci_event_packet_get_type(packet)){ 180 case BTSTACK_EVENT_STATE: 181 switch (btstack_event_state_get_state(packet)){ 182 case HCI_STATE_WORKING: 183 gap_local_bd_addr(local_addr); 184 if (using_static_address) { 185 memcpy(local_addr, static_address, 6); 186 } 187 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 188 strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 189 strcat(tlv_db_path, bd_addr_to_str_with_delimiter(local_addr, '-')); 190 strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 191 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 192 btstack_tlv_set_instance(tlv_impl, &tlv_context); 193 #ifdef ENABLE_CLASSIC 194 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 195 #endif 196 #ifdef ENABLE_BLE 197 le_device_db_tlv_configure(tlv_impl, &tlv_context); 198 #endif 199 break; 200 case HCI_STATE_OFF: 201 btstack_tlv_posix_deinit(&tlv_context); 202 if (!shutdown_triggered) break; 203 // reset stdin 204 btstack_stdin_reset(); 205 log_info("Good bye, see you.\n"); 206 exit(0); 207 break; 208 default: 209 break; 210 } 211 break; 212 case HCI_EVENT_COMMAND_COMPLETE: 213 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){ 214 if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 215 // terminate, name 248 chars 216 packet[6+248] = 0; 217 printf("Local name: %s\n", &packet[6]); 218 if (is_bcm){ 219 btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 220 } 221 } 222 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 223 local_version_information_handler(packet); 224 } 225 break; 226 default: 227 break; 228 } 229 } 230 231 static void trigger_shutdown(void){ 232 printf("CTRL-C - SIGINT received, shutting down..\n"); 233 log_info("sigint_handler: shutting down"); 234 shutdown_triggered = true; 235 hci_power_control(HCI_POWER_OFF); 236 } 237 238 static int led_state = 0; 239 void hal_led_toggle(void){ 240 led_state = 1 - led_state; 241 printf("LED State %u\n", led_state); 242 } 243 244 #define USB_MAX_PATH_LEN 7 245 int btstack_main(int argc, const char * argv[]); 246 247 int main(int argc, char * argv[]){ 248 249 QCoreApplication a(argc, argv); 250 251 /// GET STARTED with BTstack /// 252 btstack_memory_init(); 253 btstack_run_loop_init(btstack_run_loop_qt_get_instance()); 254 255 // log into file using HCI_DUMP_PACKETLOGGER format 256 char pklg_path[100]; 257 #ifdef Q_OS_WIN 258 strcpy(pklg_path, "hci_dump.pklg"); 259 #else 260 strcpy(pklg_path, "/tmp/hci_dump.pklg"); 261 #endif 262 hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 263 const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 264 hci_dump_init(hci_dump_impl); 265 printf("Packet Log: %s\n", pklg_path); 266 267 // init HCI 268 #ifdef Q_OS_WIN 269 const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance(); 270 config.device_name = "\\\\.\\COM7"; 271 #else 272 const btstack_uart_t * uart_driver = btstack_uart_posix_instance(); 273 config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT 274 #endif 275 const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 276 hci_init(transport, (void*) &config); 277 278 #ifdef HAVE_PORTAUDIO 279 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 280 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 281 #endif 282 283 // inform about BTstack state 284 hci_event_callback_registration.callback = &packet_handler; 285 hci_add_event_handler(&hci_event_callback_registration); 286 287 // register callback for CTRL-c 288 #ifdef Q_OS_WIN 289 btstack_stdin_windows_init(); 290 btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown); 291 #else 292 btstack_signal_register_callback(SIGINT, &trigger_shutdown); 293 #endif 294 295 // setup app 296 btstack_main(argc, (const char **) argv); 297 298 // enter Qt run loop 299 return a.exec(); 300 } 301