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 <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <signal.h> 51 52 #include "btstack_config.h" 53 54 #include "btstack_debug.h" 55 #include "btstack_event.h" 56 #include "btstack_link_key_db_fs.h" 57 #include "btstack_memory.h" 58 #include "btstack_run_loop.h" 59 #include "btstack_run_loop_posix.h" 60 #include "bluetooth_company_id.h" 61 #include "hci.h" 62 #include "hci_dump.h" 63 #include "btstack_stdin.h" 64 65 #include "btstack_chipset_bcm.h" 66 #include "btstack_chipset_bcm_download_firmware.h" 67 68 int btstack_main(int argc, const char * argv[]); 69 70 71 static hci_transport_config_uart_t transport_config = { 72 HCI_TRANSPORT_CONFIG_UART, 73 115200, 74 460800, // main baudrate 75 0, // flow control 76 NULL, 77 }; 78 static btstack_uart_config_t uart_config; 79 80 static int main_argc; 81 static const char ** main_argv; 82 83 static btstack_packet_callback_registration_t hci_event_callback_registration; 84 85 static void sigint_handler(int param){ 86 UNUSED(param); 87 88 printf("CTRL-C - SIGINT received, shutting down..\n"); 89 log_info("sigint_handler: shutting down"); 90 91 // reset anyway 92 btstack_stdin_reset(); 93 94 // power down 95 hci_power_control(HCI_POWER_OFF); 96 hci_close(); 97 log_info("Good bye, see you.\n"); 98 exit(0); 99 } 100 101 static int led_state = 0; 102 void hal_led_toggle(void){ 103 led_state = 1 - led_state; 104 printf("LED State %u\n", led_state); 105 } 106 107 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 108 bd_addr_t addr; 109 if (packet_type != HCI_EVENT_PACKET) return; 110 switch (hci_event_packet_get_type(packet)){ 111 case BTSTACK_EVENT_STATE: 112 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 113 gap_local_bd_addr(addr); 114 printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 115 break; 116 default: 117 break; 118 } 119 } 120 121 static void phase2(int status); 122 int main(int argc, const char * argv[]){ 123 124 /// GET STARTED with BTstack /// 125 btstack_memory_init(); 126 127 // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 128 const char * pklg_path = "/tmp/hci_dump.pklg"; 129 hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 130 printf("Packet Log: %s\n", pklg_path); 131 132 // setup run loop 133 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 134 135 // pick serial port and configure uart block driver 136 transport_config.device_name = "/dev/serial1"; 137 138 // TODO: derive BD_ADD from serial number 139 // https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 140 141 // TODO: check device tree if flowcontrol is supported/connected 142 // https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 143 144 // TODO: decide on baud rate based on hw flowcontrol and if hw uart (uart0) is used 145 // https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 146 147 // TODO: power cycle Bluetooth module 148 // toggle interal gpio 128 149 150 // get BCM chipset driver 151 const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 152 chipset->init(&transport_config); 153 154 // set path to firmware files 155 btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware"); 156 157 // set chipset name 158 btstack_chipset_bcm_set_device_name("BCM43430A1"); 159 160 // setup UART driver 161 const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance(); 162 163 // extract UART config from transport config 164 uart_config.baudrate = transport_config.baudrate_init; 165 uart_config.flowcontrol = transport_config.flowcontrol; 166 uart_config.device_name = transport_config.device_name; 167 uart_driver->init(&uart_config); 168 169 // setup HCI (to be able to use bcm chipset driver) 170 const hci_transport_t * transport = hci_transport_h5_instance(uart_driver); 171 const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 172 hci_init(transport, (void*) &transport_config); 173 hci_set_link_key_db(link_key_db); 174 hci_set_chipset(btstack_chipset_bcm_instance()); 175 176 // inform about BTstack state 177 hci_event_callback_registration.callback = &packet_handler; 178 hci_add_event_handler(&hci_event_callback_registration); 179 180 // handle CTRL-c 181 signal(SIGINT, sigint_handler); 182 183 main_argc = argc; 184 main_argv = argv; 185 186 // phase #1 download firmware 187 printf("Phase 1: Download firmware\n"); 188 189 // phase #2 start main app 190 btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2); 191 192 // go 193 btstack_run_loop_execute(); 194 return 0; 195 } 196 197 static void phase2(int status){ 198 199 if (status){ 200 printf("Download firmware failed\n"); 201 return; 202 } 203 204 printf("Phase 2: Main app\n"); 205 206 // setup app 207 btstack_main(main_argc, main_argv); 208 } 209 210