194a5538cSMatthias Ringwald /* 294a5538cSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 394a5538cSMatthias Ringwald * 494a5538cSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 594a5538cSMatthias Ringwald * modification, are permitted provided that the following conditions 694a5538cSMatthias Ringwald * are met: 794a5538cSMatthias Ringwald * 894a5538cSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 994a5538cSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1094a5538cSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1194a5538cSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1294a5538cSMatthias Ringwald * documentation and/or other materials provided with the distribution. 1394a5538cSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1494a5538cSMatthias Ringwald * contributors may be used to endorse or promote products derived 1594a5538cSMatthias Ringwald * from this software without specific prior written permission. 1694a5538cSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1794a5538cSMatthias Ringwald * personal benefit and not for any commercial purpose or for 1894a5538cSMatthias Ringwald * monetary gain. 1994a5538cSMatthias Ringwald * 2094a5538cSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2194a5538cSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2294a5538cSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2394a5538cSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2494a5538cSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2594a5538cSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2694a5538cSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2794a5538cSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2894a5538cSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2994a5538cSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3094a5538cSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3194a5538cSMatthias Ringwald * SUCH DAMAGE. 3294a5538cSMatthias Ringwald * 3394a5538cSMatthias Ringwald * Please inquire about commercial licensing options at 3494a5538cSMatthias Ringwald * [email protected] 3594a5538cSMatthias Ringwald * 3694a5538cSMatthias Ringwald */ 3794a5538cSMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39ab2c6ae4SMatthias Ringwald 4094a5538cSMatthias Ringwald // ***************************************************************************** 4194a5538cSMatthias Ringwald // 4294a5538cSMatthias Ringwald // minimal setup for HCI code 4394a5538cSMatthias Ringwald // 4494a5538cSMatthias Ringwald // ***************************************************************************** 4594a5538cSMatthias Ringwald 4694a5538cSMatthias Ringwald #include <stdint.h> 4794a5538cSMatthias Ringwald #include <stdio.h> 4894a5538cSMatthias Ringwald #include <stdlib.h> 4994a5538cSMatthias Ringwald #include <string.h> 5094a5538cSMatthias Ringwald #include <signal.h> 5194a5538cSMatthias Ringwald 5294a5538cSMatthias Ringwald #include "btstack_config.h" 5394a5538cSMatthias Ringwald 5494a5538cSMatthias Ringwald #include "btstack_debug.h" 5594a5538cSMatthias Ringwald #include "btstack_event.h" 5681862996SMatthias Ringwald #include "ble/le_device_db_tlv.h" 5781862996SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 5894a5538cSMatthias Ringwald #include "btstack_memory.h" 5994a5538cSMatthias Ringwald #include "btstack_run_loop.h" 6094a5538cSMatthias Ringwald #include "btstack_run_loop_posix.h" 613e05a31cSMatthias Ringwald #include "bluetooth_company_id.h" 6294a5538cSMatthias Ringwald #include "hci.h" 6394a5538cSMatthias Ringwald #include "hci_dump.h" 647ea7688aSMatthias Ringwald #include "btstack_stdin.h" 657daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h" 6694a5538cSMatthias Ringwald 6794a5538cSMatthias Ringwald #include "btstack_chipset_csr.h" 6894a5538cSMatthias Ringwald #include "btstack_chipset_cc256x.h" 6994a5538cSMatthias Ringwald #include "btstack_chipset_em9301.h" 7094a5538cSMatthias Ringwald #include "btstack_chipset_stlc2500d.h" 7194a5538cSMatthias Ringwald #include "btstack_chipset_tc3566x.h" 7294a5538cSMatthias Ringwald 7394a5538cSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 7494a5538cSMatthias Ringwald 757daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 767daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 777daa8bd9SMatthias Ringwald static char tlv_db_path[100]; 787daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 797daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 8091858f18SMatthias Ringwald 8194a5538cSMatthias Ringwald static hci_transport_config_uart_t config = { 8294a5538cSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 8394a5538cSMatthias Ringwald 115200, 8494a5538cSMatthias Ringwald 0, // main baudrate 8594a5538cSMatthias Ringwald 1, // flow control 8694a5538cSMatthias Ringwald NULL, 8794a5538cSMatthias Ringwald }; 8894a5538cSMatthias Ringwald 8994a5538cSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 9091858f18SMatthias Ringwald static void local_version_information_handler(uint8_t * packet); 9194a5538cSMatthias Ringwald 9294a5538cSMatthias Ringwald static void sigint_handler(int param){ 93b96c8f44SMatthias Ringwald UNUSED(param); 9494a5538cSMatthias Ringwald 95b96c8f44SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 96b96c8f44SMatthias Ringwald log_info("sigint_handler: shutting down"); 97b96c8f44SMatthias Ringwald 9894a5538cSMatthias Ringwald // reset anyway 9994a5538cSMatthias Ringwald btstack_stdin_reset(); 10094a5538cSMatthias Ringwald 101b96c8f44SMatthias Ringwald // power down 10294a5538cSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 10394a5538cSMatthias Ringwald hci_close(); 10494a5538cSMatthias Ringwald log_info("Good bye, see you.\n"); 10594a5538cSMatthias Ringwald exit(0); 10694a5538cSMatthias Ringwald } 10794a5538cSMatthias Ringwald 10894a5538cSMatthias Ringwald static int led_state = 0; 10994a5538cSMatthias Ringwald void hal_led_toggle(void){ 11094a5538cSMatthias Ringwald led_state = 1 - led_state; 11194a5538cSMatthias Ringwald printf("LED State %u\n", led_state); 11294a5538cSMatthias Ringwald } 11394a5538cSMatthias Ringwald static void use_fast_uart(void){ 114*a59ee665SMatthias Ringwald printf("Using 921600 baud.\n"); 115*a59ee665SMatthias Ringwald config.baudrate_main = 921600; 11694a5538cSMatthias Ringwald } 11794a5538cSMatthias Ringwald 11891858f18SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 11994a5538cSMatthias Ringwald printf("Local version information:\n"); 1205c2e0bddSMatthias Ringwald uint16_t hci_version = packet[6]; 1215c2e0bddSMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7); 1225c2e0bddSMatthias Ringwald uint16_t lmp_version = packet[9]; 12394a5538cSMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 12494a5538cSMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 12594a5538cSMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 12694a5538cSMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 12794a5538cSMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 12894a5538cSMatthias Ringwald printf("- LMP Revision 0x%04x\n", lmp_subversion); 12994a5538cSMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 13094a5538cSMatthias Ringwald switch (manufacturer){ 13161f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 13299dd7845SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision); 13394a5538cSMatthias Ringwald use_fast_uart(); 13494a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance()); 13594a5538cSMatthias Ringwald break; 13661f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC: 13794a5538cSMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n"); 13894a5538cSMatthias Ringwald use_fast_uart(); 13994a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance()); 14094a5538cSMatthias Ringwald break; 14161f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: 1427bc1dbabSMatthias Ringwald printf("Broadcom chipset. Not supported by posix-h5 port, please use port/posix-h5-bcm\n"); 1437bc1dbabSMatthias Ringwald exit(10); 14494a5538cSMatthias Ringwald break; 14561f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS: 14694a5538cSMatthias Ringwald printf("ST Microelectronics - using STLC2500d driver.\n"); 14794a5538cSMatthias Ringwald use_fast_uart(); 14894a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_stlc2500d_instance()); 14994a5538cSMatthias Ringwald break; 1503e05a31cSMatthias Ringwald case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA: 15194a5538cSMatthias Ringwald printf("EM Microelectronics - using EM9301 driver.\n"); 15294a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_em9301_instance()); 15394a5538cSMatthias Ringwald break; 15494a5538cSMatthias Ringwald default: 15594a5538cSMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n"); 15694a5538cSMatthias Ringwald break; 15794a5538cSMatthias Ringwald } 15894a5538cSMatthias Ringwald } 15994a5538cSMatthias Ringwald 16094a5538cSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 161cf524f2bSMatthias Ringwald bd_addr_t addr; 16294a5538cSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 16391858f18SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 16491858f18SMatthias Ringwald case BTSTACK_EVENT_STATE: 16591858f18SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 166cf524f2bSMatthias Ringwald gap_local_bd_addr(addr); 167cf524f2bSMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 1687daa8bd9SMatthias Ringwald // setup TLV 1697daa8bd9SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 1707daa8bd9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 1717daa8bd9SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 1727daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 1737daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 17481862996SMatthias Ringwald #ifdef ENABLE_CLASSIC 17581862996SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 17681862996SMatthias Ringwald #endif 17781862996SMatthias Ringwald #ifdef ENABLE_BLE 17881862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 17981862996SMatthias Ringwald #endif 18091858f18SMatthias Ringwald break; 18191858f18SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 18291858f18SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 18391858f18SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 18491858f18SMatthias Ringwald // terminate, name 248 chars 18591858f18SMatthias Ringwald packet[6+248] = 0; 18691858f18SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 18791858f18SMatthias Ringwald } 18891858f18SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 18991858f18SMatthias Ringwald local_version_information_handler(packet); 19091858f18SMatthias Ringwald } 19191858f18SMatthias Ringwald break; 19291858f18SMatthias Ringwald default: 19391858f18SMatthias Ringwald break; 19491858f18SMatthias Ringwald } 19591858f18SMatthias Ringwald } 19694a5538cSMatthias Ringwald 19794a5538cSMatthias Ringwald int main(int argc, const char * argv[]){ 19894a5538cSMatthias Ringwald 19994a5538cSMatthias Ringwald /// GET STARTED with BTstack /// 20094a5538cSMatthias Ringwald btstack_memory_init(); 20194a5538cSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 20294a5538cSMatthias Ringwald 20394a5538cSMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 204785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 205785879c6SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 206785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 20794a5538cSMatthias Ringwald 20894a5538cSMatthias Ringwald // pick serial port 209cf524f2bSMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT 210cf524f2bSMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A50285BI"; // BOOST-CC2564MODA New 211cf524f2bSMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A9OVNX5P"; // RedBear IoT pHAT breakout board 212cf524f2bSMatthias Ringwald config.device_name = "/dev/tty.usbserial-A900K0VK"; // CSR8811 breakout board 21394a5538cSMatthias Ringwald 214aa981441SMatthias Ringwald // accept path from command line 215aa981441SMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){ 216aa981441SMatthias Ringwald config.device_name = argv[2]; 217aa981441SMatthias Ringwald argc -= 2; 218aa981441SMatthias Ringwald memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *)); 219aa981441SMatthias Ringwald } 220aa981441SMatthias Ringwald printf("H5 device: %s\n", config.device_name); 221aa981441SMatthias Ringwald 22294a5538cSMatthias Ringwald // init HCI 22394a5538cSMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance(); 22494a5538cSMatthias Ringwald const hci_transport_t * transport = hci_transport_h5_instance(uart_driver); 22594a5538cSMatthias Ringwald hci_init(transport, (void*) &config); 22694a5538cSMatthias Ringwald 227daa2e90cSMatthias Ringwald // enable BCSP mode for CSR chipsets - auto detect might not work 228daa2e90cSMatthias Ringwald // hci_transport_h5_enable_bcsp_mode(); 229daa2e90cSMatthias Ringwald 23077b95b41SMatthias Ringwald // set BD_ADDR for CSR without Flash/unique address 23177b95b41SMatthias Ringwald // bd_addr_t own_address = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 23277b95b41SMatthias Ringwald // btstack_chipset_csr_set_bd_addr(own_address); 23377b95b41SMatthias Ringwald 2344b871b9eSMatthias Ringwald // enable auto-sleep mode 2354b871b9eSMatthias Ringwald // hci_transport_h5_set_auto_sleep(300); 2364b871b9eSMatthias Ringwald 23794a5538cSMatthias Ringwald // register for HCI events 23894a5538cSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 23994a5538cSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 24094a5538cSMatthias Ringwald 24194a5538cSMatthias Ringwald // handle CTRL-c 24294a5538cSMatthias Ringwald signal(SIGINT, sigint_handler); 24394a5538cSMatthias Ringwald 24494a5538cSMatthias Ringwald // setup app 24594a5538cSMatthias Ringwald btstack_main(argc, argv); 24694a5538cSMatthias Ringwald 24794a5538cSMatthias Ringwald // go 24894a5538cSMatthias Ringwald btstack_run_loop_execute(); 24994a5538cSMatthias Ringwald 25094a5538cSMatthias Ringwald return 0; 25194a5538cSMatthias Ringwald } 252