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 38*f11fd9a9SMatthias 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 54*f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h" 55*f11fd9a9SMatthias Ringwald #include "btstack_chipset_da14581.h" 56e72b198fSMatthias Ringwald #include "btstack_debug.h" 57e72b198fSMatthias Ringwald #include "btstack_event.h" 58e72b198fSMatthias Ringwald #include "btstack_memory.h" 59e72b198fSMatthias Ringwald #include "btstack_run_loop.h" 60e72b198fSMatthias Ringwald #include "btstack_run_loop_posix.h" 61*f11fd9a9SMatthias Ringwald #include "btstack_signal.h" 62*f11fd9a9SMatthias Ringwald #include "btstack_stdin.h" 63*f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h" 64793a0509SMatthias Ringwald #include "btstack_uart.h" 65e72b198fSMatthias Ringwald #include "hci.h" 66*f11fd9a9SMatthias Ringwald #include "hci_581_active_uart.h" 67e72b198fSMatthias Ringwald #include "hci_dump.h" 687435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 69c8dfe071SMatthias Ringwald #include "hci_transport.h" 70c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 71e72b198fSMatthias Ringwald 72e72b198fSMatthias Ringwald static int main_argc; 73e72b198fSMatthias Ringwald static const char ** main_argv; 74793a0509SMatthias Ringwald static const btstack_uart_t * uart_driver; 75e72b198fSMatthias Ringwald static btstack_uart_config_t uart_config; 76e72b198fSMatthias Ringwald 777daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 787daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 797daa8bd9SMatthias Ringwald static char tlv_db_path[100]; 807daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 817daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 82*f11fd9a9SMatthias Ringwald static bd_addr_t local_addr; 83*f11fd9a9SMatthias Ringwald static bool shutdown_triggered; 84*f11fd9a9SMatthias Ringwald 85*f11fd9a9SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 867daa8bd9SMatthias Ringwald 87e72b198fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 88e72b198fSMatthias Ringwald 89e72b198fSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 90e72b198fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 91e72b198fSMatthias Ringwald 115200, 92e72b198fSMatthias Ringwald 0, // main baudrate 93e72b198fSMatthias Ringwald 1, // flow control 94e72b198fSMatthias Ringwald NULL, 95e72b198fSMatthias Ringwald }; 96e72b198fSMatthias Ringwald 97e72b198fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 98e72b198fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 99e72b198fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 100e72b198fSMatthias Ringwald case BTSTACK_EVENT_STATE: 101*f11fd9a9SMatthias Ringwald switch(btstack_event_state_get_state(packet)){ 102*f11fd9a9SMatthias Ringwald case HCI_STATE_WORKING: 103*f11fd9a9SMatthias Ringwald gap_local_bd_addr(local_addr); 104*f11fd9a9SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 1057daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 106*f11fd9a9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 1077daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1087daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1097daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 11081862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 111e72b198fSMatthias Ringwald break; 112*f11fd9a9SMatthias Ringwald case HCI_STATE_OFF: 113*f11fd9a9SMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context); 114*f11fd9a9SMatthias Ringwald if (!shutdown_triggered) break; 115*f11fd9a9SMatthias Ringwald // reset stdin 116*f11fd9a9SMatthias Ringwald btstack_stdin_reset(); 117*f11fd9a9SMatthias Ringwald log_info("Good bye, see you.\n"); 118*f11fd9a9SMatthias Ringwald exit(0); 119*f11fd9a9SMatthias Ringwald break; 120*f11fd9a9SMatthias Ringwald default: 121*f11fd9a9SMatthias Ringwald break; 122*f11fd9a9SMatthias Ringwald } 123e72b198fSMatthias Ringwald default: 124e72b198fSMatthias Ringwald break; 125e72b198fSMatthias Ringwald } 126e72b198fSMatthias Ringwald } 127e72b198fSMatthias Ringwald 128*f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){ 129e72b198fSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 130e72b198fSMatthias Ringwald log_info("sigint_handler: shutting down"); 131*f11fd9a9SMatthias Ringwald shutdown_triggered = true; 132e72b198fSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 133e72b198fSMatthias Ringwald } 134e72b198fSMatthias Ringwald 135e72b198fSMatthias Ringwald static int led_state = 0; 136e72b198fSMatthias Ringwald void hal_led_toggle(void){ 137e72b198fSMatthias Ringwald led_state = 1 - led_state; 138e72b198fSMatthias Ringwald printf("LED State %u\n", led_state); 139e72b198fSMatthias Ringwald } 140e72b198fSMatthias Ringwald 141e72b198fSMatthias Ringwald static void phase2(int status){ 142e72b198fSMatthias Ringwald 143e72b198fSMatthias Ringwald if (status){ 144e72b198fSMatthias Ringwald printf("Download firmware failed\n"); 145e72b198fSMatthias Ringwald return; 146e72b198fSMatthias Ringwald } 147e72b198fSMatthias Ringwald 148e72b198fSMatthias Ringwald printf("Phase 2: Main app\n"); 149e72b198fSMatthias Ringwald 150e72b198fSMatthias Ringwald // init HCI 151793a0509SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 152e72b198fSMatthias Ringwald hci_init(transport, (void*) &transport_config); 153e72b198fSMatthias Ringwald 154e72b198fSMatthias Ringwald // inform about BTstack state 155e72b198fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 156e72b198fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 157e72b198fSMatthias Ringwald 158*f11fd9a9SMatthias Ringwald // register callback for CTRL-c 159*f11fd9a9SMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown); 160e72b198fSMatthias Ringwald 161e72b198fSMatthias Ringwald // setup app 162e72b198fSMatthias Ringwald btstack_main(main_argc, main_argv); 163e72b198fSMatthias Ringwald } 164e72b198fSMatthias Ringwald 165e72b198fSMatthias Ringwald 166e72b198fSMatthias Ringwald int main(int argc, const char * argv[]){ 167e72b198fSMatthias Ringwald 168e72b198fSMatthias Ringwald /// GET STARTED with BTstack /// 169e72b198fSMatthias Ringwald btstack_memory_init(); 170e72b198fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 171e72b198fSMatthias Ringwald 1727435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 173785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 1747435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 1757435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 1767435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 177785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 178e72b198fSMatthias Ringwald 179e72b198fSMatthias Ringwald // pick serial port and configure uart block driver 180e72b198fSMatthias Ringwald transport_config.device_name = "/dev/tty.usbmodem1442311"; 181793a0509SMatthias Ringwald uart_driver = btstack_uart_posix_instance(); 182e72b198fSMatthias Ringwald 183e72b198fSMatthias Ringwald // extract UART config from transport config, but overide initial uart speed 184e72b198fSMatthias Ringwald uart_config.baudrate = 57600; 185e72b198fSMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 186e72b198fSMatthias Ringwald uart_config.device_name = transport_config.device_name; 187e72b198fSMatthias Ringwald uart_driver->init(&uart_config); 188e72b198fSMatthias Ringwald 189e72b198fSMatthias Ringwald main_argc = argc; 190e72b198fSMatthias Ringwald main_argv = argv; 191e72b198fSMatthias Ringwald 192e72b198fSMatthias Ringwald // phase #1 download firmware 193e72b198fSMatthias Ringwald printf("Phase 1: Download firmware\n"); 194e72b198fSMatthias Ringwald 195e72b198fSMatthias Ringwald // phase #2 start main app 196793a0509SMatthias Ringwald btstack_chipset_da14581_download_firmware_with_uart(uart_driver, da14581_fw_data, da14581_fw_size, &phase2); 197e72b198fSMatthias Ringwald 198e72b198fSMatthias Ringwald // go 199e72b198fSMatthias Ringwald btstack_run_loop_execute(); 200e72b198fSMatthias Ringwald return 0; 201e72b198fSMatthias Ringwald } 202