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