11182cb7eSMatthias Ringwald /* 21182cb7eSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 31182cb7eSMatthias Ringwald * 41182cb7eSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 51182cb7eSMatthias Ringwald * modification, are permitted provided that the following conditions 61182cb7eSMatthias Ringwald * are met: 71182cb7eSMatthias Ringwald * 81182cb7eSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 91182cb7eSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 101182cb7eSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 111182cb7eSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 121182cb7eSMatthias Ringwald * documentation and/or other materials provided with the distribution. 131182cb7eSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 141182cb7eSMatthias Ringwald * contributors may be used to endorse or promote products derived 151182cb7eSMatthias Ringwald * from this software without specific prior written permission. 161182cb7eSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 171182cb7eSMatthias Ringwald * personal benefit and not for any commercial purpose or for 181182cb7eSMatthias Ringwald * monetary gain. 191182cb7eSMatthias Ringwald * 201182cb7eSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211182cb7eSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221182cb7eSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231182cb7eSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241182cb7eSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251182cb7eSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261182cb7eSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271182cb7eSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281182cb7eSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291182cb7eSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301182cb7eSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311182cb7eSMatthias Ringwald * SUCH DAMAGE. 321182cb7eSMatthias Ringwald * 331182cb7eSMatthias Ringwald * Please inquire about commercial licensing options at 341182cb7eSMatthias Ringwald * [email protected] 351182cb7eSMatthias Ringwald * 361182cb7eSMatthias Ringwald */ 371182cb7eSMatthias Ringwald 381182cb7eSMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 391182cb7eSMatthias Ringwald 401182cb7eSMatthias Ringwald // ***************************************************************************** 411182cb7eSMatthias Ringwald // 421182cb7eSMatthias Ringwald // minimal setup for HCI code 431182cb7eSMatthias Ringwald // 441182cb7eSMatthias Ringwald // ***************************************************************************** 451182cb7eSMatthias Ringwald 461182cb7eSMatthias Ringwald #include <stdint.h> 471182cb7eSMatthias Ringwald #include <stdio.h> 481182cb7eSMatthias Ringwald #include <stdlib.h> 491182cb7eSMatthias Ringwald #include <string.h> 501182cb7eSMatthias Ringwald #include <signal.h> 511182cb7eSMatthias Ringwald 521182cb7eSMatthias Ringwald #include "btstack_config.h" 531182cb7eSMatthias Ringwald 541182cb7eSMatthias Ringwald #include "bluetooth_company_id.h" 551182cb7eSMatthias Ringwald #include "ble/le_device_db_tlv.h" 561182cb7eSMatthias Ringwald #include "btstack_chipset_bcm.h" 571182cb7eSMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h" 581182cb7eSMatthias Ringwald #include "btstack_debug.h" 591182cb7eSMatthias Ringwald #include "btstack_event.h" 601182cb7eSMatthias Ringwald #include "btstack_memory.h" 611182cb7eSMatthias Ringwald #include "btstack_run_loop.h" 621182cb7eSMatthias Ringwald #include "btstack_run_loop_posix.h" 631182cb7eSMatthias Ringwald #include "btstack_stdin.h" 641182cb7eSMatthias Ringwald #include "btstack_uart.h" 651182cb7eSMatthias Ringwald #include "btstack_tlv_posix.h" 661182cb7eSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 671182cb7eSMatthias Ringwald #include "hci.h" 681182cb7eSMatthias Ringwald #include "hci_dump.h" 691182cb7eSMatthias Ringwald 701182cb7eSMatthias Ringwald 711182cb7eSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 721182cb7eSMatthias Ringwald 731182cb7eSMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 741182cb7eSMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 751182cb7eSMatthias Ringwald static char tlv_db_path[100]; 761182cb7eSMatthias Ringwald static const btstack_tlv_t * tlv_impl; 771182cb7eSMatthias Ringwald static btstack_tlv_posix_t tlv_context; 781182cb7eSMatthias Ringwald 791182cb7eSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 801182cb7eSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 811182cb7eSMatthias Ringwald 115200, 821182cb7eSMatthias Ringwald 921600, // main baudrate 831182cb7eSMatthias Ringwald 0, // flow control 841182cb7eSMatthias Ringwald NULL, 851182cb7eSMatthias Ringwald BTSTACK_UART_PARITY_EVEN, // parity 861182cb7eSMatthias Ringwald }; 871182cb7eSMatthias Ringwald static btstack_uart_config_t uart_config; 881182cb7eSMatthias Ringwald 891182cb7eSMatthias Ringwald static int main_argc; 901182cb7eSMatthias Ringwald static const char ** main_argv; 911182cb7eSMatthias Ringwald 921182cb7eSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 931182cb7eSMatthias Ringwald 941182cb7eSMatthias Ringwald static void sigint_handler(int param){ 951182cb7eSMatthias Ringwald UNUSED(param); 961182cb7eSMatthias Ringwald 971182cb7eSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 981182cb7eSMatthias Ringwald log_info("sigint_handler: shutting down"); 991182cb7eSMatthias Ringwald 1001182cb7eSMatthias Ringwald // reset anyway 1011182cb7eSMatthias Ringwald btstack_stdin_reset(); 1021182cb7eSMatthias Ringwald 1031182cb7eSMatthias Ringwald // power down 1041182cb7eSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 1051182cb7eSMatthias Ringwald hci_close(); 1061182cb7eSMatthias Ringwald log_info("Good bye, see you.\n"); 1071182cb7eSMatthias Ringwald exit(0); 1081182cb7eSMatthias Ringwald } 1091182cb7eSMatthias Ringwald 1101182cb7eSMatthias Ringwald static int led_state = 0; 1111182cb7eSMatthias Ringwald void hal_led_toggle(void){ 1121182cb7eSMatthias Ringwald led_state = 1 - led_state; 1131182cb7eSMatthias Ringwald printf("LED State %u\n", led_state); 1141182cb7eSMatthias Ringwald } 1151182cb7eSMatthias Ringwald 1161182cb7eSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1171182cb7eSMatthias Ringwald bd_addr_t addr; 1181182cb7eSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1191182cb7eSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 1201182cb7eSMatthias Ringwald case BTSTACK_EVENT_STATE: 1211182cb7eSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 1221182cb7eSMatthias Ringwald gap_local_bd_addr(addr); 1231182cb7eSMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 1241182cb7eSMatthias Ringwald // setup TLV 1251182cb7eSMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1261182cb7eSMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 1271182cb7eSMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1281182cb7eSMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1291182cb7eSMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 1301182cb7eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1311182cb7eSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 1321182cb7eSMatthias Ringwald #endif 1331182cb7eSMatthias Ringwald #ifdef ENABLE_BLE 1341182cb7eSMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 1351182cb7eSMatthias Ringwald #endif 1361182cb7eSMatthias Ringwald break; 1371182cb7eSMatthias Ringwald default: 1381182cb7eSMatthias Ringwald break; 1391182cb7eSMatthias Ringwald } 1401182cb7eSMatthias Ringwald } 1411182cb7eSMatthias Ringwald 1421182cb7eSMatthias Ringwald static void phase2(int status); 1431182cb7eSMatthias Ringwald int main(int argc, const char * argv[]){ 1441182cb7eSMatthias Ringwald 1451182cb7eSMatthias Ringwald /// GET STARTED with BTstack /// 1461182cb7eSMatthias Ringwald btstack_memory_init(); 1471182cb7eSMatthias Ringwald 1481182cb7eSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 1491182cb7eSMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 150*98451c7bSMatthias Ringwald // hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 1511182cb7eSMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 1521182cb7eSMatthias Ringwald 1531182cb7eSMatthias Ringwald // setup run loop 1541182cb7eSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 1551182cb7eSMatthias Ringwald 1561182cb7eSMatthias Ringwald // pick serial port and configure uart block driver 1571182cb7eSMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A9OVNX5P"; // RedBear IoT pHAT breakout board 1581182cb7eSMatthias Ringwald 1591182cb7eSMatthias Ringwald // get BCM chipset driver 1601182cb7eSMatthias Ringwald const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 1611182cb7eSMatthias Ringwald chipset->init(&transport_config); 1621182cb7eSMatthias Ringwald 1631182cb7eSMatthias Ringwald // set chipset name 1641182cb7eSMatthias Ringwald btstack_chipset_bcm_set_device_name("BCM43430A1"); 1651182cb7eSMatthias Ringwald 1661182cb7eSMatthias Ringwald // setup UART driver 1671182cb7eSMatthias Ringwald const btstack_uart_t * uart_driver = (const btstack_uart_t *) btstack_uart_posix_instance(); 1681182cb7eSMatthias Ringwald 1691182cb7eSMatthias Ringwald // extract UART config from transport config 1701182cb7eSMatthias Ringwald uart_config.baudrate = transport_config.baudrate_init; 1711182cb7eSMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 1721182cb7eSMatthias Ringwald uart_config.device_name = transport_config.device_name; 1731182cb7eSMatthias Ringwald uart_driver->init(&uart_config); 1741182cb7eSMatthias Ringwald 1751182cb7eSMatthias Ringwald 1761182cb7eSMatthias Ringwald // setup HCI (to be able to use bcm chipset driver) 1771182cb7eSMatthias Ringwald // init HCI 1781182cb7eSMatthias Ringwald const hci_transport_t * transport = hci_transport_h5_instance(uart_driver); 1791182cb7eSMatthias Ringwald hci_init(transport, (void*) &transport_config); 1801182cb7eSMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 1811182cb7eSMatthias Ringwald 1821182cb7eSMatthias Ringwald // inform about BTstack state 1831182cb7eSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1841182cb7eSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1851182cb7eSMatthias Ringwald 1861182cb7eSMatthias Ringwald // handle CTRL-c 1871182cb7eSMatthias Ringwald signal(SIGINT, sigint_handler); 1881182cb7eSMatthias Ringwald 1891182cb7eSMatthias Ringwald main_argc = argc; 1901182cb7eSMatthias Ringwald main_argv = argv; 1911182cb7eSMatthias Ringwald 1921182cb7eSMatthias Ringwald // phase #1 download firmware 1931182cb7eSMatthias Ringwald printf("Phase 1: Download firmware\n"); 1941182cb7eSMatthias Ringwald 1951182cb7eSMatthias Ringwald // phase #2 start main app 1961182cb7eSMatthias Ringwald btstack_chipset_bcm_download_firmware_with_uart(uart_driver, transport_config.baudrate_main, &phase2); 1971182cb7eSMatthias Ringwald 1981182cb7eSMatthias Ringwald // go 1991182cb7eSMatthias Ringwald btstack_run_loop_execute(); 2001182cb7eSMatthias Ringwald return 0; 2011182cb7eSMatthias Ringwald } 2021182cb7eSMatthias Ringwald 2031182cb7eSMatthias Ringwald static void phase2(int status){ 2041182cb7eSMatthias Ringwald 2051182cb7eSMatthias Ringwald if (status){ 2061182cb7eSMatthias Ringwald printf("Download firmware failed\n"); 2071182cb7eSMatthias Ringwald return; 2081182cb7eSMatthias Ringwald } 2091182cb7eSMatthias Ringwald 2101182cb7eSMatthias Ringwald printf("Phase 2: Main app\n"); 2111182cb7eSMatthias Ringwald 2121182cb7eSMatthias Ringwald // setup app 2131182cb7eSMatthias Ringwald btstack_main(main_argc, main_argv); 2141182cb7eSMatthias Ringwald } 2151182cb7eSMatthias Ringwald 216