1*ab2c6ae4SMatthias Ringwald /* 2*ab2c6ae4SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*ab2c6ae4SMatthias Ringwald * 4*ab2c6ae4SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*ab2c6ae4SMatthias Ringwald * modification, are permitted provided that the following conditions 6*ab2c6ae4SMatthias Ringwald * are met: 7*ab2c6ae4SMatthias Ringwald * 8*ab2c6ae4SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*ab2c6ae4SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*ab2c6ae4SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*ab2c6ae4SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*ab2c6ae4SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*ab2c6ae4SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*ab2c6ae4SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*ab2c6ae4SMatthias Ringwald * from this software without specific prior written permission. 16*ab2c6ae4SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*ab2c6ae4SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*ab2c6ae4SMatthias Ringwald * monetary gain. 19*ab2c6ae4SMatthias Ringwald * 20*ab2c6ae4SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*ab2c6ae4SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*ab2c6ae4SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*ab2c6ae4SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*ab2c6ae4SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*ab2c6ae4SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*ab2c6ae4SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*ab2c6ae4SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*ab2c6ae4SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*ab2c6ae4SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*ab2c6ae4SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*ab2c6ae4SMatthias Ringwald * SUCH DAMAGE. 32*ab2c6ae4SMatthias Ringwald * 33*ab2c6ae4SMatthias Ringwald * Please inquire about commercial licensing options at 34*ab2c6ae4SMatthias Ringwald * [email protected] 35*ab2c6ae4SMatthias Ringwald * 36*ab2c6ae4SMatthias Ringwald */ 37*ab2c6ae4SMatthias Ringwald 38*ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39*ab2c6ae4SMatthias Ringwald 404e630824SMatthias Ringwald #include <stdint.h> 414e630824SMatthias Ringwald #include <stdio.h> 424e630824SMatthias Ringwald #include <stdlib.h> 434e630824SMatthias Ringwald #include <string.h> 444e630824SMatthias Ringwald #include <signal.h> 454e630824SMatthias Ringwald 464e630824SMatthias Ringwald #include "btstack_config.h" 474e630824SMatthias Ringwald 484e630824SMatthias Ringwald #include "btstack_debug.h" 494e630824SMatthias Ringwald #include "btstack_event.h" 504e630824SMatthias Ringwald #include "btstack_memory.h" 514e630824SMatthias Ringwald #include "btstack_run_loop.h" 524e630824SMatthias Ringwald #include "btstack_run_loop_windows.h" 534e630824SMatthias Ringwald #include "hci.h" 544e630824SMatthias Ringwald #include "hci_dump.h" 554e630824SMatthias Ringwald #include "hal_led.h" 56422979b1SMatthias Ringwald #include "btstack_link_key_db_fs.h" 57422979b1SMatthias Ringwald 58341c09eeSMatthias Ringwald #include "stdin_support.h" 594e630824SMatthias Ringwald 60422979b1SMatthias Ringwald #include "btstack_chipset_bcm.h" 61422979b1SMatthias Ringwald #include "btstack_chipset_csr.h" 62422979b1SMatthias Ringwald #include "btstack_chipset_cc256x.h" 63422979b1SMatthias Ringwald #include "btstack_chipset_em9301.h" 64422979b1SMatthias Ringwald #include "btstack_chipset_stlc2500d.h" 65422979b1SMatthias Ringwald #include "btstack_chipset_tc3566x.h" 66422979b1SMatthias Ringwald 6727ff675bSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 68634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet); 6927ff675bSMatthias Ringwald 70422979b1SMatthias Ringwald static hci_transport_config_uart_t config = { 71422979b1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 72422979b1SMatthias Ringwald 115200, 73422979b1SMatthias Ringwald 0, // main baudrate 74422979b1SMatthias Ringwald 1, // flow control 75422979b1SMatthias Ringwald NULL, 76422979b1SMatthias Ringwald }; 77422979b1SMatthias Ringwald 78422979b1SMatthias Ringwald int is_bcm; 79422979b1SMatthias Ringwald 804e630824SMatthias Ringwald static int led_state = 0; 81422979b1SMatthias Ringwald 824e630824SMatthias Ringwald void hal_led_toggle(void){ 834e630824SMatthias Ringwald led_state = 1 - led_state; 844e630824SMatthias Ringwald printf("LED State %u\n", led_state); 854e630824SMatthias Ringwald } 864e630824SMatthias Ringwald 8727ff675bSMatthias Ringwald static void sigint_handler(int param){ 88b96c8f44SMatthias Ringwald UNUSED(param); 8927ff675bSMatthias Ringwald 90b96c8f44SMatthias Ringwald printf("CTRL-C = SIGINT received, shutting down..\n"); 91b96c8f44SMatthias Ringwald log_info("sigint_handler: shutting down"); 92b96c8f44SMatthias Ringwald 93b96c8f44SMatthias Ringwald // reset anyway 9427ff675bSMatthias Ringwald btstack_stdin_reset(); 95341c09eeSMatthias Ringwald 96b96c8f44SMatthias Ringwald // power down 97b96c8f44SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 98b96c8f44SMatthias Ringwald hci_close(); 9927ff675bSMatthias Ringwald log_info("Good bye, see you.\n"); 10027ff675bSMatthias Ringwald exit(0); 10127ff675bSMatthias Ringwald } 10227ff675bSMatthias Ringwald 103422979b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 104422979b1SMatthias Ringwald 105422979b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 106422979b1SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 107422979b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 108422979b1SMatthias Ringwald case BTSTACK_EVENT_STATE: 109422979b1SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 110422979b1SMatthias Ringwald printf("BTstack up and running.\n"); 111422979b1SMatthias Ringwald break; 112422979b1SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 113422979b1SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 114422979b1SMatthias Ringwald // terminate, name 248 chars 115422979b1SMatthias Ringwald packet[6+248] = 0; 116422979b1SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 117422979b1SMatthias Ringwald if (is_bcm){ 118422979b1SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 119422979b1SMatthias Ringwald } 120422979b1SMatthias Ringwald } 121634a9874SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 122634a9874SMatthias Ringwald local_version_information_handler(packet); 123634a9874SMatthias Ringwald } 124422979b1SMatthias Ringwald break; 125634a9874SMatthias Ringwald 126422979b1SMatthias Ringwald default: 127422979b1SMatthias Ringwald break; 128422979b1SMatthias Ringwald } 129422979b1SMatthias Ringwald } 130422979b1SMatthias Ringwald 131422979b1SMatthias Ringwald static void use_fast_uart(void){ 132422979b1SMatthias Ringwald printf("Using 921600 baud.\n"); 133422979b1SMatthias Ringwald config.baudrate_main = 921600; 134422979b1SMatthias Ringwald } 135422979b1SMatthias Ringwald 136634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 137422979b1SMatthias Ringwald printf("Local version information:\n"); 138422979b1SMatthias Ringwald uint16_t hci_version = little_endian_read_16(packet, 4); 139422979b1SMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 6); 140422979b1SMatthias Ringwald uint16_t lmp_version = little_endian_read_16(packet, 8); 141422979b1SMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 142422979b1SMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 143422979b1SMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 144422979b1SMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 145422979b1SMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 146422979b1SMatthias Ringwald printf("- LMP Revision 0x%04x\n", lmp_subversion); 147422979b1SMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 148422979b1SMatthias Ringwald switch (manufacturer){ 149422979b1SMatthias Ringwald case COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 150422979b1SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset.\n"); 151422979b1SMatthias Ringwald use_fast_uart(); 152422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance()); 153422979b1SMatthias Ringwald break; 154422979b1SMatthias Ringwald case COMPANY_ID_TEXAS_INSTRUMENTS_INC: 155422979b1SMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n"); 156c7e2dea5SMatthias Ringwald if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){ 157c7e2dea5SMatthias Ringwald printf("Error: LMP Subversion does not match initscript!"); 158c7e2dea5SMatthias Ringwald printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer"); 159c7e2dea5SMatthias Ringwald printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n"); 160c7e2dea5SMatthias Ringwald exit(10); 161c7e2dea5SMatthias Ringwald } 162422979b1SMatthias Ringwald use_fast_uart(); 163422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 164422979b1SMatthias Ringwald #ifdef ENABLE_EHCILL 165422979b1SMatthias Ringwald printf("eHCILL enabled.\n"); 166422979b1SMatthias Ringwald #else 167422979b1SMatthias Ringwald printf("eHCILL disable.\n"); 168422979b1SMatthias Ringwald #endif 169422979b1SMatthias Ringwald break; 170422979b1SMatthias Ringwald case COMPANY_ID_BROADCOM_CORPORATION: 171422979b1SMatthias Ringwald printf("Broadcom - using BCM driver.\n"); 172422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 173422979b1SMatthias Ringwald use_fast_uart(); 174422979b1SMatthias Ringwald is_bcm = 1; 175422979b1SMatthias Ringwald break; 176422979b1SMatthias Ringwald case COMPANY_ID_ST_MICROELECTRONICS: 177422979b1SMatthias Ringwald printf("ST Microelectronics - using STLC2500d driver.\n"); 178422979b1SMatthias Ringwald use_fast_uart(); 179422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_stlc2500d_instance()); 180422979b1SMatthias Ringwald break; 181422979b1SMatthias Ringwald case COMPANY_ID_EM_MICROELECTRONICS_MARIN: 182422979b1SMatthias Ringwald printf("EM Microelectronics - using EM9301 driver.\n"); 183422979b1SMatthias Ringwald hci_set_chipset(btstack_chipset_em9301_instance()); 184422979b1SMatthias Ringwald break; 185422979b1SMatthias Ringwald case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA: 186422979b1SMatthias Ringwald printf("Nordic Semiconductor nRF5 chipset.\n"); 187422979b1SMatthias Ringwald break; 188422979b1SMatthias Ringwald default: 189422979b1SMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n"); 190422979b1SMatthias Ringwald break; 191422979b1SMatthias Ringwald } 192422979b1SMatthias Ringwald } 193422979b1SMatthias Ringwald 19427ff675bSMatthias Ringwald int main(int argc, const char * argv[]){ 1954e630824SMatthias Ringwald printf("BTstack on windows booting up\n"); 1964e630824SMatthias Ringwald 1974e630824SMatthias Ringwald /// GET STARTED with BTstack /// 1984e630824SMatthias Ringwald btstack_memory_init(); 1994e630824SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 2004e630824SMatthias Ringwald 2015096a6aeSMatthias Ringwald hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER); 202422979b1SMatthias Ringwald 203422979b1SMatthias Ringwald // pick serial port 204422979b1SMatthias Ringwald config.device_name = "\\\\.\\COM7"; 205422979b1SMatthias Ringwald 206422979b1SMatthias Ringwald // init HCI 207422979b1SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance(); 208422979b1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 209422979b1SMatthias Ringwald const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 210422979b1SMatthias Ringwald hci_init(transport, (void*) &config); 211422979b1SMatthias Ringwald hci_set_link_key_db(link_key_db); 212422979b1SMatthias Ringwald 213422979b1SMatthias Ringwald // inform about BTstack state 214422979b1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 215422979b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 216422979b1SMatthias Ringwald 21727ff675bSMatthias Ringwald // handle CTRL-c 21827ff675bSMatthias Ringwald signal(SIGINT, sigint_handler); 21927ff675bSMatthias Ringwald 22027ff675bSMatthias Ringwald // setup app 22127ff675bSMatthias Ringwald btstack_main(argc, argv); 22227ff675bSMatthias Ringwald 22327ff675bSMatthias Ringwald // go 22427ff675bSMatthias Ringwald btstack_run_loop_execute(); 22527ff675bSMatthias Ringwald 2264e630824SMatthias Ringwald return 0; 2274e630824SMatthias Ringwald } 228