1 /* 2 * Copyright (C) 2014 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 <stdint.h> 47 #include <inttypes.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <string.h> 52 #include <signal.h> 53 #include <errno.h> 54 55 #include "btstack_config.h" 56 57 #include "btstack_debug.h" 58 #include "btstack_event.h" 59 #include "btstack_link_key_db_fs.h" 60 #include "btstack_memory.h" 61 #include "btstack_run_loop.h" 62 #include "btstack_run_loop_posix.h" 63 #include "bluetooth_company_id.h" 64 #include "hci.h" 65 #include "hci_dump.h" 66 #include "btstack_stdin.h" 67 #include "btstack_tlv_posix.h" 68 69 #include "btstack_chipset_bcm.h" 70 #include "btstack_chipset_bcm_download_firmware.h" 71 #include "btstack_control_raspi.h" 72 73 int btstack_main(int argc, const char * argv[]); 74 75 typedef enum { 76 UART_INVALID, 77 UART_SOFTWARE_NO_FLOW, 78 UART_HARDWARE_NO_FLOW, 79 UART_HARDWARE_FLOW 80 } uart_type_t; 81 82 // default config, updated depending on RasperryPi UART configuration 83 static hci_transport_config_uart_t transport_config = { 84 HCI_TRANSPORT_CONFIG_UART, 85 115200, 86 0, // main baudrate 87 0, // flow control 88 NULL, 89 }; 90 static btstack_uart_config_t uart_config; 91 92 static int main_argc; 93 static const char ** main_argv; 94 95 static btstack_packet_callback_registration_t hci_event_callback_registration; 96 97 #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 98 #define TLV_DB_PATH_POSTFIX ".tlv" 99 static char tlv_db_path[100]; 100 static const btstack_tlv_t * tlv_impl; 101 static btstack_tlv_posix_t tlv_context; 102 103 static void sigint_handler(int param){ 104 UNUSED(param); 105 106 printf("CTRL-C - SIGINT received, shutting down..\n"); 107 log_info("sigint_handler: shutting down"); 108 109 // reset anyway 110 btstack_stdin_reset(); 111 112 // power down 113 hci_power_control(HCI_POWER_OFF); 114 hci_close(); 115 log_info("Good bye, see you.\n"); 116 exit(0); 117 } 118 119 static int led_state = 0; 120 void hal_led_toggle(void){ 121 led_state = 1 - led_state; 122 printf("LED State %u\n", led_state); 123 } 124 125 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 126 bd_addr_t addr; 127 if (packet_type != HCI_EVENT_PACKET) return; 128 switch (hci_event_packet_get_type(packet)){ 129 case BTSTACK_EVENT_STATE: 130 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 131 gap_local_bd_addr(addr); 132 printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 133 // setup TLV 134 strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 135 strcat(tlv_db_path, bd_addr_to_str(addr)); 136 strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 137 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 138 btstack_tlv_set_instance(tlv_impl, &tlv_context); 139 break; 140 default: 141 break; 142 } 143 } 144 145 // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 146 static int raspi_get_bd_addr(bd_addr_t addr){ 147 148 FILE *fd = fopen( "/proc/device-tree/serial-number", "r" ); 149 if( fd == NULL ){ 150 fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) ); 151 return -1; 152 } 153 fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] ); 154 fclose( fd ); 155 156 addr[0] = 0xb8; addr[1] = 0x27; addr[2] = 0xeb; 157 addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa; 158 159 return 0; 160 } 161 162 // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 163 // on UART_INVALID errno is set 164 static uart_type_t raspi_get_bluetooth_uart_type(void){ 165 166 uint8_t deviceUart0[21] = { 0 }; 167 FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" ); 168 if( fd == NULL ) return UART_INVALID; 169 fscanf( fd, "%20s", deviceUart0 ); 170 fclose( fd ); 171 172 uint8_t deviceSerial1[21] = { 0 }; 173 fd = fopen( "/proc/device-tree/aliases/serial1", "r" ); 174 if( fd == NULL ) return UART_INVALID; 175 fscanf( fd, "%20s", deviceSerial1 ); 176 fclose( fd ); 177 178 // test if uart0 is an alias for serial1 179 if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){ 180 // HW uart 181 size_t count = 0; 182 uint8_t buf[16]; 183 fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" ); 184 if( fd == NULL ) return UART_INVALID; 185 count = fread( buf, 1, 16, fd ); 186 fclose( fd ); 187 188 // contains assigned pins 189 int pins = count / 4; 190 if( pins == 4 ){ 191 return UART_HARDWARE_FLOW; 192 } else { 193 return UART_HARDWARE_NO_FLOW; 194 } 195 } else { 196 return UART_SOFTWARE_NO_FLOW; 197 } 198 } 199 200 static void phase2(int status); 201 int main(int argc, const char * argv[]){ 202 203 /// GET STARTED with BTstack /// 204 btstack_memory_init(); 205 206 // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 207 const char * pklg_path = "/tmp/hci_dump.pklg"; 208 hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 209 printf("Packet Log: %s\n", pklg_path); 210 211 // setup run loop 212 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 213 214 // pick serial port and configure uart block driver 215 transport_config.device_name = "/dev/serial1"; 216 217 // derive bd_addr from serial number 218 bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; 219 raspi_get_bd_addr(addr); 220 221 // set UART config based on raspi Bluetooth UART type 222 switch (raspi_get_bluetooth_uart_type()){ 223 case UART_INVALID: 224 fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) ); 225 return -1; 226 case UART_SOFTWARE_NO_FLOW: 227 // ?? 228 printf("Software UART without flowcontrol, 460800 baud, H5, BT_REG_EN at GPIO 128l\n"); 229 transport_config.baudrate_main = 460800; 230 transport_config.flowcontrol = 0; 231 btstack_control_raspi_set_bt_reg_en_pin(128); 232 break; 233 case UART_HARDWARE_NO_FLOW: 234 // Raspberry Pi 3 B 235 printf("Hardware UART without flowcontrol, 921600 baud, H5, BT_REG_EN at GPIOO 128\n"); 236 transport_config.baudrate_main = 921600; 237 transport_config.flowcontrol = 0; 238 btstack_control_raspi_set_bt_reg_en_pin(128); 239 break; 240 case UART_HARDWARE_FLOW: 241 // Raspberry Pi Zero W 242 printf("Hardware UART with flowcontrol, 921600 baud, H4, BT_REG_EN at GPIO 45\n"); 243 transport_config.baudrate_main = 921600; 244 transport_config.flowcontrol = 1; 245 btstack_control_raspi_set_bt_reg_en_pin(45); 246 break; 247 } 248 249 // get BCM chipset driver 250 const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 251 chipset->init(&transport_config); 252 253 // set path to firmware files 254 btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm"); 255 256 // set chipset name 257 btstack_chipset_bcm_set_device_name("BCM43430A1"); 258 259 // setup UART driver 260 const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance(); 261 262 // extract UART config from transport config 263 uart_config.baudrate = transport_config.baudrate_init; 264 uart_config.flowcontrol = transport_config.flowcontrol; 265 uart_config.device_name = transport_config.device_name; 266 uart_driver->init(&uart_config); 267 268 // HW with FlowControl -> we can use regular h4 mode 269 const hci_transport_t * transport; 270 if (transport_config.flowcontrol){ 271 transport = hci_transport_h4_instance(uart_driver); 272 } else { 273 transport = hci_transport_h5_instance(uart_driver); 274 } 275 276 // setup HCI (to be able to use bcm chipset driver) 277 const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 278 hci_init(transport, (void*) &transport_config); 279 hci_set_bd_addr( addr ); 280 hci_set_chipset(btstack_chipset_bcm_instance()); 281 hci_set_link_key_db(link_key_db); 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 // handle CTRL-c 288 signal(SIGINT, sigint_handler); 289 290 main_argc = argc; 291 main_argv = argv; 292 293 // power cycle Bluetooth controller 294 btstack_control_t *control = btstack_control_raspi_get_instance(); 295 control->init(NULL); 296 control->off(); 297 usleep( 100000 ); 298 control->on(); 299 300 // for h4, we're done 301 if (transport_config.flowcontrol){ 302 // setup app 303 btstack_main(main_argc, main_argv); 304 } else { 305 // phase #1 download firmware 306 printf("Phase 1: Download firmware\n"); 307 308 // phase #2 start main app 309 btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2); 310 } 311 312 // go 313 btstack_run_loop_execute(); 314 return 0; 315 } 316 317 static void phase2(int status){ 318 319 if (status){ 320 printf("Download firmware failed\n"); 321 return; 322 } 323 324 printf("Phase 2: Main app\n"); 325 326 // setup app 327 btstack_main(main_argc, main_argv); 328 } 329 330