1*ccebdf93SMatthias Ringwald /* 2*ccebdf93SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*ccebdf93SMatthias Ringwald * 4*ccebdf93SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*ccebdf93SMatthias Ringwald * modification, are permitted provided that the following conditions 6*ccebdf93SMatthias Ringwald * are met: 7*ccebdf93SMatthias Ringwald * 8*ccebdf93SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*ccebdf93SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*ccebdf93SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*ccebdf93SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*ccebdf93SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*ccebdf93SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*ccebdf93SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*ccebdf93SMatthias Ringwald * from this software without specific prior written permission. 16*ccebdf93SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*ccebdf93SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*ccebdf93SMatthias Ringwald * monetary gain. 19*ccebdf93SMatthias Ringwald * 20*ccebdf93SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*ccebdf93SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*ccebdf93SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*ccebdf93SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*ccebdf93SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*ccebdf93SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*ccebdf93SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*ccebdf93SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*ccebdf93SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*ccebdf93SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*ccebdf93SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*ccebdf93SMatthias Ringwald * SUCH DAMAGE. 32*ccebdf93SMatthias Ringwald * 33*ccebdf93SMatthias Ringwald * Please inquire about commercial licensing options at 34*ccebdf93SMatthias Ringwald * [email protected] 35*ccebdf93SMatthias Ringwald * 36*ccebdf93SMatthias Ringwald */ 37*ccebdf93SMatthias Ringwald 38*ccebdf93SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39*ccebdf93SMatthias Ringwald 40*ccebdf93SMatthias Ringwald // ***************************************************************************** 41*ccebdf93SMatthias Ringwald // 42*ccebdf93SMatthias Ringwald // minimal setup for HCI code 43*ccebdf93SMatthias Ringwald // 44*ccebdf93SMatthias Ringwald // ***************************************************************************** 45*ccebdf93SMatthias Ringwald 46*ccebdf93SMatthias Ringwald #include <stdint.h> 47*ccebdf93SMatthias Ringwald #include <stdio.h> 48*ccebdf93SMatthias Ringwald #include <stdlib.h> 49*ccebdf93SMatthias Ringwald #include <string.h> 50*ccebdf93SMatthias Ringwald #include <signal.h> 51*ccebdf93SMatthias Ringwald 52*ccebdf93SMatthias Ringwald #include "btstack_config.h" 53*ccebdf93SMatthias Ringwald 54*ccebdf93SMatthias Ringwald #include "btstack_debug.h" 55*ccebdf93SMatthias Ringwald #include "btstack_event.h" 56*ccebdf93SMatthias Ringwald #include "btstack_link_key_db_fs.h" 57*ccebdf93SMatthias Ringwald #include "btstack_memory.h" 58*ccebdf93SMatthias Ringwald #include "btstack_run_loop.h" 59*ccebdf93SMatthias Ringwald #include "btstack_run_loop_posix.h" 60*ccebdf93SMatthias Ringwald #include "hci.h" 61*ccebdf93SMatthias Ringwald #include "hci_dump.h" 62*ccebdf93SMatthias Ringwald #include "btstack_stdin.h" 63*ccebdf93SMatthias Ringwald 64*ccebdf93SMatthias Ringwald #include "btstack_chipset_atwilc3000.h" 65*ccebdf93SMatthias Ringwald #include "wilc3000_bt_firmware.h" 66*ccebdf93SMatthias Ringwald 67*ccebdf93SMatthias Ringwald static int main_argc; 68*ccebdf93SMatthias Ringwald static const char ** main_argv; 69*ccebdf93SMatthias Ringwald static const btstack_uart_block_t * uart_driver; 70*ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config; 71*ccebdf93SMatthias Ringwald 72*ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 73*ccebdf93SMatthias Ringwald 74*ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 75*ccebdf93SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 76*ccebdf93SMatthias Ringwald 115200, 77*ccebdf93SMatthias Ringwald 0, // main baudrate 78*ccebdf93SMatthias Ringwald 0, // flow control 79*ccebdf93SMatthias Ringwald NULL, 80*ccebdf93SMatthias Ringwald }; 81*ccebdf93SMatthias Ringwald 82*ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 83*ccebdf93SMatthias Ringwald 84*ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 85*ccebdf93SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 86*ccebdf93SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 87*ccebdf93SMatthias Ringwald case BTSTACK_EVENT_STATE: 88*ccebdf93SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 89*ccebdf93SMatthias Ringwald printf("BTstack up and running.\n"); 90*ccebdf93SMatthias Ringwald break; 91*ccebdf93SMatthias Ringwald default: 92*ccebdf93SMatthias Ringwald break; 93*ccebdf93SMatthias Ringwald } 94*ccebdf93SMatthias Ringwald } 95*ccebdf93SMatthias Ringwald 96*ccebdf93SMatthias Ringwald static void sigint_handler(int param){ 97*ccebdf93SMatthias Ringwald UNUSED(param); 98*ccebdf93SMatthias Ringwald 99*ccebdf93SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 100*ccebdf93SMatthias Ringwald log_info("sigint_handler: shutting down"); 101*ccebdf93SMatthias Ringwald 102*ccebdf93SMatthias Ringwald // reset anyway 103*ccebdf93SMatthias Ringwald btstack_stdin_reset(); 104*ccebdf93SMatthias Ringwald 105*ccebdf93SMatthias Ringwald // power down 106*ccebdf93SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 107*ccebdf93SMatthias Ringwald hci_close(); 108*ccebdf93SMatthias Ringwald log_info("Good bye, see you.\n"); 109*ccebdf93SMatthias Ringwald exit(0); 110*ccebdf93SMatthias Ringwald } 111*ccebdf93SMatthias Ringwald 112*ccebdf93SMatthias Ringwald static int led_state = 0; 113*ccebdf93SMatthias Ringwald void hal_led_toggle(void){ 114*ccebdf93SMatthias Ringwald led_state = 1 - led_state; 115*ccebdf93SMatthias Ringwald printf("LED State %u\n", led_state); 116*ccebdf93SMatthias Ringwald } 117*ccebdf93SMatthias Ringwald 118*ccebdf93SMatthias Ringwald static void phase2(int status){ 119*ccebdf93SMatthias Ringwald 120*ccebdf93SMatthias Ringwald if (status){ 121*ccebdf93SMatthias Ringwald printf("Download firmware failed\n"); 122*ccebdf93SMatthias Ringwald return; 123*ccebdf93SMatthias Ringwald } 124*ccebdf93SMatthias Ringwald 125*ccebdf93SMatthias Ringwald printf("Phase 2: Main app\n"); 126*ccebdf93SMatthias Ringwald 127*ccebdf93SMatthias Ringwald // init HCI 128*ccebdf93SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 129*ccebdf93SMatthias Ringwald const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 130*ccebdf93SMatthias Ringwald hci_init(transport, (void*) &transport_config); 131*ccebdf93SMatthias Ringwald hci_set_link_key_db(link_key_db); 132*ccebdf93SMatthias Ringwald 133*ccebdf93SMatthias Ringwald // inform about BTstack state 134*ccebdf93SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 135*ccebdf93SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 136*ccebdf93SMatthias Ringwald 137*ccebdf93SMatthias Ringwald // handle CTRL-c 138*ccebdf93SMatthias Ringwald signal(SIGINT, sigint_handler); 139*ccebdf93SMatthias Ringwald 140*ccebdf93SMatthias Ringwald // setup app 141*ccebdf93SMatthias Ringwald btstack_main(main_argc, main_argv); 142*ccebdf93SMatthias Ringwald } 143*ccebdf93SMatthias Ringwald 144*ccebdf93SMatthias Ringwald 145*ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){ 146*ccebdf93SMatthias Ringwald 147*ccebdf93SMatthias Ringwald /// GET STARTED with BTstack /// 148*ccebdf93SMatthias Ringwald btstack_memory_init(); 149*ccebdf93SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 150*ccebdf93SMatthias Ringwald 151*ccebdf93SMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 152*ccebdf93SMatthias Ringwald hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_STDOUT); 153*ccebdf93SMatthias Ringwald 154*ccebdf93SMatthias Ringwald // pick serial port and configure uart block driver 155*ccebdf93SMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7"; 156*ccebdf93SMatthias Ringwald uart_driver = btstack_uart_block_posix_instance(); 157*ccebdf93SMatthias Ringwald 158*ccebdf93SMatthias Ringwald // extract UART config from transport config, but overide initial uart speed 159*ccebdf93SMatthias Ringwald uart_config.baudrate = 115200; 160*ccebdf93SMatthias Ringwald uart_config.flowcontrol = 0; 161*ccebdf93SMatthias Ringwald uart_config.device_name = transport_config.device_name; 162*ccebdf93SMatthias Ringwald uart_driver->init(&uart_config); 163*ccebdf93SMatthias Ringwald 164*ccebdf93SMatthias Ringwald main_argc = argc; 165*ccebdf93SMatthias Ringwald main_argv = argv; 166*ccebdf93SMatthias Ringwald 167*ccebdf93SMatthias Ringwald // phase #1 download firmware 168*ccebdf93SMatthias Ringwald printf("Phase 1: Download firmware\n"); 169*ccebdf93SMatthias Ringwald 170*ccebdf93SMatthias Ringwald // phase #2 start main app 171*ccebdf93SMatthias Ringwald btstack_chipset_atwilc3000_download_firmware(uart_driver, atwilc3000_fw_data, atwilc3000_fw_size, &phase2); 172*ccebdf93SMatthias Ringwald // phase2(0); 173*ccebdf93SMatthias Ringwald 174*ccebdf93SMatthias Ringwald // go 175*ccebdf93SMatthias Ringwald btstack_run_loop_execute(); 176*ccebdf93SMatthias Ringwald return 0; 177*ccebdf93SMatthias Ringwald } 178