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_link_key_db_fs.h" 57e72b198fSMatthias Ringwald #include "btstack_memory.h" 58e72b198fSMatthias Ringwald #include "btstack_run_loop.h" 59e72b198fSMatthias Ringwald #include "btstack_run_loop_posix.h" 60e72b198fSMatthias Ringwald #include "hci.h" 61e72b198fSMatthias Ringwald #include "hci_dump.h" 627ea7688aSMatthias Ringwald #include "btstack_stdin.h" 63e72b198fSMatthias Ringwald 64e72b198fSMatthias Ringwald #include "btstack_chipset_da14581.h" 65e72b198fSMatthias Ringwald #include "hci_581_active_uart.h" 66e72b198fSMatthias Ringwald 67e72b198fSMatthias Ringwald static int main_argc; 68e72b198fSMatthias Ringwald static const char ** main_argv; 69e72b198fSMatthias Ringwald static const btstack_uart_block_t * uart_driver; 70e72b198fSMatthias Ringwald static btstack_uart_config_t uart_config; 71e72b198fSMatthias Ringwald 72e72b198fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 73e72b198fSMatthias Ringwald 74e72b198fSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 75e72b198fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 76e72b198fSMatthias Ringwald 115200, 77e72b198fSMatthias Ringwald 0, // main baudrate 78e72b198fSMatthias Ringwald 1, // flow control 79e72b198fSMatthias Ringwald NULL, 80e72b198fSMatthias Ringwald }; 81e72b198fSMatthias Ringwald 82e72b198fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 83e72b198fSMatthias Ringwald 84e72b198fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 85e72b198fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 86e72b198fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 87e72b198fSMatthias Ringwald case BTSTACK_EVENT_STATE: 88e72b198fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 89e72b198fSMatthias Ringwald printf("BTstack up and running.\n"); 90e72b198fSMatthias Ringwald break; 91e72b198fSMatthias Ringwald default: 92e72b198fSMatthias Ringwald break; 93e72b198fSMatthias Ringwald } 94e72b198fSMatthias Ringwald } 95e72b198fSMatthias Ringwald 96e72b198fSMatthias Ringwald static void sigint_handler(int param){ 97e72b198fSMatthias Ringwald UNUSED(param); 98e72b198fSMatthias Ringwald 99e72b198fSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 100e72b198fSMatthias Ringwald log_info("sigint_handler: shutting down"); 101e72b198fSMatthias Ringwald 102e72b198fSMatthias Ringwald // reset anyway 103e72b198fSMatthias Ringwald btstack_stdin_reset(); 104e72b198fSMatthias Ringwald 105e72b198fSMatthias Ringwald // power down 106e72b198fSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 107e72b198fSMatthias Ringwald hci_close(); 108e72b198fSMatthias Ringwald log_info("Good bye, see you.\n"); 109e72b198fSMatthias Ringwald exit(0); 110e72b198fSMatthias Ringwald } 111e72b198fSMatthias Ringwald 112e72b198fSMatthias Ringwald static int led_state = 0; 113e72b198fSMatthias Ringwald void hal_led_toggle(void){ 114e72b198fSMatthias Ringwald led_state = 1 - led_state; 115e72b198fSMatthias Ringwald printf("LED State %u\n", led_state); 116e72b198fSMatthias Ringwald } 117e72b198fSMatthias Ringwald 118e72b198fSMatthias Ringwald static void phase2(int status){ 119e72b198fSMatthias Ringwald 120e72b198fSMatthias Ringwald if (status){ 121e72b198fSMatthias Ringwald printf("Download firmware failed\n"); 122e72b198fSMatthias Ringwald return; 123e72b198fSMatthias Ringwald } 124e72b198fSMatthias Ringwald 125e72b198fSMatthias Ringwald printf("Phase 2: Main app\n"); 126e72b198fSMatthias Ringwald 127e72b198fSMatthias Ringwald // init HCI 128e72b198fSMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 129e72b198fSMatthias Ringwald const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 130e72b198fSMatthias Ringwald hci_init(transport, (void*) &transport_config); 131e72b198fSMatthias Ringwald hci_set_link_key_db(link_key_db); 132e72b198fSMatthias Ringwald 133e72b198fSMatthias Ringwald // inform about BTstack state 134e72b198fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 135e72b198fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 136e72b198fSMatthias Ringwald 137e72b198fSMatthias Ringwald // handle CTRL-c 138e72b198fSMatthias Ringwald signal(SIGINT, sigint_handler); 139e72b198fSMatthias Ringwald 140e72b198fSMatthias Ringwald // setup app 141e72b198fSMatthias Ringwald btstack_main(main_argc, main_argv); 142e72b198fSMatthias Ringwald } 143e72b198fSMatthias Ringwald 144e72b198fSMatthias Ringwald 145e72b198fSMatthias Ringwald int main(int argc, const char * argv[]){ 146e72b198fSMatthias Ringwald 147e72b198fSMatthias Ringwald /// GET STARTED with BTstack /// 148e72b198fSMatthias Ringwald btstack_memory_init(); 149e72b198fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 150e72b198fSMatthias Ringwald 151e72b198fSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 152*785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 153*785879c6SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 154*785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 155e72b198fSMatthias Ringwald 156e72b198fSMatthias Ringwald // pick serial port and configure uart block driver 157e72b198fSMatthias Ringwald transport_config.device_name = "/dev/tty.usbmodem1442311"; 158e72b198fSMatthias Ringwald uart_driver = btstack_uart_block_posix_instance(); 159e72b198fSMatthias Ringwald 160e72b198fSMatthias Ringwald // extract UART config from transport config, but overide initial uart speed 161e72b198fSMatthias Ringwald uart_config.baudrate = 57600; 162e72b198fSMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 163e72b198fSMatthias Ringwald uart_config.device_name = transport_config.device_name; 164e72b198fSMatthias Ringwald uart_driver->init(&uart_config); 165e72b198fSMatthias Ringwald 166e72b198fSMatthias Ringwald main_argc = argc; 167e72b198fSMatthias Ringwald main_argv = argv; 168e72b198fSMatthias Ringwald 169e72b198fSMatthias Ringwald // phase #1 download firmware 170e72b198fSMatthias Ringwald printf("Phase 1: Download firmware\n"); 171e72b198fSMatthias Ringwald 172e72b198fSMatthias Ringwald // phase #2 start main app 173e72b198fSMatthias Ringwald btstack_chipset_da14581_download_firmware(uart_driver, da14581_fw_data, da14581_fw_size, &phase2); 174e72b198fSMatthias Ringwald 175e72b198fSMatthias Ringwald // go 176e72b198fSMatthias Ringwald btstack_run_loop_execute(); 177e72b198fSMatthias Ringwald return 0; 178e72b198fSMatthias Ringwald } 179