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 54ccebdf93SMatthias Ringwald #include "btstack_debug.h" 55ccebdf93SMatthias Ringwald #include "btstack_event.h" 5681862996SMatthias Ringwald #include "ble/le_device_db_tlv.h" 5781862996SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 58ccebdf93SMatthias Ringwald #include "btstack_memory.h" 59ccebdf93SMatthias Ringwald #include "btstack_run_loop.h" 60ccebdf93SMatthias Ringwald #include "btstack_run_loop_posix.h" 61793a0509SMatthias Ringwald #include "btstack_uart.h" 62ccebdf93SMatthias Ringwald #include "hci.h" 63ccebdf93SMatthias Ringwald #include "hci_dump.h" 64*7435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 65ccebdf93SMatthias Ringwald #include "btstack_stdin.h" 667daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h" 67ccebdf93SMatthias Ringwald 68ccebdf93SMatthias Ringwald #include "btstack_chipset_atwilc3000.h" 69a15efc86SMatthias Ringwald // #include "wilc3000_bt_firmware.h" 70a15efc86SMatthias Ringwald #include "wilc3000_ble_firmware.h" 71ccebdf93SMatthias Ringwald 72ccebdf93SMatthias Ringwald static int main_argc; 73ccebdf93SMatthias Ringwald static const char ** main_argv; 74793a0509SMatthias Ringwald static const btstack_uart_t * uart_driver; 75ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config; 76ccebdf93SMatthias 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; 827daa8bd9SMatthias Ringwald 83ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 84ccebdf93SMatthias Ringwald 85ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 86ccebdf93SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 876e0d5aedSMatthias Ringwald 921600, 886e0d5aedSMatthias Ringwald 0, // main baudrate 89f14824f7SMatthias Ringwald 1, // flow control 90ccebdf93SMatthias Ringwald NULL, 91ccebdf93SMatthias Ringwald }; 92ccebdf93SMatthias Ringwald 93ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 94ccebdf93SMatthias Ringwald 95ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 967daa8bd9SMatthias Ringwald bd_addr_t addr; 97ccebdf93SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 98ccebdf93SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 99ccebdf93SMatthias Ringwald case BTSTACK_EVENT_STATE: 100ccebdf93SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 1017daa8bd9SMatthias Ringwald gap_local_bd_addr(addr); 1027daa8bd9SMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 1037daa8bd9SMatthias Ringwald // setup TLV 1047daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1057daa8bd9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 1067daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1077daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1087daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 10981862996SMatthias Ringwald #ifdef ENABLE_BLE 11081862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 11181862996SMatthias Ringwald #endif 112ccebdf93SMatthias Ringwald break; 113ccebdf93SMatthias Ringwald default: 114ccebdf93SMatthias Ringwald break; 115ccebdf93SMatthias Ringwald } 116ccebdf93SMatthias Ringwald } 117ccebdf93SMatthias Ringwald 118ccebdf93SMatthias Ringwald static void sigint_handler(int param){ 119ccebdf93SMatthias Ringwald UNUSED(param); 120ccebdf93SMatthias Ringwald 121ccebdf93SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 122ccebdf93SMatthias Ringwald log_info("sigint_handler: shutting down"); 123ccebdf93SMatthias Ringwald 124ccebdf93SMatthias Ringwald // reset anyway 125ccebdf93SMatthias Ringwald btstack_stdin_reset(); 126ccebdf93SMatthias Ringwald 127ccebdf93SMatthias Ringwald // power down 128ccebdf93SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 129ccebdf93SMatthias Ringwald hci_close(); 130ccebdf93SMatthias Ringwald log_info("Good bye, see you.\n"); 131ccebdf93SMatthias Ringwald exit(0); 132ccebdf93SMatthias Ringwald } 133ccebdf93SMatthias Ringwald 134ccebdf93SMatthias Ringwald static int led_state = 0; 135ccebdf93SMatthias Ringwald void hal_led_toggle(void){ 136ccebdf93SMatthias Ringwald led_state = 1 - led_state; 137ccebdf93SMatthias Ringwald printf("LED State %u\n", led_state); 138ccebdf93SMatthias Ringwald } 139ccebdf93SMatthias Ringwald 140ccebdf93SMatthias Ringwald static void phase2(int status){ 141ccebdf93SMatthias Ringwald 142ccebdf93SMatthias Ringwald if (status){ 143ccebdf93SMatthias Ringwald printf("Download firmware failed\n"); 144ccebdf93SMatthias Ringwald return; 145ccebdf93SMatthias Ringwald } 146ccebdf93SMatthias Ringwald 147ccebdf93SMatthias Ringwald printf("Phase 2: Main app\n"); 148ccebdf93SMatthias Ringwald 149ccebdf93SMatthias Ringwald // init HCI 150793a0509SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 151ccebdf93SMatthias Ringwald hci_init(transport, (void*) &transport_config); 1529df38542SMatthias Ringwald hci_set_chipset(btstack_chipset_atwilc3000_instance()); 153ccebdf93SMatthias Ringwald 154ccebdf93SMatthias Ringwald // inform about BTstack state 155ccebdf93SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 156ccebdf93SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 157ccebdf93SMatthias Ringwald 158ccebdf93SMatthias Ringwald // handle CTRL-c 159ccebdf93SMatthias Ringwald signal(SIGINT, sigint_handler); 160ccebdf93SMatthias Ringwald 161ccebdf93SMatthias Ringwald // setup app 162ccebdf93SMatthias Ringwald btstack_main(main_argc, main_argv); 163ccebdf93SMatthias Ringwald } 164ccebdf93SMatthias Ringwald 165ccebdf93SMatthias Ringwald 166ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){ 167ccebdf93SMatthias Ringwald 168ccebdf93SMatthias Ringwald /// GET STARTED with BTstack /// 169ccebdf93SMatthias Ringwald btstack_memory_init(); 170ccebdf93SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 171ccebdf93SMatthias Ringwald 172*7435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 173785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 174*7435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 175*7435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 176*7435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 177785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 178ccebdf93SMatthias Ringwald 179ccebdf93SMatthias Ringwald // pick serial port and configure uart block driver 180ccebdf93SMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7"; 181793a0509SMatthias Ringwald uart_driver = btstack_uart_posix_instance(); 182ccebdf93SMatthias Ringwald 1836e0d5aedSMatthias Ringwald // extract UART config from transport config, but disable flow control and use default baudrate 1846e0d5aedSMatthias Ringwald uart_config.baudrate = HCI_DEFAULT_BAUDRATE; 185ccebdf93SMatthias Ringwald uart_config.flowcontrol = 0; 186ccebdf93SMatthias Ringwald uart_config.device_name = transport_config.device_name; 187ccebdf93SMatthias Ringwald uart_driver->init(&uart_config); 188ccebdf93SMatthias Ringwald 189ccebdf93SMatthias Ringwald main_argc = argc; 190ccebdf93SMatthias Ringwald main_argv = argv; 191ccebdf93SMatthias Ringwald 192ccebdf93SMatthias Ringwald // phase #1 download firmware 193ccebdf93SMatthias Ringwald printf("Phase 1: Download firmware\n"); 194ccebdf93SMatthias Ringwald 195ccebdf93SMatthias Ringwald // phase #2 start main app 196793a0509SMatthias 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); 197ccebdf93SMatthias Ringwald 198ccebdf93SMatthias Ringwald // go 199ccebdf93SMatthias Ringwald btstack_run_loop_execute(); 200ccebdf93SMatthias Ringwald return 0; 201ccebdf93SMatthias Ringwald } 202