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" 59793a0509SMatthias Ringwald #include "btstack_uart.h" 6081862996SMatthias Ringwald #include "ble/le_device_db_tlv.h" 61e72b198fSMatthias Ringwald #include "hci.h" 62e72b198fSMatthias Ringwald #include "hci_dump.h" 63*7435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 647ea7688aSMatthias Ringwald #include "btstack_stdin.h" 657daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h" 66e72b198fSMatthias Ringwald 67e72b198fSMatthias Ringwald #include "btstack_chipset_da14581.h" 68e72b198fSMatthias Ringwald #include "hci_581_active_uart.h" 69e72b198fSMatthias Ringwald 70e72b198fSMatthias Ringwald static int main_argc; 71e72b198fSMatthias Ringwald static const char ** main_argv; 72793a0509SMatthias Ringwald static const btstack_uart_t * uart_driver; 73e72b198fSMatthias Ringwald static btstack_uart_config_t uart_config; 74e72b198fSMatthias Ringwald 757daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 767daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 777daa8bd9SMatthias Ringwald static char tlv_db_path[100]; 787daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 797daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 807daa8bd9SMatthias Ringwald 81e72b198fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 82e72b198fSMatthias Ringwald 83e72b198fSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 84e72b198fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 85e72b198fSMatthias Ringwald 115200, 86e72b198fSMatthias Ringwald 0, // main baudrate 87e72b198fSMatthias Ringwald 1, // flow control 88e72b198fSMatthias Ringwald NULL, 89e72b198fSMatthias Ringwald }; 90e72b198fSMatthias Ringwald 91e72b198fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 92e72b198fSMatthias Ringwald 93e72b198fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 947daa8bd9SMatthias Ringwald bd_addr_t addr; 95e72b198fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 96e72b198fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 97e72b198fSMatthias Ringwald case BTSTACK_EVENT_STATE: 98e72b198fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 997daa8bd9SMatthias Ringwald gap_local_bd_addr(addr); 1007daa8bd9SMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 1017daa8bd9SMatthias Ringwald // setup TLV 1027daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1037daa8bd9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 1047daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1057daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1067daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 10781862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 108e72b198fSMatthias Ringwald break; 109e72b198fSMatthias Ringwald default: 110e72b198fSMatthias Ringwald break; 111e72b198fSMatthias Ringwald } 112e72b198fSMatthias Ringwald } 113e72b198fSMatthias Ringwald 114e72b198fSMatthias Ringwald static void sigint_handler(int param){ 115e72b198fSMatthias Ringwald UNUSED(param); 116e72b198fSMatthias Ringwald 117e72b198fSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 118e72b198fSMatthias Ringwald log_info("sigint_handler: shutting down"); 119e72b198fSMatthias Ringwald 120e72b198fSMatthias Ringwald // reset anyway 121e72b198fSMatthias Ringwald btstack_stdin_reset(); 122e72b198fSMatthias Ringwald 123e72b198fSMatthias Ringwald // power down 124e72b198fSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 125e72b198fSMatthias Ringwald hci_close(); 126e72b198fSMatthias Ringwald log_info("Good bye, see you.\n"); 127e72b198fSMatthias Ringwald exit(0); 128e72b198fSMatthias Ringwald } 129e72b198fSMatthias Ringwald 130e72b198fSMatthias Ringwald static int led_state = 0; 131e72b198fSMatthias Ringwald void hal_led_toggle(void){ 132e72b198fSMatthias Ringwald led_state = 1 - led_state; 133e72b198fSMatthias Ringwald printf("LED State %u\n", led_state); 134e72b198fSMatthias Ringwald } 135e72b198fSMatthias Ringwald 136e72b198fSMatthias Ringwald static void phase2(int status){ 137e72b198fSMatthias Ringwald 138e72b198fSMatthias Ringwald if (status){ 139e72b198fSMatthias Ringwald printf("Download firmware failed\n"); 140e72b198fSMatthias Ringwald return; 141e72b198fSMatthias Ringwald } 142e72b198fSMatthias Ringwald 143e72b198fSMatthias Ringwald printf("Phase 2: Main app\n"); 144e72b198fSMatthias Ringwald 145e72b198fSMatthias Ringwald // init HCI 146793a0509SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 147e72b198fSMatthias Ringwald hci_init(transport, (void*) &transport_config); 148e72b198fSMatthias Ringwald 149e72b198fSMatthias Ringwald // inform about BTstack state 150e72b198fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 151e72b198fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 152e72b198fSMatthias Ringwald 153e72b198fSMatthias Ringwald // handle CTRL-c 154e72b198fSMatthias Ringwald signal(SIGINT, sigint_handler); 155e72b198fSMatthias Ringwald 156e72b198fSMatthias Ringwald // setup app 157e72b198fSMatthias Ringwald btstack_main(main_argc, main_argv); 158e72b198fSMatthias Ringwald } 159e72b198fSMatthias Ringwald 160e72b198fSMatthias Ringwald 161e72b198fSMatthias Ringwald int main(int argc, const char * argv[]){ 162e72b198fSMatthias Ringwald 163e72b198fSMatthias Ringwald /// GET STARTED with BTstack /// 164e72b198fSMatthias Ringwald btstack_memory_init(); 165e72b198fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 166e72b198fSMatthias Ringwald 167*7435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 168785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 169*7435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 170*7435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 171*7435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 172785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 173e72b198fSMatthias Ringwald 174e72b198fSMatthias Ringwald // pick serial port and configure uart block driver 175e72b198fSMatthias Ringwald transport_config.device_name = "/dev/tty.usbmodem1442311"; 176793a0509SMatthias Ringwald uart_driver = btstack_uart_posix_instance(); 177e72b198fSMatthias Ringwald 178e72b198fSMatthias Ringwald // extract UART config from transport config, but overide initial uart speed 179e72b198fSMatthias Ringwald uart_config.baudrate = 57600; 180e72b198fSMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 181e72b198fSMatthias Ringwald uart_config.device_name = transport_config.device_name; 182e72b198fSMatthias Ringwald uart_driver->init(&uart_config); 183e72b198fSMatthias Ringwald 184e72b198fSMatthias Ringwald main_argc = argc; 185e72b198fSMatthias Ringwald main_argv = argv; 186e72b198fSMatthias Ringwald 187e72b198fSMatthias Ringwald // phase #1 download firmware 188e72b198fSMatthias Ringwald printf("Phase 1: Download firmware\n"); 189e72b198fSMatthias Ringwald 190e72b198fSMatthias Ringwald // phase #2 start main app 191793a0509SMatthias Ringwald btstack_chipset_da14581_download_firmware_with_uart(uart_driver, da14581_fw_data, da14581_fw_size, &phase2); 192e72b198fSMatthias Ringwald 193e72b198fSMatthias Ringwald // go 194e72b198fSMatthias Ringwald btstack_run_loop_execute(); 195e72b198fSMatthias Ringwald return 0; 196e72b198fSMatthias Ringwald } 197