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