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 68 #include "btstack_chipset_bcm.h" 69 #include "btstack_chipset_bcm_download_firmware.h" 70 #include "btstack_control_raspi.h" 71 72 int btstack_main(int argc, const char * argv[]); 73 74 typedef enum { 75 UART_INVALID, 76 UART_SOFTWARE_NO_FLOW, 77 UART_HARDWARE_NO_FLOW, 78 UART_HARDWARE_FLOW 79 } uart_type_t; 80 81 static hci_transport_config_uart_t transport_config = { 82 HCI_TRANSPORT_CONFIG_UART, 83 115200, 84 460800, // main baudrate 85 0, // flow control 86 NULL, 87 }; 88 static btstack_uart_config_t uart_config; 89 90 static int main_argc; 91 static const char ** main_argv; 92 93 static btstack_packet_callback_registration_t hci_event_callback_registration; 94 95 static void sigint_handler(int param){ 96 UNUSED(param); 97 98 printf("CTRL-C - SIGINT received, shutting down..\n"); 99 log_info("sigint_handler: shutting down"); 100 101 // reset anyway 102 btstack_stdin_reset(); 103 104 // power down 105 hci_power_control(HCI_POWER_OFF); 106 hci_close(); 107 log_info("Good bye, see you.\n"); 108 exit(0); 109 } 110 111 static int led_state = 0; 112 void hal_led_toggle(void){ 113 led_state = 1 - led_state; 114 printf("LED State %u\n", led_state); 115 } 116 117 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 118 bd_addr_t addr; 119 if (packet_type != HCI_EVENT_PACKET) return; 120 switch (hci_event_packet_get_type(packet)){ 121 case BTSTACK_EVENT_STATE: 122 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 123 gap_local_bd_addr(addr); 124 printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 125 break; 126 default: 127 break; 128 } 129 } 130 131 // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 132 static int raspi_get_bd_addr(bd_addr_t bd_addr){ 133 134 FILE *fd = fopen( "/proc/device-tree/serial-number", "r" ); 135 if( fd == NULL ){ 136 fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) ); 137 return -1; 138 } 139 fscanf( fd, "%*08" SCNx32 "%*02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] ); 140 fclose( fd ); 141 142 addr[0] = 0xb8; addr[1] = 0x27; addr[2] = 0xeb; 143 addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa; 144 145 return 0; 146 } 147 148 // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 149 // on UART_INVALID errno is set 150 static uart_type_t raspi_get_bluetooth_uart_type(void){ 151 uint8_t deviceUart0[21] = { 0 }; 152 uint8_t deviceSerial1[21] = { 0 }; 153 int fd = fopen( "/proc/device-tree/aliases/uart0", "r" ); 154 if( fd == NULL ) return UART_INVALID; 155 156 fscanf( fd, "%20s", deviceUart0 ); 157 fclose( fd ); 158 159 fd = fopen( "/proc/device-tree/aliases/serial1", "r" ); 160 if( fd == NULL ) return UART_INVALID; 161 162 fscanf( fd, "%20s", deviceSerial1 ); 163 fclose( fd ); 164 165 if( strncmp( deviceUart0, deviceSerial1, 21 ) == 0 ) 166 { 167 // HW uart 168 size_t count = 0; 169 uint8_t buf[16]; 170 fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" ); 171 if( fd == NULL ) return UART_INVALID; 172 173 count = fread( buf, 1, 16, fd ); 174 fclose( fd ); 175 176 if( count == 16 ) 177 { 178 return UART_HARDWARE_FLOW; 179 180 } else 181 { 182 return UART_HARDWARE_NO_FLOW; 183 } 184 } else 185 { 186 return UART_SOFTWARE_NO_FLOW; 187 } 188 } 189 190 static void phase2(int status); 191 int main(int argc, const char * argv[]){ 192 193 /// GET STARTED with BTstack /// 194 btstack_memory_init(); 195 196 // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 197 const char * pklg_path = "/tmp/hci_dump.pklg"; 198 hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 199 printf("Packet Log: %s\n", pklg_path); 200 201 // setup run loop 202 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 203 204 // pick serial port and configure uart block driver 205 transport_config.device_name = "/dev/serial1"; 206 207 // derive bd_addr from serial number 208 bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; 209 raspi_get_bd_addr(addr); 210 211 // set UART config based on raspi Bluetooth UART type 212 uart_type_t uart_type = raspi_get_bluetooth_uart_type(); 213 switch (raspi_get_bluetooth_uart_type()){ 214 case UART_INVALID: 215 fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) ); 216 return -1; 217 case UART_SOFTWARE_NO_FLOW: 218 printf("Software UART without flowcontrol\n"); 219 transport_config.baudrate_main = 460800; 220 transport_config.flowcontrol = 0; 221 break; 222 case UART_HARDWARE_NO_FLOW: 223 printf("Hardware UART without flowcontrol\n"); 224 transport_config.baudrate_main = 921600; 225 transport_config.flowcontrol = 0; 226 break; 227 case UART_HARDWARE_FLOW: 228 printf("Hardware UART with flowcontrol\n"); 229 transport_config.baudrate_main = 3000000; 230 transport_config.flowcontrol = 1; 231 break; 232 } 233 234 // get BCM chipset driver 235 const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 236 chipset->init(&transport_config); 237 238 // set path to firmware files 239 btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm"); 240 241 // set chipset name 242 btstack_chipset_bcm_set_device_name("BCM43430A1"); 243 244 // setup UART driver 245 const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance(); 246 247 // extract UART config from transport config 248 uart_config.baudrate = transport_config.baudrate_init; 249 uart_config.flowcontrol = transport_config.flowcontrol; 250 uart_config.device_name = transport_config.device_name; 251 uart_driver->init(&uart_config); 252 253 // setup HCI (to be able to use bcm chipset driver) 254 const hci_transport_t * transport = hci_transport_h5_instance(uart_driver); 255 const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 256 hci_init(transport, (void*) &transport_config); 257 hci_set_bd_addr( addr ); 258 259 // btstack_control_t *control = btstack_control_raspi_get_instance(); 260 // hci_set_control( control ); 261 262 hci_set_link_key_db(link_key_db); 263 hci_set_chipset(btstack_chipset_bcm_instance()); 264 265 // inform about BTstack state 266 hci_event_callback_registration.callback = &packet_handler; 267 hci_add_event_handler(&hci_event_callback_registration); 268 269 // handle CTRL-c 270 signal(SIGINT, sigint_handler); 271 272 main_argc = argc; 273 main_argv = argv; 274 275 // phase #1 download firmware 276 printf("Phase 1: Download firmware\n"); 277 278 // control->off(); 279 // sleep( 1 ); 280 // control->on(); 281 282 // phase #2 start main app 283 btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2); 284 285 // go 286 btstack_run_loop_execute(); 287 return 0; 288 } 289 290 static void phase2(int status){ 291 292 if (status){ 293 printf("Download firmware failed\n"); 294 return; 295 } 296 297 printf("Phase 2: Main app\n"); 298 299 // setup app 300 btstack_main(main_argc, main_argv); 301 } 302 303