1e72b198fSMatthias Ringwald /* 2e72b198fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3e72b198fSMatthias Ringwald * 4e72b198fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5e72b198fSMatthias Ringwald * modification, are permitted provided that the following conditions 6e72b198fSMatthias Ringwald * are met: 7e72b198fSMatthias Ringwald * 8e72b198fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9e72b198fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10e72b198fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11e72b198fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12e72b198fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13e72b198fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14e72b198fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15e72b198fSMatthias Ringwald * from this software without specific prior written permission. 16e72b198fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17e72b198fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18e72b198fSMatthias Ringwald * monetary gain. 19e72b198fSMatthias Ringwald * 20e72b198fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21e72b198fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22e72b198fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23e72b198fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24e72b198fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25e72b198fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26e72b198fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27e72b198fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28e72b198fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29e72b198fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30e72b198fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31e72b198fSMatthias Ringwald * SUCH DAMAGE. 32e72b198fSMatthias Ringwald * 33e72b198fSMatthias Ringwald * Please inquire about commercial licensing options at 34e72b198fSMatthias Ringwald * [email protected] 35e72b198fSMatthias Ringwald * 36e72b198fSMatthias Ringwald */ 37e72b198fSMatthias Ringwald 38e72b198fSMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39e72b198fSMatthias Ringwald 40e72b198fSMatthias Ringwald // ***************************************************************************** 41e72b198fSMatthias Ringwald // 42e72b198fSMatthias Ringwald // minimal setup for HCI code 43e72b198fSMatthias Ringwald // 44e72b198fSMatthias Ringwald // ***************************************************************************** 45e72b198fSMatthias Ringwald 46e72b198fSMatthias Ringwald #include <stdint.h> 47e72b198fSMatthias Ringwald #include <stdio.h> 48e72b198fSMatthias Ringwald #include <stdlib.h> 49e72b198fSMatthias Ringwald #include <string.h> 50e72b198fSMatthias Ringwald #include <signal.h> 51e72b198fSMatthias Ringwald 52e72b198fSMatthias Ringwald #include "btstack_config.h" 53e72b198fSMatthias Ringwald 54e72b198fSMatthias Ringwald #include "btstack_debug.h" 55e72b198fSMatthias Ringwald #include "btstack_event.h" 56e72b198fSMatthias Ringwald #include "btstack_memory.h" 57e72b198fSMatthias Ringwald #include "btstack_run_loop.h" 58e72b198fSMatthias Ringwald #include "btstack_run_loop_posix.h" 59*81862996SMatthias Ringwald #include "ble/le_device_db_tlv.h" 60e72b198fSMatthias Ringwald #include "hci.h" 61e72b198fSMatthias Ringwald #include "hci_dump.h" 627ea7688aSMatthias Ringwald #include "btstack_stdin.h" 637daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h" 64e72b198fSMatthias Ringwald 65e72b198fSMatthias Ringwald #include "btstack_chipset_da14581.h" 66e72b198fSMatthias Ringwald #include "hci_581_active_uart.h" 67e72b198fSMatthias Ringwald 68e72b198fSMatthias Ringwald static int main_argc; 69e72b198fSMatthias Ringwald static const char ** main_argv; 70e72b198fSMatthias Ringwald static const btstack_uart_block_t * uart_driver; 71e72b198fSMatthias Ringwald static btstack_uart_config_t uart_config; 72e72b198fSMatthias Ringwald 737daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 747daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 757daa8bd9SMatthias Ringwald static char tlv_db_path[100]; 767daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 777daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 787daa8bd9SMatthias Ringwald 79e72b198fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 80e72b198fSMatthias Ringwald 81e72b198fSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 82e72b198fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 83e72b198fSMatthias Ringwald 115200, 84e72b198fSMatthias Ringwald 0, // main baudrate 85e72b198fSMatthias Ringwald 1, // flow control 86e72b198fSMatthias Ringwald NULL, 87e72b198fSMatthias Ringwald }; 88e72b198fSMatthias Ringwald 89e72b198fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 90e72b198fSMatthias Ringwald 91e72b198fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 927daa8bd9SMatthias Ringwald bd_addr_t addr; 93e72b198fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 94e72b198fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 95e72b198fSMatthias Ringwald case BTSTACK_EVENT_STATE: 96e72b198fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 977daa8bd9SMatthias Ringwald gap_local_bd_addr(addr); 987daa8bd9SMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 997daa8bd9SMatthias Ringwald // setup TLV 1007daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1017daa8bd9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 1027daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1037daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1047daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 105*81862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 106e72b198fSMatthias Ringwald break; 107e72b198fSMatthias Ringwald default: 108e72b198fSMatthias Ringwald break; 109e72b198fSMatthias Ringwald } 110e72b198fSMatthias Ringwald } 111e72b198fSMatthias Ringwald 112e72b198fSMatthias Ringwald static void sigint_handler(int param){ 113e72b198fSMatthias Ringwald UNUSED(param); 114e72b198fSMatthias Ringwald 115e72b198fSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 116e72b198fSMatthias Ringwald log_info("sigint_handler: shutting down"); 117e72b198fSMatthias Ringwald 118e72b198fSMatthias Ringwald // reset anyway 119e72b198fSMatthias Ringwald btstack_stdin_reset(); 120e72b198fSMatthias Ringwald 121e72b198fSMatthias Ringwald // power down 122e72b198fSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 123e72b198fSMatthias Ringwald hci_close(); 124e72b198fSMatthias Ringwald log_info("Good bye, see you.\n"); 125e72b198fSMatthias Ringwald exit(0); 126e72b198fSMatthias Ringwald } 127e72b198fSMatthias Ringwald 128e72b198fSMatthias Ringwald static int led_state = 0; 129e72b198fSMatthias Ringwald void hal_led_toggle(void){ 130e72b198fSMatthias Ringwald led_state = 1 - led_state; 131e72b198fSMatthias Ringwald printf("LED State %u\n", led_state); 132e72b198fSMatthias Ringwald } 133e72b198fSMatthias Ringwald 134e72b198fSMatthias Ringwald static void phase2(int status){ 135e72b198fSMatthias Ringwald 136e72b198fSMatthias Ringwald if (status){ 137e72b198fSMatthias Ringwald printf("Download firmware failed\n"); 138e72b198fSMatthias Ringwald return; 139e72b198fSMatthias Ringwald } 140e72b198fSMatthias Ringwald 141e72b198fSMatthias Ringwald printf("Phase 2: Main app\n"); 142e72b198fSMatthias Ringwald 143e72b198fSMatthias Ringwald // init HCI 144e72b198fSMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 145e72b198fSMatthias Ringwald hci_init(transport, (void*) &transport_config); 146e72b198fSMatthias Ringwald 147e72b198fSMatthias Ringwald // inform about BTstack state 148e72b198fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 149e72b198fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 150e72b198fSMatthias Ringwald 151e72b198fSMatthias Ringwald // handle CTRL-c 152e72b198fSMatthias Ringwald signal(SIGINT, sigint_handler); 153e72b198fSMatthias Ringwald 154e72b198fSMatthias Ringwald // setup app 155e72b198fSMatthias Ringwald btstack_main(main_argc, main_argv); 156e72b198fSMatthias Ringwald } 157e72b198fSMatthias Ringwald 158e72b198fSMatthias Ringwald 159e72b198fSMatthias Ringwald int main(int argc, const char * argv[]){ 160e72b198fSMatthias Ringwald 161e72b198fSMatthias Ringwald /// GET STARTED with BTstack /// 162e72b198fSMatthias Ringwald btstack_memory_init(); 163e72b198fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 164e72b198fSMatthias Ringwald 165e72b198fSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 166785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 167785879c6SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 168785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 169e72b198fSMatthias Ringwald 170e72b198fSMatthias Ringwald // pick serial port and configure uart block driver 171e72b198fSMatthias Ringwald transport_config.device_name = "/dev/tty.usbmodem1442311"; 172e72b198fSMatthias Ringwald uart_driver = btstack_uart_block_posix_instance(); 173e72b198fSMatthias Ringwald 174e72b198fSMatthias Ringwald // extract UART config from transport config, but overide initial uart speed 175e72b198fSMatthias Ringwald uart_config.baudrate = 57600; 176e72b198fSMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 177e72b198fSMatthias Ringwald uart_config.device_name = transport_config.device_name; 178e72b198fSMatthias Ringwald uart_driver->init(&uart_config); 179e72b198fSMatthias Ringwald 180e72b198fSMatthias Ringwald main_argc = argc; 181e72b198fSMatthias Ringwald main_argv = argv; 182e72b198fSMatthias Ringwald 183e72b198fSMatthias Ringwald // phase #1 download firmware 184e72b198fSMatthias Ringwald printf("Phase 1: Download firmware\n"); 185e72b198fSMatthias Ringwald 186e72b198fSMatthias Ringwald // phase #2 start main app 187e72b198fSMatthias Ringwald btstack_chipset_da14581_download_firmware(uart_driver, da14581_fw_data, da14581_fw_size, &phase2); 188e72b198fSMatthias Ringwald 189e72b198fSMatthias Ringwald // go 190e72b198fSMatthias Ringwald btstack_run_loop_execute(); 191e72b198fSMatthias Ringwald return 0; 192e72b198fSMatthias Ringwald } 193