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 38ccebdf93SMatthias 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" 56*81862996SMatthias Ringwald #include "ble/le_device_db_tlv.h" 57*81862996SMatthias 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" 61ccebdf93SMatthias Ringwald #include "hci.h" 62ccebdf93SMatthias Ringwald #include "hci_dump.h" 63ccebdf93SMatthias Ringwald #include "btstack_stdin.h" 647daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h" 65ccebdf93SMatthias Ringwald 66ccebdf93SMatthias Ringwald #include "btstack_chipset_atwilc3000.h" 67a15efc86SMatthias Ringwald // #include "wilc3000_bt_firmware.h" 68a15efc86SMatthias Ringwald #include "wilc3000_ble_firmware.h" 69ccebdf93SMatthias Ringwald 70ccebdf93SMatthias Ringwald static int main_argc; 71ccebdf93SMatthias Ringwald static const char ** main_argv; 72ccebdf93SMatthias Ringwald static const btstack_uart_block_t * uart_driver; 73ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config; 74ccebdf93SMatthias 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 81ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 82ccebdf93SMatthias Ringwald 83ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 84ccebdf93SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 856e0d5aedSMatthias Ringwald 921600, 866e0d5aedSMatthias Ringwald 0, // main baudrate 87f14824f7SMatthias Ringwald 1, // flow control 88ccebdf93SMatthias Ringwald NULL, 89ccebdf93SMatthias Ringwald }; 90ccebdf93SMatthias Ringwald 91ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 92ccebdf93SMatthias Ringwald 93ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 947daa8bd9SMatthias Ringwald bd_addr_t addr; 95ccebdf93SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 96ccebdf93SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 97ccebdf93SMatthias Ringwald case BTSTACK_EVENT_STATE: 98ccebdf93SMatthias 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); 107*81862996SMatthias Ringwald #ifdef ENABLE_CLASSIC 108*81862996SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 109*81862996SMatthias Ringwald #endif 110*81862996SMatthias Ringwald #ifdef ENABLE_BLE 111*81862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 112*81862996SMatthias Ringwald #endif 113ccebdf93SMatthias Ringwald break; 114ccebdf93SMatthias Ringwald default: 115ccebdf93SMatthias Ringwald break; 116ccebdf93SMatthias Ringwald } 117ccebdf93SMatthias Ringwald } 118ccebdf93SMatthias Ringwald 119ccebdf93SMatthias Ringwald static void sigint_handler(int param){ 120ccebdf93SMatthias Ringwald UNUSED(param); 121ccebdf93SMatthias Ringwald 122ccebdf93SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 123ccebdf93SMatthias Ringwald log_info("sigint_handler: shutting down"); 124ccebdf93SMatthias Ringwald 125ccebdf93SMatthias Ringwald // reset anyway 126ccebdf93SMatthias Ringwald btstack_stdin_reset(); 127ccebdf93SMatthias Ringwald 128ccebdf93SMatthias Ringwald // power down 129ccebdf93SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 130ccebdf93SMatthias Ringwald hci_close(); 131ccebdf93SMatthias Ringwald log_info("Good bye, see you.\n"); 132ccebdf93SMatthias Ringwald exit(0); 133ccebdf93SMatthias Ringwald } 134ccebdf93SMatthias Ringwald 135ccebdf93SMatthias Ringwald static int led_state = 0; 136ccebdf93SMatthias Ringwald void hal_led_toggle(void){ 137ccebdf93SMatthias Ringwald led_state = 1 - led_state; 138ccebdf93SMatthias Ringwald printf("LED State %u\n", led_state); 139ccebdf93SMatthias Ringwald } 140ccebdf93SMatthias Ringwald 141ccebdf93SMatthias Ringwald static void phase2(int status){ 142ccebdf93SMatthias Ringwald 143ccebdf93SMatthias Ringwald if (status){ 144ccebdf93SMatthias Ringwald printf("Download firmware failed\n"); 145ccebdf93SMatthias Ringwald return; 146ccebdf93SMatthias Ringwald } 147ccebdf93SMatthias Ringwald 148ccebdf93SMatthias Ringwald printf("Phase 2: Main app\n"); 149ccebdf93SMatthias Ringwald 150ccebdf93SMatthias Ringwald // init HCI 151ccebdf93SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 152ccebdf93SMatthias Ringwald hci_init(transport, (void*) &transport_config); 1539df38542SMatthias Ringwald hci_set_chipset(btstack_chipset_atwilc3000_instance()); 154ccebdf93SMatthias Ringwald 155ccebdf93SMatthias Ringwald // inform about BTstack state 156ccebdf93SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 157ccebdf93SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 158ccebdf93SMatthias Ringwald 159ccebdf93SMatthias Ringwald // handle CTRL-c 160ccebdf93SMatthias Ringwald signal(SIGINT, sigint_handler); 161ccebdf93SMatthias Ringwald 162ccebdf93SMatthias Ringwald // setup app 163ccebdf93SMatthias Ringwald btstack_main(main_argc, main_argv); 164ccebdf93SMatthias Ringwald } 165ccebdf93SMatthias Ringwald 166ccebdf93SMatthias Ringwald 167ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){ 168ccebdf93SMatthias Ringwald 169ccebdf93SMatthias Ringwald /// GET STARTED with BTstack /// 170ccebdf93SMatthias Ringwald btstack_memory_init(); 171ccebdf93SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 172ccebdf93SMatthias Ringwald 173ccebdf93SMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 174785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 175785879c6SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 176785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 177ccebdf93SMatthias Ringwald 178ccebdf93SMatthias Ringwald // pick serial port and configure uart block driver 179ccebdf93SMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7"; 180ccebdf93SMatthias Ringwald uart_driver = btstack_uart_block_posix_instance(); 181ccebdf93SMatthias Ringwald 1826e0d5aedSMatthias Ringwald // extract UART config from transport config, but disable flow control and use default baudrate 1836e0d5aedSMatthias Ringwald uart_config.baudrate = HCI_DEFAULT_BAUDRATE; 184ccebdf93SMatthias Ringwald uart_config.flowcontrol = 0; 185ccebdf93SMatthias Ringwald uart_config.device_name = transport_config.device_name; 186ccebdf93SMatthias Ringwald uart_driver->init(&uart_config); 187ccebdf93SMatthias Ringwald 188ccebdf93SMatthias Ringwald main_argc = argc; 189ccebdf93SMatthias Ringwald main_argv = argv; 190ccebdf93SMatthias Ringwald 191ccebdf93SMatthias Ringwald // phase #1 download firmware 192ccebdf93SMatthias Ringwald printf("Phase 1: Download firmware\n"); 193ccebdf93SMatthias Ringwald 194ccebdf93SMatthias Ringwald // phase #2 start main app 195a15efc86SMatthias Ringwald btstack_chipset_atwilc3000_download_firmware(uart_driver, transport_config.baudrate_init, transport_config.flowcontrol, (const uint8_t *) firmware_ble, sizeof(firmware_ble), &phase2); 196ccebdf93SMatthias Ringwald 197ccebdf93SMatthias Ringwald // go 198ccebdf93SMatthias Ringwald btstack_run_loop_execute(); 199ccebdf93SMatthias Ringwald return 0; 200ccebdf93SMatthias Ringwald } 201