1ab2c6ae4SMatthias Ringwald /* 2ab2c6ae4SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3ab2c6ae4SMatthias Ringwald * 4ab2c6ae4SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5ab2c6ae4SMatthias Ringwald * modification, are permitted provided that the following conditions 6ab2c6ae4SMatthias Ringwald * are met: 7ab2c6ae4SMatthias Ringwald * 8ab2c6ae4SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9ab2c6ae4SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10ab2c6ae4SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11ab2c6ae4SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12ab2c6ae4SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13ab2c6ae4SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14ab2c6ae4SMatthias Ringwald * contributors may be used to endorse or promote products derived 15ab2c6ae4SMatthias Ringwald * from this software without specific prior written permission. 16ab2c6ae4SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17ab2c6ae4SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18ab2c6ae4SMatthias Ringwald * monetary gain. 19ab2c6ae4SMatthias Ringwald * 20ab2c6ae4SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21ab2c6ae4SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22ab2c6ae4SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25ab2c6ae4SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26ab2c6ae4SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27ab2c6ae4SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28ab2c6ae4SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29ab2c6ae4SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30ab2c6ae4SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ab2c6ae4SMatthias Ringwald * SUCH DAMAGE. 32ab2c6ae4SMatthias Ringwald * 33ab2c6ae4SMatthias Ringwald * Please inquire about commercial licensing options at 34ab2c6ae4SMatthias Ringwald * [email protected] 35ab2c6ae4SMatthias Ringwald * 36ab2c6ae4SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38d5b02eadSMatthias Ringwald #define BTSTACK_FILE__ "main.c" 39ab2c6ae4SMatthias Ringwald 404e630824SMatthias Ringwald #include <stdint.h> 414e630824SMatthias Ringwald #include <stdio.h> 424e630824SMatthias Ringwald #include <stdlib.h> 434e630824SMatthias Ringwald #include <string.h> 444e630824SMatthias Ringwald 454e630824SMatthias Ringwald #include "btstack_config.h" 464e630824SMatthias Ringwald 47d5b02eadSMatthias Ringwald #include "ble/le_device_db_tlv.h" 48d5b02eadSMatthias Ringwald #include "bluetooth_company_id.h" 49d5b02eadSMatthias Ringwald #include "btstack_chipset_bcm.h" 50d5b02eadSMatthias Ringwald #include "btstack_chipset_cc256x.h" 51d5b02eadSMatthias Ringwald #include "btstack_chipset_csr.h" 52d5b02eadSMatthias Ringwald #include "btstack_chipset_em9301.h" 53d5b02eadSMatthias Ringwald #include "btstack_chipset_stlc2500d.h" 54d5b02eadSMatthias Ringwald #include "btstack_chipset_tc3566x.h" 554e630824SMatthias Ringwald #include "btstack_debug.h" 564e630824SMatthias Ringwald #include "btstack_event.h" 574e630824SMatthias Ringwald #include "btstack_memory.h" 584e630824SMatthias Ringwald #include "btstack_run_loop.h" 594e630824SMatthias Ringwald #include "btstack_run_loop_windows.h" 60d5b02eadSMatthias Ringwald #include "btstack_stdin.h" 61d5b02eadSMatthias Ringwald #include "btstack_stdin_windows.h" 623086bcceSMatthias Ringwald #include "btstack_tlv_windows.h" 63d5b02eadSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 64d5b02eadSMatthias Ringwald #include "hal_led.h" 654e630824SMatthias Ringwald #include "hci.h" 664e630824SMatthias Ringwald #include "hci_dump.h" 673086bcceSMatthias Ringwald #include "hci_dump_windows_fs.h" 68c8dfe071SMatthias Ringwald #include "hci_transport.h" 69c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 70422979b1SMatthias Ringwald 7127ff675bSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 72634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet); 7327ff675bSMatthias Ringwald 74422979b1SMatthias Ringwald static hci_transport_config_uart_t config = { 75422979b1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 76422979b1SMatthias Ringwald 115200, 77422979b1SMatthias Ringwald 0, // main baudrate 78422979b1SMatthias Ringwald 1, // flow control 79422979b1SMatthias Ringwald NULL, 80422979b1SMatthias Ringwald }; 81422979b1SMatthias Ringwald 82422979b1SMatthias Ringwald int is_bcm; 83422979b1SMatthias Ringwald 844e630824SMatthias Ringwald static int led_state = 0; 85422979b1SMatthias Ringwald 866486d278SMatthias Ringwald #define TLV_DB_PATH_PREFIX "btstack_" 876486d278SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 886486d278SMatthias Ringwald static char tlv_db_path[100]; 896486d278SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 903086bcceSMatthias Ringwald static btstack_tlv_windows_t tlv_context; 916486d278SMatthias Ringwald static bd_addr_t local_addr; 92d5b02eadSMatthias Ringwald static bool shutdown_triggered; 936486d278SMatthias Ringwald 944e630824SMatthias Ringwald void hal_led_toggle(void){ 954e630824SMatthias Ringwald led_state = 1 - led_state; 964e630824SMatthias Ringwald printf("LED State %u\n", led_state); 974e630824SMatthias Ringwald } 984e630824SMatthias Ringwald 99d5b02eadSMatthias Ringwald static void trigger_shutdown(void){ 100d5b02eadSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 101b96c8f44SMatthias Ringwald log_info("sigint_handler: shutting down"); 102d5b02eadSMatthias Ringwald shutdown_triggered = true; 103b96c8f44SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 10427ff675bSMatthias Ringwald } 10527ff675bSMatthias Ringwald 106422979b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 107422979b1SMatthias Ringwald 108422979b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 109422979b1SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 110422979b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 111422979b1SMatthias Ringwald case BTSTACK_EVENT_STATE: 112d5b02eadSMatthias Ringwald switch (btstack_event_state_get_state(packet)){ 113d5b02eadSMatthias Ringwald case HCI_STATE_WORKING: 114d5b02eadSMatthias Ringwald gap_local_bd_addr(local_addr); 1156486d278SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 11654736c11SMatthias Ringwald btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 11754736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-')); 11854736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 1193086bcceSMatthias Ringwald tlv_impl = btstack_tlv_windows_init_instance(&tlv_context, tlv_db_path); 1206486d278SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 1216486d278SMatthias Ringwald #ifdef ENABLE_CLASSIC 1226486d278SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 1236486d278SMatthias Ringwald #endif 1246486d278SMatthias Ringwald #ifdef ENABLE_BLE 1256486d278SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 1266486d278SMatthias Ringwald #endif 127d5b02eadSMatthias Ringwald break; 128d5b02eadSMatthias Ringwald case HCI_STATE_OFF: 1293086bcceSMatthias Ringwald btstack_tlv_windows_deinit(&tlv_context); 130d5b02eadSMatthias Ringwald if (!shutdown_triggered) break; 131d5b02eadSMatthias Ringwald // reset stdin 132d5b02eadSMatthias Ringwald btstack_stdin_reset(); 133d5b02eadSMatthias Ringwald log_info("Good bye, see you.\n"); 134d5b02eadSMatthias Ringwald exit(0); 135d5b02eadSMatthias Ringwald break; 136d5b02eadSMatthias Ringwald default: 137d5b02eadSMatthias Ringwald break; 138d5b02eadSMatthias Ringwald } 139422979b1SMatthias Ringwald break; 140422979b1SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 141*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){ 142cf524f2bSMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 143422979b1SMatthias Ringwald // terminate, name 248 chars 144422979b1SMatthias Ringwald packet[6+248] = 0; 145422979b1SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 146422979b1SMatthias Ringwald if (is_bcm){ 147422979b1SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 148422979b1SMatthias Ringwald } 149422979b1SMatthias Ringwald } 150*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 151634a9874SMatthias Ringwald local_version_information_handler(packet); 152634a9874SMatthias Ringwald } 153422979b1SMatthias Ringwald break; 154422979b1SMatthias Ringwald default: 155422979b1SMatthias Ringwald break; 156422979b1SMatthias Ringwald } 157422979b1SMatthias Ringwald } 158422979b1SMatthias Ringwald static void use_fast_uart(void){ 159422979b1SMatthias Ringwald printf("Using 921600 baud.\n"); 160422979b1SMatthias Ringwald config.baudrate_main = 921600; 161422979b1SMatthias Ringwald } 162422979b1SMatthias Ringwald 163634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 164422979b1SMatthias Ringwald printf("Local version information:\n"); 1655c2e0bddSMatthias Ringwald uint16_t hci_version = packet[6]; 1665c2e0bddSMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7); 1675c2e0bddSMatthias Ringwald uint16_t lmp_version = packet[9]; 168422979b1SMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 169422979b1SMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 170422979b1SMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 171422979b1SMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 172422979b1SMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 173422979b1SMatthias Ringwald printf("- LMP Revision 0x%04x\n", lmp_subversion); 174422979b1SMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 175422979b1SMatthias Ringwald switch (manufacturer){ 17661f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 17799dd7845SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision); 178422979b1SMatthias Ringwald use_fast_uart(); 179422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance()); 180422979b1SMatthias Ringwald break; 18161f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC: 182422979b1SMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n"); 183c7e2dea5SMatthias Ringwald if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){ 184c7e2dea5SMatthias Ringwald printf("Error: LMP Subversion does not match initscript!"); 185c7e2dea5SMatthias Ringwald printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer"); 186c7e2dea5SMatthias Ringwald printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n"); 187c7e2dea5SMatthias Ringwald exit(10); 188c7e2dea5SMatthias Ringwald } 189422979b1SMatthias Ringwald use_fast_uart(); 190422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 191422979b1SMatthias Ringwald #ifdef ENABLE_EHCILL 192422979b1SMatthias Ringwald printf("eHCILL enabled.\n"); 193422979b1SMatthias Ringwald #else 194422979b1SMatthias Ringwald printf("eHCILL disable.\n"); 195422979b1SMatthias Ringwald #endif 196422979b1SMatthias Ringwald break; 19761f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: 198422979b1SMatthias Ringwald printf("Broadcom - using BCM driver.\n"); 199422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 200422979b1SMatthias Ringwald use_fast_uart(); 201422979b1SMatthias Ringwald is_bcm = 1; 202422979b1SMatthias Ringwald break; 20361f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS: 204422979b1SMatthias Ringwald printf("ST Microelectronics - using STLC2500d driver.\n"); 205422979b1SMatthias Ringwald use_fast_uart(); 206422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_stlc2500d_instance()); 207422979b1SMatthias Ringwald break; 208fbae2915SMatthias Ringwald case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA: 209422979b1SMatthias Ringwald printf("EM Microelectronics - using EM9301 driver.\n"); 210422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_em9301_instance()); 211422979b1SMatthias Ringwald break; 21261f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA: 213422979b1SMatthias Ringwald printf("Nordic Semiconductor nRF5 chipset.\n"); 214422979b1SMatthias Ringwald break; 215c5541f68SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP: 216c5541f68SMatthias Ringwald printf("Toshiba - using TC3566x driver.\n"); 217c5541f68SMatthias Ringwald hci_set_chipset(btstack_chipset_tc3566x_instance()); 218c5541f68SMatthias Ringwald use_fast_uart(); 219c5541f68SMatthias Ringwald break; 220422979b1SMatthias Ringwald default: 221422979b1SMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n"); 222422979b1SMatthias Ringwald break; 223422979b1SMatthias Ringwald } 224422979b1SMatthias Ringwald } 225422979b1SMatthias Ringwald 22627ff675bSMatthias Ringwald int main(int argc, const char * argv[]){ 2274e630824SMatthias Ringwald printf("BTstack on windows booting up\n"); 2284e630824SMatthias Ringwald 2294e630824SMatthias Ringwald /// GET STARTED with BTstack /// 2304e630824SMatthias Ringwald btstack_memory_init(); 2314e630824SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 2324e630824SMatthias Ringwald 2337435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 23434c6d5e0SMatthias Ringwald const char * pklg_path = "hci_dump.pklg"; 2353086bcceSMatthias Ringwald hci_dump_windows_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 2363086bcceSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_windows_fs_get_instance(); 2377435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 2387435ec7bSMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 239422979b1SMatthias Ringwald 240422979b1SMatthias Ringwald // pick serial port 241422979b1SMatthias Ringwald config.device_name = "\\\\.\\COM7"; 242422979b1SMatthias Ringwald 243aa981441SMatthias Ringwald // accept path from command line 244aa981441SMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){ 245aa981441SMatthias Ringwald config.device_name = argv[2]; 246aa981441SMatthias Ringwald argc -= 2; 2470033ffe4SMatthias Ringwald memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *)); 248aa981441SMatthias Ringwald } 249aa981441SMatthias Ringwald printf("H4 device: %s\n", config.device_name); 250aa981441SMatthias Ringwald 251422979b1SMatthias Ringwald // init HCI 252422979b1SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance(); 253422979b1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 254422979b1SMatthias Ringwald hci_init(transport, (void*) &config); 255422979b1SMatthias Ringwald 256422979b1SMatthias Ringwald // inform about BTstack state 257422979b1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 258422979b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 259422979b1SMatthias Ringwald 260d5b02eadSMatthias Ringwald // setup stdin to handle CTRL-c 261d5b02eadSMatthias Ringwald btstack_stdin_windows_init(); 262d5b02eadSMatthias Ringwald btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown); 26327ff675bSMatthias Ringwald 26427ff675bSMatthias Ringwald // setup app 26527ff675bSMatthias Ringwald btstack_main(argc, argv); 26627ff675bSMatthias Ringwald 26727ff675bSMatthias Ringwald // go 26827ff675bSMatthias Ringwald btstack_run_loop_execute(); 26927ff675bSMatthias Ringwald 2704e630824SMatthias Ringwald return 0; 2714e630824SMatthias Ringwald } 272