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