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" 65*7435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 66f61339eaSMatthias Ringwald #include "btstack_stdin.h" 67f61339eaSMatthias Ringwald #include "btstack_chipset_intel_firmware.h" 68f61339eaSMatthias Ringwald 69f61339eaSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 70f61339eaSMatthias Ringwald 716486d278SMatthias Ringwald 726486d278SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack_" 736486d278SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 746486d278SMatthias Ringwald static char tlv_db_path[100]; 756486d278SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 766486d278SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 776486d278SMatthias Ringwald static bd_addr_t local_addr; 786486d278SMatthias Ringwald 79f61339eaSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 80f61339eaSMatthias Ringwald static int main_argc; 81f61339eaSMatthias Ringwald static const char ** main_argv; 82f61339eaSMatthias Ringwald static const hci_transport_t * transport; 83948e8bfeSMatthias Ringwald static int intel_firmware_loaded; 84f61339eaSMatthias Ringwald 85f61339eaSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 86f61339eaSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 87f61339eaSMatthias Ringwald if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; 88f61339eaSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 896486d278SMatthias Ringwald gap_local_bd_addr(local_addr); 906486d278SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 916486d278SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 926486d278SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 936486d278SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 946486d278SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 956486d278SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 966486d278SMatthias Ringwald #ifdef ENABLE_CLASSIC 976486d278SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 986486d278SMatthias Ringwald #endif 996486d278SMatthias Ringwald #ifdef ENABLE_BLE 1006486d278SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 1016486d278SMatthias Ringwald #endif 102f61339eaSMatthias Ringwald } 103f61339eaSMatthias Ringwald 104f61339eaSMatthias Ringwald static void sigint_handler(int param){ 105f61339eaSMatthias Ringwald UNUSED(param); 106f61339eaSMatthias Ringwald 107f61339eaSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 108f61339eaSMatthias Ringwald log_info("sigint_handler: shutting down"); 109f61339eaSMatthias Ringwald 110f61339eaSMatthias Ringwald // reset anyway 111f61339eaSMatthias Ringwald btstack_stdin_reset(); 112f61339eaSMatthias Ringwald 113948e8bfeSMatthias Ringwald // power off and close only if hci was initialized before 114948e8bfeSMatthias Ringwald if (intel_firmware_loaded){ 115f61339eaSMatthias Ringwald hci_power_control( HCI_POWER_OFF); 116f61339eaSMatthias Ringwald hci_close(); 117948e8bfeSMatthias Ringwald } 118f61339eaSMatthias Ringwald exit(0); 119f61339eaSMatthias Ringwald } 120f61339eaSMatthias Ringwald 121f61339eaSMatthias Ringwald static int led_state = 0; 122f61339eaSMatthias Ringwald void hal_led_toggle(void){ 123f61339eaSMatthias Ringwald led_state = 1 - led_state; 124f61339eaSMatthias Ringwald printf("LED State %u\n", led_state); 125f61339eaSMatthias Ringwald } 126f61339eaSMatthias Ringwald 127f61339eaSMatthias Ringwald 128f61339eaSMatthias Ringwald static void intel_firmware_done(int result){ 129f61339eaSMatthias Ringwald 130f61339eaSMatthias Ringwald printf("Done %x\n", result); 131f61339eaSMatthias Ringwald 132948e8bfeSMatthias Ringwald intel_firmware_loaded = 1; 133948e8bfeSMatthias Ringwald 134f61339eaSMatthias Ringwald // init HCI 135f61339eaSMatthias Ringwald hci_init(transport, NULL); 136f61339eaSMatthias Ringwald 137f61339eaSMatthias Ringwald // inform about BTstack state 138f61339eaSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 139f61339eaSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 140f61339eaSMatthias Ringwald 141f61339eaSMatthias Ringwald // setup app 142f61339eaSMatthias Ringwald btstack_main(main_argc, main_argv); 143f61339eaSMatthias Ringwald } 144f61339eaSMatthias Ringwald 145f61339eaSMatthias Ringwald #define USB_MAX_PATH_LEN 7 146f61339eaSMatthias Ringwald int main(int argc, const char * argv[]){ 147f61339eaSMatthias Ringwald 148f61339eaSMatthias Ringwald // Prevent stdout buffering 149f61339eaSMatthias Ringwald setvbuf(stdout, NULL, _IONBF, 0); 150f61339eaSMatthias Ringwald 151f61339eaSMatthias Ringwald main_argc = argc; 152f61339eaSMatthias Ringwald main_argv = argv; 153f61339eaSMatthias Ringwald 154f61339eaSMatthias Ringwald printf("BTstack/windows-winusb booting up\n"); 155f61339eaSMatthias Ringwald 156f61339eaSMatthias Ringwald /// GET STARTED with BTstack /// 157f61339eaSMatthias Ringwald btstack_memory_init(); 158f61339eaSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 159f61339eaSMatthias Ringwald 160*7435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 161*7435ec7bSMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 162*7435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 163*7435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 164*7435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 165f61339eaSMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 166f61339eaSMatthias Ringwald 167f61339eaSMatthias Ringwald // handle CTRL-c 168f61339eaSMatthias Ringwald signal(SIGINT, sigint_handler); 169f61339eaSMatthias Ringwald 170f61339eaSMatthias Ringwald // setup USB Transport 171f61339eaSMatthias Ringwald transport = hci_transport_usb_instance(); 172f61339eaSMatthias Ringwald btstack_chipset_intel_download_firmware(hci_transport_usb_instance(), &intel_firmware_done); 173f61339eaSMatthias Ringwald 174f61339eaSMatthias Ringwald // go 175f61339eaSMatthias Ringwald btstack_run_loop_execute(); 176f61339eaSMatthias Ringwald 177f61339eaSMatthias Ringwald return 0; 178f61339eaSMatthias Ringwald } 179