1ccebdf93SMatthias Ringwald /* 2ccebdf93SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3ccebdf93SMatthias Ringwald * 4ccebdf93SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5ccebdf93SMatthias Ringwald * modification, are permitted provided that the following conditions 6ccebdf93SMatthias Ringwald * are met: 7ccebdf93SMatthias Ringwald * 8ccebdf93SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9ccebdf93SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10ccebdf93SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11ccebdf93SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12ccebdf93SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13ccebdf93SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14ccebdf93SMatthias Ringwald * contributors may be used to endorse or promote products derived 15ccebdf93SMatthias Ringwald * from this software without specific prior written permission. 16ccebdf93SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17ccebdf93SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18ccebdf93SMatthias Ringwald * monetary gain. 19ccebdf93SMatthias Ringwald * 20ccebdf93SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21ccebdf93SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22ccebdf93SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23ccebdf93SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24ccebdf93SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25ccebdf93SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26ccebdf93SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27ccebdf93SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28ccebdf93SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29ccebdf93SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30ccebdf93SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ccebdf93SMatthias Ringwald * SUCH DAMAGE. 32ccebdf93SMatthias Ringwald * 33ccebdf93SMatthias Ringwald * Please inquire about commercial licensing options at 34ccebdf93SMatthias Ringwald * [email protected] 35ccebdf93SMatthias Ringwald * 36ccebdf93SMatthias Ringwald */ 37ccebdf93SMatthias Ringwald 38793a0509SMatthias Ringwald #define BTSTACK_FILE__ "main.c" 39ccebdf93SMatthias Ringwald 40ccebdf93SMatthias Ringwald // ***************************************************************************** 41ccebdf93SMatthias Ringwald // 42ccebdf93SMatthias Ringwald // minimal setup for HCI code 43ccebdf93SMatthias Ringwald // 44ccebdf93SMatthias Ringwald // ***************************************************************************** 45ccebdf93SMatthias Ringwald 46ccebdf93SMatthias Ringwald #include <stdint.h> 47ccebdf93SMatthias Ringwald #include <stdio.h> 48ccebdf93SMatthias Ringwald #include <stdlib.h> 49ccebdf93SMatthias Ringwald #include <string.h> 50ccebdf93SMatthias Ringwald #include <signal.h> 51ccebdf93SMatthias Ringwald 52ccebdf93SMatthias Ringwald #include "btstack_config.h" 53ccebdf93SMatthias Ringwald 54*f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h" 55ccebdf93SMatthias Ringwald #include "btstack_debug.h" 56ccebdf93SMatthias Ringwald #include "btstack_event.h" 57ccebdf93SMatthias Ringwald #include "btstack_memory.h" 58ccebdf93SMatthias Ringwald #include "btstack_run_loop.h" 59ccebdf93SMatthias Ringwald #include "btstack_run_loop_posix.h" 60*f11fd9a9SMatthias Ringwald #include "btstack_signal.h" 61*f11fd9a9SMatthias Ringwald #include "btstack_stdin.h" 62*f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h" 63793a0509SMatthias Ringwald #include "btstack_uart.h" 64*f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 65ccebdf93SMatthias Ringwald #include "hci.h" 66ccebdf93SMatthias Ringwald #include "hci_dump.h" 677435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 68c8dfe071SMatthias Ringwald #include "hci_transport.h" 69c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 70ccebdf93SMatthias Ringwald 71ccebdf93SMatthias Ringwald #include "btstack_chipset_atwilc3000.h" 72a15efc86SMatthias Ringwald // #include "wilc3000_bt_firmware.h" 73a15efc86SMatthias Ringwald #include "wilc3000_ble_firmware.h" 74ccebdf93SMatthias Ringwald 75ccebdf93SMatthias Ringwald static int main_argc; 76ccebdf93SMatthias Ringwald static const char ** main_argv; 77793a0509SMatthias Ringwald static const btstack_uart_t * uart_driver; 78ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config; 79ccebdf93SMatthias Ringwald 807daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 817daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 827daa8bd9SMatthias Ringwald static char tlv_db_path[100]; 837daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 847daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 85*f11fd9a9SMatthias Ringwald // shutdown 86*f11fd9a9SMatthias Ringwald static bd_addr_t local_addr; 87*f11fd9a9SMatthias Ringwald static bool shutdown_triggered; 887daa8bd9SMatthias Ringwald 89ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 90ccebdf93SMatthias Ringwald 91ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 92ccebdf93SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 936e0d5aedSMatthias Ringwald 921600, 946e0d5aedSMatthias Ringwald 0, // main baudrate 95f14824f7SMatthias Ringwald 1, // flow control 96ccebdf93SMatthias Ringwald NULL, 97ccebdf93SMatthias Ringwald }; 98ccebdf93SMatthias Ringwald 99ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 100ccebdf93SMatthias Ringwald 101ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 102ccebdf93SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 103ccebdf93SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 104ccebdf93SMatthias Ringwald case BTSTACK_EVENT_STATE: 105*f11fd9a9SMatthias Ringwald switch(btstack_event_state_get_state(packet)){ 106*f11fd9a9SMatthias Ringwald case HCI_STATE_WORKING: 107*f11fd9a9SMatthias Ringwald gap_local_bd_addr(local_addr); 108*f11fd9a9SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 1097daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 110*f11fd9a9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 1117daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1127daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1137daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 11481862996SMatthias Ringwald #ifdef ENABLE_BLE 11581862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 11681862996SMatthias Ringwald #endif 117ccebdf93SMatthias Ringwald break; 118*f11fd9a9SMatthias Ringwald case HCI_STATE_OFF: 119*f11fd9a9SMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context); 120*f11fd9a9SMatthias Ringwald if (!shutdown_triggered) break; 121*f11fd9a9SMatthias Ringwald // reset stdin 122*f11fd9a9SMatthias Ringwald btstack_stdin_reset(); 123*f11fd9a9SMatthias Ringwald log_info("Good bye, see you.\n"); 124*f11fd9a9SMatthias Ringwald exit(0); 125*f11fd9a9SMatthias Ringwald break; 126*f11fd9a9SMatthias Ringwald default: 127*f11fd9a9SMatthias Ringwald break; 128*f11fd9a9SMatthias Ringwald } 129*f11fd9a9SMatthias Ringwald break; 130ccebdf93SMatthias Ringwald default: 131ccebdf93SMatthias Ringwald break; 132ccebdf93SMatthias Ringwald } 133ccebdf93SMatthias Ringwald } 134ccebdf93SMatthias Ringwald 135*f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){ 136ccebdf93SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 137ccebdf93SMatthias Ringwald log_info("sigint_handler: shutting down"); 138*f11fd9a9SMatthias Ringwald shutdown_triggered = true; 139ccebdf93SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 140ccebdf93SMatthias Ringwald } 141ccebdf93SMatthias Ringwald 142ccebdf93SMatthias Ringwald static int led_state = 0; 143ccebdf93SMatthias Ringwald void hal_led_toggle(void){ 144ccebdf93SMatthias Ringwald led_state = 1 - led_state; 145ccebdf93SMatthias Ringwald printf("LED State %u\n", led_state); 146ccebdf93SMatthias Ringwald } 147ccebdf93SMatthias Ringwald 148ccebdf93SMatthias Ringwald static void phase2(int status){ 149ccebdf93SMatthias Ringwald 150ccebdf93SMatthias Ringwald if (status){ 151ccebdf93SMatthias Ringwald printf("Download firmware failed\n"); 152ccebdf93SMatthias Ringwald return; 153ccebdf93SMatthias Ringwald } 154ccebdf93SMatthias Ringwald 155ccebdf93SMatthias Ringwald printf("Phase 2: Main app\n"); 156ccebdf93SMatthias Ringwald 157ccebdf93SMatthias Ringwald // init HCI 158793a0509SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 159ccebdf93SMatthias Ringwald hci_init(transport, (void*) &transport_config); 1609df38542SMatthias Ringwald hci_set_chipset(btstack_chipset_atwilc3000_instance()); 161ccebdf93SMatthias Ringwald 162ccebdf93SMatthias Ringwald // inform about BTstack state 163ccebdf93SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 164ccebdf93SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 165ccebdf93SMatthias Ringwald 166*f11fd9a9SMatthias Ringwald // register callback for CTRL-c 167*f11fd9a9SMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown); 168ccebdf93SMatthias Ringwald 169ccebdf93SMatthias Ringwald // setup app 170ccebdf93SMatthias Ringwald btstack_main(main_argc, main_argv); 171ccebdf93SMatthias Ringwald } 172ccebdf93SMatthias Ringwald 173ccebdf93SMatthias Ringwald 174ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){ 175ccebdf93SMatthias Ringwald 176ccebdf93SMatthias Ringwald /// GET STARTED with BTstack /// 177ccebdf93SMatthias Ringwald btstack_memory_init(); 178ccebdf93SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 179ccebdf93SMatthias Ringwald 1807435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 181785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 1827435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 1837435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 1847435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 185785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 186ccebdf93SMatthias Ringwald 187ccebdf93SMatthias Ringwald // pick serial port and configure uart block driver 188ccebdf93SMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7"; 189793a0509SMatthias Ringwald uart_driver = btstack_uart_posix_instance(); 190ccebdf93SMatthias Ringwald 1916e0d5aedSMatthias Ringwald // extract UART config from transport config, but disable flow control and use default baudrate 1926e0d5aedSMatthias Ringwald uart_config.baudrate = HCI_DEFAULT_BAUDRATE; 193ccebdf93SMatthias Ringwald uart_config.flowcontrol = 0; 194ccebdf93SMatthias Ringwald uart_config.device_name = transport_config.device_name; 195ccebdf93SMatthias Ringwald uart_driver->init(&uart_config); 196ccebdf93SMatthias Ringwald 197ccebdf93SMatthias Ringwald main_argc = argc; 198ccebdf93SMatthias Ringwald main_argv = argv; 199ccebdf93SMatthias Ringwald 200ccebdf93SMatthias Ringwald // phase #1 download firmware 201ccebdf93SMatthias Ringwald printf("Phase 1: Download firmware\n"); 202ccebdf93SMatthias Ringwald 203ccebdf93SMatthias Ringwald // phase #2 start main app 204793a0509SMatthias Ringwald btstack_chipset_atwilc3000_download_firmware_with_uart(uart_driver, transport_config.baudrate_init, transport_config.flowcontrol, (const uint8_t *) firmware_ble, sizeof(firmware_ble), &phase2); 205ccebdf93SMatthias Ringwald 206ccebdf93SMatthias Ringwald // go 207ccebdf93SMatthias Ringwald btstack_run_loop_execute(); 208ccebdf93SMatthias Ringwald return 0; 209ccebdf93SMatthias Ringwald } 210