1f61339eaSMatthias Ringwald /* 2f61339eaSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3f61339eaSMatthias Ringwald * 4f61339eaSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5f61339eaSMatthias Ringwald * modification, are permitted provided that the following conditions 6f61339eaSMatthias Ringwald * are met: 7f61339eaSMatthias Ringwald * 8f61339eaSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9f61339eaSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10f61339eaSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11f61339eaSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12f61339eaSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13f61339eaSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14f61339eaSMatthias Ringwald * contributors may be used to endorse or promote products derived 15f61339eaSMatthias Ringwald * from this software without specific prior written permission. 16f61339eaSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17f61339eaSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18f61339eaSMatthias Ringwald * monetary gain. 19f61339eaSMatthias Ringwald * 20f61339eaSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21f61339eaSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22f61339eaSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23f61339eaSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24f61339eaSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25f61339eaSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26f61339eaSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27f61339eaSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28f61339eaSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29f61339eaSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30f61339eaSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31f61339eaSMatthias Ringwald * SUCH DAMAGE. 32f61339eaSMatthias Ringwald * 33f61339eaSMatthias Ringwald * Please inquire about commercial licensing options at 34f61339eaSMatthias Ringwald * [email protected] 35f61339eaSMatthias Ringwald * 36f61339eaSMatthias Ringwald */ 37f61339eaSMatthias Ringwald 38f61339eaSMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39f61339eaSMatthias Ringwald 40f61339eaSMatthias Ringwald // ***************************************************************************** 41f61339eaSMatthias Ringwald // 42f61339eaSMatthias Ringwald // minimal setup for HCI code 43f61339eaSMatthias Ringwald // 44f61339eaSMatthias Ringwald // ***************************************************************************** 45f61339eaSMatthias Ringwald 46f61339eaSMatthias Ringwald #include <stdint.h> 47f61339eaSMatthias Ringwald #include <stdio.h> 48f61339eaSMatthias Ringwald #include <stdlib.h> 49f61339eaSMatthias Ringwald #include <string.h> 50f61339eaSMatthias Ringwald #include <signal.h> 51f61339eaSMatthias Ringwald 52f61339eaSMatthias Ringwald #include "btstack_config.h" 53f61339eaSMatthias Ringwald 54f61339eaSMatthias Ringwald #include "btstack_debug.h" 55f61339eaSMatthias Ringwald #include "btstack_event.h" 56f61339eaSMatthias Ringwald #include "btstack_memory.h" 57f61339eaSMatthias Ringwald #include "btstack_run_loop.h" 58f61339eaSMatthias Ringwald #include "btstack_run_loop_windows.h" 596486d278SMatthias Ringwald #include "btstack_tlv_posix.h" 606486d278SMatthias Ringwald #include "ble/le_device_db_tlv.h" 616486d278SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 62f61339eaSMatthias Ringwald #include "hal_led.h" 63f61339eaSMatthias Ringwald #include "hci.h" 64f61339eaSMatthias Ringwald #include "hci_dump.h" 657435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 66*c8dfe071SMatthias Ringwald #include "hci_transport.h" 67*c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 68f61339eaSMatthias Ringwald #include "btstack_stdin.h" 69f61339eaSMatthias Ringwald #include "btstack_chipset_intel_firmware.h" 70f61339eaSMatthias Ringwald 71f61339eaSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 72f61339eaSMatthias Ringwald 736486d278SMatthias Ringwald 746486d278SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack_" 756486d278SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 766486d278SMatthias Ringwald static char tlv_db_path[100]; 776486d278SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 786486d278SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 796486d278SMatthias Ringwald static bd_addr_t local_addr; 806486d278SMatthias Ringwald 81f61339eaSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 82f61339eaSMatthias Ringwald static int main_argc; 83f61339eaSMatthias Ringwald static const char ** main_argv; 84f61339eaSMatthias Ringwald static const hci_transport_t * transport; 85948e8bfeSMatthias Ringwald static int intel_firmware_loaded; 86f61339eaSMatthias Ringwald 87f61339eaSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 88f61339eaSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 89f61339eaSMatthias Ringwald if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; 90f61339eaSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 916486d278SMatthias Ringwald gap_local_bd_addr(local_addr); 926486d278SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 936486d278SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 946486d278SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 956486d278SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 966486d278SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 976486d278SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 986486d278SMatthias Ringwald #ifdef ENABLE_CLASSIC 996486d278SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 1006486d278SMatthias Ringwald #endif 1016486d278SMatthias Ringwald #ifdef ENABLE_BLE 1026486d278SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 1036486d278SMatthias Ringwald #endif 104f61339eaSMatthias Ringwald } 105f61339eaSMatthias Ringwald 106f61339eaSMatthias Ringwald static void sigint_handler(int param){ 107f61339eaSMatthias Ringwald UNUSED(param); 108f61339eaSMatthias Ringwald 109f61339eaSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 110f61339eaSMatthias Ringwald log_info("sigint_handler: shutting down"); 111f61339eaSMatthias Ringwald 112f61339eaSMatthias Ringwald // reset anyway 113f61339eaSMatthias Ringwald btstack_stdin_reset(); 114f61339eaSMatthias Ringwald 115948e8bfeSMatthias Ringwald // power off and close only if hci was initialized before 116948e8bfeSMatthias Ringwald if (intel_firmware_loaded){ 117f61339eaSMatthias Ringwald hci_power_control( HCI_POWER_OFF); 118f61339eaSMatthias Ringwald hci_close(); 119948e8bfeSMatthias Ringwald } 120f61339eaSMatthias Ringwald exit(0); 121f61339eaSMatthias Ringwald } 122f61339eaSMatthias Ringwald 123f61339eaSMatthias Ringwald static int led_state = 0; 124f61339eaSMatthias Ringwald void hal_led_toggle(void){ 125f61339eaSMatthias Ringwald led_state = 1 - led_state; 126f61339eaSMatthias Ringwald printf("LED State %u\n", led_state); 127f61339eaSMatthias Ringwald } 128f61339eaSMatthias Ringwald 129f61339eaSMatthias Ringwald 130f61339eaSMatthias Ringwald static void intel_firmware_done(int result){ 131f61339eaSMatthias Ringwald 132f61339eaSMatthias Ringwald printf("Done %x\n", result); 133f61339eaSMatthias Ringwald 134948e8bfeSMatthias Ringwald intel_firmware_loaded = 1; 135948e8bfeSMatthias Ringwald 136f61339eaSMatthias Ringwald // init HCI 137f61339eaSMatthias Ringwald hci_init(transport, NULL); 138f61339eaSMatthias Ringwald 139f61339eaSMatthias Ringwald // inform about BTstack state 140f61339eaSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 141f61339eaSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 142f61339eaSMatthias Ringwald 143f61339eaSMatthias Ringwald // setup app 144f61339eaSMatthias Ringwald btstack_main(main_argc, main_argv); 145f61339eaSMatthias Ringwald } 146f61339eaSMatthias Ringwald 147f61339eaSMatthias Ringwald #define USB_MAX_PATH_LEN 7 148f61339eaSMatthias Ringwald int main(int argc, const char * argv[]){ 149f61339eaSMatthias Ringwald 150f61339eaSMatthias Ringwald // Prevent stdout buffering 151f61339eaSMatthias Ringwald setvbuf(stdout, NULL, _IONBF, 0); 152f61339eaSMatthias Ringwald 153f61339eaSMatthias Ringwald main_argc = argc; 154f61339eaSMatthias Ringwald main_argv = argv; 155f61339eaSMatthias Ringwald 156f61339eaSMatthias Ringwald printf("BTstack/windows-winusb booting up\n"); 157f61339eaSMatthias Ringwald 158f61339eaSMatthias Ringwald /// GET STARTED with BTstack /// 159f61339eaSMatthias Ringwald btstack_memory_init(); 160f61339eaSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 161f61339eaSMatthias Ringwald 1627435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 1637435ec7bSMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 1647435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 1657435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 1667435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 167f61339eaSMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 168f61339eaSMatthias Ringwald 169f61339eaSMatthias Ringwald // handle CTRL-c 170f61339eaSMatthias Ringwald signal(SIGINT, sigint_handler); 171f61339eaSMatthias Ringwald 172f61339eaSMatthias Ringwald // setup USB Transport 173f61339eaSMatthias Ringwald transport = hci_transport_usb_instance(); 174f61339eaSMatthias Ringwald btstack_chipset_intel_download_firmware(hci_transport_usb_instance(), &intel_firmware_done); 175f61339eaSMatthias Ringwald 176f61339eaSMatthias Ringwald // go 177f61339eaSMatthias Ringwald btstack_run_loop_execute(); 178f61339eaSMatthias Ringwald 179f61339eaSMatthias Ringwald return 0; 180f61339eaSMatthias Ringwald } 181