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